File indexing completed on 2024-12-08 12:53:34

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "notificationpreferencemodel.h"
0008 #include <KLocalizedString>
0009 NotificationPreferenceModel::NotificationPreferenceModel(QObject *parent)
0010     : QAbstractListModel(parent)
0011 {
0012     fillModel();
0013 }
0014 
0015 NotificationPreferenceModel::~NotificationPreferenceModel() = default;
0016 
0017 int NotificationPreferenceModel::rowCount(const QModelIndex &parent) const
0018 {
0019     Q_UNUSED(parent)
0020     return mNotificationPreferenceList.count();
0021 }
0022 
0023 QVariant NotificationPreferenceModel::data(const QModelIndex &index, int role) const
0024 {
0025     if (index.row() < 0 || index.row() >= mNotificationPreferenceList.count()) {
0026         return {};
0027     }
0028     NotificationPreferenceInfo preferenceInfo = mNotificationPreferenceList.at(index.row());
0029     switch (role) {
0030     case Qt::DisplayRole:
0031     case NotificationPreferenceI18nRole:
0032         return preferenceInfo.displayText;
0033     case NotificationPreferenceRole:
0034         return preferenceInfo.preference;
0035     }
0036 
0037     return {};
0038 }
0039 
0040 void NotificationPreferenceModel::fillModel()
0041 {
0042     mNotificationPreferenceList.reserve(4);
0043     {
0044         NotificationPreferenceInfo preferenceInfo;
0045         preferenceInfo.displayText = i18n("Default");
0046         preferenceInfo.preference = QStringLiteral("default");
0047         mNotificationPreferenceList.append(std::move(preferenceInfo));
0048     }
0049     {
0050         NotificationPreferenceInfo preferenceInfo;
0051         preferenceInfo.displayText = i18n("All Messages");
0052         preferenceInfo.preference = QStringLiteral("all");
0053         mNotificationPreferenceList.append(std::move(preferenceInfo));
0054     }
0055     {
0056         NotificationPreferenceInfo preferenceInfo;
0057         preferenceInfo.displayText = i18n("Mentions");
0058         preferenceInfo.preference = QStringLiteral("mentions");
0059         mNotificationPreferenceList.append(std::move(preferenceInfo));
0060     }
0061     {
0062         NotificationPreferenceInfo preferenceInfo;
0063         preferenceInfo.displayText = i18n("Nothing");
0064         preferenceInfo.preference = QStringLiteral("nothing");
0065         mNotificationPreferenceList.append(std::move(preferenceInfo));
0066     }
0067 }
0068 
0069 int NotificationPreferenceModel::setCurrentNotificationPreference(const QString &preference)
0070 {
0071     int newStatusIndex = 0;
0072     for (int i = 0, total = mNotificationPreferenceList.count(); i < total; ++i) {
0073         if (mNotificationPreferenceList.at(i).preference == preference) {
0074             newStatusIndex = i;
0075             break;
0076         }
0077     }
0078     if (mCurrentPreference != newStatusIndex) {
0079         mCurrentPreference = newStatusIndex;
0080         Q_EMIT currentNotificationPreferenceChanged();
0081     }
0082     return mCurrentPreference;
0083 }
0084 
0085 QString NotificationPreferenceModel::currentPreference(int index) const
0086 {
0087     const QString str = mNotificationPreferenceList.at(index).preference;
0088     return str;
0089 }
0090 
0091 #include "moc_notificationpreferencemodel.cpp"