Skip to content

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
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

bbedward
Copy link

@bbedward bbedward commented Jul 14, 2025

DesktopEntries only scans one time~~, and doesnt expose scanning functions to QML. This adds a rescan function, a signal for applications changed. It just allows a nicer launcher that doesn't get stale.~~

This adds:

  • DesktopUtils which had a common function for locating .desktop files
  • DesktopEntryMonitor which implements QFileSystemWatcher
    • Watches the desktop entry target directories
  • Connect monitor to DesktopEntryManager and add a new signal applicationsChanged
    • On directory updated, it re-fetches the entries and emits the signal

@outfoxxed
Copy link
Member

outfoxxed commented Jul 14, 2025

I'm generally conflicted on this one because I prefer state change notifications to manually triggered scanning, which often leads to polling.

As I'm aware, similar GTK based tools usually listen to a signal provided by glib/gio for this. I've been planning to check what that does and implement it here if feasible, as an alternative to re-scanning. If you're interested in figuring it out, https://github.com/GNOME/glib/blob/6e2a31f97a345e0a6b57a0c5c9f9b7514d61b755/gio/gappinfo.c#L2010 is probably a good place to start.

Looks like its just a simple directory watch, but it likely has to be recursive on every potential desktop entry dir, otherwise it's unlikely to handle nix generation switches and such.

This is the general direction I'd like to take it in. No pressure on you to implement it if you don't want to.

@bbedward bbedward changed the title core: add rescan method to DesktopEntryManager core: add file system watcher for desktop entries Jul 15, 2025
@bbedward bbedward changed the title core: add file system watcher for desktop entries (wip) core: add file system watcher for desktop entries Jul 15, 2025
@bbedward bbedward changed the title (wip) core: add file system watcher for desktop entries (wip/donotmerge) core/desktopentry: add file system watcher for desktop entry directories Jul 15, 2025
@bbedward bbedward changed the title (wip/donotmerge) core/desktopentry: add file system watcher for desktop entry directories core/desktopentry: add file system watcher for desktop entry directories Jul 15, 2025
Copy link
Member

@outfoxxed outfoxxed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you've marked it as WIP. Partial review

Comment on lines 58 to 60
void DesktopEntryMonitor::addDirectoryHierarchy(const QString& dirPath) {
this->scanAndWatch(dirPath);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

this->watcher = new QFileSystemWatcher(this);
this->debounceTimer = new QTimer(this);
this->debounceTimer->setSingleShot(true);
this->debounceTimer->setInterval(500); // 500ms debounce
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

500ms is unreasonably long for a debounce

Comment on lines 27 to 39
connect(
this->watcher,
&QFileSystemWatcher::directoryChanged,
this,
&DesktopEntryMonitor::onDirectoryChanged
);
connect(
this->watcher,
&QFileSystemWatcher::fileChanged,
this,
&DesktopEntryMonitor::onFileChanged
);
connect(this->debounceTimer, &QTimer::timeout, this, &DesktopEntryMonitor::processChanges);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QObject::connect

}

void DesktopEntryMonitor::scanAndWatch(const QString& dirPath) {
QDir dir(dirPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disallowed style, use auto

this->scanAndWatch(dirPath);
}

void DesktopEntryMonitor::scanAndWatch(const QString& dirPath) {
Copy link
Member

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.

if (watchedFile.startsWith(path + "/") || watchedFile == path) {
this->watcher->removePath(watchedFile);
this->fileTimestamps.remove(watchedFile);
this->queueChange(ChangeEvent::Removed, watchedFile);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debounce is not used in a logical way. If you are going to debounce, it should guard re-scanning directories, not model updates.

Comment on lines 99 to 100
for (auto it = this->watchedFiles.begin(); it != this->watchedFiles.end();) {
const QString& watchedFile = *it;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we tracking watched files, the fs watcher already does that

return;
}

QList<QString> currentFiles = dir.entryList({"*.desktop"}, QDir::Files);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto

@bbedward
Copy link
Author

@outfoxxed I addressed the comments, I had it initially watching .desktop files and then decided against it, to just watch the directories and rescan when there's a change instead.

Sorry for the confusion, not wip anymore - ready for review

@bbedward bbedward requested a review from outfoxxed July 15, 2025 14:29
bbedward added 7 commits July 16, 2025 12:02
De-dupe code paths to scanAndWatch function, Optimize lookups by using a
QSet, remove paths from watcher if deleted
- Create less fs watchers
- Just re-scan when a directory containing .desktop files changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants