Added #ifdef USE_WATCHER to switch between INotify and Polling

depending on the platform environment var set by Qt.
This commit is contained in:
Klaas Freitag 2012-02-29 15:25:16 +01:00
parent f6846a2107
commit 1fc19647e5
9 changed files with 73 additions and 23 deletions

View File

@ -196,17 +196,21 @@ void Folder::slotSyncStarted()
void Folder::slotSyncFinished(const SyncResult &result)
{
_syncResult = result;
emit syncStateChange();
#ifdef USE_WATCHER
_watcher->setEventsEnabled(true);
#endif
// reenable the poll timer if folder is sync enabled
if( syncEnabled() ) {
qDebug() << "* " << alias() << "Poll timer enabled";
_pollTimer->start();
} else {
qDebug() << "* Not enabling poll timer for " << alias();
_pollTimer->stop();
}
_syncResult = result;
emit syncStateChange();
// reenable the poll timer if folder is sync enabled
if( syncEnabled() ) {
qDebug() << "* " << alias() << "Poll timer enabled with " << _pollTimer->interval() << "seconds";
_pollTimer->start();
} else {
qDebug() << "* Not enabling poll timer for " << alias();
_pollTimer->stop();
}
}
void Folder::setBackend( const QString& b )

View File

@ -23,13 +23,25 @@
#include "mirall/syncresult.h"
/*
* Flag to enable the folder watcher instead of the local polling to detect
* changes in the local path.
*/
#define USE_WATCHER 1
#ifdef Q_WS_WIN
#define USE_WATCHER 0
#endif
class QAction;
class QTimer;
class QIcon;
namespace Mirall {
#ifdef USE_WATCHER
class FolderWatcher;
#endif
class Folder : public QObject
{

View File

@ -25,6 +25,7 @@
#include "mirall/owncloudfolder.h"
#include "mirall/syncresult.h"
#include "mirall/folderman.h"
#include "mirall/inotify.h"
namespace Mirall {
@ -37,6 +38,10 @@ FolderMan::FolderMan(QObject *parent) :
storageDir.mkpath("folders");
_folderConfigPath = QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/folders";
#ifdef USE_WATCHER
Mirall::INotify::initialize();
#endif
_folderChangeSignalMapper = new QSignalMapper(this);
connect(_folderChangeSignalMapper, SIGNAL(mapped(const QString &)),
this, SIGNAL(folderSyncStateChange(const QString &)));

View File

@ -48,7 +48,8 @@ FolderWatcher::FolderWatcher(const QString &root, QObject *parent)
// this is not the best place for this
addIgnore("/**/.unison*");
addIgnore("*csync_timediff.ctmp*");
addIgnore(".*.sw?"); // vi swap files
#ifdef USE_WATCHER
_processTimer->setSingleShot(true);
QObject::connect(_processTimer, SIGNAL(timeout()), this, SLOT(slotProcessTimerTimeout()));
@ -56,6 +57,7 @@ FolderWatcher::FolderWatcher(const QString &root, QObject *parent)
slotAddFolderRecursive(root);
QObject::connect(_inotify, SIGNAL(notifyEvent(int, int, const QString &)),
SLOT(slotINotifyEvent(int, int, const QString &)));
#endif
// do a first synchronization to get changes while
// the application was not running
setProcessTimer();
@ -118,13 +120,16 @@ void FolderWatcher::setEventInterval(int seconds)
QStringList FolderWatcher::folders() const
{
#ifdef USE_WATCHER
return _inotify->directories();
#endif
}
void FolderWatcher::slotAddFolderRecursive(const QString &path)
{
int subdirs = 0;
qDebug() << "(+) Watcher:" << path;
#ifdef USE_WATCHER
_inotify->addPath(path);
QStringList watchedFolders(_inotify->directories());
//qDebug() << "currently watching " << watchedFolders;
@ -151,6 +156,9 @@ void FolderWatcher::slotAddFolderRecursive(const QString &path)
}
if (subdirs >0)
qDebug() << " `-> and" << subdirs << "subdirectories";
#else
qDebug() << "** Watcher is disabled!";
#endif
}
void FolderWatcher::slotINotifyEvent(int mask, int cookie, const QString &path)
@ -161,7 +169,9 @@ void FolderWatcher::slotINotifyEvent(int mask, int cookie, const QString &path)
_lastMask = mask;
_lastPath = path;
// qDebug() << "** Inotify Event " << mask << " on " << path;
if( ! eventsEnabled() ) return;
qDebug() << "** Inotify Event " << mask << " on " << path;
// cancel close write events that come after create
if (lastMask == IN_CREATE && mask == IN_CLOSE_WRITE
&& lastPath == path ) {
@ -187,10 +197,12 @@ void FolderWatcher::slotINotifyEvent(int mask, int cookie, const QString &path)
}
else if (mask & IN_DELETE) {
//qDebug() << cookie << " DELETE: " << path;
#ifdef USE_WATCHER
if (_inotify->directories().contains(path) &&
QFileInfo(path).isDir());
qDebug() << "(-) Watcher:" << path;
_inotify->removePath(path);
#endif
}
else if (mask & IN_CLOSE_WRITE) {
//qDebug() << cookie << " WRITABLE CLOSED: " << path;

View File

@ -22,8 +22,12 @@
#include <QTime>
#include <QHash>
#include "mirall/folder.h"
class QTimer;
#ifdef USE_WATCHER
class INotify;
#endif
namespace Mirall {
@ -112,8 +116,9 @@ protected slots:
private:
bool _eventsEnabled;
int _eventInterval;
#ifdef USE_WATCHER
INotify *_inotify;
#endif
QString _root;
// paths pending to notified
// QStringList _pendingPaths;

View File

@ -27,7 +27,6 @@
#define DEFAULT_READ_BUFFERSIZE 2048
namespace Mirall {
// Allocate space for static members of class.
int INotify::s_fd;
INotify::INotifyThread* INotify::s_thread;

View File

@ -73,7 +73,6 @@ private:
int _mask;
QMap<QString, int> _wds;
};
}
#endif

View File

@ -40,18 +40,27 @@ ownCloudFolder::ownCloudFolder(const QString &alias,
, _lastWalkedFiles(-1)
{
connect( _pollTimer, SIGNAL(timeout()), this, SLOT(slotPollTimerRemoteCheck()));
qDebug() << "****** connect ownCloud Timer";
#ifdef USE_WATCHER
setPollInterval( 15000 );
qDebug() << "****** ownCloud folder using watcher *******";
#else
/* If local polling is used, the polltimer of class Folder has to fire more
* often
* Set a local poll time of 2000 milliseconds, which results in a 30 seconds
* remote poll interval, defined in slotPollTimerRemoteCheck
*/
// set a local poll time of 2000 milliseconds, which results in a 30 seconds
// remote poll interval, defined in slotPollTimerRemoteCheck
connect( _pollTimer, SIGNAL(timeout()), this, SLOT(slotPollTimerRemoteCheck()));
setPollInterval( 2000 );
qDebug() << "****** ownCloud folder using local poll *******";
#endif
}
ownCloudFolder::~ownCloudFolder()
{
}
#ifndef USE_WATCHER
void ownCloudFolder::slotPollTimerRemoteCheck()
{
_localCheckOnly = true;
@ -62,6 +71,7 @@ void ownCloudFolder::slotPollTimerRemoteCheck()
}
qDebug() << "**** CSyncFolder Poll Timer check: " << _pollTimerCnt << " - " << _localCheckOnly;
}
#endif
bool ownCloudFolder::isBusy() const
{
@ -110,6 +120,7 @@ void ownCloudFolder::slotCSyncFinished()
else
qDebug() << " * owncloud csync thread finished successfully";
#ifndef USE_WATCHER
if( _csync->hasLocalChanges( _lastWalkedFiles ) ) {
qDebug() << "Last walked files: " << _lastWalkedFiles << " against " << _csync->walkedFiles();
qDebug() << "*** Local changes, lets do a full sync!" ;
@ -121,10 +132,12 @@ void ownCloudFolder::slotCSyncFinished()
qDebug() << " *** Finalize, pollTimerCounter is "<< _pollTimerCnt ;
_lastWalkedFiles = _csync->walkedFiles();
// TODO delete thread
emit syncFinished(_csync->error() ?
SyncResult(SyncResult::Error)
: SyncResult(SyncResult::Success));
}
#endif
emit syncFinished(_csync->error() ?
SyncResult(SyncResult::Error)
: SyncResult(SyncResult::Success));
}
} // ns

View File

@ -45,8 +45,9 @@ public slots:
protected slots:
void slotCSyncStarted();
void slotCSyncFinished();
#ifndef USE_WATCHER
void slotPollTimerRemoteCheck();
#endif
private:
QString _secondPath;
CSyncThread *_csync;