• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.5 API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • plasma
dataenginemanager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Library General Public License as
6  * published by the Free Software Foundation; either version 2, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "dataenginemanager.h"
21 
22 #include <QFile>
23 #include <QTextStream>
24 
25 #include <kdebug.h>
26 #include <kglobal.h>
27 #include <kstandarddirs.h>
28 #include <kservicetypetrader.h>
29 
30 #include "datacontainer.h"
31 #include "pluginloader.h"
32 #include "private/dataengine_p.h"
33 #include "private/datacontainer_p.h"
34 #include "scripting/scriptengine.h"
35 
36 namespace Plasma
37 {
38 
39 class NullEngine : public DataEngine
40 {
41  public:
42  NullEngine(QObject *parent = 0)
43  : DataEngine(parent)
44  {
45  setValid(false);
46 
47  // ref() ourselves to ensure we never get deleted
48  d->ref();
49  }
50 };
51 
52 class DataEngineManagerPrivate
53 {
54  public:
55  DataEngineManagerPrivate()
56  : nullEng(0)
57  {}
58 
59  ~DataEngineManagerPrivate()
60  {
61  foreach (Plasma::DataEngine *engine, engines) {
62  delete engine;
63  }
64  engines.clear();
65  delete nullEng;
66  }
67 
68  DataEngine *nullEngine()
69  {
70  if (!nullEng) {
71  nullEng = new NullEngine;
72  }
73 
74  return nullEng;
75  }
76 
77  DataEngine::Dict engines;
78  DataEngine *nullEng;
79 };
80 
81 class DataEngineManagerSingleton
82 {
83  public:
84  DataEngineManager self;
85 };
86 
87 K_GLOBAL_STATIC(DataEngineManagerSingleton, privateDataEngineManagerSelf)
88 
89 DataEngineManager *DataEngineManager::self()
90 {
91  return &privateDataEngineManagerSelf->self;
92 }
93 
94 DataEngineManager::DataEngineManager()
95  : d(new DataEngineManagerPrivate)
96 {
97  //startTimer(30000);
98 }
99 
100 DataEngineManager::~DataEngineManager()
101 {
102  delete d;
103 }
104 
105 Plasma::DataEngine *DataEngineManager::engine(const QString &name) const
106 {
107  if (name.isEmpty()) {
108  return d->nullEngine();
109  }
110 
111  Plasma::DataEngine::Dict::const_iterator it = d->engines.constFind(name);
112  if (it != d->engines.constEnd()) {
113  // ref and return the engine
114  //Plasma::DataEngine *engine = *it;
115  return *it;
116  }
117 
118  return d->nullEngine();
119 }
120 
121 Plasma::DataEngine *DataEngineManager::loadEngine(const QString &name)
122 {
123  Plasma::DataEngine::Dict::const_iterator it = d->engines.constFind(name);
124 
125  if (it != d->engines.constEnd()) {
126  DataEngine *engine = *it;
127  engine->d->ref();
128  return engine;
129  }
130 
131  DataEngine *engine = PluginLoader::pluginLoader()->loadDataEngine(name);
132  if (!engine) {
133  return d->nullEngine();
134  }
135 
136  engine->init();
137  d->engines[name] = engine;
138  return engine;
139 }
140 
141 void DataEngineManager::unloadEngine(const QString &name)
142 {
143  Plasma::DataEngine::Dict::iterator it = d->engines.find(name);
144 
145  if (it != d->engines.end()) {
146  Plasma::DataEngine *engine = *it;
147  engine->d->deref();
148 
149  if (!engine->d->isUsed()) {
150  d->engines.erase(it);
151  delete engine;
152  }
153  }
154 }
155 
156 QStringList DataEngineManager::listAllEngines(const QString &parentApp)
157 {
158  QString constraint;
159 
160  if (parentApp.isEmpty()) {
161  constraint.append("(not exist [X-KDE-ParentApp] or [X-KDE-ParentApp] == '')");
162  } else {
163  constraint.append("[X-KDE-ParentApp] == '").append(parentApp).append("'");
164  }
165 
166  KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine", constraint);
167 
168  QStringList engines;
169  foreach (const KService::Ptr &service, offers) {
170  QString name = service->property("X-KDE-PluginInfo-Name").toString();
171  if (!name.isEmpty()) {
172  engines.append(name);
173  }
174  }
175 
176  return engines;
177 }
178 
179 KPluginInfo::List DataEngineManager::listEngineInfo(const QString &parentApp)
180 {
181  return PluginLoader::pluginLoader()->listDataEngineInfo(parentApp);
182 }
183 
184 KPluginInfo::List DataEngineManager::listEngineInfoByCategory(const QString &category, const QString &parentApp)
185 {
186  QString constraint = QString("[X-KDE-PluginInfo-Category] == '%1'").arg(category);
187 
188  if (parentApp.isEmpty()) {
189  constraint.append(" and not exist [X-KDE-ParentApp]");
190  } else {
191  constraint.append(" and [X-KDE-ParentApp] == '").append(parentApp).append("'");
192  }
193 
194  KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine", constraint);
195  return KPluginInfo::fromServices(offers);
196 }
197 
198 void DataEngineManager::timerEvent(QTimerEvent *)
199 {
200 #ifndef NDEBUG
201  QString path = KGlobal::dirs()->locateLocal("appdata", "plasma_dataenginemanager_log");
202  QFile f(path);
203  if (!f.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
204  kDebug() << "faild to open" << path;
205  return;
206  }
207 
208  QTextStream out(&f);
209 
210  QHashIterator<QString, DataEngine*> it(d->engines);
211  out << "================================== " << KGlobal::locale()->formatDateTime(QDateTime::currentDateTime()) << endl;
212  while (it.hasNext()) {
213  it.next();
214  DataEngine *engine = it.value();
215  out << "DataEngine: " << it.key() << ' ' << engine << endl;
216  out << " Claimed # of sources: " << engine->sources().count() << endl;
217  out << " Actual # of sources: " << engine->containerDict().count() << endl;
218  out << endl << " Source Details" << endl;
219 
220  foreach (DataContainer *dc, engine->containerDict()) {
221  out << " * " << dc->objectName() << endl;
222  out << " Data count: " << dc->d->data.count() << endl;
223  out << " Stored: " << dc->isStorageEnabled() << ' ' << endl;
224  const int directs = dc->receivers(SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)));
225  if (directs > 0) {
226  out << " Direction Connections: " << directs << ' ' << endl;
227  }
228 
229  const int relays = dc->d->relays.count();
230  if (relays > 0) {
231  out << " Relays: " << dc->d->relays.count() << endl;
232  QString times;
233  foreach (SignalRelay *relay, dc->d->relays) {
234  times.append(' ').append(QString::number(relay->m_interval));
235  }
236  out << " Relay Timeouts: " << times << ' ' << endl;
237  }
238  }
239 
240  out << endl << "-----" << endl;
241  }
242  out << endl << endl;
243 #endif
244 // killTimer(event->timerId());
245 }
246 
247 } // namespace Plasma
248 
249 #include "dataenginemanager.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Thu Feb 21 2013 11:04:18 by doxygen 1.8.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

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

kdelibs-4.8.5 API Reference

Skip menu "kdelibs-4.8.5 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal