summaryrefslogtreecommitdiff
path: root/libopie2/opiecore/ofilenotify.cpp
Side-by-side diff
Diffstat (limited to 'libopie2/opiecore/ofilenotify.cpp') (more/less context) (show whitespace changes)
-rw-r--r--libopie2/opiecore/ofilenotify.cpp31
1 files changed, 23 insertions, 8 deletions
diff --git a/libopie2/opiecore/ofilenotify.cpp b/libopie2/opiecore/ofilenotify.cpp
index efd041a..11d4f87 100644
--- a/libopie2/opiecore/ofilenotify.cpp
+++ b/libopie2/opiecore/ofilenotify.cpp
@@ -1,263 +1,278 @@
/*
                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.
*/
#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 {
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 ) )
{
qWarning( "OFileNotification::watch(): Can't watch '%s': %s.", (const char*) path, "not a regular file" );
return -1;
}
- if ( notification_list.isEmpty() )
- {
- OFileNotification::registerEventHandler();
- }
-
return startWatching( path, sshot, type );
}
int OFileNotification::startWatching( const QString& path, bool sshot, OFileNotificationType type )
{
+ if ( notification_list.isEmpty() )
+ {
+ OFileNotification::registerEventHandler();
+ }
+
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::activate()
{
emit triggered( _path );
_signal.activate();
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()
{
qWarning( "OFileNotification::__eventHandler(): reached." );
char buffer[16384];
size_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 )
{
/* Parse events and queue them ! */
pevent = (struct inotify_event *)&buffer[buffer_i];
event_size = sizeof(struct inotify_event) + pevent->len;
qDebug( "pevent->len = %d\n", pevent->len);
OFileNotification* fn = notification_list[ pevent->wd ];
if ( fn )
fn->activate();
else
assert( false );
//event = malloc(event_size);
//memmove(event, pevent, event_size);
//queue_enqueue(event, q);
buffer_i += event_size;
count++;
}
qDebug( "received %d events...", count );
return;
}
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 )
{
qDebug( "ODirNotification::ODirNotification()" );
}
ODirNotification::~ODirNotification()
{
qDebug( "ODirNotification::~ODirNotification()" );
}
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 );
- return 0;
-}
+ if ( recurse == 0 )
+ {
+ OFileNotification* fn = new OFileNotification( this, "ODirNotification delegate" );
+ int result = fn->startWatching( path, sshot, type );
+ if ( result != -1 )
+ {
+ connect( fn, SIGNAL( triggered( const QString& ) ), this, SIGNAL( triggered( const QString& ) ) );
+ return result;
+ }
+ }
+ else
+ {
+ qDebug( "ODirNotification::watch(), recursion not yet implemented... :)" );
+ return -1;
}
-
} \ No newline at end of file
+
+} // namespace Ui
+
+} // namespace Opie