2014-07-12 16:58:23 +01:00
|
|
|
#include "RecursiveFileSystemWatcher.h"
|
|
|
|
|
2014-07-12 22:02:52 +01:00
|
|
|
#include <QDebug>
|
2023-08-14 17:16:53 +01:00
|
|
|
#include <QRegularExpression>
|
2014-07-12 16:58:23 +01:00
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
RecursiveFileSystemWatcher::RecursiveFileSystemWatcher(QObject* parent) : QObject(parent), m_watcher(new QFileSystemWatcher(this))
|
2014-07-12 16:58:23 +01:00
|
|
|
{
|
2023-08-14 17:16:53 +01:00
|
|
|
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &RecursiveFileSystemWatcher::fileChange);
|
|
|
|
connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &RecursiveFileSystemWatcher::directoryChange);
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void RecursiveFileSystemWatcher::setRootDir(const QDir& root)
|
2014-07-12 16:58:23 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
bool wasEnabled = m_isEnabled;
|
|
|
|
disable();
|
|
|
|
m_root = root;
|
|
|
|
setFiles(scanRecursive(m_root));
|
2023-08-14 17:16:53 +01:00
|
|
|
if (wasEnabled) {
|
2018-07-15 13:51:05 +01:00
|
|
|
enable();
|
|
|
|
}
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
|
|
|
void RecursiveFileSystemWatcher::setWatchFiles(const bool watchFiles)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
bool wasEnabled = m_isEnabled;
|
|
|
|
disable();
|
|
|
|
m_watchFiles = watchFiles;
|
2023-08-14 17:16:53 +01:00
|
|
|
if (wasEnabled) {
|
2018-07-15 13:51:05 +01:00
|
|
|
enable();
|
|
|
|
}
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RecursiveFileSystemWatcher::enable()
|
|
|
|
{
|
2023-08-14 17:16:53 +01:00
|
|
|
if (m_isEnabled) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Q_ASSERT(m_root != QDir::root());
|
|
|
|
addFilesToWatcherRecursive(m_root);
|
|
|
|
m_isEnabled = true;
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
|
|
|
void RecursiveFileSystemWatcher::disable()
|
|
|
|
{
|
2023-08-14 17:16:53 +01:00
|
|
|
if (!m_isEnabled) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_isEnabled = false;
|
|
|
|
m_watcher->removePaths(m_watcher->files());
|
|
|
|
m_watcher->removePaths(m_watcher->directories());
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void RecursiveFileSystemWatcher::setFiles(const QStringList& files)
|
2014-07-12 16:58:23 +01:00
|
|
|
{
|
2023-08-14 17:16:53 +01:00
|
|
|
if (files != m_files) {
|
2018-07-15 13:51:05 +01:00
|
|
|
m_files = files;
|
|
|
|
emit filesChanged();
|
|
|
|
}
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void RecursiveFileSystemWatcher::addFilesToWatcherRecursive(const QDir& dir)
|
2014-07-12 16:58:23 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_watcher->addPath(dir.absolutePath());
|
2023-08-14 17:16:53 +01:00
|
|
|
for (const QString& directory : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
|
2018-07-15 13:51:05 +01:00
|
|
|
addFilesToWatcherRecursive(dir.absoluteFilePath(directory));
|
|
|
|
}
|
2023-08-14 17:16:53 +01:00
|
|
|
if (m_watchFiles) {
|
|
|
|
for (const QFileInfo& info : dir.entryInfoList(QDir::Files)) {
|
2018-07-15 13:51:05 +01:00
|
|
|
m_watcher->addPath(info.absoluteFilePath());
|
|
|
|
}
|
|
|
|
}
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
2023-08-14 17:16:53 +01:00
|
|
|
QStringList RecursiveFileSystemWatcher::scanRecursive(const QDir& directory)
|
2014-07-12 16:58:23 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QStringList ret;
|
2023-08-14 17:16:53 +01:00
|
|
|
if (!m_matcher) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return {};
|
|
|
|
}
|
2023-08-14 17:16:53 +01:00
|
|
|
for (const QString& dir : directory.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden)) {
|
2018-07-15 13:51:05 +01:00
|
|
|
ret.append(scanRecursive(directory.absoluteFilePath(dir)));
|
|
|
|
}
|
2023-08-14 17:16:53 +01:00
|
|
|
for (const QString& file : directory.entryList(QDir::Files | QDir::Hidden)) {
|
2018-07-15 13:51:05 +01:00
|
|
|
auto relPath = m_root.relativeFilePath(directory.absoluteFilePath(file));
|
2023-08-14 17:16:53 +01:00
|
|
|
if (m_matcher->matches(relPath)) {
|
2018-07-15 13:51:05 +01:00
|
|
|
ret.append(relPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
void RecursiveFileSystemWatcher::fileChange(const QString& path)
|
2014-07-12 16:58:23 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
emit fileChanged(path);
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
2023-07-01 07:51:15 +01:00
|
|
|
void RecursiveFileSystemWatcher::directoryChange([[maybe_unused]] const QString& path)
|
2014-07-12 16:58:23 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
setFiles(scanRecursive(m_root));
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|