nextcloud-desktop/src/mirall/folderwatcher.cpp

177 lines
4.5 KiB
C++
Raw Normal View History

2011-04-06 13:48:02 +04:00
/*
* Copyright (C) by Duncan Mac-Vicar P. <duncan@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License
* for more details.
*/
2011-02-17 02:21:45 +03:00
// event masks
2012-05-21 18:48:49 +04:00
#include "mirall/folderwatcher.h"
#include "mirall/folder.h"
#include "mirall/inotify.h"
#include "mirall/fileutils.h"
#include <stdint.h>
2011-02-17 02:21:45 +03:00
#include <QFileInfo>
#include <QFlags>
#include <QDebug>
#include <QDir>
#include <QMutexLocker>
#include <QStringList>
#include <QTimer>
2011-02-17 02:21:45 +03:00
2012-12-04 20:46:44 +04:00
/* minimum amount of seconds between two
events to consider it a new event */
#define DEFAULT_EVENT_INTERVAL_MSEC 1000
#if defined(Q_OS_WIN)
#include "mirall/folderwatcher_win.h"
2012-12-04 20:46:44 +04:00
#elif defined(Q_OS_MAC)
#include "mirall/folderwatcher_mac.h"
#elif defined(USE_INOTIFY)
2013-07-22 22:27:42 +04:00
#include "mirall/folderwatcher_inotify.h"
2013-07-24 16:07:30 +04:00
#endif
2011-02-17 02:21:45 +03:00
namespace Mirall {
FolderWatcher::FolderWatcher(const QString &root, QObject *parent)
: QObject(parent),
_eventsEnabled(true),
_eventInterval(DEFAULT_EVENT_INTERVAL_MSEC),
_root(root),
_processTimer(new QTimer(this))
2011-02-17 02:21:45 +03:00
{
_d = new FolderWatcherPrivate(this);
_processTimer->setSingleShot(true);
QObject::connect(_processTimer, SIGNAL(timeout()), this, SLOT(slotProcessTimerTimeout()));
// do a first synchronization to get changes while
// the application was not running
setProcessTimer();
2011-02-17 02:21:45 +03:00
}
FolderWatcher::~FolderWatcher()
{
2012-12-04 21:15:37 +04:00
delete _d;
2011-02-17 02:21:45 +03:00
}
2011-03-21 00:18:38 +03:00
QString FolderWatcher::root() const
{
return _root;
}
void FolderWatcher::addIgnoreListFile( const QString& file )
{
if( file.isEmpty() ) return;
QFile infile( file );
if (!infile.open(QIODevice::ReadOnly | QIODevice::Text))
return;
while (!infile.atEnd()) {
QString line = QString::fromLocal8Bit( infile.readLine() ).trimmed();
if( !line.startsWith( QLatin1Char('#') ) && line.isEmpty() ) {
_ignores.append(line);
}
}
}
QStringList FolderWatcher::ignores() const
{
return _ignores;
}
bool FolderWatcher::eventsEnabled() const
{
return _eventsEnabled;
}
void FolderWatcher::setEventsEnabledDelayed( int delay_msec )
{
qDebug() << "Starting Event logging again in " << delay_msec << " milliseconds";
QTimer::singleShot( delay_msec, this, SLOT(setEventsEnabled()));
}
void FolderWatcher::setEventsEnabled(bool enabled)
{
qDebug() << " * event notification " << (enabled ? "enabled" : "disabled");
_eventsEnabled = enabled;
if (_eventsEnabled) {
// schedule a queue cleanup for accumulated events
2012-02-16 01:36:52 +04:00
if ( _pendingPathes.empty() )
return;
setProcessTimer();
}
else
{
// if we are disabling events, clear any ongoing timer
if (_processTimer->isActive())
_processTimer->stop();
}
}
void FolderWatcher::clearPendingEvents()
{
if (_processTimer->isActive())
_processTimer->stop();
2012-02-16 01:36:52 +04:00
_pendingPathes.clear();
}
int FolderWatcher::eventInterval() const
{
return _eventInterval;
}
void FolderWatcher::setEventInterval(int seconds)
{
_eventInterval = seconds;
}
void FolderWatcher::slotProcessTimerTimeout()
{
qDebug() << "* Processing of event queue for" << root();
2012-02-16 01:36:52 +04:00
if (!_pendingPathes.empty() ) {
2012-02-16 01:36:52 +04:00
QStringList notifyPaths = _pendingPathes.keys();
_pendingPathes.clear();
//qDebug() << lastEventTime << eventTime;
qDebug() << " * Notify" << notifyPaths.size() << "change items for" << root();
emit folderChanged(notifyPaths);
}
}
void FolderWatcher::setProcessTimer()
{
if (!_processTimer->isActive()) {
2012-08-17 19:13:17 +04:00
qDebug() << "* Pending events for" << root()
<< "will be processed after events stop for"
<< eventInterval() << "milliseconds ("
2012-08-17 19:13:17 +04:00
<< QTime::currentTime().addSecs(eventInterval()).toString(QLatin1String("HH:mm:ss"))
<< ")." << _pendingPathes.size() << "events until now )";
}
_processTimer->start(eventInterval());
}
2012-12-06 19:26:27 +04:00
void FolderWatcher::changeDetected(const QString& f)
{
if( ! eventsEnabled() ) {
2013-03-25 21:23:01 +04:00
// qDebug() << "FolderWatcher::changeDetected when eventsEnabled() -> ignore";
return;
}
_pendingPathes[f] = 1; //_pendingPathes[path]+mask;
setProcessTimer();
}
2012-12-04 20:46:44 +04:00
} // namespace Mirall
2011-02-17 02:21:45 +03:00