2
* Copyright (C) 2014, 2015
3
* Stefano Verzegnassi <stefano92.100@gmail.com>
5
* This is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License.
9
* This is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see http://www.gnu.org/licenses/.
18
#include "fswatcher.h"
19
#include <QDirIterator>
26
FSWatcher::FSWatcher(QObject *parent)
27
: QFileSystemWatcher(parent)
29
connect(this, SIGNAL(fileChanged(QString)), this, SLOT(q_fileChanged(QString)));
30
connect(this, SIGNAL(directoryChanged(QString)), this, SLOT(q_dirChanged(QString)));
33
void FSWatcher::clear()
35
if (!this->directories().isEmpty())
36
this->removePaths(this->directories());
38
if (!this->files().isEmpty())
39
this->removePaths(this->files());
42
void FSWatcher::addDirectory(const QString &path)
44
if (!this->directories().contains(path))
47
Q_EMIT directoryAdded(path);
49
parseDirectoryContent(path);
53
void FSWatcher::q_fileChanged(const QString &path)
55
// Check if file has been removed
56
if (!QFile::exists(path))
58
Q_EMIT fileRemoved(path);
62
/* We got a signal for a file already watched, but it's not been deleted.
63
That means that it has been modified. */
64
Q_EMIT fileModified(path);
67
void FSWatcher::q_dirChanged(const QString &path)
71
/* Check if file has been removed.
72
When this happens, files in that folder are automatically removed
73
from watcher. We need to manually notify their removal.
77
Q_FOREACH (const QString& file, m_cachedFilesList)
79
if (!this->files().contains(file))
81
Q_EMIT fileRemoved(file);
82
// qDebug() << "file removed" << file;
86
m_cachedFilesList = this->files();
88
Q_EMIT directoryRemoved(path);
92
// The content of the directory has changed (a file has been added or removed)
93
// Let's check it out!
94
parseDirectoryContent(path);
97
void FSWatcher::parseDirectoryContent(QString path)
100
QDirIterator dir(path, QDir::Files | QDir::NoDotAndDotDot | QDir::Readable,
101
QDirIterator::Subdirectories);
103
while (dir.hasNext())
106
filePath = dir.filePath();
108
// Do not check for files already watched.
109
if (!this->files().contains(filePath))
111
// Add the file to the tracker
112
this->addPath(filePath);
113
Q_EMIT fileAdded(filePath);
117
m_cachedFilesList = this->files();