summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--examples/opiecore/onotifytest/.cvsignore6
-rw-r--r--examples/opiecore/onotifytest/main.cpp59
-rw-r--r--examples/opiecore/onotifytest/onotifytest.pro16
-rw-r--r--examples/opiecore/opiecore.pro2
-rw-r--r--libopie2/opiecore/ofilenotify.cpp58
-rw-r--r--libopie2/opiecore/ofilenotify.h37
6 files changed, 176 insertions, 2 deletions
diff --git a/examples/opiecore/onotifytest/.cvsignore b/examples/opiecore/onotifytest/.cvsignore
new file mode 100644
index 0000000..8f7300c
--- a/dev/null
+++ b/examples/opiecore/onotifytest/.cvsignore
@@ -0,0 +1,6 @@
1Makefile*
2moc*
3*moc
4*.o
5~*
6
diff --git a/examples/opiecore/onotifytest/main.cpp b/examples/opiecore/onotifytest/main.cpp
new file mode 100644
index 0000000..467ebc9
--- a/dev/null
+++ b/examples/opiecore/onotifytest/main.cpp
@@ -0,0 +1,59 @@
1// (C) Michael 'Mickey' Lauer <mickey@Vanille.de>
2// LICENSE = "GPLv2"
3
4/* OPIE */
5#include <opie2/odebug.h>
6#include <opie2/oapplication.h>
7#include <opie2/ofilenotify.h>
8using namespace Opie::Core;
9
10/* QT */
11#include <qpushbutton.h>
12#include <qtextstream.h>
13
14class App : public OApplication
15{
16
17public:
18OFile* tmpfoo;
19
20App( int argc, char** argv ) : OApplication( argc, argv, "libopie2 notify test" )
21{
22 odebug << "App()" << oendl;
23
24 tmpfoo = new OFile( "/tmp/foo" );
25 if ( tmpfoo->open( IO_ReadWrite ) )
26 {
27 QTextStream stream( tmpfoo );
28 stream << "This is my content";
29 }
30
31 QObject::connect( tmpfoo, SIGNAL(accessed(const QString&)), this, SLOT(quit()) );
32 QObject::connect( tmpfoo, SIGNAL(closed(const QString&,bool)), this, SLOT(quit()) );
33}
34
35~App()
36{
37 odebug << "~App()" << oendl;
38
39 delete tmpfoo;
40}
41
42};
43
44int main( int argc, char** argv )
45{
46 App* app = new App( argc, argv );
47 QPushButton* b = new QPushButton( "Click me to close", 0 );
48 QObject::connect( b, SIGNAL(clicked()), app, SLOT(quit()) );
49 b->resize( 200, 200 );
50 b->move( 150, 150 );
51 b->show();
52 app->setMainWidget( b );
53 app->exec();
54 delete app;
55
56 return 0;
57
58}
59
diff --git a/examples/opiecore/onotifytest/onotifytest.pro b/examples/opiecore/onotifytest/onotifytest.pro
new file mode 100644
index 0000000..4e0faec
--- a/dev/null
+++ b/examples/opiecore/onotifytest/onotifytest.pro
@@ -0,0 +1,16 @@
1TEMPLATE = app
2CONFIG = qt warn_on
3SOURCES = main.cpp
4INCLUDEPATH += $(OPIEDIR)/include
5DEPENDPATH += $(OPIEDIR)/include
6LIBS += -lopiecore2
7TARGET = onotifytest
8
9!contains( platform, x11 ) {
10 include( $(OPIEDIR)/include.pro )
11}
12
13contains( platform, x11 ) {
14 LIBS += -L$(OPIEDIR)/lib -Wl,-rpath,$(OPIEDIR)/lib
15}
16
diff --git a/examples/opiecore/opiecore.pro b/examples/opiecore/opiecore.pro
index 1f86a40..3550055 100644
--- a/examples/opiecore/opiecore.pro
+++ b/examples/opiecore/opiecore.pro
@@ -1,2 +1,2 @@
1TEMPLATE = subdirs 1TEMPLATE = subdirs
2unix:SUBDIRS = odebugdemo oconfigdemo oglobalsettingsdemo onotifydemo oprocessdemo oplugins oinputsystemdemo 2unix:SUBDIRS = odebugdemo oconfigdemo oglobalsettingsdemo onotifydemo onotifytest oprocessdemo oplugins oinputsystemdemo
diff --git a/libopie2/opiecore/ofilenotify.cpp b/libopie2/opiecore/ofilenotify.cpp
index 4264327..36ec6bf 100644
--- a/libopie2/opiecore/ofilenotify.cpp
+++ b/libopie2/opiecore/ofilenotify.cpp
@@ -30,64 +30,120 @@ _;:,     .>    :=|. This program is free software; you can
30using namespace Opie::Core; 30using namespace Opie::Core;
31 31
32/* OPIE */ 32/* OPIE */
33 33
34/* QT */ 34/* QT */
35#include <qobject.h> 35#include <qobject.h>
36#include <qsocketnotifier.h> 36#include <qsocketnotifier.h>
37#include <qsignal.h> 37#include <qsignal.h>
38#include <qintdict.h> 38#include <qintdict.h>
39#include <qdir.h> 39#include <qdir.h>
40 40
41/* STD */ 41/* STD */
42#include <sys/types.h> 42#include <sys/types.h>
43#include <sys/stat.h> 43#include <sys/stat.h>
44#include <sys/ioctl.h> 44#include <sys/ioctl.h>
45#include <fcntl.h> 45#include <fcntl.h>
46#include <assert.h> 46#include <assert.h>
47#include <string.h> 47#include <string.h>
48#include <errno.h> 48#include <errno.h>
49#include <unistd.h> 49#include <unistd.h>
50 50
51static QIntDict<OFileNotification> notification_list; 51static QIntDict<OFileNotification> notification_list;
52 52
53QSocketNotifier* OFileNotification::_sn; 53QSocketNotifier* OFileNotification::_sn;
54int OFileNotification::_fd = -1; 54int OFileNotification::_fd = -1;
55 55
56#define INOTIFY_DEVICE "/dev/inotify" 56#define INOTIFY_DEVICE "/dev/inotify"
57 57
58namespace Opie { 58namespace Opie {
59namespace Core { 59namespace Core {
60 60
61//================================================================================================= 61//=================================================================================================
62// OFile
63//=================================================================================================
64
65OFile::OFile() : QObject( 0, 0 ), QFile()
66{
67 qDebug( "OFile()" );
68}
69
70OFile::OFile( const QString& name ) : QObject( 0, 0 ), QFile( name )
71{
72 qDebug( "OFile()" );
73}
74
75OFile::~OFile()
76{
77 qDebug( "~OFile()" );
78}
79
80void OFile::connectNotify( const char *signal )
81{
82 QString s = normalizeSignalSlot( signal+1 );
83 qDebug( "OFile::connectNotify() signal = '%s'", (const char*) s );
84
85 if ( s.startsWith( "accessed" ) )
86
87
88
89
90
91
92
93 QObject::connectNotify( signal );
94
95/*
96 void accessed( const QString& );
97 void modified( const QString& );
98 void attributed( const QString& );
99 void closed( const QString&, bool );
100 void opened( const QString& );
101 void deleted( const QString& );
102 void unmounted( const QString& );
103*/
104
105}
106
107void OFile::disconnectNotify( const char* signal )
108{
109 qDebug( "OFile::disconnectNotify() signal = '%s'", signal );
110 QObject::disconnectNotify( signal );
111}
112
113int OFile::startWatch( int mode )
114{
115}
116
117//=================================================================================================
62// OFileNotificationEvent 118// OFileNotificationEvent
63//================================================================================================= 119//=================================================================================================
64OFileNotificationEvent::OFileNotificationEvent( OFileNotification* parent, int wd, unsigned int mask, unsigned int cookie, const QString& name ) 120OFileNotificationEvent::OFileNotificationEvent( OFileNotification* parent, int wd, unsigned int mask, unsigned int cookie, const QString& name )
65 :_parent( parent ), _wd( wd ), _mask( mask ), _cookie( cookie ), _name( name ) 121 :_parent( parent ), _wd( wd ), _mask( mask ), _cookie( cookie ), _name( name )
66{ 122{
67 qDebug( "OFileNotificationEvent()" ); 123 qDebug( "OFileNotificationEvent()" );
68} 124}
69 125
70 126
71OFileNotificationEvent::~OFileNotificationEvent() 127OFileNotificationEvent::~OFileNotificationEvent()
72{ 128{
73 qDebug( "~OFileNotificationEvent()" ); 129 qDebug( "~OFileNotificationEvent()" );
74} 130}
75 131
76//================================================================================================= 132//=================================================================================================
77// OFileNotification 133// OFileNotification
78//================================================================================================= 134//=================================================================================================
79OFileNotification::OFileNotification( QObject* parent, const char* name ) 135OFileNotification::OFileNotification( QObject* parent, const char* name )
80 :QObject( parent, name ), _active( false ), _multi( true ) 136 :QObject( parent, name ), _active( false ), _multi( true )
81{ 137{
82 qDebug( "OFileNotification::OFileNotification()" ); 138 qDebug( "OFileNotification::OFileNotification()" );
83} 139}
84 140
85 141
86OFileNotification::~OFileNotification() 142OFileNotification::~OFileNotification()
87{ 143{
88 stop(); 144 stop();
89 qDebug( "OFileNotification::~OFileNotification()" ); 145 qDebug( "OFileNotification::~OFileNotification()" );
90} 146}
91 147
92 148
93bool OFileNotification::isActive() const 149bool OFileNotification::isActive() const
@@ -190,65 +246,65 @@ bool OFileNotification::activate( const OFileNotificationEvent* e )
190 case CloseNoWrite: emit closed( _path, false ); break; 246 case CloseNoWrite: emit closed( _path, false ); break;
191 case Open: emit opened( _path ); break; 247 case Open: emit opened( _path ); break;
192 case MovedFrom: emit movedFrom( _path, e->name() ); break; 248 case MovedFrom: emit movedFrom( _path, e->name() ); break;
193 case MovedTo: emit movedTo( _path, e->name() ); break; 249 case MovedTo: emit movedTo( _path, e->name() ); break;
194 case DeleteSubdir: emit deletedSubdir( _path, e->name() ); break; 250 case DeleteSubdir: emit deletedSubdir( _path, e->name() ); break;
195 case DeleteFile: emit deletedFile( _path, e->name() ); break; 251 case DeleteFile: emit deletedFile( _path, e->name() ); break;
196 case CreateSubdir: emit createdSubdir( _path, e->name() ); break; 252 case CreateSubdir: emit createdSubdir( _path, e->name() ); break;
197 case CreateFile: emit createdFile( _path, e->name() ); break; 253 case CreateFile: emit createdFile( _path, e->name() ); break;
198 case DeleteSelf: emit deleted( _path ); break; 254 case DeleteSelf: emit deleted( _path ); break;
199 case Unmount: emit unmounted( _path ); break; 255 case Unmount: emit unmounted( _path ); break;
200 default: assert( 0 ); 256 default: assert( 0 );
201 } 257 }
202 258
203 if ( !_multi ) stop(); 259 if ( !_multi ) stop();
204 260
205 return true; 261 return true;
206} 262}
207 263
208 264
209bool OFileNotification::singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type ) 265bool OFileNotification::singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type )
210{ 266{
211 OFileNotification* ofn = new OFileNotification(); 267 OFileNotification* ofn = new OFileNotification();
212 ofn->_signal.connect( receiver, member ); 268 ofn->_signal.connect( receiver, member );
213 return ofn->watch( path, true, type ) != -1; 269 return ofn->watch( path, true, type ) != -1;
214} 270}
215 271
216 272
217void OFileNotification::inotifyEventHandler() 273void OFileNotification::inotifyEventHandler()
218{ 274{
219 qDebug( "OFileNotification::inotifyEventHandler(): reached." ); 275 qDebug( "OFileNotification::inotifyEventHandler(): reached." );
220 276
221 char buffer[16384]; 277 char buffer[16384];
222 size_t buffer_i; 278 ssize_t buffer_i;
223 struct inotify_event *pevent, *event; 279 struct inotify_event *pevent, *event;
224 ssize_t r; 280 ssize_t r;
225 size_t event_size; 281 size_t event_size;
226 int count = 0; 282 int count = 0;
227 283
228 r = ::read(_fd, buffer, 16384); 284 r = ::read(_fd, buffer, 16384);
229 285
230 if ( r <= 0 ) 286 if ( r <= 0 )
231 return; 287 return;
232 288
233 buffer_i = 0; 289 buffer_i = 0;
234 while ( buffer_i < r ) 290 while ( buffer_i < r )
235 { 291 {
236 pevent = (struct inotify_event *)&buffer[buffer_i]; 292 pevent = (struct inotify_event *)&buffer[buffer_i];
237 event_size = sizeof(struct inotify_event) + pevent->len; 293 event_size = sizeof(struct inotify_event) + pevent->len;
238 OFileNotificationEvent* e = new OFileNotificationEvent( notification_list[ pevent->wd ], pevent->wd, pevent->mask, 294 OFileNotificationEvent* e = new OFileNotificationEvent( notification_list[ pevent->wd ], pevent->wd, pevent->mask,
239 pevent->cookie, pevent->len ? pevent->name : 0 ); 295 pevent->cookie, pevent->len ? pevent->name : 0 );
240 e->activate(); 296 e->activate();
241 buffer_i += event_size; 297 buffer_i += event_size;
242 count++; 298 count++;
243 } 299 }
244 300
245 qDebug( "OFileNotification::inotifyEventHandler(): processed %d events", count ); 301 qDebug( "OFileNotification::inotifyEventHandler(): processed %d events", count );
246} 302}
247 303
248 304
249bool OFileNotification::registerEventHandler() 305bool OFileNotification::registerEventHandler()
250{ 306{
251 OFileNotification::_fd = ::open( INOTIFY_DEVICE, O_RDONLY ); 307 OFileNotification::_fd = ::open( INOTIFY_DEVICE, O_RDONLY );
252 if ( OFileNotification::_fd < 0 ) 308 if ( OFileNotification::_fd < 0 )
253 { 309 {
254 qWarning( "OFileNotification::registerEventHandler(): couldn't register event handler: %s", strerror( errno ) ); 310 qWarning( "OFileNotification::registerEventHandler(): couldn't register event handler: %s", strerror( errno ) );
diff --git a/libopie2/opiecore/ofilenotify.h b/libopie2/opiecore/ofilenotify.h
index 5bbf421..05343b9 100644
--- a/libopie2/opiecore/ofilenotify.h
+++ b/libopie2/opiecore/ofilenotify.h
@@ -9,68 +9,105 @@ _;:,     .>    :=|. This program is free software; you can
9.="- .-=="i,     .._ License as published by the Free Software 9.="- .-=="i,     .._ License as published by the Free Software
10- .   .-<_>     .<> Foundation; version 2 of the License. 10- .   .-<_>     .<> Foundation; version 2 of the License.
11    ._= =}       : 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#include "linux_inotify.h" 35#include "linux_inotify.h"
36 36
37/* QT */ 37/* QT */
38#include <qsocketnotifier.h> 38#include <qsocketnotifier.h>
39#include <qsignal.h> 39#include <qsignal.h>
40#include <qstring.h> 40#include <qstring.h>
41#include <qobject.h>
42#include <qfile.h>
41 43
42namespace Opie { 44namespace Opie {
43namespace Core { 45namespace Core {
44 46
47class 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
45class OFileNotificationEvent; 82class OFileNotificationEvent;
46 83
47/*====================================================================================== 84/*======================================================================================
48 * OFileNotificationType 85 * OFileNotificationType
49 *======================================================================================*/ 86 *======================================================================================*/
50 87
51/** 88/**
52 * @brief An enumerate for the different types of file notifications 89 * @brief An enumerate for the different types of file notifications
53 * 90 *
54 * This enumerate provides a means to specify the type of events that you are interest in. 91 * This enumerate provides a means to specify the type of events that you are interest in.
55 * Valid values are: 92 * Valid values are:
56 * <ul> 93 * <ul>
57 * <li>Access: The file was accessed (read) 94 * <li>Access: The file was accessed (read)
58 * <li>Modify The file was modified (write,truncate) 95 * <li>Modify The file was modified (write,truncate)
59 * <li>Attrib = The file had its attributes changed (chmod,chown,chgrp) 96 * <li>Attrib = The file had its attributes changed (chmod,chown,chgrp)
60 * <li>CloseWrite = Writable file was closed 97 * <li>CloseWrite = Writable file was closed
61 * <li>CloseNoWrite = Unwritable file was closed 98 * <li>CloseNoWrite = Unwritable file was closed
62 * <li>Open = File was opened 99 * <li>Open = File was opened
63 * <li>MovedFrom = File was moved from X 100 * <li>MovedFrom = File was moved from X
64 * <li>MovedTo = File was moved to Y 101 * <li>MovedTo = File was moved to Y
65 * <li>DeleteSubdir = Subdir was deleted 102 * <li>DeleteSubdir = Subdir was deleted
66 * <li>DeleteFile = Subfile was deleted 103 * <li>DeleteFile = Subfile was deleted
67 * <li>CreateSubdir = Subdir was created 104 * <li>CreateSubdir = Subdir was created
68 * <li>CreateFile = Subfile was created 105 * <li>CreateFile = Subfile was created
69 * <li>DeleteSelf = Self was deleted 106 * <li>DeleteSelf = Self was deleted
70 * <li>Unmount = The backing filesystem was unmounted 107 * <li>Unmount = The backing filesystem was unmounted
71 * </ul> 108 * </ul>
72 * 109 *
73 **/ 110 **/
74 111
75enum OFileNotificationType 112enum OFileNotificationType
76{ 113{