File indexing completed on 2024-11-24 04:50:37

0001 // SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "recurrenceexceptionsmodel.h"
0005 #include "merkuro_calendar_debug.h"
0006 #include <QMetaEnum>
0007 
0008 RecurrenceExceptionsModel::RecurrenceExceptionsModel(QObject *parent, KCalendarCore::Incidence::Ptr incidencePtr)
0009     : QAbstractListModel(parent)
0010     , m_incidence(incidencePtr)
0011 {
0012     for (int i = 0; i < QMetaEnum::fromType<RecurrenceExceptionsModel::Roles>().keyCount(); i++) {
0013         const int value = QMetaEnum::fromType<RecurrenceExceptionsModel::Roles>().value(i);
0014         const QString key = QLatin1StringView(roleNames().value(value));
0015         m_dataRoles[key] = value;
0016     }
0017 
0018     connect(this, &RecurrenceExceptionsModel::incidencePtrChanged, this, &RecurrenceExceptionsModel::updateExceptions);
0019 }
0020 
0021 KCalendarCore::Incidence::Ptr RecurrenceExceptionsModel::incidencePtr()
0022 {
0023     return m_incidence;
0024 }
0025 
0026 void RecurrenceExceptionsModel::setIncidencePtr(KCalendarCore::Incidence::Ptr incidence)
0027 {
0028     if (m_incidence == incidence) {
0029         return;
0030     }
0031     m_incidence = incidence;
0032     Q_EMIT incidencePtrChanged();
0033     Q_EMIT exceptionsChanged();
0034     Q_EMIT layoutChanged();
0035 }
0036 
0037 QList<QDate> RecurrenceExceptionsModel::exceptions()
0038 {
0039     return m_exceptions;
0040 }
0041 
0042 void RecurrenceExceptionsModel::updateExceptions()
0043 {
0044     m_exceptions.clear();
0045 
0046     const auto dateTimes = m_incidence->recurrence()->exDateTimes();
0047     for (const QDateTime &dateTime : dateTimes) {
0048         m_exceptions.append(dateTime.date());
0049     }
0050 
0051     const auto dates = m_incidence->recurrence()->exDates();
0052     for (const QDate &date : dates) {
0053         m_exceptions.append(date);
0054     }
0055     Q_EMIT exceptionsChanged();
0056     Q_EMIT layoutChanged();
0057 }
0058 
0059 QVariantMap RecurrenceExceptionsModel::dataroles()
0060 {
0061     return m_dataRoles;
0062 }
0063 
0064 QVariant RecurrenceExceptionsModel::data(const QModelIndex &idx, int role) const
0065 {
0066     if (!hasIndex(idx.row(), idx.column())) {
0067         return {};
0068     }
0069     const QDate exception = m_exceptions[idx.row()];
0070     switch (role) {
0071     case DateRole:
0072         return exception;
0073     default:
0074         qCWarning(MERKURO_CALENDAR_LOG) << "Unknown role for incidence:" << QMetaEnum::fromType<Roles>().valueToKey(role);
0075         return {};
0076     }
0077 }
0078 
0079 QHash<int, QByteArray> RecurrenceExceptionsModel::roleNames() const
0080 {
0081     return {{DateRole, QByteArrayLiteral("date")}};
0082 }
0083 
0084 int RecurrenceExceptionsModel::rowCount(const QModelIndex &) const
0085 {
0086     return m_exceptions.size();
0087 }
0088 
0089 void RecurrenceExceptionsModel::addExceptionDateTime(QDateTime date)
0090 {
0091     if (!date.isValid()) {
0092         return;
0093     }
0094 
0095     // I don't know why, but different types take different date formats
0096     if (m_incidence->recurrence()->allDay()) {
0097         m_incidence->recurrence()->addExDateTime(date);
0098     } else {
0099         m_incidence->recurrence()->addExDate(date.date());
0100     }
0101 
0102     updateExceptions();
0103 }
0104 
0105 void RecurrenceExceptionsModel::deleteExceptionDateTime(QDateTime date)
0106 {
0107     if (!date.isValid()) {
0108         return;
0109     }
0110 
0111     if (m_incidence->recurrence()->allDay()) {
0112         auto dateTimes = m_incidence->recurrence()->exDateTimes();
0113         dateTimes.removeAt(dateTimes.indexOf(date));
0114         m_incidence->recurrence()->setExDateTimes(dateTimes);
0115     } else {
0116         auto dates = m_incidence->recurrence()->exDates();
0117         int removeIndex = dates.indexOf(date.date());
0118 
0119         if (removeIndex >= 0) {
0120             dates.removeAt(dates.indexOf(date.date()));
0121             m_incidence->recurrence()->setExDates(dates);
0122             updateExceptions();
0123             return;
0124         }
0125 
0126         auto dateTimes = m_incidence->recurrence()->exDateTimes();
0127 
0128         for (int i = 0; i < dateTimes.size(); i++) {
0129             if (dateTimes[i].date() == date.date()) {
0130                 dateTimes.removeAt(i);
0131             }
0132         }
0133         m_incidence->recurrence()->setExDateTimes(dateTimes);
0134     }
0135 
0136     updateExceptions();
0137 }
0138 
0139 #include "moc_recurrenceexceptionsmodel.cpp"