From 9bed2d578b6f1c4552ac7ade631b039a65a0c055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Vr=C3=A1til?= Date: Sat, 28 Jul 2018 15:04:58 +0200 Subject: [PATCH] Move string constants to a separate file --- agents/unifiedmailboxagent/common.h | 41 +++++++++++++ .../unifiedmailboxagent.cpp | 10 +--- .../unifiedmailboxmanager.cpp | 57 ++++++++++--------- .../unifiedmailboxmanager.h | 8 +-- 4 files changed, 76 insertions(+), 40 deletions(-) create mode 100644 agents/unifiedmailboxagent/common.h diff --git a/agents/unifiedmailboxagent/common.h b/agents/unifiedmailboxagent/common.h new file mode 100644 index 000000000..6c2a99607 --- /dev/null +++ b/agents/unifiedmailboxagent/common.h @@ -0,0 +1,41 @@ +/* + Copyright (C) 2018 Daniel Vrátil + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef COMMON_H +#define COMMON_H + +#include + +namespace Common { + +static const auto MailMimeType = QStringLiteral("message/rfc822"); + +static const auto InboxBoxId = QStringLiteral("inbox"); +static const auto SentBoxId = QStringLiteral("sent-mail"); +static const auto DraftsBoxId = QStringLiteral("drafts"); + +static constexpr auto SpecialCollectionInbox = "inbox"; +static constexpr auto SpecialCollectionSentMail = "send-mail"; +static constexpr auto SpecialCollectionDrafts = "drafts"; + +static const auto AgentIdentifier = QStringLiteral("akonadi_unifiedmailbox_agent"); + +} + +#endif diff --git a/agents/unifiedmailboxagent/unifiedmailboxagent.cpp b/agents/unifiedmailboxagent/unifiedmailboxagent.cpp index e6c7bf8ef..1446ec551 100644 --- a/agents/unifiedmailboxagent/unifiedmailboxagent.cpp +++ b/agents/unifiedmailboxagent/unifiedmailboxagent.cpp @@ -22,6 +22,7 @@ #include "unifiedmailboxagent_debug.h" #include "settingsdialog.h" #include "settings.h" +#include "common.h" #include #include @@ -47,13 +48,6 @@ #include #include -namespace { -static const auto MailMimeType = QStringLiteral("message/rfc822"); -static const auto Inbox = QStringLiteral("inbox"); -static const auto Sent = QStringLiteral("sent-mail"); -static const auto Drafts = QStringLiteral("drafts"); -} - UnifiedMailboxAgent::UnifiedMailboxAgent(const QString &id) : Akonadi::ResourceBase(id) @@ -121,7 +115,7 @@ void UnifiedMailboxAgent::retrieveCollections() col.setName(box->id()); col.setRemoteId(box->id()); col.setParentCollection(topLevel); - col.setContentMimeTypes({MailMimeType}); + col.setContentMimeTypes({Common::MailMimeType}); col.setRights(Akonadi::Collection::CanChangeItem | Akonadi::Collection::CanDeleteItem); col.setVirtual(true); auto displayAttr = col.attribute(Akonadi::Collection::AddIfMissing); diff --git a/agents/unifiedmailboxagent/unifiedmailboxmanager.cpp b/agents/unifiedmailboxagent/unifiedmailboxmanager.cpp index 7f7d7947c..cf9f5f434 100644 --- a/agents/unifiedmailboxagent/unifiedmailboxmanager.cpp +++ b/agents/unifiedmailboxagent/unifiedmailboxmanager.cpp @@ -21,6 +21,7 @@ #include "unifiedmailbox.h" #include "unifiedmailboxagent_debug.h" #include "utils.h" +#include "common.h" #include #include @@ -63,7 +64,7 @@ private: // static bool UnifiedMailboxManager::isUnifiedMailbox(const Akonadi::Collection &col) { - return col.resource() == QLatin1String("akonadi_unifiedmailbox_agent"); + return col.resource() == Common::AgentIdentifier; } UnifiedMailboxManager::UnifiedMailboxManager(KSharedConfigPtr config, QObject* parent) @@ -188,7 +189,7 @@ UnifiedMailboxManager::~UnifiedMailboxManager() { } -void UnifiedMailboxManager::loadBoxes(LoadCallback &&cb) +void UnifiedMailboxManager::loadBoxes(FinishedCallback &&finishedCb) { const auto group = mConfig->group("UnifiedMailboxes"); const auto boxGroups = group.groupList(); @@ -200,16 +201,16 @@ void UnifiedMailboxManager::loadBoxes(LoadCallback &&cb) } if (mMailboxes.empty()) { - createDefaultBoxes(std::move(cb)); + createDefaultBoxes(std::move(finishedCb)); } else { - discoverBoxCollections([this, cb = std::move(cb)]() { + discoverBoxCollections([this, finishedCb = std::move(finishedCb)]() { // Only now start processing changes from change recorder connect(&mMonitor, &Akonadi::ChangeRecorder::changesAdded, &mMonitor, &Akonadi::ChangeRecorder::replayNext, Qt::QueuedConnection); // And start replaying any potentially pending notification mMonitor.replayNext(); - if (cb) { - cb(); + if (finishedCb) { + finishedCb(); } }); } @@ -280,26 +281,26 @@ UnifiedMailbox * UnifiedMailboxManager::unifiedMailboxFromCollection(const Akona return box->second.get(); } -void UnifiedMailboxManager::createDefaultBoxes(LoadCallback &&cb) +void UnifiedMailboxManager::createDefaultBoxes(FinishedCallback &&finishedCb) { // First build empty boxes auto inbox = std::make_unique(); inbox->attachManager(this); - inbox->setId(QStringLiteral("inbox")); + inbox->setId(Common::InboxBoxId); inbox->setName(i18n("Inbox")); inbox->setIcon(QStringLiteral("mail-folder-inbox")); insertBox(std::move(inbox)); auto sent = std::make_unique(); sent->attachManager(this); - sent->setId(QStringLiteral("sent-mail")); + sent->setId(Common::SentBoxId); sent->setName(i18n("Sent")); sent->setIcon(QStringLiteral("mail-folder-sent")); insertBox(std::move(sent)); auto drafts = std::make_unique(); drafts->attachManager(this); - drafts->setId(QStringLiteral("drafts")); + drafts->setId(Common::DraftsBoxId); drafts->setName(i18n("Drafts")); drafts->setIcon(QStringLiteral("document-properties")); insertBox(std::move(drafts)); @@ -317,13 +318,13 @@ void UnifiedMailboxManager::createDefaultBoxes(LoadCallback &&cb) try { switch (Akonadi::SpecialMailCollections::self()->specialCollectionType(col)) { case Akonadi::SpecialMailCollections::Inbox: - mMailboxes.at(QStringLiteral("inbox"))->addSourceCollection(col.id()); + mMailboxes.at(Common::InboxBoxId)->addSourceCollection(col.id()); break; case Akonadi::SpecialMailCollections::SentMail: - mMailboxes.at(QStringLiteral("sent-mail"))->addSourceCollection(col.id()); + mMailboxes.at(Common::SentBoxId)->addSourceCollection(col.id()); break; case Akonadi::SpecialMailCollections::Drafts: - mMailboxes.at(QStringLiteral("drafts"))->addSourceCollection(col.id()); + mMailboxes.at(Common::DraftsBoxId)->addSourceCollection(col.id()); break; default: continue; @@ -335,22 +336,22 @@ void UnifiedMailboxManager::createDefaultBoxes(LoadCallback &&cb) } }); connect(list, &Akonadi::CollectionFetchJob::finished, - this, [this, cb = std::move(cb)]() { + this, [this, finishedCb = std::move(finishedCb)]() { saveBoxes(); - if (cb) { - cb(); + if (finishedCb) { + finishedCb(); } }); } -void UnifiedMailboxManager::discoverBoxCollections(LoadCallback &&cb) +void UnifiedMailboxManager::discoverBoxCollections(FinishedCallback &&finishedCb) { auto list = new Akonadi::CollectionFetchJob(Akonadi::Collection::root(), Akonadi::CollectionFetchJob::Recursive, this); - list->fetchScope().setResource(QStringLiteral("akonadi_unifiedmailbox_agent")); + list->fetchScope().setResource(Common::AgentIdentifier); connect(list, &Akonadi::CollectionFetchJob::collectionsReceived, this, [this](const Akonadi::Collection::List &list) { for (const auto &col : list) { - if (col.name() == QLatin1String("akonadi_unifiedmailbox_agent")) { + if (isUnifiedMailbox(col)) { continue; } @@ -358,9 +359,9 @@ void UnifiedMailboxManager::discoverBoxCollections(LoadCallback &&cb) } }); connect(list, &Akonadi::CollectionFetchJob::finished, - this, [cb = std::move(cb)]() { - if (cb) { - cb(); + this, [finishedCb = std::move(finishedCb)]() { + if (finishedCb) { + finishedCb(); } }); } @@ -377,12 +378,12 @@ const UnifiedMailbox *UnifiedMailboxManager::registerSpecialSourceCollection(con } decltype(mMailboxes)::iterator box; - if (attr->collectionType() == "inbox") { - box = mMailboxes.find(QStringLiteral("inbox")); - } else if (attr->collectionType() == "sent-mail") { - box = mMailboxes.find(QStringLiteral("sent-mail")); - } else if (attr->collectionType() == "drafts") { - box = mMailboxes.find(QStringLiteral("drafts")); + if (attr->collectionType() == Common::SpecialCollectionInbox) { + box = mMailboxes.find(Common::InboxBoxId); + } else if (attr->collectionType() == Common::SpecialCollectionSentMail) { + box = mMailboxes.find(Common::SentBoxId); + } else if (attr->collectionType() == Common::SpecialCollectionDrafts) { + box = mMailboxes.find(Common::DraftsBoxId); } if (box == mMailboxes.end()) { return {}; diff --git a/agents/unifiedmailboxagent/unifiedmailboxmanager.h b/agents/unifiedmailboxagent/unifiedmailboxmanager.h index f7f7dde5e..d0d2ad612 100644 --- a/agents/unifiedmailboxagent/unifiedmailboxmanager.h +++ b/agents/unifiedmailboxagent/unifiedmailboxmanager.h @@ -40,12 +40,12 @@ class UnifiedMailboxManager : public QObject Q_OBJECT friend class UnifiedMailbox; public: - using LoadCallback = std::function; + using FinishedCallback = std::function; explicit UnifiedMailboxManager(KSharedConfigPtr config, QObject *parent = nullptr); ~UnifiedMailboxManager() override; - void loadBoxes(LoadCallback &&cb = {}); + void loadBoxes(FinishedCallback &&cb = {}); void saveBoxes(); void insertBox(std::unique_ptr box); @@ -64,7 +64,7 @@ public: return mMailboxes.end(); } - void discoverBoxCollections(LoadCallback &&cb); + void discoverBoxCollections(FinishedCallback &&cb); static bool isUnifiedMailbox(const Akonadi::Collection &col); @@ -72,7 +72,7 @@ Q_SIGNALS: void updateBox(const UnifiedMailbox *box); private: - void createDefaultBoxes(LoadCallback &&cb); + void createDefaultBoxes(FinishedCallback &&cb); const UnifiedMailbox *unregisterSpecialSourceCollection(qint64 colId); const UnifiedMailbox *registerSpecialSourceCollection(const Akonadi::Collection &col);