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 <QFile> 00023 #include <QTextStream> 00024 00025 #include <kdebug.h> 00026 #include <kglobal.h> 00027 #include <kstandarddirs.h> 00028 #include <kservicetypetrader.h> 00029 00030 #include "datacontainer.h" 00031 #include "pluginloader.h" 00032 #include "private/componentinstaller_p.h" 00033 #include "private/dataengine_p.h" 00034 #include "private/datacontainer_p.h" 00035 #include "scripting/scriptengine.h" 00036 00037 namespace Plasma 00038 { 00039 00040 class NullEngine : public DataEngine 00041 { 00042 public: 00043 NullEngine(QObject *parent = 0) 00044 : DataEngine(parent) 00045 { 00046 setValid(false); 00047 00048 // ref() ourselves to ensure we never get deleted 00049 d->ref(); 00050 } 00051 }; 00052 00053 class DataEngineManagerPrivate 00054 { 00055 public: 00056 DataEngineManagerPrivate() 00057 : nullEng(0) 00058 {} 00059 00060 ~DataEngineManagerPrivate() 00061 { 00062 foreach (Plasma::DataEngine *engine, engines) { 00063 delete engine; 00064 } 00065 engines.clear(); 00066 delete nullEng; 00067 } 00068 00069 DataEngine *nullEngine() 00070 { 00071 if (!nullEng) { 00072 nullEng = new NullEngine; 00073 } 00074 00075 return nullEng; 00076 } 00077 00078 DataEngine::Dict engines; 00079 DataEngine *nullEng; 00080 }; 00081 00082 class DataEngineManagerSingleton 00083 { 00084 public: 00085 DataEngineManager self; 00086 }; 00087 00088 K_GLOBAL_STATIC(DataEngineManagerSingleton, privateDataEngineManagerSelf) 00089 00090 DataEngineManager *DataEngineManager::self() 00091 { 00092 return &privateDataEngineManagerSelf->self; 00093 } 00094 00095 DataEngineManager::DataEngineManager() 00096 : d(new DataEngineManagerPrivate) 00097 { 00098 //startTimer(30000); 00099 } 00100 00101 DataEngineManager::~DataEngineManager() 00102 { 00103 delete d; 00104 } 00105 00106 Plasma::DataEngine *DataEngineManager::engine(const QString &name) const 00107 { 00108 if (name.isEmpty()) { 00109 return d->nullEngine(); 00110 } 00111 00112 Plasma::DataEngine::Dict::const_iterator it = d->engines.constFind(name); 00113 if (it != d->engines.constEnd()) { 00114 // ref and return the engine 00115 //Plasma::DataEngine *engine = *it; 00116 return *it; 00117 } 00118 00119 return d->nullEngine(); 00120 } 00121 00122 Plasma::DataEngine *DataEngineManager::loadEngine(const QString &name) 00123 { 00124 Plasma::DataEngine::Dict::const_iterator it = d->engines.constFind(name); 00125 00126 if (it != d->engines.constEnd()) { 00127 DataEngine *engine = *it; 00128 engine->d->ref(); 00129 return engine; 00130 } 00131 00132 DataEngine *engine = PluginLoader::pluginLoader()->loadDataEngine(name); 00133 if (!engine) { 00134 // Try installing the engine. However, it's too late for this request. 00135 ComponentInstaller::self()->installMissingComponent("dataengine", name); 00136 00137 return d->nullEngine(); 00138 } 00139 00140 engine->init(); 00141 d->engines[name] = engine; 00142 return engine; 00143 } 00144 00145 void DataEngineManager::unloadEngine(const QString &name) 00146 { 00147 Plasma::DataEngine::Dict::iterator it = d->engines.find(name); 00148 00149 if (it != d->engines.end()) { 00150 Plasma::DataEngine *engine = *it; 00151 engine->d->deref(); 00152 00153 if (!engine->d->isUsed()) { 00154 d->engines.erase(it); 00155 delete engine; 00156 } 00157 } 00158 } 00159 00160 QStringList DataEngineManager::listAllEngines(const QString &parentApp) 00161 { 00162 QString constraint; 00163 00164 if (parentApp.isEmpty()) { 00165 constraint.append("(not exist [X-KDE-ParentApp] or [X-KDE-ParentApp] == '')"); 00166 } else { 00167 constraint.append("[X-KDE-ParentApp] == '").append(parentApp).append("'"); 00168 } 00169 00170 KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine", constraint); 00171 00172 QStringList engines; 00173 foreach (const KService::Ptr &service, offers) { 00174 QString name = service->property("X-KDE-PluginInfo-Name").toString(); 00175 if (!name.isEmpty()) { 00176 engines.append(name); 00177 } 00178 } 00179 00180 return engines; 00181 } 00182 00183 KPluginInfo::List DataEngineManager::listEngineInfo(const QString &parentApp) 00184 { 00185 return PluginLoader::pluginLoader()->listDataEngineInfo(parentApp); 00186 } 00187 00188 KPluginInfo::List DataEngineManager::listEngineInfoByCategory(const QString &category, const QString &parentApp) 00189 { 00190 QString constraint = QString("[X-KDE-PluginInfo-Category] == '%1'").arg(category); 00191 00192 if (parentApp.isEmpty()) { 00193 constraint.append(" and not exist [X-KDE-ParentApp]"); 00194 } else { 00195 constraint.append(" and [X-KDE-ParentApp] == '").append(parentApp).append("'"); 00196 } 00197 00198 KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine", constraint); 00199 return KPluginInfo::fromServices(offers); 00200 } 00201 00202 void DataEngineManager::timerEvent(QTimerEvent *) 00203 { 00204 #ifndef NDEBUG 00205 QString path = KGlobal::dirs()->locateLocal("appdata", "plasma_dataenginemanager_log"); 00206 QFile f(path); 00207 if (!f.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) { 00208 kDebug() << "faild to open" << path; 00209 return; 00210 } 00211 00212 QTextStream out(&f); 00213 00214 QHashIterator<QString, DataEngine*> it(d->engines); 00215 out << "================================== " << KGlobal::locale()->formatDateTime(QDateTime::currentDateTime()) << endl; 00216 while (it.hasNext()) { 00217 it.next(); 00218 DataEngine *engine = it.value(); 00219 out << "DataEngine: " << it.key() << ' ' << engine << endl; 00220 out << " Claimed # of sources: " << engine->sources().count() << endl; 00221 out << " Actual # of sources: " << engine->containerDict().count() << endl; 00222 out << endl << " Source Details" << endl; 00223 00224 foreach (DataContainer *dc, engine->containerDict()) { 00225 out << " * " << dc->objectName() << endl; 00226 out << " Data count: " << dc->d->data.count() << endl; 00227 out << " Stored: " << dc->isStorageEnabled() << ' ' << endl; 00228 const int directs = dc->receivers(SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data))); 00229 if (directs > 0) { 00230 out << " Direction Connections: " << directs << ' ' << endl; 00231 } 00232 00233 const int relays = dc->d->relays.count(); 00234 if (relays > 0) { 00235 out << " Relays: " << dc->d->relays.count() << endl; 00236 QString times; 00237 foreach (SignalRelay *relay, dc->d->relays) { 00238 times.append(' ').append(QString::number(relay->m_interval)); 00239 } 00240 out << " Relay Timeouts: " << times << ' ' << endl; 00241 } 00242 } 00243 00244 out << endl << "-----" << endl; 00245 } 00246 out << endl << endl; 00247 #endif 00248 // killTimer(event->timerId()); 00249 } 00250 00251 } // namespace Plasma 00252 00253 #include "dataenginemanager.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:51:35 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:51:35 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.