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  auto it = std::remove_if(eventList->begin(), eventList->end(), [=](const Incidence::Ptr &incidence) {
76  return !filterIncidence(incidence);
77  });
78  eventList->erase(it, eventList->end());
79 }
80 
81 // TODO: avoid duplicating apply() code
82 void CalFilter::apply(Todo::List *todoList) const
83 {
84  if (!d->mEnabled) {
85  return;
86  }
87 
88  auto it = std::remove_if(todoList->begin(), todoList->end(), [=](const Incidence::Ptr &incidence) {
89  return !filterIncidence(incidence);
90  });
91  todoList->erase(it, todoList->end());
92 }
93 
94 void CalFilter::apply(Journal::List *journalList) const
95 {
96  if (!d->mEnabled) {
97  return;
98  }
99 
100  auto it = std::remove_if(journalList->begin(), journalList->end(), [=](const Incidence::Ptr &incidence) {
101  return !filterIncidence(incidence);
102  });
103  journalList->erase(it, journalList->end());
104 }
105 
106 bool CalFilter::filterIncidence(const Incidence::Ptr &incidence) const
107 {
108  if (!d->mEnabled) {
109  return true;
110  }
111 
112  Todo::Ptr todo = incidence.dynamicCast<Todo>();
113  if (todo) {
114  if ((d->mCriteria & HideCompletedTodos) && todo->isCompleted()) {
115  // Check if completion date is suffently long ago:
116  if (todo->completed().addDays(d->mCompletedTimeSpan) < QDateTime::currentDateTimeUtc()) {
117  return false;
118  }
119  }
120 
121  if ((d->mCriteria & HideInactiveTodos) && ((todo->hasStartDate() && QDateTime::currentDateTimeUtc() < todo->dtStart()) || todo->isCompleted())) {
122  return false;
123  }
124 
125  if (d->mCriteria & HideNoMatchingAttendeeTodos) {
126  bool iAmOneOfTheAttendees = false;
127  const Attendee::List &attendees = todo->attendees();
128  if (!todo->attendees().isEmpty()) {
130  for (it = attendees.begin(); it != attendees.end(); ++it) {
131  if (d->mEmailList.contains((*it).email())) {
132  iAmOneOfTheAttendees = true;
133  break;
134  }
135  }
136  } else {
137  // no attendees, must be me only
138  iAmOneOfTheAttendees = true;
139  }
140  if (!iAmOneOfTheAttendees) {
141  return false;
142  }
143  }
144  }
145 
146  if (d->mCriteria & HideRecurring) {
147  if (incidence->recurs() || incidence->hasRecurrenceId()) {
148  return false;
149  }
150  }
151 
152  if (d->mCriteria & ShowCategories) {
153  for (QStringList::ConstIterator it = d->mCategoryList.constBegin(); it != d->mCategoryList.constEnd(); ++it) {
154  QStringList incidenceCategories = incidence->categories();
155  for (QStringList::ConstIterator it2 = incidenceCategories.constBegin(); it2 != incidenceCategories.constEnd(); ++it2) {
156  if ((*it) == (*it2)) {
157  return true;
158  }
159  }
160  }
161  return false;
162  } else {
163  for (QStringList::ConstIterator it = d->mCategoryList.constBegin(); it != d->mCategoryList.constEnd(); ++it) {
164  QStringList incidenceCategories = incidence->categories();
165  for (QStringList::ConstIterator it2 = incidenceCategories.constBegin(); it2 != incidenceCategories.constEnd(); ++it2) {
166  if ((*it) == (*it2)) {
167  return false;
168  }
169  }
170  }
171  return true;
172  }
173 }
174 
176 {
177  d->mName = name;
178 }
179 
181 {
182  return d->mName;
183 }
184 
185 void CalFilter::setEnabled(bool enabled)
186 {
187  d->mEnabled = enabled;
188 }
189 
191 {
192  return d->mEnabled;
193 }
194 
196 {
197  d->mCriteria = criteria;
198 }
199 
201 {
202  return d->mCriteria;
203 }
204 
206 {
207  d->mCategoryList = categoryList;
208 }
209 
211 {
212  return d->mCategoryList;
213 }
214 
216 {
217  d->mEmailList = emailList;
218 }
219 
221 {
222  return d->mEmailList;
223 }
224 
226 {
227  d->mCompletedTimeSpan = timespan;
228 }
229 
231 {
232  return d->mCompletedTimeSpan;
233 }
int criteria() const
Returns the inclusive filter criteria.
Definition: calfilter.cpp:200
~CalFilter()
Destroys this filter.
Definition: calfilter.cpp:58
bool isEnabled() const
Returns whether the filter is enabled or not.
Definition: calfilter.cpp:190
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:205
void setEmailList(const QStringList &emailList)
Sets the list of email addresses to be considered when filtering incidences according to the HideNoMa...
Definition: calfilter.cpp:215
int completedTimeSpan() const
Returns the completed time span for this filter.
Definition: calfilter.cpp:230
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:225
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:220
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:175
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:210
Remove incidences that recur.
Definition: calfilter.h:49
void setEnabled(bool enabled)
Enables or disables the filter.
Definition: calfilter.cpp:185
void setCriteria(int criteria)
Sets the criteria which must be fulfilled for an Incidence to pass the filter.
Definition: calfilter.cpp:195
QSharedPointer< X > dynamicCast() const const
typedef ConstIterator
QString name() const
Returns the filter name.
Definition: calfilter.cpp:180
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:106
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 Sun Sep 26 2021 22:51:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.