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"
|
2009-09-02 04:27:16 +02:00
|
|
|
#include "private/remoteservice_p.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
#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
|
|
|
|
|
|
|
#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>
|
|
|
|
#include <ktemporaryfile.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-09-02 04:27:16 +02:00
|
|
|
//#include "widgets/widget.h.template"
|
|
|
|
//#include "packagemetadata.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Service *Service::load(const QString &name, QObject *parent)
|
|
|
|
{
|
|
|
|
//TODO: scripting API support
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
return new NullService(QString(), parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(name);
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/Service", constraint);
|
|
|
|
|
|
|
|
if (offers.isEmpty()) {
|
|
|
|
kDebug() << "offers is empty for " << name;
|
|
|
|
return new NullService(name, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
KService::Ptr offer = offers.first();
|
|
|
|
QString error;
|
|
|
|
QVariantList args;
|
|
|
|
//args << name;
|
|
|
|
Service *service = 0;
|
|
|
|
|
|
|
|
if (Plasma::isPluginVersionCompatible(KPluginLoader(*offer).pluginVersion())) {
|
|
|
|
service = offer->createInstance<Plasma::Service>(parent, args, &error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!service) {
|
|
|
|
kDebug() << "Couldn't load Service \"" << name << "\"! reason given: " << error;
|
|
|
|
return new NullService(name, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (service->name().isEmpty()) {
|
|
|
|
service->setName(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return service;
|
|
|
|
}
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
Service *Service::access(const KUrl &url, QObject *parent)
|
|
|
|
{
|
|
|
|
return new RemoteService(parent, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServicePrivate::jobFinished(KJob *job)
|
|
|
|
{
|
|
|
|
emit q->finished(static_cast<ServiceJob*>(job));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServicePrivate::associatedWidgetDestroyed(QObject *obj)
|
|
|
|
{
|
|
|
|
associatedWidgets.remove(static_cast<QWidget*>(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServicePrivate::associatedGraphicsWidgetDestroyed(QObject *obj)
|
|
|
|
{
|
|
|
|
associatedGraphicsWidgets.remove(static_cast<QGraphicsWidget*>(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServicePrivate::publish(AnnouncementMethods methods, const QString &name, PackageMetadata metadata)
|
|
|
|
{
|
|
|
|
if (!serviceProvider) {
|
|
|
|
serviceProvider = new ServiceProvider(name, q);
|
|
|
|
|
|
|
|
if (methods.testFlag(ZeroconfAnnouncement) &&
|
|
|
|
(DNSSD::ServiceBrowser::isAvailable() == DNSSD::ServiceBrowser::Working)) {
|
|
|
|
//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();
|
|
|
|
textData["description"] = metadata.description().toUtf8();
|
|
|
|
publicService->setTextData(textData);
|
|
|
|
kDebug() << "about to publish";
|
|
|
|
|
|
|
|
publicService->publishAsync();
|
|
|
|
} else if (methods.testFlag(ZeroconfAnnouncement) &&
|
|
|
|
(DNSSD::ServiceBrowser::isAvailable() != DNSSD::ServiceBrowser::Working)) {
|
|
|
|
kDebug() << "sorry, but your zeroconf daemon doesn't seem to be running.";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kDebug() << "already published!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServicePrivate::unpublish()
|
|
|
|
{
|
|
|
|
if (serviceProvider) {
|
|
|
|
delete serviceProvider;
|
|
|
|
serviceProvider = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (publicService) {
|
|
|
|
delete publicService;
|
|
|
|
publicService = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ServicePrivate::isPublished() const
|
|
|
|
{
|
|
|
|
if (serviceProvider) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KConfigGroup ServicePrivate::dummyGroup()
|
|
|
|
{
|
|
|
|
if (!dummyConfig) {
|
|
|
|
if (!tempFile) {
|
|
|
|
tempFile = new KTemporaryFile;
|
|
|
|
tempFile->open();
|
|
|
|
}
|
|
|
|
|
|
|
|
dummyConfig = new KConfig(tempFile->fileName());
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
kDebug() << "No valid operations scheme has been registered";
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
|
|
|
return d->config->groupList();
|
|
|
|
}
|
|
|
|
|
|
|
|
KConfigGroup Service::operationDescription(const QString &operationName)
|
|
|
|
{
|
|
|
|
if (!d->config) {
|
|
|
|
kDebug() << "No valid operations scheme has been registered";
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceJob *Service::startOperationCall(const KConfigGroup &description, QObject *parent)
|
|
|
|
{
|
|
|
|
// TODO: nested groups?
|
|
|
|
ServiceJob *job = 0;
|
2008-12-01 01:28:50 +01:00
|
|
|
QString op = description.isValid() ? description.name() : QString();
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
if (!d->config) {
|
|
|
|
kDebug() << "No valid operations scheme has been registered";
|
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)) {
|
|
|
|
kDebug() << "Operation" << op << "is disabled";
|
|
|
|
} else {
|
|
|
|
QMap<QString, QVariant> params;
|
|
|
|
foreach (const QString &key, description.keyList()) {
|
|
|
|
KConfigSkeletonItem *item = d->config->findItem(op, key);
|
|
|
|
if (item) {
|
|
|
|
params.insert(key, description.readEntry(key, item->property()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
job = createJob(description.name(), params);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kDebug() << "Not a valid group!";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!job) {
|
|
|
|
job = new NullServiceJob(destination(), op, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
job->setParent(parent ? parent : this);
|
|
|
|
connect(job, SIGNAL(finished(KJob*)), this, SLOT(jobFinished(KJob*)));
|
|
|
|
QTimer::singleShot(0, job, SLOT(slotStart()));
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::associateWidget(QWidget *widget, const QString &operation)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
disconnect(widget, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(associatedWidgetDestroyed(QObject*)));
|
|
|
|
d->associatedWidgets.remove(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::associateWidget(QGraphicsWidget *widget, const QString &operation)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
delete d->tempFile;
|
|
|
|
d->tempFile = 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;
|
|
|
|
delete d->tempFile;
|
|
|
|
|
2009-08-02 20:24:20 +02:00
|
|
|
delete d->dummyConfig;
|
|
|
|
d->dummyConfig = 0;
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
//FIXME: make KSharedConfig and KConfigSkeleton not braindamaged in 4.2 and then get rid of the
|
|
|
|
// temp file object here
|
|
|
|
d->tempFile = new KTemporaryFile;
|
|
|
|
d->tempFile->open();
|
|
|
|
|
|
|
|
KSharedConfigPtr c = KSharedConfig::openConfig(d->tempFile->fileName(), KConfig::NoGlobals);
|
|
|
|
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()) {
|
|
|
|
kDebug() << "No name found";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString path = KStandardDirs::locate("data", "plasma/services/" + d->name + ".operations");
|
|
|
|
|
|
|
|
if (path.isEmpty()) {
|
2009-01-04 19:12:27 +01:00
|
|
|
kDebug() << "Cannot find operations description:" << d->name << ".operations";
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(path);
|
|
|
|
setOperationsScheme(&file);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "service.moc"
|
|
|
|
|