2008-05-18 06:27:54 +02: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"
|
2008-07-30 22:35:42 +02:00
|
|
|
#include "private/service_p.h"
|
2008-05-18 06:27:54 +02:00
|
|
|
|
|
|
|
#include <QFile>
|
2008-08-03 00:29:19 +02:00
|
|
|
#include <QTimer>
|
2008-05-18 06:27:54 +02:00
|
|
|
|
|
|
|
#include <KDebug>
|
|
|
|
#include <KService>
|
|
|
|
#include <KServiceTypeTrader>
|
|
|
|
#include <KSharedConfig>
|
|
|
|
#include <KStandardDirs>
|
|
|
|
#include <KTemporaryFile>
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
#include "configloader.h"
|
2008-05-18 06:27:54 +02:00
|
|
|
|
2008-07-17 18:26:29 +02:00
|
|
|
#include "version.h"
|
|
|
|
|
2008-05-18 06:27:54 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
Service::Service(QObject *parent)
|
|
|
|
: QObject(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new ServicePrivate(this))
|
2008-05-18 06:27:54 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Service::Service(QObject *parent, const QVariantList &args)
|
|
|
|
: QObject(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new ServicePrivate(this))
|
2008-05-18 06:27:54 +02:00
|
|
|
{
|
2008-10-12 02:05:39 +02:00
|
|
|
Q_UNUSED(args)
|
2008-05-18 06:27:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Service::~Service()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
Service *Service::load(const QString &name, QObject *parent)
|
2008-05-18 06:27:54 +02:00
|
|
|
{
|
|
|
|
//TODO: scripting API support
|
|
|
|
if (name.isEmpty()) {
|
2008-09-03 20:51:41 +02:00
|
|
|
return new NullService(QString(), parent);
|
2008-05-18 06:27:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2008-09-03 20:51:41 +02:00
|
|
|
return new NullService(name, parent);
|
2008-05-18 06:27:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
KService::Ptr offer = offers.first();
|
|
|
|
QString error;
|
|
|
|
QVariantList args;
|
2008-10-12 02:05:39 +02:00
|
|
|
//args << name;
|
2008-10-12 00:48:38 +02:00
|
|
|
Service *service = 0;
|
2008-07-17 18:26:29 +02:00
|
|
|
|
2008-07-29 00:51:19 +02:00
|
|
|
if (Plasma::isPluginVersionCompatible(KPluginLoader(*offer).pluginVersion())) {
|
2008-07-17 18:26:29 +02:00
|
|
|
service = offer->createInstance<Plasma::Service>(parent, args, &error);
|
2008-07-29 00:51:19 +02:00
|
|
|
}
|
2008-05-18 06:27:54 +02:00
|
|
|
|
|
|
|
if (!service) {
|
|
|
|
kDebug() << "Couldn't load Service \"" << name << "\"! reason given: " << error;
|
2008-09-03 20:51:41 +02:00
|
|
|
return new NullService(name, parent);
|
2008-05-18 06:27:54 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 02:05:39 +02:00
|
|
|
if (service->name().isEmpty()) {
|
|
|
|
service->setName(name);
|
|
|
|
}
|
|
|
|
|
2008-05-18 06:27:54 +02:00
|
|
|
return service;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::setDestination(const QString &destination)
|
|
|
|
{
|
|
|
|
d->destination = destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Service::destination() const
|
|
|
|
{
|
|
|
|
return d->destination;
|
|
|
|
}
|
|
|
|
|
2008-05-30 16:26:07 +02:00
|
|
|
QStringList Service::operationNames() const
|
2008-05-18 06:27:54 +02:00
|
|
|
{
|
|
|
|
if (!d->config) {
|
2008-07-19 02:03:08 +02:00
|
|
|
kDebug() << "No valid operations scheme has been registered";
|
2008-05-18 06:27:54 +02:00
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
2008-09-04 00:28:10 +02:00
|
|
|
return d->config->groupList();
|
2008-05-18 06:27:54 +02:00
|
|
|
}
|
|
|
|
|
2008-05-30 16:26:07 +02:00
|
|
|
KConfigGroup Service::operationDescription(const QString &operationName)
|
2008-05-18 06:27:54 +02:00
|
|
|
{
|
|
|
|
if (!d->config) {
|
2008-07-19 02:03:08 +02:00
|
|
|
kDebug() << "No valid operations scheme has been registered";
|
2008-05-18 06:27:54 +02:00
|
|
|
return KConfigGroup();
|
|
|
|
}
|
|
|
|
|
2008-09-04 01:33:57 +02:00
|
|
|
d->config->writeConfig();
|
2008-05-30 16:26:07 +02:00
|
|
|
KConfigGroup params(d->config->config(), operationName);
|
2008-10-12 00:48:38 +02:00
|
|
|
//kDebug() << "operation" << operationName
|
|
|
|
// << "requested, has keys" << params.keyList() << "from"
|
2008-09-18 06:40:28 +02:00
|
|
|
// << d->config->config()->name();
|
2008-05-18 06:27:54 +02:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
ServiceJob *Service::startOperationCall(const KConfigGroup &description, QObject *parent)
|
2008-05-18 06:27:54 +02:00
|
|
|
{
|
|
|
|
// TODO: nested groups?
|
2008-09-04 01:55:06 +02:00
|
|
|
ServiceJob *job = 0;
|
|
|
|
QString op = description.name();
|
2008-05-18 06:27:54 +02:00
|
|
|
if (!d->config) {
|
2008-07-19 02:03:08 +02:00
|
|
|
kDebug() << "No valid operations scheme has been registered";
|
2008-10-02 22:12:32 +02:00
|
|
|
} else if (d->config->hasGroup(op)) {
|
2008-09-04 01:55:06 +02: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) {
|
2008-09-04 22:13:23 +02:00
|
|
|
params.insert(key, description.readEntry(key, item->property()));
|
2008-09-04 01:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
job = createJob(description.name(), params);
|
|
|
|
}
|
2008-10-02 22:12:32 +02:00
|
|
|
} else {
|
|
|
|
kDebug() << "Not a valid group!";
|
2008-08-12 15:58:58 +02:00
|
|
|
}
|
|
|
|
|
2008-09-04 01:55:06 +02:00
|
|
|
if (!job) {
|
|
|
|
job = new NullServiceJob(destination(), op, this);
|
2008-05-18 06:27:54 +02:00
|
|
|
}
|
|
|
|
|
2008-09-30 18:03:11 +02:00
|
|
|
job->setParent(parent ? parent : this);
|
2008-05-18 06:27:54 +02:00
|
|
|
connect(job, SIGNAL(finished(KJob*)), this, SLOT(jobFinished(KJob*)));
|
2008-08-03 00:29:19 +02:00
|
|
|
QTimer::singleShot(0, job, SLOT(slotStart()));
|
2008-05-18 06:27:54 +02:00
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
2008-08-04 10:00:47 +02:00
|
|
|
void Service::associateWidget(QWidget *widget, const QString &operation)
|
|
|
|
{
|
2008-08-27 01:04:53 +02:00
|
|
|
disassociateWidget(widget);
|
2008-08-04 10:00:47 +02:00
|
|
|
d->associatedWidgets.insert(widget, operation);
|
|
|
|
connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(associatedWidgetDestroyed(QObject*)));
|
2008-08-12 15:58:58 +02:00
|
|
|
|
2008-08-27 01:04:53 +02:00
|
|
|
widget->setEnabled(!d->disabledOperations.contains(operation));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::disassociateWidget(QWidget *widget)
|
|
|
|
{
|
2008-10-12 00:48:38 +02:00
|
|
|
disconnect(widget, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(associatedWidgetDestroyed(QObject*)));
|
2008-08-27 01:04:53 +02:00
|
|
|
d->associatedWidgets.remove(widget);
|
2008-08-04 10:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Service::associateWidget(QGraphicsWidget *widget, const QString &operation)
|
|
|
|
{
|
2008-08-27 01:04:53 +02:00
|
|
|
disassociateWidget(widget);
|
2008-08-04 10:00:47 +02:00
|
|
|
d->associatedGraphicsWidgets.insert(widget, operation);
|
2008-10-12 00:48:38 +02:00
|
|
|
connect(widget, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(associatedGraphicsWidgetDestroyed(QObject*)));
|
2008-08-12 15:58:58 +02:00
|
|
|
|
2008-08-27 01:04:53 +02:00
|
|
|
widget->setEnabled(!d->disabledOperations.contains(operation));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::disassociateWidget(QGraphicsWidget *widget)
|
|
|
|
{
|
2008-10-12 00:48:38 +02:00
|
|
|
disconnect(widget, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(associatedGraphicsWidgetDestroyed(QObject*)));
|
2008-08-27 01:04:53 +02:00
|
|
|
d->associatedGraphicsWidgets.remove(widget);
|
2008-08-04 10:00:47 +02:00
|
|
|
}
|
|
|
|
|
2008-05-18 06:27:54 +02:00
|
|
|
QString Service::name() const
|
|
|
|
{
|
|
|
|
return d->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::setName(const QString &name)
|
|
|
|
{
|
|
|
|
d->name = name;
|
2008-07-29 00:51:19 +02:00
|
|
|
|
|
|
|
// now reset the config, which may be based on our name
|
|
|
|
delete d->config;
|
|
|
|
d->config = 0;
|
|
|
|
|
|
|
|
delete d->tempFile;
|
|
|
|
d->tempFile = 0;
|
|
|
|
|
|
|
|
registerOperationsScheme();
|
2008-05-18 06:27:54 +02:00
|
|
|
}
|
|
|
|
|
2008-08-12 15:58:58 +02:00
|
|
|
void Service::setOperationEnabled(const QString &operation, bool enable)
|
|
|
|
{
|
2008-10-12 01:53:52 +02:00
|
|
|
if (!d->config || !d->config->hasGroup(operation)) {
|
2008-08-12 15:58:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
d->disabledOperations.remove(operation);
|
|
|
|
} else if (!d->disabledOperations.contains(operation)) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-27 01:04:53 +02:00
|
|
|
bool Service::isOperationEnabled(const QString &operation) const
|
2008-08-12 15:58:58 +02:00
|
|
|
{
|
2008-10-12 01:53:52 +02:00
|
|
|
return d->config && d->config->hasGroup(operation) && !d->disabledOperations.contains(operation);
|
2008-08-12 15:58:58 +02:00
|
|
|
}
|
|
|
|
|
2008-05-18 06:27:54 +02:00
|
|
|
void Service::setOperationsScheme(QIODevice *xml)
|
|
|
|
{
|
|
|
|
delete d->config;
|
|
|
|
delete d->tempFile;
|
|
|
|
|
|
|
|
//FIXME: make KSharedConfig and KConfigSkeleton not braindamaged in 4.2 and then get rid of the
|
|
|
|
// temp file object here
|
|
|
|
d->tempFile = new KTemporaryFile;
|
2008-09-18 06:40:28 +02:00
|
|
|
d->tempFile->open();
|
|
|
|
|
2008-09-04 00:28:10 +02:00
|
|
|
KSharedConfigPtr c = KSharedConfig::openConfig(d->tempFile->fileName(), KConfig::NoGlobals);
|
2008-10-29 18:46:33 +01:00
|
|
|
d->config = new ConfigLoader(c, xml, this);
|
2008-09-18 06:40:28 +02:00
|
|
|
|
2008-08-04 10:00:47 +02: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()));
|
|
|
|
}
|
|
|
|
}
|
2008-05-18 06:27:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Service::registerOperationsScheme()
|
|
|
|
{
|
2008-07-29 00:51:19 +02:00
|
|
|
if (d->config) {
|
|
|
|
// we've already done our job. let's go home.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-18 06:27:54 +02:00
|
|
|
if (d->name.isEmpty()) {
|
2008-07-19 02:03:08 +02:00
|
|
|
kDebug() << "No name found";
|
2008-05-18 06:27:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString path = KStandardDirs::locate("data", "plasma/services/" + d->name + ".operations");
|
|
|
|
|
|
|
|
if (path.isEmpty()) {
|
2008-07-19 02:03:08 +02:00
|
|
|
kDebug() << "Cannot find operations description";
|
2008-05-18 06:27:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(path);
|
|
|
|
setOperationsScheme(&file);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "service.moc"
|
|
|
|
|