File indexing completed on 2024-05-12 16:25:50

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 "notificationdesktopsoundpreferencemodel.h"
0008 #include <KLocalizedString>
0009 
0010 NotificationDesktopSoundPreferenceModel::NotificationDesktopSoundPreferenceModel(QObject *parent)
0011     : QAbstractListModel(parent)
0012 {
0013     fillModel();
0014 }
0015 
0016 NotificationDesktopSoundPreferenceModel::~NotificationDesktopSoundPreferenceModel() = default;
0017 
0018 int NotificationDesktopSoundPreferenceModel::rowCount(const QModelIndex &parent) const
0019 {
0020     Q_UNUSED(parent)
0021     return mNotificationDestktopSoundPreferenceList.count();
0022 }
0023 
0024 QVariant NotificationDesktopSoundPreferenceModel::data(const QModelIndex &index, int role) const
0025 {
0026     const int rowIndex = index.row();
0027     if (rowIndex < 0 || rowIndex >= mNotificationDestktopSoundPreferenceList.count()) {
0028         return {};
0029     }
0030     NotificationDesktopSoundPreferenceInfo preferenceInfo = mNotificationDestktopSoundPreferenceList.at(rowIndex);
0031     switch (role) {
0032     case Qt::DisplayRole:
0033     case NotificationPreferenceI18n:
0034         return preferenceInfo.displayText;
0035     case NotificationPreference:
0036         return preferenceInfo.preference;
0037     }
0038 
0039     return {};
0040 }
0041 
0042 void NotificationDesktopSoundPreferenceModel::fillModel()
0043 {
0044     mNotificationDestktopSoundPreferenceList.reserve(8);
0045     // This one must be first one.
0046     {
0047         NotificationDesktopSoundPreferenceInfo preferenceInfo;
0048         preferenceInfo.displayText = i18n("Default");
0049         preferenceInfo.preference = QStringLiteral("default");
0050         mNotificationDestktopSoundPreferenceList.append(std::move(preferenceInfo));
0051     }
0052     {
0053         NotificationDesktopSoundPreferenceInfo preferenceInfo;
0054         preferenceInfo.displayText = i18n("Nothing");
0055         preferenceInfo.preference = QStringLiteral("none");
0056         mNotificationDestktopSoundPreferenceList.append(std::move(preferenceInfo));
0057     }
0058     {
0059         NotificationDesktopSoundPreferenceInfo preferenceInfo;
0060         preferenceInfo.displayText = i18n("Beep");
0061         preferenceInfo.preference = QStringLiteral("beep");
0062         mNotificationDestktopSoundPreferenceList.append(std::move(preferenceInfo));
0063     }
0064     {
0065         NotificationDesktopSoundPreferenceInfo preferenceInfo;
0066         preferenceInfo.displayText = i18n("Chelle");
0067         preferenceInfo.preference = QStringLiteral("chelle");
0068         mNotificationDestktopSoundPreferenceList.append(std::move(preferenceInfo));
0069     }
0070     {
0071         NotificationDesktopSoundPreferenceInfo preferenceInfo;
0072         preferenceInfo.displayText = i18n("Ding");
0073         preferenceInfo.preference = QStringLiteral("ding");
0074         mNotificationDestktopSoundPreferenceList.append(std::move(preferenceInfo));
0075     }
0076     {
0077         NotificationDesktopSoundPreferenceInfo preferenceInfo;
0078         preferenceInfo.displayText = i18n("Droplet");
0079         preferenceInfo.preference = QStringLiteral("droplet");
0080         mNotificationDestktopSoundPreferenceList.append(std::move(preferenceInfo));
0081     }
0082     {
0083         NotificationDesktopSoundPreferenceInfo preferenceInfo;
0084         preferenceInfo.displayText = i18n("Highbell");
0085         preferenceInfo.preference = QStringLiteral("highbell");
0086         mNotificationDestktopSoundPreferenceList.append(std::move(preferenceInfo));
0087     }
0088     {
0089         NotificationDesktopSoundPreferenceInfo preferenceInfo;
0090         preferenceInfo.displayText = i18n("Seasons");
0091         preferenceInfo.preference = QStringLiteral("seasons");
0092         mNotificationDestktopSoundPreferenceList.append(std::move(preferenceInfo));
0093     }
0094 }
0095 
0096 int NotificationDesktopSoundPreferenceModel::setCurrentNotificationPreference(const QString &preference)
0097 {
0098     int newStatusIndex = 0;
0099     for (int i = 0; i < mNotificationDestktopSoundPreferenceList.count(); ++i) {
0100         if (mNotificationDestktopSoundPreferenceList.at(i).preference == preference) {
0101             newStatusIndex = i;
0102             break;
0103         }
0104     }
0105     if (mCurrentPreference != newStatusIndex) {
0106         mCurrentPreference = newStatusIndex;
0107         Q_EMIT currentNotificationPreferenceChanged();
0108     }
0109     return mCurrentPreference;
0110 }
0111 
0112 QString NotificationDesktopSoundPreferenceModel::currentPreference(int index) const
0113 {
0114     const QString str = mNotificationDestktopSoundPreferenceList.at(index).preference;
0115     return str;
0116 }
0117 
0118 #include "moc_notificationdesktopsoundpreferencemodel.cpp"