Skip to content

Commit 77de23b

Browse files
committed
core/desktopentry: add StartupWMClass and heuristicLookup
1 parent 7b417bb commit 77de23b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/core/desktopentry.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "desktopentry.hpp"
2+
#include <algorithm>
23

34
#include <qcontainerfwd.h>
45
#include <qdebug.h>
@@ -108,6 +109,7 @@ void DesktopEntry::parseEntry(const QString& text) {
108109

109110
if (key == "Name") this->mName = value;
110111
else if (key == "GenericName") this->mGenericName = value;
112+
else if (key == "StartupWMClass") this->mStartupClass = value;
111113
else if (key == "NoDisplay") this->mNoDisplay = value == "true";
112114
else if (key == "Comment") this->mComment = value;
113115
else if (key == "Icon") this->mIcon = value;
@@ -384,6 +386,25 @@ DesktopEntry* DesktopEntryManager::byId(const QString& id) {
384386
}
385387
}
386388

389+
DesktopEntry* DesktopEntryManager::heuristicLookup(const QString& name) {
390+
if (auto* entry = DesktopEntryManager::byId(name)) return entry;
391+
392+
auto& list = this->mApplications.valueList();
393+
394+
auto iter = std::ranges::find_if(list, [&](const DesktopEntry* entry) {
395+
return name == entry->mStartupClass;
396+
});
397+
398+
if (iter != list.end()) return *iter;
399+
400+
iter = std::ranges::find_if(list, [&](const DesktopEntry* entry) {
401+
return name.toLower() == entry->mStartupClass.toLower();
402+
});
403+
404+
if (iter != list.end()) return *iter;
405+
return nullptr;
406+
}
407+
387408
ObjectModel<DesktopEntry>* DesktopEntryManager::applications() { return &this->mApplications; }
388409

389410
DesktopEntries::DesktopEntries() { DesktopEntryManager::instance(); }
@@ -392,6 +413,10 @@ DesktopEntry* DesktopEntries::byId(const QString& id) {
392413
return DesktopEntryManager::instance()->byId(id);
393414
}
394415

416+
DesktopEntry* DesktopEntries::heuristicLookup(const QString& name) {
417+
return DesktopEntryManager::instance()->heuristicLookup(name);
418+
}
419+
395420
ObjectModel<DesktopEntry>* DesktopEntries::applications() {
396421
return DesktopEntryManager::instance()->applications();
397422
}

src/core/desktopentry.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class DesktopEntry: public QObject {
2222
Q_PROPERTY(QString name MEMBER mName CONSTANT);
2323
/// Short description of the application, such as "Web Browser". May be empty.
2424
Q_PROPERTY(QString genericName MEMBER mGenericName CONSTANT);
25+
/// Initial class or app id the app intends to use. May be useful for matching running apps
26+
/// to desktop entries.
27+
Q_PROPERTY(QString startupClass MEMBER mStartupClass CONSTANT);
2528
/// If true, this application should not be displayed in menus and launchers.
2629
Q_PROPERTY(bool noDisplay MEMBER mNoDisplay CONSTANT);
2730
/// Long description of the application, such as "View websites on the internet". May be empty.
@@ -81,6 +84,7 @@ class DesktopEntry: public QObject {
8184
QString mId;
8285
QString mName;
8386
QString mGenericName;
87+
QString mStartupClass;
8488
bool mNoDisplay = false;
8589
QString mComment;
8690
QString mIcon;
@@ -151,6 +155,7 @@ class DesktopEntryManager: public QObject {
151155
void scanDesktopEntries();
152156

153157
[[nodiscard]] DesktopEntry* byId(const QString& id);
158+
[[nodiscard]] DesktopEntry* heuristicLookup(const QString& name);
154159

155160
[[nodiscard]] ObjectModel<DesktopEntry>* applications();
156161

@@ -186,7 +191,14 @@ class DesktopEntries: public QObject {
186191
explicit DesktopEntries();
187192

188193
/// Look up a desktop entry by name. Includes NoDisplay entries. May return null.
194+
///
195+
/// While this function requires an exact match, @@heuristicLookup() will correctly
196+
/// find an entry more often and is generally more useful.
189197
Q_INVOKABLE [[nodiscard]] static DesktopEntry* byId(const QString& id);
198+
/// Look up a desktop entry by name using heuristics. Unline @@byId(),
199+
/// if no exact matches are found this function will try to guess - potentially incorrectly.
200+
/// May return null.
201+
Q_INVOKABLE [[nodiscard]] static DesktopEntry* heuristicLookup(const QString& name);
190202

191203
[[nodiscard]] static ObjectModel<DesktopEntry>* applications();
192204
};

0 commit comments

Comments
 (0)