summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/maildirresource/maildirresource.cpp55
1 files changed, 28 insertions, 27 deletions
diff --git a/examples/maildirresource/maildirresource.cpp b/examples/maildirresource/maildirresource.cpp
index 9769444..54a73e9 100644
--- a/examples/maildirresource/maildirresource.cpp
+++ b/examples/maildirresource/maildirresource.cpp
@@ -100,9 +100,23 @@ QStringList MaildirResource::listAvailableFolders()
100 return folderList; 100 return folderList;
101} 101}
102 102
103static void createEntity(const QByteArray &akonadiId, const QByteArray &bufferType, Akonadi2::ApplicationDomain::ApplicationDomainType &domainObject, DomainTypeAdaptorFactoryInterface &adaptorFactory, std::function<void(const QByteArray &)> callback)
104{
105 flatbuffers::FlatBufferBuilder entityFbb;
106 adaptorFactory.createBuffer(domainObject, entityFbb);
107 flatbuffers::FlatBufferBuilder fbb;
108 //This is the resource type and not the domain type
109 auto entityId = fbb.CreateString(akonadiId.toStdString());
110 auto type = fbb.CreateString(bufferType.toStdString());
111 auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize());
112 auto location = Akonadi2::Commands::CreateCreateEntity(fbb, entityId, type, delta);
113 Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location);
114 callback(QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize()));
115}
116
103void MaildirResource::synchronizeFolders(Akonadi2::Storage::Transaction &transaction) 117void MaildirResource::synchronizeFolders(Akonadi2::Storage::Transaction &transaction)
104{ 118{
105 const QString bufferType = ENTITY_TYPE_FOLDER; 119 const QByteArray bufferType = ENTITY_TYPE_FOLDER;
106 QStringList folderList = listAvailableFolders(); 120 QStringList folderList = listAvailableFolders();
107 Trace() << "Found folders " << folderList; 121 Trace() << "Found folders " << folderList;
108 122
@@ -111,10 +125,10 @@ void MaildirResource::synchronizeFolders(Akonadi2::Storage::Transaction &transac
111 for (const auto folder : folderList) { 125 for (const auto folder : folderList) {
112 const auto remoteId = folder.toUtf8(); 126 const auto remoteId = folder.toUtf8();
113 Trace() << "Processing folder " << remoteId; 127 Trace() << "Processing folder " << remoteId;
114 auto akonadiId = resolveRemoteId(bufferType.toUtf8(), remoteId, synchronizationTransaction); 128 auto akonadiId = resolveRemoteId(bufferType, remoteId, synchronizationTransaction);
115 129
116 bool found = false; 130 bool found = false;
117 transaction.openDatabase(bufferType.toUtf8() + ".main").scan(akonadiId.toUtf8(), [&found](const QByteArray &, const QByteArray &) -> bool { 131 transaction.openDatabase(bufferType + ".main").scan(akonadiId.toUtf8(), [&found](const QByteArray &, const QByteArray &) -> bool {
118 found = true; 132 found = true;
119 return false; 133 return false;
120 }, [this](const Akonadi2::Storage::Error &error) { 134 }, [this](const Akonadi2::Storage::Error &error) {
@@ -127,23 +141,16 @@ void MaildirResource::synchronizeFolders(Akonadi2::Storage::Transaction &transac
127 folder.setProperty("name", md.name()); 141 folder.setProperty("name", md.name());
128 folder.setProperty("icon", "folder"); 142 folder.setProperty("icon", "folder");
129 if (!md.isRoot()) { 143 if (!md.isRoot()) {
144 Trace() << "subfolder parent: " << md.parent().path();
130 auto akonadiId = resolveRemoteId(ENTITY_TYPE_FOLDER, md.parent().path(), synchronizationTransaction); 145 auto akonadiId = resolveRemoteId(ENTITY_TYPE_FOLDER, md.parent().path(), synchronizationTransaction);
131 folder.setProperty("parent", akonadiId); 146 folder.setProperty("parent", akonadiId);
132 } 147 }
133 148
134 flatbuffers::FlatBufferBuilder entityFbb;
135 mFolderAdaptorFactory->createBuffer(folder, entityFbb);
136
137 flatbuffers::FlatBufferBuilder fbb;
138 //This is the resource type and not the domain type
139 auto entityId = fbb.CreateString(akonadiId.toStdString());
140 auto type = fbb.CreateString(bufferType.toStdString());
141 auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize());
142 auto location = Akonadi2::Commands::CreateCreateEntity(fbb, entityId, type, delta);
143 Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location);
144
145 Trace() << "Found a new entity: " << remoteId; 149 Trace() << "Found a new entity: " << remoteId;
146 enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize())); 150 createEntity(akonadiId.toLatin1(), bufferType, folder, *mFolderAdaptorFactory, [this](const QByteArray &buffer) {
151 enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, buffer);
152 });
153
147 } else { //modification 154 } else { //modification
148 Trace() << "Found a modified entity: " << remoteId; 155 Trace() << "Found a modified entity: " << remoteId;
149 //TODO diff and create modification if necessary 156 //TODO diff and create modification if necessary
@@ -155,7 +162,7 @@ void MaildirResource::synchronizeFolders(Akonadi2::Storage::Transaction &transac
155void MaildirResource::synchronizeMails(Akonadi2::Storage::Transaction &transaction, const QString &path) 162void MaildirResource::synchronizeMails(Akonadi2::Storage::Transaction &transaction, const QString &path)
156{ 163{
157 Trace() << "Synchronizing mails" << path; 164 Trace() << "Synchronizing mails" << path;
158 const QString bufferType = ENTITY_TYPE_MAIL; 165 const QByteArray bufferType = ENTITY_TYPE_MAIL;
159 166
160 KPIM::Maildir maildir(path, true); 167 KPIM::Maildir maildir(path, true);
161 if (!maildir.isValid()) { 168 if (!maildir.isValid()) {
@@ -177,10 +184,10 @@ void MaildirResource::synchronizeMails(Akonadi2::Storage::Transaction &transacti
177 QString fileName = entryIterator->fileName(); 184 QString fileName = entryIterator->fileName();
178 185
179 const auto remoteId = fileName.toUtf8(); 186 const auto remoteId = fileName.toUtf8();
180 auto akonadiId = resolveRemoteId(bufferType.toUtf8(), remoteId, synchronizationTransaction); 187 auto akonadiId = resolveRemoteId(bufferType, remoteId, synchronizationTransaction);
181 188
182 bool found = false; 189 bool found = false;
183 transaction.openDatabase(bufferType.toUtf8() + ".main").scan(akonadiId.toUtf8(), [&found](const QByteArray &, const QByteArray &) -> bool { 190 transaction.openDatabase(bufferType + ".main").scan(akonadiId.toUtf8(), [&found](const QByteArray &, const QByteArray &) -> bool {
184 found = true; 191 found = true;
185 return false; 192 return false;
186 }, [this](const Akonadi2::Storage::Error &error) { 193 }, [this](const Akonadi2::Storage::Error &error) {
@@ -209,16 +216,10 @@ void MaildirResource::synchronizeMails(Akonadi2::Storage::Transaction &transacti
209 flatbuffers::FlatBufferBuilder entityFbb; 216 flatbuffers::FlatBufferBuilder entityFbb;
210 mMailAdaptorFactory->createBuffer(mail, entityFbb); 217 mMailAdaptorFactory->createBuffer(mail, entityFbb);
211 218
212 flatbuffers::FlatBufferBuilder fbb;
213 //This is the resource type and not the domain type
214 auto entityId = fbb.CreateString(akonadiId.toStdString());
215 auto type = fbb.CreateString(bufferType.toStdString());
216 auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize());
217 auto location = Akonadi2::Commands::CreateCreateEntity(fbb, entityId, type, delta);
218 Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location);
219
220 Trace() << "Found a new entity: " << remoteId; 219 Trace() << "Found a new entity: " << remoteId;
221 enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize())); 220 createEntity(akonadiId.toLatin1(), bufferType, mail, *mMailAdaptorFactory, [this](const QByteArray &buffer) {
221 enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, buffer);
222 });
222 } else { //modification 223 } else { //modification
223 Trace() << "Found a modified entity: " << remoteId; 224 Trace() << "Found a modified entity: " << remoteId;
224 //TODO diff and create modification if necessary 225 //TODO diff and create modification if necessary