Added akonadi storage plugin loading.

svn path=/trunk/KDE/kdelibs/; revision=1164453
This commit is contained in:
Brian David Pritchett 2010-08-16 19:02:03 +00:00
parent a2d0e31e23
commit 4fdf87b54f
2 changed files with 48 additions and 38 deletions

View File

@ -19,6 +19,7 @@
#include "datacontainer.h" #include "datacontainer.h"
#include "private/datacontainer_p.h" #include "private/datacontainer_p.h"
#include "private/storage_p.h" #include "private/storage_p.h"
#include "pluginloader.h"
#include <kdebug.h> #include <kdebug.h>
@ -198,36 +199,43 @@ void DataContainer::store()
setNeedsToBeStored(false); setNeedsToBeStored(false);
if (d->store == NULL) { if (!d->store) {
d->store = new Storage(de->name(), 0); QVariantList args;
args.insert(0, de->name());
d->store = PluginLoader::pluginLoader()->loadService("akonadi_storage_plugin", args, 0);
if (!d->store) {
d->store = new Storage(de->name(), 0);
}
} }
KConfigGroup op = d->store->operationDescription("save"); KConfigGroup op = d->store->operationDescription("save");
op.writeEntry("source", objectName()); op.writeEntry("source", objectName());
DataEngine::Data dataToStore = data(); DataEngine::Data dataToStore = data();
DataEngine::Data::const_iterator it = dataToStore.constBegin(); DataEngine::Data::const_iterator it = dataToStore.constBegin();
while (it != dataToStore.constEnd() && dataToStore.constEnd() == data().constEnd()) { while (it != dataToStore.constEnd() && dataToStore.constEnd() == data().constEnd()) {
QVariant v = it.value(); QVariant v = it.value();
if ((it.value().type() == QVariant::String) || (it.value().type() == QVariant::Int)) { QByteArray b;
op.writeEntry("key", it.key()); QDataStream ds(&b, QIODevice::WriteOnly);
op.writeEntry("data", it.value()); ds << it.value();
} else { op.writeEntry("key", it.key());
QByteArray b; op.writeEntry("data", b.toBase64());
QDataStream ds(&b, QIODevice::WriteOnly); }
ds << it.value(); ++it;
op.writeEntry("key", "base64-" + it.key()); if (!d->store) {
op.writeEntry("data", b.toBase64()); QVariantList args;
} args.insert(0, de->name());
++it; d->store = PluginLoader::pluginLoader()->loadService("plasma_storage_akonadi", args, 0);
if (d->store == NULL) { if (!d->store) {
d->store = new Storage(de->name(), 0); d->store = new Storage(de->name(), 0);
} }
ServiceJob* job = d->store->startOperationCall(op);
d->storeCount++;
connect(job, SIGNAL(finished(KJob*)), this, SLOT(storeJobFinished(KJob*)));
} }
ServiceJob* job = d->store->startOperationCall(op);
d->storeCount++;
connect(job, SIGNAL(finished(KJob*)), this, SLOT(storeJobFinished(KJob*)));
} }
void DataContainerPrivate::storeJobFinished(KJob* ) void DataContainerPrivate::storeJobFinished(KJob* )
{ {
--storeCount; --storeCount;
@ -243,10 +251,17 @@ void DataContainer::retrieve()
if (de == NULL) { if (de == NULL) {
return; return;
} }
Storage* store = new Storage(de->name(), 0); if (!d->store) {
KConfigGroup retrieveGroup = store->operationDescription("retrieve"); QVariantList args;
args.insert(0, de->name());
d->store = PluginLoader::pluginLoader()->loadService("plasma_storage_akonadi", args, 0);
if (!d->store) {
d->store = new Storage(de->name(), 0);
}
}
KConfigGroup retrieveGroup = d->store->operationDescription("retrieve");
retrieveGroup.writeEntry("source", objectName()); retrieveGroup.writeEntry("source", objectName());
ServiceJob* retrieveJob = store->startOperationCall(retrieveGroup); ServiceJob* retrieveJob = d->store->startOperationCall(retrieveGroup);
connect(retrieveJob, SIGNAL(result(KJob*)), this, connect(retrieveJob, SIGNAL(result(KJob*)), this,
SLOT(populateFromStoredData(KJob*))); SLOT(populateFromStoredData(KJob*)));
} }
@ -261,27 +276,22 @@ void DataContainerPrivate::populateFromStoredData(KJob *job)
ServiceJob* ret = dynamic_cast<ServiceJob*>(job); ServiceJob* ret = dynamic_cast<ServiceJob*>(job);
QHash<QString, QVariant> h = ret->result().toHash(); QHash<QString, QVariant> h = ret->result().toHash();
foreach (QString key, h.keys()) { foreach (QString key, h.keys()) {
if (key.startsWith("base64-")) { QByteArray b = QByteArray::fromBase64(h[key].toString().toAscii());
QByteArray b = QByteArray::fromBase64(h[key].toString().toAscii()); QDataStream ds(&b, QIODevice::ReadOnly);
QDataStream ds(&b, QIODevice::ReadOnly); QVariant v(ds);
QVariant v(ds); key.remove(0, 7);
key.remove(0, 7); dataToInsert.insert(key, v);
dataToInsert.insert(key, v);
} else {
dataToInsert.insert(key, h[key]);
}
} }
if (!(data.isEmpty())) //Do not fill the source with old stored
//data if it is already populated with new data.
if (data.isEmpty())
{ {
//Do not fill the source with old stored data = dataToInsert;
//data if it is already populated with new data. // dirty = true;
return; // q->checkForUpdate();
} }
data = dataToInsert;
dirty = true;
q->checkForUpdate();
} }
void DataContainer::disconnectVisualization(QObject *visualization) void DataContainer::disconnectVisualization(QObject *visualization)

View File

@ -35,7 +35,7 @@ class DataContainerPrivate
public: public:
DataContainerPrivate(DataContainer *container) DataContainerPrivate(DataContainer *container)
: q(container), : q(container),
store(NULL), store(0),
dirty(false), dirty(false),
cached(false), cached(false),
enableStorage(false), enableStorage(false),
@ -66,7 +66,7 @@ public:
QMap<QObject *, SignalRelay *> relayObjects; QMap<QObject *, SignalRelay *> relayObjects;
QMap<uint, SignalRelay *> relays; QMap<uint, SignalRelay *> relays;
QTime updateTs; QTime updateTs;
Storage* store; Service* store;
bool dirty : 1; bool dirty : 1;
bool cached : 1; bool cached : 1;
bool enableStorage : 1; bool enableStorage : 1;