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