Diffstat (limited to 'libopie2/opiecore/linux/ofilenotify.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiecore/linux/ofilenotify.h | 326 |
1 files changed, 326 insertions, 0 deletions
diff --git a/libopie2/opiecore/linux/ofilenotify.h b/libopie2/opiecore/linux/ofilenotify.h new file mode 100644 index 0000000..05343b9 --- a/dev/null +++ b/libopie2/opiecore/linux/ofilenotify.h | |||
@@ -0,0 +1,326 @@ | |||
1 | /* | ||
2 | This file is part of the Opie Project | ||
3 | =. Copyright (C) 2004-2005 Michael 'Mickey' Lauer <mickey@Vanille.de> | ||
4 | .=l. Copyright (C) The Opie Team <opie-devel@handhelds.org> | ||
5 | .>+-= | ||
6 | _;:, .> :=|. This program is free software; you can | ||
7 | .> <`_, > . <= redistribute it and/or modify it under | ||
8 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | ||
9 | .="- .-=="i, .._ License as published by the Free Software | ||
10 | - . .-<_> .<> Foundation; version 2 of the License. | ||
11 | ._= =} : | ||
12 | .%`+i> _;_. | ||
13 | .i_,=:_. -<s. This program is distributed in the hope that | ||
14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
15 | : .. .:, . . . without even the implied warranty of | ||
16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | ||
18 | ..}^=.= = ; Library General Public License for more | ||
19 | ++= -. .` .: details. | ||
20 | : = ...= . :.=- | ||
21 | -. .:....=;==+<; You should have received a copy of the GNU | ||
22 | -_. . . )=. = Library General Public License along with | ||
23 | -- :-=` this library; see the file COPYING.LIB. | ||
24 | If not, write to the Free Software Foundation, | ||
25 | Inc., 59 Temple Place - Suite 330, | ||
26 | Boston, MA 02111-1307, USA. | ||
27 | */ | ||
28 | |||
29 | #ifndef OFILENOTIFY_H | ||
30 | #define OFILENOTIFY_H | ||
31 | #if defined (__GNUC__) && (__GNUC__ < 3) | ||
32 | #define _GNU_SOURCE | ||
33 | #endif | ||
34 | |||
35 | #include "linux_inotify.h" | ||
36 | |||
37 | /* QT */ | ||
38 | #include <qsocketnotifier.h> | ||
39 | #include <qsignal.h> | ||
40 | #include <qstring.h> | ||
41 | #include <qobject.h> | ||
42 | #include <qfile.h> | ||
43 | |||
44 | namespace Opie { | ||
45 | namespace Core { | ||
46 | |||
47 | class OFile : public QObject, public QFile | ||
48 | { | ||
49 | Q_OBJECT | ||
50 | |||
51 | public: | ||
52 | OFile(); | ||
53 | OFile( const QString & name ); | ||
54 | virtual ~OFile(); | ||
55 | |||
56 | protected: | ||
57 | virtual void connectNotify( const char* signal ); | ||
58 | virtual void disconnectNotify( const char* signal ); | ||
59 | |||
60 | private: | ||
61 | int startWatch( int mode ); | ||
62 | |||
63 | signals: | ||
64 | void accessed( const QString& ); | ||
65 | void modified( const QString& ); | ||
66 | void attributed( const QString& ); | ||
67 | void closed( const QString&, bool ); | ||
68 | void opened( const QString& ); | ||
69 | void deleted( const QString& ); | ||
70 | void unmounted( const QString& ); | ||
71 | }; | ||
72 | |||
73 | /* | ||
74 | void movedTo( const QString&, const QString& ); | ||
75 | void movedFrom( const QString&, const QString& ); | ||
76 | void deletedSubdir( const QString&, const QString& ); | ||
77 | void deletedFile( const QString&, const QString& ); | ||
78 | void createdSubdir( const QString&, const QString& ); | ||
79 | void createdFile( const QString&, const QString& ); | ||
80 | */ | ||
81 | |||
82 | class OFileNotificationEvent; | ||
83 | |||
84 | /*====================================================================================== | ||
85 | * OFileNotificationType | ||
86 | *======================================================================================*/ | ||
87 | |||
88 | /** | ||
89 | * @brief An enumerate for the different types of file notifications | ||
90 | * | ||
91 | * This enumerate provides a means to specify the type of events that you are interest in. | ||
92 | * Valid values are: | ||
93 | * <ul> | ||
94 | * <li>Access: The file was accessed (read) | ||
95 | * <li>Modify The file was modified (write,truncate) | ||
96 | * <li>Attrib = The file had its attributes changed (chmod,chown,chgrp) | ||
97 | * <li>CloseWrite = Writable file was closed | ||
98 | * <li>CloseNoWrite = Unwritable file was closed | ||
99 | * <li>Open = File was opened | ||
100 | * <li>MovedFrom = File was moved from X | ||
101 | * <li>MovedTo = File was moved to Y | ||
102 | * <li>DeleteSubdir = Subdir was deleted | ||
103 | * <li>DeleteFile = Subfile was deleted | ||
104 | * <li>CreateSubdir = Subdir was created | ||
105 | * <li>CreateFile = Subfile was created | ||
106 | * <li>DeleteSelf = Self was deleted | ||
107 | * <li>Unmount = The backing filesystem was unmounted | ||
108 | * </ul> | ||
109 | * | ||
110 | **/ | ||
111 | |||
112 | enum OFileNotificationType | ||
113 | { | ||
114 | Access = IN_ACCESS, | ||
115 | Modify = IN_MODIFY, | ||
116 | Attrib = IN_ATTRIB, | ||
117 | CloseWrite = IN_CLOSE_WRITE, | ||
118 | CloseNoWrite = IN_CLOSE_NOWRITE, | ||
119 | Open = IN_OPEN, | ||
120 | MovedFrom = IN_MOVED_FROM, | ||
121 | MovedTo = IN_MOVED_TO, | ||
122 | DeleteSubdir = IN_DELETE_SUBDIR, | ||
123 | DeleteFile = IN_DELETE_FILE, | ||
124 | CreateSubdir = IN_CREATE_SUBDIR, | ||
125 | CreateFile = IN_CREATE_FILE, | ||
126 | DeleteSelf = IN_DELETE_SELF, | ||
127 | Unmount = IN_UNMOUNT, | ||
128 | _QueueOverflow = IN_Q_OVERFLOW, /* Internal, don't use this in client code */ | ||
129 | _Ignored = IN_IGNORED, /* Internal, don't use this in client code */ | ||
130 | }; | ||
131 | |||
132 | /*====================================================================================== | ||
133 | * OFileNotification | ||
134 | *======================================================================================*/ | ||
135 | |||
136 | /** | ||
137 | * @brief Represents a file notification | ||
138 | * | ||
139 | * This class allows to watch for events happening to files. | ||
140 | * It uses the inotify linux (2.6.x) kernel interface. | ||
141 | * | ||
142 | * @see http://www.kernel.org/pub/linux/kernel/people/rml/inotify/ | ||
143 | * | ||
144 | * @author Michael 'Mickey' Lauer <mickey@vanille.de> | ||
145 | * | ||
146 | **/ | ||
147 | |||
148 | class OFileNotification : public QObject | ||
149 | { | ||
150 | Q_OBJECT | ||
151 | |||
152 | public: | ||
153 | OFileNotification( QObject* parent = 0, const char* name = 0 ); | ||
154 | ~OFileNotification(); | ||
155 | /** | ||
156 | * This static function calls a slot when an event with @a type happens to file @a path. | ||
157 | * | ||
158 | * It is very convenient to use this function because you do not need to | ||
159 | * bother with a timerEvent or to create a local QTimer object. | ||
160 | * | ||
161 | * Example: | ||
162 | * <pre> | ||
163 | * | ||
164 | * #include <opie2/oapplication.h> | ||
165 | * #include <opie2/ofilenotify.h> | ||
166 | * using namespace Opie::Core; | ||
167 | * | ||
168 | * int main( int argc, char **argv ) | ||
169 | * { | ||
170 | * OApplication a( argc, argv, "File Notification Example" ); | ||
171 | * OFileNotification::singleShot( "/tmp/quit", &a, SLOT(quit()), Access ); | ||
172 | * ... // create and show your widgets | ||
173 | * return a.exec(); | ||
174 | * } | ||
175 | * </pre> | ||
176 | * | ||
177 | * This sample program automatically terminates when the file "/tmp/quit" has been accessed. | ||
178 | * | ||
179 | * | ||
180 | * The @a receiver is the receiving object and the @a member is the slot. | ||
181 | **/ | ||
182 | static bool singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type = Modify ); | ||
183 | /** | ||
184 | * Starts to watch for @a type changes to @a path. Set @a sshot to True if you want to be notified only once. | ||
185 | * Note that in that case it may be more convenient to use @ref OFileNotification::singleShot() then. | ||
186 | **/ | ||
187 | int watch( const QString& path, bool sshot = false, OFileNotificationType type = Modify ); | ||
188 | /** | ||
189 | * Stop watching for file events. | ||
190 | **/ | ||
191 | void stop(); | ||
192 | /** | ||
193 | * @returns the notification type as set by @ref start(). | ||
194 | **/ | ||
195 | OFileNotificationType type() const; | ||
196 | /** | ||
197 | * @returns the path to the file being watched by this instance. | ||
198 | **/ | ||
199 | QString path() const; | ||
200 | /** | ||
201 | * @returns if a file is currently being watched. | ||
202 | **/ | ||
203 | bool isActive() const; | ||
204 | /** | ||
205 | * @internal | ||
206 | */ | ||
207 | int startWatching( const QString& path, bool sshot = false, OFileNotificationType type = Modify ); | ||
208 | |||
209 | signals: | ||
210 | void triggered( const QString&, unsigned int, const QString& ); | ||
211 | void accessed( const QString& ); | ||
212 | void modified( const QString& ); | ||
213 | void attributed( const QString& ); | ||
214 | void closed( const QString&, bool ); | ||
215 | void opened( const QString& ); | ||
216 | void movedTo( const QString&, const QString& ); | ||
217 | void movedFrom( const QString&, const QString& ); | ||
218 | void deletedSubdir( const QString&, const QString& ); | ||
219 | void deletedFile( const QString&, const QString& ); | ||
220 | void createdSubdir( const QString&, const QString& ); | ||
221 | void createdFile( const QString&, const QString& ); | ||
222 | void deleted( const QString& ); | ||
223 | void unmounted( const QString& ); | ||
224 | |||
225 | protected: | ||
226 | bool activate( const OFileNotificationEvent* e ); | ||
227 | |||
228 | private slots: | ||
229 | void inotifyEventHandler(); | ||
230 | |||
231 | private: | ||
232 | bool registerEventHandler(); | ||
233 | void unregisterEventHandler(); | ||
234 | |||
235 | QString _path; | ||
236 | OFileNotificationType _type; | ||
237 | QSignal _signal; | ||
238 | bool _active; | ||
239 | bool _multi; | ||
240 | static QSocketNotifier* _sn; | ||
241 | int _wd; // inotify watch descriptor | ||
242 | static int _fd; // inotify device descriptor | ||
243 | |||
244 | friend class OFileNotificationEvent; | ||
245 | }; | ||
246 | |||
247 | /*====================================================================================== | ||
248 | * ODirNotification | ||
249 | *======================================================================================*/ | ||
250 | |||
251 | /** | ||
252 | * @brief Represents a directory notification | ||
253 | * | ||
254 | * This class allows to watch for events happening to directories | ||
255 | * It uses the OFileNotification class | ||
256 | * | ||
257 | * @see http://www.kernel.org/pub/linux/kernel/people/rml/inotify/ | ||
258 | * | ||
259 | * @author Michael 'Mickey' Lauer <mickey@vanille.de> | ||
260 | * | ||
261 | **/ | ||
262 | |||
263 | class ODirNotification : public QObject | ||
264 | { | ||
265 | Q_OBJECT | ||
266 | |||
267 | public: | ||
268 | ODirNotification( QObject* parent = 0, const char* name = 0 ); | ||
269 | ~ODirNotification(); | ||
270 | /** | ||
271 | * Starts to watch for @a type changes to @a path. Recurse @a recurse levels down the filesystem tree, | ||
272 | * use 0 for no recursion and -1 for unlimited recursion. | ||
273 | * Set @a sshot to True if you want to be notified only once. | ||
274 | **/ | ||
275 | int watch( const QString& path, bool sshot = false, OFileNotificationType type = Modify, int recurse = 0 ); | ||
276 | |||
277 | signals: | ||
278 | /** | ||
279 | * This signal is emitted if an event happens of the specified type happens to the directory being watched. | ||
280 | **/ | ||
281 | void triggered( const QString&, unsigned int, const QString& ); | ||
282 | void accessed( const QString& ); | ||
283 | void modified( const QString& ); | ||
284 | void attributed( const QString& ); | ||
285 | void closed( const QString&, bool ); | ||
286 | void opened( const QString& ); | ||
287 | void movedTo( const QString&, const QString& ); | ||
288 | void movedFrom( const QString&, const QString& ); | ||
289 | void deletedSubdir( const QString&, const QString& ); | ||
290 | void deletedFile( const QString&, const QString& ); | ||
291 | void createdSubdir( const QString&, const QString& ); | ||
292 | void createdFile( const QString&, const QString& ); | ||
293 | void deleted( const QString& ); | ||
294 | void unmounted( const QString& ); | ||
295 | }; | ||
296 | |||
297 | /*====================================================================================== | ||
298 | * OFileNotificationEvent | ||
299 | *======================================================================================*/ | ||
300 | |||
301 | class OFileNotificationEvent | ||
302 | { | ||
303 | public: | ||
304 | OFileNotificationEvent( OFileNotification* parent, int wd, unsigned int mask, unsigned int cookie, const QString& name ); | ||
305 | ~OFileNotificationEvent(); | ||
306 | OFileNotification* parent() const { return _parent; }; | ||
307 | int descriptor() const { return _wd; }; | ||
308 | unsigned int mask() const { return _mask; }; | ||
309 | unsigned int cookie() const { return _cookie; }; | ||
310 | QString name() const { return _name; }; | ||
311 | void activate() { _parent->activate( this ); }; | ||
312 | |||
313 | private: | ||
314 | OFileNotification* _parent; | ||
315 | int _wd; | ||
316 | unsigned int _mask; | ||
317 | unsigned int _cookie; | ||
318 | QString _name; | ||
319 | }; | ||
320 | |||
321 | |||
322 | } | ||
323 | } | ||
324 | |||
325 | #endif | ||
326 | |||