• Skip to content
  • Skip to link menu
KDE 4.0 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 namespace Plasma
00026 {
00027 
00028 class NullEngine : public DataEngine
00029 {
00030     public:
00031         NullEngine(QObject* parent = 0)
00032             : DataEngine(parent)
00033         {
00034             setObjectName(i18n("Null Engine"));
00035             setValid(false);
00036 
00037             // ref() ourselves to ensure we never get deleted
00038             ref();
00039         }
00040 };
00041 
00042 class DataEngineManager::Private
00043 {
00044     public:
00045         Private()
00046             : nullEng(0)
00047         {}
00048 
00049         ~Private()
00050         {
00051             foreach (Plasma::DataEngine* engine, engines) {
00052                 delete engine;
00053             }
00054             engines.clear();
00055             delete nullEng;
00056         }
00057 
00058         DataEngine* nullEngine()
00059         {
00060             if (!nullEng) {
00061                 nullEng = new NullEngine;
00062             }
00063 
00064             return nullEng;
00065         }
00066 
00067         DataEngine::Dict engines;
00068         DataEngine* nullEng;
00069 };
00070 
00071 class DataEngineManagerSingleton
00072 {
00073     public:
00074         DataEngineManager self;
00075 };
00076 
00077 K_GLOBAL_STATIC(DataEngineManagerSingleton, privateDataEngineManagerSelf)
00078 
00079 DataEngineManager* DataEngineManager::self()
00080 {
00081     return &privateDataEngineManagerSelf->self;
00082 }
00083 
00084 DataEngineManager::DataEngineManager()
00085     : d(new Private())
00086 {
00087 }
00088 
00089 DataEngineManager::~DataEngineManager()
00090 {
00091     delete d;
00092 }
00093 
00094 Plasma::DataEngine* DataEngineManager::dataEngine(const QString& name) const
00095 {
00096     Plasma::DataEngine::Dict::const_iterator it = d->engines.find(name);
00097     if (it != d->engines.end()) {
00098         // ref and return the engine
00099         //Plasma::DataEngine *engine = *it;
00100         return *it;
00101     }
00102 
00103     return d->nullEngine();
00104 }
00105 
00106 Plasma::DataEngine* DataEngineManager::loadDataEngine(const QString& name)
00107 {
00108     Plasma::DataEngine* engine = 0;
00109     Plasma::DataEngine::Dict::const_iterator it = d->engines.find(name);
00110 
00111     if (it != d->engines.end()) {
00112         engine = *it;
00113         engine->ref();
00114         return engine;
00115     }
00116 
00117     // load the engine, add it to the engines
00118     QString constraint = QString("[X-Plasma-EngineName] == '%1'").arg(name);
00119     KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine",
00120                                                               constraint);
00121     QString error;
00122 
00123     if (offers.isEmpty()) {
00124         kDebug() << "offers are empty for " << name << " with constraint " << constraint;
00125     } else {
00126         engine = offers.first()->createInstance<Plasma::DataEngine>(0, QVariantList(), &error);
00127     }
00128 
00129     if (!engine) {
00130         kDebug() << "Couldn't load engine \"" << name << "\". Error given: " << error;
00131         return d->nullEngine();
00132     }
00133 
00134     engine->ref();
00135     engine->setObjectName(offers.first()->name());
00136     engine->setIcon(offers.first()->icon());
00137     d->engines[name] = engine;
00138     return engine;
00139 }
00140 
00141 void DataEngineManager::unloadDataEngine(const QString& name)
00142 {
00143     Plasma::DataEngine::Dict::iterator it = d->engines.find(name);
00144 
00145     if (it != d->engines.end()) {
00146         Plasma::DataEngine* engine = *it;
00147         engine->deref();
00148 
00149         if (!engine->isUsed()) {
00150             d->engines.erase(it);
00151             delete engine;
00152         }
00153     }
00154 }
00155 
00156 QStringList DataEngineManager::knownEngines()
00157 {
00158     QStringList engines;
00159     KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine");
00160     foreach (KService::Ptr service, offers) {
00161         engines.append(service->property("X-Plasma-EngineName").toString());
00162     }
00163 
00164     return engines;
00165 }
00166 
00167 } // namespace Plasma
00168 
00169 #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
  • 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