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>
|
|
|
|
|
|
|
|
#include "configxml.h"
|
|
|
|
|
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
|
|
|
{
|
2008-07-16 19:04:40 +02:00
|
|
|
registerOperationsScheme();
|
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-07-29 00:51:19 +02:00
|
|
|
// remove those first item since those are managed by Service and subclasses shouldn't
|
|
|
|
// need to worry about it. yes, it violates the constness of this var, but it lets us add
|
|
|
|
// or remove items later while applets can just pretend that their args always start at 0
|
|
|
|
QVariantList &mutableArgs = const_cast<QVariantList&>(args);
|
|
|
|
if (!mutableArgs.isEmpty()) {
|
|
|
|
setName(mutableArgs[0].toString());
|
|
|
|
mutableArgs.removeFirst();
|
|
|
|
}
|
|
|
|
|
2008-07-16 19:04:40 +02:00
|
|
|
registerOperationsScheme();
|
2008-05-18 06:27:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Service::~Service()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
Service* Service::load(const QString &name, QObject *parent)
|
|
|
|
{
|
|
|
|
//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-07-29 00:51:19 +02:00
|
|
|
args << name;
|
2008-07-17 18:26:29 +02:00
|
|
|
Service* service = 0;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-09-18 06:40:28 +02:00
|
|
|
//kDebug() << "operation" << operationName << "requested, has keys" << params.keyList() << "from"
|
|
|
|
// << d->config->config()->name();
|
2008-05-18 06:27:54 +02:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2008-09-30 18:03:11 +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-09-04 01:55:06 +02:00
|
|
|
} else {
|
|
|
|
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-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)
|
|
|
|
{
|
|
|
|
disconnect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(associatedWidgetDestroyed(QObject*)));
|
|
|
|
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);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
disconnect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(associatedGraphicsWidgetDestroyed(QObject*)));
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (!d->config->hasGroup(operation)) {
|
|
|
|
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-08-25 12:27:53 +02:00
|
|
|
return 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-08-04 10:00:47 +02:00
|
|
|
d->config = new ConfigXml(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"
|
|
|
|
|