2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "service.h"
|
|
|
|
#include "private/service_p.h"
|
2009-09-02 04:27:16 +02:00
|
|
|
#include "private/serviceprovider_p.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-09-09 17:37:03 +02:00
|
|
|
#include "config-plasma.h"
|
2009-09-09 15:40:16 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QFile>
|
|
|
|
#include <QTimer>
|
|
|
|
|
2008-11-04 03:55:37 +01:00
|
|
|
#include <kdebug.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kservice.h>
|
|
|
|
#include <kservicetypetrader.h>
|
|
|
|
#include <ksharedconfig.h>
|
|
|
|
#include <kstandarddirs.h>
|
2009-09-02 04:27:16 +02:00
|
|
|
#include <dnssd/publicservice.h>
|
|
|
|
#include <dnssd/servicebrowser.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include "configloader.h"
|
|
|
|
#include "version.h"
|
2008-11-21 02:26:43 +01:00
|
|
|
#include "private/configloader_p.h"
|
2009-10-20 21:15:32 +02:00
|
|
|
#include "private/remoteservice_p.h"
|
|
|
|
#include "private/remoteservicejob_p.h"
|
2010-07-15 23:06:21 +02:00
|
|
|
#include "pluginloader.h"
|
2011-04-29 15:18:35 +02:00
|
|
|
#include "remote/authorizationmanager_p.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
Service::Service(QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new ServicePrivate(this))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Service::Service(QObject *parent, const QVariantList &args)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new ServicePrivate(this))
|
|
|
|
{
|
|
|
|
Q_UNUSED(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
Service::~Service()
|
|
|
|
{
|
2009-09-02 04:27:16 +02:00
|
|
|
d->unpublish();
|
2008-11-04 00:08:39 +01:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
Service *Service::access(const KUrl &url, QObject *parent)
|
|
|
|
{
|
|
|
|
return new RemoteService(parent, url);
|
|
|
|
}
|
|
|
|
|
2010-05-26 18:02:18 +02:00
|
|
|
void ServicePrivate::associatedWidgetDestroyed(QObject *obj)
|
|
|
|
{
|
|
|
|
associatedWidgets.remove(static_cast<QWidget*>(obj));
|
|
|
|
}
|
2009-09-02 04:27:16 +02:00
|
|
|
|
2010-05-26 18:02:18 +02:00
|
|
|
void ServicePrivate::associatedGraphicsWidgetDestroyed(QObject *obj)
|
|
|
|
{
|
|
|
|
associatedGraphicsWidgets.remove(static_cast<QGraphicsWidget*>(obj));
|
|
|
|
}
|
2009-09-02 04:27:16 +02:00
|
|
|
|
2011-05-31 01:01:18 +02:00
|
|
|
void ServicePrivate::publish(AnnouncementMethods methods, const QString &name, const KPluginInfo &metadata)
|
2010-05-26 18:02:18 +02:00
|
|
|
{
|
|
|
|
#ifdef ENABLE_REMOTE_WIDGETS
|
|
|
|
if (!serviceProvider) {
|
|
|
|
AuthorizationManager::self()->d->prepareForServicePublication();
|
2009-09-03 01:49:58 +02:00
|
|
|
|
2010-05-26 18:02:18 +02:00
|
|
|
serviceProvider = new ServiceProvider(name, q);
|
2009-09-02 04:27:16 +02:00
|
|
|
|
2010-05-26 18:02:18 +02:00
|
|
|
if (methods.testFlag(ZeroconfAnnouncement) &&
|
2010-10-04 23:48:06 +02:00
|
|
|
(DNSSD::ServiceBrowser::isAvailable() == DNSSD::ServiceBrowser::Working)) {
|
2010-05-26 18:02:18 +02:00
|
|
|
//TODO: dynamically pick a free port number.
|
|
|
|
publicService = new DNSSD::PublicService(name, "_plasma._tcp", 4000);
|
|
|
|
|
|
|
|
QMap<QString, QByteArray> textData;
|
|
|
|
textData["name"] = name.toUtf8();
|
|
|
|
textData["plasmoidname"] = metadata.name().toUtf8();
|
2011-05-31 01:01:18 +02:00
|
|
|
textData["description"] = metadata.comment().toUtf8();
|
2011-04-28 23:48:55 +02:00
|
|
|
textData["icon"] = metadata.icon().toUtf8();
|
2010-05-26 18:02:18 +02:00
|
|
|
publicService->setTextData(textData);
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2010-05-26 18:02:18 +02:00
|
|
|
kDebug() << "about to publish";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2010-05-26 18:02:18 +02:00
|
|
|
|
|
|
|
publicService->publishAsync();
|
|
|
|
} else if (methods.testFlag(ZeroconfAnnouncement) &&
|
|
|
|
(DNSSD::ServiceBrowser::isAvailable() != DNSSD::ServiceBrowser::Working)) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2010-05-26 18:02:18 +02:00
|
|
|
kDebug() << "sorry, but your zeroconf daemon doesn't seem to be running.";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
}
|
2010-05-26 18:02:18 +02:00
|
|
|
} else {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2010-05-26 18:02:18 +02:00
|
|
|
kDebug() << "already published!";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
}
|
2010-05-26 18:02:18 +02:00
|
|
|
#else
|
|
|
|
kWarning() << "libplasma is compiled without support for remote widgets. not publishing.";
|
|
|
|
#endif
|
|
|
|
}
|
2009-09-02 04:27:16 +02:00
|
|
|
|
2010-05-26 18:02:18 +02:00
|
|
|
void ServicePrivate::unpublish()
|
|
|
|
{
|
|
|
|
delete serviceProvider;
|
|
|
|
serviceProvider = 0;
|
2009-09-02 04:27:16 +02:00
|
|
|
|
2010-05-26 18:02:18 +02:00
|
|
|
delete publicService;
|
|
|
|
publicService = 0;
|
|
|
|
}
|
2009-09-02 04:27:16 +02:00
|
|
|
|
2010-05-26 18:02:18 +02:00
|
|
|
bool ServicePrivate::isPublished() const
|
|
|
|
{
|
|
|
|
if (serviceProvider) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-09-02 04:27:16 +02:00
|
|
|
|
2010-05-26 18:02:18 +02:00
|
|
|
KConfigGroup ServicePrivate::dummyGroup()
|
|
|
|
{
|
|
|
|
if (!dummyConfig) {
|
2010-06-04 19:28:07 +02:00
|
|
|
dummyConfig = new KConfig(QString(), KConfig::SimpleConfig);
|
2009-09-02 04:27:16 +02:00
|
|
|
}
|
|
|
|
|
2010-05-26 18:02:18 +02:00
|
|
|
return KConfigGroup(dummyConfig, "DummyGroup");
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void Service::setDestination(const QString &destination)
|
|
|
|
{
|
|
|
|
d->destination = destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Service::destination() const
|
|
|
|
{
|
|
|
|
return d->destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList Service::operationNames() const
|
|
|
|
{
|
|
|
|
if (!d->config) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2008-11-04 00:08:39 +01:00
|
|
|
kDebug() << "No valid operations scheme has been registered";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
|
|
|
return d->config->groupList();
|
|
|
|
}
|
|
|
|
|
|
|
|
KConfigGroup Service::operationDescription(const QString &operationName)
|
|
|
|
{
|
|
|
|
if (!d->config) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2008-11-04 00:08:39 +01:00
|
|
|
kDebug() << "No valid operations scheme has been registered";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-08-02 20:24:20 +02:00
|
|
|
return d->dummyGroup();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d->config->writeConfig();
|
|
|
|
KConfigGroup params(d->config->config(), operationName);
|
|
|
|
//kDebug() << "operation" << operationName
|
|
|
|
// << "requested, has keys" << params.keyList() << "from"
|
|
|
|
// << d->config->config()->name();
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2011-05-26 19:01:44 +02:00
|
|
|
QHash<QString, QVariant> Service::parametersFromDescription(const KConfigGroup &description)
|
2009-10-20 21:15:32 +02:00
|
|
|
{
|
2011-05-26 19:01:44 +02:00
|
|
|
QHash<QString, QVariant> params;
|
2009-10-20 21:15:32 +02:00
|
|
|
|
|
|
|
if (!d->config || !description.isValid()) {
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString op = description.name();
|
|
|
|
foreach (const QString &key, description.keyList()) {
|
|
|
|
KConfigSkeletonItem *item = d->config->findItem(op, key);
|
|
|
|
if (item) {
|
|
|
|
params.insert(key, description.readEntry(key, item->property()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
ServiceJob *Service::startOperationCall(const KConfigGroup &description, QObject *parent)
|
|
|
|
{
|
|
|
|
// TODO: nested groups?
|
|
|
|
ServiceJob *job = 0;
|
2009-10-20 21:15:32 +02:00
|
|
|
const QString op = description.isValid() ? description.name() : QString();
|
|
|
|
|
|
|
|
RemoteService *rs = qobject_cast<RemoteService *>(this);
|
|
|
|
if (!op.isEmpty() && rs && !rs->isReady()) {
|
|
|
|
// if we have an operation, but a non-ready remote service, just let it through
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-10-20 21:15:32 +02:00
|
|
|
kDebug() << "Remote service is not ready; queueing operation";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2011-05-26 19:01:44 +02:00
|
|
|
QHash<QString, QVariant> params;
|
2009-10-20 21:15:32 +02:00
|
|
|
job = createJob(op, params);
|
|
|
|
RemoteServiceJob *rsj = qobject_cast<RemoteServiceJob *>(job);
|
|
|
|
if (rsj) {
|
|
|
|
rsj->setDelayedDescription(description);
|
|
|
|
}
|
|
|
|
} else if (!d->config) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2008-11-04 00:08:39 +01:00
|
|
|
kDebug() << "No valid operations scheme has been registered";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-12-01 01:28:50 +01:00
|
|
|
} else if (!op.isEmpty() && d->config->hasGroup(op)) {
|
2008-11-04 00:08:39 +01:00
|
|
|
if (d->disabledOperations.contains(op)) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2008-11-04 00:08:39 +01:00
|
|
|
kDebug() << "Operation" << op << "is disabled";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2011-05-26 19:01:44 +02:00
|
|
|
QHash<QString, QVariant> params = parametersFromDescription(description);
|
2009-10-20 21:15:32 +02:00
|
|
|
job = createJob(op, params);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
} else {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2010-11-02 23:05:48 +01:00
|
|
|
kDebug() << "Not a valid group!"<<d->config->groupList();
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!job) {
|
2011-07-19 20:00:25 +02:00
|
|
|
job = new NullServiceJob(d->destination, op, this);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
job->setParent(parent ? parent : this);
|
2011-04-28 18:01:16 +02:00
|
|
|
QTimer::singleShot(0, job, SLOT(autoStart()));
|
2008-11-04 00:08:39 +01:00
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::associateWidget(QWidget *widget, const QString &operation)
|
|
|
|
{
|
2011-06-03 12:53:32 +02:00
|
|
|
if (!widget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
disassociateWidget(widget);
|
|
|
|
d->associatedWidgets.insert(widget, operation);
|
|
|
|
connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(associatedWidgetDestroyed(QObject*)));
|
|
|
|
|
|
|
|
widget->setEnabled(!d->disabledOperations.contains(operation));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::disassociateWidget(QWidget *widget)
|
|
|
|
{
|
2011-06-03 12:53:32 +02:00
|
|
|
if (!widget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
disconnect(widget, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(associatedWidgetDestroyed(QObject*)));
|
|
|
|
d->associatedWidgets.remove(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::associateWidget(QGraphicsWidget *widget, const QString &operation)
|
|
|
|
{
|
2011-06-03 12:56:11 +02:00
|
|
|
if (!widget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
disassociateWidget(widget);
|
|
|
|
d->associatedGraphicsWidgets.insert(widget, operation);
|
|
|
|
connect(widget, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(associatedGraphicsWidgetDestroyed(QObject*)));
|
|
|
|
|
|
|
|
widget->setEnabled(!d->disabledOperations.contains(operation));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::disassociateWidget(QGraphicsWidget *widget)
|
|
|
|
{
|
2011-06-03 12:56:11 +02:00
|
|
|
if (!widget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
disconnect(widget, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(associatedGraphicsWidgetDestroyed(QObject*)));
|
|
|
|
d->associatedGraphicsWidgets.remove(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Service::name() const
|
|
|
|
{
|
|
|
|
return d->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::setName(const QString &name)
|
|
|
|
{
|
|
|
|
d->name = name;
|
|
|
|
|
|
|
|
// now reset the config, which may be based on our name
|
|
|
|
delete d->config;
|
|
|
|
d->config = 0;
|
|
|
|
|
2009-08-02 20:24:20 +02:00
|
|
|
delete d->dummyConfig;
|
|
|
|
d->dummyConfig = 0;
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
registerOperationsScheme();
|
2009-09-02 04:27:16 +02:00
|
|
|
|
|
|
|
emit serviceReady(this);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Service::setOperationEnabled(const QString &operation, bool enable)
|
|
|
|
{
|
|
|
|
if (!d->config || !d->config->hasGroup(operation)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
d->disabledOperations.remove(operation);
|
2008-12-11 03:47:11 +01:00
|
|
|
} else {
|
2008-11-04 00:08:39 +01:00
|
|
|
d->disabledOperations.insert(operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QHashIterator<QWidget *, QString> it(d->associatedWidgets);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (it.value() == operation) {
|
|
|
|
it.key()->setEnabled(enable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QHashIterator<QGraphicsWidget *, QString> it(d->associatedGraphicsWidgets);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (it.value() == operation) {
|
|
|
|
it.key()->setEnabled(enable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Service::isOperationEnabled(const QString &operation) const
|
|
|
|
{
|
|
|
|
return d->config && d->config->hasGroup(operation) && !d->disabledOperations.contains(operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::setOperationsScheme(QIODevice *xml)
|
|
|
|
{
|
|
|
|
delete d->config;
|
|
|
|
|
2009-08-02 20:24:20 +02:00
|
|
|
delete d->dummyConfig;
|
|
|
|
d->dummyConfig = 0;
|
|
|
|
|
2010-06-04 19:28:07 +02:00
|
|
|
KSharedConfigPtr c = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig);
|
2008-11-04 00:08:39 +01:00
|
|
|
d->config = new ConfigLoader(c, xml, this);
|
2008-11-21 02:26:43 +01:00
|
|
|
d->config->d->setWriteDefaults(true);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
emit operationsChanged();
|
|
|
|
|
|
|
|
{
|
|
|
|
QHashIterator<QWidget *, QString> it(d->associatedWidgets);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
it.key()->setEnabled(d->config->hasGroup(it.value()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QHashIterator<QGraphicsWidget *, QString> it(d->associatedGraphicsWidgets);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
it.key()->setEnabled(d->config->hasGroup(it.value()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::registerOperationsScheme()
|
|
|
|
{
|
|
|
|
if (d->config) {
|
|
|
|
// we've already done our job. let's go home.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->name.isEmpty()) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2008-11-04 00:08:39 +01:00
|
|
|
kDebug() << "No name found";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-06 05:42:26 +02:00
|
|
|
const QString path = KStandardDirs::locate("data", "plasma/services/" + d->name + ".operations");
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
if (path.isEmpty()) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-01-04 19:12:27 +01:00
|
|
|
kDebug() << "Cannot find operations description:" << d->name << ".operations";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(path);
|
|
|
|
setOperationsScheme(&file);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "service.moc"
|
|
|
|
|