Plasma5Support

service.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Aaron Seigo <aseigo@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "service.h"
8#include "private/service_p.h"
9
10#include "config-plasma5support.h"
11
12#include <QFile>
13#include <QTimer>
14
15#include <KConfigLoader>
16#include <KConfigSkeleton>
17#include <KSharedConfig>
18#include <QDebug>
19
20#include <QStandardPaths>
21
22#include "debug_p.h"
23#include "pluginloader.h"
24#include "version.h"
25
26namespace Plasma5Support
27{
29 : QObject(parent)
30 , d(new ServicePrivate(this))
31{
32}
33
35{
36 delete d;
37}
38
39void Service::setDestination(const QString &destination)
40{
41 d->destination = destination;
42}
43
44QString Service::destination() const
45{
46 return d->destination;
47}
48
49QStringList Service::operationNames() const
50{
51 if (d->operationsMap.isEmpty()) {
52#ifndef NDEBUG
53 // qCDebug(LOG_PLASMA5SUPPORT) << "No valid operations scheme has been registered";
54#endif
55 return QStringList();
56 }
57
58 return d->operationsMap.keys();
59}
60
61QVariantMap Service::operationDescription(const QString &operationName)
62{
63 if (d->operationsMap.isEmpty()) {
64#ifndef NDEBUG
65 // qCDebug(LOG_PLASMA5SUPPORT) << "No valid operations scheme has been registered";
66#endif
67 return QVariantMap();
68 }
69
70 // qCDebug(LOG_PLASMA5SUPPORT) << "operation" << operationName
71 // << "requested, has keys" << d->operationsMap.keys();
72 return d->operationsMap.value(operationName);
73}
74
75ServiceJob *Service::startOperationCall(const QVariantMap &description, QObject *parent)
76{
77 // TODO: nested groups?
78 ServiceJob *job = nullptr;
79 const QString op = !description.isEmpty() ? description.value(QStringLiteral("_name")).toString() : QString();
80
81 if (d->operationsMap.isEmpty()) {
82#ifndef NDEBUG
83 // qCDebug(LOG_PLASMA5SUPPORT) << "No valid operations scheme has been registered";
84#endif
85 } else if (!op.isEmpty() && d->operationsMap.contains(op)) {
86 if (d->disabledOperations.contains(op)) {
87#ifndef NDEBUG
88 // qCDebug(LOG_PLASMA5SUPPORT) << "Operation" << op << "is disabled";
89#endif
90 } else {
91 QVariantMap map = description;
92 job = createJob(op, map);
93 }
94 } else {
95#ifndef NDEBUG
96 // qCDebug(LOG_PLASMA5SUPPORT) << op << "is not a valid group; valid groups are:" << d->operationsMap.keys();
97#endif
98 }
99
100 if (!job) {
101 job = new NullServiceJob(d->destination, op, this);
102 }
103
104 job->setParent(parent ? parent : this);
105 QTimer::singleShot(0, job, SLOT(autoStart()));
106 return job;
107}
108
109QString Service::name() const
110{
111 return d->name;
112}
113
114void Service::setName(const QString &name)
115{
116 d->name = name;
117
118 // now reset the config, which may be based on our name
119 d->operationsMap.clear();
120
122
123 Q_EMIT serviceReady(this);
124}
125
126void Service::setOperationEnabled(const QString &operation, bool enable)
127{
128 if (d->operationsMap.isEmpty() || !d->operationsMap.contains(operation)) {
129 return;
130 }
131
132 if (enable) {
133 d->disabledOperations.remove(operation);
134 } else {
135 d->disabledOperations.insert(operation);
136 }
137
138 Q_EMIT operationEnabledChanged(operation, enable);
139}
140
141bool Service::isOperationEnabled(const QString &operation) const
142{
143 return d->operationsMap.contains(operation) && !d->disabledOperations.contains(operation);
144}
145
147{
148 d->operationsMap.clear();
149
150 // /dev/null is because I need to pass a filename argument to construct a
151 // KSharedConfig. We need a config object for the config loader even
152 // though we dont' actually want to use any config parts from it,
153 // we just want to share the KConfigLoader XML parsing.
154 KSharedConfigPtr config = KSharedConfig::openConfig(QStringLiteral("/dev/null"), KConfig::SimpleConfig);
155 KConfigLoader loader(config, xml);
156
157 const auto groupList = loader.groupList();
158 for (const QString &group : groupList) {
159 d->operationsMap[group][QStringLiteral("_name")] = group;
160 }
161 const auto itemsList = loader.items();
162 for (KConfigSkeletonItem *item : itemsList) {
163 d->operationsMap[item->group()][item->key()] = item->property();
164 }
165}
166
168{
169 if (!d->operationsMap.isEmpty()) {
170 // we've already done our job. let's go home.
171 return;
172 }
173
174 if (d->name.isEmpty()) {
175#ifndef NDEBUG
176 // qCDebug(LOG_PLASMA5SUPPORT) << "No name found";
177#endif
178 return;
179 }
180
181 const QString path =
183 QStringLiteral(PLASMA5SUPPORT_RELATIVE_DATA_INSTALL_DIR "/services/") + d->name + QStringLiteral(".operations"));
184
185 if (path.isEmpty()) {
186#ifndef NDEBUG
187 // qCDebug(LOG_PLASMA5SUPPORT) << "Cannot find operations description:" << d->name << ".operations";
188#endif
189 return;
190 }
191
192 QFile file(path);
193 setOperationsScheme(&file);
194}
195
196} // namespace Plasma5Support
197
198#include "moc_service.cpp"
QStringList groupList() const
KConfigSkeletonItem::List items() const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
This class provides jobs for use with Plasma5Support::Service.
Definition servicejob.h:39
Q_INVOKABLE void setDestination(const QString &destination)
Sets the destination for this Service to operate on.
Definition service.cpp:39
void setOperationEnabled(const QString &operation, bool enable)
Enables a given service by name.
Definition service.cpp:126
void setName(const QString &name)
Sets the name of the Service; useful for Services not loaded from plugins, which use the plugin name ...
Definition service.cpp:114
virtual ServiceJob * createJob(const QString &operation, QVariantMap &parameters)=0
Called when a job should be created by the Service.
Service(QObject *parent=nullptr)
Definition service.cpp:28
void operationEnabledChanged(const QString &operation, bool enabled)
Emitted when an operation got enabled or disabled.
Q_INVOKABLE bool isOperationEnabled(const QString &operation) const
Query to find if an operation is enabled or not.
Definition service.cpp:141
void setOperationsScheme(QIODevice *xml)
Sets the XML used to define the operation schema for this Service.
Definition service.cpp:146
~Service()
Destructor.
Definition service.cpp:34
Q_INVOKABLE QVariantMap operationDescription(const QString &operationName)
Retrieves the parameters for a given operation.
Definition service.cpp:61
virtual void registerOperationsScheme()
By default this is based on the file in plasma5support/services/name.operations, but can be reimpleme...
Definition service.cpp:167
void serviceReady(Plasma5Support::Service *service)
Emitted when this service is ready for use.
Q_INVOKABLE ServiceJob * startOperationCall(const QVariantMap &description, QObject *parent=nullptr)
Called to create a ServiceJob which is associated with a given operation and parameter set.
Definition service.cpp:75
Namespace for everything in libplasma.
Definition datamodel.cpp:15
Q_EMITQ_EMIT
QObject * parent() const const
void setParent(QObject *parent)
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
bool isEmpty() const const
<Plasma5Support/Version>
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.