diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-07-27 02:26:47 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-09-15 16:14:19 +0200 |
commit | 26816c21f60450e461a5b6ef4ef740f6070ce278 (patch) | |
tree | 55e8aee03e094abf702438e6cd26233047345e70 /examples/imapresource/imapresource.cpp | |
parent | 9a9bb39f7641a818434cafa0dae0c8aa47124c0b (diff) | |
download | sink-26816c21f60450e461a5b6ef4ef740f6070ce278.tar.gz sink-26816c21f60450e461a5b6ef4ef740f6070ce278.zip |
Ported to the kasync revamp
Diffstat (limited to 'examples/imapresource/imapresource.cpp')
-rw-r--r-- | examples/imapresource/imapresource.cpp | 74 |
1 files changed, 34 insertions, 40 deletions
diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp index e199ea1..f78376a 100644 --- a/examples/imapresource/imapresource.cpp +++ b/examples/imapresource/imapresource.cpp | |||
@@ -56,6 +56,8 @@ | |||
56 | 56 | ||
57 | SINK_DEBUG_AREA("imapresource") | 57 | SINK_DEBUG_AREA("imapresource") |
58 | 58 | ||
59 | Q_DECLARE_METATYPE(QSharedPointer<Imap::ImapServerProxy>) | ||
60 | |||
59 | using namespace Imap; | 61 | using namespace Imap; |
60 | using namespace Sink; | 62 | using namespace Sink; |
61 | 63 | ||
@@ -217,22 +219,22 @@ public: | |||
217 | SinkLog() << "Synchronizing mails" << folder.normalizedPath(); | 219 | SinkLog() << "Synchronizing mails" << folder.normalizedPath(); |
218 | auto capabilities = imap->getCapabilities(); | 220 | auto capabilities = imap->getCapabilities(); |
219 | bool canDoIncrementalRemovals = false; | 221 | bool canDoIncrementalRemovals = false; |
220 | return KAsync::start<void>([=]() { | 222 | return KAsync::syncStart<void>([=]() { |
221 | //TODO update flags | 223 | //TODO update flags |
222 | }) | 224 | }) |
223 | .then<void, KAsync::Job<void>>([=]() { | 225 | .then<void>([=]() { |
224 | //TODO Remove what's no longer existing | 226 | //TODO Remove what's no longer existing |
225 | if (canDoIncrementalRemovals) { | 227 | if (canDoIncrementalRemovals) { |
226 | } else { | 228 | } else { |
227 | return imap->fetchUids(folder).then<void, QVector<qint64>>([this, folder](const QVector<qint64> &uids) { | 229 | return imap->fetchUids(folder).syncThen<void, QVector<qint64>>([this, folder](const QVector<qint64> &uids) { |
228 | SinkTrace() << "Syncing removals"; | 230 | SinkTrace() << "Syncing removals"; |
229 | synchronizeRemovals(folder.normalizedPath(), uids.toList().toSet()); | 231 | synchronizeRemovals(folder.normalizedPath(), uids.toList().toSet()); |
230 | commit(); | 232 | commit(); |
231 | }).then<void>([](){}); | 233 | }); |
232 | } | 234 | } |
233 | return KAsync::null<void>(); | 235 | return KAsync::null<void>(); |
234 | }) | 236 | }) |
235 | .then<void, KAsync::Job<void>>([this, folder, imap]() { | 237 | .then<void>([this, folder, imap]() { |
236 | SinkTrace() << "About to fetch mail"; | 238 | SinkTrace() << "About to fetch mail"; |
237 | const auto uidNext = syncStore().readValue(folder.normalizedPath().toUtf8() + "uidnext").toLongLong(); | 239 | const auto uidNext = syncStore().readValue(folder.normalizedPath().toUtf8() + "uidnext").toLongLong(); |
238 | auto maxUid = QSharedPointer<qint64>::create(0); | 240 | auto maxUid = QSharedPointer<qint64>::create(0); |
@@ -248,7 +250,7 @@ public: | |||
248 | [](int progress, int total) { | 250 | [](int progress, int total) { |
249 | SinkTrace() << "Progress: " << progress << " out of " << total; | 251 | SinkTrace() << "Progress: " << progress << " out of " << total; |
250 | }) | 252 | }) |
251 | .then<void>([this, maxUid, folder]() { | 253 | .syncThen<void>([this, maxUid, folder]() { |
252 | syncStore().writeValue(folder.normalizedPath().toUtf8() + "uidnext", QByteArray::number(*maxUid)); | 254 | syncStore().writeValue(folder.normalizedPath().toUtf8() + "uidnext", QByteArray::number(*maxUid)); |
253 | }); | 255 | }); |
254 | }); | 256 | }); |
@@ -330,15 +332,12 @@ public: | |||
330 | flags << Imap::Flags::Flagged; | 332 | flags << Imap::Flags::Flagged; |
331 | } | 333 | } |
332 | QDateTime internalDate = mail.getDate(); | 334 | QDateTime internalDate = mail.getDate(); |
333 | auto rid = QSharedPointer<QByteArray>::create(); | ||
334 | return login.then(imap->append(mailbox, content, flags, internalDate)) | 335 | return login.then(imap->append(mailbox, content, flags, internalDate)) |
335 | .then<void, qint64>([imap, mailbox, rid, mail](qint64 uid) { | 336 | .addToContext(imap) |
337 | .syncThen<QByteArray, qint64>([mail](qint64 uid) { | ||
336 | const auto remoteId = assembleMailRid(mail, uid); | 338 | const auto remoteId = assembleMailRid(mail, uid); |
337 | //FIXME this get's called after the final error handler? WTF? | ||
338 | SinkTrace() << "Finished creating a new mail: " << remoteId; | 339 | SinkTrace() << "Finished creating a new mail: " << remoteId; |
339 | *rid = remoteId; | 340 | return remoteId; |
340 | }).then<QByteArray>([rid, imap]() { //FIXME fix KJob so we don't need this extra clause | ||
341 | return *rid; | ||
342 | }); | 341 | }); |
343 | } else if (operation == Sink::Operation_Removal) { | 342 | } else if (operation == Sink::Operation_Removal) { |
344 | const auto folderId = folderIdFromMailRid(oldRemoteId); | 343 | const auto folderId = folderIdFromMailRid(oldRemoteId); |
@@ -348,7 +347,7 @@ public: | |||
348 | KIMAP::ImapSet set; | 347 | KIMAP::ImapSet set; |
349 | set.add(uid); | 348 | set.add(uid); |
350 | return login.then(imap->remove(mailbox, set)) | 349 | return login.then(imap->remove(mailbox, set)) |
351 | .then<QByteArray>([imap, oldRemoteId]() { | 350 | .syncThen<QByteArray>([imap, oldRemoteId] { |
352 | SinkTrace() << "Finished removing a mail: " << oldRemoteId; | 351 | SinkTrace() << "Finished removing a mail: " << oldRemoteId; |
353 | return QByteArray(); | 352 | return QByteArray(); |
354 | }); | 353 | }); |
@@ -374,29 +373,24 @@ public: | |||
374 | const QString oldMailbox = syncStore().resolveLocalId(ENTITY_TYPE_FOLDER, folderId); | 373 | const QString oldMailbox = syncStore().resolveLocalId(ENTITY_TYPE_FOLDER, folderId); |
375 | QByteArray content = KMime::LFtoCRLF(mail.getMimeMessage()); | 374 | QByteArray content = KMime::LFtoCRLF(mail.getMimeMessage()); |
376 | QDateTime internalDate = mail.getDate(); | 375 | QDateTime internalDate = mail.getDate(); |
377 | auto rid = QSharedPointer<QByteArray>::create(); | ||
378 | KIMAP::ImapSet set; | 376 | KIMAP::ImapSet set; |
379 | set.add(uid); | 377 | set.add(uid); |
380 | return login.then(imap->append(mailbox, content, flags, internalDate)) | 378 | return login.then(imap->append(mailbox, content, flags, internalDate)) |
381 | .then<void, qint64>([imap, mailbox, rid, mail](qint64 uid) { | 379 | .addToContext(imap) |
380 | .then<QByteArray, qint64>([=](qint64 uid) { | ||
382 | const auto remoteId = assembleMailRid(mail, uid); | 381 | const auto remoteId = assembleMailRid(mail, uid); |
383 | SinkTrace() << "Finished creating a modified mail: " << remoteId; | 382 | SinkTrace() << "Finished creating a modified mail: " << remoteId; |
384 | *rid = remoteId; | 383 | return imap->remove(oldMailbox, set).then(KAsync::value(remoteId)); |
385 | }) | ||
386 | .then(imap->remove(oldMailbox, set)) | ||
387 | .then<QByteArray>([rid, imap]() { | ||
388 | return *rid; | ||
389 | }); | 384 | }); |
390 | } else { | 385 | } else { |
391 | SinkTrace() << "Updating flags only."; | 386 | SinkTrace() << "Updating flags only."; |
392 | KIMAP::ImapSet set; | 387 | KIMAP::ImapSet set; |
393 | set.add(uid); | 388 | set.add(uid); |
394 | return login.then(imap->select(mailbox)) | 389 | return login.then(imap->select(mailbox)) |
390 | .addToContext(imap) | ||
395 | .then(imap->storeFlags(set, flags)) | 391 | .then(imap->storeFlags(set, flags)) |
396 | .then<void>([imap, mailbox]() { | 392 | .syncThen<QByteArray>([=] { |
397 | SinkTrace() << "Finished modifying mail"; | 393 | SinkTrace() << "Finished modifying mail"; |
398 | }) | ||
399 | .then<QByteArray>([oldRemoteId, imap]() { | ||
400 | return oldRemoteId; | 394 | return oldRemoteId; |
401 | }); | 395 | }); |
402 | } | 396 | } |
@@ -416,13 +410,13 @@ public: | |||
416 | SinkTrace() << "Creating a new folder: " << parentFolder << folder.getName(); | 410 | SinkTrace() << "Creating a new folder: " << parentFolder << folder.getName(); |
417 | auto rid = QSharedPointer<QByteArray>::create(); | 411 | auto rid = QSharedPointer<QByteArray>::create(); |
418 | auto createFolder = login.then<QString>(imap->createSubfolder(parentFolder, folder.getName())) | 412 | auto createFolder = login.then<QString>(imap->createSubfolder(parentFolder, folder.getName())) |
419 | .then<void, QString>([imap, rid](const QString &createdFolder) { | 413 | .syncThen<void, QString>([imap, rid](const QString &createdFolder) { |
420 | SinkTrace() << "Finished creating a new folder: " << createdFolder; | 414 | SinkTrace() << "Finished creating a new folder: " << createdFolder; |
421 | *rid = createdFolder.toUtf8(); | 415 | *rid = createdFolder.toUtf8(); |
422 | }); | 416 | }); |
423 | if (folder.getSpecialPurpose().isEmpty()) { | 417 | if (folder.getSpecialPurpose().isEmpty()) { |
424 | return createFolder | 418 | return createFolder |
425 | .then<QByteArray>([rid](){ | 419 | .syncThen<QByteArray>([rid](){ |
426 | return *rid; | 420 | return *rid; |
427 | }); | 421 | }); |
428 | } else { //We try to merge special purpose folders first | 422 | } else { //We try to merge special purpose folders first |
@@ -435,7 +429,7 @@ public: | |||
435 | }; | 429 | }; |
436 | } | 430 | } |
437 | })) | 431 | })) |
438 | .then<void, KAsync::Job<void>>([specialPurposeFolders, folder, imap, parentFolder, rid]() -> KAsync::Job<void> { | 432 | .then<void>([specialPurposeFolders, folder, imap, parentFolder, rid]() -> KAsync::Job<void> { |
439 | for (const auto &purpose : folder.getSpecialPurpose()) { | 433 | for (const auto &purpose : folder.getSpecialPurpose()) { |
440 | if (specialPurposeFolders->contains(purpose)) { | 434 | if (specialPurposeFolders->contains(purpose)) { |
441 | auto f = specialPurposeFolders->value(purpose); | 435 | auto f = specialPurposeFolders->value(purpose); |
@@ -446,13 +440,13 @@ public: | |||
446 | } | 440 | } |
447 | SinkTrace() << "No match found for merging, creating a new folder"; | 441 | SinkTrace() << "No match found for merging, creating a new folder"; |
448 | return imap->createSubfolder(parentFolder, folder.getName()) | 442 | return imap->createSubfolder(parentFolder, folder.getName()) |
449 | .then<void, QString>([imap, rid](const QString &createdFolder) { | 443 | .syncThen<void, QString>([imap, rid](const QString &createdFolder) { |
450 | SinkTrace() << "Finished creating a new folder: " << createdFolder; | 444 | SinkTrace() << "Finished creating a new folder: " << createdFolder; |
451 | *rid = createdFolder.toUtf8(); | 445 | *rid = createdFolder.toUtf8(); |
452 | }); | 446 | }); |
453 | 447 | ||
454 | }) | 448 | }) |
455 | .then<QByteArray>([rid](){ | 449 | .syncThen<QByteArray>([rid](){ |
456 | return *rid; | 450 | return *rid; |
457 | }); | 451 | }); |
458 | return mergeJob; | 452 | return mergeJob; |
@@ -460,7 +454,7 @@ public: | |||
460 | } else if (operation == Sink::Operation_Removal) { | 454 | } else if (operation == Sink::Operation_Removal) { |
461 | SinkTrace() << "Removing a folder: " << oldRemoteId; | 455 | SinkTrace() << "Removing a folder: " << oldRemoteId; |
462 | return login.then<void>(imap->remove(oldRemoteId)) | 456 | return login.then<void>(imap->remove(oldRemoteId)) |
463 | .then<QByteArray>([oldRemoteId, imap]() { | 457 | .syncThen<QByteArray>([oldRemoteId, imap]() { |
464 | SinkTrace() << "Finished removing a folder: " << oldRemoteId; | 458 | SinkTrace() << "Finished removing a folder: " << oldRemoteId; |
465 | return QByteArray(); | 459 | return QByteArray(); |
466 | }); | 460 | }); |
@@ -468,11 +462,11 @@ public: | |||
468 | SinkTrace() << "Renaming a folder: " << oldRemoteId << folder.getName(); | 462 | SinkTrace() << "Renaming a folder: " << oldRemoteId << folder.getName(); |
469 | auto rid = QSharedPointer<QByteArray>::create(); | 463 | auto rid = QSharedPointer<QByteArray>::create(); |
470 | return login.then<QString>(imap->renameSubfolder(oldRemoteId, folder.getName())) | 464 | return login.then<QString>(imap->renameSubfolder(oldRemoteId, folder.getName())) |
471 | .then<void, QString>([imap, rid](const QString &createdFolder) { | 465 | .syncThen<void, QString>([imap, rid](const QString &createdFolder) { |
472 | SinkTrace() << "Finished renaming a folder: " << createdFolder; | 466 | SinkTrace() << "Finished renaming a folder: " << createdFolder; |
473 | *rid = createdFolder.toUtf8(); | 467 | *rid = createdFolder.toUtf8(); |
474 | }) | 468 | }) |
475 | .then<QByteArray>([rid](){ | 469 | .syncThen<QByteArray>([rid](){ |
476 | return *rid; | 470 | return *rid; |
477 | }); | 471 | }); |
478 | } | 472 | } |
@@ -566,7 +560,8 @@ KAsync::Job<void> ImapResource::inspect(int inspectionType, const QByteArray &in | |||
566 | SinkTrace() << "Connecting to:" << mServer << mPort; | 560 | SinkTrace() << "Connecting to:" << mServer << mPort; |
567 | SinkTrace() << "as:" << mUser; | 561 | SinkTrace() << "as:" << mUser; |
568 | auto inspectionJob = imap->login(mUser, mPassword) | 562 | auto inspectionJob = imap->login(mUser, mPassword) |
569 | .then<void>(imap->select(folderRemoteId).then<void>([](){})) | 563 | .then<Imap::SelectResult>(imap->select(folderRemoteId)) |
564 | .syncThen<void, Imap::SelectResult>([](Imap::SelectResult){}) | ||
570 | .then<void>(imap->fetch(set, scope, [imap, messageByUid](const QVector<Imap::Message> &messages) { | 565 | .then<void>(imap->fetch(set, scope, [imap, messageByUid](const QVector<Imap::Message> &messages) { |
571 | for (const auto &m : messages) { | 566 | for (const auto &m : messages) { |
572 | messageByUid->insert(m.uid, m); | 567 | messageByUid->insert(m.uid, m); |
@@ -575,7 +570,7 @@ KAsync::Job<void> ImapResource::inspect(int inspectionType, const QByteArray &in | |||
575 | 570 | ||
576 | if (inspectionType == Sink::ResourceControl::Inspection::PropertyInspectionType) { | 571 | if (inspectionType == Sink::ResourceControl::Inspection::PropertyInspectionType) { |
577 | if (property == "unread") { | 572 | if (property == "unread") { |
578 | return inspectionJob.then<void, KAsync::Job<void>>([=]() { | 573 | return inspectionJob.then<void>([=]() { |
579 | auto msg = messageByUid->value(uid); | 574 | auto msg = messageByUid->value(uid); |
580 | if (expectedValue.toBool() && msg.flags.contains(Imap::Flags::Seen)) { | 575 | if (expectedValue.toBool() && msg.flags.contains(Imap::Flags::Seen)) { |
581 | return KAsync::error<void>(1, "Expected unread but couldn't find it."); | 576 | return KAsync::error<void>(1, "Expected unread but couldn't find it."); |
@@ -587,7 +582,7 @@ KAsync::Job<void> ImapResource::inspect(int inspectionType, const QByteArray &in | |||
587 | }); | 582 | }); |
588 | } | 583 | } |
589 | if (property == "subject") { | 584 | if (property == "subject") { |
590 | return inspectionJob.then<void, KAsync::Job<void>>([=]() { | 585 | return inspectionJob.then<void>([=]() { |
591 | auto msg = messageByUid->value(uid); | 586 | auto msg = messageByUid->value(uid); |
592 | if (msg.msg->subject(true)->asUnicodeString() != expectedValue.toString()) { | 587 | if (msg.msg->subject(true)->asUnicodeString() != expectedValue.toString()) { |
593 | return KAsync::error<void>(1, "Subject not as expected: " + msg.msg->subject(true)->asUnicodeString()); | 588 | return KAsync::error<void>(1, "Subject not as expected: " + msg.msg->subject(true)->asUnicodeString()); |
@@ -597,7 +592,7 @@ KAsync::Job<void> ImapResource::inspect(int inspectionType, const QByteArray &in | |||
597 | } | 592 | } |
598 | } | 593 | } |
599 | if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) { | 594 | if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) { |
600 | return inspectionJob.then<void, KAsync::Job<void>>([=]() { | 595 | return inspectionJob.then<void>([=]() { |
601 | if (!messageByUid->contains(uid)) { | 596 | if (!messageByUid->contains(uid)) { |
602 | SinkWarning() << "Existing messages are: " << messageByUid->keys(); | 597 | SinkWarning() << "Existing messages are: " << messageByUid->keys(); |
603 | SinkWarning() << "We're looking for: " << uid; | 598 | SinkWarning() << "We're looking for: " << uid; |
@@ -628,20 +623,19 @@ KAsync::Job<void> ImapResource::inspect(int inspectionType, const QByteArray &in | |||
628 | scope.mode = KIMAP::FetchJob::FetchScope::Headers; | 623 | scope.mode = KIMAP::FetchJob::FetchScope::Headers; |
629 | auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort); | 624 | auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort); |
630 | auto messageByUid = QSharedPointer<QHash<qint64, Imap::Message>>::create(); | 625 | auto messageByUid = QSharedPointer<QHash<qint64, Imap::Message>>::create(); |
631 | auto inspectionJob = imap->login(mUser, mPassword) | 626 | return imap->login(mUser, mPassword) |
632 | .then<void>(imap->select(remoteId).then<void>([](){})) | 627 | .then<void>(imap->select(remoteId).syncThen<void>([](){})) |
633 | .then<void>(imap->fetch(set, scope, [=](const QVector<Imap::Message> &messages) { | 628 | .then<void>(imap->fetch(set, scope, [=](const QVector<Imap::Message> &messages) { |
634 | for (const auto &m : messages) { | 629 | for (const auto &m : messages) { |
635 | messageByUid->insert(m.uid, m); | 630 | messageByUid->insert(m.uid, m); |
636 | } | 631 | } |
637 | })) | 632 | })) |
638 | .then<void, KAsync::Job<void>>([imap, messageByUid, expectedCount]() { | 633 | .then<void>([imap, messageByUid, expectedCount]() { |
639 | if (messageByUid->size() != expectedCount) { | 634 | if (messageByUid->size() != expectedCount) { |
640 | return KAsync::error<void>(1, QString("Wrong number of messages on the server; found %1 instead of %2.").arg(messageByUid->size()).arg(expectedCount)); | 635 | return KAsync::error<void>(1, QString("Wrong number of messages on the server; found %1 instead of %2.").arg(messageByUid->size()).arg(expectedCount)); |
641 | } | 636 | } |
642 | return KAsync::null<void>(); | 637 | return KAsync::null<void>(); |
643 | }); | 638 | }); |
644 | return inspectionJob; | ||
645 | } | 639 | } |
646 | if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) { | 640 | if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) { |
647 | auto folderByPath = QSharedPointer<QSet<QString>>::create(); | 641 | auto folderByPath = QSharedPointer<QSet<QString>>::create(); |
@@ -655,7 +649,7 @@ KAsync::Job<void> ImapResource::inspect(int inspectionType, const QByteArray &in | |||
655 | *folderByName << f.pathParts.last(); | 649 | *folderByName << f.pathParts.last(); |
656 | } | 650 | } |
657 | })) | 651 | })) |
658 | .then<void, KAsync::Job<void>>([this, folderByName, folderByPath, folder, remoteId, imap]() { | 652 | .then<void>([this, folderByName, folderByPath, folder, remoteId, imap]() { |
659 | if (!folderByName->contains(folder.getName())) { | 653 | if (!folderByName->contains(folder.getName())) { |
660 | SinkWarning() << "Existing folders are: " << *folderByPath; | 654 | SinkWarning() << "Existing folders are: " << *folderByPath; |
661 | SinkWarning() << "We're looking for: " << folder.getName(); | 655 | SinkWarning() << "We're looking for: " << folder.getName(); |