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

Plasma

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.h>
00023 #include <kservicetypetrader.h>
00024 
00025 #include "private/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             setValid(false);
00038 
00039             // ref() ourselves to ensure we never get deleted
00040             d->ref();
00041         }
00042 };
00043 
00044 class DataEngineManagerPrivate
00045 {
00046     public:
00047         DataEngineManagerPrivate()
00048             : nullEng(0)
00049         {}
00050 
00051         ~DataEngineManagerPrivate()
00052         {
00053             foreach (Plasma::DataEngine *engine, engines) {
00054                 delete engine;
00055             }
00056             engines.clear();
00057             delete nullEng;
00058         }
00059 
00060         DataEngine *nullEngine()
00061         {
00062             if (!nullEng) {
00063                 nullEng = new NullEngine;
00064             }
00065 
00066             return nullEng;
00067         }
00068 
00069         DataEngine::Dict engines;
00070         DataEngine *nullEng;
00071 };
00072 
00073 class DataEngineManagerSingleton
00074 {
00075     public:
00076         DataEngineManager self;
00077 };
00078 
00079 K_GLOBAL_STATIC(DataEngineManagerSingleton, privateDataEngineManagerSelf)
00080 
00081 DataEngineManager *DataEngineManager::self()
00082 {
00083     return &privateDataEngineManagerSelf->self;
00084 }
00085 
00086 DataEngineManager::DataEngineManager()
00087     : d(new DataEngineManagerPrivate)
00088 {
00089 }
00090 
00091 DataEngineManager::~DataEngineManager()
00092 {
00093     delete d;
00094 }
00095 
00096 Plasma::DataEngine *DataEngineManager::engine(const QString &name) const
00097 {
00098     Plasma::DataEngine::Dict::const_iterator it = d->engines.constFind(name);
00099     if (it != d->engines.constEnd()) {
00100         // ref and return the engine
00101         //Plasma::DataEngine *engine = *it;
00102         return *it;
00103     }
00104 
00105     return d->nullEngine();
00106 }
00107 
00108 Plasma::DataEngine *DataEngineManager::loadEngine(const QString &name)
00109 {
00110     Plasma::DataEngine *engine = 0;
00111     Plasma::DataEngine::Dict::const_iterator it = d->engines.constFind(name);
00112 
00113     if (it != d->engines.constEnd()) {
00114         engine = *it;
00115         engine->d->ref();
00116         return engine;
00117     }
00118 
00119     // load the engine, add it to the engines
00120     QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(name);
00121     KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine",
00122                                                               constraint);
00123     QString error;
00124 
00125     if (offers.isEmpty()) {
00126         kDebug() << "offers are empty for " << name << " with constraint " << constraint;
00127     } else {
00128         QVariantList allArgs;
00129         allArgs << offers.first()->storageId();
00130         QString api = offers.first()->property("X-Plasma-API").toString();
00131         if (api.isEmpty()) {
00132             if (offers.first()) {
00133                 KPluginLoader plugin(*offers.first());
00134                 if (Plasma::isPluginVersionCompatible(plugin.pluginVersion())) {
00135                     engine = offers.first()->createInstance<Plasma::DataEngine>(0, allArgs, &error);
00136                 }
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(const QString &parentApp)
00169 {
00170     QString constraint;
00171 
00172     if (parentApp.isEmpty()) {
00173         constraint.append("not exist [X-KDE-ParentApp]");
00174     } else {
00175         constraint.append("[X-KDE-ParentApp] == '").append(parentApp).append("'");
00176     }
00177 
00178     KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine", constraint);
00179 
00180     QStringList engines;
00181     foreach (const KService::Ptr &service, offers) {
00182         QString name = service->property("X-KDE-PluginInfo-Name").toString();
00183         if (!name.isEmpty()) {
00184             engines.append(name);
00185         }
00186     }
00187 
00188     return engines;
00189 }
00190 
00191 KPluginInfo::List DataEngineManager::listEngineInfo(const QString &parentApp)
00192 {
00193     QString constraint;
00194 
00195     if (parentApp.isEmpty()) {
00196         constraint.append("not exist [X-KDE-ParentApp]");
00197     } else {
00198         constraint.append("[X-KDE-ParentApp] == '").append(parentApp).append("'");
00199     }
00200 
00201     KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine", constraint);
00202     return KPluginInfo::fromServices(offers);
00203 }
00204 
00205 KPluginInfo::List DataEngineManager::listEngineInfoByCategory(const QString &category, const QString &parentApp)
00206 {
00207     QString constraint = QString("[X-KDE-PluginInfo-Category] == '%1'").arg(category);
00208 
00209     if (parentApp.isEmpty()) {
00210         constraint.append(" and not exist [X-KDE-ParentApp]");
00211     } else {
00212         constraint.append(" and [X-KDE-ParentApp] == '").append(parentApp).append("'");
00213     }
00214 
00215     KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine", constraint);
00216     return KPluginInfo::fromServices(offers);
00217 }
00218 
00219 } // namespace Plasma
00220 
00221 #include "dataenginemanager.moc"

Plasma

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
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