Plasma5Support

dataengine.h
1/*
2 SPDX-FileCopyrightText: 2006-2007 Aaron Seigo <aseigo@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#ifndef PLASMA_DATAENGINE_H
8#define PLASMA_DATAENGINE_H
9
10#include <QHash>
11#include <QObject>
12#include <QStringList>
13
14#include <plasma5support/plasma5support_export.h>
15
16#include <KPluginFactory>
17#include <KPluginMetaData>
18#include <plasma5support/plasma5support.h>
19#include <plasma5support/service.h>
20
22
23namespace Plasma5Support
24{
25class DataContainer;
26class Service;
27class DataEnginePrivate;
28
29/**
30 * @class DataEngine plasma5support/dataengine.h <Plasma/DataEngine>
31 *
32 * @short Data provider for plasmoids (Plasma plugins)
33 *
34 * This is the base class for DataEngines, which provide access to bodies of
35 * data via a common and consistent interface. The common use of a DataEngine
36 * is to provide data to a widget for display. This allows a user interface
37 * element to show all sorts of data: as long as there is a DataEngine, the
38 * data is retrievable.
39 *
40 * DataEngines are loaded as plugins on demand and provide zero, one or more
41 * data sources which are identified by name. For instance, a network
42 * DataEngine might provide a data source for each network interface.
43 **/
44class PLASMA5SUPPORT_EXPORT DataEngine : public QObject
45{
46 Q_OBJECT
47
48public:
53
54 /**
55 * Constructor.
56 *
57 * @param parent The parent object.
58 * @param plugin metadata that describes the engine
59 *
60 * @since 5.67
61 */
62 explicit DataEngine(const KPluginMetaData &plugin, QObject *parent = nullptr);
63
64 explicit DataEngine(QObject *parent = nullptr);
65
66 ~DataEngine() override;
67
68 /**
69 * @return a list of all the data sources available via this DataEngine
70 * Whether these sources are currently available (which is what
71 * the default implementation provides) or not is up to the
72 * DataEngine to decide.
73 **/
74 virtual QStringList sources() const;
75
76 /**
77 * @param source the source to target the Service at
78 * @return a Service that has the source as a destination. The service
79 * is parented to the DataEngine, but should be deleted by the
80 * caller when finished with it
81 */
82 Q_INVOKABLE virtual Service *serviceForSource(const QString &source);
83
84 /**
85 * @return description of the plugin that implements this DataEngine
86 *
87 * @since 5.67
88 */
89 KPluginMetaData metadata() const;
90
91 /**
92 * Connects a source to an object for data updates. The object must
93 * have a slot with the following signature:
94 * @code
95 * void dataUpdated(const QString &sourceName, const Plasma5Support::DataEngine::Data &data);
96 * @endcode
97 *
98 * The data is a QHash of QVariants keyed by QString names, allowing
99 * one data source to provide sets of related data.
100 *
101 * @param source the name of the data source
102 * @param visualization the object to connect the data source to
103 * @param pollingInterval the frequency, in milliseconds, with which to check for updates;
104 * a value of 0 (the default) means to update only
105 * when there is new data spontaneously generated
106 * (e.g. by the engine); any other value results in
107 * periodic updates from this source. This value is
108 * per-visualization and can be handy for items that require
109 * constant updates such as scrolling graphs or clocks.
110 * If the data has not changed, no update will be sent.
111 * @param intervalAlignment the number of ms to align the interval to
112 **/
113 Q_INVOKABLE void connectSource(const QString &source,
114 QObject *visualization,
115 uint pollingInterval = 0,
116 Plasma5Support::Types::IntervalAlignment intervalAlignment = Types::NoAlignment) const;
117
118 /**
119 * Connects all currently existing sources to an object for data updates.
120 * The object must have a slot with the following signature:
121 * @code
122 * void dataUpdated(const QString &sourceName, const Plasma5Support::DataEngine::Data &data);
123 * @endcode
124 *
125 * The data is a QHash of QVariants keyed by QString names, allowing
126 * one data source to provide sets of related data.
127 *
128 * This method may be called multiple times for the same visualization
129 * without side-effects. This can be useful to change the pollingInterval.
130 *
131 * Note that this method does not automatically connect sources that
132 * may appear later on. Connecting and responding to the sourceAdded signal
133 * is still required to achieve that.
134 *
135 * @param visualization the object to connect the data source to
136 * @param pollingInterval the frequency, in milliseconds, with which to check for updates;
137 * a value of 0 (the default) means to update only
138 * when there is new data spontaneously generated
139 * (e.g. by the engine); any other value results in
140 * periodic updates from this source. This value is
141 * per-visualization and can be handy for items that require
142 * constant updates such as scrolling graphs or clocks.
143 * If the data has not changed, no update will be sent.
144 * @param intervalAlignment the number of ms to align the interval to
145 **/
146 Q_INVOKABLE void
147 connectAllSources(QObject *visualization, uint pollingInterval = 0, Plasma5Support::Types::IntervalAlignment intervalAlignment = Types::NoAlignment) const;
148
149 /**
150 * Disconnects a source from an object that was receiving data updates.
151 *
152 * @param source the name of the data source
153 * @param visualization the object to connect the data source to
154 **/
155 Q_INVOKABLE void disconnectSource(const QString &source, QObject *visualization) const;
156
157 /**
158 * Retrieves a pointer to the DataContainer for a given source. This method
159 * should not be used if possible.
160 *
161 * @param source the name of the source.
162 * @return pointer to a DataContainer, or zero on failure
163 **/
164 Q_INVOKABLE DataContainer *containerForSource(const QString &source);
165
166 /**
167 * @return The model associated to a source if any. The ownership of the model stays with the DataContainer.
168 * Returns 0 if there isn't any model associated or if the source doesn't exists.
169 */
170 QAbstractItemModel *modelForSource(const QString &source);
171
172 /**
173 * Returns true if this engine is valid, otherwise returns false
174 *
175 * @return true if the engine is valid
176 **/
177 bool isValid() const;
178
179 /**
180 * Returns true if the data engine is empty, which is to say that it has no
181 * data sources currently.
182 *
183 * @return true if the engine has no sources currently
184 */
185 bool isEmpty() const;
186
187Q_SIGNALS:
188 /**
189 * Emitted when a new data source is created
190 *
191 * Note that you do not need to emit this yourself unless
192 * you are reimplementing sources() and want to advertise
193 * that a new source is available (but hasn't been created
194 * yet).
195 *
196 * @param source the name of the new data source
197 **/
198 void sourceAdded(const QString &source);
199
200 /**
201 * Emitted when a data source is removed.
202 *
203 * Note that you do not need to emit this yourself unless
204 * you have reimplemented sources() and want to signal that
205 * a source that was available but was never created is no
206 * longer available.
207 *
208 * @param source the name of the data source that was removed
209 **/
210 void sourceRemoved(const QString &source);
211
212protected:
213 /**
214 * When a source that does not currently exist is requested by the
215 * consumer, this method is called to give the DataEngine the
216 * opportunity to create one.
217 *
218 * The name of the data source (e.g. the source parameter passed into
219 * setData) must be the same as the name passed to sourceRequestEvent
220 * otherwise the requesting visualization may not receive notice of a
221 * data update.
222 *
223 * If the source can not be populated with data immediately (e.g. due to
224 * an asynchronous data acquisition method such as an HTTP request)
225 * the source must still be created, even if it is empty. This can
226 * be accomplished in these cases with the follow line:
227 *
228 * setData(name, DataEngine::Data());
229 *
230 * @param source the name of the source that has been requested
231 * @return true if a DataContainer was set up, false otherwise
232 */
233 virtual bool sourceRequestEvent(const QString &source);
234
235 /**
236 * Called by internal updating mechanisms to trigger the engine
237 * to refresh the data contained in a given source. Reimplement this
238 * method when using facilities such as setPollingInterval.
239 * @see setPollingInterval
240 *
241 * @param source the name of the source that should be updated
242 * @return true if the data was changed, or false if there was no
243 * change or if the change will occur later
244 **/
245 virtual bool updateSourceEvent(const QString &source);
246
247 /**
248 * Sets a value for a data source. If the source
249 * doesn't exist then it is created.
250 *
251 * @param source the name of the data source
252 * @param value the data to associated with the source
253 **/
254 void setData(const QString &source, const QVariant &value);
255
256 /**
257 * Sets a value for a data source. If the source
258 * doesn't exist then it is created.
259 *
260 * @param source the name of the data source
261 * @param key the key to use for the data
262 * @param value the data to associated with the source
263 **/
264 void setData(const QString &source, const QString &key, const QVariant &value);
265
266 /**
267 * Adds a set of data to a data source. If the source
268 * doesn't exist then it is created.
269 *
270 * @param source the name of the data source
271 * @param data the data to add to the source
272 **/
273 void setData(const QString &source, const QVariantMap &data);
274
275 /**
276 * Removes all the data associated with a data source.
277 *
278 * @param source the name of the data source
279 **/
280 void removeAllData(const QString &source);
281
282 /**
283 * Removes a data entry from a source
284 *
285 * @param source the name of the data source
286 * @param key the data entry to remove
287 **/
288 void removeData(const QString &source, const QString &key);
289
290 /**
291 * Associates a model to a data source. If the source
292 * doesn't exist then it is created. The source will have the key "HasModel" to easily indicate there is a model present.
293 *
294 * The ownership of the model is transferred to the DataContainer,
295 * so the model will be deleted when a new one is set or when the
296 * DataContainer itself is deleted. As the DataContainer, it will be
297 * deleted when there won't be any
298 * visualization associated to this source.
299 *
300 * @param source the name of the data source
301 * @param model the model instance
302 */
303 void setModel(const QString &source, QAbstractItemModel *model);
304
305 /**
306 * Adds an already constructed data source. The DataEngine takes
307 * ownership of the DataContainer object. The objectName of the source
308 * is used for the source name.
309 *
310 * @param source the DataContainer to add to the DataEngine
311 **/
312 void addSource(DataContainer *source);
313
314 /**
315 * Sets the minimum amount of time, in milliseconds, that must pass between
316 * successive updates of data. This can help prevent too many updates happening
317 * due to multiple update requests coming in, which can be useful for
318 * expensive (time- or resource-wise) update mechanisms.
319 *
320 * The default minimumPollingInterval is -1, or "never perform automatic updates"
321 *
322 * @param minimumMs the minimum time lapse, in milliseconds, between updates.
323 * A value less than 0 means to never perform automatic updates,
324 * a value of 0 means update immediately on every update request,
325 * a value >0 will result in a minimum time lapse being enforced.
326 **/
327 void setMinimumPollingInterval(int minimumMs);
328
329 /**
330 * @return the minimum time between updates. @see setMinimumPollingInterval
331 **/
332 int minimumPollingInterval() const;
333
334 /**
335 * Sets up an internal update tick for all data sources. On every update,
336 * updateSourceEvent will be called for each applicable source.
337 * @see updateSourceEvent
338 *
339 * @param frequency the time, in milliseconds, between updates. A value of 0
340 * will stop internally triggered updates.
341 **/
342 void setPollingInterval(uint frequency);
343
344 /**
345 * Removes all data sources
346 **/
347 void removeAllSources();
348
349 /**
350 * Sets whether or not this engine is valid, e.g. can be used.
351 * In practice, only the internal fall-back engine, the NullEngine
352 * should have need for this.
353 *
354 * @param valid whether or not the engine is valid
355 **/
356 void setValid(bool valid);
357
358 /**
359 * @return the list of active DataContainers.
360 */
361 QHash<QString, DataContainer *> containerDict() const;
362
363 /**
364 * Reimplemented from QObject
365 **/
366 void timerEvent(QTimerEvent *event) override;
367
368 /**
369 * Sets a source to be stored for easy retrieval
370 * when the real source of the data (usually a network connection)
371 * is unavailable.
372 * @param source the name of the source
373 * @param store if source should be stored
374 * @since 4.6
375 */
376 void setStorageEnabled(const QString &source, bool store);
377
378protected Q_SLOTS:
379 /**
380 * Removes a data source.
381 * @param source the name of the data source to remove
382 **/
383 void removeSource(const QString &source);
384
385 /**
386 * Immediately updates all existing sources when called
387 */
388 void updateAllSources();
389
390 /**
391 * Forces an immediate update to all connected sources, even those with
392 * timeouts that haven't yet expired. This should _only_ be used when
393 * there was no data available, e.g. due to network non-availability,
394 * and then it becomes available. Normal changes in data values due to
395 * calls to updateSource or in the natural progression of the monitored
396 * object (e.g. CPU heat) should not result in a call to this method!
397 *
398 * @since 4.4
399 */
400 void forceImmediateUpdateOfAllVisualizations();
401
402private:
403 friend class DataEnginePrivate;
404 friend class DataEngineManager;
405 friend class PlasmoidServiceJob;
406 friend class NullEngine;
407
408 Q_PRIVATE_SLOT(d, void internalUpdateSource(DataContainer *source))
409 Q_PRIVATE_SLOT(d, void sourceDestroyed(QObject *object))
410 Q_PRIVATE_SLOT(d, void scheduleSourcesUpdated())
411
412 DataEnginePrivate *d;
413};
414
415} // Plasma namespace
416
417#endif // multiple inclusion guard
A set of data exported via a DataEngine.
Data provider for plasmoids (Plasma plugins)
Definition dataengine.h:45
void sourceAdded(const QString &source)
Emitted when a new data source is created.
void sourceRemoved(const QString &source)
Emitted when a data source is removed.
This class provides a generic API for write access to settings or services.
Definition service.h:78
IntervalAlignment
Possible timing alignments.
Namespace for everything in libplasma.
Definition datamodel.cpp:15
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.