2014-01-11 23:32:06 +04:00
|
|
|
/*
|
2014-01-13 19:16:19 +04:00
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
2014-01-11 23:32:06 +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
|
2014-01-13 19:16:19 +04:00
|
|
|
* the Free Software Foundation; version 2 of the License.
|
2014-01-11 23:32:06 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mirall/folderwatcher.h"
|
|
|
|
#include "mirall/folderwatcher_qt.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
namespace Mirall {
|
|
|
|
|
|
|
|
FolderWatcherPrivate::FolderWatcherPrivate()
|
|
|
|
:QObject(), _parent(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-13 18:23:52 +04:00
|
|
|
FolderWatcherPrivate::FolderWatcherPrivate(FolderWatcher *p, const QString &path)
|
2014-01-11 23:32:06 +04:00
|
|
|
: QObject(), _parent(p)
|
|
|
|
|
|
|
|
{
|
|
|
|
_watcher.reset(new QFileSystemWatcher);
|
|
|
|
|
|
|
|
QObject::connect(_watcher.data(), SIGNAL(directoryChanged(QString)),
|
2014-01-13 18:23:52 +04:00
|
|
|
_parent, SLOT(changeDetected(QString)) );
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "slotAddFolderRecursive", Q_ARG(QString, path));
|
2014-01-11 23:32:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// attention: result list passed by reference!
|
|
|
|
bool FolderWatcherPrivate::findFoldersBelow( const QDir& dir, QStringList& fullList )
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
if( !(dir.exists() && dir.isReadable()) ) {
|
|
|
|
qDebug() << "Non existing path coming in: " << dir.absolutePath();
|
|
|
|
ok = false;
|
|
|
|
} else {
|
|
|
|
QStringList nameFilter;
|
|
|
|
nameFilter << QLatin1String("*");
|
|
|
|
QDir::Filters filter = QDir::Dirs | QDir::NoDotAndDotDot|QDir::NoSymLinks;
|
|
|
|
const QStringList pathes = dir.entryList(nameFilter, filter);
|
|
|
|
|
|
|
|
QStringList::const_iterator constIterator;
|
|
|
|
for (constIterator = pathes.constBegin(); constIterator != pathes.constEnd();
|
|
|
|
++constIterator) {
|
|
|
|
const QString fullPath(dir.path()+QLatin1String("/")+(*constIterator));
|
|
|
|
fullList.append(fullPath);
|
|
|
|
ok = findFoldersBelow(QDir(fullPath), fullList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderWatcherPrivate::slotAddFolderRecursive(const QString &path)
|
|
|
|
{
|
|
|
|
int subdirs = 0;
|
|
|
|
qDebug() << "(+) Watcher:" << path;
|
|
|
|
|
|
|
|
_watcher->addPath(path);
|
|
|
|
const QStringList watchedFolders(_watcher->directories());
|
|
|
|
|
|
|
|
QStringList allSubfolders;
|
|
|
|
if( !findFoldersBelow(QDir(path), allSubfolders)) {
|
|
|
|
qDebug() << "Could not traverse all sub folders";
|
|
|
|
}
|
|
|
|
// qDebug() << "currently watching " << watchedFolders;
|
|
|
|
QStringListIterator subfoldersIt(allSubfolders);
|
|
|
|
while (subfoldersIt.hasNext()) {
|
|
|
|
QString subfolder = subfoldersIt.next();
|
|
|
|
// qDebug() << " (**) subfolder: " << subfolder;
|
|
|
|
QDir folder (subfolder);
|
|
|
|
if (folder.exists() && !watchedFolders.contains(folder.path())) {
|
|
|
|
subdirs++;
|
2014-01-13 18:23:38 +04:00
|
|
|
if( _parent->pathIsIgnored(subfolder) ) {
|
|
|
|
qDebug() << "* Not adding" << folder.path();
|
|
|
|
continue;
|
2014-01-11 23:32:06 +04:00
|
|
|
}
|
|
|
|
_watcher->addPath(folder.path());
|
2014-01-13 18:23:38 +04:00
|
|
|
} else {
|
2014-01-11 23:32:06 +04:00
|
|
|
qDebug() << " `-> discarded:" << folder.path();
|
2014-01-13 18:23:38 +04:00
|
|
|
}
|
2014-01-11 23:32:06 +04:00
|
|
|
}
|
|
|
|
|
2014-01-13 18:23:38 +04:00
|
|
|
if (subdirs >0) {
|
|
|
|
qDebug() << " `-> and" << subdirs << "subdirectories";
|
2014-01-11 23:32:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderWatcherPrivate::removePath(const QString &path )
|
|
|
|
{
|
|
|
|
_watcher->removePath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Mirall
|