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_CORE_DIRECTORYLOCK_H
21#define LIBREPCB_CORE_DIRECTORYLOCK_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "filepath.h"
27
28#include <QtCore>
29
30#include <functional>
31
32/*******************************************************************************
33 * Namespace / Forward Declarations
34 ******************************************************************************/
35namespace librepcb {
36
37/*******************************************************************************
38 * Class DirectoryLock
39 ******************************************************************************/
40
154class DirectoryLock final {
155 Q_DECLARE_TR_FUNCTIONS(DirectoryLock)
156
157public:
158 // Types
159
163 enum class LockStatus {
165 Unlocked,
167 StaleLock,
169 LockedByThisApp,
171 LockedByOtherApp,
173 LockedByOtherUser,
175 LockedByUnknownApp,
176 };
177
191 typedef std::function<bool(const FilePath& dir, LockStatus status,
192 const QString& user)>
194
195 // Constructors / Destructor
196
203 DirectoryLock() noexcept;
204
210 DirectoryLock(const DirectoryLock& other) = delete;
211
217 explicit DirectoryLock(const FilePath& dir) noexcept;
218
225 ~DirectoryLock() noexcept;
226
227 // Setters
228
237 void setDirToLock(const FilePath& dir) noexcept;
238
239 // Getters
240
248 const FilePath& getDirToLock() const noexcept { return mDirToLock; }
249
256 const FilePath& getLockFilepath() const noexcept { return mLockFilePath; }
257
268 LockStatus getStatus(QString* lockedByUser = nullptr) const;
269
270 // General Methods
271
289 void tryLock(LockHandlerCallback lockHandler = nullptr);
290
302 bool unlockIfLocked();
303
314 void lock();
315
325 void unlock();
326
327 // Operator Overloadings
328 DirectoryLock& operator=(const DirectoryLock& rhs) = delete;
329
330private: // Methods
336 static QSet<FilePath>& dirsLockedByThisAppInstance() noexcept;
337
338private: // Data
343
351
366};
367
368/*******************************************************************************
369 * End of File
370 ******************************************************************************/
371
372} // namespace librepcb
373
374#endif
This class can be used to implement file-based directory locks.
Definition: directorylock.h:154
FilePath mDirToLock
The filepath to the directory to lock (passed by setDirToLock())
Definition: directorylock.h:342
static QSet< FilePath > & dirsLockedByThisAppInstance() noexcept
Get the global set of filepaths locked by this application instance.
Definition: directorylock.cpp:229
void setDirToLock(const FilePath &dir) noexcept
Specify the directory for which you need the lock.
Definition: directorylock.cpp:64
bool unlockIfLocked()
Unlock the specified directory if it was locked by this object.
Definition: directorylock.cpp:178
const FilePath & getLockFilepath() const noexcept
Get the filepath of the lock file (NOT the directory to lock!)
Definition: directorylock.h:256
DirectoryLock & operator=(const DirectoryLock &rhs)=delete
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:193
void unlock()
Unlock the specified directory (remove the lock file)
Definition: directorylock.cpp:216
void lock()
Lock the specified directory (create/update the lock file)
Definition: directorylock.cpp:187
LockStatus
The return type of getStatus()
Definition: directorylock.h:163
const FilePath & getDirToLock() const noexcept
Get the filepath of the directory to lock (passed by setDirToLock())
Definition: directorylock.h:248
DirectoryLock() noexcept
The default constructor.
Definition: directorylock.cpp:41
FilePath mLockFilePath
The filepath to the lock file.
Definition: directorylock.h:350
bool mLockedByThisObject
This attribute defines if the lock is active by this object.
Definition: directorylock.h:365
LockStatus getStatus(QString *lockedByUser=nullptr) const
Get the lock status of the specified directory.
Definition: directorylock.cpp:74
void tryLock(LockHandlerCallback lockHandler=nullptr)
Lock the specified directory if not already locked.
Definition: directorylock.cpp:152
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:129
Definition: occmodel.cpp:77