2014-07-12 21:28:23 +05:30
|
|
|
#include "RecursiveFileSystemWatcher.h"
|
|
|
|
|
|
|
|
#include <QRegularExpression>
|
2014-07-13 02:32:52 +05:30
|
|
|
#include <QDebug>
|
2014-07-12 21:28:23 +05:30
|
|
|
|
|
|
|
RecursiveFileSystemWatcher::RecursiveFileSystemWatcher(QObject *parent)
|
2014-07-13 02:32:52 +05:30
|
|
|
: QObject(parent), m_exp(".*"), m_watcher(new QFileSystemWatcher(this))
|
2014-07-12 21:28:23 +05:30
|
|
|
{
|
2014-07-13 02:32:52 +05:30
|
|
|
connect(m_watcher, &QFileSystemWatcher::fileChanged, this,
|
|
|
|
&RecursiveFileSystemWatcher::fileChange);
|
|
|
|
connect(m_watcher, &QFileSystemWatcher::directoryChanged, this,
|
|
|
|
&RecursiveFileSystemWatcher::directoryChange);
|
2014-07-12 21:28:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void RecursiveFileSystemWatcher::setRootDir(const QDir &root)
|
|
|
|
{
|
|
|
|
bool wasEnabled = m_isEnabled;
|
|
|
|
disable();
|
|
|
|
m_root = root;
|
|
|
|
setFiles(scanRecursive(m_root));
|
|
|
|
if (wasEnabled)
|
|
|
|
{
|
|
|
|
enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void RecursiveFileSystemWatcher::setWatchFiles(const bool watchFiles)
|
|
|
|
{
|
|
|
|
bool wasEnabled = m_isEnabled;
|
|
|
|
disable();
|
|
|
|
m_watchFiles = watchFiles;
|
|
|
|
if (wasEnabled)
|
|
|
|
{
|
|
|
|
enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecursiveFileSystemWatcher::enable()
|
|
|
|
{
|
2014-07-13 02:32:52 +05:30
|
|
|
if (m_isEnabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-12 21:28:23 +05:30
|
|
|
Q_ASSERT(m_root != QDir::root());
|
|
|
|
addFilesToWatcherRecursive(m_root);
|
|
|
|
m_isEnabled = true;
|
|
|
|
}
|
|
|
|
void RecursiveFileSystemWatcher::disable()
|
|
|
|
{
|
2014-07-13 02:32:52 +05:30
|
|
|
if (!m_isEnabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-12 21:28:23 +05:30
|
|
|
m_isEnabled = false;
|
|
|
|
m_watcher->removePaths(m_watcher->files());
|
|
|
|
m_watcher->removePaths(m_watcher->directories());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecursiveFileSystemWatcher::setFiles(const QStringList &files)
|
|
|
|
{
|
|
|
|
if (files != m_files)
|
|
|
|
{
|
|
|
|
m_files = files;
|
|
|
|
emit filesChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecursiveFileSystemWatcher::addFilesToWatcherRecursive(const QDir &dir)
|
|
|
|
{
|
|
|
|
m_watcher->addPath(dir.absolutePath());
|
2014-07-13 02:32:52 +05:30
|
|
|
for (const QString &directory : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
|
2014-07-12 21:28:23 +05:30
|
|
|
{
|
2014-07-13 02:32:52 +05:30
|
|
|
addFilesToWatcherRecursive(dir.absoluteFilePath(directory));
|
2014-07-12 21:28:23 +05:30
|
|
|
}
|
|
|
|
if (m_watchFiles)
|
|
|
|
{
|
|
|
|
for (const QFileInfo &info : dir.entryInfoList(QDir::Files))
|
|
|
|
{
|
|
|
|
m_watcher->addPath(info.absoluteFilePath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-13 02:32:52 +05:30
|
|
|
QStringList RecursiveFileSystemWatcher::scanRecursive(const QDir &directory)
|
2014-07-12 21:28:23 +05:30
|
|
|
{
|
|
|
|
QStringList ret;
|
|
|
|
QRegularExpression exp(m_exp);
|
2014-07-13 02:32:52 +05:30
|
|
|
for (const QString &dir : directory.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
|
2014-07-12 21:28:23 +05:30
|
|
|
{
|
2014-07-13 02:32:52 +05:30
|
|
|
ret.append(scanRecursive(directory.absoluteFilePath(dir)));
|
|
|
|
}
|
|
|
|
for (const QString &file : directory.entryList(QDir::Files))
|
|
|
|
{
|
|
|
|
if (exp.match(file).hasMatch())
|
2014-07-12 21:28:23 +05:30
|
|
|
{
|
2014-07-13 02:32:52 +05:30
|
|
|
ret.append(m_root.relativeFilePath(directory.absoluteFilePath(file)));
|
2014-07-12 21:28:23 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecursiveFileSystemWatcher::fileChange(const QString &path)
|
|
|
|
{
|
|
|
|
emit fileChanged(path);
|
|
|
|
}
|
|
|
|
void RecursiveFileSystemWatcher::directoryChange(const QString &path)
|
|
|
|
{
|
|
|
|
setFiles(scanRecursive(m_root));
|
|
|
|
}
|