File indexing completed on 2025-01-26 05:09:00
0001 /* 0002 SPDX-FileCopyrightText: 2009 Alan Alpert <alan.alpert@nokia.com> 0003 SPDX-FileCopyrightText: 2010 Ménard Alexis <menard@kde.org> 0004 SPDX-FileCopyrightText: 2010 Marco MArtin <mart@kde.org> 0005 SPDX-FileCopyrightText: 2013 Sebastian Kügler <sebas@kde.org> 0006 0007 SPDX-License-Identifier: LGPL-2.0-or-later 0008 */ 0009 0010 #ifndef DATASOURCE_H 0011 #define DATASOURCE_H 0012 0013 #include <QObject> 0014 #include <QQmlParserStatus> 0015 #include <QQmlPropertyMap> 0016 0017 #include <Plasma5Support/DataEngine> 0018 #include <Plasma5Support/DataEngineConsumer> 0019 0020 // This class will hopefully be removed in KF6 (along with DataEngines in general) 0021 0022 namespace Plasma5Support 0023 { 0024 class DataEngine; 0025 0026 /** 0027 * @class DataSource 0028 * @short Provides data from a range of plugins 0029 */ 0030 class DataSource : public QObject, public QQmlParserStatus, DataEngineConsumer 0031 { 0032 Q_OBJECT 0033 Q_INTERFACES(QQmlParserStatus) 0034 0035 public: 0036 enum Change { 0037 NoChange = 0, 0038 DataEngineChanged = 1, 0039 SourcesChanged = 2, 0040 }; 0041 Q_DECLARE_FLAGS(Changes, Change) 0042 0043 typedef QMap<QString, QVariant> Data; 0044 0045 explicit DataSource(QObject *parent = nullptr); 0046 0047 void classBegin() override; 0048 void componentComplete() override; 0049 0050 /** 0051 * True if the connection to the Plasma DataEngine is valid. 0052 */ 0053 Q_PROPERTY(bool valid READ valid) 0054 bool valid() const 0055 { 0056 return m_dataEngine && m_dataEngine->isValid(); 0057 } 0058 0059 /** 0060 * Polling interval in milliseconds when the data will be fetched again. If 0, no polling will be done. 0061 */ 0062 Q_PROPERTY(int interval READ interval WRITE setInterval NOTIFY intervalChanged) 0063 int interval() const 0064 { 0065 return m_interval; 0066 } 0067 void setInterval(const int interval); 0068 0069 /** 0070 * The interval to align polling to. 0071 */ 0072 Q_PROPERTY(Plasma5Support::Types::IntervalAlignment intervalAlignment READ intervalAlignment WRITE setIntervalAlignment NOTIFY intervalAlignmentChanged) 0073 Plasma5Support::Types::IntervalAlignment intervalAlignment() const 0074 { 0075 return m_intervalAlignment; 0076 } 0077 void setIntervalAlignment(Plasma5Support::Types::IntervalAlignment intervalAlignment); 0078 0079 /** 0080 * Plugin name of the Plasma5Support DataEngine 0081 */ 0082 Q_PROPERTY(QString dataEngine READ engine WRITE setEngine NOTIFY engineChanged) 0083 Q_PROPERTY(QString engine READ engine WRITE setEngine NOTIFY engineChanged) 0084 QString engine() const 0085 { 0086 return m_engine; 0087 } 0088 void setEngine(const QString &e); 0089 0090 /** 0091 * List of all the sources connected to the DataEngine. 0092 */ 0093 Q_PROPERTY(QStringList connectedSources READ connectedSources WRITE setConnectedSources NOTIFY connectedSourcesChanged) 0094 QStringList connectedSources() const 0095 { 0096 return m_connectedSources; 0097 } 0098 void setConnectedSources(const QStringList &s); 0099 0100 /** 0101 * Read-only list of all the sources available from the DataEngine (connected or not). 0102 */ 0103 Q_PROPERTY(QStringList sources READ sources NOTIFY sourcesChanged) 0104 QStringList sources() const 0105 { 0106 return m_sources; 0107 } 0108 0109 /** 0110 * All the data fetched by this dataengine. 0111 * This is a map of maps. 0112 * 0113 * 1. At the first level, there are the source names. 0114 * 2. At the second level, there are source-specific keys set by the DataEngine. 0115 * 0116 * Refer to a particular DataEngine implementation for available sources, 0117 * keys and expected value types. 0118 */ 0119 Q_PROPERTY(QQmlPropertyMap *data READ data CONSTANT) 0120 QQmlPropertyMap *data() const 0121 { 0122 return m_data; 0123 } 0124 0125 /** 0126 * All the models associated to this DataEngine, indexed by source. 0127 * 0128 * In order for a model to be available, besides being implemented in the 0129 * DataEngine, the user has to be connected to its source, i.e. the 0130 * source name has to be present in connectedSources list. 0131 */ 0132 Q_PROPERTY(QQmlPropertyMap *models READ models CONSTANT) 0133 QQmlPropertyMap *models() const 0134 { 0135 return m_models; 0136 } 0137 0138 /** 0139 * @returns a Plasma5Support::Service given a source name 0140 * @param source source name we want a service of 0141 */ 0142 Q_INVOKABLE QObject *serviceForSource(const QString &source); 0143 0144 /** 0145 * Connects a new source and adds it to the connectedSources list. 0146 */ 0147 Q_INVOKABLE void connectSource(const QString &source); 0148 0149 /** 0150 * Disconnects from a DataEngine source and removes it from the connectedSources list. 0151 */ 0152 Q_INVOKABLE void disconnectSource(const QString &source); 0153 0154 public Q_SLOTS: 0155 void dataUpdated(const QString &sourceName, const Plasma5Support::DataEngine::Data &data); 0156 void modelChanged(const QString &sourceName, QAbstractItemModel *model); 0157 0158 protected Q_SLOTS: 0159 void removeSource(const QString &source); 0160 void setupData(); 0161 void updateSources(); 0162 0163 Q_SIGNALS: 0164 void newData(const QString &sourceName, const QVariantMap &data); 0165 void sourceAdded(const QString &source); 0166 void sourceRemoved(const QString &source); 0167 void sourceConnected(const QString &source); 0168 void sourceDisconnected(const QString &source); 0169 void intervalChanged(); 0170 void intervalAlignmentChanged(); 0171 void engineChanged(); 0172 void dataChanged(); 0173 void connectedSourcesChanged(); 0174 void sourcesChanged(); 0175 0176 private: 0177 bool m_ready; 0178 QString m_id; 0179 int m_interval; 0180 Plasma5Support::Types::IntervalAlignment m_intervalAlignment; 0181 QString m_engine; 0182 QQmlPropertyMap *m_data = nullptr; 0183 QQmlPropertyMap *m_models = nullptr; 0184 Plasma5Support::DataEngine *m_dataEngine = nullptr; 0185 std::unique_ptr<Plasma5Support::DataEngineConsumer> m_dataEngineConsumer; 0186 QStringList m_sources; 0187 QStringList m_connectedSources; 0188 QStringList m_oldSources; 0189 QStringList m_newSources; 0190 Changes m_changes; 0191 QHash<QString, Plasma5Support::Service *> m_services; 0192 }; 0193 Q_DECLARE_OPERATORS_FOR_FLAGS(DataSource::Changes) 0194 } 0195 #endif