qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
profilelocker.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2015-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 #include "profilelocker.h"
23 #include <QDebug>
24 #include <QDir>
25 
34 using namespace std;
35 
36 unique_ptr<QLockFile> ProfileLocker::lockfile;
38 
39 QString ProfileLocker::lockPathFromName(const QString& name)
40 {
41  return Settings::getInstance().getPaths().getSettingsDirPath() + '/' + name + ".lock";
42 }
43 
52 bool ProfileLocker::isLockable(QString profile)
53 {
54  // If we already have the lock, it's definitely lockable
55  if (lockfile && curLockName == profile)
56  return true;
57 
58  QLockFile newLock(lockPathFromName(profile));
59  return newLock.tryLock();
60 }
61 
67 bool ProfileLocker::lock(QString profile)
68 {
69  if (lockfile && curLockName == profile)
70  return true;
71 
72  QLockFile* newLock = new QLockFile(lockPathFromName(profile));
73  newLock->setStaleLockTime(0);
74  if (!newLock->tryLock()) {
75  delete newLock;
76  return false;
77  }
78 
79  unlock();
80  lockfile.reset(newLock);
81  curLockName = profile;
82  return true;
83 }
84 
89 {
90  if (!lockfile)
91  return;
92 
93  lockfile->unlock();
94  lockfile.reset();
95  curLockName.clear();
96 }
97 
105 {
106  if (!lockfile) {
107  qCritical() << "assertLock: We don't seem to own any lock!";
108  deathByBrokenLock();
109  }
110 
111  if (!QFile(lockPathFromName(curLockName)).exists()) {
112  QString tmp = curLockName;
113  unlock();
114  if (lock(tmp)) {
115  qCritical() << "assertLock: Lock file was lost, but could be restored";
116  } else {
117  qCritical() << "assertLock: Lock file was lost, and could *NOT* be restored";
118  deathByBrokenLock();
119  }
120  }
121 }
122 
127 {
128  qCritical() << "Lock is *BROKEN*, exiting immediately";
129  abort();
130 }
131 
137 {
138  return lockfile.operator bool();
139 }
140 
146 {
147  if (lockfile)
148  return curLockName;
149  else
150  return QString();
151 }
Paths::getSettingsDirPath
QString getSettingsDirPath() const
Get path to directory, where the settings files are stored.
Definition: paths.cpp:286
ProfileLocker::unlock
static void unlock()
Releases the lock on the current profile.
Definition: profilelocker.cpp:88
settings.h
ProfileLocker::getCurLockName
static QString getCurLockName()
Get current locked profile name.
Definition: profilelocker.cpp:145
ProfileLocker::deathByBrokenLock
static void deathByBrokenLock()
Print an error then exit immediately.
Definition: profilelocker.cpp:126
ProfileLocker::lock
static bool lock(QString profile)
Tries to acquire the lock on a profile, will not block.
Definition: profilelocker.cpp:67
ProfileLocker::assertLock
static void assertLock()
Check that we actually own the lock. In case the file was deleted on disk, restore it....
Definition: profilelocker.cpp:104
profilelocker.h
Settings::getPaths
Paths & getPaths()
Definition: settings.cpp:834
ProfileLocker::lockfile
static std::unique_ptr< QLockFile > lockfile
Definition: profilelocker.h:44
ProfileLocker::hasLock
static bool hasLock()
Chacks, that profile locked.
Definition: profilelocker.cpp:136
Settings::getInstance
static Settings & getInstance()
Returns the singleton instance.
Definition: settings.cpp:88
ProfileLocker::isLockable
static bool isLockable(QString profile)
Checks if a profile is currently locked by another instance. If we own the lock, we consider it locka...
Definition: profilelocker.cpp:52
ProfileLocker::lockPathFromName
static QString lockPathFromName(const QString &name)
Definition: profilelocker.cpp:39
ProfileLocker::curLockName
static QString curLockName
Definition: profilelocker.h:45