author | mickeyl <mickeyl> | 2005-05-10 13:30:51 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2005-05-10 13:30:51 (UTC) |
commit | e31f22952f47aeb54b206349f1e469704a6a6e8f (patch) (side-by-side diff) | |
tree | 2aa06bf1ebd9850b1e987d1dcc94794699a0c9fe | |
parent | 9bcf6a5112d31ffdc196ebcd22a584c68f9f7b1e (diff) | |
download | opie-e31f22952f47aeb54b206349f1e469704a6a6e8f.zip opie-e31f22952f47aeb54b206349f1e469704a6a6e8f.tar.gz opie-e31f22952f47aeb54b206349f1e469704a6a6e8f.tar.bz2 |
add foundation for a much more intuitive API for file notifications
-rw-r--r-- | examples/opiecore/onotifytest/.cvsignore | 6 | ||||
-rw-r--r-- | examples/opiecore/onotifytest/main.cpp | 59 | ||||
-rw-r--r-- | examples/opiecore/onotifytest/onotifytest.pro | 16 | ||||
-rw-r--r-- | examples/opiecore/opiecore.pro | 2 | ||||
-rw-r--r-- | libopie2/opiecore/ofilenotify.cpp | 58 | ||||
-rw-r--r-- | libopie2/opiecore/ofilenotify.h | 37 |
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 @@ +Makefile* +moc* +*moc +*.o +~* + 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 @@ +// (C) Michael 'Mickey' Lauer <mickey@Vanille.de> +// LICENSE = "GPLv2" + +/* OPIE */ +#include <opie2/odebug.h> +#include <opie2/oapplication.h> +#include <opie2/ofilenotify.h> +using namespace Opie::Core; + +/* QT */ +#include <qpushbutton.h> +#include <qtextstream.h> + +class App : public OApplication +{ + +public: +OFile* tmpfoo; + +App( int argc, char** argv ) : OApplication( argc, argv, "libopie2 notify test" ) +{ + odebug << "App()" << oendl; + + tmpfoo = new OFile( "/tmp/foo" ); + if ( tmpfoo->open( IO_ReadWrite ) ) + { + QTextStream stream( tmpfoo ); + stream << "This is my content"; + } + + QObject::connect( tmpfoo, SIGNAL(accessed(const QString&)), this, SLOT(quit()) ); + QObject::connect( tmpfoo, SIGNAL(closed(const QString&,bool)), this, SLOT(quit()) ); +} + +~App() +{ + odebug << "~App()" << oendl; + + delete tmpfoo; +} + +}; + +int main( int argc, char** argv ) +{ + App* app = new App( argc, argv ); + QPushButton* b = new QPushButton( "Click me to close", 0 ); + QObject::connect( b, SIGNAL(clicked()), app, SLOT(quit()) ); + b->resize( 200, 200 ); + b->move( 150, 150 ); + b->show(); + app->setMainWidget( b ); + app->exec(); + delete app; + + return 0; + +} + 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 @@ +TEMPLATE = app +CONFIG = qt warn_on +SOURCES = main.cpp +INCLUDEPATH += $(OPIEDIR)/include +DEPENDPATH += $(OPIEDIR)/include +LIBS += -lopiecore2 +TARGET = onotifytest + +!contains( platform, x11 ) { + include( $(OPIEDIR)/include.pro ) +} + +contains( platform, x11 ) { + LIBS += -L$(OPIEDIR)/lib -Wl,-rpath,$(OPIEDIR)/lib +} + 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 @@ TEMPLATE = subdirs -unix:SUBDIRS = odebugdemo oconfigdemo oglobalsettingsdemo onotifydemo oprocessdemo oplugins oinputsystemdemo +unix: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 @@ -14,96 +14,152 @@ _;:, .> :=|. This program is free software; you can + . -:. = it will be useful, but WITHOUT ANY WARRANTY; : .. .:, . . . without even the implied warranty of =_ + =;=|` MERCHANTABILITY or FITNESS FOR A _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU ..}^=.= = ; Library General Public License for more ++= -. .` .: details. : = ...= . :.=- -. .:....=;==+<; You should have received a copy of the GNU -_. . . )=. = Library General Public License along with -- :-=` this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "ofilenotify.h" using namespace Opie::Core; /* OPIE */ /* QT */ #include <qobject.h> #include <qsocketnotifier.h> #include <qsignal.h> #include <qintdict.h> #include <qdir.h> /* STD */ #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <fcntl.h> #include <assert.h> #include <string.h> #include <errno.h> #include <unistd.h> static QIntDict<OFileNotification> notification_list; QSocketNotifier* OFileNotification::_sn; int OFileNotification::_fd = -1; #define INOTIFY_DEVICE "/dev/inotify" namespace Opie { namespace Core { //================================================================================================= +// OFile +//================================================================================================= + +OFile::OFile() : QObject( 0, 0 ), QFile() +{ + qDebug( "OFile()" ); +} + +OFile::OFile( const QString& name ) : QObject( 0, 0 ), QFile( name ) +{ + qDebug( "OFile()" ); +} + +OFile::~OFile() +{ + qDebug( "~OFile()" ); +} + +void OFile::connectNotify( const char *signal ) +{ + QString s = normalizeSignalSlot( signal+1 ); + qDebug( "OFile::connectNotify() signal = '%s'", (const char*) s ); + + if ( s.startsWith( "accessed" ) ) + + + + + + + + QObject::connectNotify( signal ); + +/* + void accessed( const QString& ); + void modified( const QString& ); + void attributed( const QString& ); + void closed( const QString&, bool ); + void opened( const QString& ); + void deleted( const QString& ); + void unmounted( const QString& ); +*/ + +} + +void OFile::disconnectNotify( const char* signal ) +{ + qDebug( "OFile::disconnectNotify() signal = '%s'", signal ); + QObject::disconnectNotify( signal ); +} + +int OFile::startWatch( int mode ) +{ +} + +//================================================================================================= // OFileNotificationEvent //================================================================================================= OFileNotificationEvent::OFileNotificationEvent( OFileNotification* parent, int wd, unsigned int mask, unsigned int cookie, const QString& name ) :_parent( parent ), _wd( wd ), _mask( mask ), _cookie( cookie ), _name( name ) { qDebug( "OFileNotificationEvent()" ); } OFileNotificationEvent::~OFileNotificationEvent() { qDebug( "~OFileNotificationEvent()" ); } //================================================================================================= // OFileNotification //================================================================================================= OFileNotification::OFileNotification( QObject* parent, const char* name ) :QObject( parent, name ), _active( false ), _multi( true ) { qDebug( "OFileNotification::OFileNotification()" ); } OFileNotification::~OFileNotification() { stop(); qDebug( "OFileNotification::~OFileNotification()" ); } bool OFileNotification::isActive() const { return _active; } int OFileNotification::watch( const QString& path, bool sshot, OFileNotificationType type ) { // check if path exists and is a regular file struct stat s; if ( ::stat( (const char*) path, &s ) == -1 ) { qWarning( "OFileNotification::watch(): Can't watch '%s': %s.", (const char*) path, strerror( errno ) ); return -1; } if ( !S_ISREG( s.st_mode ) ) { @@ -174,97 +230,97 @@ bool OFileNotification::activate( const OFileNotificationEvent* e ) { qDebug( "OFileNotification::activate(): e = ( %s, %d, 0x%08x, %d, %s )", (const char*) _path, e->descriptor(), e->mask(), e->cookie(), (const char*) e->name() ); // dumb signal _signal.activate(); // generic signal emit triggered( _path, e->mask(), e->name() ); // specialized signals switch ( e->mask() ) { case Access: emit accessed( _path ); break; case Modify: emit modified( _path ); break; case Attrib: emit attributed( _path); break; case CloseWrite: emit closed( _path, true ); break; case CloseNoWrite: emit closed( _path, false ); break; case Open: emit opened( _path ); break; case MovedFrom: emit movedFrom( _path, e->name() ); break; case MovedTo: emit movedTo( _path, e->name() ); break; case DeleteSubdir: emit deletedSubdir( _path, e->name() ); break; case DeleteFile: emit deletedFile( _path, e->name() ); break; case CreateSubdir: emit createdSubdir( _path, e->name() ); break; case CreateFile: emit createdFile( _path, e->name() ); break; case DeleteSelf: emit deleted( _path ); break; case Unmount: emit unmounted( _path ); break; default: assert( 0 ); } if ( !_multi ) stop(); return true; } bool OFileNotification::singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type ) { OFileNotification* ofn = new OFileNotification(); ofn->_signal.connect( receiver, member ); return ofn->watch( path, true, type ) != -1; } void OFileNotification::inotifyEventHandler() { qDebug( "OFileNotification::inotifyEventHandler(): reached." ); char buffer[16384]; - size_t buffer_i; + ssize_t buffer_i; struct inotify_event *pevent, *event; ssize_t r; size_t event_size; int count = 0; r = ::read(_fd, buffer, 16384); if ( r <= 0 ) return; buffer_i = 0; while ( buffer_i < r ) { pevent = (struct inotify_event *)&buffer[buffer_i]; event_size = sizeof(struct inotify_event) + pevent->len; OFileNotificationEvent* e = new OFileNotificationEvent( notification_list[ pevent->wd ], pevent->wd, pevent->mask, pevent->cookie, pevent->len ? pevent->name : 0 ); e->activate(); buffer_i += event_size; count++; } qDebug( "OFileNotification::inotifyEventHandler(): processed %d events", count ); } bool OFileNotification::registerEventHandler() { OFileNotification::_fd = ::open( INOTIFY_DEVICE, O_RDONLY ); if ( OFileNotification::_fd < 0 ) { qWarning( "OFileNotification::registerEventHandler(): couldn't register event handler: %s", strerror( errno ) ); return false; } OFileNotification::_sn = new QSocketNotifier( _fd, QSocketNotifier::Read, this, "inotify event" ); connect( OFileNotification::_sn, SIGNAL( activated(int) ), this, SLOT( inotifyEventHandler() ) ); qDebug( "OFileNotification::registerEventHandler(): done" ); return true; } void OFileNotification::unregisterEventHandler() { if ( _sn ) delete _sn; if ( OFileNotification::_fd ) ::close( OFileNotification::_fd ); 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 @@ -1,92 +1,129 @@ /* This file is part of the Opie Project =. Copyright (C) 2004-2005 Michael 'Mickey' Lauer <mickey@Vanille.de> .=l. Copyright (C) The Opie Team <opie-devel@handhelds.org> .>+-= _;:, .> :=|. This program is free software; you can .> <`_, > . <= redistribute it and/or modify it under :`=1 )Y*s>-.-- : the terms of the GNU Library General Public .="- .-=="i, .._ License as published by the Free Software - . .-<_> .<> Foundation; version 2 of the License. ._= =} : .%`+i> _;_. .i_,=:_. -<s. This program is distributed in the hope that + . -:. = it will be useful, but WITHOUT ANY WARRANTY; : .. .:, . . . without even the implied warranty of =_ + =;=|` MERCHANTABILITY or FITNESS FOR A _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU ..}^=.= = ; Library General Public License for more ++= -. .` .: details. : = ...= . :.=- -. .:....=;==+<; You should have received a copy of the GNU -_. . . )=. = Library General Public License along with -- :-=` this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef OFILENOTIFY_H #define OFILENOTIFY_H #if defined (__GNUC__) && (__GNUC__ < 3) #define _GNU_SOURCE #endif #include "linux_inotify.h" /* QT */ #include <qsocketnotifier.h> #include <qsignal.h> #include <qstring.h> +#include <qobject.h> +#include <qfile.h> namespace Opie { namespace Core { +class OFile : public QObject, public QFile +{ + Q_OBJECT + + public: + OFile(); + OFile( const QString & name ); + virtual ~OFile(); + + protected: + virtual void connectNotify( const char* signal ); + virtual void disconnectNotify( const char* signal ); + + private: + int startWatch( int mode ); + + signals: + void accessed( const QString& ); + void modified( const QString& ); + void attributed( const QString& ); + void closed( const QString&, bool ); + void opened( const QString& ); + void deleted( const QString& ); + void unmounted( const QString& ); +}; + +/* + void movedTo( const QString&, const QString& ); + void movedFrom( const QString&, const QString& ); + void deletedSubdir( const QString&, const QString& ); + void deletedFile( const QString&, const QString& ); + void createdSubdir( const QString&, const QString& ); + void createdFile( const QString&, const QString& ); +*/ + class OFileNotificationEvent; /*====================================================================================== * OFileNotificationType *======================================================================================*/ /** * @brief An enumerate for the different types of file notifications * * This enumerate provides a means to specify the type of events that you are interest in. * Valid values are: * <ul> * <li>Access: The file was accessed (read) * <li>Modify The file was modified (write,truncate) * <li>Attrib = The file had its attributes changed (chmod,chown,chgrp) * <li>CloseWrite = Writable file was closed * <li>CloseNoWrite = Unwritable file was closed * <li>Open = File was opened * <li>MovedFrom = File was moved from X * <li>MovedTo = File was moved to Y * <li>DeleteSubdir = Subdir was deleted * <li>DeleteFile = Subfile was deleted * <li>CreateSubdir = Subdir was created * <li>CreateFile = Subfile was created * <li>DeleteSelf = Self was deleted * <li>Unmount = The backing filesystem was unmounted * </ul> * **/ enum OFileNotificationType { Access = IN_ACCESS, Modify = IN_MODIFY, Attrib = IN_ATTRIB, CloseWrite = IN_CLOSE_WRITE, CloseNoWrite = IN_CLOSE_NOWRITE, Open = IN_OPEN, MovedFrom = IN_MOVED_FROM, MovedTo = IN_MOVED_TO, DeleteSubdir = IN_DELETE_SUBDIR, DeleteFile = IN_DELETE_FILE, CreateSubdir = IN_CREATE_SUBDIR, CreateFile = IN_CREATE_FILE, DeleteSelf = IN_DELETE_SELF, Unmount = IN_UNMOUNT, _QueueOverflow = IN_Q_OVERFLOW, /* Internal, don't use this in client code */ _Ignored = IN_IGNORED, /* Internal, don't use this in client code */ |