File indexing completed on 2024-12-22 05:00:56

0001 /*
0002    SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "unifiedmailbox.h"
0008 #include "common.h"
0009 #include "unifiedmailboxmanager.h"
0010 #include "utils.h"
0011 
0012 #include <KConfigGroup>
0013 
0014 bool UnifiedMailbox::operator==(const UnifiedMailbox &other) const
0015 {
0016     return mId == other.mId;
0017 }
0018 
0019 void UnifiedMailbox::load(const KConfigGroup &group)
0020 {
0021     mId = group.name();
0022     mName = group.readEntry("name");
0023     mIcon = group.readEntry("icon", QStringLiteral("folder-mail"));
0024     mSources = listToSet(group.readEntry("sources", QList<qint64>{}));
0025     // This is not authoritative, we will do collection discovery anyway
0026     mCollectionId = group.readEntry("collectionId", -1ll);
0027 }
0028 
0029 void UnifiedMailbox::save(KConfigGroup &group) const
0030 {
0031     group.writeEntry("name", name());
0032     group.writeEntry("icon", icon());
0033     group.writeEntry("sources", setToList(sourceCollections()));
0034     // just for caching, we will do collection discovery on next start anyway
0035     group.writeEntry("collectionId", collectionId());
0036 }
0037 
0038 bool UnifiedMailbox::isSpecial() const
0039 {
0040     return mId == Common::InboxBoxId || mId == Common::SentBoxId || mId == Common::DraftsBoxId;
0041 }
0042 
0043 void UnifiedMailbox::setCollectionId(qint64 id)
0044 {
0045     mCollectionId = id;
0046 }
0047 
0048 qint64 UnifiedMailbox::collectionId() const
0049 {
0050     return mCollectionId;
0051 }
0052 
0053 void UnifiedMailbox::setId(const QString &id)
0054 {
0055     mId = id;
0056 }
0057 
0058 QString UnifiedMailbox::id() const
0059 {
0060     return mId;
0061 }
0062 
0063 void UnifiedMailbox::setName(const QString &name)
0064 {
0065     mName = name;
0066 }
0067 
0068 QString UnifiedMailbox::name() const
0069 {
0070     return mName;
0071 }
0072 
0073 void UnifiedMailbox::setIcon(const QString &icon)
0074 {
0075     mIcon = icon;
0076 }
0077 
0078 QString UnifiedMailbox::icon() const
0079 {
0080     return mIcon;
0081 }
0082 
0083 void UnifiedMailbox::addSourceCollection(qint64 source)
0084 {
0085     mSources.insert(source);
0086     if (mManager) {
0087         mManager->mMonitor.setCollectionMonitored(Akonadi::Collection{source});
0088         mManager->mSourceToBoxMap.insert({source, this});
0089     }
0090 }
0091 
0092 void UnifiedMailbox::removeSourceCollection(qint64 source)
0093 {
0094     mSources.remove(source);
0095     if (mManager) {
0096         mManager->mMonitor.setCollectionMonitored(Akonadi::Collection{source}, false);
0097         mManager->mSourceToBoxMap.erase(source);
0098     }
0099 }
0100 
0101 void UnifiedMailbox::setSourceCollections(const QSet<qint64> &sources)
0102 {
0103     while (!mSources.empty()) {
0104         removeSourceCollection(*mSources.begin());
0105     }
0106     for (auto source : sources) {
0107         addSourceCollection(source);
0108     }
0109 }
0110 
0111 QSet<qint64> UnifiedMailbox::sourceCollections() const
0112 {
0113     return mSources;
0114 }
0115 
0116 void UnifiedMailbox::attachManager(UnifiedMailboxManager *manager)
0117 {
0118     if (mManager != manager) {
0119         if (manager) {
0120             // Force that we start monitoring all the collections
0121             for (const auto source : std::as_const(mSources)) {
0122                 manager->mMonitor.setCollectionMonitored(Akonadi::Collection{source});
0123                 manager->mSourceToBoxMap.insert({source, this});
0124             }
0125         } else {
0126             for (const auto &source : std::as_const(mSources)) {
0127                 mManager->mMonitor.setCollectionMonitored(Akonadi::Collection{source}, false);
0128                 mManager->mSourceToBoxMap.erase(source);
0129             }
0130         }
0131         mManager = manager;
0132     }
0133 }