From b86f3f62096a0ce2899fa53849b20fb751544f93 Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Sun, 10 Jun 2007 04:03:50 +0000 Subject: [PATCH] when a data source is requested but does not exist, allow the engine the opportunity to add it by reimplementing dataSourceRequested(const QString&). this makes it trivial to add timezones to the time engine, rss feeds to an rss engine, weather stations to a weather engine or hardware data to solid engines. svn path=/trunk/KDE/kdebase/workspace/lib/plasma/; revision=673373 --- dataengine.cpp | 42 +++++++++++++++++++++++++++++++++++------- dataengine.h | 23 +++++++++++++++++++++-- datasource.h | 3 +-- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/dataengine.cpp b/dataengine.cpp index 8286a7bc4..93acccd21 100644 --- a/dataengine.cpp +++ b/dataengine.cpp @@ -41,9 +41,9 @@ class DataEngine::Private updateTimer->setSingleShot(true); } - DataSource* source(const QString& sourceName) + DataSource* source(const QString& sourceName, bool createWhenMissing = true) { - DataSource::Dict::const_iterator it = sources.find(sourceName); + DataEngine::SourceDict::const_iterator it = sources.find(sourceName); if (it != sources.constEnd()) { DataSource* s = it.value(); if (limit > 0) { @@ -60,6 +60,10 @@ class DataEngine::Private return it.value(); } + if (!createWhenMissing) { + return 0; + } + kDebug() << "DataEngine " << engine->objectName() << ": could not find DataSource " << sourceName << ", creating" << endl; @@ -91,8 +95,14 @@ class DataEngine::Private updateTimer->start(0); } + bool dataSourceRequested(const QString& source) + { + //get around const! =P + return engine->dataSourceRequested(source); + } + QAtomic ref; - DataSource::Dict sources; + DataEngine::SourceDict sources; QQueue sourceQueue; DataEngine* engine; QTimer* updateTimer; @@ -124,7 +134,15 @@ QStringList DataEngine::dataSources() const void DataEngine::connectSource(const QString& source, QObject* visualization) const { - DataSource* s = d->source(source); + DataSource* s = d->source(source, false); + + if (!s) { + kDebug() << "requesting source " << source << endl; + d->dataSourceRequested(source); + s = d->source(source); + kDebug() << "and no we have " << s << endl; + } + connect(s, SIGNAL(updated(QString,Plasma::DataEngine::Data)), visualization, SLOT(updated(QString,Plasma::DataEngine::Data))); QMetaObject::invokeMethod(visualization, SLOT(updated(QString,Plasma::DataEngine::Data)), @@ -161,6 +179,12 @@ void DataEngine::init() // start things in motion external to themselves before they can work } +bool DataEngine::dataSourceRequested(const QString &name) +{ + Q_UNUSED(name) + return false; +} + void DataEngine::setData(const QString& source, const QVariant& value) { setData(source, source, value); @@ -175,7 +199,7 @@ void DataEngine::setData(const QString& source, const QString& key, const QVaria void DataEngine::addSource(DataSource* source) { - DataSource::Dict::const_iterator it = d->sources.find(source->objectName()); + SourceDict::const_iterator it = d->sources.find(source->objectName()); if (it != d->sources.constEnd()) { kDebug() << "source named \"" << source->objectName() << "\" already exists." << endl; return; @@ -214,7 +238,7 @@ Plasma::DataSource* DataEngine::createDataSource(const QString& source, const QS void DataEngine::removeDataSource(const QString& source) { - DataSource::Dict::iterator it = d->sources.find(source); + SourceDict::iterator it = d->sources.find(source); if (it != d->sources.end()) { emit dataSourceRemoved(it.key()); d->sources.erase(it); @@ -257,6 +281,11 @@ void DataEngine::setValid(bool valid) d->valid = valid; } +DataEngine::SourceDict DataEngine::sourceDict() const +{ + return d->sources; +} + void DataEngine::setIcon(const QString& icon) { d->icon = icon; @@ -279,4 +308,3 @@ void DataEngine::checkForUpdates() } #include "dataengine.moc" - diff --git a/dataengine.h b/dataengine.h index e7223158b..04efeff36 100644 --- a/dataengine.h +++ b/dataengine.h @@ -55,6 +55,7 @@ class PLASMA_EXPORT DataEngine : public QObject typedef QHash Dict; typedef QHash Data; typedef QHashIterator DataIterator; + typedef QHash SourceDict; /** * Default constructor. @@ -166,6 +167,15 @@ class PLASMA_EXPORT DataEngine : public QObject **/ virtual void init(); + /** + * When a source that does not currently exist is requested by the + * consumer, this method is called to give the DataEngine the + * opportunity to create one. + * + * @return true if a DataSource was set up, false otherwise + */ + virtual bool dataSourceRequested(const QString &name); + /** * Sets a value for a data source. If the source * doesn't exist then it is created. @@ -173,7 +183,7 @@ class PLASMA_EXPORT DataEngine : public QObject * @param source the name of the data source * @param value the data to associated with the source **/ - void setData(const QString& source, const QVariant& value); + void setData(const QString &source, const QVariant &value); /** * Sets a value for a data source. If the source @@ -225,7 +235,16 @@ class PLASMA_EXPORT DataEngine : public QObject **/ void setValid(bool valid); - private Q_SLOTS: + /** + * @return the list of active DataSources. + */ + SourceDict sourceDict() const; + + protected Q_SLOTS: + /** + * Call this method when you call setData directly on a DataSource. + * If this method is not called, no updated(..) signals will be emitted! + */ void checkForUpdates(); private: diff --git a/datasource.h b/datasource.h index aaa779505..07afdb1fb 100644 --- a/datasource.h +++ b/datasource.h @@ -43,8 +43,7 @@ class PLASMA_EXPORT DataSource : public QObject Q_OBJECT public: - typedef QHash Dict; - typedef QHash Grouping; + //typedef QHash Grouping; /** * Constructs a default DataSource, which has no name or data