File indexing completed on 2024-12-15 04:54:38

0001 /******************************************************************************
0002  *
0003  *  SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  *
0007  *******************************************************************************/
0008 
0009 #pragma once
0010 
0011 #include "core/sortorder.h"
0012 #include <QList>
0013 #include <QMap>
0014 #include <QObject>
0015 
0016 #include <Akonadi/Collection>
0017 
0018 namespace KMime
0019 {
0020 class DateFormatter;
0021 }
0022 
0023 namespace MessageList
0024 {
0025 namespace Core
0026 {
0027 class Aggregation;
0028 class Theme;
0029 class StorageModel;
0030 class Widget;
0031 
0032 /**
0033  * @brief: The manager for all the existing MessageList::Widget objects.
0034  *
0035  * This class is the "central" object of the whole MessageList framework.
0036  * It's a singleton that can be accessed only by the means of static methods,
0037  * is created automatically when the first MessageList::Widget object is created
0038  * and destroyed automatically when the last MessageList::Widget object is destroyed.
0039  *
0040  * This class takes care of loading/storing/maintaining the settings for the
0041  * whole MessageList framework. It also keeps track of all the existing
0042  * MessageList::Widget objects and takes care of updating them when settings change.
0043  */
0044 class Manager : public QObject
0045 {
0046     Q_OBJECT
0047 protected:
0048     explicit Manager();
0049     ~Manager() override;
0050 
0051 private:
0052     static Manager *mInstance;
0053     QList<Widget *> mWidgetList;
0054     QMap<QString, Aggregation *> mAggregations;
0055     QMap<QString, Theme *> mThemes;
0056     KMime::DateFormatter *const mDateFormatter;
0057     const QString mCachedLocalizedUnknownText;
0058 
0059 public:
0060     // instance management
0061     static Manager *instance()
0062     {
0063         return mInstance;
0064     }
0065 
0066     // widget registration
0067     static void registerWidget(Widget *pWidget);
0068     static void unregisterWidget(Widget *pWidget);
0069 
0070     const KMime::DateFormatter *dateFormatter() const
0071     {
0072         return mDateFormatter;
0073     }
0074 
0075     const QString &cachedLocalizedUnknownText() const
0076     {
0077         return mCachedLocalizedUnknownText;
0078     }
0079 
0080     // aggregation sets management
0081     const Aggregation *aggregationForStorageModel(const StorageModel *storageModel, bool *storageUsesPrivateAggregation);
0082     const Aggregation *aggregationForStorageModel(const QString &storageModel, bool *storageUsesPrivateAggregation);
0083     const Aggregation *aggregationForStorageModel(const Akonadi::Collection &storageModel, bool *storageUsesPrivateAggregation);
0084 
0085     void saveAggregationForStorageModel(const StorageModel *storageModel, const QString &id, bool storageUsesPrivateAggregation);
0086     void saveAggregationForStorageModel(const QString &index, const QString &id, bool storageUsesPrivateAggregation);
0087     void saveAggregationForStorageModel(const Akonadi::Collection &col, const QString &id, bool storageUsesPrivateAggregation);
0088 
0089     const Aggregation *defaultAggregation();
0090     const Aggregation *aggregation(const QString &id);
0091 
0092     void addAggregation(Aggregation *set);
0093     void removeAllAggregations();
0094 
0095     const QMap<QString, Aggregation *> &aggregations() const
0096     {
0097         return mAggregations;
0098     }
0099 
0100     /**
0101      * This is called by the aggregation configuration dialog
0102      * once the sets have been changed.
0103      */
0104     void aggregationsConfigurationCompleted();
0105 
0106     // sort order management
0107     const SortOrder sortOrderForStorageModel(const StorageModel *storageModel, bool *storageUsesPrivateSortOrder);
0108     void saveSortOrderForStorageModel(const StorageModel *storageModel, SortOrder order, bool storageUsesPrivateSortOrder);
0109 
0110     // theme sets management
0111     const Theme *themeForStorageModel(const Akonadi::Collection &col, bool *storageUsesPrivateTheme);
0112     const Theme *themeForStorageModel(const StorageModel *storageModel, bool *storageUsesPrivateTheme);
0113     const Theme *themeForStorageModel(const QString &id, bool *storageUsesPrivateTheme);
0114 
0115     void saveThemeForStorageModel(const StorageModel *storageModel, const QString &id, bool storageUsesPrivateTheme);
0116     void saveThemeForStorageModel(int index, const QString &id, bool storageUsesPrivateTheme);
0117     void saveThemeForStorageModel(const QString &storageModelIndex, const QString &id, bool storageUsesPrivateTheme);
0118 
0119     const Theme *defaultTheme();
0120     const Theme *theme(const QString &id);
0121 
0122     void addTheme(Theme *set);
0123     void removeAllThemes();
0124 
0125     const QMap<QString, Theme *> &themes() const
0126     {
0127         return mThemes;
0128     }
0129 
0130     /**
0131      * This is called by the theme configuration dialog
0132      * once the sets have been changed.
0133      */
0134     void themesConfigurationCompleted();
0135 
0136 protected Q_SLOTS:
0137     /**
0138      * Reloads the global configuration from the config files (so we assume it has changed)
0139      * The settings private to MessageList (like Themes or Aggregations) aren't reloaded.
0140      * If the global configuration has changed then all the views are reloaded.
0141      */
0142     void reloadGlobalConfiguration();
0143 
0144     /**
0145      * Explicitly reloads the contents of all the widgets.
0146      */
0147     void reloadAllWidgets();
0148 
0149 Q_SIGNALS:
0150     void aggregationsChanged();
0151     void themesChanged();
0152 
0153 private:
0154     // internal configuration stuff
0155     void loadConfiguration();
0156     void saveConfiguration();
0157     void loadGlobalConfiguration();
0158     void saveGlobalConfiguration();
0159 
0160     // internal option set management
0161     void createDefaultAggregations();
0162     void createDefaultThemes();
0163 };
0164 } // namespace Core
0165 } // namespace MessageList