22#include "private/authorizationmanager_p.h"
23#include "private/service_p.h"
24#include "private/serviceprovider_p.h"
26#include "config-plasma.h"
29#include <QGraphicsWidget>
34#include <kservicetypetrader.h>
35#include <ksharedconfig.h>
36#include <kstandarddirs.h>
37#include <dnssd/publicservice.h>
38#include <dnssd/servicebrowser.h>
42#include "private/configloader_p.h"
43#include "private/remoteservice_p.h"
44#include "private/remoteservicejob_p.h"
52 d(new ServicePrivate(this))
56Service::Service(
QObject *parent,
const QVariantList &args)
58 d(new ServicePrivate(this))
75Service *Service::load(
const QString &name,
const QVariantList &args,
QObject *parent)
82 return new RemoteService(parent, url);
85void ServicePrivate::jobFinished(
KJob *job)
87 emit q->finished(
static_cast<ServiceJob*
>(job));
90void ServicePrivate::associatedWidgetDestroyed(
QObject *obj)
92 associatedWidgets.remove(
static_cast<QWidget*
>(obj));
95void ServicePrivate::associatedGraphicsWidgetDestroyed(
QObject *obj)
97 associatedGraphicsWidgets.remove(
static_cast<QGraphicsObject*
>(obj));
100void ServicePrivate::publish(AnnouncementMethods methods,
const QString &name,
const PackageMetadata &metadata)
102#ifdef ENABLE_REMOTE_WIDGETS
103 if (!serviceProvider) {
104 AuthorizationManager::self()->d->prepareForServicePublication();
106 serviceProvider =
new ServiceProvider(name, q);
108 if (methods.testFlag(ZeroconfAnnouncement) &&
109 (DNSSD::ServiceBrowser::isAvailable() == DNSSD::ServiceBrowser::Working)) {
111 publicService =
new DNSSD::PublicService(name,
"_plasma._tcp", 4000);
113 QMap<QString, QByteArray> textData;
114 textData[
"name"] = name.toUtf8();
115 textData[
"plasmoidname"] = metadata.name().toUtf8();
116 textData[
"description"] = metadata.description().toUtf8();
117 textData[
"icon"] = metadata.icon().toUtf8();
118 publicService->setTextData(textData);
119 kDebug() <<
"about to publish";
121 publicService->publishAsync();
122 }
else if (methods.testFlag(ZeroconfAnnouncement) &&
123 (DNSSD::ServiceBrowser::isAvailable() != DNSSD::ServiceBrowser::Working)) {
124 kDebug() <<
"sorry, but your zeroconf daemon doesn't seem to be running.";
127 kDebug() <<
"already published!";
130 kWarning() <<
"libplasma is compiled without support for remote widgets. not publishing.";
134void ServicePrivate::unpublish()
136 delete serviceProvider;
139 delete publicService;
143bool ServicePrivate::isPublished()
const
145 if (serviceProvider) {
152KConfigGroup ServicePrivate::dummyGroup()
155 dummyConfig =
new KConfig(QString(), KConfig::SimpleConfig);
158 return KConfigGroup(dummyConfig,
"DummyGroup");
161void Service::setDestination(
const QString &destination)
166QString Service::destination()
const
168 return d->destination;
171QStringList Service::operationNames()
const
174 kDebug() <<
"No valid operations scheme has been registered";
175 return QStringList();
178 return d->config->groupList();
181KConfigGroup Service::operationDescription(
const QString &operationName)
184 kDebug() <<
"No valid operations scheme has been registered";
185 return d->dummyGroup();
188 d->config->writeConfig();
189 KConfigGroup params(d->config->config(), operationName);
196QMap<QString, QVariant> Service::parametersFromDescription(
const KConfigGroup &description)
198 QMap<QString, QVariant> params;
200 if (!d->config || !description.isValid()) {
204 const QString op = description.name();
205 foreach (
const QString &key, description.keyList()) {
206 KConfigSkeletonItem *item = d->config->findItem(op, key);
208 params.insert(key, description.readEntry(key, item->property()));
219 const QString op = description.isValid() ? description.name() : QString();
221 RemoteService *rs = qobject_cast<RemoteService *>(
this);
222 if (!op.isEmpty() && rs && !rs->isReady()) {
224 kDebug() <<
"Remote service is not ready; queueing operation";
225 QMap<QString, QVariant> params;
227 RemoteServiceJob *rsj = qobject_cast<RemoteServiceJob *>(job);
229 rsj->setDelayedDescription(description);
231 }
else if (!d->config) {
232 kDebug() <<
"No valid operations scheme has been registered";
233 }
else if (!op.isEmpty() && d->config->hasGroup(op)) {
234 if (d->disabledOperations.contains(op)) {
235 kDebug() <<
"Operation" << op <<
"is disabled";
241 kDebug() << op <<
"is not a valid group; valid groups are:" << d->config->groupList();
248 job->setParent(parent ? parent :
this);
250 QTimer::singleShot(0, job, SLOT(autoStart()));
254void Service::associateWidget(
QWidget *widget,
const QString &operation)
261 d->associatedWidgets.insert(widget, operation);
262 connect(widget, SIGNAL(destroyed(
QObject*)),
this, SLOT(associatedWidgetDestroyed(
QObject*)));
264 widget->setEnabled(!d->disabledOperations.contains(operation));
267void Service::disassociateWidget(
QWidget *widget)
273 disconnect(widget, SIGNAL(destroyed(
QObject*)),
274 this, SLOT(associatedWidgetDestroyed(
QObject*)));
275 d->associatedWidgets.remove(widget);
288void Service::associateItem(QGraphicsObject *widget,
const QString &operation)
295 d->associatedGraphicsWidgets.insert(widget, operation);
296 connect(widget, SIGNAL(destroyed(
QObject*)),
297 this, SLOT(associatedGraphicsWidgetDestroyed(
QObject*)));
299 widget->setEnabled(!d->disabledOperations.contains(operation));
302void Service::disassociateItem(QGraphicsObject *widget)
308 disconnect(widget, SIGNAL(destroyed(
QObject*)),
309 this, SLOT(associatedGraphicsWidgetDestroyed(
QObject*)));
310 d->associatedGraphicsWidgets.remove(widget);
313QString Service::name()
const
318void Service::setName(
const QString &name)
326 delete d->dummyConfig;
334void Service::setOperationEnabled(
const QString &operation,
bool enable)
336 if (!d->config || !d->config->hasGroup(operation)) {
341 d->disabledOperations.remove(operation);
343 d->disabledOperations.insert(operation);
347 QHashIterator<QWidget *, QString> it(d->associatedWidgets);
348 while (it.hasNext()) {
350 if (it.value() == operation) {
351 it.key()->setEnabled(enable);
357 QHashIterator<QGraphicsObject *, QString> it(d->associatedGraphicsWidgets);
358 while (it.hasNext()) {
360 if (it.value() == operation) {
361 it.key()->setEnabled(enable);
367bool Service::isOperationEnabled(
const QString &operation)
const
369 return d->config && d->config->hasGroup(operation) && !d->disabledOperations.contains(operation);
372void Service::setOperationsScheme(QIODevice *xml)
376 delete d->dummyConfig;
379 KSharedConfigPtr c = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig);
381 d->config->d->setWriteDefaults(
true);
386 QHashIterator<QWidget *, QString> it(d->associatedWidgets);
387 while (it.hasNext()) {
389 it.key()->setEnabled(d->config->hasGroup(it.value()));
394 QHashIterator<QGraphicsObject *, QString> it(d->associatedGraphicsWidgets);
395 while (it.hasNext()) {
397 it.key()->setEnabled(d->config->hasGroup(it.value()));
402void Service::registerOperationsScheme()
409 if (d->name.isEmpty()) {
410 kDebug() <<
"No name found";
414 const QString path = KStandardDirs::locate(
"data",
"plasma/services/" + d->name +
".operations");
416 if (path.isEmpty()) {
417 kDebug() <<
"Cannot find operations description:" << d->name <<
".operations";
427#include "service.moc"
Service * loadService(const QString &name, const QVariantList &args, QObject *parent=0)
Load a Service plugin.
static PluginLoader * pluginLoader()
Return the active plugin loader.
This class provides jobs for use with Plasma::Service.
This class provides a generic API for write access to settings or services.
void setOperationsScheme(QIODevice *xml)
Sets the XML used to define the operation schema for this Service.
Q_INVOKABLE void disassociateItem(QGraphicsObject *widget)
Disassociates a graphics item if it has been associated with an operation on this service.
void operationsChanged()
Emitted when the Service's operations change.
static Service * load(const QString &name, const QVariantList &args, QObject *parent=0)
Used to load a given service from a plugin.
void serviceReady(Plasma::Service *service)
Emitted when this service is ready for use.
Q_INVOKABLE void associateItem(QGraphicsObject *item, const QString &operation)
Associates a graphics item with an operation, which allows the service to automatically manage,...
virtual void registerOperationsScheme()
By default this is based on the file in plasma/services/name.operations, but can be reimplented to us...
virtual ServiceJob * createJob(const QString &operation, QMap< QString, QVariant > ¶meters)=0
Called when a job should be created by the Service.
Q_INVOKABLE QMap< QString, QVariant > parametersFromDescription(const KConfigGroup &description)
Q_INVOKABLE void disassociateWidget(QWidget *widget)
Disassociates a widget if it has been associated with an operation on this service.
void finished(Plasma::ServiceJob *job)
Emitted when a job associated with this Service completes its task.
Namespace for everything in libplasma.