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-21 16:28:03 +02:00
|
|
|
#include "dataengine.h"
|
|
|
|
|
2007-05-28 07:43:54 +02:00
|
|
|
#include <QQueue>
|
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>
|
|
|
|
|
2007-05-20 22:36:59 +02:00
|
|
|
#include "datasource.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
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
class DataEngine::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private(DataEngine* e)
|
2007-05-28 07:43:54 +02:00
|
|
|
: engine(e),
|
|
|
|
limit(0)
|
2007-05-19 10:38:46 +02:00
|
|
|
{
|
|
|
|
updateTimer = new QTimer(engine);
|
|
|
|
updateTimer->setSingleShot(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSource* source(const QString& sourceName)
|
|
|
|
{
|
|
|
|
DataSource::Dict::const_iterator it = sources.find(sourceName);
|
|
|
|
if (it != sources.constEnd()) {
|
2007-05-28 07:43:54 +02:00
|
|
|
DataSource* s = it.value();
|
|
|
|
if (limit > 0) {
|
|
|
|
QQueue<DataSource*>::iterator it = sourceQueue.begin();
|
|
|
|
while (it != sourceQueue.end()) {
|
|
|
|
if (*it == s) {
|
|
|
|
sourceQueue.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
sourceQueue.enqueue(s);
|
|
|
|
}
|
2007-05-19 10:38:46 +02:00
|
|
|
return it.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
kDebug() << "DataEngine " << engine->objectName()
|
|
|
|
<< ": could not find DataSource " << sourceName
|
|
|
|
<< ", creating" << endl;
|
|
|
|
DataSource* s = new DataSource(engine);
|
2007-05-27 09:59:17 +02:00
|
|
|
s->setObjectName(sourceName);
|
2007-05-19 10:38:46 +02:00
|
|
|
sources.insert(sourceName, s);
|
2007-05-28 07:43:54 +02:00
|
|
|
|
|
|
|
if (limit > 0) {
|
|
|
|
trimQueue();
|
|
|
|
sourceQueue.enqueue(s);
|
|
|
|
}
|
2007-05-23 08:38:44 +02:00
|
|
|
emit engine->newDataSource(sourceName);
|
2007-05-19 10:38:46 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-05-28 07:43:54 +02:00
|
|
|
void trimQueue()
|
|
|
|
{
|
|
|
|
while (sourceQueue.count() >= limit) {
|
|
|
|
DataSource* punted = sourceQueue.dequeue();
|
|
|
|
engine->removeDataSource(punted->objectName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
void queueUpdate()
|
|
|
|
{
|
|
|
|
if (updateTimer->isActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTimer->start(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QAtomic ref;
|
|
|
|
DataSource::Dict sources;
|
2007-05-28 07:43:54 +02:00
|
|
|
QQueue<DataSource*> sourceQueue;
|
2007-05-19 10:38:46 +02:00
|
|
|
DataEngine* engine;
|
|
|
|
QTimer* updateTimer;
|
2007-05-23 09:13:00 +02:00
|
|
|
QString icon;
|
2007-05-28 07:43:54 +02:00
|
|
|
uint limit;
|
2007-05-19 10:38:46 +02:00
|
|
|
};
|
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()));
|
2007-05-23 09:13:00 +02:00
|
|
|
//FIXME: we should delay this call; to when is the question.
|
2007-05-19 10:38:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-05-23 10:27:09 +02:00
|
|
|
QStringList DataEngine::dataSources() const
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
return d->sources.keys();
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-05-23 10:27:09 +02:00
|
|
|
void DataEngine::connectSource(const QString& source, QObject* visualization) const
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
DataSource* s = d->source(source);
|
2007-05-22 04:49:54 +02:00
|
|
|
connect(s, SIGNAL(updated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(updated(QString,Plasma::DataEngine::Data)));
|
2007-05-28 07:43:54 +02:00
|
|
|
QMetaObject::invokeMethod(visualization, SLOT(updated(QString,Plasma::DataEngine::Data)),
|
|
|
|
Q_ARG(QString, s->objectName()),
|
|
|
|
Q_ARG(Plasma::DataEngine::Data, s->data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataEngine::connectAllSources(QObject* visualization) const
|
|
|
|
{
|
|
|
|
foreach (const DataSource* s, d->sources) {
|
|
|
|
connect(s, SIGNAL(updated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(updated(QString,Plasma::DataEngine::Data)));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const DataSource* s, d->sources) {
|
|
|
|
QMetaObject::invokeMethod(visualization,
|
|
|
|
SLOT(updated(QString,Plasma::DataEngine::Data)),
|
|
|
|
Q_ARG(QString, s->objectName()),
|
|
|
|
Q_ARG(Plasma::DataEngine::Data, s->data()));
|
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-05-23 10:27:09 +02:00
|
|
|
DataEngine::Data DataEngine::query(const QString& source) const
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
|
|
|
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-23 08:38:44 +02:00
|
|
|
void DataEngine::addSource(DataSource* source)
|
|
|
|
{
|
2007-05-27 09:59:17 +02:00
|
|
|
DataSource::Dict::const_iterator it = d->sources.find(source->objectName());
|
2007-05-23 08:38:44 +02:00
|
|
|
if (it != d->sources.constEnd()) {
|
2007-05-27 09:59:17 +02:00
|
|
|
kDebug() << "source named \"" << source->objectName() << "\" already exists." << endl;
|
2007-05-23 08:38:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-05-27 09:59:17 +02:00
|
|
|
d->sources.insert(source->objectName(), source);
|
|
|
|
emit newDataSource(source->objectName());
|
2007-05-23 08:38:44 +02:00
|
|
|
}
|
|
|
|
|
2007-05-28 07:43:54 +02:00
|
|
|
void DataEngine::setSourceLimit(uint limit)
|
|
|
|
{
|
|
|
|
if (d->limit == limit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->limit = limit;
|
|
|
|
|
|
|
|
if (d->limit > 0) {
|
|
|
|
d->trimQueue();
|
|
|
|
} else {
|
|
|
|
d->sourceQueue.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()) {
|
2007-05-22 04:49:54 +02:00
|
|
|
emit dataSourceRemoved(it.key());
|
2007-05-19 10:38:46 +02:00
|
|
|
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();
|
2007-05-22 04:49:54 +02:00
|
|
|
emit dataSourceRemoved(it.key());
|
2007-05-19 10:38:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-05-23 10:27:09 +02:00
|
|
|
bool DataEngine::isUsed() const
|
2006-12-17 00:04:44 +01:00
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
return d->ref != 0;
|
|
|
|
}
|
|
|
|
|
2007-05-23 09:13:00 +02:00
|
|
|
void DataEngine::setIcon(const QString& icon)
|
|
|
|
{
|
|
|
|
d->icon = icon;
|
|
|
|
}
|
|
|
|
|
2007-05-23 10:27:09 +02:00
|
|
|
QString DataEngine::icon() const
|
2007-05-23 09:13:00 +02:00
|
|
|
{
|
|
|
|
return d->icon;
|
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
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
|
|
|
|