author | mickeyl <mickeyl> | 2005-05-06 19:47:16 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2005-05-06 19:47:16 (UTC) |
commit | 8b1a64ffe497f5395544c6702225003aca293f80 (patch) (unidiff) | |
tree | 98d8aa7db946ae0bc9f96f51fd040627a125dceb | |
parent | e73cc9ec4596e1b5e9eed13af710606f358860ba (diff) | |
download | opie-8b1a64ffe497f5395544c6702225003aca293f80.zip opie-8b1a64ffe497f5395544c6702225003aca293f80.tar.gz opie-8b1a64ffe497f5395544c6702225003aca293f80.tar.bz2 |
fix a couple of issues with OFileNotification, seems to work with single and multiple file notifications now
add a named trigger
-rw-r--r-- | libopie2/opiecore/ofilenotify.cpp | 55 | ||||
-rw-r--r-- | libopie2/opiecore/ofilenotify.h | 42 |
2 files changed, 84 insertions, 13 deletions
diff --git a/libopie2/opiecore/ofilenotify.cpp b/libopie2/opiecore/ofilenotify.cpp index c221e58..efd041a 100644 --- a/libopie2/opiecore/ofilenotify.cpp +++ b/libopie2/opiecore/ofilenotify.cpp | |||
@@ -59,122 +59,134 @@ namespace Opie { | |||
59 | namespace Core { | 59 | namespace Core { |
60 | 60 | ||
61 | OFileNotification::OFileNotification( QObject* parent, const char* name ) | 61 | OFileNotification::OFileNotification( QObject* parent, const char* name ) |
62 | :QObject( parent, name ), _active( false ), _multi( true ) | 62 | :QObject( parent, name ), _active( false ), _multi( true ) |
63 | { | 63 | { |
64 | qDebug( "OFileNotification::OFileNotification()" ); | 64 | qDebug( "OFileNotification::OFileNotification()" ); |
65 | } | 65 | } |
66 | 66 | ||
67 | 67 | ||
68 | OFileNotification::~OFileNotification() | 68 | OFileNotification::~OFileNotification() |
69 | { | 69 | { |
70 | stop(); | 70 | stop(); |
71 | qDebug( "OFileNotification::~OFileNotification()" ); | 71 | qDebug( "OFileNotification::~OFileNotification()" ); |
72 | } | 72 | } |
73 | 73 | ||
74 | 74 | ||
75 | bool OFileNotification::isActive() const | 75 | bool OFileNotification::isActive() const |
76 | { | 76 | { |
77 | return _active; | 77 | return _active; |
78 | } | 78 | } |
79 | 79 | ||
80 | 80 | ||
81 | int OFileNotification::watch( const QString& path, bool sshot, OFileNotificationType type ) | 81 | int OFileNotification::watch( const QString& path, bool sshot, OFileNotificationType type ) |
82 | { | 82 | { |
83 | if ( QFile::exists( path ) ) | 83 | // check if path exists and is a regular file |
84 | struct stat s; | ||
85 | if ( ::stat( (const char*) path, &s ) == -1 ) | ||
84 | { | 86 | { |
87 | qWarning( "OFileNotification::watch(): Can't watch '%s': %s.", (const char*) path, strerror( errno ) ); | ||
88 | return -1; | ||
89 | } | ||
90 | if ( !S_ISREG( s.st_mode ) ) | ||
91 | { | ||
92 | qWarning( "OFileNotification::watch(): Can't watch '%s': %s.", (const char*) path, "not a regular file" ); | ||
93 | return -1; | ||
94 | } | ||
95 | |||
85 | if ( notification_list.isEmpty() ) | 96 | if ( notification_list.isEmpty() ) |
86 | { | 97 | { |
87 | OFileNotification::registerEventHandler(); | 98 | OFileNotification::registerEventHandler(); |
88 | } | 99 | } |
89 | 100 | ||
101 | return startWatching( path, sshot, type ); | ||
102 | } | ||
103 | |||
104 | |||
105 | int OFileNotification::startWatching( const QString& path, bool sshot, OFileNotificationType type ) | ||
106 | { | ||
90 | struct inotify_watch_request iwr; | 107 | struct inotify_watch_request iwr; |
91 | ::memset( &iwr, 0, sizeof iwr ); | 108 | ::memset( &iwr, 0, sizeof iwr ); |
92 | iwr.name = const_cast<char*>( (const char*) path ); | 109 | iwr.name = const_cast<char*>( (const char*) path ); |
93 | iwr.mask = type; | 110 | iwr.mask = type; |
94 | 111 | ||
95 | _wd = ::ioctl( OFileNotification::_fd, INOTIFY_WATCH, &iwr ); | 112 | _wd = ::ioctl( OFileNotification::_fd, INOTIFY_WATCH, &iwr ); |
96 | 113 | ||
97 | if ( _wd < 0 ) | 114 | if ( _wd < 0 ) |
98 | { | 115 | { |
99 | qWarning( "OFileNotification::watch(): inotify can't watch '%s': %s.", (const char*) path, strerror( errno ) ); | 116 | qWarning( "OFileNotification::watch(): inotify can't watch '%s': %s.", (const char*) path, strerror( errno ) ); |
100 | return -1; | 117 | return -1; |
101 | } | 118 | } |
102 | 119 | ||
103 | notification_list.insert( _wd, this ); | 120 | notification_list.insert( _wd, this ); |
121 | _path = path; | ||
104 | _multi = !sshot; | 122 | _multi = !sshot; |
105 | _type = type; | 123 | _type = type; |
106 | _active = true; | 124 | _active = true; |
107 | qDebug( "OFileNotification::watch(): watching '%s' [wd=%d].", (const char*) path, _wd ); | 125 | qDebug( "OFileNotification::watch(): watching '%s' [wd=%d].", (const char*) path, _wd ); |
108 | return _wd; | 126 | return _wd; |
109 | } | 127 | } |
110 | else | ||
111 | { | ||
112 | qWarning( "OFileNotification::watch(): Can't watch '%s': %s.", (const char*) path, strerror( errno ) ); | ||
113 | return -1; | ||
114 | } | ||
115 | } | ||
116 | 128 | ||
117 | 129 | ||
118 | void OFileNotification::stop() | 130 | void OFileNotification::stop() |
119 | { | 131 | { |
120 | notification_list.remove( _wd ); | 132 | notification_list.remove( _wd ); |
121 | _path = QString::null; | 133 | _path = QString::null; |
122 | _wd = 0; | 134 | _wd = 0; |
123 | _active = false; | 135 | _active = false; |
124 | if ( notification_list.isEmpty() ) | 136 | if ( notification_list.isEmpty() ) |
125 | { | 137 | { |
126 | OFileNotification::unregisterEventHandler(); | 138 | OFileNotification::unregisterEventHandler(); |
127 | } | 139 | } |
128 | } | 140 | } |
129 | 141 | ||
130 | 142 | ||
131 | OFileNotificationType OFileNotification::type() const | 143 | OFileNotificationType OFileNotification::type() const |
132 | { | 144 | { |
133 | return _type; | 145 | return _type; |
134 | } | 146 | } |
135 | 147 | ||
136 | 148 | ||
137 | QString OFileNotification::path() const | 149 | QString OFileNotification::path() const |
138 | { | 150 | { |
139 | return _path; | 151 | return _path; |
140 | } | 152 | } |
141 | 153 | ||
142 | 154 | ||
143 | bool OFileNotification::activate() | 155 | bool OFileNotification::activate() |
144 | { | 156 | { |
145 | emit triggered(); | 157 | emit triggered( _path ); |
146 | _signal.activate(); | 158 | _signal.activate(); |
147 | if ( !_multi ) stop(); | 159 | if ( !_multi ) stop(); |
148 | return true; | 160 | return true; |
149 | } | 161 | } |
150 | 162 | ||
151 | 163 | ||
152 | void OFileNotification::singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type ) | 164 | bool OFileNotification::singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type ) |
153 | { | 165 | { |
154 | OFileNotification* ofn = new OFileNotification(); | 166 | OFileNotification* ofn = new OFileNotification(); |
155 | ofn->_signal.connect( receiver, member ); | 167 | ofn->_signal.connect( receiver, member ); |
156 | ofn->watch( path, true, type ); | 168 | return ofn->watch( path, true, type ) != -1; |
157 | } | 169 | } |
158 | 170 | ||
159 | 171 | ||
160 | void OFileNotification::inotifyEventHandler() | 172 | void OFileNotification::inotifyEventHandler() |
161 | { | 173 | { |
162 | qWarning( "OFileNotification::__eventHandler(): reached." ); | 174 | qWarning( "OFileNotification::__eventHandler(): reached." ); |
163 | 175 | ||
164 | char buffer[16384]; | 176 | char buffer[16384]; |
165 | size_t buffer_i; | 177 | size_t buffer_i; |
166 | struct inotify_event *pevent, *event; | 178 | struct inotify_event *pevent, *event; |
167 | ssize_t r; | 179 | ssize_t r; |
168 | size_t event_size; | 180 | size_t event_size; |
169 | int count = 0; | 181 | int count = 0; |
170 | 182 | ||
171 | r = ::read(_fd, buffer, 16384); | 183 | r = ::read(_fd, buffer, 16384); |
172 | 184 | ||
173 | if ( r <= 0 ) | 185 | if ( r <= 0 ) |
174 | return; | 186 | return; |
175 | 187 | ||
176 | buffer_i = 0; | 188 | buffer_i = 0; |
177 | while ( buffer_i < r ) | 189 | while ( buffer_i < r ) |
178 | { | 190 | { |
179 | /* Parse events and queue them ! */ | 191 | /* Parse events and queue them ! */ |
180 | pevent = (struct inotify_event *)&buffer[buffer_i]; | 192 | pevent = (struct inotify_event *)&buffer[buffer_i]; |
@@ -197,32 +209,55 @@ void OFileNotification::inotifyEventHandler() | |||
197 | qDebug( "received %d events...", count ); | 209 | qDebug( "received %d events...", count ); |
198 | return; | 210 | return; |
199 | } | 211 | } |
200 | 212 | ||
201 | 213 | ||
202 | bool OFileNotification::registerEventHandler() | 214 | bool OFileNotification::registerEventHandler() |
203 | { | 215 | { |
204 | OFileNotification::_fd = ::open( INOTIFY_DEVICE, O_RDONLY ); | 216 | OFileNotification::_fd = ::open( INOTIFY_DEVICE, O_RDONLY ); |
205 | if ( OFileNotification::_fd < 0 ) | 217 | if ( OFileNotification::_fd < 0 ) |
206 | { | 218 | { |
207 | qWarning( "OFileNotification::registerEventHandler(): couldn't register event handler: %s", strerror( errno ) ); | 219 | qWarning( "OFileNotification::registerEventHandler(): couldn't register event handler: %s", strerror( errno ) ); |
208 | return false; | 220 | return false; |
209 | } | 221 | } |
210 | 222 | ||
211 | OFileNotification::_sn = new QSocketNotifier( _fd, QSocketNotifier::Read, this, "inotify event" ); | 223 | OFileNotification::_sn = new QSocketNotifier( _fd, QSocketNotifier::Read, this, "inotify event" ); |
212 | connect( OFileNotification::_sn, SIGNAL( activated(int) ), this, SLOT( inotifyEventHandler() ) ); | 224 | connect( OFileNotification::_sn, SIGNAL( activated(int) ), this, SLOT( inotifyEventHandler() ) ); |
213 | 225 | ||
214 | qDebug( "OFileNotification::registerEventHandler(): done" ); | 226 | qDebug( "OFileNotification::registerEventHandler(): done" ); |
215 | return true; | 227 | return true; |
216 | } | 228 | } |
217 | 229 | ||
218 | 230 | ||
219 | void OFileNotification::unregisterEventHandler() | 231 | void OFileNotification::unregisterEventHandler() |
220 | { | 232 | { |
233 | if ( _sn ) delete _sn; | ||
221 | if ( OFileNotification::_fd ) | 234 | if ( OFileNotification::_fd ) |
222 | ::close( OFileNotification::_fd ); | 235 | ::close( OFileNotification::_fd ); |
223 | qDebug( "OFileNotification::unregisterEventHandler(): done" ); | 236 | qDebug( "OFileNotification::unregisterEventHandler(): done" ); |
224 | } | 237 | } |
225 | 238 | ||
239 | //================================================================================================= | ||
240 | // ODirNotification | ||
241 | //================================================================================================= | ||
242 | ODirNotification::ODirNotification( QObject* parent, const char* name ) | ||
243 | :QObject( parent, name ) | ||
244 | { | ||
245 | qDebug( "ODirNotification::ODirNotification()" ); | ||
246 | } | ||
247 | |||
248 | |||
249 | ODirNotification::~ODirNotification() | ||
250 | { | ||
251 | qDebug( "ODirNotification::~ODirNotification()" ); | ||
252 | } | ||
253 | |||
226 | 254 | ||
255 | int ODirNotification::watch( const QString& path, bool sshot, OFileNotificationType type, int recurse ) | ||
256 | { | ||
257 | qDebug( "ODirNotification::watch( %s, %d, 0x%08x, %d )", (const char*) path, sshot, type, recurse ); | ||
258 | return 0; | ||
227 | } | 259 | } |
260 | |||
261 | } | ||
262 | |||
228 | } | 263 | } |
diff --git a/libopie2/opiecore/ofilenotify.h b/libopie2/opiecore/ofilenotify.h index ea525e9..41ba84d 100644 --- a/libopie2/opiecore/ofilenotify.h +++ b/libopie2/opiecore/ofilenotify.h | |||
@@ -77,120 +77,156 @@ enum OFileNotificationType | |||
77 | Attrib = IN_ATTRIB, | 77 | Attrib = IN_ATTRIB, |
78 | CloseWrite = IN_CLOSE_WRITE, | 78 | CloseWrite = IN_CLOSE_WRITE, |
79 | CloseNoWrite = IN_CLOSE_NOWRITE, | 79 | CloseNoWrite = IN_CLOSE_NOWRITE, |
80 | Open = IN_OPEN, | 80 | Open = IN_OPEN, |
81 | MovedFrom = IN_MOVED_FROM, | 81 | MovedFrom = IN_MOVED_FROM, |
82 | MovedTo = IN_MOVED_TO, | 82 | MovedTo = IN_MOVED_TO, |
83 | DeleteSubdir = IN_DELETE_SUBDIR, | 83 | DeleteSubdir = IN_DELETE_SUBDIR, |
84 | DeleteFile = IN_DELETE_FILE, | 84 | DeleteFile = IN_DELETE_FILE, |
85 | CreateSubdir = IN_CREATE_SUBDIR, | 85 | CreateSubdir = IN_CREATE_SUBDIR, |
86 | CreateFile = IN_CREATE_FILE, | 86 | CreateFile = IN_CREATE_FILE, |
87 | DeleteSelf = IN_DELETE_SELF, | 87 | DeleteSelf = IN_DELETE_SELF, |
88 | Unmount = IN_UNMOUNT, | 88 | Unmount = IN_UNMOUNT, |
89 | _QueueOverflow = IN_Q_OVERFLOW, /* Internal, don't use this in client code */ | 89 | _QueueOverflow = IN_Q_OVERFLOW, /* Internal, don't use this in client code */ |
90 | _Ignored = IN_IGNORED, /* Internal, don't use this in client code */ | 90 | _Ignored = IN_IGNORED, /* Internal, don't use this in client code */ |
91 | }; | 91 | }; |
92 | 92 | ||
93 | /*====================================================================================== | 93 | /*====================================================================================== |
94 | * OFileNotification | 94 | * OFileNotification |
95 | *======================================================================================*/ | 95 | *======================================================================================*/ |
96 | 96 | ||
97 | /** | 97 | /** |
98 | * @brief Represents a file notification | 98 | * @brief Represents a file notification |
99 | * | 99 | * |
100 | * This class allows to watch for events happening to files. | 100 | * This class allows to watch for events happening to files. |
101 | * It uses the inotify kernel interface | 101 | * It uses the inotify linux (2.6.x) kernel interface. |
102 | * | 102 | * |
103 | * @see http://www.kernel.org/pub/linux/kernel/people/rml/inotify/ | 103 | * @see http://www.kernel.org/pub/linux/kernel/people/rml/inotify/ |
104 | * | 104 | * |
105 | * @author Michael 'Mickey' Lauer <mickey@vanille.de> | 105 | * @author Michael 'Mickey' Lauer <mickey@vanille.de> |
106 | * | 106 | * |
107 | **/ | 107 | **/ |
108 | 108 | ||
109 | class OFileNotification : public QObject | 109 | class OFileNotification : public QObject |
110 | { | 110 | { |
111 | Q_OBJECT | 111 | Q_OBJECT |
112 | 112 | ||
113 | public: | 113 | public: |
114 | OFileNotification( QObject* parent = 0, const char* name = 0 ); | 114 | OFileNotification( QObject* parent = 0, const char* name = 0 ); |
115 | ~OFileNotification(); | 115 | ~OFileNotification(); |
116 | /** | 116 | /** |
117 | * This static function calls a slot when an event with @a type happens to file @a path. | 117 | * This static function calls a slot when an event with @a type happens to file @a path. |
118 | * | 118 | * |
119 | * It is very convenient to use this function because you do not need to | 119 | * It is very convenient to use this function because you do not need to |
120 | * bother with a timerEvent or to create a local QTimer object. | 120 | * bother with a timerEvent or to create a local QTimer object. |
121 | * | 121 | * |
122 | * Example: | 122 | * Example: |
123 | * <pre> | 123 | * <pre> |
124 | * | 124 | * |
125 | * #include <opie2/oapplication.h> | 125 | * #include <opie2/oapplication.h> |
126 | * #include <opie2/ofilenotify.h> | 126 | * #include <opie2/ofilenotify.h> |
127 | * using namespace Opie::Core; | 127 | * using namespace Opie::Core; |
128 | * | 128 | * |
129 | * int main( int argc, char **argv ) | 129 | * int main( int argc, char **argv ) |
130 | * { | 130 | * { |
131 | * OApplication a( argc, argv, "File Notification Example" ); | 131 | * OApplication a( argc, argv, "File Notification Example" ); |
132 | * OFileNotification::singleShot( "/tmp/quit", &a, SLOT(quit()), Create ); | 132 | * OFileNotification::singleShot( "/tmp/quit", &a, SLOT(quit()), Create ); |
133 | * ... // create and show your widgets | 133 | * ... // create and show your widgets |
134 | * return a.exec(); | 134 | * return a.exec(); |
135 | * } | 135 | * } |
136 | * </pre> | 136 | * </pre> |
137 | * | 137 | * |
138 | * This sample program automatically terminates when the file "/tmp/quit" has been created. | 138 | * This sample program automatically terminates when the file "/tmp/quit" has been created. |
139 | * | 139 | * |
140 | * | 140 | * |
141 | * The @a receiver is the receiving object and the @a member is the slot. | 141 | * The @a receiver is the receiving object and the @a member is the slot. |
142 | **/ | 142 | **/ |
143 | static void singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type = Modify ); | 143 | static bool singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type = Modify ); |
144 | /** | 144 | /** |
145 | * Starts to watch for @a type changes to @a path. Set @a sshot to True if you want to be notified only once. | 145 | * Starts to watch for @a type changes to @a path. Set @a sshot to True if you want to be notified only once. |
146 | * Note that in that case it may be more convenient to use @ref OFileNotification::singleShot() then. | 146 | * Note that in that case it may be more convenient to use @ref OFileNotification::singleShot() then. |
147 | **/ | 147 | **/ |
148 | int watch( const QString& path, bool sshot = false, OFileNotificationType type = Modify ); | 148 | int watch( const QString& path, bool sshot = false, OFileNotificationType type = Modify ); |
149 | /** | 149 | /** |
150 | * Stop watching for file events. | 150 | * Stop watching for file events. |
151 | **/ | 151 | **/ |
152 | void stop(); | 152 | void stop(); |
153 | /** | 153 | /** |
154 | * @returns the notification type as set by @ref start(). | 154 | * @returns the notification type as set by @ref start(). |
155 | **/ | 155 | **/ |
156 | OFileNotificationType type() const; | 156 | OFileNotificationType type() const; |
157 | /** | 157 | /** |
158 | * @returns the path to the file being watched by this instance. | 158 | * @returns the path to the file being watched by this instance. |
159 | **/ | 159 | **/ |
160 | QString path() const; | 160 | QString path() const; |
161 | /** | 161 | /** |
162 | * @returns if a file is currently being watched. | 162 | * @returns if a file is currently being watched. |
163 | **/ | 163 | **/ |
164 | bool isActive() const; | 164 | bool isActive() const; |
165 | /** | ||
166 | * @internal | ||
167 | */ | ||
168 | int startWatching( const QString& path, bool sshot = false, OFileNotificationType type = Modify ); | ||
165 | 169 | ||
166 | signals: | 170 | signals: |
167 | /** | 171 | /** |
168 | * This signal is emitted if an event happens of the specified type happens to the file being watched. | 172 | * This signal is emitted if an event happens of the specified type happens to the file being watched. |
169 | **/ | 173 | **/ |
170 | void triggered(); | 174 | void triggered( const QString& name ); |
171 | 175 | ||
172 | protected: | 176 | protected: |
173 | bool activate(); | 177 | bool activate(); |
174 | 178 | ||
175 | private slots: | 179 | private slots: |
176 | void inotifyEventHandler(); | 180 | void inotifyEventHandler(); |
177 | 181 | ||
178 | private: | 182 | private: |
179 | bool registerEventHandler(); | 183 | bool registerEventHandler(); |
180 | void unregisterEventHandler(); | 184 | void unregisterEventHandler(); |
181 | 185 | ||
182 | QString _path; | 186 | QString _path; |
183 | OFileNotificationType _type; | 187 | OFileNotificationType _type; |
184 | QSignal _signal; | 188 | QSignal _signal; |
185 | bool _active; | 189 | bool _active; |
186 | bool _multi; | 190 | bool _multi; |
187 | static QSocketNotifier* _sn; | 191 | static QSocketNotifier* _sn; |
188 | int _wd; // inotify watch descriptor | 192 | int _wd; // inotify watch descriptor |
189 | static int _fd; // inotify device descriptor | 193 | static int _fd; // inotify device descriptor |
190 | }; | 194 | }; |
191 | 195 | ||
196 | /*====================================================================================== | ||
197 | * ODirNotification | ||
198 | *======================================================================================*/ | ||
199 | |||
200 | /** | ||
201 | * @brief Represents a directory notification | ||
202 | * | ||
203 | * This class allows to watch for events happening to directories | ||
204 | * It uses the OFileNotification class | ||
205 | * | ||
206 | * @see http://www.kernel.org/pub/linux/kernel/people/rml/inotify/ | ||
207 | * | ||
208 | * @author Michael 'Mickey' Lauer <mickey@vanille.de> | ||
209 | * | ||
210 | **/ | ||
211 | |||
212 | class ODirNotification : public QObject | ||
213 | { | ||
214 | Q_OBJECT | ||
215 | |||
216 | public: | ||
217 | ODirNotification( QObject* parent = 0, const char* name = 0 ); | ||
218 | ~ODirNotification(); | ||
219 | /** | ||
220 | * Starts to watch for @a type changes to @a path. Recurse @a recurse levels down the filesystem tree, | ||
221 | * use 0 for no recursion and -1 for unlimited recursion. | ||
222 | * Set @a sshot to True if you want to be notified only once. | ||
223 | **/ | ||
224 | int watch( const QString& path, bool sshot = false, OFileNotificationType type = Modify, int recurse = 0 ); | ||
225 | }; | ||
226 | |||
227 | |||
192 | } | 228 | } |
193 | } | 229 | } |
194 | 230 | ||
195 | #endif | 231 | #endif |
196 | 232 | ||