File indexing completed on 2024-04-21 16:17:20

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