File indexing completed on 2024-05-05 05:38:37

0001 /*
0002     SPDX-FileCopyrightText: 2016 Eike Hein <hein@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include "taskmanager_export.h"
0010 
0011 #include <QIcon>
0012 #include <QModelIndex>
0013 #include <QUrl>
0014 
0015 #include <KService>
0016 #include <KSharedConfig>
0017 
0018 namespace TaskManager
0019 {
0020 struct AppData {
0021     QString id; // Application id (*.desktop sans extension).
0022     QString name; // Application name.
0023     QString genericName; // Generic application name.
0024     QIcon icon;
0025     QUrl url;
0026     bool skipTaskbar = false;
0027 };
0028 
0029 enum UrlComparisonMode {
0030     Strict = 0,
0031     IgnoreQueryItems,
0032 };
0033 
0034 /**
0035  * Fills in and returns an AppData struct based on the given URL.
0036  *
0037  * If the URL contains iconData in its query string, it is decoded and
0038  * set as AppData.icon, taking precedence over normal icon discovery.
0039  *
0040  * If the URL is using the preferred:// scheme, the URL it resolves to
0041  * is set as AppData.url.
0042  *
0043  * The supplied fallback icon is set as AppData.icon if no other icon
0044  * could be found.
0045  *
0046  * @see defaultApplication
0047  * @param url A URL to a .desktop file or executable, or a preferred:// URL.
0048  * @param fallbackIcon An icon to use when none could be read from the URL or
0049  * otherwise found.
0050  * @returns @c AppData filled in based on the given URL.
0051  */
0052 TASKMANAGER_EXPORT AppData appDataFromUrl(const QUrl &url, const QIcon &fallbackIcon = QIcon());
0053 
0054 /**
0055  * Takes several bits of window metadata as input and tries to find
0056  * the .desktop file for the application owning this window, or,
0057  * failing that, the path to its executable.
0058  *
0059  * The source for the metadata is generally the window's appId on
0060  * Wayland, or the window class part of the WM_CLASS window property
0061  * on X Windows.
0062  *
0063  * TODO: The supplied config object can contain various mapping and
0064  * mangling rules that affect the behavior of this function, allowing
0065  * to map bits of metadata to different values and other things. This
0066  * config file format still needs to be documented fully; in the
0067  * meantime the bundled default rules in taskmanagerrulesrc (the
0068  * config file opened by various models in this library) can be used
0069  * for reference.
0070  *
0071  * @param appId A string uniquely identifying the application owning
0072  * the window, ideally matching a .desktop file name.
0073  * @param pid The process id for the process owning the window.
0074  * @param config A KConfig object parameterizing the matching
0075  * behavior.
0076  * @param xWindowsWMClassName The instance name part of X Windows'
0077  * WM_CLASS window property.
0078  * @returns A .desktop file or executable path for the application
0079  * owning the window.
0080  */
0081 TASKMANAGER_EXPORT QUrl windowUrlFromMetadata(const QString &appId,
0082                                               quint32 pid = 0,
0083                                               KSharedConfig::Ptr config = KSharedConfig::Ptr(),
0084                                               const QString &xWindowsWMClassName = QString());
0085 
0086 /**
0087  * Returns a list of (usually application) KService instances for the
0088  * given process id, by examining the process and querying the service
0089  * database for process metadata.
0090  *
0091  * @param pid A process id.
0092  * @param rulesConfig A KConfig object parameterizing the matching
0093  * behavior.
0094  * @returns A list of KService instances.
0095  */
0096 TASKMANAGER_EXPORT KService::List servicesFromPid(quint32 pid, KSharedConfig::Ptr rulesConfig = KSharedConfig::Ptr());
0097 
0098 /**
0099  * Returns a list of (usually application) KService instances for the
0100  * given process command line and process name, by mangling the command
0101  * line in various ways and checking the data against the Exec keys in
0102  * the service database. Mangling is done e.g. to check for executable
0103  * names with and without paths leading to them and to ignore arguments.
0104  * if needed.
0105  *
0106  * The [Settings]TryIgnoreRuntimes key in the supplied config object can
0107  * hold a comma-separated list of runtime executables that this code will
0108  * try to ignore in the process command line. This is useful in cases where
0109  * the command line has the contents of a .desktop Exec key prefixed with
0110  * a runtime executable. The code tries to strip the path to the runtime
0111  * executable if needed.
0112  *
0113  * @param cmdLine A process command line.
0114  * @param processName The process name.
0115  * @param rulesConfig A KConfig object parameterizing the matching
0116  * behavior.
0117  * @returns A list of KService instances.
0118  */
0119 TASKMANAGER_EXPORT KService::List
0120 servicesFromCmdLine(const QString &cmdLine, const QString &processName, KSharedConfig::Ptr rulesConfig = KSharedConfig::Ptr());
0121 
0122 /**
0123  * Returns an application id for an URL using the preferred:// scheme.
0124  *
0125  * Recognized values for the host component of the URL are:
0126  * - "browser"
0127  * - "mailer"
0128  * - "terminal"
0129  * - "filemanager"
0130  *
0131  * @param url A URL using the preferred:// scheme.
0132  * @returns an application id for the given URL.
0133  **/
0134 TASKMANAGER_EXPORT QString defaultApplication(const QUrl &url);
0135 
0136 /**
0137  * Convenience function to compare two launcher URLs either strictly
0138  * or ignoring their query strings.
0139  *
0140  * @see LauncherTasksModel
0141  * @param a The first launcher URL.
0142  * @param b The second launcher URL.
0143  * @param mode The comparison mode. Either Strict or IgnoreQueryItems.
0144  * @returns @c true if the URLs match.
0145  **/
0146 TASKMANAGER_EXPORT bool launcherUrlsMatch(const QUrl &a, const QUrl &b, UrlComparisonMode mode = Strict);
0147 
0148 /**
0149  * Determines whether tasks model entries belong to the same app.
0150  *
0151  * @param a The first model index.
0152  * @param b The second model index.
0153  * @returns @c true if the model entries belong to the same app.
0154  **/
0155 TASKMANAGER_EXPORT bool appsMatch(const QModelIndex &a, const QModelIndex &b);
0156 
0157 /**
0158  * Given global coordinates, returns the geometry of the screen they are
0159  * on, or the geometry of the screen they are closest to.
0160  *
0161  * @param pos Coordinates in global space.
0162  * @return The geometry of the screen containing pos or closest to pos.
0163  */
0164 TASKMANAGER_EXPORT QRect screenGeometry(const QPoint &pos);
0165 
0166 /**
0167  * Attempts to run the application described by the AppData struct that
0168  * is passed in, optionally also handing the application a list of URLs
0169  * to open.
0170  *
0171  * @param appData An application data struct.
0172  * @param urls A list of URLs for the application to open.
0173  */
0174 TASKMANAGER_EXPORT void runApp(const AppData &appData, const QList<QUrl> &urls = QList<QUrl>());
0175 
0176 bool canLauchNewInstance(const AppData &appData);
0177 }