make all calls to scheduleSourcesUpdate async so engines that use that internally don't incure a time penalty for doing so

svn path=/trunk/KDE/kdelibs/; revision=965473
This commit is contained in:
Aaron J. Seigo 2009-05-09 00:32:45 +00:00
parent e7349f709f
commit 230a162b94
2 changed files with 41 additions and 45 deletions

View File

@ -45,14 +45,12 @@ DataEngine::DataEngine(QObject *parent, KService::Ptr service)
: QObject(parent), : QObject(parent),
d(new DataEnginePrivate(this, service)) d(new DataEnginePrivate(this, service))
{ {
connect(d->updateTimer, SIGNAL(timeout()), this, SLOT(scheduleSourcesUpdated()));
} }
DataEngine::DataEngine(QObject *parent, const QVariantList &args) DataEngine::DataEngine(QObject *parent, const QVariantList &args)
: QObject(parent), : QObject(parent),
d(new DataEnginePrivate(this, KService::serviceByStorageId(args.count() > 0 ? args[0].toString() : QString()))) d(new DataEnginePrivate(this, KService::serviceByStorageId(args.count() > 0 ? args[0].toString() : QString())))
{ {
connect(d->updateTimer, SIGNAL(timeout()), this, SLOT(scheduleSourcesUpdated()));
} }
DataEngine::~DataEngine() DataEngine::~DataEngine()
@ -129,8 +127,9 @@ DataEngine::Data DataEngine::query(const QString &source) const
return DataEngine::Data(); return DataEngine::Data();
} else if (!newSource && d->minPollingInterval >= 0 && } else if (!newSource && d->minPollingInterval >= 0 &&
s->timeSinceLastUpdate() >= uint(d->minPollingInterval)) { s->timeSinceLastUpdate() >= uint(d->minPollingInterval)) {
if (const_cast<DataEngine*>(this)->updateSourceEvent(source)) { DataEngine *unconstThis = const_cast<DataEngine*>(this);
d->queueUpdate(); if (unconstThis->updateSourceEvent(source)) {
unconstThis->scheduleSourcesUpdated();
} }
} }
@ -189,7 +188,7 @@ void DataEngine::setData(const QString &source, const QString &key, const QVaria
emit sourceAdded(source); emit sourceAdded(source);
} }
d->queueUpdate(); scheduleSourcesUpdated();
} }
void DataEngine::setData(const QString &source, const Data &data) void DataEngine::setData(const QString &source, const Data &data)
@ -211,7 +210,7 @@ void DataEngine::setData(const QString &source, const Data &data)
emit sourceAdded(source); emit sourceAdded(source);
} }
d->queueUpdate(); scheduleSourcesUpdated();
} }
void DataEngine::removeAllData(const QString &source) void DataEngine::removeAllData(const QString &source)
@ -219,7 +218,7 @@ void DataEngine::removeAllData(const QString &source)
DataContainer *s = d->source(source, false); DataContainer *s = d->source(source, false);
if (s) { if (s) {
s->removeAllData(); s->removeAllData();
d->queueUpdate(); scheduleSourcesUpdated();
} }
} }
@ -228,7 +227,7 @@ void DataEngine::removeData(const QString &source, const QString &key)
DataContainer *s = d->source(source, false); DataContainer *s = d->source(source, false);
if (s) { if (s) {
s->setData(key, QVariant()); s->setData(key, QVariant());
d->queueUpdate(); scheduleSourcesUpdated();
} }
} }
@ -243,7 +242,7 @@ void DataEngine::addSource(DataContainer *source)
this, SLOT(internalUpdateSource(DataContainer*))); this, SLOT(internalUpdateSource(DataContainer*)));
d->sources.insert(source->objectName(), source); d->sources.insert(source->objectName(), source);
emit sourceAdded(source->objectName()); emit sourceAdded(source->objectName());
d->queueUpdate(); scheduleSourcesUpdated();
} }
void DataEngine::setMaxSourceCount(uint limit) void DataEngine::setMaxSourceCount(uint limit)
@ -359,27 +358,34 @@ DataEngine::SourceDict DataEngine::containerDict() const
void DataEngine::timerEvent(QTimerEvent *event) void DataEngine::timerEvent(QTimerEvent *event)
{ {
if (event->timerId() != d->updateTimerId) { if (event->timerId() == d->updateTimerId) {
// if the freq update is less than 0, don't bother
if (d->minPollingInterval < 0) {
//kDebug() << "uh oh.. no polling allowed!";
return;
}
// minPollingInterval
if (d->updateTimestamp.elapsed() < d->minPollingInterval) {
//kDebug() << "hey now.. slow down!";
return;
}
d->updateTimestamp.restart();
d->updateTimerId = 0;
updateAllSources();
} else if (event->timerId() == d->checkSourcesTimerId) {
QHashIterator<QString, Plasma::DataContainer*> it(d->sources);
while (it.hasNext()) {
it.next();
it.value()->checkForUpdate();
}
killTimer(d->checkSourcesTimerId);
d->checkSourcesTimerId = 0;
} else {
QObject::timerEvent(event); QObject::timerEvent(event);
return;
} }
event->accept();
// if the freq update is less than 0, don't bother
if (d->minPollingInterval < 0) {
//kDebug() << "uh oh.. no polling allowed!";
return;
}
// minPollingInterval
if (d->updateTimestamp.elapsed() < d->minPollingInterval) {
//kDebug() << "hey now.. slow down!";
return;
}
d->updateTimestamp.restart();
updateAllSources();
} }
void DataEngine::updateAllSources() void DataEngine::updateAllSources()
@ -420,11 +426,11 @@ const Package *DataEngine::package() const
void DataEngine::scheduleSourcesUpdated() void DataEngine::scheduleSourcesUpdated()
{ {
QHashIterator<QString, Plasma::DataContainer*> it(d->sources); if (d->checkSourcesTimerId) {
while (it.hasNext()) { return;
it.next();
it.value()->checkForUpdate();
} }
d->checkSourcesTimerId = startTimer(0);
} }
QString DataEngine::name() const QString DataEngine::name() const
@ -443,6 +449,7 @@ DataEnginePrivate::DataEnginePrivate(DataEngine *e, KService::Ptr service)
: q(e), : q(e),
dataEngineDescription(service), dataEngineDescription(service),
refCount(-1), // first ref refCount(-1), // first ref
checkSourcesTimerId(0),
updateTimerId(0), updateTimerId(0),
minPollingInterval(-1), minPollingInterval(-1),
limit(0), limit(0),
@ -450,8 +457,6 @@ DataEnginePrivate::DataEnginePrivate(DataEngine *e, KService::Ptr service)
script(0), script(0),
package(0) package(0)
{ {
updateTimer = new QTimer(q);
updateTimer->setSingleShot(true);
updateTimestamp.start(); updateTimestamp.start();
if (!service) { if (!service) {
@ -512,7 +517,7 @@ void DataEnginePrivate::internalUpdateSource(DataContainer *source)
if (q->updateSourceEvent(source->objectName())) { if (q->updateSourceEvent(source->objectName())) {
//kDebug() << "queuing an update"; //kDebug() << "queuing an update";
queueUpdate(); q->scheduleSourcesUpdated();
}/* else { }/* else {
kDebug() << "no update"; kDebug() << "no update";
}*/ }*/
@ -643,14 +648,6 @@ void DataEnginePrivate::trimQueue()
} }
} }
void DataEnginePrivate::queueUpdate()
{
if (updateTimer->isActive()) {
return;
}
updateTimer->start(0);
}
} }
#include "dataengine.moc" #include "dataengine.moc"

View File

@ -40,7 +40,6 @@ class DataEnginePrivate
Plasma::IntervalAlignment align, bool immediateCall = true); Plasma::IntervalAlignment align, bool immediateCall = true);
DataContainer *requestSource(const QString &sourceName, bool *newSource = 0); DataContainer *requestSource(const QString &sourceName, bool *newSource = 0);
void trimQueue(); void trimQueue();
void queueUpdate();
void internalUpdateSource(DataContainer*); void internalUpdateSource(DataContainer*);
/** /**
@ -65,12 +64,12 @@ class DataEnginePrivate
DataEngine *q; DataEngine *q;
KPluginInfo dataEngineDescription; KPluginInfo dataEngineDescription;
int refCount; int refCount;
int checkSourcesTimerId;
int updateTimerId; int updateTimerId;
int minPollingInterval; int minPollingInterval;
QTime updateTimestamp; QTime updateTimestamp;
DataEngine::SourceDict sources; DataEngine::SourceDict sources;
QQueue<DataContainer*> sourceQueue; QQueue<DataContainer*> sourceQueue;
QTimer *updateTimer;
QString icon; QString icon;
uint limit; uint limit;
bool valid; bool valid;