Akonadi Calendar

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

KDE's Doxygen guidelines are available online.