From b05edd5644738e7608d13a8b5b679f43d70f4dd4 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 22 Jan 2017 18:35:52 +0100 Subject: Don't treat the IMAP namespace as subfolder. INBOX.INBOX means that the INBOX folder is in the INBOX. namespace. --- examples/imapresource/imapserverproxy.cpp | 43 ++++++++++++++++------ examples/imapresource/imapserverproxy.h | 17 ++++++--- .../imapresource/tests/imapserverproxytest.cpp | 14 ++++++- 3 files changed, 57 insertions(+), 17 deletions(-) (limited to 'examples/imapresource') diff --git a/examples/imapresource/imapserverproxy.cpp b/examples/imapresource/imapserverproxy.cpp index a98483b..d5c212d 100644 --- a/examples/imapresource/imapserverproxy.cpp +++ b/examples/imapresource/imapserverproxy.cpp @@ -370,16 +370,16 @@ KAsync::Job ImapServerProxy::createSubfolder(const QString &parentMailb { return KAsync::start([this, parentMailbox, folderName]() { Q_ASSERT(!mPersonalNamespaceSeparator.isNull()); - auto folder = QSharedPointer::create(); + QString folder; if (parentMailbox.isEmpty()) { - *folder = mPersonalNamespaces.toList().first() + folderName; + folder = mPersonalNamespaces.isEmpty() ? "" : mPersonalNamespaces.toList().first() + folderName; } else { - *folder = parentMailbox + mPersonalNamespaceSeparator + folderName; + folder = parentMailbox + mPersonalNamespaceSeparator + folderName; } - SinkTrace() << "Creating subfolder: " << *folder; - return create(*folder) + SinkTrace() << "Creating subfolder: " << folder; + return create(folder) .then([=]() { - return *folder; + return folder; }); }); } @@ -390,15 +390,35 @@ KAsync::Job ImapServerProxy::renameSubfolder(const QString &oldMailbox, Q_ASSERT(!mPersonalNamespaceSeparator.isNull()); auto parts = oldMailbox.split(mPersonalNamespaceSeparator); parts.removeLast(); - auto folder = QSharedPointer::create(parts.join(mPersonalNamespaceSeparator) + mPersonalNamespaceSeparator + newName); - SinkTrace() << "Renaming subfolder: " << oldMailbox << *folder; - return rename(oldMailbox, *folder) + QString folder = parts.join(mPersonalNamespaceSeparator) + mPersonalNamespaceSeparator + newName; + SinkTrace() << "Renaming subfolder: " << oldMailbox << folder; + return rename(oldMailbox, folder) .then([=]() { - return *folder; + return folder; }); }); } +QString ImapServerProxy::getNamespace(const QString &name) +{ + for (const auto &ns : mPersonalNamespaces) { + if (name.startsWith(ns)) { + return ns; + } + } + for (const auto &ns : mSharedNamespaces) { + if (name.startsWith(ns)) { + return ns; + } + } + for (const auto &ns : mUserNamespaces) { + if (name.startsWith(ns)) { + return ns; + } + } + return QString{}; +} + KAsync::Job ImapServerProxy::fetchFolders(std::function callback) { SinkTrace() << "Fetching folders"; @@ -409,7 +429,8 @@ KAsync::Job ImapServerProxy::fetchFolders(std::functioncontains(mailbox.name); SinkLog() << "Found mailbox: " << mailbox.name << flags << FolderFlags::Noselect << noselect << " sub: " << subscribed; - callback(Folder{mailbox.name, mailbox.separator, noselect, subscribed, flags}); + auto ns = getNamespace(mailbox.name); + callback(Folder{mailbox.name, ns, mailbox.separator, noselect, subscribed, flags}); })); } diff --git a/examples/imapresource/imapserverproxy.h b/examples/imapresource/imapserverproxy.h index 2175ca1..6785fc2 100644 --- a/examples/imapresource/imapserverproxy.h +++ b/examples/imapresource/imapserverproxy.h @@ -67,12 +67,12 @@ struct Message { struct Folder { Folder() = default; - Folder(const QString &path, const QChar &separator, bool noselect_, bool subscribed_, const QByteArrayList &flags_) + Folder(const QString &path, const QString &ns, const QChar &separator, bool noselect_, bool subscribed_, const QByteArrayList &flags_) : noselect(noselect_), subscribed(subscribed_), flags(flags_), mPath(path), - pathParts(path.split(separator)), + mNamespace(ns), mSeparator(separator) { } @@ -91,13 +91,19 @@ struct Folder { QString parentPath() const { Q_ASSERT(!mSeparator.isNull()); - auto parts = pathParts; + auto parts = mPath.split(mSeparator); parts.removeLast(); - return parts.join(mSeparator); + auto parentPath = parts.join(mSeparator); + //Don't return the namespace for root folders as parent folder + if (mNamespace.startsWith(parentPath)) { + return QString{}; + } + return parentPath; } QString name() const { + auto pathParts = mPath.split(mSeparator); Q_ASSERT(!pathParts.isEmpty()); return pathParts.last(); } @@ -108,7 +114,7 @@ struct Folder { private: QString mPath; - QList pathParts; + QString mNamespace; QChar mSeparator; }; @@ -178,6 +184,7 @@ public: KAsync::Job> fetchUids(const Folder &folder); private: + QString getNamespace(const QString &name); QObject mGuard; }; diff --git a/examples/imapresource/tests/imapserverproxytest.cpp b/examples/imapresource/tests/imapserverproxytest.cpp index 27d73ea..ec5df49 100644 --- a/examples/imapresource/tests/imapserverproxytest.cpp +++ b/examples/imapresource/tests/imapserverproxytest.cpp @@ -57,9 +57,21 @@ private slots: void testFetchFolders() { + QMap expectedFolderAndParent; + expectedFolderAndParent.insert("INBOX", ""); + expectedFolderAndParent.insert("Drafts", ""); + expectedFolderAndParent.insert("Trash", ""); + expectedFolderAndParent.insert("test", ""); ImapServerProxy imap("localhost", 993); VERIFYEXEC(imap.login("doe", "doe")); - VERIFYEXEC(imap.fetchFolders([](const Folder &){})); + QVector list; + VERIFYEXEC(imap.fetchFolders([&](const Folder &f){ list << f;})); + for (const auto &f : list) { + QVERIFY(expectedFolderAndParent.contains(f.name())); + QCOMPARE(expectedFolderAndParent.value(f.name()), f.parentPath()); + expectedFolderAndParent.remove(f.name()); + } + QVERIFY(expectedFolderAndParent.isEmpty()); } void testFetchFoldersFailure() -- cgit v1.2.3