Diffstat (limited to 'libopie2/opiecore/ofilenotify.h') (more/less context) (show whitespace changes)
-rw-r--r-- | libopie2/opiecore/ofilenotify.h | 93 |
1 files changed, 50 insertions, 43 deletions
diff --git a/libopie2/opiecore/ofilenotify.h b/libopie2/opiecore/ofilenotify.h index d820f7c..e3621cf 100644 --- a/libopie2/opiecore/ofilenotify.h +++ b/libopie2/opiecore/ofilenotify.h | |||
@@ -1,190 +1,197 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of the Opie Project | 2 | This file is part of the Opie Project |
3 | =. Copyright (C) 2004 Michael 'Mickey' Lauer <mickey@Vanille.de> | 3 | =. Copyright (C) 2004-2005 Michael 'Mickey' Lauer <mickey@Vanille.de> |
4 | .=l. Copyright (C) The Opie Team <opie-devel@handhelds.org> | 4 | .=l. Copyright (C) The Opie Team <opie-devel@handhelds.org> |
5 | .>+-= | 5 | .>+-= |
6 | _;:, .> :=|. This program is free software; you can | 6 | _;:, .> :=|. This program is free software; you can |
7 | .> <`_, > . <= redistribute it and/or modify it under | 7 | .> <`_, > . <= redistribute it and/or modify it under |
8 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | 8 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public |
9 | .="- .-=="i, .._ License as published by the Free Software | 9 | .="- .-=="i, .._ License as published by the Free Software |
10 | - . .-<_> .<> Foundation; either version 2 of the License, | 10 | - . .-<_> .<> Foundation; version 2 of the License. |
11 | ._= =} : or (at your option) any later version. | 11 | ._= =} : |
12 | .%`+i> _;_. | 12 | .%`+i> _;_. |
13 | .i_,=:_. -<s. This program is distributed in the hope that | 13 | .i_,=:_. -<s. This program is distributed in the hope that |
14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
15 | : .. .:, . . . without even the implied warranty of | 15 | : .. .:, . . . without even the implied warranty of |
16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | 17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU |
18 | ..}^=.= = ; Library General Public License for more | 18 | ..}^=.= = ; Library General Public License for more |
19 | ++= -. .` .: details. | 19 | ++= -. .` .: details. |
20 | : = ...= . :.=- | 20 | : = ...= . :.=- |
21 | -. .:....=;==+<; You should have received a copy of the GNU | 21 | -. .:....=;==+<; You should have received a copy of the GNU |
22 | -_. . . )=. = Library General Public License along with | 22 | -_. . . )=. = Library General Public License along with |
23 | -- :-=` this library; see the file COPYING.LIB. | 23 | -- :-=` this library; see the file COPYING.LIB. |
24 | If not, write to the Free Software Foundation, | 24 | If not, write to the Free Software Foundation, |
25 | Inc., 59 Temple Place - Suite 330, | 25 | Inc., 59 Temple Place - Suite 330, |
26 | Boston, MA 02111-1307, USA. | 26 | Boston, MA 02111-1307, USA. |
27 | */ | 27 | */ |
28 | 28 | ||
29 | #ifndef OFILENOTIFY_H | 29 | #ifndef OFILENOTIFY_H |
30 | #define OFILENOTIFY_H | 30 | #define OFILENOTIFY_H |
31 | #if defined (__GNUC__) && (__GNUC__ < 3) | 31 | #if defined (__GNUC__) && (__GNUC__ < 3) |
32 | #define _GNU_SOURCE | 32 | #define _GNU_SOURCE |
33 | #endif | 33 | #endif |
34 | 34 | ||
35 | /* QT */ | 35 | /* QT */ |
36 | #include <qobject.h> | 36 | #include <qsocketnotifier.h> |
37 | #include <qsignal.h> | 37 | #include <qsignal.h> |
38 | #include <qstring.h> | 38 | #include <qstring.h> |
39 | 39 | ||
40 | /* STD */ | 40 | /* STD */ |
41 | #include <signal.h> | 41 | #include "inotify.h" |
42 | #include <fcntl.h> | ||
43 | 42 | ||
44 | namespace Opie { | 43 | namespace Opie { |
45 | namespace Core { | 44 | namespace Core { |
46 | 45 | ||
47 | /*====================================================================================== | 46 | /*====================================================================================== |
48 | * OFileNotificationType | 47 | * OFileNotificationType |
49 | *======================================================================================*/ | 48 | *======================================================================================*/ |
50 | 49 | ||
51 | /** | 50 | /** |
52 | * @brief An enumerate for the different types of file notifications | 51 | * @brief An enumerate for the different types of file notifications |
53 | * | 52 | * |
54 | * This enumerate provides a means to specify the type of events that you are interest in. | 53 | * This enumerate provides a means to specify the type of events that you are interest in. |
55 | * Valid values are: | 54 | * Valid values are: |
56 | * <ul> | 55 | * <ul> |
57 | * <li>Access: The file was accessed (read) | 56 | * <li>Access: The file was accessed (read) |
58 | * <li>Modify The file was modified (write,truncate) | 57 | * <li>Modify The file was modified (write,truncate) |
59 | * <li>Create = The file was created in the directory | ||
60 | * <li>Delete = The file was unlinked from directory | ||
61 | * <li>Rename = The file was renamed | ||
62 | * <li>Attrib = The file had its attributes changed (chmod,chown,chgrp) | 58 | * <li>Attrib = The file had its attributes changed (chmod,chown,chgrp) |
59 | * <li>CloseWrite = Writable file was closed | ||
60 | * <li>CloseNoWrite = Unwritable file was closed | ||
61 | * <li>Open = File was opened | ||
62 | * <li>MovedFrom = File was moved from X | ||
63 | * <li>MovedTo = File was moved to Y | ||
64 | * <li>DeleteSubdir = Subdir was deleted | ||
65 | * <li>DeleteFile = Subfile was deleted | ||
66 | * <li>CreateSubdir = Subdir was created | ||
67 | * <li>CreateFile = Subfile was created | ||
68 | * <li>DeleteSelf = Self was deleted | ||
69 | * <li>Unmount = The backing filesystem was unmounted | ||
63 | * </ul> | 70 | * </ul> |
64 | * | 71 | * |
65 | **/ | 72 | **/ |
66 | 73 | ||
67 | enum OFileNotificationType { Single = 0x0000000, | 74 | enum OFileNotificationType |
68 | Multi = DN_MULTISHOT, | 75 | { |
69 | Access = DN_ACCESS, | 76 | Access = IN_ACCESS, |
70 | Modify = DN_MODIFY, | 77 | Modify = IN_MODIFY, |
71 | Create = DN_CREATE, | 78 | Attrib = IN_ATTRIB, |
72 | Delete = DN_DELETE, | 79 | CloseWrite = IN_CLOSE_WRITE, |
73 | Rename = DN_RENAME, | 80 | CloseNoWrite = IN_CLOSE_NOWRITE, |
74 | Attrib = DN_ATTRIB }; | 81 | Open = IN_OPEN, |
82 | MovedFrom = IN_MOVED_FROM, | ||
83 | MovedTo = IN_MOVED_TO, | ||
84 | DeleteSubdir = IN_DELETE_SUBDIR, | ||
85 | DeleteFile = IN_DELETE_FILE, | ||
86 | CreateSubdir = IN_CREATE_SUBDIR, | ||
87 | CreateFile = IN_CREATE_FILE, | ||
88 | DeleteSelf = IN_DELETE_SELF, | ||
89 | Unmount = IN_UNMOUNT, | ||
90 | _QueueOverflow = IN_Q_OVERFLOW, /* Internal, don't use this in client code */ | ||
91 | _Ignored = IN_IGNORED, /* Internal, don't use this in client code */ | ||
92 | }; | ||
75 | 93 | ||
76 | /*====================================================================================== | 94 | /*====================================================================================== |
77 | * OFileNotification | 95 | * OFileNotification |
78 | *======================================================================================*/ | 96 | *======================================================================================*/ |
79 | 97 | ||
80 | /** | 98 | /** |
81 | * @brief Represents a file notification | 99 | * @brief Represents a file notification |
82 | * | 100 | * |
83 | * This class allows to watch for events happening to files. | 101 | * This class allows to watch for events happening to files. |
84 | * It uses the dnotify kernel interface which is a very efficient signalling interface. | 102 | * It uses the inotify kernel interface |
85 | * | 103 | * |
86 | * @see <file:///usr/src/linux/Documentation/dnotify.txt> | 104 | * @see http://www.kernel.org/pub/linux/kernel/people/rml/inotify/ |
87 | * | 105 | * |
88 | * @author Michael 'Mickey' Lauer <mickey@vanille.de> | 106 | * @author Michael 'Mickey' Lauer <mickey@vanille.de> |
89 | * | 107 | * |
90 | **/ | 108 | **/ |
91 | 109 | ||
92 | class OFileNotification : public QObject | 110 | class OFileNotification : public QObject |
93 | { | 111 | { |
94 | Q_OBJECT | 112 | Q_OBJECT |
95 | 113 | ||
96 | public: | 114 | public: |
97 | OFileNotification( QObject* parent = 0, const char* name = 0 ); | 115 | OFileNotification( QObject* parent = 0, const char* name = 0 ); |
98 | ~OFileNotification(); | 116 | ~OFileNotification(); |
99 | /** | 117 | /** |
100 | * This static function calls a slot when an event with @a type happens to file @a path. | 118 | * This static function calls a slot when an event with @a type happens to file @a path. |
101 | * | 119 | * |
102 | * It is very convenient to use this function because you do not need to | 120 | * It is very convenient to use this function because you do not need to |
103 | * bother with a timerEvent or to create a local QTimer object. | 121 | * bother with a timerEvent or to create a local QTimer object. |
104 | * | 122 | * |
105 | * Example: | 123 | * Example: |
106 | * <pre> | 124 | * <pre> |
107 | * | 125 | * |
108 | * #include <opie2/oapplication.h> | 126 | * #include <opie2/oapplication.h> |
109 | * #include <opie2/onitify.h> | 127 | * #include <opie2/ofilenotify.h> |
110 | * using namespace Opie::Core; | 128 | * using namespace Opie::Core; |
111 | * | 129 | * |
112 | * int main( int argc, char **argv ) | 130 | * int main( int argc, char **argv ) |
113 | * { | 131 | * { |
114 | * OApplication a( argc, argv, "File Notification Example" ); | 132 | * OApplication a( argc, argv, "File Notification Example" ); |
115 | * OFileNotification::singleShot( "/tmp/quit", &a, SLOT(quit()), Create ); | 133 | * OFileNotification::singleShot( "/tmp/quit", &a, SLOT(quit()), Create ); |
116 | * ... // create and show your widgets | 134 | * ... // create and show your widgets |
117 | * return a.exec(); | 135 | * return a.exec(); |
118 | * } | 136 | * } |
119 | * </pre> | 137 | * </pre> |
120 | * | 138 | * |
121 | * This sample program automatically terminates when the file "/tmp/quite" has been created. | 139 | * This sample program automatically terminates when the file "/tmp/quit" has been created. |
122 | * | 140 | * |
123 | * | 141 | * |
124 | * The @a receiver is the receiving object and the @a member is the slot. | 142 | * The @a receiver is the receiving object and the @a member is the slot. |
125 | **/ | 143 | **/ |
126 | static void singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type = Modify ); | 144 | static void singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type = Modify ); |
127 | /** | 145 | /** |
128 | * Starts to watch for @a type changes to @a path. Set @a sshot to True if you want to be notified only once. | 146 | * Starts to watch for @a type changes to @a path. Set @a sshot to True if you want to be notified only once. |
129 | * Note that in that case it may be more convenient to use @ref OFileNotification::singleShot() then. | 147 | * Note that in that case it may be more convenient to use @ref OFileNotification::singleShot() then. |
130 | **/ | 148 | **/ |
131 | int start( const QString& path, bool sshot = false, OFileNotificationType type = Modify ); | 149 | int watch( const QString& path, bool sshot = false, OFileNotificationType type = Modify ); |
132 | /** | 150 | /** |
133 | * Stop watching for file events. | 151 | * Stop watching for file events. |
134 | **/ | 152 | **/ |
135 | void stop(); | 153 | void stop(); |
136 | /** | 154 | /** |
137 | * @returns the notification type as set by @ref start(). | 155 | * @returns the notification type as set by @ref start(). |
138 | **/ | 156 | **/ |
139 | OFileNotificationType type() const; | 157 | OFileNotificationType type() const; |
140 | /** | 158 | /** |
141 | * @returns the path to the file being watched by this instance. | 159 | * @returns the path to the file being watched by this instance. |
142 | **/ | 160 | **/ |
143 | QString path() const; | 161 | QString path() const; |
144 | /** | 162 | /** |
145 | * @returns the UNIX file descriptor for the file being watched. | ||
146 | **/ | ||
147 | int fileno() const; | ||
148 | /** | ||
149 | * @returns if a file is currently being watched. | 163 | * @returns if a file is currently being watched. |
150 | **/ | 164 | **/ |
151 | bool isActive() const; | 165 | bool isActive() const; |
152 | 166 | ||
153 | signals: | 167 | signals: |
154 | /** | 168 | /** |
155 | * This signal is emitted if an event happens of the specified type happens to the file being watched. | 169 | * This signal is emitted if an event happens of the specified type happens to the file being watched. |
156 | **/ | 170 | **/ |
157 | void triggered(); | 171 | void triggered(); |
158 | 172 | ||
159 | protected: | 173 | protected: |
160 | bool activate(); | 174 | bool activate(); |
161 | virtual bool hasChanged(); | 175 | |
162 | static bool registerSignalHandler(); | 176 | private slots: |
163 | static void unregisterSignalHandler(); | 177 | void inotifyEventHandler(); |
164 | static void __signalHandler( int sig, siginfo_t *si, void *data ); | ||
165 | 178 | ||
166 | private: | 179 | private: |
180 | bool registerEventHandler(); | ||
181 | void unregisterEventHandler(); | ||
182 | |||
167 | QString _path; | 183 | QString _path; |
168 | OFileNotificationType _type; | 184 | OFileNotificationType _type; |
169 | QSignal _signal; | 185 | QSignal _signal; |
170 | int _fd; | ||
171 | bool _active; | 186 | bool _active; |
172 | struct stat _stat; | 187 | bool _multi; |
173 | }; | 188 | static QSocketNotifier* _sn; |
174 | 189 | int _wd; // inotify watch descriptor | |
175 | #if 0 | 190 | static int _fd; // inotify device descriptor |
176 | |||
177 | class ODirectoryNotification : public OFileNotification | ||
178 | { | ||
179 | |||
180 | public: | ||
181 | virtual bool hasChanged() { return true; }; | ||
182 | }; | 191 | }; |
183 | 192 | ||
184 | #endif | ||
185 | |||
186 | } | 193 | } |
187 | } | 194 | } |
188 | 195 | ||
189 | #endif | 196 | #endif |
190 | 197 | ||