summaryrefslogtreecommitdiff
path: root/libopie2
Unidiff
Diffstat (limited to 'libopie2') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiecore/ofilenotify.cpp95
-rw-r--r--libopie2/opiecore/ofilenotify.h42
2 files changed, 104 insertions, 33 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
@@ -79,40 +79,52 @@ bool OFileNotification::isActive() const
79 79
80 80
81int OFileNotification::watch( const QString& path, bool sshot, OFileNotificationType type ) 81int 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 {
85 if ( notification_list.isEmpty() ) 87 qWarning( "OFileNotification::watch(): Can't watch '%s': %s.", (const char*) path, strerror( errno ) );
86 { 88 return -1;
87 OFileNotification::registerEventHandler();
88 }
89
90 struct inotify_watch_request iwr;
91 ::memset( &iwr, 0, sizeof iwr );
92 iwr.name = const_cast<char*>( (const char*) path );
93 iwr.mask = type;
94
95 _wd = ::ioctl( OFileNotification::_fd, INOTIFY_WATCH, &iwr );
96
97 if ( _wd < 0 )
98 {
99 qWarning( "OFileNotification::watch(): inotify can't watch '%s': %s.", (const char*) path, strerror( errno ) );
100 return -1;
101 }
102
103 notification_list.insert( _wd, this );
104 _multi = !sshot;
105 _type = type;
106 _active = true;
107 qDebug( "OFileNotification::watch(): watching '%s' [wd=%d].", (const char*) path, _wd );
108 return _wd;
109 } 89 }
110 else 90 if ( !S_ISREG( s.st_mode ) )
111 { 91 {
112 qWarning( "OFileNotification::watch(): Can't watch '%s': %s.", (const char*) path, strerror( errno ) ); 92 qWarning( "OFileNotification::watch(): Can't watch '%s': %s.", (const char*) path, "not a regular file" );
93 return -1;
94 }
95
96 if ( notification_list.isEmpty() )
97 {
98 OFileNotification::registerEventHandler();
99 }
100
101 return startWatching( path, sshot, type );
102}
103
104
105int OFileNotification::startWatching( const QString& path, bool sshot, OFileNotificationType type )
106{
107 struct inotify_watch_request iwr;
108 ::memset( &iwr, 0, sizeof iwr );
109 iwr.name = const_cast<char*>( (const char*) path );
110 iwr.mask = type;
111
112 _wd = ::ioctl( OFileNotification::_fd, INOTIFY_WATCH, &iwr );
113
114 if ( _wd < 0 )
115 {
116 qWarning( "OFileNotification::watch(): inotify can't watch '%s': %s.", (const char*) path, strerror( errno ) );
113 return -1; 117 return -1;
114 } 118 }
119
120 notification_list.insert( _wd, this );
121 _path = path;
122 _multi = !sshot;
123 _type = type;
124 _active = true;
125 qDebug( "OFileNotification::watch(): watching '%s' [wd=%d].", (const char*) path, _wd );
126 return _wd;
115} 127}
116 128
117 129
118void OFileNotification::stop() 130void OFileNotification::stop()
@@ -141,20 +153,20 @@ QString OFileNotification::path() const
141 153
142 154
143bool OFileNotification::activate() 155bool 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
152void OFileNotification::singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type ) 164bool 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
160void OFileNotification::inotifyEventHandler() 172void OFileNotification::inotifyEventHandler()
@@ -217,12 +229,35 @@ bool OFileNotification::registerEventHandler()
217 229
218 230
219void OFileNotification::unregisterEventHandler() 231void 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//=================================================================================================
242ODirNotification::ODirNotification( QObject* parent, const char* name )
243 :QObject( parent, name )
244{
245 qDebug( "ODirNotification::ODirNotification()" );
246}
247
226 248
249ODirNotification::~ODirNotification()
250{
251 qDebug( "ODirNotification::~ODirNotification()" );
227} 252}
253
254
255int 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;
228} 259}
260
261}
262
263} \ No newline at end of file
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
@@ -97,9 +97,9 @@ enum OFileNotificationType
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>
@@ -139,9 +139,9 @@ class OFileNotification : public QObject
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 **/
@@ -161,14 +161,18 @@ class OFileNotification : public QObject
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
@@ -188,8 +192,40 @@ class OFileNotification : public QObject
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
212class 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