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
This commit is contained in:
Aaron J. Seigo 2007-06-10 04:03:50 +00:00
parent b00436fbe1
commit b86f3f6209
3 changed files with 57 additions and 11 deletions

View File

@ -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<DataSource*> 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"

View File

@ -55,6 +55,7 @@ class PLASMA_EXPORT DataEngine : public QObject
typedef QHash<QString, DataEngine*> Dict;
typedef QHash<QString, QVariant> Data;
typedef QHashIterator<QString, QVariant> DataIterator;
typedef QHash<QString, DataSource*> 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:

View File

@ -43,8 +43,7 @@ class PLASMA_EXPORT DataSource : public QObject
Q_OBJECT
public:
typedef QHash<QString, DataSource*> Dict;
typedef QHash<QString, Dict> Grouping;
//typedef QHash<QString, DataEngine::SourceDict> Grouping;
/**
* Constructs a default DataSource, which has no name or data