summaryrefslogtreecommitdiffstats
path: root/common/synchronizer.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-06-10 15:49:48 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-06-10 15:49:48 +0200
commit638e75d6f3d00fb473fd45e325fcfb34c6340c65 (patch)
tree4db057d7dff07c9e03cf5a732c660e705c17327a /common/synchronizer.cpp
parentce0feb3ef62c9438b0aedd601461cbb340faa021 (diff)
downloadsink-638e75d6f3d00fb473fd45e325fcfb34c6340c65.tar.gz
sink-638e75d6f3d00fb473fd45e325fcfb34c6340c65.zip
Create the drafts folder if necessary and merge it with the source
version
Diffstat (limited to 'common/synchronizer.cpp')
-rw-r--r--common/synchronizer.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/common/synchronizer.cpp b/common/synchronizer.cpp
index 0314997..b127ec5 100644
--- a/common/synchronizer.cpp
+++ b/common/synchronizer.cpp
@@ -164,6 +164,59 @@ void Synchronizer::createOrModify(const QByteArray &bufferType, const QByteArray
164 } 164 }
165} 165}
166 166
167template<typename DomainType>
168void Synchronizer::createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const DomainType &entity, const QHash<QByteArray, Sink::Query::Comparator> &mergeCriteria)
169{
170
171 Trace() << "Create or modify" << bufferType << remoteId;
172 auto mainDatabase = Storage::mainDatabase(transaction(), bufferType);
173 const auto sinkId = syncStore().resolveRemoteId(bufferType, remoteId);
174 const auto found = mainDatabase.contains(sinkId);
175 auto adaptorFactory = Sink::AdaptorFactoryRegistry::instance().getFactory(mResourceType, bufferType);
176 if (!found) {
177 if (!mergeCriteria.isEmpty()) {
178 Sink::Query query;
179 query.propertyFilter = mergeCriteria;
180 bool merge = false;
181 Sink::EntityReader<DomainType> reader(mResourceInstanceIdentifier, mResourceType, transaction());
182 reader.query(query,
183 [this, bufferType, remoteId, &merge](const DomainType &o) -> bool{
184 merge = true;
185 Trace() << "Merging local entity with remote entity: " << o.identifier() << remoteId;
186 syncStore().recordRemoteId(bufferType, o.identifier(), remoteId);
187 return false;
188 });
189 if (!merge) {
190 Trace() << "Found a new entity: " << remoteId;
191 createEntity(
192 sinkId, bufferType, entity, *adaptorFactory, [this](const QByteArray &buffer) { enqueueCommand(Sink::Commands::CreateEntityCommand, buffer); });
193 }
194 } else {
195 Trace() << "Found a new entity: " << remoteId;
196 createEntity(
197 sinkId, bufferType, entity, *adaptorFactory, [this](const QByteArray &buffer) { enqueueCommand(Sink::Commands::CreateEntityCommand, buffer); });
198 }
199 } else { // modification
200 qint64 retrievedRevision = 0;
201 if (auto current = EntityReaderUtils::getLatest(mainDatabase, sinkId, *adaptorFactory, retrievedRevision)) {
202 bool changed = false;
203 for (const auto &property : entity.changedProperties()) {
204 if (entity.getProperty(property) != current->getProperty(property)) {
205 Trace() << "Property changed " << sinkId << property;
206 changed = true;
207 }
208 }
209 if (changed) {
210 Trace() << "Found a modified entity: " << remoteId;
211 modifyEntity(sinkId, Sink::Storage::maxRevision(transaction()), bufferType, entity, *adaptorFactory,
212 [this](const QByteArray &buffer) { enqueueCommand(Sink::Commands::ModifyEntityCommand, buffer); });
213 }
214 } else {
215 Warning() << "Failed to get current entity";
216 }
217 }
218}
219
167KAsync::Job<void> Synchronizer::synchronize() 220KAsync::Job<void> Synchronizer::synchronize()
168{ 221{
169 Trace() << "Synchronizing"; 222 Trace() << "Synchronizing";
@@ -202,3 +255,11 @@ Sink::Storage::Transaction &Synchronizer::syncTransaction()
202 } 255 }
203 return mSyncTransaction; 256 return mSyncTransaction;
204} 257}
258
259#define REGISTER_TYPE(T) \
260 template void Synchronizer::createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const T &entity, const QHash<QByteArray, Sink::Query::Comparator> &mergeCriteria)
261
262REGISTER_TYPE(ApplicationDomain::Event);
263REGISTER_TYPE(ApplicationDomain::Mail);
264REGISTER_TYPE(ApplicationDomain::Folder);
265