File indexing completed on 2022-11-29 13:02:28

0001 /*
0002     SPDX-FileCopyrightText: 2013-2016 Ivan Cukic <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef ACTIVITIES_CACHE_P_H
0008 #define ACTIVITIES_CACHE_P_H
0009 
0010 #include <memory>
0011 
0012 #include <QObject>
0013 
0014 #include <common/dbus/org.kde.ActivityManager.Activities.h>
0015 #include <utils/ptr_to.h>
0016 
0017 #include "activities_interface.h"
0018 #include "consumer.h"
0019 
0020 namespace KActivities
0021 {
0022 class ActivitiesCache : public QObject
0023 {
0024     Q_OBJECT
0025 
0026 public:
0027     static std::shared_ptr<ActivitiesCache> self();
0028 
0029     ~ActivitiesCache() override;
0030 
0031 Q_SIGNALS:
0032     void activityAdded(const QString &id);
0033     void activityChanged(const QString &id);
0034     void activityRemoved(const QString &id);
0035 
0036     void activityStateChanged(const QString &id, int state);
0037     void activityNameChanged(const QString &id, const QString &name);
0038     void activityDescriptionChanged(const QString &id, const QString &description);
0039     void activityIconChanged(const QString &id, const QString &icon);
0040 
0041     void currentActivityChanged(const QString &id);
0042     void serviceStatusChanged(Consumer::ServiceStatus status);
0043     void activityListChanged();
0044     void runningActivityListChanged();
0045 
0046 private Q_SLOTS:
0047     void updateAllActivities();
0048     void loadOfflineDefaults();
0049 
0050     void updateActivity(const QString &id);
0051     void updateActivityState(const QString &id, int state);
0052     void removeActivity(const QString &id);
0053 
0054     void setActivityInfoFromReply(QDBusPendingCallWatcher *watcher);
0055     void setAllActivitiesFromReply(QDBusPendingCallWatcher *watcher);
0056     void setCurrentActivityFromReply(QDBusPendingCallWatcher *watcher);
0057 
0058     void setActivityName(const QString &id, const QString &name);
0059     void setActivityDescription(const QString &id, const QString &description);
0060     void setActivityIcon(const QString &id, const QString &icon);
0061 
0062     void setActivityInfo(const ActivityInfo &info);
0063     void setAllActivities(const ActivityInfoList &activities);
0064     void setCurrentActivity(const QString &activity);
0065 
0066     void setServiceStatus(bool status);
0067 
0068 public:
0069     template<typename _Result, typename _Functor>
0070     void passInfoFromReply(QDBusPendingCallWatcher *watcher, _Functor f);
0071 
0072     static bool infoLessThan(const ActivityInfo &info, const ActivityInfo &other)
0073     {
0074         const auto comp = QString::compare(info.name, other.name, Qt::CaseInsensitive);
0075         return comp < 0 || (comp == 0 && info.id < other.id);
0076     }
0077 
0078     ActivityInfoList::iterator find(const QString &id)
0079     {
0080         return std::find_if(m_activities.begin(), m_activities.end(), [&id](const ActivityInfo &info) {
0081             return info.id == id;
0082         });
0083     }
0084 
0085     ActivityInfoList::iterator lower_bound(const ActivityInfo &info)
0086     {
0087         return std::lower_bound(m_activities.begin(), m_activities.end(), info, &infoLessThan);
0088     }
0089 
0090     template<int Policy = kamd::utils::Const>
0091     inline typename kamd::utils::ptr_to<ActivityInfo, Policy>::type getInfo(const QString &id)
0092     {
0093         const auto where = find(id);
0094 
0095         if (where != m_activities.end()) {
0096             return &(*where);
0097         }
0098 
0099         return nullptr;
0100     }
0101 
0102     template<typename TargetSlot>
0103     void onCallFinished(QDBusPendingCall &call, TargetSlot slot)
0104     {
0105         auto watcher = new QDBusPendingCallWatcher(call, this);
0106 
0107         connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher *)), this, slot);
0108     }
0109 
0110     ActivitiesCache();
0111 
0112     QList<ActivityInfo> m_activities;
0113     QString m_currentActivity;
0114     Consumer::ServiceStatus m_status;
0115 };
0116 
0117 } // namespace KActivities
0118 
0119 #endif /* ACTIVITIES_CACHE_P_H */