1// Copyright (C) 2013 David Faure <faure+bluesystems@kde.org>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QLOCKFILE_H
5#define QLOCKFILE_H
6
7#include <QtCore/qstring.h>
8#include <QtCore/qscopedpointer.h>
9
10#include <chrono>
11
12QT_BEGIN_NAMESPACE
13
14class QLockFilePrivate;
15
16class Q_CORE_EXPORT QLockFile
17{
18public:
19 QLockFile(const QString &fileName);
20 ~QLockFile();
21
22 QString fileName() const;
23
24 bool lock();
25 bool tryLock(int timeout);
26 void unlock();
27
28 void setStaleLockTime(int);
29 int staleLockTime() const;
30
31 bool tryLock(std::chrono::milliseconds timeout = std::chrono::milliseconds::zero());
32
33 void setStaleLockTime(std::chrono::milliseconds value);
34 std::chrono::milliseconds staleLockTimeAsDuration() const;
35
36 bool isLocked() const;
37 bool getLockInfo(qint64 *pid, QString *hostname, QString *appname) const;
38 bool removeStaleLockFile();
39
40 enum LockError {
41 NoError = 0,
42 LockFailedError = 1,
43 PermissionError = 2,
44 UnknownError = 3
45 };
46 LockError error() const;
47
48protected:
49 QScopedPointer<QLockFilePrivate> d_ptr;
50
51private:
52 Q_DECLARE_PRIVATE(QLockFile)
53 Q_DISABLE_COPY(QLockFile)
54};
55
56QT_END_NAMESPACE
57
58#endif // QLOCKFILE_H
59

source code of qtbase/src/corelib/io/qlockfile.h