-rw-r--r-- | libopie2/opiecore/linux/ofilenotify.cpp | 79 | ||||
-rw-r--r-- | libopie2/opiecore/linux/ofilenotify.h | 13 |
2 files changed, 66 insertions, 26 deletions
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 @@ -228,2 +228,8 @@ QString OFileNotification::path() const +bool OFileNotification::isSingleShot() const +{ + return !_multi; +} + + bool OFileNotification::activate( const OFileNotificationEvent* e ) @@ -333,3 +339,3 @@ void OFileNotification::unregisterEventHandler() ODirNotification::ODirNotification( QObject* parent, const char* name ) - :QObject( parent, name ) + :QObject( parent, name ), _topfilenotification( 0 ), _type( Nothing ), _depth( -123 ) { @@ -344,2 +350,20 @@ 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. +*/ @@ -347,8 +371,14 @@ int ODirNotification::watch( const QString& path, bool sshot, OFileNotificationT { - 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& ) ) ); @@ -368,4 +398,6 @@ int ODirNotification::watch( const QString& path, bool sshot, OFileNotificationT - if ( recurse ) + if ( recurse != 0 ) { + connect( fn, SIGNAL( createdSubdir( const QString&, const QString& ) ), this, SLOT( subdirCreated( const QString&, const QString& ) ) ); + QDir directory( path ); @@ -377,3 +409,3 @@ int ODirNotification::watch( const QString& path, bool sshot, OFileNotificationT 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 ) @@ -385,3 +417,2 @@ int ODirNotification::watch( const QString& path, bool sshot, OFileNotificationT } -//connect( fn, SIGNAL( triggered( const QString&, unsigned int, const QString& ) ), this, SIGNAL( triggered( const QString&, unsigned int, const QString& ) ) ); } @@ -391,23 +422,19 @@ int ODirNotification::watch( const QString& path, bool sshot, OFileNotificationT -// 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. -*/ + } +} 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 @@ -113,2 +113,3 @@ enum OFileNotificationType { + Nothing = 0, Access = IN_ACCESS, @@ -200,2 +201,6 @@ class OFileNotification : public QObject /** + * @returns if the notification is single-shot + */ + bool isSingleShot() const; + /** * @returns if a file is currently being watched. @@ -294,2 +299,10 @@ class ODirNotification : public QObject void unmounted( const QString& ); + + private slots: + void subdirCreated( const QString&, const QString& ); + + private: + OFileNotification* _topfilenotification; + OFileNotificationType _type; + int _depth; }; |