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"
24 #include <KServiceTypeTrader>
29 class NullEngine
: public DataEngine
32 NullEngine(QObject
* parent
= 0)
35 setObjectName(i18n("Null Engine"));
38 // ref() ourselves to ensure we never get deleted
43 class DataEngineManager::Private
52 foreach (Plasma::DataEngine
* engine
, engines
) {
59 DataEngine
* nullEngine()
62 nullEng
= new NullEngine
;
68 DataEngine::Dict engines
;
72 class DataEngineManagerSingleton
75 DataEngineManager self
;
78 K_GLOBAL_STATIC(DataEngineManagerSingleton
, privateDataEngineManagerSelf
)
80 DataEngineManager
* DataEngineManager::self()
82 return &privateDataEngineManagerSelf
->self
;
85 DataEngineManager::DataEngineManager()
90 DataEngineManager::~DataEngineManager()
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;
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()) {
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",
124 if (offers
.isEmpty()) {
125 kDebug() << "offers are empty for " << name
<< " with constraint " << constraint
;
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
);
133 engine
= new DataEngine(0, offers
.first());
138 kDebug() << "Couldn't load engine \"" << name
<< "\". Error given: " << error
;
139 return d
->nullEngine();
142 d
->engines
[name
] = 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
;
154 if (!engine
->isUsed()) {
155 d
->engines
.erase(it
);
161 QStringList
DataEngineManager::knownEngines()
164 KService::List offers
= KServiceTypeTrader::self()->query("Plasma/DataEngine");
165 foreach (KService::Ptr service
, offers
) {
166 engines
.append(service
->property("X-Plasma-EngineName").toString());
172 } // namespace Plasma
174 #include "dataenginemanager.moc"