nextcloud-desktop/src/gui/folderwatcher.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

133 lines
3 KiB
C++
Raw Normal View History

2011-04-06 13:48:02 +04:00
/*
2014-01-13 19:16:19 +04:00
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
2011-04-06 13:48:02 +04:00
*
* 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.
2011-04-06 13:48:02 +04:00
*
* 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
#include "folderwatcher.h"
2012-05-21 18:48:49 +04:00
#include <stdint.h>
2011-02-17 02:21:45 +03:00
#include <QFileInfo>
#include <QFlags>
#include <QDir>
#include <QMutexLocker>
#include <QStringList>
#include <QTimer>
2011-02-17 02:21:45 +03:00
#if defined(Q_OS_WIN)
#include "folderwatcher_win.h"
#elif defined(Q_OS_MAC)
#include "folderwatcher_mac.h"
2014-04-29 13:02:44 +04:00
#elif defined(Q_OS_UNIX)
#include "folderwatcher_linux.h"
#endif
#include "folder.h"
2014-11-10 00:34:07 +03:00
namespace OCC {
2011-02-17 02:21:45 +03:00
Q_LOGGING_CATEGORY(lcFolderWatcher, "nextcloud.gui.folderwatcher", QtInfoMsg)
FolderWatcher::FolderWatcher(Folder *folder)
: QObject(folder)
, _folder(folder)
2011-02-17 02:21:45 +03:00
{
2011-03-21 00:18:38 +03:00
}
FolderWatcher::~FolderWatcher()
{
}
void FolderWatcher::init(const QString &root)
{
_d.reset(new FolderWatcherPrivate(this, root));
_timer.start();
}
bool FolderWatcher::pathIsIgnored(const QString &path)
{
if (path.isEmpty())
return true;
if (!_folder)
return false;
#ifndef OWNCLOUD_TEST
if (_folder->isFileExcludedAbsolute(path)) {
qCDebug(lcFolderWatcher) << "* Ignoring file" << path;
return true;
}
#endif
return false;
}
bool FolderWatcher::isReliable() const
{
return _isReliable;
}
void FolderWatcher::changeDetected(const QString &path)
{
QStringList paths(path);
changeDetected(paths);
}
void FolderWatcher::changeDetected(const QStringList &paths)
{
// TODO: this shortcut doesn't look very reliable:
// - why is the timeout only 1 second?
// - what if there is more than one file being updated frequently?
// - why do we skip the file altogether instead of e.g. reducing the upload frequency?
// Check if the same path was reported within the last second.
QSet<QString> pathsSet = paths.toSet();
if (pathsSet == _lastPaths && _timer.elapsed() < 1000) {
// the same path was reported within the last second. Skip.
return;
}
_lastPaths = pathsSet;
_timer.restart();
QSet<QString> changedPaths;
// ------- handle ignores:
for (int i = 0; i < paths.size(); ++i) {
QString path = paths[i];
if (pathIsIgnored(path)) {
continue;
}
changedPaths.insert(path);
}
if (changedPaths.isEmpty()) {
return;
}
qCInfo(lcFolderWatcher) << "Detected changes in paths:" << changedPaths;
foreach (const QString &path, changedPaths) {
emit pathChanged(path);
}
}
void FolderWatcher::addPath(const QString &path)
{
_d->addPath(path);
}
void FolderWatcher::removePath(const QString &path)
{
_d->removePath(path);
}
2014-11-10 00:34:07 +03:00
} // namespace OCC