2007-05-23 09:51:28 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#include <KParts/ComponentFactory>
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-06-03 00:23:26 +02:00
|
|
|
class NullEngine : public DataEngine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NullEngine(QObject* parent = 0)
|
|
|
|
: DataEngine(parent)
|
|
|
|
{
|
|
|
|
setValid(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-05-27 10:01:31 +02:00
|
|
|
class DataEngineManager::Private
|
2007-05-24 21:36:05 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
2007-06-03 00:23:26 +02:00
|
|
|
: null(0)
|
2007-05-24 21:36:05 +02:00
|
|
|
{}
|
|
|
|
|
2007-06-03 00:23:26 +02:00
|
|
|
~Private()
|
|
|
|
{
|
|
|
|
delete null;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEngine* nullEngine()
|
|
|
|
{
|
|
|
|
if (!null) {
|
|
|
|
null = new NullEngine;
|
|
|
|
}
|
2007-06-03 00:39:23 +02:00
|
|
|
|
|
|
|
return null;
|
2007-06-03 00:23:26 +02:00
|
|
|
}
|
2007-05-24 21:36:05 +02:00
|
|
|
|
2007-06-03 00:23:26 +02:00
|
|
|
DataEngine::Dict m_engines;
|
|
|
|
DataEngine* null;
|
2007-05-24 21:36:05 +02:00
|
|
|
};
|
|
|
|
|
2007-05-27 10:01:31 +02:00
|
|
|
class DataEngineManagerSingleton
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DataEngineManager self;
|
|
|
|
};
|
|
|
|
|
|
|
|
K_GLOBAL_STATIC(DataEngineManagerSingleton, privateSelf)
|
|
|
|
|
|
|
|
DataEngineManager* DataEngineManager::self()
|
|
|
|
{
|
|
|
|
return &privateSelf->self;
|
|
|
|
}
|
|
|
|
|
2007-05-23 09:51:28 +02:00
|
|
|
DataEngineManager::DataEngineManager()
|
2007-05-24 21:36:05 +02:00
|
|
|
: d(new Private())
|
2007-05-23 09:51:28 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEngineManager::~DataEngineManager()
|
|
|
|
{
|
2007-05-24 21:36:05 +02:00
|
|
|
foreach (Plasma::DataEngine* engine, d->m_engines) {
|
2007-05-23 09:51:28 +02:00
|
|
|
delete engine;
|
|
|
|
}
|
2007-05-24 21:36:05 +02:00
|
|
|
d->m_engines.clear();
|
2007-05-23 09:51:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::DataEngine* DataEngineManager::dataEngine(const QString& name) const
|
|
|
|
{
|
2007-05-24 21:36:05 +02:00
|
|
|
Plasma::DataEngine::Dict::const_iterator it = d->m_engines.find(name);
|
|
|
|
if (it != d->m_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
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::DataEngine* DataEngineManager::loadDataEngine(const QString& name)
|
|
|
|
{
|
2007-06-03 00:57:55 +02:00
|
|
|
Plasma::DataEngine* engine = 0;
|
|
|
|
Plasma::DataEngine::Dict::const_iterator it = d->m_engines.find(name);
|
2007-05-23 09:51:28 +02:00
|
|
|
|
2007-06-03 00:57:55 +02:00
|
|
|
if (it != d->m_engines.end()) {
|
|
|
|
engine = *it;
|
2007-05-23 09:51:28 +02:00
|
|
|
engine->ref();
|
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load the engine, add it to the engines
|
|
|
|
QString constraint = QString("[X-EngineName] == '%1'").arg(name);
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine",
|
|
|
|
constraint);
|
|
|
|
|
|
|
|
if (offers.isEmpty()) {
|
|
|
|
kDebug() << "offers are empty for " << name << " with constraint " << constraint << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
engine = KService::createInstance<Plasma::DataEngine>(offers.first(), 0);
|
|
|
|
if (!engine) {
|
2007-05-24 21:57:31 +02:00
|
|
|
kDebug() << "Couldn't load engine \"" << name << "\"!" << endl;
|
2007-06-03 00:23:26 +02:00
|
|
|
engine = d->nullEngine();
|
2007-05-23 09:51:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
engine->ref();
|
|
|
|
engine->setObjectName(offers.first()->name());
|
|
|
|
engine->setIcon(offers.first()->icon());
|
2007-05-24 21:36:05 +02:00
|
|
|
d->m_engines[name] = engine;
|
2007-05-23 09:51:28 +02:00
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngineManager::unloadDataEngine(const QString& name)
|
|
|
|
{
|
|
|
|
Plasma::DataEngine* engine = dataEngine(name);
|
|
|
|
|
|
|
|
if (engine) {
|
|
|
|
engine->deref();
|
|
|
|
|
|
|
|
if (!engine->isUsed()) {
|
2007-05-24 21:36:05 +02:00
|
|
|
d->m_engines.remove(name);
|
2007-05-23 09:51:28 +02:00
|
|
|
delete engine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-27 10:01:31 +02:00
|
|
|
QStringList DataEngineManager::knownEngines()
|
2007-05-23 09:51:28 +02:00
|
|
|
{
|
|
|
|
QStringList engines;
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine");
|
|
|
|
foreach (KService::Ptr service, offers) {
|
|
|
|
engines.append(service->property("X-EngineName").toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return engines;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|