From 10dd76c7945e1c9e81d3c57cbd8609d01fd9b42a Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Mon, 28 May 2007 05:43:54 +0000 Subject: [PATCH] * when connecting sources, invoke the updated method so they get the current values immediately * add the ability to limit the number of data sources in an engine; least used items drop off. the algorithm for this is pretty simplistic and not overly scalable, but that's something that can be fixed when we run into problems (e.g. setLimit(1000) or something silly =) svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=668927 --- dataengine.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++--- dataengine.h | 25 ++++++++++++++++++- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/dataengine.cpp b/dataengine.cpp index dfa1365fe..b83fc434a 100644 --- a/dataengine.cpp +++ b/dataengine.cpp @@ -18,6 +18,7 @@ #include "dataengine.h" +#include #include #include @@ -32,7 +33,8 @@ class DataEngine::Private { public: Private(DataEngine* e) - : engine(e) + : engine(e), + limit(0) { updateTimer = new QTimer(engine); updateTimer->setSingleShot(true); @@ -42,6 +44,18 @@ class DataEngine::Private { DataSource::Dict::const_iterator it = sources.find(sourceName); if (it != sources.constEnd()) { + DataSource* s = it.value(); + if (limit > 0) { + QQueue::iterator it = sourceQueue.begin(); + while (it != sourceQueue.end()) { + if (*it == s) { + sourceQueue.erase(it); + break; + } + ++it; + } + sourceQueue.enqueue(s); + } return it.value(); } @@ -51,10 +65,23 @@ class DataEngine::Private DataSource* s = new DataSource(engine); s->setObjectName(sourceName); sources.insert(sourceName, s); + + if (limit > 0) { + trimQueue(); + sourceQueue.enqueue(s); + } emit engine->newDataSource(sourceName); return s; } + void trimQueue() + { + while (sourceQueue.count() >= limit) { + DataSource* punted = sourceQueue.dequeue(); + engine->removeDataSource(punted->objectName()); + } + } + void queueUpdate() { if (updateTimer->isActive()) { @@ -65,9 +92,11 @@ class DataEngine::Private QAtomic ref; DataSource::Dict sources; + QQueue sourceQueue; DataEngine* engine; QTimer* updateTimer; QString icon; + uint limit; }; @@ -92,12 +121,27 @@ QStringList DataEngine::dataSources() const void DataEngine::connectSource(const QString& source, QObject* visualization) const { - Q_UNUSED(source) - Q_UNUSED(visualization) - DataSource* s = d->source(source); connect(s, SIGNAL(updated(QString,Plasma::DataEngine::Data)), visualization, SLOT(updated(QString,Plasma::DataEngine::Data))); + 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())); + } } DataEngine::Data DataEngine::query(const QString& source) const @@ -138,6 +182,21 @@ void DataEngine::addSource(DataSource* source) emit newDataSource(source->objectName()); } +void DataEngine::setSourceLimit(uint limit) +{ + if (d->limit == limit) { + return; + } + + d->limit = limit; + + if (d->limit > 0) { + d->trimQueue(); + } else { + d->sourceQueue.clear(); + } +} + /* Plasma::DataSource* DataEngine::createDataSource(const QString& source, const QString& domain) { diff --git a/dataengine.h b/dataengine.h index b5842b89f..9adce2b42 100644 --- a/dataengine.h +++ b/dataengine.h @@ -84,6 +84,19 @@ class PLASMA_EXPORT DataEngine : public QObject **/ void connectSource(const QString& source, QObject* visualization) const; + /** + * Connects all sources to an object for data updates. The object must + * have a slot with the following signature: + * + * SLOT(updated(QString, Plasma::DataEngine::Data)) + * + * The data is a QHash of QVariants keyed by QString names, allowing + * one data source to provide sets of related data. + * + * @param visualization the object to connect the data source to + **/ + void connectAllSources(QObject* viualization) const; + /** * Gets the Data associated with a data source. * @@ -174,7 +187,17 @@ class PLASMA_EXPORT DataEngine : public QObject **/ void addSource(DataSource* source); -/* void createDataSource(const QString& source, + /** + * Sets an upper limit on the number of data sources to keep in this engine. + * If the limit is exceeded, then the oldest data source, as defined by last + * update, is dropped. + * + * @param limit the maximum number of sources to keep active + **/ + void setSourceLimit(uint limit); + +/* DataSource* domain(const QString &domain); + void createDataSource(const QString& source, const QString& domain = QString());*/ /**