Plasma

datasource.h
1 /*
2  SPDX-FileCopyrightText: 2009 Alan Alpert <[email protected]>
3  SPDX-FileCopyrightText: 2010 Ménard Alexis <[email protected]>
4  SPDX-FileCopyrightText: 2010 Marco MArtin <[email protected]>
5  SPDX-FileCopyrightText: 2013 Sebastian Kügler <[email protected]>
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 <QQmlParserStatus>
15 #include <QQmlPropertyMap>
16 
17 #include <Plasma/DataEngine>
18 #include <Plasma/DataEngineConsumer>
19 
20 class QQmlPropertyMap;
21 
22 // This class will hopefully be removed in KF6 (along with DataEngines in general)
23 
24 namespace Plasma
25 {
26 class DataEngine;
27 
28 /**
29  * @class DataSource
30  * @short Provides data from a range of plugins
31  */
33 {
34  Q_OBJECT
36 
37 public:
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  */
55  Q_PROPERTY(bool valid READ valid)
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(Plasma::Types::IntervalAlignment intervalAlignment READ intervalAlignment WRITE setIntervalAlignment NOTIFY intervalAlignmentChanged)
76  {
77  return m_intervalAlignment;
78  }
79  void setIntervalAlignment(Plasma::Types::IntervalAlignment intervalAlignment);
80 
81  /**
82  * Plugin name of the Plasma 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 Plasma::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 
156 public Q_SLOTS:
157  void dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data);
158  void modelChanged(const QString &sourceName, QAbstractItemModel *model);
159 
160 protected Q_SLOTS:
161  void removeSource(const QString &source);
162  void setupData();
163  void updateSources();
164 
165 Q_SIGNALS:
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 
178 private:
179  bool m_ready;
180  QString m_id;
181  int m_interval;
182  Plasma::Types::IntervalAlignment m_intervalAlignment;
183  QString m_engine;
184  QQmlPropertyMap *m_data = nullptr;
185  QQmlPropertyMap *m_models = nullptr;
186  Plasma::DataEngine *m_dataEngine = nullptr;
187  std::unique_ptr<Plasma::DataEngineConsumer> m_dataEngineConsumer;
188  QStringList m_sources;
189  QStringList m_connectedSources;
190  QStringList m_oldSources;
191  QStringList m_newSources;
192  Changes m_changes;
194 };
195 Q_DECLARE_OPERATORS_FOR_FLAGS(DataSource::Changes)
196 }
197 #endif
Q_OBJECTQ_OBJECT
QQmlPropertyMap data
All the data fetched by this dataengine.
Definition: datasource.h:121
Q_PROPERTY(...)
Data provider for plasmoids (Plasma plugins)
Definition: dataengine.h:50
Q_INVOKABLE QObject * serviceForSource(const QString &source)
Definition: datasource.cpp:205
Namespace for everything in libplasma.
Definition: datamodel.cpp:14
Q_SLOTSQ_SLOTS
int interval
Polling interval in milliseconds when the data will be fetched again.
Definition: datasource.h:64
Plasma::Types::IntervalAlignment intervalAlignment
The interval to align polling to.
Definition: datasource.h:74
Q_INVOKABLE void connectSource(const QString &source)
Connects a new source and adds it to the connectedSources list.
Definition: datasource.cpp:218
QStringList sources
Read-only list of all the sources available from the DataEngine (connected or not).
Definition: datasource.h:105
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
Provides data from a range of plugins.
Definition: datasource.h:32
Q_INVOKABLEQ_INVOKABLE
Q_INVOKABLE void disconnectSource(const QString &source)
Disconnects from a DataEngine source and removes it from the connectedSources list.
Definition: datasource.cpp:232
Q_SIGNALSQ_SIGNALS
Q_INTERFACES(...)
bool isValid() const
Returns true if this engine is valid, otherwise returns false.
Definition: dataengine.cpp:328
IntervalAlignment
Possible timing alignments.
Definition: plasma.h:223
QString dataEngine
Plugin name of the Plasma DataEngine.
Definition: datasource.h:84
QObject * parent() const const
QQmlPropertyMap models
All the models associated to this DataEngine, indexed by source.
Definition: datasource.h:134
A class that makes it safe and easy to use DataEngines.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Sep 26 2023 04:05:57 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.