Plasma5Support

dataenginemanager.cpp
1/*
2 SPDX-FileCopyrightText: 2006-2007 Aaron Seigo <aseigo@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "dataenginemanager_p.h"
8
9#include <QFile>
10#include <QTextStream>
11
12#include <QDebug>
13
14#include <QStandardPaths>
15
16#include "datacontainer.h"
17#include "debug_p.h"
18#include "pluginloader.h"
19#include "private/datacontainer_p.h"
20#include "private/dataengine_p.h"
21
22namespace Plasma5Support
23{
24class NullEngine : public DataEngine
25{
26public:
27 explicit NullEngine(QObject *parent = nullptr)
29 {
30 setValid(false);
31
32 // ref() ourselves to ensure we never get deleted
33 d->ref();
34 }
35};
36
37class DataEngineManagerPrivate
38{
39public:
40 DataEngineManagerPrivate()
41 : nullEng(nullptr)
42 {
43 }
44
45 ~DataEngineManagerPrivate()
46 {
47 for (Plasma5Support::DataEngine *engine : std::as_const(engines)) {
48 delete engine;
49 }
50 engines.clear();
51 delete nullEng;
52 }
53
54 DataEngine *nullEngine()
55 {
56 if (!nullEng) {
57 nullEng = new NullEngine;
58 }
59
60 return nullEng;
61 }
62
63 DataEngine::Dict engines;
64 DataEngine *nullEng;
65};
66
67class DataEngineManagerSingleton
68{
69public:
70 DataEngineManager self;
71};
72
73Q_GLOBAL_STATIC(DataEngineManagerSingleton, privateDataEngineManagerSelf)
74
75DataEngineManager *DataEngineManager::self()
76{
77 return &privateDataEngineManagerSelf()->self;
78}
79
80DataEngineManager::DataEngineManager()
81 : d(new DataEngineManagerPrivate)
82{
83 // startTimer(30000);
84}
85
86DataEngineManager::~DataEngineManager()
87{
88 delete d;
89}
90
91Plasma5Support::DataEngine *DataEngineManager::engine(const QString &name) const
92{
93 if (name.isEmpty()) {
94 return d->nullEngine();
95 }
96
97 Plasma5Support::DataEngine::Dict::const_iterator it = d->engines.constFind(name);
98 if (it != d->engines.constEnd()) {
99 return *it;
100 }
101
102 return d->nullEngine();
103}
104
105Plasma5Support::DataEngine *DataEngineManager::loadEngine(const QString &name)
106{
107 if (name.isEmpty()) {
108 qCDebug(LOG_PLASMA5SUPPORT) << "Asked an engine with empty name";
109 return d->nullEngine();
110 }
111 Plasma5Support::DataEngine::Dict::const_iterator it = d->engines.constFind(name);
112
113 if (it != d->engines.constEnd()) {
114 DataEngine *engine = *it;
115 engine->d->ref();
116 return engine;
117 }
118
119 // Expect the plugin id to be the same as the file name or not explicitly set
120 const KPluginMetaData data(QLatin1String("plasma5support/dataengine/plasma_engine_") + name);
121 DataEngine *engine = KPluginFactory::instantiatePlugin<Plasma5Support::DataEngine>(data).plugin;
122 if (!engine) {
123 qCDebug(LOG_PLASMA5SUPPORT) << "Can't find a dataengine named" << name;
124 return d->nullEngine();
125 }
126
127 d->engines[name] = engine;
128 return engine;
129}
130
131void DataEngineManager::unloadEngine(const QString &name)
132{
133 Plasma5Support::DataEngine::Dict::iterator it = d->engines.find(name);
134
135 if (it != d->engines.end()) {
136 Plasma5Support::DataEngine *engine = *it;
137 engine->d->deref();
138
139 if (!engine->d->isUsed()) {
140 d->engines.erase(it);
141 delete engine;
142 }
143 }
144}
145
146void DataEngineManager::timerEvent(QTimerEvent *)
147{
148#ifndef NDEBUG
149 QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/plasma_dataenginemanager_log");
150 QFile f(path);
152 // qCDebug(LOG_PLASMA) << "failed to open" << path;
153 return;
154 }
155
156 QTextStream out(&f);
157
159 out << "================================== " << QLocale().toString(QDateTime::currentDateTime()) << '\n';
160 while (it.hasNext()) {
161 it.next();
162 DataEngine *engine = it.value();
163 out << "DataEngine: " << it.key() << ' ' << engine << '\n';
164 out << " Claimed # of sources: " << engine->sources().count() << '\n';
165 out << " Actual # of sources: " << engine->containerDict().count() << '\n';
166 out << "\n Source Details" << '\n';
167
168 const auto lst = engine->containerDict();
169 for (DataContainer *dc : lst) {
170 out << " * " << dc->objectName() << '\n';
171 out << " Data count: " << dc->d->data.count() << '\n';
172 out << " Stored: " << dc->isStorageEnabled() << " \n";
173 const int directs = dc->receivers(SIGNAL(dataUpdated(QString, Plasma5Support::DataEngine::Data)));
174 if (directs > 0) {
175 out << " Direction Connections: " << directs << " \n";
176 }
177
178 const int relays = dc->d->relays.count();
179 if (relays > 0) {
180 out << " Relays: " << dc->d->relays.count() << '\n';
181 QString times;
182 for (SignalRelay *relay : std::as_const(dc->d->relays)) {
183 times.append(QLatin1Char(' ') + QString::number(relay->m_interval));
184 }
185 out << " Relay Timeouts: " << times << " \n";
186 }
187 }
188
189 out << "\n-----\n";
190 }
191 out << "\n\n";
192#endif
193 // killTimer(event->timerId());
194}
195
196} // namespace Plasma5Support
197
198#include "moc_dataenginemanager_p.cpp"
Data provider for plasmoids (Plasma plugins)
Definition dataengine.h:45
void setValid(bool valid)
Sets whether or not this engine is valid, e.g.
DataEngine(const KPluginMetaData &plugin, QObject *parent=nullptr)
Constructor.
QString path(const QString &relativePath)
QString name(StandardAction id)
Namespace for everything in libplasma.
Definition datamodel.cpp:15
QDateTime currentDateTime()
void clear()
QString toString(QDate date, FormatType format) const const
QObject * parent() const const
QString writableLocation(StandardLocation type)
QString & append(QChar ch)
bool isEmpty() const const
QString number(double n, char format, int precision)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.