MauiKit Calendar

recurrenceexceptionsmodel.cpp
1// SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#include "recurrenceexceptionsmodel.h"
5#include "kalendar_debug.h"
6#include <QDebug>
7#include <QMetaEnum>
8
9RecurrenceExceptionsModel::RecurrenceExceptionsModel(QObject *parent, KCalendarCore::Incidence::Ptr incidencePtr)
10 : QAbstractListModel(parent)
11 , m_incidence(incidencePtr)
12{
13 for (int i = 0; i < QMetaEnum::fromType<RecurrenceExceptionsModel::Roles>().keyCount(); i++) {
15 const QString key = QLatin1String(roleNames().value(value));
16 m_dataRoles[key] = value;
17 }
18
19 connect(this, &RecurrenceExceptionsModel::incidencePtrChanged, this, &RecurrenceExceptionsModel::updateExceptions);
20}
21
22KCalendarCore::Incidence::Ptr RecurrenceExceptionsModel::incidencePtr()
23{
24 return m_incidence;
25}
26
27void RecurrenceExceptionsModel::setIncidencePtr(KCalendarCore::Incidence::Ptr incidence)
28{
29 if (m_incidence == incidence) {
30 return;
31 }
32 m_incidence = incidence;
33 Q_EMIT incidencePtrChanged();
34 Q_EMIT exceptionsChanged();
36}
37
38QList<QDate> RecurrenceExceptionsModel::exceptions()
39{
40 return m_exceptions;
41}
42
43void RecurrenceExceptionsModel::updateExceptions()
44{
45 m_exceptions.clear();
46
47 const auto dateTimes = m_incidence->recurrence()->exDateTimes();
48 for (const QDateTime &dateTime : dateTimes) {
49 m_exceptions.append(dateTime.date());
50 }
51
52 const auto dates = m_incidence->recurrence()->exDates();
53 for (const QDate &date : dates) {
54 m_exceptions.append(date);
55 }
56 Q_EMIT exceptionsChanged();
58}
59
60QVariantMap RecurrenceExceptionsModel::dataroles()
61{
62 return m_dataRoles;
63}
64
65QVariant RecurrenceExceptionsModel::data(const QModelIndex &idx, int role) const
66{
67 if (!hasIndex(idx.row(), idx.column())) {
68 return {};
69 }
70 const QDate exception = m_exceptions[idx.row()];
71 switch (role) {
72 case DateRole:
73 return exception;
74 default:
75 qCWarning(KALENDAR_LOG) << "Unknown role for incidence:" << QMetaEnum::fromType<Roles>().valueToKey(role);
76 return {};
77 }
78}
79
80QHash<int, QByteArray> RecurrenceExceptionsModel::roleNames() const
81{
82 return {{DateRole, QByteArrayLiteral("date")}};
83}
84
85int RecurrenceExceptionsModel::rowCount(const QModelIndex &) const
86{
87 return m_exceptions.size();
88}
89
90void RecurrenceExceptionsModel::addExceptionDateTime(QDateTime date)
91{
92 if (!date.isValid()) {
93 return;
94 }
95
96 // I don't know why, but different types take different date formats
97 if (m_incidence->recurrence()->allDay()) {
98 m_incidence->recurrence()->addExDateTime(date);
99 } else {
100 m_incidence->recurrence()->addExDate(date.date());
101 }
102
103 updateExceptions();
104}
105
106void RecurrenceExceptionsModel::deleteExceptionDateTime(QDateTime date)
107{
108 if (!date.isValid()) {
109 return;
110 }
111
112 if (m_incidence->recurrence()->allDay()) {
113 auto dateTimes = m_incidence->recurrence()->exDateTimes();
114 dateTimes.removeAt(dateTimes.indexOf(date));
115 m_incidence->recurrence()->setExDateTimes(dateTimes);
116 } else {
117 auto dates = m_incidence->recurrence()->exDates();
118 int removeIndex = dates.indexOf(date.date());
119
120 if (removeIndex >= 0) {
121 dates.removeAt(dates.indexOf(date.date()));
122 m_incidence->recurrence()->setExDates(dates);
123 updateExceptions();
124 return;
125 }
126
127 auto dateTimes = m_incidence->recurrence()->exDateTimes();
128
129 for (int i = 0; i < dateTimes.size(); i++) {
130 if (dateTimes[i].date() == date.date()) {
131 dateTimes.removeAt(i);
132 }
133 }
134 m_incidence->recurrence()->setExDateTimes(dateTimes);
135 }
136
137 updateExceptions();
138}
AKONADI_CALENDAR_EXPORT KCalendarCore::Incidence::Ptr incidence(const Akonadi::Item &item)
bool hasIndex(int row, int column, const QModelIndex &parent) const const
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
QDate date() const const
bool isValid() const const
void append(QList< T > &&value)
void clear()
qsizetype size() const const
QMetaEnum fromType()
int value(int index) const const
const char * valueToKey(int value) const const
int column() const const
int row() const const
Q_EMITQ_EMIT
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:35:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.