[plasma/plasma-workspace] libtaskmanager: libtaskmanager: Return KService objects when matching windows to desktop files

Nicolas Fella <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 18a3e7fbd1c2eabdbc74b4f63fd6cdc8a2725a71 by Nicolas Fella.
Committed on 10/08/2026 at 14:48.
Pushed by nicolasfella into branch 'master'.

libtaskmanager: Return KService objects when matching windows to desktop files

KService is a more reasonable intermediate representation than QUrl

The calling code still relies on URLs, but that can be refactored later to use KService directly

M  +25   -25   libtaskmanager/tasktools.cpp
M  +20   -2    libtaskmanager/tasktools.h

https://invent.kde.org/plasma/plasma-workspace/-/commit/18a3e7fbd1c2eabdbc74b4f63fd6cdc8a2725a71

diff --git a/libtaskmanager/tasktools.cpp b/libtaskmanager/tasktools.cpp
index f5b85211a1..870f944272 100644
--- a/libtaskmanager/tasktools.cpp
+++ b/libtaskmanager/tasktools.cpp
@@ -182,7 +182,27 @@ AppData appDataFromUrl(const QUrl &url, const QIcon &fallbackIcon)
 
 QUrl windowUrlFromMetadata(const QString &appId, quint32 pid, const QString &xWindowsWMClassName)
 {
-    QUrl url;
+    const auto service = serviceFromMetadata(appId, pid, xWindowsWMClassName);
+
+    if (service) {
+        // applications: URLs are used to refer to applications by their KService::menuId
+        // (i.e. .desktop file name) rather than the absolute path to a .desktop file.
+        if (!service->menuId().isEmpty()) {
+            return QUrl(u"applications:" + service->menuId());
+        }
+
+        if (!service->entryPath().isEmpty()) {
+            return QUrl::fromLocalFile(service->entryPath());
+        }
+
+        return QUrl::fromLocalFile(service->exec());
+    }
+
+    return QUrl();
+}
+
+KService::Ptr serviceFromMetadata(const QString &appId, quint32 pid, const QString &xWindowsWMClassName)
+{
     KService::List services;
     bool triedPid = false;
 
@@ -249,14 +269,14 @@ QUrl windowUrlFromMetadata(const QString &appId, quint32 pid, const QString &xWi
             if (services.isEmpty() && appId.startsWith(QLatin1String("/"))) {
                 // Check if it's a path to a .desktop file.
                 if (KDesktopFile::isDesktopFile(appId) && QFile::exists(appId)) {
-                    return QUrl::fromLocalFile(appId);
+                    return KService::Ptr(new KService(appId));
                 }
 
                 // Check if the appId passes as a .desktop file path if we add the extension.
                 const QString appIdPlusExtension(appId + QStringLiteral(".desktop"));
 
                 if (KDesktopFile::isDesktopFile(appIdPlusExtension) && QFile::exists(appIdPlusExtension)) {
-                    return QUrl::fromLocalFile(appIdPlusExtension);
+                    return KService::Ptr(new KService(appIdPlusExtension));
                 }
             }
 
@@ -323,30 +343,10 @@ QUrl windowUrlFromMetadata(const QString &appId, quint32 pid, const QString &xWi
     }
 
     if (!services.isEmpty()) {
-        const QString &menuId = services.at(0)->menuId();
-
-        // applications: URLs are used to refer to applications by their KService::menuId
-        // (i.e. .desktop file name) rather than the absolute path to a .desktop file.
-        if (!menuId.isEmpty()) {
-            url.setUrl(QString(u"applications:" + menuId));
-            return url;
-        }
-
-        QString path = services.at(0)->entryPath();
-
-        if (path.isEmpty()) {
-            path = services.at(0)->exec();
-        }
-
-        if (!path.isEmpty()) {
-            QString query = url.query();
-            url = QUrl::fromLocalFile(path);
-            url.setQuery(query);
-            return url;
-        }
+        return services.first();
     }
 
-    return url;
+    return {};
 }
 
 KService::List servicesFromEnvironment(quint32 pid)
diff --git a/libtaskmanager/tasktools.h b/libtaskmanager/tasktools.h
index eec7ed3d93..41f1a3c2c9 100644
--- a/libtaskmanager/tasktools.h
+++ b/libtaskmanager/tasktools.h
@@ -91,9 +91,27 @@ TASKMANAGER_EXPORT QUrl windowUrlFromMetadata(const QString &appId, quint32 pid
 TASKMANAGER_EXPORT KService::List servicesFromPid(quint32 pid);
 
 /**
- * Tries to map a given application to a desktop file
- * by looking into its process environment.
+ * Takes several bits of window metadata as input and tries to find
+ * the .desktop file for the application owning this window, or,
+ * failing that, the path to its executable.
+ *
+ * The source for the metadata is generally the window's appId on
+ * Wayland, or the window class part of the WM_CLASS window property
+ * on X Windows.
+ *
+ * @param appId A string uniquely identifying the application owning
+ * the window, ideally matching a .desktop file name.
+ * @param pid The process id for the process owning the window.
+ * @param xWindowsWMClassName The instance name part of X Windows'
+ * WM_CLASS window property.
+ * @returns A KService object matching the window
  */
+KService::Ptr serviceFromMetadata(const QString &appId, quint32 pid, const QString &xWindowsWMClassName);
+
+/**
+* Tries to map a given application to a desktop file
+* by looking into its process environment.
+*/
 KService::List servicesFromEnvironment(quint32 pid);
 
 /**
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.