7#include "activityengine.h"
8#include "activityservice.h"
10#include <plasmaactivities/controller.h>
11#include <plasmaactivities/info.h>
13#include <QApplication>
14#include <QDBusServiceWatcher>
16#define ACTIVITYMANAGER_SERVICE "org.kde.kactivitymanagerd"
17#define ACTIVITYRANKING_OBJECT "/ActivityRanking"
19ActivityEngine::ActivityEngine(
QObject *parent)
25void ActivityEngine::init()
27 if (qApp->applicationName() == QLatin1String(
"plasma-netbook")) {
31 m_activityController =
new KActivities::Controller(
this);
32 m_currentActivity = m_activityController->currentActivity();
33 const QStringList activities = m_activityController->activities();
35 for (
const QString &
id : activities) {
39 connect(m_activityController, &KActivities::Controller::activityAdded,
this, &ActivityEngine::activityAdded);
40 connect(m_activityController, &KActivities::Controller::activityRemoved,
this, &ActivityEngine::activityRemoved);
41 connect(m_activityController, &KActivities::Controller::currentActivityChanged,
this, &ActivityEngine::currentActivityChanged);
46 m_runningActivities = m_activityController->activities(KActivities::Info::Running);
47 setData(QStringLiteral(
"Status"), QStringLiteral(
"Current"), m_currentActivity);
48 setData(QStringLiteral(
"Status"), QStringLiteral(
"Running"), m_runningActivities);
50 m_watcher =
new QDBusServiceWatcher(ACTIVITYMANAGER_SERVICE,
64void ActivityEngine::insertActivity(
const QString &
id)
67 KActivities::Info *activity =
new KActivities::Info(
id,
this);
68 m_activities[id] = activity;
69 setData(
id, QStringLiteral(
"Name"), activity->name());
70 setData(
id, QStringLiteral(
"Icon"), activity->icon());
71 setData(
id, QStringLiteral(
"Current"), m_currentActivity ==
id);
74 switch (activity->state()) {
75 case KActivities::Info::Running:
76 state = QLatin1String(
"Running");
78 case KActivities::Info::Starting:
79 state = QLatin1String(
"Starting");
81 case KActivities::Info::Stopping:
82 state = QLatin1String(
"Stopping");
84 case KActivities::Info::Stopped:
85 state = QLatin1String(
"Stopped");
87 case KActivities::Info::Invalid:
89 state = QLatin1String(
"Invalid");
91 setData(
id, QStringLiteral(
"State"), state);
92 setData(
id, QStringLiteral(
"Score"), m_activityScores.value(
id));
94 connect(activity, &KActivities::Info::infoChanged,
this, &ActivityEngine::activityDataChanged);
95 connect(activity, &KActivities::Info::stateChanged,
this, &ActivityEngine::activityStateChanged);
97 m_runningActivities << id;
100void ActivityEngine::disableRanking()
102 delete m_activityRankingClient;
105void ActivityEngine::enableRanking()
107 m_activityRankingClient =
new org::kde::ActivityManager::ActivityRanking(ACTIVITYMANAGER_SERVICE, ACTIVITYRANKING_OBJECT,
QDBusConnection::sessionBus());
108 connect(m_activityRankingClient, &org::kde::ActivityManager::ActivityRanking::rankingChanged,
this, &ActivityEngine::rankingChanged);
111 ACTIVITYRANKING_OBJECT,
112 QStringLiteral(
"org.kde.ActivityManager.ActivityRanking"),
113 QStringLiteral(
"activities"));
115 QDBusPendingCallWatcher *watcher =
new QDBusPendingCallWatcher(reply,
this);
121 QDBusPendingReply<ActivityDataList> reply = *watcher;
123 qDebug() <<
"Error getting activity scores: " << reply.
error().
message();
125 setActivityScores(reply.
value());
131void ActivityEngine::rankingChanged(
const QStringList &topActivities,
const ActivityDataList &activities)
133 Q_UNUSED(topActivities)
135 setActivityScores(activities);
138void ActivityEngine::setActivityScores(
const ActivityDataList &activities)
140 QSet<QString> presentActivities;
141 m_activityScores.clear();
143 for (
const ActivityData &activity : activities) {
144 if (m_activities.contains(activity.id)) {
145 setData(activity.id, QStringLiteral(
"Score"), activity.score);
147 presentActivities.
insert(activity.id);
148 m_activityScores[activity.id] = activity.score;
151 const auto controllerActivities = m_activityController->activities();
152 for (
const QString &activityId : controllerActivities) {
153 if (!presentActivities.
contains(activityId) && m_activities.contains(activityId)) {
154 setData(activityId, QStringLiteral(
"Score"), 0);
159void ActivityEngine::activityAdded(
const QString &
id)
162 setData(QStringLiteral(
"Status"), QStringLiteral(
"Running"), m_runningActivities);
165void ActivityEngine::activityRemoved(
const QString &
id)
168 KActivities::Info *activity = m_activities.take(
id);
172 m_runningActivities.removeAll(
id);
173 setData(QStringLiteral(
"Status"), QStringLiteral(
"Running"), m_runningActivities);
176void ActivityEngine::currentActivityChanged(
const QString &
id)
178 setData(m_currentActivity, QStringLiteral(
"Current"),
false);
179 m_currentActivity = id;
180 setData(
id, QStringLiteral(
"Current"),
true);
181 setData(QStringLiteral(
"Status"), QStringLiteral(
"Current"),
id);
184void ActivityEngine::activityDataChanged()
190 setData(activity->id(), QStringLiteral(
"Name"), activity->name());
191 setData(activity->id(), QStringLiteral(
"Icon"), activity->icon());
192 setData(activity->id(), QStringLiteral(
"Current"), m_currentActivity == activity->id());
193 setData(activity->id(), QStringLiteral(
"Score"), m_activityScores.value(activity->id()));
196void ActivityEngine::activityStateChanged()
199 const QString
id = activity->id();
204 switch (activity->state()) {
205 case KActivities::Info::Running:
206 state = QLatin1String(
"Running");
208 case KActivities::Info::Starting:
209 state = QLatin1String(
"Starting");
211 case KActivities::Info::Stopping:
212 state = QLatin1String(
"Stopping");
214 case KActivities::Info::Stopped:
215 state = QLatin1String(
"Stopped");
217 case KActivities::Info::Invalid:
219 state = QLatin1String(
"Invalid");
221 setData(
id, QStringLiteral(
"State"), state);
223 if (activity->state() == KActivities::Info::Running) {
224 if (!m_runningActivities.contains(
id)) {
225 m_runningActivities << id;
228 m_runningActivities.removeAll(
id);
231 setData(QStringLiteral(
"Status"), QStringLiteral(
"Running"), m_runningActivities);
237 ActivityService *service =
new ActivityService(m_activityController, source);
244#include "activityengine.moc"
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
Data provider for plasmoids (Plasma plugins)
void removeSource(const QString &source)
Removes a data source.
void setData(const QString &source, const QVariant &value)
Sets a value for a data source.
This class provides a generic API for write access to settings or services.
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
Namespace for everything in libplasma.
QDBusPendingCall asyncCall(const QDBusMessage &message, int timeout) const const
QDBusConnection sessionBus()
QString message() const const
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
void finished(QDBusPendingCallWatcher *self)
QDBusError error() const const
bool isError() const const
typename Select< 0 >::Type value() const const
void serviceRegistered(const QString &serviceName)
void serviceUnregistered(const QString &serviceName)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
QObject * sender() const const
void setParent(QObject *parent)
bool contains(const QSet< T > &other) const const
iterator insert(const T &value)