PlasmaActivities

utils.h
1/*
2 SPDX-FileCopyrightText: 2016 Ivan Čukić <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#ifndef KACTIVITIES_UTILS_H
8#define KACTIVITIES_UTILS_H
9
10QTextStream out(stdout);
11
12class StringListView
13{
14public:
15 StringListView(const QStringList &list, int start, int end = -1)
16 : m_list(list)
17 , m_start(start)
18 , m_size((end == -1 ? list.count() : end) - start)
19 {
20 }
21
22 const QString &operator()(int index) const
23 {
24 return m_list[m_start + index];
25 }
26
27 int count() const
28 {
29 return m_size;
30 }
31
32private:
33 const QStringList &m_list;
34 int m_start;
35 int m_size;
36};
37
38KActivities::Controller *controller = nullptr;
39
40class Flags
41{
42public:
43 Flags()
44 : bare(false)
45 , color(true)
46 {
47 }
48
49 bool bare;
50 bool color;
51
52} flags;
53
54QString toDashes(const QString &command)
55{
56 QString result(command);
57
58 for (int i = 0; i < result.size() - 1; ++i) {
59 if (result[i].isLower() && result[i + 1].isUpper()) {
60 result[i + 1] = result[i + 1].toLower();
61 result.insert(i + 1, QStringLiteral("-"));
62 }
63 }
64
65 return result;
66}
67
68void printActivity(const QString &id)
69{
70 // clang-format off
71 if (flags.bare) {
72 out << id << "\n";
73
74 } else {
75 using namespace KActivities;
76 Info info(id);
77
78 out
79 << (
80 info.id() == controller->currentActivity() ? "[CURRENT] " :
81 info.state() == Info::Running ? "[RUNNING] " :
82 info.state() == Info::Stopped ? "[STOPPED] " :
83 info.state() == Info::Starting ? "[STARTING]" :
84 info.state() == Info::Stopping ? "[STOPPING]" :
85 "unknown "
86 )
87 << info.id()
88 << " "
89 << info.name()
90 << " ("
91 << info.icon()
92 << ")\n"
93 ;
94
95 if (info.id() == controller->currentActivity()
96 && info.state() != Info::Running) {
97 qWarning()
98 << "Activity is the current one, but its state is"
99 << (
100 info.state() == Info::Running ? "running" :
101 info.state() == Info::Stopped ? "stopped" :
102 info.state() == Info::Starting ? "starting" :
103 info.state() == Info::Stopping ? "stopping" :
104 "unknown "
105 );
106 }
107 }
108 // clang-format on
109}
110
111template<typename T>
112T awaitFuture(const QFuture<T> &future)
113{
114 while (!future.isFinished()) {
116 }
117
118 return future.result();
119}
120
121void awaitFuture(const QFuture<void> &future)
122{
123 while (!future.isFinished()) {
125 }
126}
127
128void switchToActivity(const QString &id)
129{
130 auto result = awaitFuture(controller->setCurrentActivity(id));
131
132 if (!flags.bare) {
133 if (result) {
134 qDebug() << "Current activity is" << id;
135 } else {
136 qDebug() << "Failed to change the activity";
137 }
138 }
139}
140
141// clang-format off
142#define DEFINE_COMMAND(Command, MinArgCount) \
143 struct Command##_command { \
144 const StringListView &args; \
145 Command##_command(const StringListView &args) \
146 : args(args) \
147 { \
148 if (args.count() < MinArgCount + 1) { \
149 qFatal("not enough arguments for " #Command); \
150 } \
151 } \
152 \
153 int operator()(); \
154 }; \
155 \
156 int Command##_command::operator()()
157
158#endif
159// clang-format on
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
@ Starting
Activity is begin started.
Definition info.h:94
@ Running
Activity is running.
Definition info.h:93
@ Stopped
Activity is stopped.
Definition info.h:95
@ Stopping
Activity is begin started.
Definition info.h:96
Q_SCRIPTABLE Q_NOREPLY void start()
Namespace for everything in libkactivities.
KIOCORE_EXPORT QStringList list(const QString &fileClass)
const QList< QKeySequence > & end()
void processEvents(QEventLoop::ProcessEventsFlags flags)
bool isFinished() const const
T result() const const
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.