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

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_ACTIVITY_MODEL_H
0008 #define KACTIVITIES_IMPORTS_ACTIVITY_MODEL_H
0009 
0010 // Qt
0011 #include <QAbstractListModel>
0012 #include <QCollator>
0013 #include <QJSValue>
0014 #include <QObject>
0015 
0016 // STL and Boost
0017 #include <boost/container/flat_set.hpp>
0018 #include <memory>
0019 
0020 // Local
0021 #include <lib/consumer.h>
0022 #include <lib/controller.h>
0023 #include <lib/info.h>
0024 
0025 class QModelIndex;
0026 class QDBusPendingCallWatcher;
0027 
0028 namespace KActivities
0029 {
0030 namespace Imports
0031 {
0032 /**
0033  * ActivityModel
0034  */
0035 
0036 class ActivityModel : public QAbstractListModel
0037 {
0038     Q_OBJECT
0039 
0040     Q_PROPERTY(QString shownStates READ shownStates WRITE setShownStates NOTIFY shownStatesChanged)
0041 
0042 public:
0043     explicit ActivityModel(QObject *parent = nullptr);
0044     ~ActivityModel() override;
0045 
0046     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0047 
0048     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0049 
0050     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0051 
0052     QHash<int, QByteArray> roleNames() const override;
0053 
0054     enum Roles {
0055         ActivityId = Qt::UserRole,
0056         ActivityDescription = Qt::UserRole + 1,
0057         ActivityIcon = Qt::UserRole + 2,
0058         ActivityState = Qt::UserRole + 3,
0059         ActivityBackground = Qt::UserRole + 4,
0060         ActivityCurrent = Qt::UserRole + 5,
0061     };
0062 
0063     enum State {
0064         All = 0,
0065         Invalid = 0,
0066         Running = 2,
0067         Starting = 3,
0068         Stopped = 4,
0069         Stopping = 5,
0070     };
0071     Q_ENUM(State)
0072 
0073 public Q_SLOTS:
0074     // Activity control methods
0075     void setActivityName(const QString &id, const QString &name, const QJSValue &callback);
0076     void setActivityDescription(const QString &id, const QString &description, const QJSValue &callback);
0077     void setActivityIcon(const QString &id, const QString &icon, const QJSValue &callback);
0078 
0079     void setCurrentActivity(const QString &id, const QJSValue &callback);
0080 
0081     void addActivity(const QString &name, const QJSValue &callback);
0082     void removeActivity(const QString &id, const QJSValue &callback);
0083 
0084     void stopActivity(const QString &id, const QJSValue &callback);
0085     void startActivity(const QString &id, const QJSValue &callback);
0086 
0087     // Model property getters and setters
0088     void setShownStates(const QString &states);
0089     QString shownStates() const;
0090 
0091 Q_SIGNALS:
0092     void shownStatesChanged(const QString &state);
0093 
0094 private Q_SLOTS:
0095     void onActivityNameChanged(const QString &name);
0096     void onActivityDescriptionChanged(const QString &description);
0097     void onActivityIconChanged(const QString &icon);
0098     void onActivityStateChanged(KActivities::Info::State state);
0099 
0100     void replaceActivities(const QStringList &activities);
0101     void onActivityAdded(const QString &id, bool notifyClients = true);
0102     void onActivityRemoved(const QString &id);
0103     void onCurrentActivityChanged(const QString &id);
0104 
0105     void setServiceStatus(KActivities::Consumer::ServiceStatus status);
0106 
0107 private:
0108     KActivities::Controller m_service;
0109     boost::container::flat_set<State> m_shownStates;
0110     QString m_shownStatesString;
0111 
0112     typedef std::shared_ptr<Info> InfoPtr;
0113 
0114     struct InfoPtrComparator {
0115         bool operator()(const InfoPtr &left, const InfoPtr &right) const
0116         {
0117             QCollator c;
0118             c.setCaseSensitivity(Qt::CaseInsensitive);
0119             c.setNumericMode(true);
0120             int rc = c.compare(left->name(), right->name());
0121             if (rc == 0) {
0122                 return left->id() < right->id();
0123             }
0124             return rc < 0;
0125         }
0126     };
0127 
0128     boost::container::flat_set<InfoPtr, InfoPtrComparator> m_knownActivities;
0129     boost::container::flat_set<InfoPtr, InfoPtrComparator> m_shownActivities;
0130 
0131     InfoPtr registerActivity(const QString &id);
0132     void unregisterActivity(const QString &id);
0133     void showActivity(InfoPtr activityInfo, bool notifyClients);
0134     void hideActivity(const QString &id);
0135     void backgroundsUpdated(const QStringList &activities);
0136 
0137     InfoPtr findActivity(QObject *ptr) const;
0138 
0139     class Private;
0140     friend class Private;
0141 };
0142 
0143 } // namespace Imports
0144 } // namespace KActivities
0145 
0146 #endif // KACTIVITIES_IMPORTS_ACTIVITY_MODEL_H