KCalendarCore

sorting.cpp
1 /*
2  This file is part of the kcalcore library.
3 
4  SPDX-FileCopyrightText: 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
5  SPDX-FileContributor: Alvaro Manera <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 #include "sorting.h"
10 #include "event.h"
11 #include "journal.h"
12 #include "todo.h"
13 
14 // PENDING(kdab) Review
15 // The QString::compare() need to be replace by a DUI string comparisons.
16 // See http://qt.gitorious.org/maemo-6-ui-framework/libdui
17 // If not compiled in "meego-mode" should we be using locale compares?
18 
19 using namespace KCalendarCore;
20 
40 enum DateTimeComparison {
41  Before = 0x01,
44  AtStart = 0x02,
48  Inside = 0x04,
52  AtEnd = 0x08,
56  After = 0x10,
59  Equal = AtStart | Inside | AtEnd,
62  Outside = Before | AtStart | Inside | AtEnd | After,
67  StartsAt = AtStart | Inside | AtEnd | After,
72  EndsAt = Before | AtStart | Inside | AtEnd
77 };
78 
107 DateTimeComparison compare(const QDateTime &dt1, bool isAllDay1, const QDateTime &dt2, bool isAllDay2)
108 {
109  QDateTime start1, start2;
110  //FIXME When secondOccurrence is available in QDateTime
111  //const bool conv = (!d->equalSpec(*other.d) || d->secondOccurrence() != other.d->secondOccurrence());
112  const bool conv =
113  dt1.timeSpec() != dt2.timeSpec() ||
114  (dt1.timeSpec() == Qt::OffsetFromUTC && dt1.offsetFromUtc() != dt2.offsetFromUtc()) || (dt1.timeSpec() == Qt::TimeZone && dt1.timeZone() != dt2.timeZone());
115  if (conv) {
116  // Different time specs or one is a time which occurs twice,
117  // so convert to UTC before comparing
118  start1 = dt1.toUTC();
119  start2 = dt2.toUTC();
120  } else {
121  // Same time specs, so no need to convert to UTC
122  start1 = dt1;
123  start2 = dt2;
124  }
125  if (isAllDay1 || isAllDay2) {
126  // At least one of the instances is date-only, so we need to compare
127  // time periods rather than just times.
128  QDateTime end1, end2;
129  if (conv) {
130  if (isAllDay1) {
131  QDateTime dt(dt1);
132  dt.setTime(QTime(23, 59, 59, 999));
133  end1 = dt.toUTC();
134  } else {
135  end1 = start1;
136  }
137  if (isAllDay2) {
138  QDateTime dt(dt2);
139  dt.setTime(QTime(23, 59, 59, 999));
140  end2 = dt.toUTC();
141  } else {
142  end2 = start2;
143  }
144  } else {
145  if (isAllDay1) {
146  end1 = QDateTime(dt1.date(), QTime(23, 59, 59, 999), Qt::LocalTime);
147  } else {
148  end1 = dt1;
149  }
150  if (isAllDay2) {
151  end2 = QDateTime(dt2.date(), QTime(23, 59, 59, 999), Qt::LocalTime);
152  } else {
153  end2 = dt2;
154  }
155  }
156 
157  if (start1 == start2) {
158  return !isAllDay1 ? AtStart : (end1 == end2) ? Equal
159  : (end1 < end2) ? static_cast<DateTimeComparison>(AtStart | Inside)
160  : static_cast<DateTimeComparison>(AtStart | Inside | AtEnd | After);
161  }
162 
163  if (start1 < start2) {
164  return (end1 < start2) ? Before
165  : (end1 == end2) ? static_cast<DateTimeComparison>(Before | AtStart | Inside | AtEnd)
166  : (end1 == start2) ? static_cast<DateTimeComparison>(Before | AtStart)
167  : (end1 < end2) ? static_cast<DateTimeComparison>(Before | AtStart | Inside) : Outside;
168  } else {
169  return (start1 > end2) ? After
170  : (start1 == end2) ? (end1 == end2 ? AtEnd : static_cast<DateTimeComparison>(AtEnd | After))
171  : (end1 == end2) ? static_cast<DateTimeComparison>(Inside | AtEnd)
172  : (end1 < end2) ? Inside : static_cast<DateTimeComparison>(Inside | AtEnd | After);
173  }
174  }
175  return (start1 == start2) ? Equal : (start1 < start2) ? Before : After;
176 }
177 
178 bool KCalendarCore::Events::startDateLessThan(const Event::Ptr &e1, const Event::Ptr &e2)
179 {
180  DateTimeComparison res = compare(e1->dtStart(), e1->allDay(), e2->dtStart(), e2->allDay());
181  if (res == Equal) {
182  return Events::summaryLessThan(e1, e2);
183  } else {
184  return (res & Before || res & AtStart);
185  }
186 }
187 
188 bool KCalendarCore::Events::startDateMoreThan(const Event::Ptr &e1, const Event::Ptr &e2)
189 {
190  DateTimeComparison res = compare(e1->dtStart(), e1->allDay(), e2->dtStart(), e2->allDay());
191  if (res == Equal) {
192  return Events::summaryMoreThan(e1, e2);
193  } else {
194  return (res & After || res & AtEnd);
195  }
196 }
197 
198 bool KCalendarCore::Events::summaryLessThan(const Event::Ptr &e1, const Event::Ptr &e2)
199 {
200  return QString::compare(e1->summary(), e2->summary(), Qt::CaseInsensitive) < 0;
201 }
202 
203 bool KCalendarCore::Events::summaryMoreThan(const Event::Ptr &e1, const Event::Ptr &e2)
204 {
205  return QString::compare(e1->summary(), e2->summary(), Qt::CaseInsensitive) > 0;
206 }
207 
208 bool KCalendarCore::Events::endDateLessThan(const Event::Ptr &e1, const Event::Ptr &e2)
209 {
210  DateTimeComparison res = compare(e1->dtEnd(), e1->allDay(), e2->dtEnd(), e2->allDay());
211  if (res == Equal) {
212  return Events::summaryLessThan(e1, e2);
213  } else {
214  return (res & Before || res & AtStart);
215  }
216 }
217 
218 bool KCalendarCore::Events::endDateMoreThan(const Event::Ptr &e1, const Event::Ptr &e2)
219 {
220  DateTimeComparison res = compare(e1->dtEnd(), e1->allDay(), e2->dtEnd(), e2->allDay());
221  if (res == Equal) {
222  return Events::summaryMoreThan(e1, e2);
223  } else {
224  return (res & After || res & AtEnd);
225  }
226 }
227 
228 bool KCalendarCore::Journals::dateLessThan(const Journal::Ptr &j1, const Journal::Ptr &j2)
229 {
230  DateTimeComparison res = compare(j1->dtStart(), j1->allDay(), j2->dtStart(), j2->allDay());
231  return (res & Before || res & AtStart);
232 }
233 
234 bool KCalendarCore::Journals::dateMoreThan(const Journal::Ptr &j1, const Journal::Ptr &j2)
235 {
236  DateTimeComparison res = compare(j1->dtStart(), j1->allDay(), j2->dtStart(), j2->allDay());
237  return (res & After || res & AtEnd);
238 }
239 
240 bool KCalendarCore::Journals::summaryLessThan(const Journal::Ptr &j1, const Journal::Ptr &j2)
241 {
242  return QString::compare(j1->summary(), j2->summary(), Qt::CaseInsensitive) < 0;
243 }
244 
245 bool KCalendarCore::Journals::summaryMoreThan(const Journal::Ptr &j1, const Journal::Ptr &j2)
246 {
247  return QString::compare(j1->summary(), j2->summary(), Qt::CaseInsensitive) > 0;
248 }
249 
250 bool KCalendarCore::Todos::startDateLessThan(const Todo::Ptr &t1, const Todo::Ptr &t2)
251 {
252  DateTimeComparison res = compare(t1->dtStart(), t1->allDay(), t2->dtStart(), t2->allDay());
253  if (res == Equal) {
254  return Todos::summaryLessThan(t1, t2);
255  } else {
256  return (res & Before || res & AtStart);
257  }
258 }
259 
260 bool KCalendarCore::Todos::startDateMoreThan(const Todo::Ptr &t1, const Todo::Ptr &t2)
261 {
262  DateTimeComparison res = compare(t1->dtStart(), t1->allDay(), t2->dtStart(), t2->allDay());
263  if (res == Equal) {
264  return Todos::summaryMoreThan(t1, t2);
265  } else {
266  return (res & After || res & AtEnd);
267  }
268 }
269 
270 bool KCalendarCore::Todos::dueDateLessThan(const Todo::Ptr &t1, const Todo::Ptr &t2)
271 {
272  DateTimeComparison res = compare(t1->dtDue(), t1->allDay(), t2->dtDue(), t2->allDay());
273  if (res == Equal) {
274  return Todos::summaryLessThan(t1, t2);
275  } else {
276  return (res & Before || res & AtStart);
277  }
278 }
279 
280 bool KCalendarCore::Todos::dueDateMoreThan(const Todo::Ptr &t1, const Todo::Ptr &t2)
281 {
282  DateTimeComparison res = compare(t1->dtDue(), t1->allDay(), t2->dtDue(), t2->allDay());
283  if (res == Equal) {
284  return Todos::summaryMoreThan(t1, t2);
285  } else {
286  return (res & After || res & AtEnd);
287  }
288 }
289 
290 bool KCalendarCore::Todos::priorityLessThan(const Todo::Ptr &t1, const Todo::Ptr &t2)
291 {
292  if (t1->priority() < t2->priority()) {
293  return true;
294  } else if (t1->priority() == t2->priority()) {
295  return Todos::summaryLessThan(t1, t2);
296  } else {
297  return false;
298  }
299 }
300 
301 bool KCalendarCore::Todos::priorityMoreThan(const Todo::Ptr &t1, const Todo::Ptr &t2)
302 {
303  if (t1->priority() > t2->priority()) {
304  return true;
305  } else if (t1->priority() == t2->priority()) {
306  return Todos::summaryMoreThan(t1, t2);
307  } else {
308  return false;
309  }
310 }
311 
312 bool KCalendarCore::Todos::percentLessThan(const Todo::Ptr &t1, const Todo::Ptr &t2)
313 {
314  if (t1->percentComplete() < t2->percentComplete()) {
315  return true;
316  } else if (t1->percentComplete() == t2->percentComplete()) {
317  return Todos::summaryLessThan(t1, t2);
318  } else {
319  return false;
320  }
321 }
322 
323 bool KCalendarCore::Todos::percentMoreThan(const Todo::Ptr &t1, const Todo::Ptr &t2)
324 {
325  if (t1->percentComplete() > t2->percentComplete()) {
326  return true;
327  } else if (t1->percentComplete() == t2->percentComplete()) {
328  return Todos::summaryMoreThan(t1, t2);
329  } else {
330  return false;
331  }
332 }
333 
334 bool KCalendarCore::Todos::summaryLessThan(const Todo::Ptr &t1, const Todo::Ptr &t2)
335 {
336  return QString::compare(t1->summary(), t2->summary(), Qt::CaseInsensitive) < 0;
337 }
338 
339 bool KCalendarCore::Todos::summaryMoreThan(const Todo::Ptr &t1, const Todo::Ptr &t2)
340 {
341  return QString::compare(t1->summary(), t2->summary(), Qt::CaseInsensitive) > 0;
342 }
343 
344 bool KCalendarCore::Todos::createdLessThan(const Todo::Ptr &t1, const Todo::Ptr &t2)
345 {
346  DateTimeComparison res = compare(t1->created(), t1->allDay(), t2->created(), t2->allDay());
347  if (res == Equal) {
348  return Todos::summaryLessThan(t1, t2);
349  } else {
350  return (res & Before || res & AtStart);
351  }
352 }
353 
354 bool KCalendarCore::Todos::createdMoreThan(const Todo::Ptr &t1, const Todo::Ptr &t2)
355 {
356  DateTimeComparison res = compare(t1->created(), t1->allDay(), t2->created(), t2->allDay());
357  if (res == Equal) {
358  return Todos::summaryMoreThan(t1, t2);
359  } else {
360  return (res & After || res & AtEnd);
361  }
362 }
363 
364 bool KCalendarCore::Incidences::dateLessThan(const Incidence::Ptr &i1,
365  const Incidence::Ptr &i2)
366 {
367  DateTimeComparison res = compare(i1->dateTime(Incidence::RoleSort), i1->allDay(),
368  i2->dateTime(Incidence::RoleSort), i2->allDay());
369  if (res == Equal) {
370  return Incidences::summaryLessThan(i1, i2);
371  } else {
372  return (res & Before || res & AtStart);
373  }
374 }
375 
376 bool KCalendarCore::Incidences::dateMoreThan(const Incidence::Ptr &i1,
377  const Incidence::Ptr &i2)
378 {
379  DateTimeComparison res = compare(i1->dateTime(Incidence::RoleSort), i1->allDay(),
380  i2->dateTime(Incidence::RoleSort), i2->allDay());
381  if (res == Equal) {
382  return Incidences::summaryMoreThan(i1, i2);
383  } else {
384  return (res & After || res & AtEnd);
385  }
386 }
387 
388 bool KCalendarCore::Incidences::createdLessThan(const Incidence::Ptr &i1,
389  const Incidence::Ptr &i2)
390 {
391  DateTimeComparison res = compare(i1->created(), i1->allDay(), i2->created(), i2->allDay());
392  if (res == Equal) {
393  return Incidences::summaryLessThan(i1, i2);
394  } else {
395  return (res & Before || res & AtStart);
396  }
397 }
398 
399 bool KCalendarCore::Incidences::createdMoreThan(const Incidence::Ptr &i1,
400  const Incidence::Ptr &i2)
401 {
402  DateTimeComparison res = compare(i1->created(), i1->allDay(), i2->created(), i2->allDay());
403  if (res == Equal) {
404  return Incidences::summaryMoreThan(i1, i2);
405  } else {
406  return (res & After || res & AtEnd);
407  }
408 }
409 
410 bool KCalendarCore::Incidences::summaryLessThan(const Incidence::Ptr &i1,
411  const Incidence::Ptr &i2)
412 {
413  return QString::compare(i1->summary(), i2->summary(), Qt::CaseInsensitive) < 0;
414 }
415 
416 bool KCalendarCore::Incidences::summaryMoreThan(const Incidence::Ptr &i1,
417  const Incidence::Ptr &i2)
418 {
419  return QString::compare(i1->summary(), i2->summary(), Qt::CaseInsensitive) > 0;
420 }
421 
QDateTime toUTC() const const
OffsetFromUTC
Role for an incidence&#39;s date/time used when sorting.
CaseInsensitive
This file is part of the API for handling calendar data and defines the Todo class.
This file is part of the API for handling calendar data and defines the Journal class.
Qt::TimeSpec timeSpec() const const
QDate date() const const
This file is part of the API for handling calendar data and defines the Event class.
int offsetFromUtc() const const
int compare(const QString &other, Qt::CaseSensitivity cs) const const
Namespace for all KCalendarCore types.
Definition: alarm.h:36
QTimeZone timeZone() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Nov 26 2020 22:49:37 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.