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 #pragma once
0008 
0009 #include <QObject>
0010 #include <QSet>
0011 #include <QSettings>
0012 
0013 #include <KSharedConfig>
0014 
0015 #include <Akonadi/ChangeRecorder>
0016 
0017 #include <functional>
0018 #include <memory>
0019 #include <unordered_map>
0020 
0021 class UnifiedMailbox;
0022 class UnifiedMailboxManager : public QObject
0023 {
0024     Q_OBJECT
0025     friend class UnifiedMailbox;
0026 
0027 public:
0028     using FinishedCallback = std::function<void()>;
0029     using Entry = std::pair<const QString, std::unique_ptr<UnifiedMailbox>>;
0030 
0031     explicit UnifiedMailboxManager(const KSharedConfigPtr &config, QObject *parent = nullptr);
0032     ~UnifiedMailboxManager() override;
0033 
0034     void loadBoxes(FinishedCallback &&cb = {});
0035     void saveBoxes();
0036     void discoverBoxCollections(FinishedCallback &&cb = {});
0037 
0038     void insertBox(std::unique_ptr<UnifiedMailbox> box);
0039     void removeBox(const QString &id);
0040 
0041     [[nodiscard]] UnifiedMailbox *unifiedMailboxForSource(qint64 source) const;
0042     [[nodiscard]] UnifiedMailbox *unifiedMailboxFromCollection(const Akonadi::Collection &col) const;
0043 
0044     inline auto begin() const
0045     {
0046         return mMailboxes.begin();
0047     }
0048 
0049     inline auto end() const
0050     {
0051         return mMailboxes.end();
0052     }
0053 
0054     [[nodiscard]] static bool isUnifiedMailbox(const Akonadi::Collection &col);
0055 
0056     // Internal change recorder, for unittests
0057     Akonadi::ChangeRecorder &changeRecorder();
0058 Q_SIGNALS:
0059     void updateBox(const UnifiedMailbox *box);
0060 
0061 private:
0062     void createDefaultBoxes(FinishedCallback &&cb);
0063 
0064     const UnifiedMailbox *unregisterSpecialSourceCollection(qint64 colId);
0065     const UnifiedMailbox *registerSpecialSourceCollection(const Akonadi::Collection &col);
0066 
0067     // Using std::unique_ptr because QScopedPointer is not movable
0068     // Using std::unordered_map because Qt containers do not support movable-only types,
0069     std::unordered_map<QString, std::unique_ptr<UnifiedMailbox>> mMailboxes;
0070     std::unordered_map<qint64, UnifiedMailbox *> mSourceToBoxMap;
0071 
0072     Akonadi::ChangeRecorder mMonitor;
0073     QSettings mMonitorSettings;
0074 
0075     KSharedConfigPtr mConfig;
0076 };