Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / libs / plasma / dataenginemanager.cpp
bloba9e4acbe33499dfab0eecc5243c46a45045597dd
1 /*
2 * Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "dataenginemanager.h"
21 #include "scripting/scriptengine.h"
23 #include <KDebug>
24 #include <KServiceTypeTrader>
26 namespace Plasma
29 class NullEngine : public DataEngine
31 public:
32 NullEngine(QObject* parent = 0)
33 : DataEngine(parent)
35 setObjectName(i18n("Null Engine"));
36 setValid(false);
38 // ref() ourselves to ensure we never get deleted
39 ref();
43 class DataEngineManager::Private
45 public:
46 Private()
47 : nullEng(0)
50 ~Private()
52 foreach (Plasma::DataEngine* engine, engines) {
53 delete engine;
55 engines.clear();
56 delete nullEng;
59 DataEngine* nullEngine()
61 if (!nullEng) {
62 nullEng = new NullEngine;
65 return nullEng;
68 DataEngine::Dict engines;
69 DataEngine* nullEng;
72 class DataEngineManagerSingleton
74 public:
75 DataEngineManager self;
78 K_GLOBAL_STATIC(DataEngineManagerSingleton, privateDataEngineManagerSelf)
80 DataEngineManager* DataEngineManager::self()
82 return &privateDataEngineManagerSelf->self;
85 DataEngineManager::DataEngineManager()
86 : d(new Private())
90 DataEngineManager::~DataEngineManager()
92 delete d;
95 Plasma::DataEngine* DataEngineManager::get(const QString& name) const
97 Plasma::DataEngine::Dict::const_iterator it = d->engines.find(name);
98 if (it != d->engines.end()) {
99 // ref and return the engine
100 //Plasma::DataEngine *engine = *it;
101 return *it;
104 return d->nullEngine();
107 Plasma::DataEngine* DataEngineManager::load(const QString& name)
109 Plasma::DataEngine* engine = 0;
110 Plasma::DataEngine::Dict::const_iterator it = d->engines.find(name);
112 if (it != d->engines.end()) {
113 engine = *it;
114 engine->ref();
115 return engine;
118 // load the engine, add it to the engines
119 QString constraint = QString("[X-Plasma-EngineName] == '%1'").arg(name);
120 KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine",
121 constraint);
122 QString error;
124 if (offers.isEmpty()) {
125 kDebug() << "offers are empty for " << name << " with constraint " << constraint;
126 } else {
127 QVariantList allArgs;
128 allArgs << offers.first()->storageId();
129 QString language = offers.first()->property("X-Plasma-Language").toString();
130 if (language.isEmpty()) {
131 engine = offers.first()->createInstance<Plasma::DataEngine>(0, allArgs, &error);
132 } else {
133 engine = new DataEngine(0, offers.first());
137 if (!engine) {
138 kDebug() << "Couldn't load engine \"" << name << "\". Error given: " << error;
139 return d->nullEngine();
142 d->engines[name] = engine;
143 return engine;
146 void DataEngineManager::unload(const QString& name)
148 Plasma::DataEngine::Dict::iterator it = d->engines.find(name);
150 if (it != d->engines.end()) {
151 Plasma::DataEngine* engine = *it;
152 engine->deref();
154 if (!engine->isUsed()) {
155 d->engines.erase(it);
156 delete engine;
161 QStringList DataEngineManager::knownEngines()
163 QStringList engines;
164 KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine");
165 foreach (KService::Ptr service, offers) {
166 engines.append(service->property("X-Plasma-EngineName").toString());
169 return engines;
172 } // namespace Plasma
174 #include "dataenginemanager.moc"