File indexing completed on 2025-01-26 05:01:09

0001 /*
0002     SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
0003     SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "configstorage.h"
0009 #include "../../config-ICU.h"
0010 
0011 #include <QMetaEnum>
0012 
0013 #include <KSharedConfig>
0014 
0015 CalendarSystemModel::CalendarSystemModel(QObject *parent)
0016     : QAbstractListModel(parent)
0017 {
0018     const QMetaEnum e = QMetaEnum::fromType<CalendarSystem::System>();
0019 
0020     beginInsertRows(QModelIndex(), 0, e.keyCount() - 2 /* Gregorian */);
0021 
0022     m_items.reserve(e.keyCount() - 1);
0023     for (int k = 0; k < e.keyCount(); k++) {
0024         const auto system = static_cast<CalendarSystem::System>(e.value(k));
0025 
0026         auto it = std::find_if(s_calendarMap.cbegin(), s_calendarMap.cend(), [system](const std::pair<QString, CalendarSystemItem> &pr) {
0027             return pr.second.system == system;
0028         });
0029 
0030         if (it != s_calendarMap.cend()) {
0031             m_items.emplace_back(it->second);
0032         }
0033     }
0034 
0035     endInsertRows();
0036 }
0037 
0038 QVariant CalendarSystemModel::data(const QModelIndex &index, int role) const
0039 {
0040     if (!index.isValid()) {
0041         return QVariant();
0042     }
0043 
0044     const auto &item = m_items.at(index.row());
0045 
0046     switch (role) {
0047     case Qt::DisplayRole:
0048         return item.text;
0049     case IdRole:
0050         return item.id;
0051     default:
0052         return QVariant();
0053     }
0054 }
0055 
0056 int CalendarSystemModel::rowCount(const QModelIndex &parent) const
0057 {
0058     return parent.isValid() ? 0 : m_items.size();
0059 }
0060 
0061 QHash<int, QByteArray> CalendarSystemModel::roleNames() const
0062 {
0063     return {
0064         {Qt::DisplayRole, QByteArrayLiteral("display")},
0065         {IdRole, QByteArrayLiteral("id")},
0066     };
0067 }
0068 
0069 int CalendarSystemModel::indexOf(const QString &id) const
0070 {
0071     const auto it = std::find_if(m_items.cbegin(), m_items.cend(), [&id](const CalendarSystemItem &item) {
0072         return item.id == id;
0073     });
0074 
0075     if (it != m_items.cend()) {
0076         return std::distance(m_items.cbegin(), it);
0077     }
0078 
0079     return 0;
0080 }
0081 
0082 ConfigStorage::ConfigStorage(QObject *parent)
0083     : QObject(parent)
0084     , m_calendarSystemModel(new CalendarSystemModel(this))
0085 {
0086     auto config = KSharedConfig::openConfig(QStringLiteral("plasma_calendar_alternatecalendar"));
0087     m_generalConfigGroup = config->group("General");
0088 
0089     m_calendarSystem = m_generalConfigGroup.readEntry("calendarSystem", "Gregorian");
0090     m_dateOffset = m_generalConfigGroup.readEntry("dateOffset", 0);
0091 }
0092 
0093 QAbstractListModel *ConfigStorage::calendarSystemModel() const
0094 {
0095     return m_calendarSystemModel;
0096 }
0097 
0098 int ConfigStorage::currentIndex() const
0099 {
0100     return m_calendarSystemModel->indexOf(m_calendarSystem);
0101 }
0102 
0103 void ConfigStorage::save()
0104 {
0105     m_generalConfigGroup.writeEntry("calendarSystem", m_calendarSystem, KConfigBase::Notify);
0106     m_generalConfigGroup.writeEntry("dateOffset", m_dateOffset, KConfigBase::Notify);
0107 
0108     m_generalConfigGroup.sync();
0109 }