diff options
-rw-r--r-- | common/synchronizer.cpp | 47 | ||||
-rw-r--r-- | common/synchronizer.h | 1 | ||||
-rw-r--r-- | examples/imapresource/imapresource.cpp | 3 |
3 files changed, 31 insertions, 20 deletions
diff --git a/common/synchronizer.cpp b/common/synchronizer.cpp index 15a06e7..8de45a9 100644 --- a/common/synchronizer.cpp +++ b/common/synchronizer.cpp | |||
@@ -143,36 +143,45 @@ void Synchronizer::scanForRemovals(const QByteArray &bufferType, const std::func | |||
143 | }); | 143 | }); |
144 | } | 144 | } |
145 | 145 | ||
146 | void Synchronizer::modify(const QByteArray &bufferType, const QByteArray &remoteId, const Sink::ApplicationDomain::ApplicationDomainType &entity) | ||
147 | { | ||
148 | auto mainDatabase = Storage::mainDatabase(transaction(), bufferType); | ||
149 | const auto sinkId = syncStore().resolveRemoteId(bufferType, remoteId); | ||
150 | auto adaptorFactory = Sink::AdaptorFactoryRegistry::instance().getFactory(mResourceType, bufferType); | ||
151 | Q_ASSERT(adaptorFactory); | ||
152 | qint64 retrievedRevision = 0; | ||
153 | if (auto current = EntityReaderUtils::getLatest(mainDatabase, sinkId, *adaptorFactory, retrievedRevision)) { | ||
154 | bool changed = false; | ||
155 | for (const auto &property : entity.changedProperties()) { | ||
156 | if (entity.getProperty(property) != current->getProperty(property)) { | ||
157 | SinkTrace() << "Property changed " << sinkId << property; | ||
158 | changed = true; | ||
159 | } | ||
160 | } | ||
161 | if (changed) { | ||
162 | SinkTrace() << "Found a modified entity: " << remoteId; | ||
163 | modifyEntity(sinkId, Sink::Storage::maxRevision(transaction()), bufferType, entity, *adaptorFactory, | ||
164 | [this](const QByteArray &buffer) { enqueueCommand(Sink::Commands::ModifyEntityCommand, buffer); }); | ||
165 | } | ||
166 | } else { | ||
167 | SinkWarning() << "Failed to get current entity"; | ||
168 | } | ||
169 | } | ||
170 | |||
146 | void Synchronizer::createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const Sink::ApplicationDomain::ApplicationDomainType &entity) | 171 | void Synchronizer::createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const Sink::ApplicationDomain::ApplicationDomainType &entity) |
147 | { | 172 | { |
148 | SinkTrace() << "Create or modify" << bufferType << remoteId; | 173 | SinkTrace() << "Create or modify" << bufferType << remoteId; |
149 | auto mainDatabase = Storage::mainDatabase(transaction(), bufferType); | 174 | auto mainDatabase = Storage::mainDatabase(transaction(), bufferType); |
150 | const auto sinkId = syncStore().resolveRemoteId(bufferType, remoteId); | 175 | const auto sinkId = syncStore().resolveRemoteId(bufferType, remoteId); |
151 | const auto found = mainDatabase.contains(sinkId); | 176 | const auto found = mainDatabase.contains(sinkId); |
152 | auto adaptorFactory = Sink::AdaptorFactoryRegistry::instance().getFactory(mResourceType, bufferType); | ||
153 | Q_ASSERT(adaptorFactory); | ||
154 | if (!found) { | 177 | if (!found) { |
155 | SinkTrace() << "Found a new entity: " << remoteId; | 178 | SinkTrace() << "Found a new entity: " << remoteId; |
179 | auto adaptorFactory = Sink::AdaptorFactoryRegistry::instance().getFactory(mResourceType, bufferType); | ||
180 | Q_ASSERT(adaptorFactory); | ||
156 | createEntity( | 181 | createEntity( |
157 | sinkId, bufferType, entity, *adaptorFactory, [this](const QByteArray &buffer) { enqueueCommand(Sink::Commands::CreateEntityCommand, buffer); }); | 182 | sinkId, bufferType, entity, *adaptorFactory, [this](const QByteArray &buffer) { enqueueCommand(Sink::Commands::CreateEntityCommand, buffer); }); |
158 | } else { // modification | 183 | } else { // modification |
159 | qint64 retrievedRevision = 0; | 184 | modify(bufferType, remoteId, entity); |
160 | if (auto current = EntityReaderUtils::getLatest(mainDatabase, sinkId, *adaptorFactory, retrievedRevision)) { | ||
161 | bool changed = false; | ||
162 | for (const auto &property : entity.changedProperties()) { | ||
163 | if (entity.getProperty(property) != current->getProperty(property)) { | ||
164 | SinkTrace() << "Property changed " << sinkId << property; | ||
165 | changed = true; | ||
166 | } | ||
167 | } | ||
168 | if (changed) { | ||
169 | SinkTrace() << "Found a modified entity: " << remoteId; | ||
170 | modifyEntity(sinkId, Sink::Storage::maxRevision(transaction()), bufferType, entity, *adaptorFactory, | ||
171 | [this](const QByteArray &buffer) { enqueueCommand(Sink::Commands::ModifyEntityCommand, buffer); }); | ||
172 | } | ||
173 | } else { | ||
174 | SinkWarning() << "Failed to get current entity"; | ||
175 | } | ||
176 | } | 185 | } |
177 | } | 186 | } |
178 | 187 | ||
diff --git a/common/synchronizer.h b/common/synchronizer.h index 81d4f27..5f60128 100644 --- a/common/synchronizer.h +++ b/common/synchronizer.h | |||
@@ -84,6 +84,7 @@ protected: | |||
84 | void createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const Sink::ApplicationDomain::ApplicationDomainType &entity); | 84 | void createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const Sink::ApplicationDomain::ApplicationDomainType &entity); |
85 | template <typename DomainType> | 85 | template <typename DomainType> |
86 | void createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const DomainType &entity, const QHash<QByteArray, Sink::Query::Comparator> &mergeCriteria); | 86 | void createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const DomainType &entity, const QHash<QByteArray, Sink::Query::Comparator> &mergeCriteria); |
87 | void modify(const QByteArray &bufferType, const QByteArray &remoteId, const Sink::ApplicationDomain::ApplicationDomainType &entity); | ||
87 | 88 | ||
88 | // template <typename DomainType> | 89 | // template <typename DomainType> |
89 | // void create(const DomainType &entity); | 90 | // void create(const DomainType &entity); |
diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp index 3a7be2d..0b1437a 100644 --- a/examples/imapresource/imapresource.cpp +++ b/examples/imapresource/imapresource.cpp | |||
@@ -233,7 +233,8 @@ public: | |||
233 | auto mail = Sink::ApplicationDomain::Mail::create(mResourceInstanceIdentifier); | 233 | auto mail = Sink::ApplicationDomain::Mail::create(mResourceInstanceIdentifier); |
234 | mail.setUnread(!message.flags.contains(Imap::Flags::Seen)); | 234 | mail.setUnread(!message.flags.contains(Imap::Flags::Seen)); |
235 | mail.setImportant(message.flags.contains(Imap::Flags::Flagged)); | 235 | mail.setImportant(message.flags.contains(Imap::Flags::Flagged)); |
236 | createOrModify(ENTITY_TYPE_MAIL, remoteId, mail); | 236 | |
237 | modify(ENTITY_TYPE_MAIL, remoteId, mail); | ||
237 | } | 238 | } |
238 | }); | 239 | }); |
239 | }) | 240 | }) |