From fd7cff5da4b33be1e7606c516f7dda00397600b8 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 14 Nov 2016 13:25:44 +0100 Subject: Made the use of the folder struct a bit more expressive --- examples/imapresource/imapresource.cpp | 19 +++++++----- examples/imapresource/imapserverproxy.cpp | 11 ++++--- examples/imapresource/imapserverproxy.h | 51 ++++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp index 302d681..d3949ae 100644 --- a/examples/imapresource/imapresource.cpp +++ b/examples/imapresource/imapresource.cpp @@ -119,7 +119,7 @@ public: [&folderList](const QByteArray &remoteId) -> bool { // folderList.contains(remoteId) for (const auto &folderPath : folderList) { - if (folderPath.path == remoteId) { + if (folderPath.path() == remoteId) { return true; } } @@ -128,7 +128,7 @@ public: ); for (const auto &f : folderList) { - createFolder(f.pathParts.last(), f.path, f.parentPath(), "folder"); + createFolder(f.name(), f.path(), f.parentPath(), "folder"); } } @@ -190,12 +190,12 @@ public: KAsync::Job synchronizeFolder(QSharedPointer imap, const Imap::Folder &folder) { QSet uids; - SinkLog() << "Synchronizing mails" << folder.normalizedPath(); + SinkLog() << "Synchronizing mails" << folder.path(); auto capabilities = imap->getCapabilities(); bool canDoIncrementalRemovals = false; return KAsync::start([=]() { //First we fetch flag changes for all messages. Since we don't know which messages are locally available we just get everything and only apply to what we have. - SinkLog() << "About to update flags" << folder.normalizedPath(); + SinkLog() << "About to update flags" << folder.path(); auto uidNext = syncStore().readValue(folder.normalizedPath().toUtf8() + "uidnext").toLongLong(); const auto changedsince = syncStore().readValue(folder.normalizedPath().toUtf8() + "changedsince").toLongLong(); return imap->fetchFlags(folder, KIMAP2::ImapSet(1, qMax(uidNext, qint64(1))), changedsince, [this, folder](const Message &message) { @@ -361,7 +361,10 @@ public: if (folder.noselect) { return KAsync::null(); } - return synchronizeFolder(imap, folder); + return synchronizeFolder(imap, folder) + .onError([folder](const KAsync::Error &error) { + SinkWarning() << "Failed to sync folder: ." << folder.normalizedPath(); + }); }); return job; @@ -492,8 +495,8 @@ public: auto specialPurposeFolders = QSharedPointer>::create(); auto mergeJob = imap->login(mUser, mPassword) .then(imap->fetchFolders([=](const Imap::Folder &folder) { - if (SpecialPurpose::isSpecialPurposeFolderName(folder.pathParts.last())) { - specialPurposeFolders->insert(SpecialPurpose::getSpecialPurposeType(folder.pathParts.last()), folder.path); + if (SpecialPurpose::isSpecialPurposeFolderName(folder.name())) { + specialPurposeFolders->insert(SpecialPurpose::getSpecialPurposeType(folder.name()), folder.path()); }; })) .then([specialPurposeFolders, folder, imap, parentFolder, rid]() -> KAsync::Job { @@ -707,7 +710,7 @@ KAsync::Job ImapResource::inspect(int inspectionType, const QByteArray &in auto inspectionJob = imap->login(mUser, mPassword) .then(imap->fetchFolders([=](const Imap::Folder &f) { *folderByPath << f.normalizedPath(); - *folderByName << f.pathParts.last(); + *folderByName << f.name(); })) .then([this, folderByName, folderByPath, folder, remoteId, imap]() { if (!folderByName->contains(folder.getName())) { diff --git a/examples/imapresource/imapserverproxy.cpp b/examples/imapresource/imapserverproxy.cpp index 56ddf89..d6f0c7f 100644 --- a/examples/imapresource/imapserverproxy.cpp +++ b/examples/imapresource/imapserverproxy.cpp @@ -157,6 +157,8 @@ KAsync::Job ImapServerProxy::select(const QString &mailbox) select->setCondstoreEnabled(mCapabilities.contains("CONDSTORE")); return runJob(select, [select](KJob* job) -> SelectResult { return {select->uidValidity(), select->nextUid(), select->highestModSequence()}; + }).onError([=] (const KAsync::Error &error) { + SinkWarning() << "Select failed: " << mailbox; }); } @@ -388,17 +390,16 @@ KAsync::Job ImapServerProxy::fetchFolders(std::function &flags){ bool noselect = flags.contains(QByteArray(FolderFlags::Noselect).toLower()) || flags.contains(QByteArray(FolderFlags::Noselect)); SinkLog() << "Found mailbox: " << mailbox.name << flags << FolderFlags::Noselect << noselect; - callback(Folder{mailbox.name.split(mailbox.separator), mailbox.name, mailbox.separator, noselect}); + callback(Folder{mailbox.name, mailbox.separator, noselect}); }); } QString ImapServerProxy::mailboxFromFolder(const Folder &folder) const { - if (folder.path.isEmpty()) { - Q_ASSERT(!mPersonalNamespaceSeparator.isNull()); - return folder.pathParts.join(mPersonalNamespaceSeparator); + if (folder.path().isEmpty()) { + return folder.path(mPersonalNamespaceSeparator); } else { - return folder.path; + return folder.path(); } } diff --git a/examples/imapresource/imapserverproxy.h b/examples/imapresource/imapserverproxy.h index 6e6626e..22527c0 100644 --- a/examples/imapresource/imapserverproxy.h +++ b/examples/imapresource/imapserverproxy.h @@ -59,35 +59,64 @@ struct Message { struct Folder { Folder() = default; - Folder(QList pathParts_, const QString &path_, const QChar &separator_, bool noselect_) - : pathParts(pathParts_), - path(path_), - separator(separator_), - noselect(noselect_) + Folder(const QString &path, const QChar &separator, bool noselect_) + : noselect(noselect_), + mPath(path), + pathParts(path.split(separator)), + mSeparator(separator) { } Folder(const QString &path_) - : path(path_) + : mPath(path_) { } QString normalizedPath() const { - return pathParts.join('/'); + return path('/'); + } + + QString path(const QChar &s) const + { + Q_ASSERT(!s.isNull()); + return pathParts.join(s); + } + + QString path() const + { + Q_ASSERT(!mPath.isEmpty()); + return mPath; } QString parentPath() const { + Q_ASSERT(!mSeparator.isNull()); auto parts = pathParts; parts.removeLast(); - return parts.join(separator); + return parts.join(mSeparator); + } + + QString normalizedParentPath() const + { + Q_ASSERT(!pathParts.isEmpty()); + auto parts = pathParts; + parts.removeLast(); + return parts.join('/'); + } + + QString name() const + { + Q_ASSERT(!pathParts.isEmpty()); + return pathParts.last(); } - QList pathParts; - QString path; - QChar separator; bool noselect = false; + +private: + QString mPath; + QList pathParts; + QChar mSeparator; }; struct SelectResult { -- cgit v1.2.3