Plasma
dataenginemanager.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 "dataenginemanager.h"
00021
00022 #include <kdebug.h>
00023 #include <kservicetypetrader.h>
00024
00025 #include "private/dataengine_p.h"
00026 #include "scripting/scriptengine.h"
00027
00028 namespace Plasma
00029 {
00030
00031 class NullEngine : public DataEngine
00032 {
00033 public:
00034 NullEngine(QObject *parent = 0)
00035 : DataEngine(parent)
00036 {
00037 setValid(false);
00038
00039
00040 d->ref();
00041 }
00042 };
00043
00044 class DataEngineManagerPrivate
00045 {
00046 public:
00047 DataEngineManagerPrivate()
00048 : nullEng(0)
00049 {}
00050
00051 ~DataEngineManagerPrivate()
00052 {
00053 foreach (Plasma::DataEngine *engine, engines) {
00054 delete engine;
00055 }
00056 engines.clear();
00057 delete nullEng;
00058 }
00059
00060 DataEngine *nullEngine()
00061 {
00062 if (!nullEng) {
00063 nullEng = new NullEngine;
00064 }
00065
00066 return nullEng;
00067 }
00068
00069 DataEngine::Dict engines;
00070 DataEngine *nullEng;
00071 };
00072
00073 class DataEngineManagerSingleton
00074 {
00075 public:
00076 DataEngineManager self;
00077 };
00078
00079 K_GLOBAL_STATIC(DataEngineManagerSingleton, privateDataEngineManagerSelf)
00080
00081 DataEngineManager *DataEngineManager::self()
00082 {
00083 return &privateDataEngineManagerSelf->self;
00084 }
00085
00086 DataEngineManager::DataEngineManager()
00087 : d(new DataEngineManagerPrivate)
00088 {
00089 }
00090
00091 DataEngineManager::~DataEngineManager()
00092 {
00093 delete d;
00094 }
00095
00096 Plasma::DataEngine *DataEngineManager::engine(const QString &name) const
00097 {
00098 Plasma::DataEngine::Dict::const_iterator it = d->engines.constFind(name);
00099 if (it != d->engines.constEnd()) {
00100
00101
00102 return *it;
00103 }
00104
00105 return d->nullEngine();
00106 }
00107
00108 Plasma::DataEngine *DataEngineManager::loadEngine(const QString &name)
00109 {
00110 Plasma::DataEngine *engine = 0;
00111 Plasma::DataEngine::Dict::const_iterator it = d->engines.constFind(name);
00112
00113 if (it != d->engines.constEnd()) {
00114 engine = *it;
00115 engine->d->ref();
00116 return engine;
00117 }
00118
00119
00120 QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(name);
00121 KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine",
00122 constraint);
00123 QString error;
00124
00125 if (offers.isEmpty()) {
00126 kDebug() << "offers are empty for " << name << " with constraint " << constraint;
00127 } else {
00128 QVariantList allArgs;
00129 allArgs << offers.first()->storageId();
00130 QString api = offers.first()->property("X-Plasma-API").toString();
00131 if (api.isEmpty()) {
00132 if (offers.first()) {
00133 KPluginLoader plugin(*offers.first());
00134 if (Plasma::isPluginVersionCompatible(plugin.pluginVersion())) {
00135 engine = offers.first()->createInstance<Plasma::DataEngine>(0, allArgs, &error);
00136 }
00137 }
00138 } else {
00139 engine = new DataEngine(0, offers.first());
00140 }
00141 }
00142
00143 if (!engine) {
00144 kDebug() << "Couldn't load engine \"" << name << "\". Error given: " << error;
00145 return d->nullEngine();
00146 }
00147
00148 engine->init();
00149 d->engines[name] = engine;
00150 return engine;
00151 }
00152
00153 void DataEngineManager::unloadEngine(const QString &name)
00154 {
00155 Plasma::DataEngine::Dict::iterator it = d->engines.find(name);
00156
00157 if (it != d->engines.end()) {
00158 Plasma::DataEngine *engine = *it;
00159 engine->d->deref();
00160
00161 if (!engine->d->isUsed()) {
00162 d->engines.erase(it);
00163 delete engine;
00164 }
00165 }
00166 }
00167
00168 QStringList DataEngineManager::listAllEngines(const QString &parentApp)
00169 {
00170 QString constraint;
00171
00172 if (parentApp.isEmpty()) {
00173 constraint.append("not exist [X-KDE-ParentApp]");
00174 } else {
00175 constraint.append("[X-KDE-ParentApp] == '").append(parentApp).append("'");
00176 }
00177
00178 KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine", constraint);
00179
00180 QStringList engines;
00181 foreach (const KService::Ptr &service, offers) {
00182 QString name = service->property("X-KDE-PluginInfo-Name").toString();
00183 if (!name.isEmpty()) {
00184 engines.append(name);
00185 }
00186 }
00187
00188 return engines;
00189 }
00190
00191 KPluginInfo::List DataEngineManager::listEngineInfo(const QString &parentApp)
00192 {
00193 QString constraint;
00194
00195 if (parentApp.isEmpty()) {
00196 constraint.append("not exist [X-KDE-ParentApp]");
00197 } else {
00198 constraint.append("[X-KDE-ParentApp] == '").append(parentApp).append("'");
00199 }
00200
00201 KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine", constraint);
00202 return KPluginInfo::fromServices(offers);
00203 }
00204
00205 }
00206
00207 #include "dataenginemanager.moc"