• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

KCalCore Library

  • sources
  • kde-4.12
  • kdepimlibs
  • kcalcore
occurrenceiterator.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the kcalcore library.
3 
4  Copyright (C) 2013 Christian Mollekopf <mollekopf@kolabsys.com>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
32 #include "occurrenceiterator.h"
33 #include "calendar.h"
34 #include "calfilter.h"
35 
36 #include <KDebug>
37 #include <QDate>
38 
39 using namespace KCalCore;
40 
45 //@cond PRIVATE
46 class KCalCore::OccurrenceIterator::Private
47 {
48 public:
49  Private(OccurrenceIterator *qq)
50  : q(qq),
51  occurrenceIt(occurrenceList)
52  {
53  }
54 
55  OccurrenceIterator *q;
56  KDateTime start;
57  KDateTime end;
58 
59  struct Occurrence
60  {
61  Occurrence()
62  {
63  }
64 
65  Occurrence(const Incidence::Ptr &i, const KDateTime &d)
66  : incidence(i), date(d)
67  {
68  }
69 
70  Incidence::Ptr incidence;
71  KDateTime date;
72  };
73  QList<Occurrence> occurrenceList;
74  QListIterator<Occurrence> occurrenceIt;
75  Occurrence current;
76 
77  /*
78  * KCalCore::CalFilter can't handle individual occurrences.
79  * When filtering completed to-dos, the CalFilter doesn't hide
80  * them if it's a recurring to-do.
81  */
82  bool occurrenceIsHidden(const Calendar &calendar,
83  const Incidence::Ptr &inc,
84  const KDateTime &occurrenceDate)
85  {
86  if ((inc->type() == Incidence::TypeTodo) &&
87  calendar.filter() &&
88  (calendar.filter()->criteria() & KCalCore::CalFilter::HideCompletedTodos)) {
89  if (inc->recurs()) {
90  const Todo::Ptr todo = inc.staticCast<Todo>();
91  if (todo && (occurrenceDate < todo->dtDue())) {
92  return true;
93  }
94  } else if (inc->hasRecurrenceId()) {
95  const Todo::Ptr mainTodo = calendar.todo(inc->uid());
96  if (mainTodo && mainTodo->isCompleted()) {
97  return true;
98  }
99  }
100  }
101  return false;
102  }
103 
104  void setupIterator(const Calendar &calendar, const Incidence::List &incidences)
105  {
106  foreach(const Incidence::Ptr &inc, incidences) {
107  if (inc->hasRecurrenceId()) {
108  continue;
109  }
110  if (inc->recurs()) {
111  QHash<KDateTime, Incidence::Ptr> recurrenceIds;
112  KDateTime incidenceRecStart = inc->dateTime(Incidence::RoleRecurrenceStart);
113  foreach(const Incidence::Ptr &exception, calendar.instances(inc)) {
114  if (incidenceRecStart.isValid())
115  recurrenceIds.insert(exception->recurrenceId().toTimeSpec(incidenceRecStart.timeSpec()), exception);
116  }
117  const bool isAllDay = inc->allDay();
118  const DateTimeList occurrences = inc->recurrence()->timesInInterval(start, end);
119  Incidence::Ptr incidence(inc);
120  qint64 offset(0);
121  foreach(KDateTime occurrenceDate, occurrences) { //krazy:exclude=foreach
122  //timesInInterval generates always date-times,
123  //which is not what we want for all-day events
124  occurrenceDate.setDateOnly(isAllDay);
125 
126  bool resetIncidence = false;
127  if (recurrenceIds.contains(occurrenceDate)) {
128  // TODO: exclude exceptions where the start/end is not within
129  // (so the occurrence of the recurrence is omitted, but no exception is added)
130  if (recurrenceIds.value(occurrenceDate)->status() == Incidence::StatusCanceled)
131  continue;
132 
133  incidence = recurrenceIds.value(occurrenceDate);
134  occurrenceDate = incidence->dtStart();
135  resetIncidence = !incidence->thisAndFuture();
136  offset = incidence->recurrenceId().secsTo_long(incidence->dtStart());
137  } else if (inc != incidence) { //thisAndFuture exception is active
138  occurrenceDate = occurrenceDate.addSecs(offset);
139  }
140  if (!occurrenceIsHidden(calendar, incidence, occurrenceDate)) {
141  occurrenceList << Private::Occurrence(incidence, occurrenceDate);
142  }
143  if (resetIncidence) {
144  incidence = inc;
145  offset = 0;
146  }
147  }
148  } else {
149  occurrenceList << Private::Occurrence(inc, inc->dtStart());
150  }
151  }
152  occurrenceIt = QListIterator<Private::Occurrence>(occurrenceList);
153  }
154 };
155 //@endcond
156 
157 static uint qHash(const KDateTime &dt)
158 {
159  return qHash(dt.toString());
160 }
161 
173 OccurrenceIterator::OccurrenceIterator(const Calendar &calendar,
174  const KDateTime &start,
175  const KDateTime &end)
176  : d(new KCalCore::OccurrenceIterator::Private(this))
177 {
178  d->start = start;
179  d->end = end;
180 
181  Event::List events = calendar.rawEvents(start.date(), end.date(), start.timeSpec());
182  if (calendar.filter()) {
183  calendar.filter()->apply(&events);
184  }
185 
186  Todo::List todos = calendar.rawTodos(start.date(), end.date(), start.timeSpec());
187  if (calendar.filter()) {
188  calendar.filter()->apply(&todos);
189  }
190 
191  Journal::List journals;
192  const Journal::List allJournals = calendar.rawJournals();
193  foreach(const KCalCore::Journal::Ptr &journal, allJournals) {
194  const QDate journalStart = journal->dtStart().toTimeSpec(start.timeSpec()).date();
195  if (journal->dtStart().isValid() &&
196  journalStart >= start.date() &&
197  journalStart <= end.date())
198  journals << journal;
199  }
200 
201  if (calendar.filter()) {
202  calendar.filter()->apply(&journals);
203  }
204 
205  const Incidence::List incidences =
206  KCalCore::Calendar::mergeIncidenceList(events, todos, journals);
207  d->setupIterator(calendar, incidences);
208 }
209 
210 OccurrenceIterator::OccurrenceIterator(const Calendar &calendar,
211  const Incidence::Ptr &incidence,
212  const KDateTime &start,
213  const KDateTime &end)
214  : d(new KCalCore::OccurrenceIterator::Private(this))
215 {
216  Q_ASSERT(incidence);
217  d->start = start;
218  d->end = end;
219  d->setupIterator(calendar, Incidence::List() << incidence);
220 }
221 
222 OccurrenceIterator::~OccurrenceIterator()
223 {
224 }
225 
226 bool OccurrenceIterator::hasNext() const
227 {
228  return d->occurrenceIt.hasNext();
229 }
230 
231 void OccurrenceIterator::next()
232 {
233  d->current = d->occurrenceIt.next();
234 }
235 
236 Incidence::Ptr OccurrenceIterator::incidence() const
237 {
238  return d->current.incidence;
239 }
240 
241 KDateTime OccurrenceIterator::occurrenceStartDate() const
242 {
243  return d->current.date;
244 }
KCalCore::Calendar::rawEvents
virtual Event::List rawEvents(EventSortField sortField=EventSortUnsorted, SortDirection sortDirection=SortDirectionAscending) const =0
Returns a sorted, unfiltered list of all Events for this Calendar.
KCalCore::OccurrenceIterator::incidence
Incidence::Ptr incidence() const
Returns either main incidence or exception, depending on occurrence.
Definition: occurrenceiterator.cpp:236
qHash
static uint qHash(const KDateTime &dt)
Private class that helps to provide binary compatibility between releases.
Definition: occurrenceiterator.cpp:157
occurrenceiterator.h
This file is part of the API for handling calendar data and defines the OccurrenceIterator class...
KCalCore::Journal::List
QVector< Ptr > List
List of journals.
Definition: journal.h:54
KCalCore::IncidenceBase::TypeTodo
Type is a to-do.
Definition: incidencebase.h:123
KCalCore::OccurrenceIterator
Iterate over calendar items in a calendar.
Definition: occurrenceiterator.h:46
KCalCore::CalFilter::HideCompletedTodos
Remove completed to-dos.
Definition: calfilter.h:63
KCalCore::Incidence::Ptr
QSharedPointer< Incidence > Ptr
A shared pointer to an Incidence.
Definition: incidence.h:112
KCalCore::OccurrenceIterator::occurrenceStartDate
KDateTime occurrenceStartDate() const
Returns the start date of the occurrence.
Definition: occurrenceiterator.cpp:241
KCalCore::Event::List
QVector< Ptr > List
List of events.
Definition: event.h:60
KCalCore::Calendar::rawJournals
virtual Journal::List rawJournals(JournalSortField sortField=JournalSortUnsorted, SortDirection sortDirection=SortDirectionAscending) const =0
Returns a sorted, unfiltered list of all Journals for this Calendar.
KCalCore::Calendar::instances
virtual Incidence::List instances(const Incidence::Ptr &incidence) const
Returns an unfiltered list of all exceptions of this recurring incidence.
KCalCore::Calendar::mergeIncidenceList
static Incidence::List mergeIncidenceList(const Event::List &events, const Todo::List &todos, const Journal::List &journals)
Create a merged list of Events, Todos, and Journals.
Definition: calendar.cpp:1348
KCalCore::SortableList
A QList which can be sorted.
Definition: sortablelist.h:86
KCalCore::CalFilter::apply
void apply(Event::List *eventList) const
Applies the filter to a list of Events.
Definition: calfilter.cpp:87
calendar.h
This file is part of the API for handling calendar data and defines the Calendar class.
KCalCore::OccurrenceIterator::next
void next()
Advance iterator to the next occurrence.
Definition: occurrenceiterator.cpp:231
KCalCore::Incidence::StatusCanceled
event or to-do canceled; journal removed
Definition: incidence.h:83
KCalCore::Calendar::rawTodos
virtual Todo::List rawTodos(TodoSortField sortField=TodoSortUnsorted, SortDirection sortDirection=SortDirectionAscending) const =0
Returns a sorted, unfiltered list of all Todos for this Calendar.
KCalCore::Todo::List
QVector< Ptr > List
List of to-dos.
Definition: todo.h:55
KCalCore::OccurrenceIterator::OccurrenceIterator
OccurrenceIterator(const Calendar &calendar, const KDateTime &start=KDateTime(), const KDateTime &end=KDateTime())
Creates iterator that iterates over all occurrences of all incidences between.
Definition: occurrenceiterator.cpp:173
KCalCore::Todo
Provides a To-do in the sense of RFC2445.
Definition: todo.h:44
KCalCore::Incidence::List
QVector< Ptr > List
List of incidences.
Definition: incidence.h:117
KCalCore::Calendar
Represents the main calendar class.
Definition: calendar.h:128
KCalCore::Calendar::todo
virtual Todo::Ptr todo(const QString &uid, const KDateTime &recurrenceId=KDateTime()) const =0
Returns the Todo associated with the given unique identifier.
calfilter.h
This file is part of the API for handling calendar data and defines the CalFilter class...
KCalCore::Todo::Ptr
QSharedPointer< Todo > Ptr
A shared pointer to a Todo object.
Definition: todo.h:50
KCalCore::Journal::Ptr
QSharedPointer< Journal > Ptr
A shared pointer to a Journal object.
Definition: journal.h:49
KCalCore::Calendar::filter
CalFilter * filter() const
Returns the calendar filter.
KCalCore::IncidenceBase::RoleRecurrenceStart
Role for determining the start of the recurrence.
Definition: incidencebase.h:147
KCalCore::CalFilter::criteria
int criteria() const
Returns the inclusive filter criteria.
Definition: calfilter.cpp:240
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:59:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KCalCore Library

Skip menu "KCalCore Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal