Akonadi Calendar

calendarmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Bruno Virlet <bvirlet@kdemail.net>
3 SPDX-FileCopyrightText: 2009 KDAB
4 SPDX-FileContributor: Frank Osterfeld <osterfeld@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "calendarmodel_p.h"
10
11#include "calendarutils.h"
12#include <Akonadi/ItemFetchScope>
13#include <Akonadi/Monitor>
14#include <KCalendarCore/Event>
15#include <KCalendarCore/Journal>
16#include <KCalendarCore/Todo>
17
18#include <KLocalizedString>
19
20#include <QIcon>
21
22using namespace Akonadi;
23
24class Akonadi::CalendarModelPrivate
25{
26public:
27 CalendarModelPrivate() = default;
28
29 QWeakPointer<CalendarModel> m_weakPointer;
30};
31
32CalendarModel::CalendarModel(Akonadi::Monitor *monitor)
33 : EntityTreeModel(monitor)
34 , d(new CalendarModelPrivate())
35{
36 monitor->itemFetchScope().fetchAllAttributes(true);
37}
38
39CalendarModel::Ptr CalendarModel::create(Monitor *monitor)
40{
41 auto model = new CalendarModel(monitor);
42 CalendarModel::Ptr modelPtr = CalendarModel::Ptr(model);
43 model->setWeakPointer(modelPtr.toWeakRef());
44 return modelPtr;
45}
46
47CalendarModel::~CalendarModel() = default;
48
49QWeakPointer<CalendarModel> CalendarModel::weakPointer() const
50{
51 return d->m_weakPointer;
52}
53
54void CalendarModel::setWeakPointer(const QWeakPointer<CalendarModel> &weakPointer)
55{
56 d->m_weakPointer = weakPointer;
57}
58
59QVariant CalendarModel::entityData(const Akonadi::Item &item, int column, int role) const
60{
62 if (!inc) {
63 return {};
64 }
65
66 switch (role) {
67 case Qt::DecorationRole: {
68 if (column != Summary) {
69 return {};
70 }
71 const auto incType{inc->type()};
73 return QIcon::fromTheme(QStringLiteral("view-pim-tasks"));
74 } else if (incType == KCalendarCore::IncidenceBase::TypeJournal) {
75 return QIcon::fromTheme(QStringLiteral("view-pim-journal"));
76 } else if (incType == KCalendarCore::IncidenceBase::TypeEvent) {
77 return QIcon::fromTheme(QStringLiteral("view-calendar"));
78 }
79 return QIcon::fromTheme(QStringLiteral("network-wired"));
80 }
81 case Qt::DisplayRole:
82 switch (column) {
83 case Summary:
84 return inc->summary();
85
86 case DateTimeStart:
87 return inc->dtStart().toString();
88
89 case DateTimeEnd:
90 return inc->dateTime(KCalendarCore::Incidence::RoleEndTimeZone).toString();
91
92 case DateTimeDue:
94 return t->dtDue().toString();
95 } else {
96 return {};
97 }
98
99 case Priority:
101 return t->priority();
102 } else {
103 return {};
104 }
105
106 case PercentComplete:
108 return t->percentComplete();
109 } else {
110 return {};
111 }
112
113 case Type:
114 return inc->typeStr();
115 default:
116 break;
117 }
118
119 return {};
120 case SortRole:
121 switch (column) {
122 case Summary:
123 return inc->summary();
124
125 case DateTimeStart:
126 return inc->dtStart().toUTC();
127
128 case DateTimeEnd:
129 return inc->dateTime(KCalendarCore::Incidence::RoleEndTimeZone).toUTC();
130
131 case DateTimeDue:
133 return t->dtDue().toUTC();
134 } else {
135 return {};
136 }
137
138 case Priority:
140 return t->priority();
141 } else {
142 return {};
143 }
144
145 case PercentComplete:
147 return t->percentComplete();
148 } else {
149 return {};
150 }
151
152 case Type:
153 return inc->type();
154
155 default:
156 break;
157 }
158
159 return {};
160
161 case RecursRole:
162 return inc->recurs();
163
164 default:
165 return {};
166 }
167}
168
169QVariant CalendarModel::entityData(const Akonadi::Collection &collection, int column, int role) const
170{
171 return EntityTreeModel::entityData(collection, column, role);
172}
173
174int CalendarModel::entityColumnCount(EntityTreeModel::HeaderGroup headerSet) const
175{
176 if (headerSet == EntityTreeModel::ItemListHeaders) {
177 return ItemColumnCount;
178 } else {
179 return CollectionColumnCount;
180 }
181}
182
183QVariant CalendarModel::entityHeaderData(int section, Qt::Orientation orientation, int role, EntityTreeModel::HeaderGroup headerSet) const
184{
185 if (role != Qt::DisplayRole || orientation != Qt::Horizontal) {
186 return {};
187 }
188
189 if (headerSet == EntityTreeModel::ItemListHeaders) {
190 switch (section) {
191 case Summary:
192 return i18nc("@title:column calendar event summary", "Summary");
193 case DateTimeStart:
194 return i18nc("@title:column calendar event start date and time", "Start Date and Time");
195 case DateTimeEnd:
196 return i18nc("@title:column calendar event end date and time", "End Date and Time");
197 case Type:
198 return i18nc("@title:column calendar event type", "Type");
199 case DateTimeDue:
200 return i18nc("@title:column todo item due date and time", "Due Date and Time");
201 case Priority:
202 return i18nc("@title:column todo item priority", "Priority");
203 case PercentComplete:
204 return i18nc("@title:column todo item completion in percent", "Complete");
205 default:
206 return {};
207 }
208 }
209
211 switch (section) {
212 case CollectionTitle:
213 return i18nc("@title:column calendar title", "Calendar");
214 default:
215 return {};
216 }
217 }
218 return {};
219}
220
221#include "moc_calendarmodel_p.cpp"
virtual QVariant entityData(const Collection &collection, int column, int role=Qt::DisplayRole) const
ItemFetchScope & itemFetchScope()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Incidence::Ptr incidence(const Akonadi::Item &item)
Returns the incidence from an Akonadi item, or a null pointer if the item has no such payload.
AKONADI_CALENDAR_EXPORT KCalendarCore::Todo::Ptr todo(const Akonadi::Item &item)
Returns the todo from an Akonadi item, or a null pointer if the item has no such payload.
FreeBusyManager::Singleton.
QIcon fromTheme(const QString &name)
DecorationRole
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:37:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.