diff --git a/datacontainer.cpp b/datacontainer.cpp index 2dc8e1910..afc93b2dd 100644 --- a/datacontainer.cpp +++ b/datacontainer.cpp @@ -190,13 +190,19 @@ void DataContainer::store() if (!needsToBeStored()){ return; } + DataEngine* de = getDataEngine(); if (de == NULL) { return; } + setNeedsToBeStored(false); - Storage* store = new Storage(de->name(), 0); - KConfigGroup op = store->operationDescription("save"); + + if (d->store == NULL) { + d->store = new Storage(de->name(), 0); + } + + KConfigGroup op = d->store->operationDescription("save"); op.writeEntry("source", objectName()); DataEngine::Data dataToStore = data(); DataEngine::Data::const_iterator it = dataToStore.constBegin(); @@ -213,13 +219,21 @@ void DataContainer::store() op.writeEntry("data", b.toBase64()); } ++it; - ServiceJob* job = store->startOperationCall(op); - job->start(); + if (d->store == NULL) { + d->store = new Storage(de->name(), 0); + } + ServiceJob* job = d->store->startOperationCall(op); + d->storeCount++; + connect(job, "finished(KJob*)", this, "storeJobFinished(KJob*)"); } +} - //FIXME: this may result in the service being deleted before the jobs ... resulting in the - //jobs potentially being terminated prematurely - store->deleteLater(); +void DataContainer::storeJobFinished(KJob* job) +{ + d->storeCount--; + if (d->storeCount == 0) { + d->store->deleteLater(); + } } void DataContainer::retrieve() diff --git a/datacontainer.h b/datacontainer.h index 5eb123254..7a6772e19 100644 --- a/datacontainer.h +++ b/datacontainer.h @@ -259,6 +259,12 @@ class PLASMA_EXPORT DataContainer : public QObject */ void populateFromStoredData(KJob *job); + /** + * Deletes the store member of DataContainerPrivate if + * there are no more references to it. + */ + void storeJobFinished(KJob *job); + private: friend class SignalRelay; DataContainerPrivate *const d; diff --git a/private/datacontainer_p.h b/private/datacontainer_p.h index 26f923304..297a48bfc 100644 --- a/private/datacontainer_p.h +++ b/private/datacontainer_p.h @@ -23,6 +23,7 @@ #include #include #include "servicejob.h" +#include "storage_p.h" namespace Plasma { @@ -36,7 +37,9 @@ public: : dirty(false), cached(false), enableStorage(false), - isStored(true) + isStored(true), + store(NULL), + storeCount(0) {} SignalRelay *signalRelay(const DataContainer *dc, QObject *visualization, @@ -50,10 +53,12 @@ public: QMap relayObjects; QMap relays; QTime updateTs; + Storage* store; bool dirty : 1; bool cached : 1; bool enableStorage : 1; bool isStored : 1; + int storeCount; }; class SignalRelay : public QObject