diff --git a/dataengine.cpp b/dataengine.cpp index b83fc434a..81395add1 100644 --- a/dataengine.cpp +++ b/dataengine.cpp @@ -34,7 +34,8 @@ class DataEngine::Private public: Private(DataEngine* e) : engine(e), - limit(0) + limit(0), + valid(true) { updateTimer = new QTimer(engine); updateTimer->setSingleShot(true); @@ -97,6 +98,7 @@ class DataEngine::Private QTimer* updateTimer; QString icon; uint limit; + bool valid; }; @@ -111,6 +113,7 @@ DataEngine::DataEngine(QObject* parent) DataEngine::~DataEngine() { + //kDebug() << objectName() << ": bye bye birdy! " << endl; delete d; } @@ -244,6 +247,16 @@ bool DataEngine::isUsed() const return d->ref != 0; } +bool DataEngine::isValid() const +{ + return d->valid; +} + +bool DataEngine::setValid(bool valid) +{ + d->valid = valid; +} + void DataEngine::setIcon(const QString& icon) { d->icon = icon; diff --git a/dataengine.h b/dataengine.h index 738517caf..76655d87d 100644 --- a/dataengine.h +++ b/dataengine.h @@ -128,6 +128,11 @@ class PLASMA_EXPORT DataEngine : public QObject **/ bool isUsed() const; + /** + * Returns true if this engine is valid, otherwise returns false + **/ + bool isValid() const; + /** * Sets the icon for this data engine **/ @@ -211,6 +216,15 @@ class PLASMA_EXPORT DataEngine : public QObject **/ void clearAllDataSources(); + /** + * Sets whether or not this engine is valid, e.g. can be used. + * In practice, only the internal fall-back engine, the NullEngine + * should have need for this. + * + * @param valid whether or not the engine is valid + **/ + bool setValid(bool valid); + private Q_SLOTS: void checkForUpdates(); diff --git a/dataenginemanager.cpp b/dataenginemanager.cpp index bb6bc4add..aa73390d4 100644 --- a/dataenginemanager.cpp +++ b/dataenginemanager.cpp @@ -25,14 +25,37 @@ namespace Plasma { +class NullEngine : public DataEngine +{ + public: + NullEngine(QObject* parent = 0) + : DataEngine(parent) + { + setValid(false); + } +}; + class DataEngineManager::Private { public: Private() + : null(0) {} - Plasma::DataEngine::Dict m_engines; + ~Private() + { + delete null; + } + DataEngine* nullEngine() + { + if (!null) { + null = new NullEngine; + } + } + + DataEngine::Dict m_engines; + DataEngine* null; }; class DataEngineManagerSingleton @@ -70,7 +93,7 @@ Plasma::DataEngine* DataEngineManager::dataEngine(const QString& name) const return *it; } - return 0; + return d->nullEngine(); } Plasma::DataEngine* DataEngineManager::loadDataEngine(const QString& name) @@ -95,7 +118,7 @@ Plasma::DataEngine* DataEngineManager::loadDataEngine(const QString& name) engine = KService::createInstance(offers.first(), 0); if (!engine) { kDebug() << "Couldn't load engine \"" << name << "\"!" << endl; - return 0; + engine = d->nullEngine(); } engine->ref(); diff --git a/dataenginemanager.h b/dataenginemanager.h index c3232eae0..bb8d0af23 100644 --- a/dataenginemanager.h +++ b/dataenginemanager.h @@ -48,7 +48,8 @@ class PLASMA_EXPORT DataEngineManager /** * Returns a data engine object if one is loaded and available. - * Otherwise, returns 0. + * On failure, the fallback NullEngine (which does nothing and + * !isValid()) is returned. * * @param name the name of the engine */ @@ -61,7 +62,7 @@ class PLASMA_EXPORT DataEngineManager * value cached. Call unloadDataEngine when finished with the engine. * * @param name the name of the engine - * @return the data engine that was loaded, or 0 on failure. + * @return the data engine that was loaded, or the NullEngine on failure. */ Plasma::DataEngine* loadDataEngine(const QString& name);