2010-10-12 21:51:57 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2009 by Alan Alpert <alan.alpert@nokia.com>
|
|
|
|
* Copyright 2010 by Ménard Alexis <menard@kde.org>
|
|
|
|
* Copyright 2010 by Marco MArtin <mart@kde.org>
|
|
|
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "datasource_p.h"
|
2010-10-26 14:01:59 +02:00
|
|
|
|
2010-10-12 21:51:57 +02:00
|
|
|
#include "qdeclarativeengine.h"
|
|
|
|
#include "qdeclarativecontext.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <Plasma/Applet>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
DataSource::DataSource(QObject* parent)
|
|
|
|
: QObject(parent),
|
|
|
|
m_interval(1000),
|
|
|
|
m_dataEngine(0)
|
|
|
|
{
|
|
|
|
setObjectName("DataSource");
|
|
|
|
|
|
|
|
connect(this, SIGNAL(engineChanged()),
|
|
|
|
this, SLOT(setupData()));
|
2010-11-08 11:27:36 +01:00
|
|
|
connect(this, SIGNAL(connectedSourcesChanged()),
|
2011-04-17 20:01:45 +02:00
|
|
|
this, SLOT(setupData()), Qt::QueuedConnection);
|
2010-10-12 21:51:57 +02:00
|
|
|
connect(this, SIGNAL(intervalChanged()),
|
|
|
|
this, SLOT(setupData()));
|
|
|
|
}
|
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
void DataSource::setConnectedSources(const QStringList &sources)
|
2010-10-12 21:51:57 +02:00
|
|
|
{
|
2010-11-08 11:27:36 +01:00
|
|
|
foreach (const QString &source, sources) {
|
|
|
|
if (!m_connectedSources.contains(source)) {
|
|
|
|
m_newSources.append(source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach (const QString &source, m_connectedSources) {
|
2010-11-09 00:07:25 +01:00
|
|
|
if (!sources.contains(source)) {
|
2010-11-08 11:27:36 +01:00
|
|
|
m_oldSources.append(source);
|
|
|
|
}
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
if (!m_newSources.isEmpty() || !m_oldSources.isEmpty()) {
|
|
|
|
m_connectedSources = sources;
|
|
|
|
m_changes |= SourcesChanged;
|
|
|
|
emit connectedSourcesChanged();
|
|
|
|
}
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
void DataSource::setEngine(const QString &e)
|
2010-10-12 21:51:57 +02:00
|
|
|
{
|
2010-11-08 11:27:36 +01:00
|
|
|
if (e == m_engine) {
|
2010-10-12 21:51:57 +02:00
|
|
|
return;
|
|
|
|
}
|
2010-11-08 11:27:36 +01:00
|
|
|
m_engine = e;
|
2010-10-12 21:51:57 +02:00
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
m_changes |= DataEngineChanged;
|
|
|
|
emit engineChanged();
|
|
|
|
}
|
2010-10-12 21:51:57 +02:00
|
|
|
|
2010-11-08 21:50:09 +01:00
|
|
|
void DataSource::setInterval(const int interval)
|
|
|
|
{
|
|
|
|
if (interval == m_interval) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_interval = interval;
|
|
|
|
m_changes |= DataEngineChanged;
|
|
|
|
emit intervalChanged();
|
|
|
|
}
|
|
|
|
|
2010-11-09 00:07:25 +01:00
|
|
|
//TODO: event compression for this
|
2010-11-08 11:27:36 +01:00
|
|
|
void DataSource::setupData()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (m_changes & DataEngineChanged) {
|
|
|
|
if (m_dataEngine) {
|
|
|
|
foreach (const QString &source, m_connectedSources) {
|
|
|
|
m_dataEngine->disconnectSource(source, this);
|
|
|
|
}
|
|
|
|
}
|
2011-04-12 22:12:48 +02:00
|
|
|
|
|
|
|
qDeleteAll(m_services);
|
2010-11-08 11:27:36 +01:00
|
|
|
m_services.clear();
|
2010-10-12 21:51:57 +02:00
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
m_dataEngine = dataEngine(m_engine);
|
|
|
|
if (!m_dataEngine) {
|
|
|
|
kWarning() << "DataEngine" << m_engine << "not found";
|
|
|
|
return;
|
|
|
|
}
|
2011-02-24 21:32:10 +01:00
|
|
|
connect(m_dataEngine, SIGNAL(sourceAdded(const QString&)), this, SIGNAL(sourcesChanged()));
|
|
|
|
connect(m_dataEngine, SIGNAL(sourceRemoved(const QString&)), this, SIGNAL(sourcesChanged()));
|
2010-11-08 11:27:36 +01:00
|
|
|
|
|
|
|
connect(m_dataEngine, SIGNAL(sourceAdded(const QString&)), this, SIGNAL(sourceAdded(const QString&)));
|
|
|
|
connect(m_dataEngine, SIGNAL(sourceRemoved(const QString&)), this, SLOT(removeSource(const QString&)));
|
2010-11-09 00:07:25 +01:00
|
|
|
connect(m_dataEngine, SIGNAL(sourceRemoved(const QString&)), this, SIGNAL(sourceRemoved(const QString&)));
|
2010-11-08 11:27:36 +01:00
|
|
|
|
2010-11-09 00:07:25 +01:00
|
|
|
//TODO: remove after event compression done
|
2010-11-08 11:27:36 +01:00
|
|
|
if (!(m_changes & SourcesChanged)) {
|
|
|
|
foreach (const QString &source, m_connectedSources) {
|
2010-11-08 21:50:09 +01:00
|
|
|
m_dataEngine->connectSource(source, this, m_interval);
|
2010-11-09 00:07:25 +01:00
|
|
|
emit sourceConnected(source);
|
2010-11-08 11:27:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-12 21:51:57 +02:00
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
if (m_changes & SourcesChanged) {
|
|
|
|
if (m_dataEngine) {
|
|
|
|
foreach (const QString &source, m_oldSources) {
|
|
|
|
m_dataEngine->disconnectSource(source, this);
|
2010-11-09 00:07:25 +01:00
|
|
|
emit sourceDisconnected(source);
|
2010-11-08 11:27:36 +01:00
|
|
|
}
|
|
|
|
foreach (const QString &source, m_newSources) {
|
2010-11-08 21:50:09 +01:00
|
|
|
m_dataEngine->connectSource(source, this, m_interval);
|
2010-11-09 00:07:25 +01:00
|
|
|
emit sourceConnected(source);
|
2010-11-08 11:27:36 +01:00
|
|
|
}
|
|
|
|
m_oldSources.clear();
|
|
|
|
m_newSources.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_changes = NoChange;
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataSource::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
|
|
|
|
{
|
2011-01-20 21:22:44 +01:00
|
|
|
//it can arrive also data we don't explicitly connected a source
|
|
|
|
if (m_connectedSources.contains(sourceName)) {
|
|
|
|
m_data.insert(sourceName.toLatin1(), data);
|
2010-10-12 21:51:57 +02:00
|
|
|
|
2011-01-20 21:29:00 +01:00
|
|
|
emit dataChanged();
|
|
|
|
emit newData(sourceName, data);
|
2011-04-12 22:12:53 +02:00
|
|
|
} else if (m_dataEngine) {
|
|
|
|
m_dataEngine->disconnectSource(sourceName, this);
|
2011-01-20 21:29:00 +01:00
|
|
|
}
|
2010-11-08 11:27:36 +01:00
|
|
|
}
|
2010-10-26 14:01:59 +02:00
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
void DataSource::removeSource(const QString &source)
|
|
|
|
{
|
|
|
|
m_data.remove(source);
|
2010-10-12 21:51:57 +02:00
|
|
|
|
2010-11-09 00:07:25 +01:00
|
|
|
//TODO: emit those signals as last thing
|
2010-11-08 11:27:36 +01:00
|
|
|
if (m_connectedSources.contains(source)) {
|
2010-11-09 00:07:25 +01:00
|
|
|
emit sourceDisconnected(source);
|
2010-11-08 11:27:36 +01:00
|
|
|
emit connectedSourcesChanged();
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
2011-04-12 22:12:48 +02:00
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
if (m_dataEngine) {
|
|
|
|
m_connectedSources.removeAll(source);
|
|
|
|
m_newSources.removeAll(source);
|
|
|
|
m_oldSources.removeAll(source);
|
2011-04-12 22:12:48 +02:00
|
|
|
|
|
|
|
QHash<QString, Plasma::Service *>::iterator it = m_services.find(source);
|
|
|
|
if (it != m_services.end()) {
|
|
|
|
delete it.value();
|
|
|
|
m_services.erase(it);
|
|
|
|
}
|
2010-11-08 11:27:36 +01:00
|
|
|
}
|
|
|
|
}
|
2010-10-12 21:51:57 +02:00
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
QStringList DataSource::keysForSource(const QString &source) const
|
|
|
|
{
|
|
|
|
if (!m_data.contains(source)) {
|
|
|
|
return QStringList();
|
|
|
|
}
|
2010-11-08 21:50:09 +01:00
|
|
|
return m_data.value(source).value<Data>().keys();
|
2010-11-08 11:27:36 +01:00
|
|
|
}
|
2010-10-12 21:51:57 +02:00
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
Plasma::Service *DataSource::serviceForSource(const QString &source)
|
|
|
|
{
|
|
|
|
if (!m_services.contains(source)) {
|
|
|
|
m_services[source] = m_dataEngine->serviceForSource(source);
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
return m_services.value(source);
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
void DataSource::connectSource(const QString &source)
|
2010-10-12 21:51:57 +02:00
|
|
|
{
|
2011-04-25 00:17:25 +02:00
|
|
|
if (m_connectedSources.contains(source)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
m_newSources.append(source);
|
|
|
|
m_connectedSources.append(source);
|
|
|
|
m_changes |= SourcesChanged;
|
2010-11-09 00:07:25 +01:00
|
|
|
emit sourceConnected(source);
|
2010-11-08 11:27:36 +01:00
|
|
|
emit connectedSourcesChanged();
|
|
|
|
}
|
2010-10-12 21:51:57 +02:00
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
void DataSource::disconnectSource(const QString &source)
|
|
|
|
{
|
|
|
|
m_oldSources.append(source);
|
|
|
|
m_connectedSources.removeAll(source);
|
|
|
|
m_changes |= SourcesChanged;
|
2010-11-09 00:07:25 +01:00
|
|
|
emit sourceDisconnected(source);
|
2010-11-08 11:27:36 +01:00
|
|
|
emit connectedSourcesChanged();
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#include "datasource_p.moc"
|