diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-07-21 20:39:11 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-09-15 16:14:19 +0200 |
commit | 07d9eaaa97dfa63bb3ff9767c6b2e152ad8a04e0 (patch) | |
tree | ea8d32e3a213ec9562ceccb66fb24ca6b37621df /examples/imapresource/imapresource.cpp | |
parent | 5f127fa687079c7180142e371c86bd6dacfb845e (diff) | |
download | sink-07d9eaaa97dfa63bb3ff9767c6b2e152ad8a04e0.tar.gz sink-07d9eaaa97dfa63bb3ff9767c6b2e152ad8a04e0.zip |
Prepare incremental syncing.
Diffstat (limited to 'examples/imapresource/imapresource.cpp')
-rw-r--r-- | examples/imapresource/imapresource.cpp | 67 |
1 files changed, 41 insertions, 26 deletions
diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp index 24fd426..3ad80f9 100644 --- a/examples/imapresource/imapresource.cpp +++ b/examples/imapresource/imapresource.cpp | |||
@@ -211,14 +211,49 @@ public: | |||
211 | SinkLog() << "Removed " << count << " mails in " << path << Sink::Log::TraceTime(elapsed) << " " << elapsed/qMax(count, 1) << " [ms/mail]"; | 211 | SinkLog() << "Removed " << count << " mails in " << path << Sink::Log::TraceTime(elapsed) << " " << elapsed/qMax(count, 1) << " [ms/mail]"; |
212 | } | 212 | } |
213 | 213 | ||
214 | KAsync::Job<void> synchronizeFolder(QSharedPointer<ImapServerProxy> imap, const Imap::Folder &folder) | ||
215 | { | ||
216 | QSet<qint64> uids; | ||
217 | SinkLog() << "Synchronizing mails" << folder.normalizedPath(); | ||
218 | auto capabilities = imap->getCapabilities(); | ||
219 | bool canDoIncrementalRemovals = false; | ||
220 | return KAsync::start<void>([=]() { | ||
221 | //TODO update flags | ||
222 | }) | ||
223 | .then<void, KAsync::Job<void>>([=]() { | ||
224 | //TODO Remove what's no longer existing | ||
225 | if (canDoIncrementalRemovals) { | ||
226 | } else { | ||
227 | return imap->fetchUids(folder).then<void, QVector<qint64>>([this, folder](const QVector<qint64> &uids) { | ||
228 | SinkTrace() << "Syncing removals"; | ||
229 | synchronizeRemovals(folder.normalizedPath(), uids.toList().toSet()); | ||
230 | commit(); | ||
231 | }).then<void>([](){}); | ||
232 | } | ||
233 | return KAsync::null<void>(); | ||
234 | }) | ||
235 | .then<void, KAsync::Job<void>>([this, folder, imap]() { | ||
236 | auto uidNext = 0; | ||
237 | return imap->fetchMessages(folder, uidNext, [this, folder](const QVector<Message> &messages) { | ||
238 | SinkTrace() << "Got mail"; | ||
239 | synchronizeMails(folder.normalizedPath(), messages); | ||
240 | }, | ||
241 | [](int progress, int total) { | ||
242 | SinkTrace() << "Progress: " << progress << " out of " << total; | ||
243 | }); | ||
244 | }); | ||
245 | |||
246 | |||
247 | } | ||
248 | |||
214 | KAsync::Job<void> synchronizeWithSource() Q_DECL_OVERRIDE | 249 | KAsync::Job<void> synchronizeWithSource() Q_DECL_OVERRIDE |
215 | { | 250 | { |
216 | SinkLog() << " Synchronizing"; | 251 | SinkLog() << " Synchronizing"; |
217 | return KAsync::start<void>([this](KAsync::Future<void> future) { | 252 | return KAsync::start<void>([this](KAsync::Future<void> future) { |
218 | SinkTrace() << "Connecting to:" << mServer << mPort; | 253 | SinkTrace() << "Connecting to:" << mServer << mPort; |
219 | SinkTrace() << "as:" << mUser; | 254 | SinkTrace() << "as:" << mUser; |
220 | ImapServerProxy imap(mServer, mPort); | 255 | auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort); |
221 | auto loginFuture = imap.login(mUser, mPassword).exec(); | 256 | auto loginFuture = imap->login(mUser, mPassword).exec(); |
222 | loginFuture.waitForFinished(); | 257 | loginFuture.waitForFinished(); |
223 | if (loginFuture.errorCode()) { | 258 | if (loginFuture.errorCode()) { |
224 | SinkWarning() << "Login failed."; | 259 | SinkWarning() << "Login failed."; |
@@ -229,7 +264,7 @@ public: | |||
229 | } | 264 | } |
230 | 265 | ||
231 | QVector<Folder> folderList; | 266 | QVector<Folder> folderList; |
232 | auto folderFuture = imap.fetchFolders([this, &imap, &folderList](const QVector<Folder> &folders) { | 267 | auto folderFuture = imap->fetchFolders([this, &imap, &folderList](const QVector<Folder> &folders) { |
233 | synchronizeFolders(folders); | 268 | synchronizeFolders(folders); |
234 | commit(); | 269 | commit(); |
235 | folderList << folders; | 270 | folderList << folders; |
@@ -248,27 +283,7 @@ public: | |||
248 | if (folder.noselect) { | 283 | if (folder.noselect) { |
249 | continue; | 284 | continue; |
250 | } | 285 | } |
251 | QSet<qint64> uids; | 286 | synchronizeFolder(imap, folder).exec().waitForFinished(); |
252 | SinkLog() << "Synchronizing mails" << folder.normalizedPath(); | ||
253 | auto messagesFuture = imap.fetchMessages(folder, [this, folder, &uids](const QVector<Message> &messages) { | ||
254 | for (const auto &msg : messages) { | ||
255 | uids << msg.uid; | ||
256 | } | ||
257 | synchronizeMails(folder.normalizedPath(), messages); | ||
258 | }, | ||
259 | [](int progress, int total) { | ||
260 | SinkTrace() << "Progress: " << progress << " out of " << total; | ||
261 | }).exec(); | ||
262 | messagesFuture.waitForFinished(); | ||
263 | commit(); | ||
264 | if (messagesFuture.errorCode()) { | ||
265 | SinkWarning() << "Folder sync failed: " << folder.normalizedPath(); | ||
266 | continue; | ||
267 | } | ||
268 | //Remove what there is to remove | ||
269 | synchronizeRemovals(folder.normalizedPath(), uids); | ||
270 | commit(); | ||
271 | SinkTrace() << "Folder synchronized: " << folder.normalizedPath(); | ||
272 | } | 287 | } |
273 | future.setFinished(); | 288 | future.setFinished(); |
274 | }); | 289 | }); |
@@ -541,7 +556,7 @@ KAsync::Job<void> ImapResource::inspect(int inspectionType, const QByteArray &in | |||
541 | SinkTrace() << "Connecting to:" << mServer << mPort; | 556 | SinkTrace() << "Connecting to:" << mServer << mPort; |
542 | SinkTrace() << "as:" << mUser; | 557 | SinkTrace() << "as:" << mUser; |
543 | auto inspectionJob = imap->login(mUser, mPassword) | 558 | auto inspectionJob = imap->login(mUser, mPassword) |
544 | .then<void>(imap->select(folderRemoteId)) | 559 | .then<void>(imap->select(folderRemoteId).then<void>([](){})) |
545 | .then<void>(imap->fetch(set, scope, [imap, messageByUid](const QVector<Imap::Message> &messages) { | 560 | .then<void>(imap->fetch(set, scope, [imap, messageByUid](const QVector<Imap::Message> &messages) { |
546 | for (const auto &m : messages) { | 561 | for (const auto &m : messages) { |
547 | messageByUid->insert(m.uid, m); | 562 | messageByUid->insert(m.uid, m); |
@@ -604,7 +619,7 @@ KAsync::Job<void> ImapResource::inspect(int inspectionType, const QByteArray &in | |||
604 | auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort); | 619 | auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort); |
605 | auto messageByUid = QSharedPointer<QHash<qint64, Imap::Message>>::create(); | 620 | auto messageByUid = QSharedPointer<QHash<qint64, Imap::Message>>::create(); |
606 | auto inspectionJob = imap->login(mUser, mPassword) | 621 | auto inspectionJob = imap->login(mUser, mPassword) |
607 | .then<void>(imap->select(remoteId)) | 622 | .then<void>(imap->select(remoteId).then<void>([](){})) |
608 | .then<void>(imap->fetch(set, scope, [=](const QVector<Imap::Message> &messages) { | 623 | .then<void>(imap->fetch(set, scope, [=](const QVector<Imap::Message> &messages) { |
609 | for (const auto &m : messages) { | 624 | for (const auto &m : messages) { |
610 | messageByUid->insert(m.uid, m); | 625 | messageByUid->insert(m.uid, m); |