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),
|
2007-06-03 00:23:26 +02:00
|
|
|
limit(0),
|
|
|
|
valid(true)
|
2007-05-19 10:38:46 +02:00
|
|
|
{
|
|
|
|
updateTimer = new QTimer(engine);
|
|
|
|
updateTimer->setSingleShot(true);
|
|
|
|
}
|
|
|
|
|
2007-06-10 06:03:50 +02:00
|
|
|
DataSource* source(const QString& sourceName, bool createWhenMissing = true)
|
2007-05-19 10:38:46 +02:00
|
|
|
{
|
2007-06-10 06:03:50 +02:00
|
|
|
DataEngine::SourceDict::const_iterator it = sources.find(sourceName);
|
2007-05-19 10:38:46 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2007-06-10 06:03:50 +02:00
|
|
|
if (!createWhenMissing) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-10 07:39:27 +02:00
|
|
|
/* kDebug() << "DataEngine " << engine->objectName()
|
2007-05-19 10:38:46 +02:00
|
|
|
<< ": could not find DataSource " << sourceName
|
2007-06-10 07:39:27 +02:00
|
|
|
<< ", creating" << endl;*/
|
2007-05-19 10:38:46 +02:00
|
|
|
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-06-13 06:56:49 +02:00
|
|
|
emit engine->newSource(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();
|
2007-06-11 23:10:44 +02:00
|
|
|
engine->removeSource(punted->objectName());
|
2007-05-28 07:43:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
void queueUpdate()
|
|
|
|
{
|
|
|
|
if (updateTimer->isActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTimer->start(0);
|
|
|
|
}
|
|
|
|
|
2007-06-13 06:56:49 +02:00
|
|
|
bool sourceRequested(const QString& source)
|
2007-06-10 06:03:50 +02:00
|
|
|
{
|
|
|
|
//get around const! =P
|
2007-06-13 06:56:49 +02:00
|
|
|
return engine->sourceRequested(source);
|
2007-06-10 06:03:50 +02:00
|
|
|
}
|
|
|
|
|
2007-05-19 10:38:46 +02:00
|
|
|
QAtomic ref;
|
2007-06-10 06:03:50 +02:00
|
|
|
DataEngine::SourceDict 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-06-03 00:23:26 +02:00
|
|
|
bool valid;
|
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-06-03 00:23:26 +02:00
|
|
|
//kDebug() << objectName() << ": bye bye birdy! " << endl;
|
2007-05-19 10:38:46 +02:00
|
|
|
delete d;
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-06-13 06:56:49 +02:00
|
|
|
QStringList DataEngine::sources() 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-06-10 06:03:50 +02:00
|
|
|
DataSource* s = d->source(source, false);
|
|
|
|
|
|
|
|
if (!s) {
|
2007-06-10 07:39:27 +02:00
|
|
|
// we didn't find a data source, so give the engine an opportunity to make one
|
2007-06-13 06:56:49 +02:00
|
|
|
if (d->sourceRequested(source)) {
|
2007-06-10 07:39:27 +02:00
|
|
|
s = d->source(source);
|
|
|
|
if (s) {
|
|
|
|
// now we have a source; since it was created on demand, assume
|
|
|
|
// it should be removed when not used
|
2007-06-11 23:10:44 +02:00
|
|
|
connect(s, SIGNAL(unused(QString)), this, SLOT(removeSource(QString)));
|
2007-06-10 07:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
return;
|
2007-06-10 06:03:50 +02:00
|
|
|
}
|
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
2007-06-10 07:39:27 +02:00
|
|
|
void DataEngine::disconnectSource(const QString& source, QObject* visualization) const
|
|
|
|
{
|
|
|
|
DataSource* s = d->source(source, false);
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect(s, SIGNAL(updated(QString,Plasma::DataEngine::Data)),
|
|
|
|
visualization, SLOT(updated(QString,Plasma::DataEngine::Data)));
|
|
|
|
}
|
|
|
|
|
2007-05-28 07:43:54 +02:00
|
|
|
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-06-13 06:56:49 +02:00
|
|
|
bool DataEngine::sourceRequested(const QString &name)
|
2007-06-10 06:03:50 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(name)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-06-10 06:03:50 +02:00
|
|
|
SourceDict::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);
|
2007-06-13 06:56:49 +02:00
|
|
|
emit newSource(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-06-11 23:10:44 +02:00
|
|
|
void DataEngine::removeSource(const QString& source)
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2007-06-10 07:39:27 +02:00
|
|
|
//kDebug() << "removing source " << source << endl;
|
2007-06-10 06:03:50 +02:00
|
|
|
SourceDict::iterator it = d->sources.find(source);
|
2007-05-19 10:38:46 +02:00
|
|
|
if (it != d->sources.end()) {
|
2007-06-13 06:56:49 +02:00
|
|
|
emit sourceRemoved(it.key());
|
2007-05-19 10:38:46 +02:00
|
|
|
d->sources.erase(it);
|
|
|
|
}
|
2007-03-01 00:35:26 +01:00
|
|
|
}
|
|
|
|
|
2007-06-13 06:56:49 +02:00
|
|
|
void DataEngine::clearSources()
|
2007-03-01 00:35:26 +01:00
|
|
|
{
|
2007-05-19 10:38:46 +02:00
|
|
|
QMutableHashIterator<QString, Plasma::DataSource*> it(d->sources);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
2007-06-13 06:56:49 +02:00
|
|
|
emit sourceRemoved(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-06-03 00:23:26 +02:00
|
|
|
bool DataEngine::isValid() const
|
|
|
|
{
|
|
|
|
return d->valid;
|
|
|
|
}
|
|
|
|
|
2007-06-04 18:11:55 +02:00
|
|
|
void DataEngine::setValid(bool valid)
|
2007-06-03 00:23:26 +02:00
|
|
|
{
|
|
|
|
d->valid = valid;
|
|
|
|
}
|
|
|
|
|
2007-06-10 06:03:50 +02:00
|
|
|
DataEngine::SourceDict DataEngine::sourceDict() const
|
|
|
|
{
|
|
|
|
return d->sources;
|
|
|
|
}
|
|
|
|
|
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"
|