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.
|
|
|
|
*/
|
|
|
|
|
2011-11-01 19:27:55 +01:00
|
|
|
#include "datasource.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),
|
2011-09-01 16:21:37 +02:00
|
|
|
m_interval(0),
|
2010-10-12 21:51:57 +02:00
|
|
|
m_dataEngine(0)
|
|
|
|
{
|
|
|
|
setObjectName("DataSource");
|
|
|
|
}
|
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
void DataSource::setConnectedSources(const QStringList &sources)
|
2010-10-12 21:51:57 +02:00
|
|
|
{
|
2011-09-22 22:43:32 +02:00
|
|
|
bool sourcesChanged = false;
|
2010-11-08 11:27:36 +01:00
|
|
|
foreach (const QString &source, sources) {
|
|
|
|
if (!m_connectedSources.contains(source)) {
|
2011-09-22 22:43:32 +02:00
|
|
|
sourcesChanged = true;
|
|
|
|
if (m_dataEngine) {
|
|
|
|
m_connectedSources.append(source);
|
|
|
|
m_dataEngine->connectSource(source, this, m_interval);
|
|
|
|
emit sourceConnected(source);
|
|
|
|
}
|
2010-11-08 11:27:36 +01:00
|
|
|
}
|
|
|
|
}
|
2011-09-22 22:43:32 +02:00
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
foreach (const QString &source, m_connectedSources) {
|
2010-11-09 00:07:25 +01:00
|
|
|
if (!sources.contains(source)) {
|
2011-09-22 22:43:32 +02:00
|
|
|
m_data.remove(source);
|
|
|
|
sourcesChanged = true;
|
|
|
|
if (m_dataEngine) {
|
|
|
|
m_dataEngine->disconnectSource(source, this);
|
|
|
|
emit sourceDisconnected(source);
|
|
|
|
}
|
2010-11-08 11:27:36 +01:00
|
|
|
}
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
2011-09-22 22:43:32 +02:00
|
|
|
if (sourcesChanged) {
|
2010-11-08 11:27:36 +01:00
|
|
|
m_connectedSources = sources;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-22 22:43:32 +02:00
|
|
|
m_engine = e;
|
|
|
|
setupData();
|
2010-11-08 11:27:36 +01:00
|
|
|
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;
|
|
|
|
}
|
2011-09-22 22:43:32 +02:00
|
|
|
|
2010-11-08 21:50:09 +01:00
|
|
|
m_interval = interval;
|
2011-09-22 22:43:32 +02:00
|
|
|
setupData();
|
2010-11-08 21:50:09 +01:00
|
|
|
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()
|
|
|
|
{
|
2011-09-22 22:43:32 +02:00
|
|
|
//FIXME: should all services be deleted just because we're changing the interval, etc?
|
|
|
|
qDeleteAll(m_services);
|
|
|
|
m_services.clear();
|
2010-11-08 11:27:36 +01:00
|
|
|
|
2011-09-22 22:43:32 +02:00
|
|
|
Plasma::DataEngine *engine = dataEngine(m_engine);
|
|
|
|
if (!engine) {
|
|
|
|
kWarning() << "DataEngine" << m_engine << "not found";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (engine != m_dataEngine) {
|
2010-11-08 11:27:36 +01:00
|
|
|
if (m_dataEngine) {
|
2011-09-22 22:43:32 +02:00
|
|
|
m_dataEngine->disconnect(this);
|
|
|
|
finishedWithEngine(m_dataEngine->pluginName());
|
2010-11-08 11:27:36 +01:00
|
|
|
}
|
2011-04-12 22:12:48 +02:00
|
|
|
|
2011-09-22 22:43:32 +02:00
|
|
|
m_dataEngine = engine;
|
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-10-12 21:51:57 +02:00
|
|
|
|
2011-09-22 22:43:32 +02:00
|
|
|
foreach (const QString &source, m_connectedSources) {
|
|
|
|
m_dataEngine->connectSource(source, this, m_interval);
|
|
|
|
emit sourceConnected(source);
|
2010-11-08 11:27:36 +01:00
|
|
|
}
|
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)) {
|
2011-09-22 22:43:32 +02:00
|
|
|
m_connectedSources.removeAll(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) {
|
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
|
|
|
Plasma::Service *DataSource::serviceForSource(const QString &source)
|
|
|
|
{
|
|
|
|
if (!m_services.contains(source)) {
|
2011-09-15 21:05:06 +02:00
|
|
|
Plasma::Service *service = m_dataEngine->serviceForSource(source);
|
|
|
|
if (!service) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
m_services[source] = service;
|
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_connectedSources.append(source);
|
2011-09-22 22:43:32 +02:00
|
|
|
if (m_dataEngine) {
|
|
|
|
m_dataEngine->connectSource(source, this, m_interval);
|
|
|
|
emit sourceConnected(source);
|
|
|
|
emit connectedSourcesChanged();
|
|
|
|
}
|
2010-11-08 11:27:36 +01:00
|
|
|
}
|
2010-10-12 21:51:57 +02:00
|
|
|
|
2010-11-08 11:27:36 +01:00
|
|
|
void DataSource::disconnectSource(const QString &source)
|
|
|
|
{
|
2011-09-22 22:43:32 +02:00
|
|
|
if (m_dataEngine && m_connectedSources.contains(source)) {
|
|
|
|
m_connectedSources.removeAll(source);
|
|
|
|
m_dataEngine->disconnectSource(source, this);
|
|
|
|
emit sourceDisconnected(source);
|
|
|
|
emit connectedSourcesChanged();
|
|
|
|
}
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-11-01 19:27:55 +01:00
|
|
|
#include "datasource.moc"
|