summaryrefslogtreecommitdiff
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--examples/opiecore/onotifytest/main.cpp2
-rw-r--r--libopie2/opiecore/linux/ofilenotify.cpp79
-rw-r--r--libopie2/opiecore/linux/ofilenotify.h13
3 files changed, 67 insertions, 27 deletions
diff --git a/examples/opiecore/onotifytest/main.cpp b/examples/opiecore/onotifytest/main.cpp
index 8374c59..b773da9 100644
--- a/examples/opiecore/onotifytest/main.cpp
+++ b/examples/opiecore/onotifytest/main.cpp
@@ -1,66 +1,66 @@
// (C) Michael 'Mickey' Lauer <mickey@Vanille.de>
// LICENSE = "GPLv2"
#include "main.h"
/* OPIE */
#include <opie2/odebug.h>
#include <opie2/oapplication.h>
#include <opie2/ofilenotify.h>
using namespace Opie::Core;
/* QT */
#include <qdir.h>
#include <qpushbutton.h>
#include <qstringlist.h>
#include <qtextstream.h>
#include <qmessagebox.h>
App::App( int argc, char** argv ) : QApplication( argc, argv )
{
odebug << "App()" << oendl;
#if 0
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()) );
#endif
ODirNotification* tmpfoo = new ODirNotification( 0, 0 );
- int result = tmpfoo->watch( "/tmp/foo", false, CreateFile, 1 );
+ int result = tmpfoo->watch( "/tmp/foo", false, CreateFile, 2 );
QObject::connect( tmpfoo, SIGNAL(triggered(const QString&,unsigned int,const QString&)),
this, SLOT(triggered(const QString&,unsigned int,const QString&)) );
}
App::~App()
{
odebug << "~App()" << oendl;
}
void App::triggered( const QString& str1, unsigned int id, const QString& str2 )
{
QMessageBox::information( qApp->desktop(), "info", QString( "%1\n%2\n%3" ).arg( str1 ).arg( id ).arg( str2 ) );
}
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()), qApp, SLOT(quit()) );
b->resize( 200, 200 );
b->move( 150, 150 );
b->show();
app->setMainWidget( b );
app->exec();
delete app;
return 0;
}
diff --git a/libopie2/opiecore/linux/ofilenotify.cpp b/libopie2/opiecore/linux/ofilenotify.cpp
index 3096f7e..a7820ee 100644
--- a/libopie2/opiecore/linux/ofilenotify.cpp
+++ b/libopie2/opiecore/linux/ofilenotify.cpp
@@ -181,96 +181,102 @@ int OFileNotification::startWatching( const QString& path, bool sshot, OFileNoti
struct inotify_watch_request iwr;
::memset( &iwr, 0, sizeof iwr );
iwr.name = const_cast<char*>( (const char*) path );
iwr.mask = type;
_wd = ::ioctl( OFileNotification::_fd, INOTIFY_WATCH, &iwr );
if ( _wd < 0 )
{
qWarning( "OFileNotification::watch(): inotify can't watch '%s': %s.", (const char*) path, strerror( errno ) );
return -1;
}
notification_list.insert( _wd, this );
_path = path;
_multi = !sshot;
_type = type;
_active = true;
qDebug( "OFileNotification::watch(): watching '%s' [wd=%d].", (const char*) path, _wd );
return _wd;
}
void OFileNotification::stop()
{
notification_list.remove( _wd );
_path = QString::null;
_wd = 0;
_active = false;
if ( notification_list.isEmpty() )
{
OFileNotification::unregisterEventHandler();
}
}
OFileNotificationType OFileNotification::type() const
{
return _type;
}
QString OFileNotification::path() const
{
return _path;
}
+bool OFileNotification::isSingleShot() const
+{
+ return !_multi;
+}
+
+
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." );
@@ -286,132 +292,153 @@ void OFileNotification::inotifyEventHandler()
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 );
qDebug( "OFileNotification::unregisterEventHandler(): done" );
}
//=================================================================================================
// ODirNotification
//=================================================================================================
ODirNotification::ODirNotification( QObject* parent, const char* name )
- :QObject( parent, name )
+ :QObject( parent, name ), _topfilenotification( 0 ), _type( Nothing ), _depth( -123 )
{
qDebug( "ODirNotification::ODirNotification()" );
}
ODirNotification::~ODirNotification()
{
qDebug( "ODirNotification::~ODirNotification()" );
}
+/*
+ Love-Trowbridge recursive directory scanning algorithm:
+
+ Step 1. Start at initial directory foo. Add watch.
+
+ Step 2. Setup handlers for watch created in Step 1.
+ Specifically, ensure that a directory created
+ in foo will result in a handled CREATE_SUBDIR
+ event.
+
+ Step 3. Read the contents of foo.
+
+ Step 4. For each subdirectory of foo read in step 3, repeat
+ step 1.
+
+ Step 5. For any CREATE_SUBDIR event on bar, if a watch is
+ not yet created on bar, repeat step 1 on bar.
+*/
int ODirNotification::watch( const QString& path, bool sshot, OFileNotificationType type, int recurse )
{
- qDebug( "ODirNotification::watch( %s, %d, 0x%08x, %d )", (const char*) path, sshot, type, recurse );
-
+ if ( _type == Nothing ) _type = type; // only set it once - for the top level call
+ OFileNotificationType subtype = ( recurse != 0 ) ? (OFileNotificationType) int( _type | CreateSubdir ) : _type;
+ qDebug( "ODirNotification::watch( %s, %d, 0x%08x, %d )", (const char*) path, sshot, subtype, recurse );
OFileNotification* fn = new OFileNotification( this, "ODirNotification delegate" );
- int result = fn->startWatching( path, sshot, type );
+
+ int result = fn->startWatching( path, sshot, subtype );
if ( result != -1 )
{
+
+ if ( !_topfilenotification ) _topfilenotification = fn; // only set it once - for the top level call
+ if ( _depth == -123 ) _depth = recurse; // only set it once - for the top level call
+
connect( fn, SIGNAL( triggered( const QString&, unsigned int, const QString& ) ), this, SIGNAL( triggered( const QString&, unsigned int, const QString& ) ) );
connect( fn, SIGNAL( accessed( const QString& ) ), this, SIGNAL( accessed( const QString& ) ) );
connect( fn, SIGNAL( modified( const QString& ) ), this, SIGNAL( modified( const QString& ) ) );
connect( fn, SIGNAL( attributed( const QString& ) ), this, SIGNAL( attributed( const QString& ) ) );
connect( fn, SIGNAL( closed( const QString&, bool ) ), this, SIGNAL( closed( const QString&, bool ) ) );
connect( fn, SIGNAL( opened( const QString& ) ), this, SIGNAL( opened( const QString& ) ) );
connect( fn, SIGNAL( movedTo( const QString&, const QString& ) ), this, SIGNAL( movedTo( const QString&, const QString& ) ) );
connect( fn, SIGNAL( movedFrom( const QString&, const QString& ) ), this, SIGNAL( movedFrom( const QString&, const QString& ) ) );
connect( fn, SIGNAL( deletedSubdir( const QString&, const QString& ) ), this, SIGNAL( deletedSubdir( const QString&, const QString& ) ) );
connect( fn, SIGNAL( deletedFile( const QString&, const QString& ) ), this, SIGNAL( deletedFile( const QString&, const QString& ) ) );;
connect( fn, SIGNAL( createdSubdir( const QString&, const QString& ) ), this, SIGNAL( createdSubdir( const QString&, const QString& ) ) );
connect( fn, SIGNAL( createdFile( const QString&, const QString& ) ), this, SIGNAL( createdFile( const QString&, const QString& ) ) );
connect( fn, SIGNAL( deleted( const QString& ) ), this, SIGNAL( deleted( const QString& ) ) );
connect( fn, SIGNAL( unmounted( const QString& ) ), this, SIGNAL( unmounted( const QString& ) ) );
- if ( recurse )
+ if ( recurse != 0 )
{
+ connect( fn, SIGNAL( createdSubdir( const QString&, const QString& ) ), this, SLOT( subdirCreated( const QString&, const QString& ) ) );
+
QDir directory( path );
QStringList subdirs = directory.entryList( QDir::Dirs );
for ( QStringList::Iterator it = subdirs.begin(); it != subdirs.end(); ++it )
{
if ( (*it) == "." || (*it) == ".." ) continue;
QString subpath = QString( "%1/%2" ).arg( path ).arg( *it );
- int subresult = watch( subpath, sshot, type, recurse-1 );
+ int subresult = watch( subpath, sshot, subtype, recurse-1 );
if ( subresult == -1 )
{
qDebug( "ODirNotification::watch(): subresult for '%s' was -1. Interrupting", (const char*) (*it) );
return -1;
}
}
}
-//connect( fn, SIGNAL( triggered( const QString&, unsigned int, const QString& ) ), this, SIGNAL( triggered( const QString&, unsigned int, const QString& ) ) );
}
else return -1;
}
-// void ODirNotification::subdirCreated( const QString& name )
-
-
-/*
- Love-Trowbridge recursive directory scanning algorithm:
-
- Step 1. Start at initial directory foo. Add watch.
-
- Step 2. Setup handlers for watch created in Step 1.
- Specifically, ensure that a directory created
- in foo will result in a handled CREATE_SUBDIR
- event.
-
- Step 3. Read the contents of foo.
+void ODirNotification::subdirCreated( const QString& dir, const QString& subdir )
+{
+ qDebug( "*** ODirNotification::subdirCreated '%s/%s'", (const char*) dir, (const char*) subdir );
+ QString newdir = dir;
+ if ( newdir.startsWith( _topfilenotification->path() ) )
+ {
+ newdir.replace( _topfilenotification->path(), "" );
+ int level = newdir.contains( '/' );
+ qDebug( "*** dirpart = '%s' ==> level = %d", (const char*) newdir, level );
- Step 4. For each subdirectory of foo read in step 3, repeat
- step 1.
+ if ( _depth == -1 || _depth > level )
+ {
+ watch( QString( "%1/%2" ).arg( dir ).arg( subdir ), _topfilenotification->isSingleShot(), _topfilenotification->type(), _depth == -1 ? -1 : _depth-level-1 );
+ }
- Step 5. For any CREATE_SUBDIR event on bar, if a watch is
- not yet created on bar, repeat step 1 on bar.
-*/
+ }
+}
} // namespace Ui
} // namespace Opie
diff --git a/libopie2/opiecore/linux/ofilenotify.h b/libopie2/opiecore/linux/ofilenotify.h
index 05343b9..17e6b5d 100644
--- a/libopie2/opiecore/linux/ofilenotify.h
+++ b/libopie2/opiecore/linux/ofilenotify.h
@@ -66,261 +66,274 @@ class OFile : public QObject, public QFile
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
{
+ Nothing = 0,
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 */
};
/*======================================================================================
* OFileNotification
*======================================================================================*/
/**
* @brief Represents a file notification
*
* This class allows to watch for events happening to files.
* It uses the inotify linux (2.6.x) kernel interface.
*
* @see http://www.kernel.org/pub/linux/kernel/people/rml/inotify/
*
* @author Michael 'Mickey' Lauer <mickey@vanille.de>
*
**/
class OFileNotification : public QObject
{
Q_OBJECT
public:
OFileNotification( QObject* parent = 0, const char* name = 0 );
~OFileNotification();
/**
* This static function calls a slot when an event with @a type happens to file @a path.
*
* It is very convenient to use this function because you do not need to
* bother with a timerEvent or to create a local QTimer object.
*
* Example:
* <pre>
*
* #include <opie2/oapplication.h>
* #include <opie2/ofilenotify.h>
* using namespace Opie::Core;
*
* int main( int argc, char **argv )
* {
* OApplication a( argc, argv, "File Notification Example" );
* OFileNotification::singleShot( "/tmp/quit", &a, SLOT(quit()), Access );
* ... // create and show your widgets
* return a.exec();
* }
* </pre>
*
* This sample program automatically terminates when the file "/tmp/quit" has been accessed.
*
*
* The @a receiver is the receiving object and the @a member is the slot.
**/
static bool singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type = Modify );
/**
* Starts to watch for @a type changes to @a path. Set @a sshot to True if you want to be notified only once.
* Note that in that case it may be more convenient to use @ref OFileNotification::singleShot() then.
**/
int watch( const QString& path, bool sshot = false, OFileNotificationType type = Modify );
/**
* Stop watching for file events.
**/
void stop();
/**
* @returns the notification type as set by @ref start().
**/
OFileNotificationType type() const;
/**
* @returns the path to the file being watched by this instance.
**/
QString path() const;
/**
+ * @returns if the notification is single-shot
+ */
+ bool isSingleShot() const;
+ /**
* @returns if a file is currently being watched.
**/
bool isActive() const;
/**
* @internal
*/
int startWatching( const QString& path, bool sshot = false, OFileNotificationType type = Modify );
signals:
void triggered( const QString&, unsigned int, const QString& );
void accessed( const QString& );
void modified( const QString& );
void attributed( const QString& );
void closed( const QString&, bool );
void opened( 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& );
void deleted( const QString& );
void unmounted( const QString& );
protected:
bool activate( const OFileNotificationEvent* e );
private slots:
void inotifyEventHandler();
private:
bool registerEventHandler();
void unregisterEventHandler();
QString _path;
OFileNotificationType _type;
QSignal _signal;
bool _active;
bool _multi;
static QSocketNotifier* _sn;
int _wd; // inotify watch descriptor
static int _fd; // inotify device descriptor
friend class OFileNotificationEvent;
};
/*======================================================================================
* ODirNotification
*======================================================================================*/
/**
* @brief Represents a directory notification
*
* This class allows to watch for events happening to directories
* It uses the OFileNotification class
*
* @see http://www.kernel.org/pub/linux/kernel/people/rml/inotify/
*
* @author Michael 'Mickey' Lauer <mickey@vanille.de>
*
**/
class ODirNotification : public QObject
{
Q_OBJECT
public:
ODirNotification( QObject* parent = 0, const char* name = 0 );
~ODirNotification();
/**
* Starts to watch for @a type changes to @a path. Recurse @a recurse levels down the filesystem tree,
* use 0 for no recursion and -1 for unlimited recursion.
* Set @a sshot to True if you want to be notified only once.
**/
int watch( const QString& path, bool sshot = false, OFileNotificationType type = Modify, int recurse = 0 );
signals:
/**
* This signal is emitted if an event happens of the specified type happens to the directory being watched.
**/
void triggered( const QString&, unsigned int, const QString& );
void accessed( const QString& );
void modified( const QString& );
void attributed( const QString& );
void closed( const QString&, bool );
void opened( 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& );
void deleted( const QString& );
void unmounted( const QString& );
+
+ private slots:
+ void subdirCreated( const QString&, const QString& );
+
+ private:
+ OFileNotification* _topfilenotification;
+ OFileNotificationType _type;
+ int _depth;
};
/*======================================================================================
* OFileNotificationEvent
*======================================================================================*/
class OFileNotificationEvent
{
public:
OFileNotificationEvent( OFileNotification* parent, int wd, unsigned int mask, unsigned int cookie, const QString& name );
~OFileNotificationEvent();
OFileNotification* parent() const { return _parent; };
int descriptor() const { return _wd; };
unsigned int mask() const { return _mask; };
unsigned int cookie() const { return _cookie; };
QString name() const { return _name; };
void activate() { _parent->activate( this ); };
private:
OFileNotification* _parent;
int _wd;
unsigned int _mask;
unsigned int _cookie;
QString _name;
};
}
}
#endif