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

0001 /* SPDX-FileCopyrightText: 2009 James Bendig <james@imptalk.com>
0002 
0003    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 #include "themecombobox.h"
0006 
0007 #include "utils/themecombobox.h"
0008 #include "utils/themecombobox_p.h"
0009 
0010 #include "core/manager.h"
0011 #include "core/theme.h"
0012 #include "messagelistsettings.h"
0013 #include "storagemodel.h"
0014 
0015 using namespace MessageList::Core;
0016 using namespace MessageList::Utils;
0017 
0018 ThemeComboBox::ThemeComboBox(QWidget *parent)
0019     : QComboBox(parent)
0020     , d(new ThemeComboBoxPrivate(this))
0021 {
0022     if (Manager::instance()) {
0023         d->slotLoadThemes();
0024     } else {
0025         setEnabled(false);
0026     }
0027 }
0028 
0029 ThemeComboBox::~ThemeComboBox() = default;
0030 
0031 QString ThemeComboBox::currentTheme() const
0032 {
0033     return itemData(currentIndex()).toString();
0034 }
0035 
0036 void ThemeComboBox::writeDefaultConfig() const
0037 {
0038     KConfigGroup group(MessageListSettings::self()->config(), QStringLiteral("MessageListView::StorageModelThemes"));
0039 
0040     const QString themeID = currentTheme();
0041     group.writeEntry(QStringLiteral("DefaultSet"), themeID);
0042     if (Manager::instance()) {
0043         Manager::instance()->themesConfigurationCompleted();
0044     }
0045 }
0046 
0047 void ThemeComboBox::writeStorageModelConfig(MessageList::Core::StorageModel *storageModel, bool isPrivateSetting) const
0048 {
0049     writeStorageModelConfig(storageModel->id(), isPrivateSetting);
0050 }
0051 
0052 void ThemeComboBox::writeStorageModelConfig(const QString &id, bool isPrivateSetting) const
0053 {
0054     if (Manager::instance()) {
0055         QString themeID;
0056         if (isPrivateSetting) {
0057             themeID = currentTheme();
0058         } else { // explicitly use default theme id when using default theme.
0059             themeID = Manager::instance()->defaultTheme()->id();
0060         }
0061         Manager::instance()->saveThemeForStorageModel(id, themeID, isPrivateSetting);
0062         Manager::instance()->themesConfigurationCompleted();
0063     }
0064 }
0065 
0066 void ThemeComboBox::readStorageModelConfig(const Akonadi::Collection &col, bool &isPrivateSetting)
0067 {
0068     if (Manager::instance()) {
0069         const Theme *theme = Manager::instance()->themeForStorageModel(col, &isPrivateSetting);
0070         d->setCurrentTheme(theme);
0071     }
0072 }
0073 
0074 void ThemeComboBox::readStorageModelConfig(MessageList::Core::StorageModel *storageModel, bool &isPrivateSetting)
0075 {
0076     if (Manager::instance()) {
0077         const Theme *theme = Manager::instance()->themeForStorageModel(storageModel, &isPrivateSetting);
0078         d->setCurrentTheme(theme);
0079     }
0080 }
0081 
0082 void ThemeComboBox::selectDefault()
0083 {
0084     if (Manager::instance()) {
0085         const Theme *defaultTheme = Manager::instance()->defaultTheme();
0086         d->setCurrentTheme(defaultTheme);
0087     }
0088 }
0089 
0090 void ThemeComboBox::slotLoadThemes()
0091 {
0092     d->slotLoadThemes();
0093 }
0094 
0095 void ThemeComboBoxPrivate::slotLoadThemes()
0096 {
0097     if (!Manager::instance()) {
0098         return;
0099     }
0100     q->clear();
0101 
0102     // Get all message list themes and sort them into alphabetical order.
0103     QList<Theme *> themes = Manager::instance()->themes().values();
0104     std::sort(themes.begin(), themes.end(), MessageList::Core::Theme::compareName);
0105 
0106     for (const Theme *theme : std::as_const(themes)) {
0107         q->addItem(theme->name(), QVariant(theme->id()));
0108     }
0109 }
0110 
0111 void ThemeComboBoxPrivate::setCurrentTheme(const Theme *theme)
0112 {
0113     Q_ASSERT(theme != nullptr);
0114 
0115     const QString themeID = theme->id();
0116     const int themeIndex = q->findData(QVariant(themeID));
0117     q->setCurrentIndex(themeIndex);
0118 }
0119 
0120 #include "moc_themecombobox.cpp"