File indexing completed on 2022-12-06 13:15:25

0001 /*
0002     SPDX-FileCopyrightText: 2012, 2013, 2014 Ivan Cukic <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef KACTIVITIES_IMPORTS_RESOURCE_MODEL_H
0008 #define KACTIVITIES_IMPORTS_RESOURCE_MODEL_H
0009 
0010 // Qt
0011 #include <QJSValue>
0012 #include <QObject>
0013 #include <QSortFilterProxyModel>
0014 #include <QSqlDatabase>
0015 #include <QSqlTableModel>
0016 
0017 // KDE
0018 #include <KConfigGroup>
0019 
0020 // STL and Boost
0021 #include <memory>
0022 
0023 // Local
0024 #include <lib/consumer.h>
0025 #include <lib/controller.h>
0026 #include <lib/info.h>
0027 
0028 class QModelIndex;
0029 class QDBusPendingCallWatcher;
0030 
0031 namespace KActivities
0032 {
0033 namespace Imports
0034 {
0035 /**
0036  * ResourceModel
0037  */
0038 
0039 class ResourceModel : public QSortFilterProxyModel
0040 {
0041     Q_OBJECT
0042 
0043     /**
0044      * Sets for which activities should the resources be shown for.
0045      * Coma-separated values.
0046      * Special values are:
0047      *  - ":current" for the current activity
0048      *  - ":any" show resources that are linked to any activity, including "global"
0049      *  - ":global" show resources that are globally linked
0050      */
0051     Q_PROPERTY(QString shownActivities READ shownActivities WRITE setShownActivities NOTIFY shownActivitiesChanged)
0052 
0053     /**
0054      * Sets for which agents should the resources be shown for.
0055      * Coma-separated values.
0056      * Special values are:
0057      *  - ":current" for the current application
0058      *  - ":any" show resources that are linked to any agent, including "global"
0059      *  - ":global" show resources that are globally linked
0060      */
0061     Q_PROPERTY(QString shownAgents READ shownAgents WRITE setShownAgents NOTIFY shownAgentsChanged)
0062 
0063     /**
0064      * If the model is empty, use this config file to read the default items.
0065      * The default items are automatically linked globally, not per-activity.
0066      * It needs to have the following format: 'config-namerc/ConfigGroup/ConfigEntry'.
0067      * The config entry needs to be a list of strings.
0068      */
0069     Q_PROPERTY(QString defaultItemsConfig READ defaultItemsConfig WRITE setDefaultItemsConfig)
0070 
0071 public:
0072     explicit ResourceModel(QObject *parent = nullptr);
0073     ~ResourceModel() override;
0074 
0075     enum Roles {
0076         ResourceRole = Qt::UserRole,
0077         ActivityRole = Qt::UserRole + 1,
0078         AgentRole = Qt::UserRole + 2,
0079         DescriptionRole = Qt::UserRole + 3,
0080     };
0081 
0082     QHash<int, QByteArray> roleNames() const override;
0083 
0084     virtual QVariant data(const QModelIndex &proxyIndex, int role = Qt::DisplayRole) const override;
0085 
0086 public Q_SLOTS:
0087     // Resource linking control methods
0088     void linkResourceToActivity(const QString &resource, const QJSValue &callback) const;
0089     void linkResourceToActivity(const QString &resource, const QString &activity, const QJSValue &callback) const;
0090     void linkResourceToActivity(const QString &agent, const QString &resource, const QString &activity, const QJSValue &callback) const;
0091 
0092     void unlinkResourceFromActivity(const QString &resource, const QJSValue &callback);
0093     void unlinkResourceFromActivity(const QString &resource, const QString &activity, const QJSValue &callback);
0094     void unlinkResourceFromActivity(const QString &agent, const QString &resource, const QString &activity, const QJSValue &callback);
0095     void unlinkResourceFromActivity(const QStringList &agents, const QString &resource, const QStringList &activities, const QJSValue &callback);
0096 
0097     bool isResourceLinkedToActivity(const QString &resource);
0098     bool isResourceLinkedToActivity(const QString &resource, const QString &activity);
0099     bool isResourceLinkedToActivity(const QString &agent, const QString &resource, const QString &activity);
0100     bool isResourceLinkedToActivity(const QStringList &agents, const QString &resource, const QStringList &activities);
0101 
0102     // Model property getters and setters
0103     void setShownActivities(const QString &activities);
0104     QString shownActivities() const;
0105 
0106     void setShownAgents(const QString &agents);
0107     QString shownAgents() const;
0108 
0109     QString defaultItemsConfig() const;
0110     void setDefaultItemsConfig(const QString &defaultItemsConfig);
0111 
0112     void setOrder(const QStringList &resources);
0113     void move(int sourceItem, int destinationItem);
0114     void sortItems(Qt::SortOrder sortOrder);
0115 
0116     KConfigGroup config() const;
0117 
0118     int count() const;
0119     QString displayAt(int row) const;
0120     QString resourceAt(int row) const;
0121 
0122 protected:
0123     bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
0124 
0125 Q_SIGNALS:
0126     void shownActivitiesChanged();
0127     void shownAgentsChanged();
0128 
0129 private Q_SLOTS:
0130     void onCurrentActivityChanged(const QString &activity);
0131 
0132     void onResourceLinkedToActivity(const QString &initiatingAgent, const QString &targettedResource, const QString &usedActivity);
0133     void onResourceUnlinkedFromActivity(const QString &initiatingAgent, const QString &targettedResource, const QString &usedActivity);
0134 
0135 private:
0136     KActivities::Consumer m_service;
0137 
0138     inline QVariant dataForColumn(const QModelIndex &index, int column) const;
0139 
0140     QString activityToWhereClause(const QString &activity) const;
0141     QString agentToWhereClause(const QString &agent) const;
0142     QString whereClause(const QStringList &activities, const QStringList &agents) const;
0143 
0144     void loadDefaultsIfNeeded() const;
0145 
0146     bool loadDatabase();
0147     QString m_databaseFile;
0148     QSqlDatabase m_database;
0149     QSqlTableModel *m_databaseModel;
0150 
0151     QStringList m_shownActivities;
0152     QStringList m_shownAgents;
0153     QStringList m_sorting;
0154 
0155     QString m_defaultItemsConfig;
0156     mutable bool m_defaultItemsLoaded;
0157 
0158     void reloadData();
0159     QString validateResource(const QString &resource) const;
0160 
0161     class LinkerService;
0162     std::shared_ptr<LinkerService> m_linker;
0163 
0164     mutable KConfigGroup m_config;
0165 };
0166 
0167 } // namespace Imports
0168 } // namespace KActivities
0169 
0170 #endif // KACTIVITIES_IMPORTS_RESOURCE_MODEL_H