• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-runtime API Reference
  • KDE Home
  • Contact Us
 

PlasmaCore

  • sources
  • kde-4.14
  • kde-runtime
  • plasma
  • declarativeimports
  • core
datasource.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2009 by Alan Alpert <alan.alpert@nokia.com>
3  * Copyright 2010 by Ménard Alexis <menard@kde.org>
4  * Copyright 2010 by Marco MArtin <mart@kde.org>
5 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Library General Public License as
8  * published by the Free Software Foundation; either version 2, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #include "datasource.h"
23 
24 #include "qdeclarativeengine.h"
25 #include "qdeclarativecontext.h"
26 
27 
28 #include <Plasma/Applet>
29 
30 
31 namespace Plasma
32 {
33 DataSource::DataSource(QObject* parent)
34  : QObject(parent),
35  m_interval(0),
36  m_dataEngine(0)
37 {
38  setObjectName("DataSource");
39 }
40 
41 void DataSource::setConnectedSources(const QStringList &sources)
42 {
43  bool sourcesChanged = false;
44  foreach (const QString &source, sources) {
45  if (!m_connectedSources.contains(source)) {
46  sourcesChanged = true;
47  if (m_dataEngine) {
48  m_connectedSources.append(source);
49  m_dataEngine->connectSource(source, this, m_interval);
50  emit sourceConnected(source);
51  }
52  }
53  }
54 
55  foreach (const QString &source, m_connectedSources) {
56  if (!sources.contains(source)) {
57  m_data.remove(source);
58  sourcesChanged = true;
59  if (m_dataEngine) {
60  m_dataEngine->disconnectSource(source, this);
61  emit sourceDisconnected(source);
62  }
63  }
64  }
65 
66  if (sourcesChanged) {
67  m_connectedSources = sources;
68  emit connectedSourcesChanged();
69  }
70 }
71 
72 void DataSource::setEngine(const QString &e)
73 {
74  if (e == m_engine) {
75  return;
76  }
77 
78  m_engine = e;
79  setupData();
80  emit engineChanged();
81 }
82 
83 void DataSource::setInterval(const int interval)
84 {
85  if (interval == m_interval) {
86  return;
87  }
88 
89  m_interval = interval;
90  setupData();
91  emit intervalChanged();
92 }
93 
94 //TODO: event compression for this
95 void DataSource::setupData()
96 {
97  //FIXME: should all services be deleted just because we're changing the interval, etc?
98  qDeleteAll(m_services);
99  m_services.clear();
100 
101  Plasma::DataEngine *engine = dataEngine(m_engine);
102  if (!engine) {
103  kWarning() << "DataEngine" << m_engine << "not found";
104  return;
105  }
106 
107  if (engine != m_dataEngine) {
108  if (m_dataEngine) {
109  m_dataEngine->disconnect(this);
110  finishedWithEngine(m_dataEngine->pluginName());
111  }
112 
113  /*
114  * It is due little explanation why this is a queued connection:
115  * If sourceAdded arrives immediately, in the case we have a datamodel
116  * with items at source level we connect too soon (before setData for
117  * all roles is done), having a dataupdated in the datamodel with only
118  * the first role, killing off the other roles.
119  * Besides causing a model reset more, unfortunately setRoleNames can be done a single time, so is not possible adding new roles after the
120  * first setRoleNames() is called.
121  * This fixes engines that have 1 item per source like the
122  * recommendations engine.
123  */
124  m_dataEngine = engine;
125  connect(m_dataEngine, SIGNAL(sourceAdded(const QString&)), this, SIGNAL(sourcesChanged()), Qt::QueuedConnection);
126  connect(m_dataEngine, SIGNAL(sourceRemoved(const QString&)), this, SIGNAL(sourcesChanged()));
127 
128  connect(m_dataEngine, SIGNAL(sourceAdded(const QString&)), this, SIGNAL(sourceAdded(const QString&)), Qt::QueuedConnection);
129  connect(m_dataEngine, SIGNAL(sourceRemoved(const QString&)), this, SLOT(removeSource(const QString&)));
130  connect(m_dataEngine, SIGNAL(sourceRemoved(const QString&)), this, SIGNAL(sourceRemoved(const QString&)));
131  }
132 
133  foreach (const QString &source, m_connectedSources) {
134  m_dataEngine->connectSource(source, this, m_interval);
135  emit sourceConnected(source);
136  }
137 }
138 
139 void DataSource::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
140 {
141  //it can arrive also data we don't explicitly connected a source
142  if (m_connectedSources.contains(sourceName)) {
143  m_data.insert(sourceName.toLatin1(), data);
144 
145  emit dataChanged();
146  emit newData(sourceName, data);
147  } else if (m_dataEngine) {
148  m_dataEngine->disconnectSource(sourceName, this);
149  }
150 }
151 
152 void DataSource::removeSource(const QString &source)
153 {
154  m_data.remove(source);
155 
156  //TODO: emit those signals as last thing
157  if (m_connectedSources.contains(source)) {
158  m_connectedSources.removeAll(source);
159  emit sourceDisconnected(source);
160  emit connectedSourcesChanged();
161  }
162 
163  if (m_dataEngine) {
164  QHash<QString, Plasma::Service *>::iterator it = m_services.find(source);
165  if (it != m_services.end()) {
166  delete it.value();
167  m_services.erase(it);
168  }
169  }
170 }
171 
172 Plasma::Service *DataSource::serviceForSource(const QString &source)
173 {
174  if (!m_services.contains(source)) {
175  Plasma::Service *service = m_dataEngine->serviceForSource(source);
176  if (!service) {
177  return 0;
178  }
179  m_services[source] = service;
180  }
181 
182  return m_services.value(source);
183 }
184 
185 void DataSource::connectSource(const QString &source)
186 {
187  if (m_connectedSources.contains(source)) {
188  return;
189  }
190 
191  m_connectedSources.append(source);
192  if (m_dataEngine) {
193  m_dataEngine->connectSource(source, this, m_interval);
194  emit sourceConnected(source);
195  emit connectedSourcesChanged();
196  }
197 }
198 
199 void DataSource::disconnectSource(const QString &source)
200 {
201  if (m_dataEngine && m_connectedSources.contains(source)) {
202  m_connectedSources.removeAll(source);
203  m_dataEngine->disconnectSource(source, this);
204  emit sourceDisconnected(source);
205  emit connectedSourcesChanged();
206  }
207 }
208 
209 }
210 #include "datasource.moc"
datasource.h
Plasma::DataSource::data
QVariantHash data() const
Definition: datasource.h:96
Plasma::DataSource::dataUpdated
void dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
Definition: datasource.cpp:139
Plasma::DataSource::interval
int interval() const
Definition: datasource.h:67
Plasma::DataSource::DataSource
DataSource(QObject *parent=0)
Definition: datasource.cpp:33
Plasma::DataSource::engine
QString engine() const
Definition: datasource.h:75
Plasma::DataSource::sourceDisconnected
void sourceDisconnected(const QString &source)
Plasma::DataSource::sourceRemoved
void sourceRemoved(const QString &source)
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
Plasma::DataSource::dataChanged
void dataChanged()
Plasma::DataSource::sourceAdded
void sourceAdded(const QString &source)
Plasma::DataSource::setupData
void setupData()
Definition: datasource.cpp:95
Plasma::DataSource::removeSource
void removeSource(const QString &source)
Definition: datasource.cpp:152
Plasma::DataSource::connectSource
Q_INVOKABLE void connectSource(const QString &source)
Connect a new source.
Definition: datasource.cpp:185
QList::append
void append(const T &value)
QHash
QObject
QObject::setObjectName
void setObjectName(const QString &name)
QList::removeAll
int removeAll(const T &value)
Plasma::DataSource::connectedSourcesChanged
void connectedSourcesChanged()
Plasma::DataEngineConsumer::finishedWithEngine
void finishedWithEngine(const QString &name)
Definition: dataengineconsumer.cpp:92
Plasma::DataSource::setConnectedSources
void setConnectedSources(const QStringList &s)
Definition: datasource.cpp:41
Plasma::DataSource::setInterval
void setInterval(const int interval)
Definition: datasource.cpp:83
QString
QStringList
QHash::erase
iterator erase(iterator pos)
QHash::clear
void clear()
QHash::value
const T value(const Key &key) const
QHash::find
iterator find(const Key &key)
Plasma::DataSource::dataEngine
QString dataEngine
Plugin name of the Plasma DataEngine.
Definition: datasource.h:73
Plasma::DataSource::newData
void newData(const QString &sourceName, const Plasma::DataEngine::Data &data)
QString::toLatin1
QByteArray toLatin1() const
Plasma::DataSource::sources
QStringList sources() const
Definition: datasource.h:89
QHash::contains
bool contains(const Key &key) const
Plasma::DataSource::sourceConnected
void sourceConnected(const QString &source)
QHash::end
iterator end()
Plasma::DataSource::sourcesChanged
void sourcesChanged()
Plasma::DataSource::setEngine
void setEngine(const QString &e)
Definition: datasource.cpp:72
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Plasma::DataSource::disconnectSource
Q_INVOKABLE void disconnectSource(const QString &source)
Disconnects from a DataEngine Source.
Definition: datasource.cpp:199
Plasma::DataSource::intervalChanged
void intervalChanged()
Plasma::DataSource::serviceForSource
Q_INVOKABLE Plasma::Service * serviceForSource(const QString &source)
Definition: datasource.cpp:172
Plasma::DataSource::engineChanged
void engineChanged()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:08:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

PlasmaCore

Skip menu "PlasmaCore"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kde-runtime API Reference

Skip menu "kde-runtime API Reference"
  • KCMShell
  • KNotify
  • Plasma Runtime
  •     PlasmaCore
  •     DragAndDrop
  •     PlasmaComponents
  •     PlasmaExtraComponents
  •     QtExtraComponents

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal