Eventviews

viewcalendar.cpp
1/*
2 * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
5 */
6
7#include "viewcalendar.h"
8#include "agendaview.h"
9#include "calendarview_debug.h"
10#include "helper.h"
11
12#include <Akonadi/CalendarUtils>
13#include <Akonadi/EntityTreeModel>
14
15#include <QAbstractProxyModel>
16#include <QSharedPointer>
17
18#include <ranges>
19
20using namespace EventViews;
21
22ViewCalendar::~ViewCalendar() = default;
23
24MultiViewCalendar::~MultiViewCalendar() = default;
25
26KCalendarCore::Calendar::Ptr MultiViewCalendar::getCalendar() const
27{
28 return {};
29}
30
31KCalendarCore::Incidence::List MultiViewCalendar::incidences() const
32{
34 for (const ViewCalendar::Ptr &cal : std::as_const(mSubCalendars)) {
35 if (cal->getCalendar()) {
36 list += cal->getCalendar()->incidences();
37 }
38 }
39 return list;
40}
41
42int MultiViewCalendar::calendarCount() const
43{
44 return mSubCalendars.size();
45}
46
47Akonadi::CollectionCalendar::Ptr MultiViewCalendar::calendarForCollection(const Akonadi::Collection &collection) const
48{
49 return calendarForCollection(collection.id());
50}
51
52Akonadi::CollectionCalendar::Ptr MultiViewCalendar::calendarForCollection(Akonadi::Collection::Id collectionId) const
53{
54 const auto cal = std::find_if(mSubCalendars.begin(), mSubCalendars.end(), [collectionId](const auto &calendar) {
55 if (const auto &akonadiCal = qSharedPointerDynamicCast<AkonadiViewCalendar>(calendar); akonadiCal) {
56 return akonadiCal->mCalendar->collection().id() == collectionId;
57 }
58 return false;
59 });
60 if (cal == mSubCalendars.end()) {
61 return {};
62 }
63
64 return qSharedPointerCast<AkonadiViewCalendar>(*cal)->mCalendar;
65}
66
67ViewCalendar::Ptr MultiViewCalendar::findCalendar(const KCalendarCore::Incidence::Ptr &incidence) const
68{
69 for (const ViewCalendar::Ptr &cal : std::as_const(mSubCalendars)) {
70 if (cal->isValid(incidence)) {
71 return cal;
72 }
73 }
74 return {};
75}
76
77ViewCalendar::Ptr MultiViewCalendar::findCalendar(const QString &incidenceIdentifier) const
78{
79 for (const ViewCalendar::Ptr &cal : std::as_const(mSubCalendars)) {
80 if (cal->isValid(incidenceIdentifier)) {
81 return cal;
82 }
83 }
84 return {};
85}
86
87void MultiViewCalendar::addCalendar(const ViewCalendar::Ptr &calendar)
88{
89 if (!mSubCalendars.contains(calendar)) {
90 mSubCalendars.append(calendar);
91 }
92}
93
94void MultiViewCalendar::removeCalendar(const ViewCalendar::Ptr &calendar)
95{
96 mSubCalendars.removeOne(calendar);
97}
98
99QString MultiViewCalendar::displayName(const KCalendarCore::Incidence::Ptr &incidence) const
100{
101 ViewCalendar::Ptr cal = findCalendar(incidence);
102 if (cal) {
103 return cal->displayName(incidence);
104 }
105 return {};
106}
107
108QString MultiViewCalendar::iconForIncidence(const KCalendarCore::Incidence::Ptr &incidence) const
109{
110 ViewCalendar::Ptr cal = findCalendar(incidence);
111 if (cal) {
112 return cal->iconForIncidence(incidence);
113 }
114 return {};
115}
116
117bool MultiViewCalendar::isValid(const KCalendarCore::Incidence::Ptr &incidence) const
118{
119 ViewCalendar::Ptr cal = findCalendar(incidence);
120 return !cal.isNull();
121}
122
123bool MultiViewCalendar::isValid(const QString &incidenceIdentifier) const
124{
125 ViewCalendar::Ptr cal = findCalendar(incidenceIdentifier);
126 return !cal.isNull();
127}
128
129QColor MultiViewCalendar::resourceColor(const KCalendarCore::Incidence::Ptr &incidence) const
130{
131 ViewCalendar::Ptr cal = findCalendar(incidence);
132 if (cal) {
133 return cal->resourceColor(incidence);
134 }
135 return {};
136}
137
138Akonadi::Item MultiViewCalendar::item(const KCalendarCore::Incidence::Ptr &incidence) const
139{
140 for (auto &cal : mSubCalendars) {
141 const auto &akonadiCal = qSharedPointerDynamicCast<AkonadiViewCalendar>(cal);
142 if (!akonadiCal) {
143 continue;
144 }
145 if (const auto &item = akonadiCal->item(incidence); item.isValid()) {
146 return item;
147 }
148 }
149 return {};
150}
151
152AkonadiViewCalendar::~AkonadiViewCalendar() = default;
153
154bool AkonadiViewCalendar::isValid(const KCalendarCore::Incidence::Ptr &incidence) const
155{
156 if (!mCalendar) {
157 return false;
158 }
159
160 if (item(incidence).isValid()) {
161 return true;
162 }
163 return false;
164}
165
166bool AkonadiViewCalendar::isValid(const QString &incidenceIdentifier) const
167{
168 if (!mCalendar) {
169 return false;
170 }
171
172 return !mCalendar->incidence(incidenceIdentifier).isNull();
173}
174
175Akonadi::Item AkonadiViewCalendar::item(const KCalendarCore::Incidence::Ptr &incidence) const
176{
177 if (!mCalendar || !incidence) {
178 return {};
179 }
180 bool ok = false;
181 Akonadi::Item::Id id = incidence->customProperty("VOLATILE", "AKONADI-ID").toLongLong(&ok);
182
183 if (id == -1 || !ok) {
184 id = mCalendar->item(incidence).id();
185
186 if (id == -1) {
187 // Ok, we really don't know the ID, give up.
188 qCWarning(CALENDARVIEW_LOG) << "Item is invalid. uid = " << incidence->instanceIdentifier();
189 return {};
190 }
191 return mCalendar->item(incidence->instanceIdentifier());
192 }
193 return mCalendar->item(id);
194}
195
196QString AkonadiViewCalendar::displayName(const KCalendarCore::Incidence::Ptr &incidence) const
197{
198 auto *model = mAgendaView->model();
199 Akonadi::EntityTreeModel *etm = nullptr;
200 while (model) {
201 if (const auto *proxy = qobject_cast<QAbstractProxyModel *>(model); proxy != nullptr) {
202 model = proxy->sourceModel();
203 } else if (etm = qobject_cast<Akonadi::EntityTreeModel *>(model); etm != nullptr) {
204 break;
205 } else {
206 model = nullptr;
207 }
208 }
209 if (etm) {
210 return Akonadi::CalendarUtils::displayName(etm, item(incidence).parentCollection());
211 }
212
213 return {};
214}
215
216QColor AkonadiViewCalendar::resourceColor(const KCalendarCore::Incidence::Ptr &incidence) const
217{
218 Q_UNUSED(incidence);
219 return EventViews::resourceColor(mCalendar->collection(), mAgendaView->preferences());
220}
221
222QString AkonadiViewCalendar::iconForIncidence(const KCalendarCore::Incidence::Ptr &incidence) const
223{
224 return mAgendaView->iconForItem(item(incidence));
225}
226
227KCalendarCore::Calendar::Ptr AkonadiViewCalendar::getCalendar() const
228{
229 return mCalendar;
230}
bool isValid() const
AKONADI_CALENDAR_EXPORT KCalendarCore::Incidence::Ptr incidence(const Akonadi::Item &item)
AKONADI_CALENDAR_EXPORT QString displayName(Akonadi::ETMCalendar *calendar, const Akonadi::Collection &collection)
Namespace EventViews provides facilities for displaying incidences, including events,...
Definition agenda.h:33
EVENTVIEWS_EXPORT QColor resourceColor(const Akonadi::Collection &collection, const PrefsPtr &preferences)
This method returns the proper resource / subresource color for the view.
Definition helper.cpp:56
KIOCORE_EXPORT QStringList list(const QString &fileClass)
bool isNull() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:05:37 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.