• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libplasma

dataenginemanager.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "dataenginemanager.h"
00021 
00022 #include <KDebug>
00023 #include <KServiceTypeTrader>
00024 
00025 #include "dataengine_p.h"
00026 #include "scripting/scriptengine.h"
00027 
00028 namespace Plasma
00029 {
00030 
00031 class NullEngine : public DataEngine
00032 {
00033     public:
00034         NullEngine(QObject* parent = 0)
00035             : DataEngine(parent)
00036         {
00037             setObjectName(i18n("Null Engine"));
00038             setValid(false);
00039 
00040             // ref() ourselves to ensure we never get deleted
00041             d->ref();
00042         }
00043 };
00044 
00045 class DataEngineManagerPrivate
00046 {
00047     public:
00048         DataEngineManagerPrivate()
00049             : nullEng(0)
00050         {}
00051 
00052         ~DataEngineManagerPrivate()
00053         {
00054             foreach (Plasma::DataEngine* engine, engines) {
00055                 delete engine;
00056             }
00057             engines.clear();
00058             delete nullEng;
00059         }
00060 
00061         DataEngine* nullEngine()
00062         {
00063             if (!nullEng) {
00064                 nullEng = new NullEngine;
00065             }
00066 
00067             return nullEng;
00068         }
00069 
00070         DataEngine::Dict engines;
00071         DataEngine* nullEng;
00072 };
00073 
00074 class DataEngineManagerSingleton
00075 {
00076     public:
00077         DataEngineManager self;
00078 };
00079 
00080 K_GLOBAL_STATIC(DataEngineManagerSingleton, privateDataEngineManagerSelf)
00081 
00082 DataEngineManager* DataEngineManager::self()
00083 {
00084     return &privateDataEngineManagerSelf->self;
00085 }
00086 
00087 DataEngineManager::DataEngineManager()
00088     : d(new DataEngineManagerPrivate())
00089 {
00090 }
00091 
00092 DataEngineManager::~DataEngineManager()
00093 {
00094     delete d;
00095 }
00096 
00097 Plasma::DataEngine* DataEngineManager::engine(const QString& name) const
00098 {
00099     Plasma::DataEngine::Dict::const_iterator it = d->engines.find(name);
00100     if (it != d->engines.end()) {
00101         // ref and return the engine
00102         //Plasma::DataEngine *engine = *it;
00103         return *it;
00104     }
00105 
00106     return d->nullEngine();
00107 }
00108 
00109 Plasma::DataEngine* DataEngineManager::loadEngine(const QString& name)
00110 {
00111     Plasma::DataEngine* engine = 0;
00112     Plasma::DataEngine::Dict::const_iterator it = d->engines.find(name);
00113 
00114     if (it != d->engines.end()) {
00115         engine = *it;
00116         engine->d->ref();
00117         return engine;
00118     }
00119 
00120     // load the engine, add it to the engines
00121     QString constraint = QString("[X-Plasma-EngineName] == '%1'").arg(name);
00122     KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine",
00123                                                               constraint);
00124     QString error;
00125 
00126     if (offers.isEmpty()) {
00127         kDebug() << "offers are empty for " << name << " with constraint " << constraint;
00128     } else {
00129         QVariantList allArgs;
00130         allArgs << offers.first()->storageId();
00131         QString api = offers.first()->property("X-Plasma-API").toString();
00132         if (api.isEmpty()) {
00133             if (offers.first()) {
00134                 KPluginLoader plugin(*offers.first());
00135                 if (Plasma::isPluginVersionCompatible(plugin.pluginVersion()))
00136                     engine = offers.first()->createInstance<Plasma::DataEngine>(0, allArgs, &error);
00137             }
00138         } else {
00139             engine = new DataEngine(0, offers.first());
00140         }
00141     }
00142 
00143     if (!engine) {
00144         kDebug() << "Couldn't load engine \"" << name << "\". Error given: " << error;
00145         return d->nullEngine();
00146     }
00147 
00148     engine->init();
00149     d->engines[name] = engine;
00150     return engine;
00151 }
00152 
00153 void DataEngineManager::unloadEngine(const QString& name)
00154 {
00155     Plasma::DataEngine::Dict::iterator it = d->engines.find(name);
00156 
00157     if (it != d->engines.end()) {
00158         Plasma::DataEngine* engine = *it;
00159         engine->d->deref();
00160 
00161         if (!engine->d->isUsed()) {
00162             d->engines.erase(it);
00163             delete engine;
00164         }
00165     }
00166 }
00167 
00168 QStringList DataEngineManager::listAllEngines()
00169 {
00170     QStringList engines;
00171     KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine");
00172     foreach (const KService::Ptr &service, offers) {
00173         engines.append(service->property("X-Plasma-EngineName").toString());
00174     }
00175 
00176     return engines;
00177 }
00178 
00179 } // namespace Plasma
00180 
00181 #include "dataenginemanager.moc"

libplasma

Skip menu "libplasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal