delay usage checks so quite disconnect/reconnects don't result in unecessary loss of data

(only to reload it again immediately thereafter...)
This commit is contained in:
Aaron Seigo 2011-10-30 10:35:29 +01:00
parent 97743420a0
commit 176d4b896b
3 changed files with 31 additions and 15 deletions

View File

@ -31,8 +31,6 @@ DataContainer::DataContainer(QObject *parent)
: QObject(parent), : QObject(parent),
d(new DataContainerPrivate(this)) d(new DataContainerPrivate(this))
{ {
d->storageTimer = new QTimer(this);
QObject::connect(d->storageTimer, SIGNAL(timeout()), this, SLOT(store()));
} }
DataContainer::~DataContainer() DataContainer::~DataContainer()
@ -61,7 +59,7 @@ void DataContainer::setData(const QString &key, const QVariant &value)
//setData() since the last time it was stored. This //setData() since the last time it was stored. This
//gives us only one singleShot timer. //gives us only one singleShot timer.
if (isStorageEnabled() || !needsToBeStored()) { if (isStorageEnabled() || !needsToBeStored()) {
d->storageTimer->start(180000); d->storageTimer.start(180000, this);
} }
setNeedsToBeStored(true); setNeedsToBeStored(true);
@ -330,11 +328,22 @@ void DataContainer::setNeedsUpdate(bool update)
void DataContainer::checkUsage() void DataContainer::checkUsage()
{ {
if (d->relays.count() < 1 && if (!d->checkUsageTimer.isActive()) {
receivers(SIGNAL(dataUpdated(QString, Plasma::DataEngine::Data))) < 1) { d->checkUsageTimer.start(10, this);
// DO NOT CALL ANYTHING AFTER THIS LINE AS IT MAY GET DELETED! }
kDebug() << objectName() << "is unused"; }
emit becameUnused(objectName());
void DataContainer::timerEvent(QTimerEvent * event)
{
if (event->timerId() == d->checkUsageTimer.timerId()) {
if (d->relays.count() < 1 &&
receivers(SIGNAL(dataUpdated(QString, Plasma::DataEngine::Data))) < 1) {
// DO NOT CALL ANYTHING AFTER THIS LINE AS IT MAY GET DELETED!
kDebug() << objectName() << "is unused";
emit becameUnused(objectName());
}
} else if (event->timerId() == d->storageTimer.timerId()) {
d->store();
} }
} }

View File

@ -245,6 +245,11 @@ class PLASMA_EXPORT DataContainer : public QObject
**/ **/
void checkUsage(); void checkUsage();
/**
* @reimp from QObject
*/
void timerEvent(QTimerEvent * event);
private: private:
friend class SignalRelay; friend class SignalRelay;
friend class DataContainerPrivate; friend class DataContainerPrivate;
@ -253,7 +258,6 @@ class PLASMA_EXPORT DataContainer : public QObject
Q_PRIVATE_SLOT(d, void storeJobFinished(KJob *job)) Q_PRIVATE_SLOT(d, void storeJobFinished(KJob *job))
Q_PRIVATE_SLOT(d, void populateFromStoredData(KJob *job)) Q_PRIVATE_SLOT(d, void populateFromStoredData(KJob *job))
Q_PRIVATE_SLOT(d, void store())
Q_PRIVATE_SLOT(d, void retrieve()) Q_PRIVATE_SLOT(d, void retrieve())
}; };

View File

@ -20,11 +20,13 @@
#ifndef PLASMA_DATACONTAINER_P_H #ifndef PLASMA_DATACONTAINER_P_H
#define PLASMA_DATACONTAINER_P_H #define PLASMA_DATACONTAINER_P_H
#include <QtCore/QTimerEvent>
#include <QtCore/QTime>
#include "servicejob.h" #include "servicejob.h"
#include "storage_p.h" #include "storage_p.h"
#include <QtCore/QTimerEvent>
#include <QtCore/QTime>
#include <QtCore/QBasicTimer>
class QTimer; class QTimer;
namespace Plasma namespace Plasma
@ -38,11 +40,11 @@ public:
DataContainerPrivate(DataContainer *container) DataContainerPrivate(DataContainer *container)
: q(container), : q(container),
storage(NULL), storage(NULL),
storageCount(0),
dirty(false), dirty(false),
cached(false), cached(false),
enableStorage(false), enableStorage(false),
isStored(true), isStored(true)
storageCount(0)
{ {
} }
@ -71,14 +73,15 @@ public:
DataEngine::Data data; DataEngine::Data data;
QMap<QObject *, SignalRelay *> relayObjects; QMap<QObject *, SignalRelay *> relayObjects;
QMap<uint, SignalRelay *> relays; QMap<uint, SignalRelay *> relays;
QTimer *storageTimer;
QTime updateTs; QTime updateTs;
Storage* storage; Storage* storage;
QBasicTimer storageTimer;
QBasicTimer checkUsageTimer;
int storageCount;
bool dirty : 1; bool dirty : 1;
bool cached : 1; bool cached : 1;
bool enableStorage : 1; bool enableStorage : 1;
bool isStored : 1; bool isStored : 1;
int storageCount;
}; };
class SignalRelay : public QObject class SignalRelay : public QObject