summaryrefslogtreecommitdiffstats
path: root/examples/imapresource/imapserverproxy.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-09-14 16:16:42 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-09-15 16:14:19 +0200
commit6d5be4fb7b8cbc450e2780905eaac9a18b486c5c (patch)
tree3e0f42e15889c271a41f4d6225a44b21c066b16a /examples/imapresource/imapserverproxy.cpp
parentca9c54670cc2d26386c80a2aeed2c999313ec362 (diff)
downloadsink-6d5be4fb7b8cbc450e2780905eaac9a18b486c5c.tar.gz
sink-6d5be4fb7b8cbc450e2780905eaac9a18b486c5c.zip
New synchronization algorithm that only fetches the last 14 days.
Diffstat (limited to 'examples/imapresource/imapserverproxy.cpp')
-rw-r--r--examples/imapresource/imapserverproxy.cpp94
1 files changed, 59 insertions, 35 deletions
diff --git a/examples/imapresource/imapserverproxy.cpp b/examples/imapresource/imapserverproxy.cpp
index a75825e..58164d3 100644
--- a/examples/imapresource/imapserverproxy.cpp
+++ b/examples/imapresource/imapserverproxy.cpp
@@ -263,21 +263,24 @@ KAsync::Job<void> ImapServerProxy::fetch(const KIMAP2::ImapSet &set, KIMAP2::Fet
263 263
264KAsync::Job<QVector<qint64>> ImapServerProxy::search(const KIMAP2::ImapSet &set) 264KAsync::Job<QVector<qint64>> ImapServerProxy::search(const KIMAP2::ImapSet &set)
265{ 265{
266 return search(KIMAP2::Term(KIMAP2::Term::Uid, set));
267}
268
269KAsync::Job<QVector<qint64>> ImapServerProxy::search(const KIMAP2::Term &term)
270{
266 auto search = new KIMAP2::SearchJob(mSession); 271 auto search = new KIMAP2::SearchJob(mSession);
267 search->setTerm(KIMAP2::Term(KIMAP2::Term::Uid, set)); 272 search->setTerm(term);
268 search->setUidBased(true); 273 search->setUidBased(true);
269 return runJob<QVector<qint64>>(search, [](KJob *job) -> QVector<qint64> { 274 return runJob<QVector<qint64>>(search, [](KJob *job) -> QVector<qint64> {
270 return static_cast<KIMAP2::SearchJob*>(job)->results(); 275 return static_cast<KIMAP2::SearchJob*>(job)->results();
271 }); 276 });
272} 277}
273 278
274KAsync::Job<void> ImapServerProxy::fetch(const KIMAP2::ImapSet &set, KIMAP2::FetchJob::FetchScope scope, const std::function<void(const QVector<Message> &)> &callback) 279KAsync::Job<void> ImapServerProxy::fetch(const KIMAP2::ImapSet &set, KIMAP2::FetchJob::FetchScope scope, const std::function<void(const Message &)> &callback)
275{ 280{
276 return fetch(set, scope, 281 return fetch(set, scope,
277 [callback](const KIMAP2::FetchJob::Result &result) { 282 [callback](const KIMAP2::FetchJob::Result &result) {
278 QVector<Message> list; 283 callback(Message{result.uid, result.size, result.attributes, result.flags, result.message});
279 list << Message{result.uid, result.size, result.attributes, result.flags, result.message};
280 callback(list);
281 }); 284 });
282} 285}
283 286
@@ -286,9 +289,9 @@ QStringList ImapServerProxy::getCapabilities() const
286 return mCapabilities; 289 return mCapabilities;
287} 290}
288 291
289KAsync::Job<QList<qint64>> ImapServerProxy::fetchHeaders(const QString &mailbox, const qint64 minUid) 292KAsync::Job<QVector<qint64>> ImapServerProxy::fetchHeaders(const QString &mailbox, const qint64 minUid)
290{ 293{
291 auto list = QSharedPointer<QList<qint64>>::create(); 294 auto list = QSharedPointer<QVector<qint64>>::create();
292 KIMAP2::FetchJob::FetchScope scope; 295 KIMAP2::FetchJob::FetchScope scope;
293 scope.mode = KIMAP2::FetchJob::FetchScope::Flags; 296 scope.mode = KIMAP2::FetchJob::FetchScope::Flags;
294 297
@@ -304,14 +307,19 @@ KAsync::Job<QList<qint64>> ImapServerProxy::fetchHeaders(const QString &mailbox,
304 307
305 list->append(result.uid); 308 list->append(result.uid);
306 }) 309 })
307 .syncThen<QList<qint64>>([list](){ 310 .syncThen<QVector<qint64>>([list](){
308 return *list; 311 return *list;
309 }); 312 });
310} 313}
311 314
312KAsync::Job<QVector<qint64>> ImapServerProxy::fetchUids(const QString &mailbox) 315KAsync::Job<QVector<qint64>> ImapServerProxy::fetchUids(const QString &mailbox)
313{ 316{
314 return select(mailbox).then<QVector<qint64>>(search(KIMAP2::ImapSet(1, 0))); 317 return select(mailbox).then<QVector<qint64>>(search(KIMAP2::Term(KIMAP2::Term::Uid, KIMAP2::ImapSet(1, 0))));
318}
319
320KAsync::Job<QVector<qint64>> ImapServerProxy::fetchUidsSince(const QString &mailbox, const QDate &since)
321{
322 return select(mailbox).then<QVector<qint64>>(search(KIMAP2::Term(KIMAP2::Term::Since, since)));
315} 323}
316 324
317KAsync::Job<void> ImapServerProxy::list(KIMAP2::ListJob::Option option, const std::function<void(const KIMAP2::MailBoxDescriptor &mailboxes, const QList<QByteArray> &flags)> &callback) 325KAsync::Job<void> ImapServerProxy::list(KIMAP2::ListJob::Option option, const std::function<void(const KIMAP2::MailBoxDescriptor &mailboxes, const QList<QByteArray> &flags)> &callback)
@@ -390,7 +398,7 @@ QString ImapServerProxy::mailboxFromFolder(const Folder &folder) const
390 return folder.pathParts.join(mPersonalNamespaceSeparator); 398 return folder.pathParts.join(mPersonalNamespaceSeparator);
391} 399}
392 400
393KAsync::Job<SelectResult> ImapServerProxy::fetchFlags(const Folder &folder, const KIMAP2::ImapSet &set, qint64 changedsince, std::function<void(const QVector<Message> &)> callback) 401KAsync::Job<SelectResult> ImapServerProxy::fetchFlags(const Folder &folder, const KIMAP2::ImapSet &set, qint64 changedsince, std::function<void(const Message &)> callback)
394{ 402{
395 SinkTrace() << "Fetching flags " << folder.normalizedPath(); 403 SinkTrace() << "Fetching flags " << folder.normalizedPath();
396 return select(mailboxFromFolder(folder)).then<SelectResult, SelectResult>([=](const SelectResult &selectResult) -> KAsync::Job<SelectResult> { 404 return select(mailboxFromFolder(folder)).then<SelectResult, SelectResult>([=](const SelectResult &selectResult) -> KAsync::Job<SelectResult> {
@@ -413,7 +421,7 @@ KAsync::Job<SelectResult> ImapServerProxy::fetchFlags(const Folder &folder, cons
413 }); 421 });
414} 422}
415 423
416KAsync::Job<void> ImapServerProxy::fetchMessages(const Folder &folder, qint64 uidNext, std::function<void(const QVector<Message> &)> callback, std::function<void(int, int)> progress) 424KAsync::Job<void> ImapServerProxy::fetchMessages(const Folder &folder, qint64 uidNext, std::function<void(const Message &)> callback, std::function<void(int, int)> progress)
417{ 425{
418 auto time = QSharedPointer<QTime>::create(); 426 auto time = QSharedPointer<QTime>::create();
419 time->start(); 427 time->start();
@@ -426,40 +434,56 @@ KAsync::Job<void> ImapServerProxy::fetchMessages(const Folder &folder, qint64 ui
426 } 434 }
427 435
428 SinkTrace() << "Fetching messages from " << folder.normalizedPath() << selectResult.uidNext << uidNext; 436 SinkTrace() << "Fetching messages from " << folder.normalizedPath() << selectResult.uidNext << uidNext;
429 return fetchHeaders(mailboxFromFolder(folder), (uidNext + 1)).then<void, QList<qint64>>([this, callback, time, progress, folder](const QList<qint64> &uidsToFetch){ 437 return fetchHeaders(mailboxFromFolder(folder), (uidNext + 1)).then<void, QVector<qint64>>([this, callback, time, progress, folder](const QVector<qint64> &uidsToFetch){
430 SinkTrace() << "Fetched headers" << folder.normalizedPath(); 438 SinkTrace() << "Fetched headers" << folder.normalizedPath();
431 SinkTrace() << " Total: " << uidsToFetch.size(); 439 SinkTrace() << " Total: " << uidsToFetch.size();
432 SinkTrace() << " Uids to fetch: " << uidsToFetch; 440 SinkTrace() << " Uids to fetch: " << uidsToFetch;
433 SinkTrace() << " Took: " << Sink::Log::TraceTime(time->elapsed()); 441 SinkTrace() << " Took: " << Sink::Log::TraceTime(time->elapsed());
434 auto totalCount = uidsToFetch.size(); 442 return fetchMessages(folder, uidsToFetch, callback, progress);
443 });
444
445 });
446}
447
448KAsync::Job<void> ImapServerProxy::fetchMessages(const Folder &folder, const QVector<qint64> &uidsToFetch, std::function<void(const Message &)> callback, std::function<void(int, int)> progress)
449{
450 auto time = QSharedPointer<QTime>::create();
451 time->start();
452 Q_ASSERT(!mPersonalNamespaceSeparator.isNull());
453 return select(mailboxFromFolder(folder)).then<void, SelectResult>([this, callback, folder, time, progress, uidsToFetch](const SelectResult &selectResult) -> KAsync::Job<void> {
454
455 SinkTrace() << "Fetching messages" << folder.normalizedPath();
456 SinkTrace() << " Total: " << uidsToFetch.size();
457 SinkTrace() << " Uids to fetch: " << uidsToFetch;
458 auto totalCount = uidsToFetch.size();
459 if (progress) {
460 progress(0, totalCount);
461 }
462 if (uidsToFetch.isEmpty()) {
463 SinkTrace() << "Nothing to fetch";
464 return KAsync::null<void>();
465 }
466 KIMAP2::FetchJob::FetchScope scope;
467 scope.parts.clear();
468 scope.mode = KIMAP2::FetchJob::FetchScope::Full;
469
470 KIMAP2::ImapSet set;
471 set.add(uidsToFetch);
472 auto count = QSharedPointer<int>::create();
473 return fetch(set, scope, [=](const Message &message) {
474 *count += 1;
435 if (progress) { 475 if (progress) {
436 progress(0, totalCount); 476 progress(*count, totalCount);
437 }
438 if (uidsToFetch.isEmpty()) {
439 SinkTrace() << "Nothing to fetch";
440 callback(QVector<Message>());
441 return KAsync::null<void>();
442 } 477 }
443 KIMAP2::FetchJob::FetchScope scope; 478 callback(message);
444 scope.parts.clear();
445 scope.mode = KIMAP2::FetchJob::FetchScope::Full;
446
447 KIMAP2::ImapSet set;
448 set.add(uidsToFetch.toVector());
449 auto count = QSharedPointer<int>::create();
450 return fetch(set, scope, [=](const QVector<Message> &messages) {
451 *count += messages.size();
452 if (progress) {
453 progress(*count, totalCount);
454 }
455 callback(messages);
456 });
457 }); 479 });
458 480 })
481 .syncThen<void>([time]() {
482 SinkTrace() << "The fetch took: " << Sink::Log::TraceTime(time->elapsed());
459 }); 483 });
460} 484}
461 485
462KAsync::Job<void> ImapServerProxy::fetchMessages(const Folder &folder, std::function<void(const QVector<Message> &)> callback, std::function<void(int, int)> progress) 486KAsync::Job<void> ImapServerProxy::fetchMessages(const Folder &folder, std::function<void(const Message &)> callback, std::function<void(int, int)> progress)
463{ 487{
464 return fetchMessages(folder, 0, callback, progress); 488 return fetchMessages(folder, 0, callback, progress);
465} 489}