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 <QAction>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QMutexLocker>
|
2011-04-05 14:16:24 +04:00
|
|
|
#include <QTimer>
|
2011-02-17 02:21:45 +03:00
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
#include "mirall/constants.h"
|
|
|
|
#include "mirall/folder.h"
|
|
|
|
#include "mirall/folderwatcher.h"
|
|
|
|
|
2011-09-26 15:12:00 +04:00
|
|
|
#define DEFAULT_POLL_INTERVAL_SEC 10
|
2011-04-05 14:16:24 +04:00
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
namespace Mirall {
|
|
|
|
|
2011-04-06 11:52:02 +04:00
|
|
|
Folder::Folder(const QString &alias, const QString &path, 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),
|
|
|
|
_pollTimer(new QTimer(this)),
|
2011-04-06 11:52:02 +04:00
|
|
|
_pollInterval(DEFAULT_POLL_INTERVAL_SEC),
|
2011-04-06 17:22:40 +04:00
|
|
|
_alias(alias),
|
|
|
|
_onlyOnlineEnabled(false),
|
2011-04-06 17:57:18 +04:00
|
|
|
_onlyThisLANEnabled(false),
|
|
|
|
_online(false)
|
2011-02-17 02:21:45 +03:00
|
|
|
{
|
2011-04-06 14:09:56 +04:00
|
|
|
_openAction = new QAction(QIcon::fromTheme(FOLDER_ICON), path, this);
|
2011-02-17 17:10:06 +03:00
|
|
|
_openAction->setIconVisibleInMenu(true);
|
2011-04-06 14:09:56 +04:00
|
|
|
_openAction->setIcon(QIcon::fromTheme(FOLDER_ICON));
|
2011-02-17 17:10:06 +03:00
|
|
|
|
2011-02-17 13:27:05 +03:00
|
|
|
QObject::connect(_openAction, SIGNAL(triggered(bool)), SLOT(slotOpenFolder()));
|
2011-02-17 02:21:45 +03:00
|
|
|
|
2011-04-06 12:40:15 +04:00
|
|
|
_pollTimer->setSingleShot(true);
|
2011-04-05 14:16:24 +04:00
|
|
|
_pollTimer->setInterval(pollInterval() * 1000);
|
|
|
|
QObject::connect(_pollTimer, SIGNAL(timeout()), this, SLOT(slotPollTimerTimeout()));
|
|
|
|
_pollTimer->start();
|
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
_watcher = new Mirall::FolderWatcher(path, this);
|
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
|
|
|
|
2011-04-06 17:57:18 +04:00
|
|
|
_online = _networkMgr.isOnline();
|
|
|
|
QObject::connect(&_networkMgr, SIGNAL(onlineStateChanged(bool)), SLOT(slotOnlineChanged(bool)));
|
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
}
|
|
|
|
|
2011-02-17 13:27:05 +03:00
|
|
|
QAction * Folder::openAction() const
|
2011-02-17 02:21:45 +03:00
|
|
|
{
|
2011-02-17 13:27:05 +03:00
|
|
|
return _openAction;
|
2011-02-17 02:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Folder::~Folder()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return _path;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-04-05 14:16:24 +04:00
|
|
|
int Folder::pollInterval() const
|
|
|
|
{
|
|
|
|
return _pollInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Folder::setPollInterval(int seconds)
|
|
|
|
{
|
|
|
|
_pollInterval = seconds;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 17:57:18 +04:00
|
|
|
void Folder::evaluateSync(const QStringList &pathList)
|
|
|
|
{
|
2011-09-27 10:15:30 +04:00
|
|
|
if (!_online && onlyOnlineEnabled()) {
|
|
|
|
qDebug() << "*" << alias() << "sync skipped, not online";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
startSync(pathList);
|
2011-04-06 17:57:18 +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
|
|
|
qDebug() << "* " << alias() << "Poll timer disabled";
|
2011-04-06 12:40:15 +04:00
|
|
|
_pollTimer->stop();
|
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
|
|
|
{
|
2011-04-06 17:57:18 +04:00
|
|
|
evaluateSync(pathList);
|
2011-02-17 02:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Folder::slotOpenFolder()
|
|
|
|
{
|
|
|
|
QDesktopServices::openUrl(QUrl(_path));
|
|
|
|
}
|
|
|
|
|
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-04-06 14:09:56 +04:00
|
|
|
_openAction->setIcon(QIcon::fromTheme(FOLDER_SYNC_ICON));
|
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
|
|
|
{
|
2011-03-27 04:26:41 +04:00
|
|
|
_watcher->setEventsEnabled(true);
|
2011-04-06 14:09:56 +04:00
|
|
|
_openAction->setIcon(QIcon::fromTheme(FOLDER_ICON));
|
2011-04-06 12:40:15 +04:00
|
|
|
// reenable the poll timer
|
2011-04-06 17:57:18 +04:00
|
|
|
qDebug() << "* " << alias() << "Poll timer enabled";
|
2011-04-06 12:40:15 +04:00
|
|
|
_pollTimer->start();
|
2011-02-17 17:10:06 +03:00
|
|
|
}
|
2011-02-17 02:21:45 +03:00
|
|
|
|
|
|
|
} // namespace Mirall
|
|
|
|
|
|
|
|
#include "folder.moc"
|