libplasma
service.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00051
00052
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
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
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
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
00177
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
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 }
00204
00205 #include "service.moc"
00206