diff options
author | Jan Dalheimer <jan@dalheimer.de> | 2014-07-12 17:58:23 +0200 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2014-07-12 23:31:05 +0200 |
commit | 5c4384235989b16d6931cc0cfa26dd192af68b96 (patch) | |
tree | 90e9fa1b758c97302726e01882e9c7c914426182 /logic/RecursiveFileSystemWatcher.cpp | |
parent | aba1f89e2abfd596eb01c674d1b2deee3bdc1047 (diff) | |
download | MultiMC-5c4384235989b16d6931cc0cfa26dd192af68b96.tar MultiMC-5c4384235989b16d6931cc0cfa26dd192af68b96.tar.gz MultiMC-5c4384235989b16d6931cc0cfa26dd192af68b96.tar.lz MultiMC-5c4384235989b16d6931cc0cfa26dd192af68b96.tar.xz MultiMC-5c4384235989b16d6931cc0cfa26dd192af68b96.zip |
Add a new page that can show all sorts of logs
Diffstat (limited to 'logic/RecursiveFileSystemWatcher.cpp')
-rw-r--r-- | logic/RecursiveFileSystemWatcher.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/logic/RecursiveFileSystemWatcher.cpp b/logic/RecursiveFileSystemWatcher.cpp new file mode 100644 index 00000000..5cfa7e4e --- /dev/null +++ b/logic/RecursiveFileSystemWatcher.cpp @@ -0,0 +1,97 @@ +#include "RecursiveFileSystemWatcher.h" + +#include <QRegularExpression> + +RecursiveFileSystemWatcher::RecursiveFileSystemWatcher(QObject *parent) + : QObject(parent), + m_watcher(new QFileSystemWatcher(this)) +{ + connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &RecursiveFileSystemWatcher::fileChange); + connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &RecursiveFileSystemWatcher::directoryChange); +} + +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() +{ + Q_ASSERT(m_root != QDir::root()); + addFilesToWatcherRecursive(m_root); + m_isEnabled = true; +} +void RecursiveFileSystemWatcher::disable() +{ + 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()); + for (const QFileInfo &info : dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) + { + addFilesToWatcherRecursive(info.absoluteDir()); + } + if (m_watchFiles) + { + for (const QFileInfo &info : dir.entryInfoList(QDir::Files)) + { + m_watcher->addPath(info.absoluteFilePath()); + } + } +} +QStringList RecursiveFileSystemWatcher::scanRecursive(const QDir &dir) +{ + QStringList ret; + QRegularExpression exp(m_exp); + for (const QFileInfo &info : dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Files)) + { + if (info.isFile() && exp.match(info.absoluteFilePath()).hasMatch()) + { + ret.append(info.absoluteFilePath()); + } + else if (info.isDir()) + { + ret.append(scanRecursive(info.absoluteDir())); + } + } + return ret; +} + +void RecursiveFileSystemWatcher::fileChange(const QString &path) +{ + emit fileChanged(path); +} +void RecursiveFileSystemWatcher::directoryChange(const QString &path) +{ + setFiles(scanRecursive(m_root)); +} |