File indexing completed on 2024-05-05 05:28:21

0001 /*
0002  * SPDX-FileCopyrightText: 2019 Dimitris Kardarakos <dimkard@posteo.net>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "recurrenceperiodmodel.h"
0008 #include <KCalendarCore/Recurrence>
0009 #include <KLocalizedString>
0010 
0011 using namespace KCalendarCore;
0012 
0013 ReccurencePeriodModel::ReccurencePeriodModel(QObject *parent): QAbstractListModel(parent)
0014 {
0015     initialize();
0016 }
0017 
0018 ReccurencePeriodModel::~ReccurencePeriodModel() = default;
0019 
0020 void ReccurencePeriodModel::initialize()
0021 {
0022     beginResetModel();
0023 
0024     m_periodtypes = {
0025         {.periodType = Recurrence::rNone, .periodTypeDesc = periodDecription(Recurrence::rNone)},
0026         {.periodType = Recurrence::rYearlyMonth, .periodTypeDesc = periodDecription(Recurrence::rYearlyMonth)},
0027         {.periodType = Recurrence::rMonthlyDay, .periodTypeDesc = periodDecription(Recurrence::rMonthlyDay)},
0028         {.periodType = Recurrence::rWeekly, .periodTypeDesc = periodDecription(Recurrence::rWeekly)},
0029         {.periodType = Recurrence::rDaily, .periodTypeDesc = periodDecription(Recurrence::rDaily)},
0030     };
0031 
0032     endResetModel();
0033 }
0034 
0035 QHash<int, QByteArray> ReccurencePeriodModel::roleNames() const
0036 {
0037     return {
0038         {RepeatDescriptionRole, "repeatDescription"},
0039         {RepeatCodeRole, "repeatCode"}
0040     };
0041 }
0042 
0043 QVariant ReccurencePeriodModel::data(const QModelIndex &index, int role) const
0044 {
0045     if (!index.isValid()) {
0046         return QStringLiteral("Invalid index");
0047     }
0048 
0049     switch (role) {
0050     case Qt::DisplayRole:
0051         return m_periodtypes.at(index.row()).periodTypeDesc;
0052     case RepeatDescriptionRole:
0053         return m_periodtypes.at(index.row()).periodTypeDesc;
0054     case RepeatCodeRole:
0055         return m_periodtypes.at(index.row()).periodType;
0056     default:
0057         return QLatin1String();
0058     }
0059 }
0060 
0061 int ReccurencePeriodModel::rowCount(const QModelIndex &parent) const
0062 {
0063     if (parent.isValid()) {
0064         return 0;
0065     }
0066 
0067     return m_periodtypes.count();
0068 }
0069 
0070 ushort ReccurencePeriodModel::noRepeat() const
0071 {
0072     return Recurrence::rNone;
0073 }
0074 
0075 ushort ReccurencePeriodModel::repeatYearlyDay() const
0076 {
0077     return Recurrence::rYearlyDay;
0078 }
0079 
0080 ushort ReccurencePeriodModel::repeatYearlyMonth() const
0081 {
0082     return Recurrence::rYearlyMonth;
0083 }
0084 
0085 ushort ReccurencePeriodModel::repeatYearlyPos() const
0086 {
0087     return Recurrence::rYearlyPos;
0088 }
0089 
0090 ushort ReccurencePeriodModel::repeatMonthlyDay() const
0091 {
0092     return Recurrence::rMonthlyDay;
0093 }
0094 
0095 ushort ReccurencePeriodModel::repeatMonthlyPos() const
0096 {
0097     return Recurrence::rMonthlyPos;
0098 }
0099 
0100 ushort ReccurencePeriodModel::repeatWeekly() const
0101 {
0102     return Recurrence::rWeekly;
0103 }
0104 
0105 ushort ReccurencePeriodModel::repeatDaily() const
0106 {
0107     return Recurrence::rDaily;
0108 }
0109 
0110 QString ReccurencePeriodModel::periodDecription(const int periodType) const
0111 {
0112     switch (periodType) {
0113     case Recurrence::rNone:
0114         return i18n("Do not repeat");
0115     case Recurrence::rYearlyDay:
0116     case Recurrence::rYearlyMonth:
0117     case Recurrence::rYearlyPos:
0118         return i18n("Yearly");
0119     case Recurrence::rMonthlyDay:
0120     case Recurrence::rMonthlyPos:
0121         return i18n("Monthly");
0122     case Recurrence::rWeekly:
0123         return i18n("Weekly");
0124     case Recurrence::rDaily:
0125         return i18n("Daily");
0126     default:
0127         return QString();
0128     }
0129 }
0130 
0131 QString ReccurencePeriodModel::repeatDescription(const int repeatType, const int repeatEvery, const int stopAfter) const
0132 {
0133     return QStringLiteral("%1%2").arg((repeatType == Recurrence::rYearlyMonth || repeatType == Recurrence::rYearlyDay || repeatType == Recurrence::rYearlyPos) ? i18np("Every year", "Every %1 years", repeatEvery) :
0134                                (repeatType == Recurrence::rMonthlyPos || repeatType == Recurrence::rMonthlyDay) ? i18np("Every month", "Every %1 months", repeatEvery) :
0135                                (repeatType == Recurrence::rWeekly) ? i18np("Every week", "Every %1 weeks", repeatEvery) :
0136                                (repeatType == Recurrence::rDaily) ? i18np("Every day", "Every %1 days", repeatEvery) : i18n("Never"), (repeatType == Recurrence::rNone) || (stopAfter < 1) ? QString() : i18np("; once", "; %1 times", stopAfter));
0137 }