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"
23
#include <KServiceTypeTrader>
28
class NullEngine : public DataEngine
31
NullEngine(QObject* parent = 0)
34
setObjectName(i18n("Null Engine"));
37
// ref() ourselves to ensure we never get deleted
42
class DataEngineManager::Private
51
foreach (Plasma::DataEngine* engine, engines) {
58
DataEngine* nullEngine()
61
nullEng = new NullEngine;
67
DataEngine::Dict engines;
71
class DataEngineManagerSingleton
74
DataEngineManager self;
77
K_GLOBAL_STATIC(DataEngineManagerSingleton, privateDataEngineManagerSelf)
79
DataEngineManager* DataEngineManager::self()
81
return &privateDataEngineManagerSelf->self;
84
DataEngineManager::DataEngineManager()
89
DataEngineManager::~DataEngineManager()
94
Plasma::DataEngine* DataEngineManager::dataEngine(const QString& name) const
96
Plasma::DataEngine::Dict::const_iterator it = d->engines.find(name);
97
if (it != d->engines.end()) {
98
// ref and return the engine
99
//Plasma::DataEngine *engine = *it;
103
return d->nullEngine();
106
Plasma::DataEngine* DataEngineManager::loadDataEngine(const QString& name)
108
Plasma::DataEngine* engine = 0;
109
Plasma::DataEngine::Dict::const_iterator it = d->engines.find(name);
111
if (it != d->engines.end()) {
117
// load the engine, add it to the engines
118
QString constraint = QString("[X-EngineName] == '%1'").arg(name);
119
KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine",
123
if (offers.isEmpty()) {
124
kDebug() << "offers are empty for " << name << " with constraint " << constraint;
126
engine = KService::createInstance<Plasma::DataEngine>(offers.first(), 0, QVariantList(), &error);
130
kDebug() << "Couldn't load engine \"" << name << "\". Error given: " << error;
131
return d->nullEngine();
135
engine->setObjectName(offers.first()->name());
136
engine->setIcon(offers.first()->icon());
137
d->engines[name] = engine;
141
void DataEngineManager::unloadDataEngine(const QString& name)
143
Plasma::DataEngine::Dict::iterator it = d->engines.find(name);
145
if (it != d->engines.end()) {
146
Plasma::DataEngine* engine = *it;
149
if (!engine->isUsed()) {
150
d->engines.erase(it);
156
QStringList DataEngineManager::knownEngines()
159
KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine");
160
foreach (KService::Ptr service, offers) {
161
engines.append(service->property("X-EngineName").toString());
167
} // namespace Plasma
169
#include "dataenginemanager.moc"