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 /libopie2/opiecore/ofilenotify.cpp | |
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
Diffstat (limited to 'libopie2/opiecore/ofilenotify.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | libopie2/opiecore/ofilenotify.cpp | 55 |
1 files changed, 45 insertions, 10 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 | |||
@@ -77,19 +77,36 @@ bool OFileNotification::isActive() const | |||
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 ); |
@@ -98,24 +115,19 @@ int OFileNotification::watch( const QString& path, bool sshot, OFileNotification | |||
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; |
@@ -139,24 +151,24 @@ QString OFileNotification::path() const | |||
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." ); |
@@ -215,14 +227,37 @@ bool OFileNotification::registerEventHandler() | |||
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 | } |