mirror of
https://git.outfoxxed.me/quickshell/quickshell.git
synced 2026-02-23 03:33:57 +11:00
core/desktopentry: watch for changes and rescan entries
This commit is contained in:
parent
49646e4407
commit
59f5744f30
6 changed files with 521 additions and 153 deletions
68
src/core/desktopentrymonitor.cpp
Normal file
68
src/core/desktopentrymonitor.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
#include "desktopentrymonitor.hpp"
|
||||
|
||||
#include <qdir.h>
|
||||
#include <qfileinfo.h>
|
||||
#include <qfilesystemwatcher.h>
|
||||
#include <qobject.h>
|
||||
#include <qstring.h>
|
||||
#include <qtmetamacros.h>
|
||||
|
||||
#include "desktopentry.hpp"
|
||||
|
||||
namespace {
|
||||
void addPathAndParents(QFileSystemWatcher& watcher, const QString& path) {
|
||||
watcher.addPath(path);
|
||||
|
||||
auto p = QFileInfo(path).absolutePath();
|
||||
while (!p.isEmpty()) {
|
||||
watcher.addPath(p);
|
||||
const auto parent = QFileInfo(p).dir().absolutePath();
|
||||
if (parent == p) break;
|
||||
p = parent;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
DesktopEntryMonitor::DesktopEntryMonitor(QObject* parent): QObject(parent) {
|
||||
this->debounceTimer.setSingleShot(true);
|
||||
this->debounceTimer.setInterval(100);
|
||||
|
||||
QObject::connect(
|
||||
&this->watcher,
|
||||
&QFileSystemWatcher::directoryChanged,
|
||||
this,
|
||||
&DesktopEntryMonitor::onDirectoryChanged
|
||||
);
|
||||
QObject::connect(
|
||||
&this->debounceTimer,
|
||||
&QTimer::timeout,
|
||||
this,
|
||||
&DesktopEntryMonitor::processChanges
|
||||
);
|
||||
|
||||
this->startMonitoring();
|
||||
}
|
||||
|
||||
void DesktopEntryMonitor::startMonitoring() {
|
||||
for (const auto& path: DesktopEntryManager::desktopPaths()) {
|
||||
if (!QDir(path).exists()) continue;
|
||||
addPathAndParents(this->watcher, path);
|
||||
this->scanAndWatch(path);
|
||||
}
|
||||
}
|
||||
|
||||
void DesktopEntryMonitor::scanAndWatch(const QString& dirPath) {
|
||||
auto dir = QDir(dirPath);
|
||||
if (!dir.exists()) return;
|
||||
|
||||
this->watcher.addPath(dirPath);
|
||||
|
||||
auto subdirs = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
|
||||
for (const auto& subdir: subdirs) this->watcher.addPath(subdir.absoluteFilePath());
|
||||
}
|
||||
|
||||
void DesktopEntryMonitor::onDirectoryChanged(const QString& /*path*/) {
|
||||
this->debounceTimer.start();
|
||||
}
|
||||
|
||||
void DesktopEntryMonitor::processChanges() { emit this->desktopEntriesChanged(); }
|
||||
Loading…
Add table
Add a link
Reference in a new issue