nextcloud-desktop/src/mirall/folder.cpp

264 lines
6 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
#include <QDebug>
#include <QTimer>
2011-02-17 02:21:45 +03:00
#include <QUrl>
#include "mirall/folder.h"
#include "mirall/folderwatcher.h"
#include "mirall/mirallconfigfile.h"
#include "mirall/syncresult.h"
2011-02-17 02:21:45 +03:00
#define DEFAULT_POLL_INTERVAL_SEC 15000
2011-02-17 02:21:45 +03:00
namespace Mirall {
Folder::Folder(const QString &alias, const QString &path, const QString& secondPath, QObject *parent)
2011-02-17 02:21:45 +03:00
: QObject(parent),
2011-09-27 10:15:30 +04:00
_errorCount(0),
_path(path),
_secondPath(secondPath),
_pollTimer(new QTimer(this)),
_alias(alias),
_onlyOnlineEnabled(false),
2011-04-06 17:57:18 +04:00
_onlyThisLANEnabled(false),
2011-10-13 18:41:24 +04:00
_online(false),
_enabled(true)
2011-02-17 02:21:45 +03:00
{
qsrand(QTime::currentTime().msec());
_pollTimer->setSingleShot(true);
int polltime = DEFAULT_POLL_INTERVAL_SEC - 2000+ (int)( 4000.0*qrand()/(RAND_MAX+1.0));
_pollTimer->setInterval( polltime );
QObject::connect(_pollTimer, SIGNAL(timeout()), this, SLOT(slotPollTimerTimeout()));
_pollTimer->start();
2012-04-15 16:47:43 +04:00
#ifdef USE_INOTIFY
2011-02-17 02:21:45 +03:00
_watcher = new Mirall::FolderWatcher(path, this);
MirallConfigFile cfg;
_watcher->setIgnoreListFile( cfg.excludeFile() );
QObject::connect(_watcher, SIGNAL(folderChanged(const QStringList &)),
SLOT(slotChanged(const QStringList &)));
#endif
QObject::connect(this, SIGNAL(syncStarted()),
SLOT(slotSyncStarted()));
2011-04-08 13:36:53 +04:00
QObject::connect(this, SIGNAL(syncFinished(const SyncResult &)),
SLOT(slotSyncFinished(const SyncResult &)));
2011-04-06 17:57:18 +04:00
_online = _networkMgr.isOnline();
QObject::connect(&_networkMgr, SIGNAL(onlineStateChanged(bool)), SLOT(slotOnlineChanged(bool)));
_syncResult = SyncResult( SyncResult::NotYetStarted );
2011-02-17 02:21:45 +03:00
}
Folder::~Folder()
{
}
2011-04-06 11:52:02 +04:00
QString Folder::alias() const
{
return _alias;
}
QString Folder::path() const
{
return _path;
}
QString Folder::secondPath() const
{
return _secondPath;
}
2011-10-13 18:41:24 +04:00
bool Folder::syncEnabled() const
{
return _enabled;
}
void Folder::setSyncEnabled( bool doit )
{
_enabled = doit;
2012-04-15 16:47:43 +04:00
#ifdef USE_INOTIFY
2011-11-04 14:41:49 +04:00
_watcher->setEventsEnabled( doit );
#endif
if( doit && ! _pollTimer->isActive() ) {
_pollTimer->start();
}
qDebug() << "setSyncEnabled - ############################ " << doit;
if( doit ) {
// undefined until next sync
_syncResult.setStatus( SyncResult::NotYetStarted);
evaluateSync( QStringList() );
} else {
// disabled.
_syncResult.setStatus( SyncResult::Disabled );
}
2011-10-13 18:41:24 +04:00
}
bool Folder::onlyOnlineEnabled() const
{
return _onlyOnlineEnabled;
}
void Folder::setOnlyOnlineEnabled(bool enabled)
{
_onlyOnlineEnabled = enabled;
}
bool Folder::onlyThisLANEnabled() const
{
return _onlyThisLANEnabled;
}
void Folder::setOnlyThisLANEnabled(bool enabled)
{
_onlyThisLANEnabled = enabled;
}
int Folder::pollInterval() const
{
return _pollTimer->interval();
}
void Folder::setPollInterval(int milliseconds)
{
_pollTimer->setInterval( milliseconds );
}
2011-09-27 10:15:30 +04:00
int Folder::errorCount()
{
return _errorCount;
}
void Folder::resetErrorCount()
{
_errorCount = 0;
}
void Folder::incrementErrorCount()
{
// if the error count gets higher than three, the interval timer
// of the watcher is doubled.
_errorCount++;
if( _errorCount > 1 ) {
2012-04-15 16:47:43 +04:00
#ifdef USE_INOTIFY
2011-09-27 10:15:30 +04:00
int interval = _watcher->eventInterval();
int newInt = 2*interval;
qDebug() << "Set new watcher interval to " << newInt;
_watcher->setEventInterval( newInt );
#endif
2011-09-27 10:15:30 +04:00
_errorCount = 0;
}
}
SyncResult Folder::syncResult() const
2011-10-13 18:41:24 +04:00
{
return _syncResult;
2011-10-13 18:41:24 +04:00
}
2011-04-06 17:57:18 +04:00
void Folder::evaluateSync(const QStringList &pathList)
{
2011-10-13 18:41:24 +04:00
if( !_enabled ) {
qDebug() << "*" << alias() << "sync skipped, disabled!";
return;
}
2011-09-27 10:15:30 +04:00
if (!_online && onlyOnlineEnabled()) {
qDebug() << "*" << alias() << "sync skipped, not online";
return;
}
// stop the poll timer here. Its started again in the slot of
// sync finished.
qDebug() << "* " << alias() << "Poll timer disabled";
_pollTimer->stop();
_syncResult.setStatus( SyncResult::NotYetStarted );
emit scheduleToSync( alias() );
}
void Folder::startSync( const QStringList &pathList )
{
_syncResult = SyncResult( SyncResult::SyncRunning );
emit syncStateChange();
2011-04-06 17:57:18 +04:00
}
void Folder::slotPollTimerTimeout()
{
qDebug() << "* Polling" << alias() << "for changes. Ignoring all pending events until now";
2012-04-15 16:47:43 +04:00
#ifdef USE_INOTIFY
_watcher->clearPendingEvents();
#endif
2011-04-06 17:57:18 +04:00
evaluateSync(QStringList());
}
void Folder::slotOnlineChanged(bool online)
{
qDebug() << "* " << alias() << "is" << (online ? "now online" : "no longer online");
_online = online;
}
void Folder::slotChanged(const QStringList &pathList)
2011-02-17 02:21:45 +03:00
{
2012-02-16 01:36:52 +04:00
qDebug() << "** Changed was notified on " << pathList;
2011-04-06 17:57:18 +04:00
evaluateSync(pathList);
2011-02-17 02:21:45 +03:00
}
void Folder::slotSyncStarted()
{
// disable events until syncing is done
2012-04-15 16:47:43 +04:00
#ifdef USE_INOTIFY
_watcher->setEventsEnabled(false);
#endif
}
2011-04-08 13:36:53 +04:00
void Folder::slotSyncFinished(const SyncResult &result)
{
2012-04-15 16:47:43 +04:00
#ifdef USE_INOTIFY
_watcher->setEventsEnabled(true);
#endif
_syncResult = result;
emit syncStateChange();
// reenable the poll timer if folder is sync enabled
if( syncEnabled() ) {
qDebug() << "* " << alias() << "Poll timer enabled with " << _pollTimer->interval() << "milliseconds";
_pollTimer->start();
} else {
qDebug() << "* Not enabling poll timer for " << alias();
_pollTimer->stop();
}
}
2011-02-17 02:21:45 +03:00
2011-10-18 12:22:24 +04:00
void Folder::setBackend( const QString& b )
{
_backend = b;
}
QString Folder::backend() const
{
return _backend;
}
2011-02-17 02:21:45 +03:00
} // namespace Mirall