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.
|
|
|
|
*/
|
2013-02-25 12:54:19 +04:00
|
|
|
#include "config.h"
|
2012-07-20 19:13:23 +04:00
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
#include "mirall/folder.h"
|
|
|
|
#include "mirall/folderwatcher.h"
|
2012-03-28 14:23:34 +04:00
|
|
|
#include "mirall/mirallconfigfile.h"
|
2012-03-31 14:44:22 +04:00
|
|
|
#include "mirall/syncresult.h"
|
2011-02-17 02:21:45 +03:00
|
|
|
|
2012-05-21 18:48:49 +04:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QUrl>
|
2012-06-25 17:31:13 +04:00
|
|
|
#include <QFileSystemWatcher>
|
|
|
|
#include <QDir>
|
2012-05-21 18:48:49 +04:00
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
namespace Mirall {
|
|
|
|
|
2012-03-26 15:20:15 +04:00
|
|
|
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),
|
2011-04-05 14:16:24 +04:00
|
|
|
_path(path),
|
2012-03-26 15:20:15 +04:00
|
|
|
_secondPath(secondPath),
|
2011-04-05 14:16:24 +04:00
|
|
|
_pollTimer(new QTimer(this)),
|
2011-04-06 17:22:40 +04:00
|
|
|
_alias(alias),
|
|
|
|
_onlyOnlineEnabled(false),
|
2011-04-06 17:57:18 +04:00
|
|
|
_onlyThisLANEnabled(false),
|
2011-10-13 18:41:24 +04:00
|
|
|
_online(false),
|
2012-02-20 19:45:27 +04:00
|
|
|
_enabled(true)
|
2011-02-17 02:21:45 +03:00
|
|
|
{
|
2012-03-13 18:42:29 +04:00
|
|
|
qsrand(QTime::currentTime().msec());
|
2012-05-26 16:37:21 +04:00
|
|
|
MirallConfigFile cfgFile;
|
2012-03-08 14:39:31 +04:00
|
|
|
|
2011-04-06 12:40:15 +04:00
|
|
|
_pollTimer->setSingleShot(true);
|
2012-05-26 16:37:21 +04:00
|
|
|
int polltime = cfgFile.remotePollInterval()- 2000 + (int)( 4000.0*qrand()/(RAND_MAX+1.0));
|
|
|
|
qDebug() << "setting remote poll timer interval to" << polltime << "msec for folder " << alias;
|
2012-03-08 14:39:31 +04:00
|
|
|
_pollTimer->setInterval( polltime );
|
2012-02-28 19:49:13 +04:00
|
|
|
|
2011-04-05 14:16:24 +04:00
|
|
|
QObject::connect(_pollTimer, SIGNAL(timeout()), this, SLOT(slotPollTimerTimeout()));
|
|
|
|
_pollTimer->start();
|
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
_watcher = new Mirall::FolderWatcher(path, this);
|
2012-03-28 14:23:34 +04:00
|
|
|
|
|
|
|
MirallConfigFile cfg;
|
|
|
|
|
|
|
|
_watcher->setIgnoreListFile( cfg.excludeFile() );
|
|
|
|
|
2011-03-23 01:03:43 +03:00
|
|
|
QObject::connect(_watcher, SIGNAL(folderChanged(const QStringList &)),
|
|
|
|
SLOT(slotChanged(const QStringList &)));
|
2011-03-28 01:29:45 +04:00
|
|
|
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-03-28 01:29:45 +04:00
|
|
|
|
2012-04-21 23:03:29 +04:00
|
|
|
#if QT_VERSION >= 0x040700
|
2011-04-06 17:57:18 +04:00
|
|
|
_online = _networkMgr.isOnline();
|
|
|
|
QObject::connect(&_networkMgr, SIGNAL(onlineStateChanged(bool)), SLOT(slotOnlineChanged(bool)));
|
2012-04-21 23:03:29 +04:00
|
|
|
#else
|
|
|
|
_online = true;
|
|
|
|
#endif
|
2011-04-06 17:57:18 +04:00
|
|
|
|
2012-04-20 16:31:06 +04:00
|
|
|
_syncResult.setStatus( SyncResult::NotYetStarted );
|
2012-02-20 19:45:27 +04:00
|
|
|
|
2012-10-30 15:42:17 +04:00
|
|
|
// check if the local path exists
|
|
|
|
checkLocalPath();
|
2011-02-17 02:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Folder::~Folder()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-10-30 15:42:17 +04:00
|
|
|
void Folder::checkLocalPath()
|
|
|
|
{
|
|
|
|
QFileInfo fi(_path);
|
|
|
|
|
|
|
|
if( fi.isDir() && fi.isReadable() ) {
|
|
|
|
qDebug() << "Checked local path ok";
|
|
|
|
} else {
|
|
|
|
if( !fi.exists() ) {
|
2012-10-30 17:37:15 +04:00
|
|
|
// try to create the local dir
|
|
|
|
QDir d(_path);
|
|
|
|
if( d.mkpath(_path) ) {
|
|
|
|
qDebug() << "Successfully created the local dir " << _path;
|
2012-10-30 15:42:17 +04:00
|
|
|
}
|
|
|
|
}
|
2012-10-30 17:37:15 +04:00
|
|
|
// Check directory again
|
|
|
|
if( !fi.exists() ) {
|
|
|
|
_syncResult.setErrorString(tr("Local folder %1 does not exist.").arg(_path));
|
|
|
|
_syncResult.setStatus( SyncResult::SetupError );
|
|
|
|
} else if( !fi.isDir() ) {
|
|
|
|
_syncResult.setErrorString(tr("%1 should be a directory but is not.").arg(_path));
|
|
|
|
_syncResult.setStatus( SyncResult::SetupError );
|
|
|
|
} else if( !fi.isReadable() ) {
|
|
|
|
_syncResult.setErrorString(tr("%1 is not readable.").arg(_path));
|
|
|
|
_syncResult.setStatus( SyncResult::SetupError );
|
|
|
|
}
|
2012-10-30 15:42:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// if all is fine, connect a FileSystemWatcher
|
|
|
|
if( _syncResult.status() != SyncResult::SetupError ) {
|
|
|
|
_pathWatcher = new QFileSystemWatcher(this);
|
|
|
|
_pathWatcher->addPath( _path );
|
|
|
|
connect(_pathWatcher, SIGNAL(directoryChanged(QString)),
|
|
|
|
SLOT(slotLocalPathChanged(QString)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 11:52:02 +04:00
|
|
|
QString Folder::alias() const
|
|
|
|
{
|
|
|
|
return _alias;
|
|
|
|
}
|
|
|
|
|
2011-02-17 17:10:06 +03:00
|
|
|
QString Folder::path() const
|
|
|
|
{
|
2012-10-29 14:22:02 +04:00
|
|
|
QString p(_path);
|
|
|
|
if( ! p.endsWith(QLatin1Char('/')) ) {
|
|
|
|
p.append(QLatin1Char('/'));
|
|
|
|
}
|
|
|
|
return p;
|
2011-02-17 17:10:06 +03:00
|
|
|
}
|
|
|
|
|
2012-03-26 15:20:15 +04:00
|
|
|
QString Folder::secondPath() const
|
|
|
|
{
|
|
|
|
return _secondPath;
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:16:44 +04:00
|
|
|
QString Folder::nativePath() const
|
|
|
|
{
|
|
|
|
return QDir::toNativeSeparators(_path);
|
|
|
|
}
|
|
|
|
|
2011-10-13 18:41:24 +04:00
|
|
|
bool Folder::syncEnabled() const
|
|
|
|
{
|
|
|
|
return _enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Folder::setSyncEnabled( bool doit )
|
|
|
|
{
|
|
|
|
_enabled = doit;
|
2011-11-04 14:41:49 +04:00
|
|
|
_watcher->setEventsEnabled( doit );
|
2012-03-08 14:39:31 +04:00
|
|
|
if( doit && ! _pollTimer->isActive() ) {
|
|
|
|
_pollTimer->start();
|
|
|
|
}
|
2012-03-26 15:20:15 +04:00
|
|
|
|
2012-03-31 14:44:22 +04:00
|
|
|
qDebug() << "setSyncEnabled - ############################ " << doit;
|
2012-03-26 15:20:15 +04:00
|
|
|
if( doit ) {
|
|
|
|
// undefined until next sync
|
2012-03-31 14:44:22 +04:00
|
|
|
_syncResult.setStatus( SyncResult::NotYetStarted);
|
2012-04-17 15:16:48 +04:00
|
|
|
_syncResult.clearErrors();
|
2012-03-26 15:20:15 +04:00
|
|
|
evaluateSync( QStringList() );
|
|
|
|
} else {
|
2012-04-30 10:56:56 +04:00
|
|
|
// disable folder. Done through the _enabled-flag set above
|
2012-03-26 15:20:15 +04:00
|
|
|
}
|
2011-10-13 18:41:24 +04:00
|
|
|
}
|
|
|
|
|
2011-04-06 17:22:40 +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;
|
|
|
|
}
|
|
|
|
|
2013-02-10 17:57:57 +04:00
|
|
|
int Folder::pollInterval() const
|
2011-04-05 14:16:24 +04:00
|
|
|
{
|
2012-02-28 19:49:13 +04:00
|
|
|
return _pollTimer->interval();
|
2011-04-05 14:16:24 +04:00
|
|
|
}
|
|
|
|
|
2013-02-18 17:56:21 +04:00
|
|
|
void Folder::setSyncState(SyncResult::Status state)
|
|
|
|
{
|
|
|
|
_syncResult.setStatus(state);
|
|
|
|
}
|
|
|
|
|
2013-02-10 17:57:57 +04:00
|
|
|
void Folder::setPollInterval(int milliseconds)
|
2011-04-05 14:16:24 +04:00
|
|
|
{
|
2012-02-28 19:49:13 +04:00
|
|
|
_pollTimer->setInterval( milliseconds );
|
2011-04-05 14:16:24 +04:00
|
|
|
}
|
|
|
|
|
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 ) {
|
|
|
|
int interval = _watcher->eventInterval();
|
|
|
|
int newInt = 2*interval;
|
|
|
|
qDebug() << "Set new watcher interval to " << newInt;
|
|
|
|
_watcher->setEventInterval( newInt );
|
|
|
|
_errorCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-20 19:45:27 +04:00
|
|
|
SyncResult Folder::syncResult() const
|
2011-10-13 18:41:24 +04:00
|
|
|
{
|
2012-02-20 19:45:27 +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;
|
|
|
|
}
|
2012-02-28 19:49:13 +04:00
|
|
|
|
2012-03-12 20:37:18 +04:00
|
|
|
// stop the poll timer here. Its started again in the slot of
|
|
|
|
// sync finished.
|
|
|
|
qDebug() << "* " << alias() << "Poll timer disabled";
|
|
|
|
_pollTimer->stop();
|
2012-03-31 14:44:22 +04:00
|
|
|
|
|
|
|
_syncResult.setStatus( SyncResult::NotYetStarted );
|
2012-03-29 12:13:19 +04:00
|
|
|
emit scheduleToSync( alias() );
|
2012-03-31 14:44:22 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-05 14:16:24 +04:00
|
|
|
void Folder::slotPollTimerTimeout()
|
|
|
|
{
|
2011-04-06 12:40:15 +04:00
|
|
|
qDebug() << "* Polling" << alias() << "for changes. Ignoring all pending events until now";
|
2011-04-05 14:16:24 +04:00
|
|
|
_watcher->clearPendingEvents();
|
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;
|
2011-04-05 14:16:24 +04:00
|
|
|
}
|
|
|
|
|
2011-03-23 01:03:43 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-02-17 17:10:06 +03:00
|
|
|
void Folder::slotSyncStarted()
|
|
|
|
{
|
2011-03-27 04:26:41 +04:00
|
|
|
// disable events until syncing is done
|
|
|
|
_watcher->setEventsEnabled(false);
|
2011-02-17 17:10:06 +03:00
|
|
|
}
|
|
|
|
|
2011-04-08 13:36:53 +04:00
|
|
|
void Folder::slotSyncFinished(const SyncResult &result)
|
2011-02-17 17:10:06 +03:00
|
|
|
{
|
2012-12-06 21:38:45 +04:00
|
|
|
_watcher->setEventsEnabledDelayed(2000);
|
2011-12-12 20:47:30 +04:00
|
|
|
|
2012-04-17 17:02:18 +04:00
|
|
|
qDebug() << "OO folder slotSyncFinished: result: " << int(result.status()) << " local: " << result.localRunOnly();
|
2012-02-29 18:25:16 +04:00
|
|
|
emit syncStateChange();
|
|
|
|
|
|
|
|
// reenable the poll timer if folder is sync enabled
|
|
|
|
if( syncEnabled() ) {
|
2012-03-08 14:39:31 +04:00
|
|
|
qDebug() << "* " << alias() << "Poll timer enabled with " << _pollTimer->interval() << "milliseconds";
|
2012-02-29 18:25:16 +04:00
|
|
|
_pollTimer->start();
|
|
|
|
} else {
|
|
|
|
qDebug() << "* Not enabling poll timer for " << alias();
|
|
|
|
_pollTimer->stop();
|
|
|
|
}
|
2011-02-17 17:10:06 +03:00
|
|
|
}
|
2011-02-17 02:21:45 +03:00
|
|
|
|
2012-06-25 17:31:13 +04:00
|
|
|
void Folder::slotLocalPathChanged( const QString& dir )
|
|
|
|
{
|
|
|
|
QDir notifiedDir(dir);
|
|
|
|
QDir localPath(_path );
|
|
|
|
|
|
|
|
if( notifiedDir == localPath ) {
|
|
|
|
if( !localPath.exists() ) {
|
|
|
|
qDebug() << "ALARM: The local path was DELETED!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-22 15:29:50 +04:00
|
|
|
void Folder::setConfigFile( const QString& file )
|
|
|
|
{
|
|
|
|
_configFile = file;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Folder::configFile()
|
|
|
|
{
|
|
|
|
return _configFile;
|
|
|
|
}
|
|
|
|
|
2011-10-18 12:22:24 +04:00
|
|
|
void Folder::setBackend( const QString& b )
|
|
|
|
{
|
|
|
|
_backend = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Folder::backend() const
|
|
|
|
{
|
|
|
|
return _backend;
|
|
|
|
}
|
|
|
|
|
2012-06-11 12:10:07 +04:00
|
|
|
void Folder::wipe()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
} // namespace Mirall
|
|
|
|
|