1 | /* |
2 | This file is part of libkabc. |
3 | |
4 | Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org> |
5 | |
6 | This library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Library General Public |
8 | License as published by the Free Software Foundation; either |
9 | version 2 of the License, or (at your option) any later version. |
10 | |
11 | This library 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 GNU |
14 | Library General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Library General Public License |
17 | along with this library; see the file COPYING.LIB. If not, write to |
18 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
19 | Boston, MA 02110-1301, USA. |
20 | */ |
21 | #ifndef KABC_LOCK_H |
22 | #define KABC_LOCK_H |
23 | |
24 | #include "kabc_export.h" |
25 | #include <QtCore/QString> |
26 | #include <QtCore/QObject> |
27 | |
28 | namespace KABC { |
29 | |
30 | /** |
31 | This class provides locking functionality for a file, directory or an |
32 | arbitrary string-represented resource. |
33 | */ |
34 | class KABC_DEPRECATED_EXPORT Lock : public QObject |
35 | { |
36 | Q_OBJECT |
37 | public: |
38 | /** |
39 | Constructor. |
40 | |
41 | @param identifier An identifier for the resource to be locked, e.g. a file |
42 | name. |
43 | */ |
44 | Lock( const QString &identifier ); |
45 | |
46 | /** |
47 | Destruct lock object. This also removes the lock on the resource. |
48 | */ |
49 | ~Lock(); |
50 | |
51 | /** |
52 | Lock resource. |
53 | */ |
54 | virtual bool lock(); |
55 | |
56 | /** |
57 | Unlock resource. |
58 | */ |
59 | virtual bool unlock(); |
60 | |
61 | /** |
62 | Returns the lastest error message. |
63 | */ |
64 | virtual QString error() const; |
65 | |
66 | /** |
67 | Returns the path of the lock file. |
68 | |
69 | The file will be located in the directory returned by locksDir() |
70 | and have the file extension @c .lock |
71 | */ |
72 | QString lockFileName() const; |
73 | |
74 | /** |
75 | Reads the process ID and the application name from a lock file. |
76 | |
77 | @param filename The lock file to read from. Full path or relative to |
78 | current working directory |
79 | @param pid The variable the process ID will be read into |
80 | @param app The variable the application name will be read into |
81 | |
82 | @return @c false if @p filename could not be read, otherwise @c true, even |
83 | when reading the values fails |
84 | */ |
85 | static bool readLockFile( const QString &filename, int &pid, QString &app ); |
86 | |
87 | /** |
88 | Writes the process ID and the application name to a lock file. |
89 | |
90 | @param filename The lock file to write to |
91 | |
92 | @return @c false if the file could not be opened for writing, otherwise |
93 | @c true |
94 | */ |
95 | static bool writeLockFile( const QString &filename ); |
96 | |
97 | /** |
98 | Returns the path of the directory where locks are created. |
99 | */ |
100 | static QString locksDir(); |
101 | |
102 | Q_SIGNALS: |
103 | /** |
104 | Emitted after the lock has been locked. |
105 | */ |
106 | void locked(); |
107 | /** |
108 | Emitted after the lock has been unlocked. |
109 | */ |
110 | void unlocked(); |
111 | |
112 | private: |
113 | class Private; |
114 | Private *const d; |
115 | }; |
116 | |
117 | } |
118 | |
119 | #endif |
120 | |