summaryrefslogtreecommitdiffstats
path: root/examples/dummyresource/resourcefactory.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-12-30 19:05:12 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-12-30 19:05:12 +0100
commit90052518d801195bbcbcecbe1fcf6594937cf0cb (patch)
tree037bf38e5359b43afae5f04a6b592ee6cea80062 /examples/dummyresource/resourcefactory.cpp
parentecc2a18e1afc1b99df6725066c9ae552d09e90d8 (diff)
downloadsink-90052518d801195bbcbcecbe1fcf6594937cf0cb.tar.gz
sink-90052518d801195bbcbcecbe1fcf6594937cf0cb.zip
Use the existing synchronization facilities also in the DummyResource
Diffstat (limited to 'examples/dummyresource/resourcefactory.cpp')
-rw-r--r--examples/dummyresource/resourcefactory.cpp120
1 files changed, 39 insertions, 81 deletions
diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp
index 08e3594..a984097 100644
--- a/examples/dummyresource/resourcefactory.cpp
+++ b/examples/dummyresource/resourcefactory.cpp
@@ -57,110 +57,68 @@ DummyResource::DummyResource(const QByteArray &instanceIdentifier, const QShared
57 QVector<Akonadi2::Preprocessor*>() << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Event>); 57 QVector<Akonadi2::Preprocessor*>() << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Event>);
58} 58}
59 59
60void DummyResource::createEvent(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb, Akonadi2::Storage::Transaction &transaction) 60Akonadi2::ApplicationDomain::Event::Ptr DummyResource::createEvent(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, Akonadi2::Storage::Transaction &transaction)
61{ 61{
62 static uint8_t rawData[100]; 62 static uint8_t rawData[100];
63 Akonadi2::ApplicationDomain::Event event; 63 auto event = Akonadi2::ApplicationDomain::Event::Ptr::create();
64 event.setProperty("summary", data.value("summary").toString()); 64 event->setProperty("summary", data.value("summary").toString());
65 event.setProperty("remoteId", ridBuffer); 65 event->setProperty("remoteId", ridBuffer);
66 event.setProperty("description", data.value("description").toString()); 66 event->setProperty("description", data.value("description").toString());
67 event.setProperty("attachment", QByteArray::fromRawData(reinterpret_cast<const char*>(rawData), 100)); 67 event->setProperty("attachment", QByteArray::fromRawData(reinterpret_cast<const char*>(rawData), 100));
68 mEventAdaptorFactory->createBuffer(event, entityFbb); 68 return event;
69} 69}
70 70
71QString DummyResource::resolveRemoteId(const QByteArray &bufferType, const QString &remoteId, Akonadi2::Storage::Transaction &transaction) 71Akonadi2::ApplicationDomain::Mail::Ptr DummyResource::createMail(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, Akonadi2::Storage::Transaction &transaction)
72{ 72{
73 //Lookup local id for remote id, or insert a new pair otherwise 73 auto mail = Akonadi2::ApplicationDomain::Mail::Ptr::create();
74 auto remoteIdWithType = bufferType + remoteId.toUtf8(); 74 mail->setProperty("subject", data.value("subject").toString());
75 QByteArray akonadiId = Index("rid.mapping", transaction).lookup(remoteIdWithType); 75 mail->setProperty("senderEmail", data.value("senderEmail").toString());
76 if (akonadiId.isEmpty()) { 76 mail->setProperty("senderName", data.value("senderName").toString());
77 akonadiId = QUuid::createUuid().toString().toUtf8(); 77 mail->setProperty("date", data.value("date").toString());
78 Index("rid.mapping", transaction).add(remoteIdWithType, akonadiId); 78 mail->setProperty("folder", resolveRemoteId(ENTITY_TYPE_FOLDER, data.value("parentFolder").toByteArray(), transaction));
79 } 79 mail->setProperty("unread", data.value("unread").toBool());
80 return akonadiId; 80 mail->setProperty("important", data.value("important").toBool());
81 return mail;
81} 82}
82 83
83 84Akonadi2::ApplicationDomain::Folder::Ptr DummyResource::createFolder(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, Akonadi2::Storage::Transaction &transaction)
84void DummyResource::createMail(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb, Akonadi2::Storage::Transaction &transaction)
85{ 85{
86 Akonadi2::ApplicationDomain::Mail mail; 86 auto folder = Akonadi2::ApplicationDomain::Folder::Ptr::create();
87 mail.setProperty("subject", data.value("subject").toString()); 87 folder->setProperty("name", data.value("name").toString());
88 mail.setProperty("senderEmail", data.value("senderEmail").toString()); 88 folder->setProperty("icon", data.value("icon").toString());
89 mail.setProperty("senderName", data.value("senderName").toString());
90 mail.setProperty("date", data.value("date").toString());
91 mail.setProperty("folder", resolveRemoteId(ENTITY_TYPE_FOLDER, data.value("parentFolder").toString(), transaction));
92 mail.setProperty("unread", data.value("unread").toBool());
93 mail.setProperty("important", data.value("important").toBool());
94 mMailAdaptorFactory->createBuffer(mail, entityFbb);
95}
96
97void DummyResource::createFolder(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb, Akonadi2::Storage::Transaction &transaction)
98{
99 Akonadi2::ApplicationDomain::Folder folder;
100 folder.setProperty("name", data.value("name").toString());
101 folder.setProperty("icon", data.value("icon").toString());
102 if (!data.value("parent").toString().isEmpty()) { 89 if (!data.value("parent").toString().isEmpty()) {
103 auto akonadiId = resolveRemoteId(ENTITY_TYPE_FOLDER, data.value("parent").toString(), transaction); 90 auto akonadiId = resolveRemoteId(ENTITY_TYPE_FOLDER, data.value("parent").toByteArray(), transaction);
104 folder.setProperty("parent", akonadiId); 91 folder->setProperty("parent", akonadiId);
105 } 92 }
106 mFolderAdaptorFactory->createBuffer(folder, entityFbb); 93 return folder;
107} 94}
108 95
109void DummyResource::synchronize(const QString &bufferType, const QMap<QString, QMap<QString, QVariant> > &data, Akonadi2::Storage::Transaction &transaction, std::function<void(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb, Akonadi2::Storage::Transaction &)> createEntity) 96void DummyResource::synchronize(const QByteArray &bufferType, const QMap<QString, QMap<QString, QVariant> > &data, Akonadi2::Storage::Transaction &transaction, Akonadi2::Storage::Transaction &synchronizationTransaction, DomainTypeAdaptorFactoryInterface &adaptorFactory, std::function<Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, Akonadi2::Storage::Transaction &)> createEntity)
110{ 97{
111 Akonadi2::Storage store(Akonadi2::storageLocation(), mResourceInstanceIdentifier + ".synchronization", Akonadi2::Storage::ReadWrite); 98 //TODO find items to remove
112 auto synchronizationTransaction = store.createTransaction(Akonadi2::Storage::ReadWrite);
113 Index ridMapping("rid.mapping", synchronizationTransaction);
114 for (auto it = data.constBegin(); it != data.constEnd(); it++) { 99 for (auto it = data.constBegin(); it != data.constEnd(); it++) {
115 const auto remoteId = it.key().toUtf8(); 100 const auto remoteId = it.key().toUtf8();
116 auto akonadiId = resolveRemoteId(bufferType.toUtf8(), remoteId, synchronizationTransaction); 101 auto entity = createEntity(remoteId, it.value(), synchronizationTransaction);
117 102 createOrModify(transaction, synchronizationTransaction, adaptorFactory, bufferType, remoteId, *entity);
118 bool found = false;
119 transaction.openDatabase(bufferType.toUtf8() + ".main").scan(akonadiId.toUtf8(), [&found](const QByteArray &, const QByteArray &) -> bool {
120 found = true;
121 return false;
122 }, [this](const Akonadi2::Storage::Error &error) {
123 }, true);
124
125 if (!found) { //A new entity
126 flatbuffers::FlatBufferBuilder entityFbb;
127 createEntity(remoteId, it.value(), entityFbb, synchronizationTransaction);
128
129 flatbuffers::FlatBufferBuilder fbb;
130 //This is the resource type and not the domain type
131 auto entityId = fbb.CreateString(akonadiId.toStdString());
132 auto type = fbb.CreateString(bufferType.toStdString());
133 auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize());
134 auto location = Akonadi2::Commands::CreateCreateEntity(fbb, entityId, type, delta);
135 Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location);
136
137 Trace() << "Found a new entity: " << remoteId;
138 enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize()));
139 } else { //modification
140 Trace() << "Found a modified entity: " << remoteId;
141 //TODO diff and create modification if necessary
142 }
143 } 103 }
144 //TODO find items to remove
145} 104}
146 105
147KAsync::Job<void> DummyResource::synchronizeWithSource() 106KAsync::Job<void> DummyResource::synchronizeWithSource(Akonadi2::Storage &mainStore, Akonadi2::Storage &synchronizationStore)
148{ 107{
149 Log() << " Synchronizing"; 108 Log() << " Synchronizing";
150 return KAsync::start<void>([this](KAsync::Future<void> &f) { 109 return KAsync::start<void>([this, &mainStore, &synchronizationStore]() {
151 auto transaction = Akonadi2::Storage(Akonadi2::storageLocation(), mResourceInstanceIdentifier, Akonadi2::Storage::ReadOnly).createTransaction(Akonadi2::Storage::ReadOnly); 110 auto transaction = mainStore.createTransaction(Akonadi2::Storage::ReadOnly);
152 111 auto synchronizationTransaction = synchronizationStore.createTransaction(Akonadi2::Storage::ReadWrite);
153 synchronize(ENTITY_TYPE_EVENT, DummyStore::instance().events(), transaction, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb, Akonadi2::Storage::Transaction &synchronizationTransaction) { 112 synchronize(ENTITY_TYPE_EVENT, DummyStore::instance().events(), transaction, synchronizationTransaction, *mEventAdaptorFactory, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, Akonadi2::Storage::Transaction &synchronizationTransaction) {
154 createEvent(ridBuffer, data, entityFbb, synchronizationTransaction); 113 return createEvent(ridBuffer, data, synchronizationTransaction);
155 }); 114 });
156 synchronize(ENTITY_TYPE_MAIL, DummyStore::instance().mails(), transaction, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb, Akonadi2::Storage::Transaction &synchronizationTransaction) { 115 synchronize(ENTITY_TYPE_MAIL, DummyStore::instance().mails(), transaction, synchronizationTransaction, *mMailAdaptorFactory, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, Akonadi2::Storage::Transaction &synchronizationTransaction) {
157 createMail(ridBuffer, data, entityFbb, synchronizationTransaction); 116 return createMail(ridBuffer, data, synchronizationTransaction);
158 }); 117 });
159 synchronize(ENTITY_TYPE_FOLDER, DummyStore::instance().folders(), transaction, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb, Akonadi2::Storage::Transaction &synchronizationTransaction) { 118 synchronize(ENTITY_TYPE_FOLDER, DummyStore::instance().folders(), transaction, synchronizationTransaction, *mFolderAdaptorFactory, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, Akonadi2::Storage::Transaction &synchronizationTransaction) {
160 createFolder(ridBuffer, data, entityFbb, synchronizationTransaction); 119 return createFolder(ridBuffer, data, synchronizationTransaction);
161 }); 120 });
162 121 Log() << "Done Synchronizing";
163 f.setFinished();
164 }); 122 });
165} 123}
166 124