Plasma5Support

datasource.h
1/*
2 SPDX-FileCopyrightText: 2009 Alan Alpert <alan.alpert@nokia.com>
3 SPDX-FileCopyrightText: 2010 Ménard Alexis <menard@kde.org>
4 SPDX-FileCopyrightText: 2010 Marco MArtin <mart@kde.org>
5 SPDX-FileCopyrightText: 2013 Sebastian Kügler <sebas@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#ifndef DATASOURCE_H
11#define DATASOURCE_H
12
13#include <QObject>
14#include <QQmlEngine>
15#include <QQmlParserStatus>
16#include <QQmlPropertyMap>
17
18#include <Plasma5Support/DataEngine>
19#include <Plasma5Support/DataEngineConsumer>
20
21// This class will hopefully be removed in KF6 (along with DataEngines in general)
22
23namespace Plasma5Support
24{
25class DataEngine;
26
27/**
28 * @class DataSource
29 * @short Provides data from a range of plugins
30 */
32{
34 QML_ELEMENT
36
37public:
38 enum Change {
39 NoChange = 0,
40 DataEngineChanged = 1,
41 SourcesChanged = 2,
42 };
43 Q_DECLARE_FLAGS(Changes, Change)
44
46
47 explicit DataSource(QObject *parent = nullptr);
48
49 void classBegin() override;
50 void componentComplete() override;
51
52 /**
53 * True if the connection to the Plasma DataEngine is valid.
54 */
56 bool valid() const
57 {
58 return m_dataEngine && m_dataEngine->isValid();
59 }
60
61 /**
62 * Polling interval in milliseconds when the data will be fetched again. If 0, no polling will be done.
63 */
64 Q_PROPERTY(int interval READ interval WRITE setInterval NOTIFY intervalChanged)
65 int interval() const
66 {
67 return m_interval;
68 }
69 void setInterval(const int interval);
70
71 /**
72 * The interval to align polling to.
73 */
74 Q_PROPERTY(Plasma5Support::Types::IntervalAlignment intervalAlignment READ intervalAlignment WRITE setIntervalAlignment NOTIFY intervalAlignmentChanged)
76 {
77 return m_intervalAlignment;
78 }
80
81 /**
82 * Plugin name of the Plasma5Support DataEngine
83 */
84 Q_PROPERTY(QString dataEngine READ engine WRITE setEngine NOTIFY engineChanged)
85 Q_PROPERTY(QString engine READ engine WRITE setEngine NOTIFY engineChanged)
86 QString engine() const
87 {
88 return m_engine;
89 }
90 void setEngine(const QString &e);
91
92 /**
93 * List of all the sources connected to the DataEngine.
94 */
95 Q_PROPERTY(QStringList connectedSources READ connectedSources WRITE setConnectedSources NOTIFY connectedSourcesChanged)
97 {
98 return m_connectedSources;
99 }
100 void setConnectedSources(const QStringList &s);
101
102 /**
103 * Read-only list of all the sources available from the DataEngine (connected or not).
104 */
105 Q_PROPERTY(QStringList sources READ sources NOTIFY sourcesChanged)
106 QStringList sources() const
107 {
108 return m_sources;
109 }
110
111 /**
112 * All the data fetched by this dataengine.
113 * This is a map of maps.
114 *
115 * 1. At the first level, there are the source names.
116 * 2. At the second level, there are source-specific keys set by the DataEngine.
117 *
118 * Refer to a particular DataEngine implementation for available sources,
119 * keys and expected value types.
120 */
122 QQmlPropertyMap *data() const
123 {
124 return m_data;
125 }
126
127 /**
128 * All the models associated to this DataEngine, indexed by source.
129 *
130 * In order for a model to be available, besides being implemented in the
131 * DataEngine, the user has to be connected to its source, i.e. the
132 * source name has to be present in connectedSources list.
133 */
135 QQmlPropertyMap *models() const
136 {
137 return m_models;
138 }
139
140 /**
141 * @returns a Plasma5Support::Service given a source name
142 * @param source source name we want a service of
143 */
145
146 /**
147 * Connects a new source and adds it to the connectedSources list.
148 */
149 Q_INVOKABLE void connectSource(const QString &source);
150
151 /**
152 * Disconnects from a DataEngine source and removes it from the connectedSources list.
153 */
154 Q_INVOKABLE void disconnectSource(const QString &source);
155
156public Q_SLOTS:
157 void dataUpdated(const QString &sourceName, const Plasma5Support::DataEngine::Data &data);
158 void modelChanged(const QString &sourceName, QAbstractItemModel *model);
159
160protected Q_SLOTS:
161 void removeSource(const QString &source);
162 void setupData();
163 void updateSources();
164
166 void newData(const QString &sourceName, const QVariantMap &data);
167 void sourceAdded(const QString &source);
168 void sourceRemoved(const QString &source);
169 void sourceConnected(const QString &source);
170 void sourceDisconnected(const QString &source);
171 void intervalChanged();
172 void intervalAlignmentChanged();
173 void engineChanged();
174 void dataChanged();
175 void connectedSourcesChanged();
176 void sourcesChanged();
177
178private:
179 bool m_ready;
180 QString m_id;
181 int m_interval;
183 QString m_engine;
184 QQmlPropertyMap *m_data = nullptr;
185 QQmlPropertyMap *m_models = nullptr;
186 Plasma5Support::DataEngine *m_dataEngine = nullptr;
187 std::unique_ptr<Plasma5Support::DataEngineConsumer> m_dataEngineConsumer;
188 QStringList m_sources;
189 QStringList m_connectedSources;
190 QStringList m_oldSources;
191 QStringList m_newSources;
192 Changes m_changes;
194};
195Q_DECLARE_OPERATORS_FOR_FLAGS(DataSource::Changes)
196}
197#endif
A class that makes it safe and easy to use DataEngines.
Data provider for plasmoids (Plasma plugins)
Definition dataengine.h:45
bool isValid() const
Returns true if this engine is valid, otherwise returns false.
Provides data from a range of plugins.
Definition datasource.h:32
Plasma5Support::Types::IntervalAlignment intervalAlignment
The interval to align polling to.
Definition datasource.h:74
Q_INVOKABLE void disconnectSource(const QString &source)
Disconnects from a DataEngine source and removes it from the connectedSources list.
QString dataEngine
Plugin name of the Plasma5Support DataEngine.
Definition datasource.h:84
QStringList connectedSources
List of all the sources connected to the DataEngine.
Definition datasource.h:95
bool valid
True if the connection to the Plasma DataEngine is valid.
Definition datasource.h:55
QQmlPropertyMap * data
All the data fetched by this dataengine.
Definition datasource.h:121
Q_INVOKABLE QObject * serviceForSource(const QString &source)
QQmlPropertyMap * models
All the models associated to this DataEngine, indexed by source.
Definition datasource.h:134
Q_INVOKABLE void connectSource(const QString &source)
Connects a new source and adds it to the connectedSources list.
int interval
Polling interval in milliseconds when the data will be fetched again.
Definition datasource.h:64
QStringList sources
Read-only list of all the sources available from the DataEngine (connected or not).
Definition datasource.h:105
IntervalAlignment
Possible timing alignments.
Namespace for everything in libplasma.
Definition datamodel.cpp:15
Q_INTERFACES(...)
Q_INVOKABLEQ_INVOKABLE
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
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.