* introduce a NullEngine

* return an instance of the NullEngine on engine loading failure; this way applets don't have to test for engine existence and can just assume something gets returned
* DataEngine::isValid() in case someone really cares to know whether they have a non-functional engine (e.g. the NullEngine)

svn path=/trunk/KDE/kdebase/workspace/lib/plasma/; revision=670900
This commit is contained in:
Aaron J. Seigo 2007-06-02 22:23:26 +00:00
parent bbaebc6b1b
commit 2638b6031b
4 changed files with 57 additions and 6 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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<Plasma::DataEngine>(offers.first(), 0);
if (!engine) {
kDebug() << "Couldn't load engine \"" << name << "\"!" << endl;
return 0;
engine = d->nullEngine();
}
engine->ref();

View File

@ -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);