KCalendarCore

calfilter.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the kcalcore library.
3 
4  SPDX-FileCopyrightText: 2001 Cornelius Schumacher <[email protected]>
5  SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <[email protected]>
6  SPDX-FileCopyrightText: 2004 Bram Schoenmakers <[email protected]>
7 
8  SPDX-License-Identifier: LGPL-2.0-or-later
9 */
10 /**
11  @file
12  This file is part of the API for handling calendar data and
13  defines the CalFilter class.
14 
15  @brief
16  Provides a filter for calendars.
17 
18  @author Cornelius Schumacher <[email protected]>
19  @author Reinhold Kainhofer <[email protected]>
20  @author Bram Schoenmakers <[email protected]>
21 */
22 
23 #include "calfilter.h"
24 
25 using namespace KCalendarCore;
26 
27 /**
28  Private class that helps to provide binary compatibility between releases.
29  @internal
30 */
31 //@cond PRIVATE
32 class Q_DECL_HIDDEN KCalendarCore::CalFilter::Private
33 {
34 public:
35  Private()
36  {
37  }
38  QString mName; // filter name
39  QStringList mCategoryList;
40  QStringList mEmailList;
41  int mCriteria = 0;
42  int mCompletedTimeSpan = 0;
43  bool mEnabled = true;
44 };
45 //@endcond
46 
48  : d(new KCalendarCore::CalFilter::Private)
49 {
50 }
51 
53  : d(new KCalendarCore::CalFilter::Private)
54 {
55  d->mName = name;
56 }
57 
59 {
60  delete d;
61 }
62 
64 {
65  return d->mName == filter.d->mName && d->mCriteria == filter.d->mCriteria && d->mCategoryList == filter.d->mCategoryList
66  && d->mEmailList == filter.d->mEmailList && d->mCompletedTimeSpan == filter.d->mCompletedTimeSpan;
67 }
68 
69 void CalFilter::apply(Event::List *eventList) const
70 {
71  if (!d->mEnabled) {
72  return;
73  }
74 
75  Event::List::Iterator it = eventList->begin();
76  while (it != eventList->end()) {
77  if (!filterIncidence(*it)) {
78  it = eventList->erase(it);
79  } else {
80  ++it;
81  }
82  }
83 }
84 
85 // TODO: avoid duplicating apply() code
86 void CalFilter::apply(Todo::List *todoList) const
87 {
88  if (!d->mEnabled) {
89  return;
90  }
91 
92  Todo::List::Iterator it = todoList->begin();
93  while (it != todoList->end()) {
94  if (!filterIncidence(*it)) {
95  it = todoList->erase(it);
96  } else {
97  ++it;
98  }
99  }
100 }
101 
102 void CalFilter::apply(Journal::List *journalList) const
103 {
104  if (!d->mEnabled) {
105  return;
106  }
107 
108  Journal::List::Iterator it = journalList->begin();
109  while (it != journalList->end()) {
110  if (!filterIncidence(*it)) {
111  it = journalList->erase(it);
112  } else {
113  ++it;
114  }
115  }
116 }
117 
118 bool CalFilter::filterIncidence(const Incidence::Ptr &incidence) const
119 {
120  if (!d->mEnabled) {
121  return true;
122  }
123 
124  Todo::Ptr todo = incidence.dynamicCast<Todo>();
125  if (todo) {
126  if ((d->mCriteria & HideCompletedTodos) && todo->isCompleted()) {
127  // Check if completion date is suffently long ago:
128  if (todo->completed().addDays(d->mCompletedTimeSpan) < QDateTime::currentDateTimeUtc()) {
129  return false;
130  }
131  }
132 
133  if ((d->mCriteria & HideInactiveTodos) && ((todo->hasStartDate() && QDateTime::currentDateTimeUtc() < todo->dtStart()) || todo->isCompleted())) {
134  return false;
135  }
136 
137  if (d->mCriteria & HideNoMatchingAttendeeTodos) {
138  bool iAmOneOfTheAttendees = false;
139  const Attendee::List &attendees = todo->attendees();
140  if (!todo->attendees().isEmpty()) {
142  for (it = attendees.begin(); it != attendees.end(); ++it) {
143  if (d->mEmailList.contains((*it).email())) {
144  iAmOneOfTheAttendees = true;
145  break;
146  }
147  }
148  } else {
149  // no attendees, must be me only
150  iAmOneOfTheAttendees = true;
151  }
152  if (!iAmOneOfTheAttendees) {
153  return false;
154  }
155  }
156  }
157 
158  if (d->mCriteria & HideRecurring) {
159  if (incidence->recurs() || incidence->hasRecurrenceId()) {
160  return false;
161  }
162  }
163 
164  if (d->mCriteria & ShowCategories) {
165  for (QStringList::ConstIterator it = d->mCategoryList.constBegin(); it != d->mCategoryList.constEnd(); ++it) {
166  QStringList incidenceCategories = incidence->categories();
167  for (QStringList::ConstIterator it2 = incidenceCategories.constBegin(); it2 != incidenceCategories.constEnd(); ++it2) {
168  if ((*it) == (*it2)) {
169  return true;
170  }
171  }
172  }
173  return false;
174  } else {
175  for (QStringList::ConstIterator it = d->mCategoryList.constBegin(); it != d->mCategoryList.constEnd(); ++it) {
176  QStringList incidenceCategories = incidence->categories();
177  for (QStringList::ConstIterator it2 = incidenceCategories.constBegin(); it2 != incidenceCategories.constEnd(); ++it2) {
178  if ((*it) == (*it2)) {
179  return false;
180  }
181  }
182  }
183  return true;
184  }
185 }
186 
188 {
189  d->mName = name;
190 }
191 
193 {
194  return d->mName;
195 }
196 
197 void CalFilter::setEnabled(bool enabled)
198 {
199  d->mEnabled = enabled;
200 }
201 
203 {
204  return d->mEnabled;
205 }
206 
208 {
209  d->mCriteria = criteria;
210 }
211 
213 {
214  return d->mCriteria;
215 }
216 
218 {
219  d->mCategoryList = categoryList;
220 }
221 
223 {
224  return d->mCategoryList;
225 }
226 
228 {
229  d->mEmailList = emailList;
230 }
231 
233 {
234  return d->mEmailList;
235 }
236 
238 {
239  d->mCompletedTimeSpan = timespan;
240 }
241 
243 {
244  return d->mCompletedTimeSpan;
245 }
int criteria() const
Returns the inclusive filter criteria.
Definition: calfilter.cpp:212
typedef Iterator
~CalFilter()
Destroys this filter.
Definition: calfilter.cpp:58
bool isEnabled() const
Returns whether the filter is enabled or not.
Definition: calfilter.cpp:202
QVector::iterator begin()
void setCategoryList(const QStringList &categoryList)
Sets the list of categories to be considered when filtering incidences according to the ShowCategorie...
Definition: calfilter.cpp:217
void setEmailList(const QStringList &emailList)
Sets the list of email addresses to be considered when filtering incidences according ot the HideNoMa...
Definition: calfilter.cpp:227
int completedTimeSpan() const
Returns the completed time span for this filter.
Definition: calfilter.cpp:242
CalFilter()
Constructs an empty filter – a filter without a name or criteria.
Definition: calfilter.cpp:47
QVector::iterator erase(QVector::iterator begin, QVector::iterator end)
void setCompletedTimeSpan(int timespan)
Sets the number of days for the HideCompletedTodos criteria.
Definition: calfilter.cpp:237
typedef ConstIterator
bool operator==(const CalFilter &filter) const
Compares this with filter for equality.
Definition: calfilter.cpp:63
QStringList emailList() const
Returns the email list for this filter.
Definition: calfilter.cpp:232
Remove to-dos that haven&#39;t started yet.
Definition: calfilter.h:52
void setName(const QString &name)
Sets the filter name.
Definition: calfilter.cpp:187
Show incidences with at least one matching category.
Definition: calfilter.h:51
Provides a To-do in the sense of RFC2445.
Definition: todo.h:30
void apply(Event::List *eventList) const
Applies the filter to a list of Events.
Definition: calfilter.cpp:69
QStringList categoryList() const
Returns the category list for this filter.
Definition: calfilter.cpp:222
Remove incidences that recur.
Definition: calfilter.h:49
void setEnabled(bool enabled)
Enables or disables the filter.
Definition: calfilter.cpp:197
void setCriteria(int criteria)
Sets the criteria which must be fulfilled for an Incidence to pass the filter.
Definition: calfilter.cpp:207
QSharedPointer< X > dynamicCast() const const
typedef ConstIterator
QString name() const
Returns the filter name.
Definition: calfilter.cpp:192
Provides a filter for calendars.
Definition: calfilter.h:42
QList::const_iterator constEnd() const const
QList::const_iterator constBegin() const const
This file is part of the API for handling calendar data and defines the CalFilter class...
bool filterIncidence(const Incidence::Ptr &incidence) const
Applies the filter criteria to the specified Incidence.
Definition: calfilter.cpp:118
QVector::iterator end()
Remove to-dos without a matching attendee.
Definition: calfilter.h:53
QDateTime currentDateTimeUtc()
Namespace for all KCalendarCore types.
Definition: alarm.h:36
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Apr 10 2021 22:50:59 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.