Plasma

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

KDE's Doxygen guidelines are available online.