plasma-framework/declarativeimports/core/datasource.cpp
Marco Martin 7ee1ebf5ac a crazy experiment: if one of the dataitems is a QVariantList of QVariantMaps (like many dataengines, like rss, microblog, opendesktop...)
make it available as a QAbstractModel, so will be possible to categorize it
(considering to put it behind a qsortfilterproxymodel to make it easy to do filtering)

svn path=/trunk/KDE/kdebase/runtime/; revision=1189920
2010-10-26 12:01:59 +00:00

155 lines
4.2 KiB
C++

/*
* 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"
#include "datamodel.h"
#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");
m_data = new QDeclarativePropertyMap(this);
connect(this, SIGNAL(engineChanged()),
this, SLOT(setupData()));
connect(this, SIGNAL(sourceChanged()),
this, SLOT(setupData()));
connect(this, SIGNAL(intervalChanged()),
this, SLOT(setupData()));
}
Plasma::DataModel *DataSource::modelFromKey(const QString &key)
{
return qobject_cast<DataModel*>(qvariant_cast<QObject*>(m_data->value(key.toLatin1())));
}
void DataSource::setSource(const QString &s)
{
if (s == m_source) {
return;
}
m_source = s;
emit sourceChanged();
}
void DataSource::setupData()
{
if (/*m_source.isEmpty() ||*/ m_engine.isEmpty()) {
return;
}
if (m_dataEngine) {
m_dataEngine->disconnectSource(m_connectedSource, this);
m_dataEngine = 0;
m_keys.clear();
emit keysChanged();
}
Plasma::DataEngine* de = dataEngine(m_engine);
if (!de) {
kWarning() << "DataEngine not found";
return;
}
de->connectSource(m_source, this, m_interval);
m_dataEngine = de;
m_connectedSource = m_source;
connect(de, SIGNAL(sourceAdded(constQString&)), this, SIGNAL(sourcesChanged()));
connect(de, SIGNAL(sourceRemoved(constQString&)), this, SIGNAL(sourcesChanged()));
}
void DataSource::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
{
Q_UNUSED(sourceName);//Only one source
QStringList newKeys;
foreach (const QString &key, data.keys()) {
// Properties in QML must start lowercase.
QString ourKey = key.toLower();
if (data.value(key).canConvert<QVariantList>() && data.value(key).value<QVariantList>().first().canConvert<QVariantMap>()) {
DataModel *model = modelFromKey(ourKey);
if (model) {
model->removeRows(0, model->rowCount());
} else {
model = new DataModel(m_data);
m_data->insert(ourKey.toLatin1(), qVariantFromValue(static_cast<QObject*>(model)));
}
model->setItems(data.value(key).value<QVariantList>());
} else {
DataModel *model = modelFromKey(ourKey);
if (model) {
model->deleteLater();
}
m_data->insert(ourKey.toLatin1(), data.value(key));
}
newKeys << ourKey;
}
if (newKeys != m_keys) {
//FIXME: pretty utterly inefficient
foreach (const QString &key, m_keys) {
if (!newKeys.contains(key)) {
DataModel *model = modelFromKey(key);
if (model) {
model->deleteLater();
}
m_data->insert(key.toLatin1(), QVariant());
}
}
emit keysChanged();
m_keys = newKeys;
}
emit dataChanged();
}
Plasma::Service *DataSource::service()
{
if (!m_service) {
m_service = m_dataEngine->serviceForSource(m_source);
}
return m_service;
}
}
#include "datasource_p.moc"