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 "notificationdesktopdurationpreferencemodel.h"
0008 #include <KLocalizedString>
0009 
0010 NotificationDesktopDurationPreferenceModel::NotificationDesktopDurationPreferenceModel(QObject *parent)
0011     : QAbstractListModel(parent)
0012 {
0013     fillModel();
0014 }
0015 
0016 NotificationDesktopDurationPreferenceModel::~NotificationDesktopDurationPreferenceModel() = default;
0017 
0018 int NotificationDesktopDurationPreferenceModel::rowCount(const QModelIndex &parent) const
0019 {
0020     Q_UNUSED(parent)
0021     return mNotificationDestktopDurationPreferenceList.count();
0022 }
0023 
0024 QVariant NotificationDesktopDurationPreferenceModel::data(const QModelIndex &index, int role) const
0025 {
0026     const int rowIndex = index.row();
0027     if (rowIndex < 0 || rowIndex >= mNotificationDestktopDurationPreferenceList.count()) {
0028         return {};
0029     }
0030     NotificationDesktopDurationPreferenceInfo preferenceInfo = mNotificationDestktopDurationPreferenceList.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 NotificationDesktopDurationPreferenceModel::fillModel()
0043 {
0044     mNotificationDestktopDurationPreferenceList.reserve(6);
0045     {
0046         // Default ???? Verify it. Perhaps 0 ? Don't know
0047         NotificationDesktopDurationPreferenceInfo preferenceInfo;
0048         preferenceInfo.displayText = i18n("Default");
0049         preferenceInfo.preference = QStringLiteral("default");
0050         mNotificationDestktopDurationPreferenceList.append(std::move(preferenceInfo));
0051     }
0052     for (int i = 1; i <= 5; ++i) {
0053         NotificationDesktopDurationPreferenceInfo preferenceInfo;
0054         preferenceInfo.displayText = i18np("1 second", "%1 seconds", i);
0055         preferenceInfo.preference = QString::number(i);
0056         mNotificationDestktopDurationPreferenceList.append(std::move(preferenceInfo));
0057     }
0058 }
0059 
0060 int NotificationDesktopDurationPreferenceModel::setCurrentNotificationPreference(const QString &preference)
0061 {
0062     int newStatusIndex = 0;
0063     for (int i = 0; i < mNotificationDestktopDurationPreferenceList.count(); ++i) {
0064         if (mNotificationDestktopDurationPreferenceList.at(i).preference == preference) {
0065             newStatusIndex = i;
0066             break;
0067         }
0068     }
0069     if (mCurrentPreference != newStatusIndex) {
0070         mCurrentPreference = newStatusIndex;
0071         Q_EMIT currentNotificationPreferenceChanged();
0072     }
0073     return mCurrentPreference;
0074 }
0075 
0076 QString NotificationDesktopDurationPreferenceModel::currentPreference(int index) const
0077 {
0078     const QString str = mNotificationDestktopDurationPreferenceList.at(index).preference;
0079     return str;
0080 }
0081 
0082 #include "moc_notificationdesktopdurationpreferencemodel.cpp"