PlasmaActivities

main.cpp
1/*
2 SPDX-FileCopyrightText: 2016 Ivan Cukic <ivan.cukic(at)kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include <QCoreApplication>
8#include <QDebug>
9#include <QTimer>
10
11#include <PlasmaActivities/Controller>
12
13#include "utils.h"
14
15// Output modifiers
16
17DEFINE_COMMAND(bare, 0)
18{
19 flags.bare = true;
20 return 0;
21}
22
23DEFINE_COMMAND(noBare, 0)
24{
25 flags.bare = false;
26 return 0;
27}
28
29DEFINE_COMMAND(color, 0)
30{
31 flags.color = true;
32 return 0;
33}
34
35DEFINE_COMMAND(noColor, 0)
36{
37 flags.color = false;
38 return 0;
39}
40
41// Activity management
42
43DEFINE_COMMAND(createActivity, 1)
44{
45 auto result = awaitFuture(controller->addActivity(args(1)));
46
47 qDebug().noquote() << result;
48
49 return 1;
50}
51
52DEFINE_COMMAND(removeActivity, 1)
53{
54 awaitFuture(controller->removeActivity(args(1)));
55
56 return 1;
57}
58
59DEFINE_COMMAND(startActivity, 1)
60{
61 awaitFuture(controller->startActivity(args(1)));
62
63 return 1;
64}
65
66DEFINE_COMMAND(stopActivity, 1)
67{
68 awaitFuture(controller->stopActivity(args(1)));
69
70 return 1;
71}
72
73DEFINE_COMMAND(listActivities, 0)
74{
75 for (const auto &activity : controller->activities()) {
76 printActivity(activity);
77 }
78
79 return 0;
80}
81
82DEFINE_COMMAND(currentActivity, 0)
83{
84 printActivity(controller->currentActivity());
85
86 return 0;
87}
88
89DEFINE_COMMAND(setActivityProperty, 3)
90{
91 const auto what = args(1);
92 const auto id = args(2);
93 const auto value = args(3);
94
95 // clang-format off
96 awaitFuture(
97 what == QLatin1String("name") ? controller->setActivityName(id, value) :
98 what == QLatin1String("description") ? controller->setActivityDescription(id, value) :
99 what == QLatin1String("icon") ? controller->setActivityIcon(id, value) :
101 );
102 // clang-format on
103
104 return 3;
105}
106
107DEFINE_COMMAND(activityProperty, 2)
108{
109 const auto what = args(1);
110 const auto id = args(2);
111
112 KActivities::Info info(id);
113 // clang-format off
114 out << (
115 what == QLatin1String("name") ? info.name() :
116 what == QLatin1String("description") ? info.description() :
117 what == QLatin1String("icon") ? info.icon() :
118 QString()
119 ) << "\n";
120 // clang-format on
121 return 2;
122}
123
124// Activity switching
125
126DEFINE_COMMAND(setCurrentActivity, 1)
127{
128 switchToActivity(args(1));
129
130 return 1;
131}
132
133DEFINE_COMMAND(nextActivity, 0)
134{
135 controller->nextActivity();
136 return 0;
137}
138
139DEFINE_COMMAND(previousActivity, 0)
140{
141 controller->previousActivity();
142 return 0;
143}
144
145void printHelp()
146{
147 if (!flags.bare) {
148 qDebug() << "\nModifiers (applied only to trailing commands):"
149 << "\n --bare, --no-bare - show minimal info vs show everything"
150 << "\n --color, --no-color - make the output pretty"
151
152 << "\n\nCommands:"
153 << "\n --list-activities - lists all activities"
154 << "\n --create-activity Name - creates a new activity with the specified name"
155 << "\n --remove-activity ID - removes the activity with the specified id"
156 << "\n --start-activity ID - starts the specified activity"
157 << "\n --stop-activity ID - stops the specified activity"
158
159 << "\n --current-activity - show the current activity"
160 << "\n --set-current-activity - sets the current activity"
161 << "\n --next-activity - switches to the next activity (in list-activities order)"
162 << "\n --previous-activity - switches to the previous activity (in list-activities order)"
163
164 << "\n --activity-property What ID"
165 << "\n - gets activity name, icon or description"
166 << "\n --set-activity-property What ID Value"
167 << "\n - changes activity name, icon or description";
168
169 } else {
170 qDebug() << "\n--bare"
171 << "\n--no-bare"
172 << "\n--color"
173 << "\n--no-color"
174 << "\n--list-activities"
175 << "\n--create-activity NAME"
176 << "\n--remove-activity ID"
177
178 << "\n--current-activity"
179 << "\n--set-current-activity"
180 << "\n--next-activity"
181 << "\n--previous-activity";
182 }
183}
184
185int main(int argc, char *argv[])
186{
187 QCoreApplication app(argc, argv);
188
189 QTimer::singleShot(0, &app, [] {
190 const auto args = QCoreApplication::arguments();
191
192 controller = new KActivities::Controller();
193
194 while (controller->serviceStatus() != KActivities::Controller::Running) {
196 }
197
198// clang-format off
199 #define MATCH_COMMAND(Command) \
200 else if (args[argId] == QLatin1String("--") + toDashes(QStringLiteral(#Command))) \
201 { \
202 argId += 1 + Command##_command({ args, argId })(); \
203 }
204 // clang-format on
205 if (args.count() <= 1) {
206 printHelp();
207
208 } else {
209 for (int argId = 1; argId < args.count();) {
210 if (args[argId] == QLatin1String("--help")) {
211 printHelp();
212 argId++;
213 }
214
215 MATCH_COMMAND(bare)
216 MATCH_COMMAND(noBare)
217 MATCH_COMMAND(color)
218 MATCH_COMMAND(noColor)
219
220 MATCH_COMMAND(listActivities)
221
222 MATCH_COMMAND(currentActivity)
223 MATCH_COMMAND(setCurrentActivity)
224 MATCH_COMMAND(activityProperty)
225 MATCH_COMMAND(setActivityProperty)
226 MATCH_COMMAND(nextActivity)
227 MATCH_COMMAND(previousActivity)
228
229 MATCH_COMMAND(createActivity)
230 MATCH_COMMAND(removeActivity)
231 MATCH_COMMAND(startActivity)
232 MATCH_COMMAND(stopActivity)
233
234 else
235 {
236 qDebug() << "Skipping unknown argument" << args[argId];
237 argId++;
238 }
239 }
240 }
241
242 delete controller;
243
245 });
246
247 return app.exec();
248}
@ Running
Service is running properly.
Definition consumer.h:79
This class provides methods for controlling and managing the activities.
Definition controller.h:38
This class provides info about an activity.
Definition info.h:59
QStringList arguments()
void processEvents(QEventLoop::ProcessEventsFlags flags)
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.