LibrePCB Developers Documentation
directorylock.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free 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  * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_DIRECTORYLOCK_H
21 #define LIBREPCB_DIRECTORYLOCK_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../exceptions.h"
27 #include "filepath.h"
28 
29 #include <QtCore>
30 
31 #include <functional>
32 
33 /*******************************************************************************
34  * Namespace / Forward Declarations
35  ******************************************************************************/
36 namespace librepcb {
37 
38 /*******************************************************************************
39  * Class DirectoryLock
40  ******************************************************************************/
41 
155 class DirectoryLock final {
156  Q_DECLARE_TR_FUNCTIONS(DirectoryLock)
157 
158 public:
159  // Types
160 
164  enum class LockStatus {
166  Unlocked,
168  StaleLock,
177  };
178 
192  typedef std::function<bool(const FilePath& dir, LockStatus status,
193  const QString& user)>
195 
196  // Constructors / Destructor
197 
204  DirectoryLock() noexcept;
205 
211  DirectoryLock(const DirectoryLock& other) = delete;
212 
218  explicit DirectoryLock(const FilePath& dir) noexcept;
219 
226  ~DirectoryLock() noexcept;
227 
228  // Setters
229 
238  void setDirToLock(const FilePath& dir) noexcept;
239 
240  // Getters
241 
249  const FilePath& getDirToLock() const noexcept { return mDirToLock; }
250 
257  const FilePath& getLockFilepath() const noexcept { return mLockFilePath; }
258 
269  LockStatus getStatus(QString* lockedByUser = nullptr) const;
270 
271  // General Methods
272 
290  void tryLock(LockHandlerCallback lockHandler = nullptr);
291 
303  bool unlockIfLocked();
304 
315  void lock();
316 
326  void unlock();
327 
328  // Operator Overloadings
329  DirectoryLock& operator=(const DirectoryLock& rhs) = delete;
330 
331 private: // Methods
337  static QSet<FilePath>& dirsLockedByThisAppInstance() noexcept;
338 
339 private: // Data
344 
352 
367 };
368 
369 /*******************************************************************************
370  * End of File
371  ******************************************************************************/
372 
373 } // namespace librepcb
374 
375 #endif // LIBREPCB_DIRECTORYLOCK_H
void lock()
Lock the specified directory (create/update the lock file)
Definition: directorylock.cpp:182
const FilePath & getLockFilepath() const noexcept
Get the filepath of the lock file (NOT the directory to lock!)
Definition: directorylock.h:257
The directory is locked by another user or machine.
FilePath mLockFilePath
The filepath to the lock file.
Definition: directorylock.h:351
Definition: airwiresbuilder.cpp:32
The directory is locked by another application instance on this machine.
The directory is locked by this application instance.
DirectoryLock() noexcept
The default constructor.
Definition: directorylock.cpp:39
void tryLock(LockHandlerCallback lockHandler=nullptr)
Lock the specified directory if not already locked.
Definition: directorylock.cpp:149
bool mLockedByThisObject
This attribute defines if the lock is active by this object.
Definition: directorylock.h:366
The directory is not locked (lock file does not exist).
The directory is locked by a crashed application instance.
FilePath mDirToLock
The filepath to the directory to lock (passed by setDirToLock())
Definition: directorylock.h:343
std::function< bool(const FilePath &dir, LockStatus status, const QString &user)> LockHandlerCallback
Callback type used to determine whether a lock should be overridden or not.
Definition: directorylock.h:194
bool unlockIfLocked()
Unlock the specified directory if it was locked by this object.
Definition: directorylock.cpp:173
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:127
void setDirToLock(const FilePath &dir) noexcept
Specify the directory for which you need the lock.
Definition: directorylock.cpp:61
The directory is locked by an unknown application (may be stale).
DirectoryLock & operator=(const DirectoryLock &rhs)=delete
const FilePath & getDirToLock() const noexcept
Get the filepath of the directory to lock (passed by setDirToLock())
Definition: directorylock.h:249
static QSet< FilePath > & dirsLockedByThisAppInstance() noexcept
Get the global set of filepaths locked by this application instance.
Definition: directorylock.cpp:224
LockStatus
The return type of getStatus()
Definition: directorylock.h:164
LockStatus getStatus(QString *lockedByUser=nullptr) const
Get the lock status of the specified directory.
Definition: directorylock.cpp:71
void unlock()
Unlock the specified directory (remove the lock file)
Definition: directorylock.cpp:211
This class can be used to implement file-based directory locks.
Definition: directorylock.h:155