PlasmaActivities

info.cpp
1/*
2 SPDX-FileCopyrightText: 2010-2016 Ivan Cukic <ivan.cukic(at)kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "info.h"
8#include "info_p.h"
9#include "manager_p.h"
10
11#include "utils/dbusfuture_p.h"
12
13#include <QFileSystemWatcher>
14
15namespace KActivities
16{
17// InfoPrivate
18
19InfoPrivate::InfoPrivate(Info *info, const QString &activity)
20 : q(info)
21 , cache(ActivitiesCache::self())
22 , id(activity)
23{
24}
25
26// clang-format off
27// Filters out signals for only this activity
28#define IMPLEMENT_SIGNAL_HANDLER(INTERNAL) \
29 void InfoPrivate::INTERNAL(const QString &_id) const \
30 { if (id == _id) Q_EMIT q->INTERNAL(); }
31
32IMPLEMENT_SIGNAL_HANDLER(added)
33IMPLEMENT_SIGNAL_HANDLER(removed)
34IMPLEMENT_SIGNAL_HANDLER(started)
35IMPLEMENT_SIGNAL_HANDLER(stopped)
36IMPLEMENT_SIGNAL_HANDLER(infoChanged)
37
38#undef IMPLEMENT_SIGNAL_HANDLER
39
40#define IMPLEMENT_SIGNAL_HANDLER(INTERNAL) \
41 void InfoPrivate::INTERNAL##Changed(const QString &_id, \
42 const QString &val) const \
43 { \
44 if (id == _id) { \
45 Q_EMIT q->INTERNAL##Changed(val); \
46 } \
47 }
48
49IMPLEMENT_SIGNAL_HANDLER(name)
50IMPLEMENT_SIGNAL_HANDLER(description)
51IMPLEMENT_SIGNAL_HANDLER(icon)
52
53#undef IMPLEMENT_SIGNAL_HANDLER
54
55void InfoPrivate::activityStateChanged(const QString &idChanged,
56 int newState) const
57{
58 if (idChanged == id) {
59 auto state = static_cast<Info::State>(newState);
60 Q_EMIT q->stateChanged(state);
61
62 if (state == KActivities::Info::Stopped) {
63 Q_EMIT q->stopped();
64 } else if (state == KActivities::Info::Running) {
65 Q_EMIT q->started();
66 }
67 }
68}
69
70void InfoPrivate::setCurrentActivity(const QString &currentActivity)
71{
72 if (isCurrent) {
73 if (currentActivity != id) {
74 // We are no longer the current activity
75 isCurrent = false;
76 Q_EMIT q->isCurrentChanged(false);
77 }
78 } else {
79 if (currentActivity == id) {
80 // We are the current activity
81 isCurrent = true;
82 Q_EMIT q->isCurrentChanged(true);
83 }
84 }
85}
86
87// Info
88Info::Info(const QString &activity, QObject *parent)
89 : QObject(parent)
90 , d(new InfoPrivate(this, activity))
91{
92 // qDebug() << "Created an instance of Info: " << (void*)this;
93#define PASS_SIGNAL_HANDLER(SIGNAL_NAME,SLOT_NAME) \
94 connect(d->cache.get(), SIGNAL(SIGNAL_NAME(QString)), \
95 this, SLOT(SLOT_NAME(QString)));
96
97 PASS_SIGNAL_HANDLER(activityAdded,added)
98 PASS_SIGNAL_HANDLER(activityRemoved,removed)
99 // PASS_SIGNAL_HANDLER(started)
100 // PASS_SIGNAL_HANDLER(stopped)
101 PASS_SIGNAL_HANDLER(activityChanged,infoChanged)
102#undef PASS_SIGNAL_HANDLER
103
104#define PASS_SIGNAL_HANDLER(SIGNAL_NAME,SLOT_NAME,TYPE) \
105 connect(d->cache.get(), SIGNAL(SIGNAL_NAME(QString,TYPE)), \
106 this, SLOT(SLOT_NAME(QString,TYPE))); \
107
108 PASS_SIGNAL_HANDLER(activityStateChanged,activityStateChanged,int);
109 PASS_SIGNAL_HANDLER(activityNameChanged,nameChanged,QString);
110 PASS_SIGNAL_HANDLER(activityDescriptionChanged,descriptionChanged,QString);
111 PASS_SIGNAL_HANDLER(activityIconChanged,iconChanged,QString);
112// clang-format on
113#undef PASS_SIGNAL_HANDLER
114 connect(d->cache.get(), SIGNAL(currentActivityChanged(QString)), this, SLOT(setCurrentActivity(QString)));
115
116 d->isCurrent = (d->cache.get()->m_currentActivity == activity);
117}
118
119Info::~Info()
120{
121 // qDebug() << "Deleted an instance of Info: " << (void*)this;
122}
123
124bool Info::isValid() const
125{
126 auto currentState = state();
127 return (currentState != Invalid && currentState != Unknown);
128}
129
131{
132 return QStringLiteral("activities://") + d->id;
133}
134
135QString Info::id() const
136{
137 return d->id;
138}
139
140bool Info::isCurrent() const
141{
142 return d->isCurrent;
143}
144
145Info::State Info::state() const
146{
147 if (d->cache->m_status == Consumer::Unknown) {
148 return Info::Unknown;
149 }
150
151 auto info = d->cache->getInfo(d->id);
152
153 if (!info) {
154 return Info::Invalid;
155 }
156
157 return static_cast<Info::State>(info->state);
158}
159
160void InfoPrivate::setServiceStatus(Consumer::ServiceStatus status) const
161{
162 switch (status) {
165 activityStateChanged(id, Info::Unknown);
166 break;
167
168 default:
169 activityStateChanged(id, q->state());
170 break;
171 }
172}
173
175{
176 Availability result = Nothing;
177
178 if (!Manager::isServiceRunning()) {
179 return result;
180 }
181
182 if (Manager::activities()->ListActivities().value().contains(d->id)) {
183 result = BasicInfo;
184
185 if (Manager::features()->IsFeatureOperational(QStringLiteral("resources/linking"))) {
186 result = Everything;
187 }
188 }
189
190 return result;
191}
192
193// clang-format off
194#define CREATE_GETTER(What) \
195 QString Info::What() const \
196 { \
197 auto info = d->cache->getInfo(d->id); \
198 return info ? info->What : QString(); \
199 }
200// clang-format on
201
202CREATE_GETTER(name)
203CREATE_GETTER(description)
204CREATE_GETTER(icon)
205
206#undef CREATE_GETTER
207
208} // namespace KActivities
209
210#include "moc_info.cpp"
ServiceStatus
Different states of the activities service.
Definition consumer.h:76
@ Unknown
Unable to determine the status of the service.
Definition consumer.h:78
@ NotRunning
Service is not running.
Definition consumer.h:77
This class provides info about an activity.
Definition info.h:59
bool isValid() const
Definition info.cpp:124
State
State of the activity.
Definition info.h:90
@ Unknown
Information is not yet retrieved from the service.
Definition info.h:92
@ Running
Activity is running.
Definition info.h:93
@ Invalid
This activity does not exist.
Definition info.h:91
@ Stopped
Activity is stopped.
Definition info.h:95
Availability
Specifies which parts of this class are functional.
Definition info.h:81
@ Everything
Everything is available.
Definition info.h:84
@ Nothing
No activity info provided (isValid is false)
Definition info.h:82
@ BasicInfo
Basic info is provided.
Definition info.h:83
Availability availability() const
Definition info.cpp:174
QString uri() const
Definition info.cpp:130
Q_SCRIPTABLE CaptureState status()
Namespace for everything in libkactivities.
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 12:01:06 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.