2006-12-17 00:04:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Aaron Seigo <aseigo@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 version 2 as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
#include <QTimer>
|
2007-03-01 09:22:38 +01:00
|
|
|
#include <QVariant>
|
2007-03-01 00:35:26 +01:00
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
#include <KDebug>
|
|
|
|
|
2006-12-17 00:04:44 +01:00
|
|
|
#include "dataengine.h"
|
2007-05-19 10:38:46 +02:00
|
|
|
#include "datavisualization.h"
|
2007-03-01 00:35:26 +01:00
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
2006-12-17 00:04:44 +01:00
|
|
|
|
|
|
|
class DataSource::Private
|
|
|
|
{
|
|
|
|
public:
|
2007-05-19 10:38:46 +02:00
|
|
|
Private()
|
|
|
|
: dirty(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
QString name;
|
|
|
|
Data data;
|
|
|
|
bool dirty;
|
2006-12-17 00:04:44 +01:00
|
|
|
};
|
|
|
|
|
2007-03-01 00:35:26 +01:00
|
|
|
DataSource::DataSource(QObject* parent)
|
2007-05-19 10:38:46 +02:00
|
|
|
: QObject(parent),
|
|
|
|
d(new Private())
|
2006-12-17 00:04:44 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-03-01 00:35:26 +01:00
|
|
|
DataSource::~DataSource()
|
2006-12-17 00:04:44 +01:00
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2007-03-01 00:35:26 +01:00
|
|
|
QString DataSource::name()
|
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
return objectName();
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
void DataSource::setName(const QString& name)
|
|
|
|
{
|
|
|
|
setObjectName(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Plasma::DataSource::Data DataSource::data() const
|
|
|
|
{
|
|
|
|
return d->data;
|
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
void DataSource::setData(const QString& key, const QVariant& value)
|
|
|
|
{
|
|
|
|
d->data[key] = value;
|
|
|
|
d->dirty = true;
|
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
void DataSource::checkForUpdate()
|
|
|
|
{
|
|
|
|
if (d->dirty) {
|
|
|
|
emit updated(d->data);
|
|
|
|
d->dirty = false;
|
|
|
|
}
|
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
class DataEngine::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private(DataEngine* e)
|
|
|
|
: engine(e)
|
|
|
|
{
|
|
|
|
updateTimer = new QTimer(engine);
|
|
|
|
updateTimer->setSingleShot(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSource* source(const QString& sourceName)
|
|
|
|
{
|
|
|
|
DataSource::Dict::const_iterator it = sources.find(sourceName);
|
|
|
|
if (it != sources.constEnd()) {
|
|
|
|
return it.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
kDebug() << "DataEngine " << engine->objectName()
|
|
|
|
<< ": could not find DataSource " << sourceName
|
|
|
|
<< ", creating" << endl;
|
|
|
|
DataSource* s = new DataSource(engine);
|
|
|
|
s->setName(sourceName);
|
|
|
|
sources.insert(sourceName, s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void queueUpdate()
|
|
|
|
{
|
|
|
|
if (updateTimer->isActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTimer->start(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QAtomic ref;
|
|
|
|
DataSource::Dict sources;
|
|
|
|
DataEngine* engine;
|
|
|
|
QTimer* updateTimer;
|
|
|
|
};
|
2007-03-01 00:35:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
DataEngine::DataEngine(QObject* parent)
|
2007-05-19 10:38:46 +02:00
|
|
|
: QObject(parent),
|
|
|
|
d(new Private(this))
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
connect(d->updateTimer, SIGNAL(timeout()), this, SLOT(checkForUpdates()));
|
|
|
|
//FIXME: should we try and delay this call?
|
|
|
|
init();
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DataEngine::~DataEngine()
|
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
delete d;
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList DataEngine::dataSources()
|
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
return d->sources.keys();
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
void DataEngine::connectSource(const QString& source, DataVisualization* visualization)
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
|
|
|
Q_UNUSED(source)
|
|
|
|
Q_UNUSED(visualization)
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
DataSource* s = d->source(source);
|
|
|
|
// if (!s) {
|
|
|
|
// kDebug() << "DataEngine " << objectName() << ": could not find DataSource " << source << endl;
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
connect(s, SIGNAL(updated(Plasma::DataSource::Data)),
|
|
|
|
visualization, SLOT(updated(Plasma::DataSource::Data)));
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DataSource::Data DataEngine::query(const QString& source)
|
|
|
|
{
|
|
|
|
Q_UNUSED(source)
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
DataSource* s = d->source(source);
|
|
|
|
return s->data();
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::init()
|
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
// default implementation does nothing. this is for engines that have to
|
|
|
|
// start things in motion external to themselves before they can work
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
void DataEngine::setData(const QString& source, const QVariant& value)
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
setData(source, source, value);
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
void DataEngine::setData(const QString& source, const QString& key, const QVariant& value)
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
DataSource* s = d->source(source);
|
|
|
|
s->setData(key, value);
|
|
|
|
d->queueUpdate();
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
/*
|
|
|
|
Plasma::DataSource* DataEngine::createDataSource(const QString& source, const QString& domain)
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
|
|
|
Q_UNUSED(domain)
|
2007-05-19 10:38:46 +02:00
|
|
|
//TODO: add support for domains of sources
|
|
|
|
|
|
|
|
if (d->source(source)) {
|
|
|
|
kDebug() << "DataEngine " << objectName() << ": source " << source << " already exists " << endl;
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}*/
|
2007-03-01 00:35:26 +01:00
|
|
|
|
|
|
|
void DataEngine::removeDataSource(const QString& source)
|
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
DataSource::Dict::iterator it = d->sources.find(source);
|
|
|
|
if (it != d->sources.end()) {
|
|
|
|
d->sources.erase(it);
|
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::clearAllDataSources()
|
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
QMutableHashIterator<QString, Plasma::DataSource*> it(d->sources);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
delete it.value();
|
|
|
|
it.remove();
|
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::ref()
|
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
d->ref.ref();
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::deref()
|
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
d->ref.deref();
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DataEngine::isUsed()
|
2006-12-17 00:04:44 +01:00
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
return d->ref != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::checkForUpdates()
|
|
|
|
{
|
|
|
|
QHashIterator<QString, Plasma::DataSource*> it(d->sources);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
it.value()->checkForUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-17 00:04:44 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
#include "dataengine.moc"
|
2006-12-17 00:04:44 +01:00
|
|
|
|