-
Notifications
You must be signed in to change notification settings - Fork 31
core/desktopentry: add file system watcher for desktop entry directories #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bbedward
wants to merge
7
commits into
quickshell-mirror:master
Choose a base branch
from
bbedward:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+300
−26
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e6ea6b6
Add rescan option for DesktopEntryManager
bbedward ab1e455
More comprehensive DesktopEntry monitoring with QFileSystemWatcher
bbedward 537d415
Cleanup and optimizations
bbedward d6ab231
Use extractIdFromPath helper in scanPath
bbedward 0bbce67
Only watch the desktop folders
bbedward daa7f93
consistency cleanups
bbedward b9cbcd2
use auto everywhere
bbedward File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include "desktopentrymonitor.hpp" | ||
|
||
#include <qdir.h> | ||
#include <qdiriterator.h> | ||
#include <qfileinfo.h> | ||
#include <qlogging.h> | ||
#include <qloggingcategory.h> | ||
#include <qtenvironmentvariables.h> | ||
|
||
#include "desktoputils.hpp" | ||
#include "logcat.hpp" | ||
|
||
namespace { | ||
QS_LOGGING_CATEGORY(logDesktopMonitor, "quickshell.desktopentrymonitor", QtWarningMsg); | ||
} | ||
|
||
DesktopEntryMonitor::DesktopEntryMonitor(QObject* parent): QObject(parent) { | ||
this->watcher = new QFileSystemWatcher(this); | ||
this->debounceTimer = new QTimer(this); | ||
this->debounceTimer->setSingleShot(true); | ||
this->debounceTimer->setInterval(50); | ||
|
||
// Initialize XDG desktop paths | ||
this->initializeDesktopPaths(); | ||
|
||
// Setup connections | ||
QObject::connect( | ||
this->watcher, | ||
&QFileSystemWatcher::directoryChanged, | ||
this, | ||
&DesktopEntryMonitor::onDirectoryChanged | ||
); | ||
QObject::connect( | ||
this->debounceTimer, | ||
&QTimer::timeout, | ||
this, | ||
&DesktopEntryMonitor::processChanges | ||
); | ||
|
||
// Start monitoring | ||
this->startMonitoring(); | ||
} | ||
|
||
void DesktopEntryMonitor::initializeDesktopPaths() { | ||
this->desktopPaths = DesktopUtils::getDesktopDirectories(); | ||
} | ||
|
||
void DesktopEntryMonitor::startMonitoring() { | ||
for (const QString& path: this->desktopPaths) { | ||
if (QDir(path).exists()) { | ||
qCDebug(logDesktopMonitor) << "Monitoring desktop entry path:" << path; | ||
this->scanAndWatch(path); | ||
} | ||
} | ||
} | ||
|
||
void DesktopEntryMonitor::scanAndWatch(const QString& dirPath) { | ||
auto dir = QDir(dirPath); | ||
if (!dir.exists()) return; | ||
|
||
// Add directory to watcher | ||
if (this->watcher->addPath(dirPath)) { | ||
qCDebug(logDesktopMonitor) << "Added directory to watcher:" << dirPath; | ||
} | ||
|
||
// Recurse into subdirs | ||
for (const auto& sub: dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { | ||
this->scanAndWatch(dir.absoluteFilePath(sub)); | ||
} | ||
} | ||
|
||
void DesktopEntryMonitor::onDirectoryChanged(const QString& path) { | ||
qCDebug(logDesktopMonitor) << "Directory changed:" << path; | ||
auto dir = QDir(path); | ||
|
||
// Check if directory still exists - handle removal/unmounting | ||
if (!dir.exists()) { | ||
qCDebug(logDesktopMonitor) << "Directory no longer exists, cleaning up:" << path; | ||
this->watcher->removePath(path); | ||
// Directory removal will be handled by full rescan | ||
this->queueChange(ChangeEvent::Modified, path); // Trigger full rescan | ||
return; | ||
} | ||
|
||
// Check for new subdirectories | ||
auto subdirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot); | ||
for (const QString& subdir: subdirs) { | ||
auto subdirPath = dir.absoluteFilePath(subdir); | ||
if (!this->watcher->directories().contains(subdirPath)) { | ||
this->scanAndWatch(subdirPath); | ||
} | ||
} | ||
|
||
// Queue a change to trigger full rescan of all desktop paths | ||
this->queueChange(ChangeEvent::Modified, path); | ||
} | ||
|
||
void DesktopEntryMonitor::queueChange(ChangeEvent event, const QString& path) { | ||
this->pendingChanges.insert(path, event); | ||
this->debounceTimer->start(); | ||
} | ||
|
||
void DesktopEntryMonitor::processChanges() { | ||
if (this->pendingChanges.isEmpty()) return; | ||
|
||
qCDebug(logDesktopMonitor) << "Processing directory changes, triggering full rescan"; | ||
|
||
// Clear pending changes since we're doing a full rescan | ||
this->pendingChanges.clear(); | ||
|
||
// Emit with empty hash to signal full rescan needed | ||
auto emptyChanges = QHash<QString, ChangeEvent> {}; | ||
emit desktopEntriesChanged(emptyChanges); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include <qfilesystemwatcher.h> | ||
#include <qhash.h> | ||
#include <qobject.h> | ||
#include <qstringlist.h> | ||
#include <qtimer.h> | ||
|
||
class DesktopEntryMonitor: public QObject { | ||
Q_OBJECT | ||
|
||
public: | ||
enum class ChangeEvent { Added, Modified, Removed }; | ||
|
||
explicit DesktopEntryMonitor(QObject* parent = nullptr); | ||
~DesktopEntryMonitor() = default; | ||
|
||
signals: | ||
void desktopEntriesChanged(const QHash<QString, ChangeEvent>& changes); | ||
|
||
private slots: | ||
void onDirectoryChanged(const QString& path); | ||
void processChanges(); | ||
|
||
private: | ||
void initializeDesktopPaths(); | ||
void startMonitoring(); | ||
void scanAndWatch(const QString& dirPath); | ||
void queueChange(ChangeEvent event, const QString& path); | ||
|
||
QFileSystemWatcher* watcher; | ||
QStringList desktopPaths; | ||
QTimer* debounceTimer; | ||
QHash<QString, ChangeEvent> pendingChanges; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scanAndWatch + all the various change handlers share a lot of the code. consider initialization to be a diff from an empty set of entries and share the code.