1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/src/profilelocker.cpp

100 lines
2.2 KiB
C++
Raw Normal View History

2015-04-25 00:53:19 +08:00
#include "profilelocker.h"
#include "src/misc/settings.h"
#include <QDir>
#include <QDebug>
using namespace std;
unique_ptr<QLockFile> ProfileLocker::lockfile;
QString ProfileLocker::curLockName;
QString ProfileLocker::lockPathFromName(const QString& name)
{
return Settings::getInstance().getSettingsDirPath()+'/'+name+".lock";
}
bool ProfileLocker::isLockable(QString profile)
{
// If we already have the lock, it's definitely lockable
if (lockfile && curLockName == profile)
return true;
QLockFile newLock(lockPathFromName(profile));
return newLock.tryLock();
}
bool ProfileLocker::lock(QString profile)
{
if (lockfile && curLockName == profile)
return true;
QLockFile* newLock = new QLockFile(lockPathFromName(profile));
if (!newLock->tryLock())
{
delete newLock;
return false;
}
unlock();
lockfile.reset(newLock);
curLockName = profile;
return true;
}
void ProfileLocker::unlock()
{
if (!lockfile)
return;
lockfile->unlock();
delete lockfile.release();
lockfile = nullptr;
curLockName.clear();
}
void ProfileLocker::clearAllLocks()
{
qDebug() << "clearAllLocks: Wiping out all lock files";
2015-04-25 00:53:19 +08:00
if (lockfile)
unlock();
QDir dir(Settings::getInstance().getSettingsDirPath());
dir.setFilter(QDir::Files);
dir.setNameFilters({"*.lock"});
QFileInfoList files = dir.entryInfoList();
for (QFileInfo fileInfo : files)
{
QFile file(fileInfo.absoluteFilePath());
file.remove();
}
}
void ProfileLocker::assertLock()
{
if (!lockfile)
{
qCritical() << "assertLock: We don't seem to own any lock!";
deathByBrokenLock();
}
if (!QFile(lockPathFromName(curLockName)).exists())
{
QString tmp = curLockName;
unlock();
if (lock(tmp))
{
qCritical() << "assertLock: Lock file was lost, but could be restored";
}
else
{
qCritical() << "assertLock: Lock file was lost, and could *NOT* be restored";
deathByBrokenLock();
}
}
}
void ProfileLocker::deathByBrokenLock()
{
qCritical() << "Lock is *BROKEN*, exiting immediately";
abort();
}