From 4cd598035dcc297ad3a3af16fb5eda218c018a16 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 30 Aug 2016 00:35:03 +0200 Subject: Flag updates --- examples/imapresource/imapresource.cpp | 19 +++++++++-- examples/imapresource/imapserverproxy.cpp | 29 ++++++++++++++--- examples/imapresource/imapserverproxy.h | 1 + examples/imapresource/tests/imapmailsynctest.cpp | 8 +++++ .../maildirresource/tests/maildirmailsynctest.cpp | 7 ++++ tests/mailsynctest.cpp | 38 +++++++++++++++++++++- tests/mailsynctest.h | 2 ++ 7 files changed, 97 insertions(+), 7 deletions(-) diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp index aad7887..3a7be2d 100644 --- a/examples/imapresource/imapresource.cpp +++ b/examples/imapresource/imapresource.cpp @@ -219,8 +219,23 @@ public: SinkLog() << "Synchronizing mails" << folder.normalizedPath(); auto capabilities = imap->getCapabilities(); bool canDoIncrementalRemovals = false; - return KAsync::syncStart([=]() { - //TODO update flags + return KAsync::start([=]() { + const auto changedsince = syncStore().readValue(folder.normalizedPath().toUtf8() + "changedsince").toLongLong(); + // auto changedsince = QSharedPointer::create(0); + //FIXME this should generate a compiletime error + return imap->fetchFlags(folder, changedsince, [this, folder](const QVector &messages) { + // synchronizeMails(folder.normalizedPath(), messages); + const auto folderLocalId = syncStore().resolveRemoteId(ENTITY_TYPE_FOLDER, folder.normalizedPath().toUtf8()); + for (const auto &message : messages) { + const auto remoteId = assembleMailRid(folderLocalId, message.uid); + + + auto mail = Sink::ApplicationDomain::Mail::create(mResourceInstanceIdentifier); + mail.setUnread(!message.flags.contains(Imap::Flags::Seen)); + mail.setImportant(message.flags.contains(Imap::Flags::Flagged)); + createOrModify(ENTITY_TYPE_MAIL, remoteId, mail); + } + }); }) .then([=]() { //TODO Remove what's no longer existing diff --git a/examples/imapresource/imapserverproxy.cpp b/examples/imapresource/imapserverproxy.cpp index 25cbc57..4ce413a 100644 --- a/examples/imapresource/imapserverproxy.cpp +++ b/examples/imapresource/imapserverproxy.cpp @@ -416,6 +416,27 @@ QString ImapServerProxy::mailboxFromFolder(const Folder &folder) const return folder.pathParts.join(mPersonalNamespaceSeparator); } +KAsync::Job ImapServerProxy::fetchFlags(const Folder &folder, qint64 changedsince, std::function &)> callback) +{ + SinkTrace() << "Fetching flags " << folder.normalizedPath(); + return select(mailboxFromFolder(folder)).then([=](const SelectResult &selectResult) -> KAsync::Job { + SinkTrace() << "Modeseq " << folder.normalizedPath() << selectResult.highestModSequence << changedsince; + + if (selectResult.highestModSequence == static_cast(changedsince)) { + SinkTrace()<< folder.normalizedPath() << "Changedsince didn't change, nothing to do."; + return KAsync::null(); + } + + SinkTrace() << "Fetching flags " << folder.normalizedPath() << selectResult.highestModSequence << changedsince; + + KIMAP2::FetchJob::FetchScope scope; + scope.mode = KIMAP2::FetchJob::FetchScope::Flags; + scope.changedSince = changedsince; + + return fetch(KIMAP2::ImapSet(1, 0), scope, callback); + }); +} + KAsync::Job ImapServerProxy::fetchMessages(const Folder &folder, qint64 uidNext, std::function &)> callback, std::function progress) { auto time = QSharedPointer::create(); @@ -423,14 +444,14 @@ KAsync::Job ImapServerProxy::fetchMessages(const Folder &folder, qint64 ui Q_ASSERT(!mPersonalNamespaceSeparator.isNull()); return select(mailboxFromFolder(folder)).then([this, callback, folder, time, progress, uidNext](const SelectResult &selectResult) -> KAsync::Job { - SinkLog() << "UIDNEXT " << selectResult.uidNext << uidNext; + SinkTrace() << "UIDNEXT " << folder.normalizedPath() << selectResult.uidNext << uidNext; if (selectResult.uidNext == (uidNext + 1)) { - SinkTrace() << "Uidnext didn't change, nothing to do."; + SinkTrace()<< folder.normalizedPath() << "Uidnext didn't change, nothing to do."; return KAsync::null(); } - return fetchHeaders(mailboxFromFolder(folder), (uidNext + 1)).then>([this, callback, time, progress](const QList &uidsToFetch){ - SinkTrace() << "Fetched headers"; + return fetchHeaders(mailboxFromFolder(folder), (uidNext + 1)).then>([this, callback, time, progress, folder](const QList &uidsToFetch){ + SinkTrace() << "Fetched headers" << folder.normalizedPath(); SinkTrace() << " Total: " << uidsToFetch.size(); SinkTrace() << " Uids to fetch: " << uidsToFetch; SinkTrace() << " Took: " << Sink::Log::TraceTime(time->elapsed()); diff --git a/examples/imapresource/imapserverproxy.h b/examples/imapresource/imapserverproxy.h index 9adce3d..83efc38 100644 --- a/examples/imapresource/imapserverproxy.h +++ b/examples/imapresource/imapserverproxy.h @@ -138,6 +138,7 @@ public: KAsync::Job fetchFolders(std::function &)> callback); KAsync::Job fetchMessages(const Folder &folder, std::function &)> callback, std::function progress = std::function()); KAsync::Job fetchMessages(const Folder &folder, qint64 uidNext, std::function &)> callback, std::function progress = std::function()); + KAsync::Job fetchFlags(const Folder &folder, qint64 changedsince, std::function &)> callback); KAsync::Job> fetchUids(const Folder &folder); private: diff --git a/examples/imapresource/tests/imapmailsynctest.cpp b/examples/imapresource/tests/imapmailsynctest.cpp index fcc659d..f908412 100644 --- a/examples/imapresource/tests/imapmailsynctest.cpp +++ b/examples/imapresource/tests/imapmailsynctest.cpp @@ -104,6 +104,14 @@ protected: VERIFYEXEC(imap.login("doe", "doe")); VERIFYEXEC(imap.remove("INBOX." + folderPath.join('.'), messages)); } + + void markAsImportant(const QStringList &folderPath, const QByteArray &messageIdentifier) Q_DECL_OVERRIDE + { + Imap::ImapServerProxy imap("localhost", 993); + VERIFYEXEC(imap.login("doe", "doe")); + VERIFYEXEC(imap.select("INBOX." + folderPath.join('.'))); + VERIFYEXEC(imap.addFlags(KIMAP2::ImapSet::fromImapSequenceSet(messageIdentifier), QByteArrayList() << Imap::Flags::Flagged)); + } }; QTEST_MAIN(ImapMailSyncTest) diff --git a/examples/maildirresource/tests/maildirmailsynctest.cpp b/examples/maildirresource/tests/maildirmailsynctest.cpp index 444fb42..f2cf3e0 100644 --- a/examples/maildirresource/tests/maildirmailsynctest.cpp +++ b/examples/maildirresource/tests/maildirmailsynctest.cpp @@ -125,6 +125,13 @@ protected: KPIM::Maildir maildir(rootPath + folderPath.join('/')); maildir.removeEntry(messageIdentifier); } + + void markAsImportant(const QStringList &folderPath, const QByteArray &messageIdentifier) Q_DECL_OVERRIDE + { + auto rootPath = tempDir.path() + "/maildir1/"; + KPIM::Maildir maildir(rootPath + folderPath.join('/')); + maildir.changeEntryFlags(messageIdentifier, KPIM::Maildir::Flagged); + } }; QTEST_MAIN(MaildirMailSyncTest) diff --git a/tests/mailsynctest.cpp b/tests/mailsynctest.cpp index 9c57f0a..8260978 100644 --- a/tests/mailsynctest.cpp +++ b/tests/mailsynctest.cpp @@ -344,7 +344,43 @@ void MailSyncTest::testFetchNewRemovedMessages() } } -//TODO test flag sync +void MailSyncTest::testFlagChange() +{ + Sink::Query query; + query.resources << mResourceInstanceIdentifier; + query.request().request(); + + auto msg = KMime::Message::Ptr::create(); + msg->subject(true)->fromUnicodeString("Foobar", "utf8"); + msg->assemble(); + auto messageIdentifier = createMessage(QStringList() << "test", msg->encodedContent(true)); + + Store::synchronize(query).exec().waitForFinished(); + ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + { + auto job = Store::fetchAll(query).syncThen>([](const QList &mails) { + QCOMPARE(mails.size(), 2); + QVERIFY(!mails.at(1)->getImportant()); + }); + VERIFYEXEC(job); + } + + markAsImportant(QStringList() << "test", messageIdentifier); + + // Ensure all local data is processed + VERIFYEXEC(Store::synchronize(query)); + ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + { + auto job = Store::fetchAll(query).syncThen>([](const QList &mails) { + QCOMPARE(mails.size(), 2); + QVERIFY(mails.at(1)->getImportant()); + }); + VERIFYEXEC(job); + } + +} void MailSyncTest::testFailingSync() { diff --git a/tests/mailsynctest.h b/tests/mailsynctest.h index 31d3f03..94643f6 100644 --- a/tests/mailsynctest.h +++ b/tests/mailsynctest.h @@ -50,6 +50,7 @@ protected: virtual void removeFolder(const QStringList &folderPath) = 0; virtual QByteArray createMessage(const QStringList &folderPath, const QByteArray &message) = 0; virtual void removeMessage(const QStringList &folderPath, const QByteArray &messageIdentifier) = 0; + virtual void markAsImportant(const QStringList &folderPath, const QByteArray &messageIdentifier) = 0; private slots: void initTestCase(); @@ -66,6 +67,7 @@ private slots: void testListMails(); void testResyncMails(); void testFetchNewRemovedMessages(); + void testFlagChange(); void testFailingSync(); }; -- cgit v1.2.3