nextcloud-desktop/src/mirall/folderwatcher.cpp

166 lines
4.1 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
#ifdef NEW_FW_BEHAVIOR
#if defined(Q_OS_WIN)
#include "mirall/folderwatcher_win.cpp"
#elif defined(Q_OS_MAC)
#include "mirall/folderwatcher_mac.cpp"
#endif
#endif
2012-04-15 16:47:43 +04:00
#ifdef USE_INOTIFY
2012-12-04 20:46:44 +04:00
#include "mirall/folderwatcher_inotify.cpp"
#else
2012-12-04 20:46:44 +04:00
#include "mirall/folderwatcher_dummy.cpp"
#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)),
_lastMask(0),
_initialSyncDone(false)
2011-02-17 02:21:45 +03:00
{
2012-12-04 20:46:44 +04:00
setupBackend();
// do a first synchronization to get changes while
// the application was not running
setProcessTimer();
2011-02-17 02:21:45 +03:00
}
FolderWatcher::~FolderWatcher()
{
}
2011-03-21 00:18:38 +03:00
QString FolderWatcher::root() const
{
return _root;
}
void FolderWatcher::setIgnoreListFile( 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();
2012-08-17 19:13:17 +04:00
if( !line.startsWith( QLatin1Char('#') )) {
addIgnore(line);
}
}
}
void FolderWatcher::addIgnore(const QString &pattern)
{
if( pattern.isEmpty() ) return;
_ignores.append(pattern);
}
bool FolderWatcher::eventsEnabled() const
{
return _eventsEnabled;
}
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() || !_initialSyncDone) {
QStringList notifyPaths = _pendingPathes.keys();
_pendingPathes.clear();
//qDebug() << lastEventTime << eventTime;
qDebug() << " * Notify" << notifyPaths.size() << "changed items for" << root();
emit folderChanged(notifyPaths);
_initialSyncDone = true;
}
}
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() << "seconds ("
<< QTime::currentTime().addSecs(eventInterval()).toString(QLatin1String("HH:mm:ss"))
<< ")." << _pendingPathes.size() << "events until now )";
}
_processTimer->start(eventInterval());
}
2012-12-04 20:46:44 +04:00
} // namespace Mirall
2011-02-17 02:21:45 +03:00