• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libplasma

service.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2008 Aaron Seigo <aseigo@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "service.h"
00021 #include "service_p.h"
00022 
00023 #include <QFile>
00024 
00025 #include <KDebug>
00026 #include <KService>
00027 #include <KServiceTypeTrader>
00028 #include <KSharedConfig>
00029 #include <KStandardDirs>
00030 #include <KTemporaryFile>
00031 
00032 #include "configxml.h"
00033 
00034 #include "version.h"
00035 
00036 namespace Plasma
00037 {
00038 
00039 Service::Service(QObject *parent)
00040     : QObject(parent),
00041       d(new ServicePrivate(this))
00042 {
00043     registerOperationsScheme();
00044 }
00045 
00046 Service::Service(QObject *parent, const QVariantList &args)
00047     : QObject(parent),
00048       d(new ServicePrivate(this))
00049 {
00050     // remove those first item since those are managed by Service and subclasses shouldn't
00051     // need to worry about it. yes, it violates the constness of this var, but it lets us add
00052     // or remove items later while applets can just pretend that their args always start at 0
00053     QVariantList &mutableArgs = const_cast<QVariantList&>(args);
00054     if (!mutableArgs.isEmpty()) {
00055         setName(mutableArgs[0].toString());
00056         mutableArgs.removeFirst();
00057     }
00058 
00059     registerOperationsScheme();
00060 }
00061 
00062 Service::~Service()
00063 {
00064     delete d;
00065 }
00066 
00067 Service* Service::load(const QString &name, QObject *parent)
00068 {
00069     //TODO: scripting API support
00070     if (name.isEmpty()) {
00071         return new NullService(parent);
00072     }
00073 
00074     QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(name);
00075     KService::List offers = KServiceTypeTrader::self()->query("Plasma/Service", constraint);
00076 
00077     if (offers.isEmpty()) {
00078         kDebug() << "offers is empty for " << name;
00079         return new NullService(parent);
00080     }
00081 
00082     KService::Ptr offer = offers.first();
00083     QString error;
00084     QVariantList args;
00085     args << name;
00086     Service* service = 0;
00087 
00088     if (Plasma::isPluginVersionCompatible(KPluginLoader(*offer).pluginVersion())) {
00089         service = offer->createInstance<Plasma::Service>(parent, args, &error);
00090     }
00091 
00092     if (!service) {
00093         kDebug() << "Couldn't load Service \"" << name << "\"! reason given: " << error;
00094         return new NullService(parent);
00095     }
00096 
00097     return service;
00098 }
00099 
00100 void Service::setDestination(const QString &destination)
00101 {
00102     d->destination = destination;
00103 }
00104 
00105 QString Service::destination() const
00106 {
00107     return d->destination;
00108 }
00109 
00110 QStringList Service::operationNames() const
00111 {
00112     if (!d->config) {
00113         return QStringList();
00114     }
00115 
00116     return d->config->config()->groupList();
00117 }
00118 
00119 KConfigGroup Service::operationDescription(const QString &operationName)
00120 {
00121     if (!d->config) {
00122         return KConfigGroup();
00123     }
00124 
00125     KConfigGroup params(d->config->config(), operationName);
00126     return params;
00127 }
00128 
00129 ServiceJob* Service::startOperationCall(const KConfigGroup &description)
00130 {
00131     // TODO: nested groups?
00132     if (!d->config) {
00133         return new NullServiceJob(parent());
00134     }
00135 
00136     d->config->writeConfig();
00137     QMap<QString, QVariant> params;
00138     QString op = description.name();
00139     foreach (const QString &key, description.keyList()) {
00140         KConfigSkeletonItem *item = d->config->findItem(op, key);
00141         if (item) {
00142             params.insert(key, item->property());
00143         }
00144     }
00145 
00146     ServiceJob *job = createJob(description.name(), params);
00147     connect(job, SIGNAL(finished(KJob*)), this, SLOT(jobFinished(KJob*)));
00148     job->start();
00149     return job;
00150 }
00151 
00152 QString Service::name() const
00153 {
00154     return d->name;
00155 }
00156 
00157 void Service::setName(const QString &name)
00158 {
00159     d->name = name;
00160 
00161     // now reset the config, which may be based on our name
00162     delete d->config;
00163     d->config = 0;
00164 
00165     delete d->tempFile;
00166     d->tempFile = 0;
00167 
00168     registerOperationsScheme();
00169 }
00170 
00171 void Service::setOperationsScheme(QIODevice *xml)
00172 {
00173     delete d->config;
00174     delete d->tempFile;
00175 
00176     //FIXME: make KSharedConfig and KConfigSkeleton not braindamaged in 4.2 and then get rid of the
00177     //       temp file object here
00178     d->tempFile = new KTemporaryFile;
00179     d->config = new ConfigXml(KSharedConfig::openConfig(d->tempFile->fileName()), xml, this);
00180 }
00181 
00182 void Service::registerOperationsScheme()
00183 {
00184     if (d->config) {
00185         // we've already done our job. let's go home.
00186         return;
00187     }
00188 
00189     if (d->name.isEmpty()) {
00190         return;
00191     }
00192 
00193     QString path = KStandardDirs::locate("data", "plasma/services/" + d->name + ".operations");
00194 
00195     if (path.isEmpty()) {
00196         return;
00197     }
00198 
00199     QFile file(path);
00200     setOperationsScheme(&file);
00201 }
00202 
00203 } // namespace Plasma
00204 
00205 #include "service.moc"
00206 

libplasma

Skip menu "libplasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal