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

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 "abstracttasksmodel.h"
0010 
0011 #include <memory>
0012 
0013 #include "taskmanager_export.h"
0014 
0015 #include <QUrl>
0016 
0017 namespace TaskManager
0018 {
0019 /**
0020  * @short A tasks model for launchers.
0021  *
0022  * This model presents tasks sourced from list of launcher URLs. The
0023  * list can be read from and written to from a notifiable prop, enabling
0024  * storage outside the instance (e.g. in config).
0025  *
0026  * Extends AbstractTasksModel API with API for adding, removing, checking
0027  * for and moving launchers by URL or row index.
0028  *
0029  * Launcher URLs can use the preferred:// protocol to request system
0030  * default applications such as "browser" and "mailer".
0031  *
0032  * @see defaultApplication
0033  *
0034  * @author Eike Hein <hein@kde.org>
0035  */
0036 
0037 class TASKMANAGER_EXPORT LauncherTasksModel : public AbstractTasksModel
0038 {
0039     Q_OBJECT
0040 
0041     Q_PROPERTY(QStringList launcherList READ launcherList WRITE setLauncherList NOTIFY launcherListChanged)
0042 
0043 public:
0044     explicit LauncherTasksModel(QObject *parent = nullptr);
0045     ~LauncherTasksModel() override;
0046 
0047     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0048     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0049     int rowCountForActivity(const QString &activity) const;
0050 
0051     /**
0052      * The list of launcher URLs serialized to strings along with
0053      * the activities they belong to.
0054      *
0055      * @see setLauncherList
0056      * @returns the list of launcher URLs serialized to strings.
0057      **/
0058     QStringList launcherList() const;
0059 
0060     /**
0061      * Replace the list of launcher URL strings.
0062      *
0063      * Invalid or empty URLs will be rejected. Duplicate URLs will be
0064      * collapsed.
0065      *
0066      * @see launcherList
0067      * @param launchers A list of launcher URL strings.
0068      **/
0069     void setLauncherList(const QStringList &launchers);
0070 
0071     /**
0072      * Request adding a launcher with the given URL.
0073      *
0074      * If this URL is already in the list, the request will fail. URLs are
0075      * compared for equality after removing the query string used to hold
0076      * metadata.
0077      *
0078      * @see launcherUrlsMatch
0079      * @param url A launcher URL.
0080      * @returns @c true if a launcher was added.
0081      */
0082     bool requestAddLauncher(const QUrl &url);
0083 
0084     /**
0085      * Request removing the launcher with the given URL.
0086      *
0087      * If this URL is already in the list, the request will fail. URLs are
0088      * compared for equality after removing the query string used to hold
0089      * metadata.
0090      *
0091      * @see launcherUrlsMatch
0092      * @param url A launcher URL.
0093      * @returns @c true if the launcher was removed.
0094      */
0095     bool requestRemoveLauncher(const QUrl &url);
0096 
0097     /**
0098      * Request adding a launcher with the given URL to current activity.
0099      *
0100      * If this URL is already in the list, the request will fail. URLs are
0101      * compared for equality after removing the query string used to hold
0102      * metadata.
0103      *
0104      * @see launcherUrlsMatch
0105      * @param url A launcher URL.
0106      * @returns @c true if a launcher was added.
0107      */
0108     bool requestAddLauncherToActivity(const QUrl &url, const QString &activity);
0109 
0110     /**
0111      * Request removing the launcher with the given URL from the current activity.
0112      *
0113      * If this URL is already in the list, the request will fail. URLs are
0114      * compared for equality after removing the query string used to hold
0115      * metadata.
0116      *
0117      * @see launcherUrlsMatch
0118      * @param url A launcher URL.
0119      * @returns @c true if the launcher was removed.
0120      */
0121     bool requestRemoveLauncherFromActivity(const QUrl &url, const QString &activity);
0122 
0123     /**
0124      * Return the list of activities the launcher belongs to.
0125      * If there is no launcher with that url, the list will be empty,
0126      * while if the launcher is on all activities, it will contain a
0127      * null uuid.
0128      *
0129      * URLs are compared for equality after removing the query string used
0130      * to hold metadata.
0131      */
0132     QStringList launcherActivities(const QUrl &url) const;
0133 
0134     /**
0135      * Return the position of the launcher with the given URL.
0136      *
0137      * URLs are compared for equality after removing the query string used
0138      * to hold metadata.
0139      *
0140      * @see launcherUrlsMatch
0141      * @param url A launcher URL.
0142      * @returns @c -1 if no launcher exists for the given URL.
0143      */
0144     int launcherPosition(const QUrl &url) const;
0145 
0146     /**
0147      * Runs the URL (i.e. launches the application) at the given index.
0148      *
0149      * @param index An index in this launcher tasks model.
0150      */
0151     void requestActivate(const QModelIndex &index) override;
0152 
0153     /**
0154      * Runs the URL (i.e. launches the application) at the given index.
0155      *
0156      * @param index An index in this launcher tasks model.
0157      */
0158     void requestNewInstance(const QModelIndex &index) override;
0159 
0160     /**
0161      * Runs the application backing the launcher at the given index with the given URLs.
0162      *
0163      * @param index An index in this launcher tasks model
0164      * @param urls The URLs to be passed to the application
0165      */
0166     void requestOpenUrls(const QModelIndex &index, const QList<QUrl> &urls) override;
0167 
0168 Q_SIGNALS:
0169     void launcherListChanged() const;
0170 
0171 private:
0172     class Private;
0173     std::unique_ptr<Private> d;
0174 };
0175 
0176 }