2007-05-23 09:51:28 +02:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
2007-05-23 09:51:28 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-08-06 13:20:02 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
2007-05-23 09:51:28 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dataenginemanager.h"
|
|
|
|
|
|
|
|
#include <KDebug>
|
|
|
|
#include <KServiceTypeTrader>
|
|
|
|
|
2008-07-30 22:35:42 +02:00
|
|
|
#include "private/dataengine_p.h"
|
2008-04-29 03:17:12 +02:00
|
|
|
#include "scripting/scriptengine.h"
|
|
|
|
|
2007-05-23 09:51:28 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-06-03 00:23:26 +02:00
|
|
|
class NullEngine : public DataEngine
|
|
|
|
{
|
|
|
|
public:
|
2008-10-12 00:48:38 +02:00
|
|
|
NullEngine(QObject *parent = 0)
|
2007-06-03 00:23:26 +02:00
|
|
|
: DataEngine(parent)
|
|
|
|
{
|
2007-07-11 05:08:57 +02:00
|
|
|
setObjectName(i18n("Null Engine"));
|
2007-06-03 00:23:26 +02:00
|
|
|
setValid(false);
|
2007-07-11 05:08:57 +02:00
|
|
|
|
|
|
|
// ref() ourselves to ensure we never get deleted
|
2008-04-29 03:17:12 +02:00
|
|
|
d->ref();
|
2007-06-03 00:23:26 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
class DataEngineManagerPrivate
|
2007-05-24 21:36:05 +02:00
|
|
|
{
|
|
|
|
public:
|
2008-07-01 20:56:43 +02:00
|
|
|
DataEngineManagerPrivate()
|
2007-06-07 22:57:18 +02:00
|
|
|
: nullEng(0)
|
2007-05-24 21:36:05 +02:00
|
|
|
{}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
~DataEngineManagerPrivate()
|
2007-06-03 00:23:26 +02:00
|
|
|
{
|
2008-10-12 00:48:38 +02:00
|
|
|
foreach (Plasma::DataEngine *engine, engines) {
|
2007-06-07 22:57:18 +02:00
|
|
|
delete engine;
|
|
|
|
}
|
2007-07-11 05:08:57 +02:00
|
|
|
engines.clear();
|
2007-06-07 22:57:18 +02:00
|
|
|
delete nullEng;
|
2007-06-03 00:23:26 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
DataEngine *nullEngine()
|
2007-06-03 00:23:26 +02:00
|
|
|
{
|
2007-06-07 22:57:18 +02:00
|
|
|
if (!nullEng) {
|
|
|
|
nullEng = new NullEngine;
|
2007-06-03 00:23:26 +02:00
|
|
|
}
|
2007-06-03 00:39:23 +02:00
|
|
|
|
2007-06-07 22:57:18 +02:00
|
|
|
return nullEng;
|
2007-06-03 00:23:26 +02:00
|
|
|
}
|
2007-05-24 21:36:05 +02:00
|
|
|
|
2007-07-11 05:08:57 +02:00
|
|
|
DataEngine::Dict engines;
|
2008-10-12 00:48:38 +02:00
|
|
|
DataEngine *nullEng;
|
2007-05-24 21:36:05 +02:00
|
|
|
};
|
|
|
|
|
2007-05-27 10:01:31 +02:00
|
|
|
class DataEngineManagerSingleton
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DataEngineManager self;
|
|
|
|
};
|
|
|
|
|
2007-07-21 17:58:44 +02:00
|
|
|
K_GLOBAL_STATIC(DataEngineManagerSingleton, privateDataEngineManagerSelf)
|
2007-05-27 10:01:31 +02:00
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
DataEngineManager *DataEngineManager::self()
|
2007-05-27 10:01:31 +02:00
|
|
|
{
|
2007-07-21 17:58:44 +02:00
|
|
|
return &privateDataEngineManagerSelf->self;
|
2007-05-27 10:01:31 +02:00
|
|
|
}
|
|
|
|
|
2007-05-23 09:51:28 +02:00
|
|
|
DataEngineManager::DataEngineManager()
|
2008-08-23 19:44:14 +02:00
|
|
|
: d(new DataEngineManagerPrivate)
|
2007-05-23 09:51:28 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEngineManager::~DataEngineManager()
|
|
|
|
{
|
2007-06-07 22:57:18 +02:00
|
|
|
delete d;
|
2007-05-23 09:51:28 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
Plasma::DataEngine *DataEngineManager::engine(const QString &name) const
|
2007-05-23 09:51:28 +02:00
|
|
|
{
|
2007-07-11 05:08:57 +02:00
|
|
|
Plasma::DataEngine::Dict::const_iterator it = d->engines.find(name);
|
|
|
|
if (it != d->engines.end()) {
|
2007-05-23 09:51:28 +02:00
|
|
|
// ref and return the engine
|
|
|
|
//Plasma::DataEngine *engine = *it;
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
2007-06-03 00:23:26 +02:00
|
|
|
return d->nullEngine();
|
2007-05-23 09:51:28 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
Plasma::DataEngine *DataEngineManager::loadEngine(const QString &name)
|
2007-05-23 09:51:28 +02:00
|
|
|
{
|
2008-10-12 00:48:38 +02:00
|
|
|
Plasma::DataEngine *engine = 0;
|
2007-07-11 05:08:57 +02:00
|
|
|
Plasma::DataEngine::Dict::const_iterator it = d->engines.find(name);
|
2007-05-23 09:51:28 +02:00
|
|
|
|
2007-07-11 05:08:57 +02:00
|
|
|
if (it != d->engines.end()) {
|
2007-06-03 00:57:55 +02:00
|
|
|
engine = *it;
|
2008-04-29 03:17:12 +02:00
|
|
|
engine->d->ref();
|
2007-05-23 09:51:28 +02:00
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load the engine, add it to the engines
|
2008-09-08 18:55:05 +02:00
|
|
|
QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(name);
|
2007-05-23 09:51:28 +02:00
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine",
|
|
|
|
constraint);
|
2007-08-29 05:06:48 +02:00
|
|
|
QString error;
|
2007-05-23 09:51:28 +02:00
|
|
|
|
|
|
|
if (offers.isEmpty()) {
|
2008-01-08 02:25:09 +01:00
|
|
|
kDebug() << "offers are empty for " << name << " with constraint " << constraint;
|
2007-07-11 05:08:57 +02:00
|
|
|
} else {
|
2008-02-25 02:19:07 +01:00
|
|
|
QVariantList allArgs;
|
|
|
|
allArgs << offers.first()->storageId();
|
2008-04-29 18:24:14 +02:00
|
|
|
QString api = offers.first()->property("X-Plasma-API").toString();
|
|
|
|
if (api.isEmpty()) {
|
2008-07-17 18:26:29 +02:00
|
|
|
if (offers.first()) {
|
|
|
|
KPluginLoader plugin(*offers.first());
|
2008-10-12 00:48:38 +02:00
|
|
|
if (Plasma::isPluginVersionCompatible(plugin.pluginVersion())) {
|
2008-07-17 18:26:29 +02:00
|
|
|
engine = offers.first()->createInstance<Plasma::DataEngine>(0, allArgs, &error);
|
2008-10-12 00:48:38 +02:00
|
|
|
}
|
2008-07-17 18:26:29 +02:00
|
|
|
}
|
2008-02-04 05:41:40 +01:00
|
|
|
} else {
|
2008-02-25 12:24:37 +01:00
|
|
|
engine = new DataEngine(0, offers.first());
|
2008-02-04 05:41:40 +01:00
|
|
|
}
|
2007-05-23 09:51:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!engine) {
|
2008-01-08 02:25:09 +01:00
|
|
|
kDebug() << "Couldn't load engine \"" << name << "\". Error given: " << error;
|
2007-07-11 05:08:57 +02:00
|
|
|
return d->nullEngine();
|
2007-05-23 09:51:28 +02:00
|
|
|
}
|
|
|
|
|
2008-03-06 06:18:36 +01:00
|
|
|
engine->init();
|
2007-07-11 05:08:57 +02:00
|
|
|
d->engines[name] = engine;
|
2007-05-23 09:51:28 +02:00
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
void DataEngineManager::unloadEngine(const QString &name)
|
2007-05-23 09:51:28 +02:00
|
|
|
{
|
2007-07-11 05:08:57 +02:00
|
|
|
Plasma::DataEngine::Dict::iterator it = d->engines.find(name);
|
2007-05-23 09:51:28 +02:00
|
|
|
|
2007-07-11 05:08:57 +02:00
|
|
|
if (it != d->engines.end()) {
|
2008-10-12 00:48:38 +02:00
|
|
|
Plasma::DataEngine *engine = *it;
|
2008-04-29 03:17:12 +02:00
|
|
|
engine->d->deref();
|
2007-05-23 09:51:28 +02:00
|
|
|
|
2008-04-29 03:17:12 +02:00
|
|
|
if (!engine->d->isUsed()) {
|
2007-07-11 05:08:57 +02:00
|
|
|
d->engines.erase(it);
|
2007-05-23 09:51:28 +02:00
|
|
|
delete engine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-23 21:37:42 +02:00
|
|
|
QStringList DataEngineManager::listAllEngines()
|
2007-05-23 09:51:28 +02:00
|
|
|
{
|
|
|
|
QStringList engines;
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine");
|
2008-04-26 18:19:00 +02:00
|
|
|
foreach (const KService::Ptr &service, offers) {
|
2008-09-08 18:55:05 +02:00
|
|
|
QString name = service->property("X-KDE-PluginInfo-Name").toString();
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
engines.append(name);
|
|
|
|
}
|
2007-05-23 09:51:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return engines;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
2007-07-23 01:20:50 +02:00
|
|
|
#include "dataenginemanager.moc"
|