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 (!attendees.isEmpty()) {
129  iAmOneOfTheAttendees = std::any_of(attendees.cbegin(), attendees.cend(), [this](const Attendee &att) {
130  return d->mEmailList.contains(att.email());
131  });
132  } else {
133  // no attendees, must be me only
134  iAmOneOfTheAttendees = true;
135  }
136  if (!iAmOneOfTheAttendees) {
137  return false;
138  }
139  }
140  }
141 
142  if (d->mCriteria & HideRecurring) {
143  if (incidence->recurs() || incidence->hasRecurrenceId()) {
144  return false;
145  }
146  }
147 
148  const QStringList incidenceCategories = incidence->categories();
149  bool isFound = false;
150  for (const auto &category : std::as_const(d->mCategoryList)) {
151  if (incidenceCategories.contains(category)) {
152  isFound = true;
153  break;
154  }
155  }
156 
157  return (d->mCriteria & ShowCategories) ? isFound : !isFound;
158 }
159 
160 void CalFilter::setName(const QString &name)
161 {
162  d->mName = name;
163 }
164 
166 {
167  return d->mName;
168 }
169 
170 void CalFilter::setEnabled(bool enabled)
171 {
172  d->mEnabled = enabled;
173 }
174 
176 {
177  return d->mEnabled;
178 }
179 
180 void CalFilter::setCriteria(int criteria)
181 {
182  d->mCriteria = criteria;
183 }
184 
186 {
187  return d->mCriteria;
188 }
189 
190 void CalFilter::setCategoryList(const QStringList &categoryList)
191 {
192  d->mCategoryList = categoryList;
193 }
194 
196 {
197  return d->mCategoryList;
198 }
199 
200 void CalFilter::setEmailList(const QStringList &emailList)
201 {
202  d->mEmailList = emailList;
203 }
204 
206 {
207  return d->mEmailList;
208 }
209 
211 {
212  d->mCompletedTimeSpan = timespan;
213 }
214 
216 {
217  return d->mCompletedTimeSpan;
218 }
void setEmailList(const QStringList &emailList)
Sets the list of email addresses to be considered when filtering incidences according to the HideNoMa...
Definition: calfilter.cpp:200
void setCriteria(int criteria)
Sets the criteria which must be fulfilled for an Incidence to pass the filter.
Definition: calfilter.cpp:180
QStringList categoryList() const
Returns the category list for this filter.
Definition: calfilter.cpp:195
bool isEmpty() const const
QStringList emailList() const
Returns the email list for this filter.
Definition: calfilter.cpp:205
Represents information related to an attendee of an Calendar Incidence, typically a meeting or task (...
Definition: attendee.h:44
QString name() const
Returns the filter name.
Definition: calfilter.cpp:165
void apply(Event::List *eventList) const
Applies the filter to a list of Events.
Definition: calfilter.cpp:69
QVector::iterator begin()
QVector::const_iterator cend() const const
Namespace for all KCalendarCore types.
Definition: alarm.h:36
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
QDateTime currentDateTimeUtc()
void setCompletedTimeSpan(int timespan)
Sets the number of days for the HideCompletedTodos criteria.
Definition: calfilter.cpp:210
void setCategoryList(const QStringList &categoryList)
Sets the list of categories to be considered when filtering incidences according to the ShowCategorie...
Definition: calfilter.cpp:190
~CalFilter()
Destroys this filter.
Definition: calfilter.cpp:58
@ HideInactiveTodos
Remove to-dos that haven't started yet.
Definition: calfilter.h:52
CalFilter()
Constructs an empty filter – a filter without a name or criteria.
Definition: calfilter.cpp:47
Provides a filter for calendars.
Definition: calfilter.h:42
Provides a To-do in the sense of RFC2445.
Definition: todo.h:33
@ ShowCategories
Show incidences with at least one matching category.
Definition: calfilter.h:51
QVector::const_iterator cbegin() const const
bool isEnabled() const
Returns whether the filter is enabled or not.
Definition: calfilter.cpp:175
int completedTimeSpan() const
Returns the completed time span for this filter.
Definition: calfilter.cpp:215
int criteria() const
Returns the inclusive filter criteria.
Definition: calfilter.cpp:185
@ HideRecurring
Remove incidences that recur.
Definition: calfilter.h:49
void setName(const QString &name)
Sets the filter name.
Definition: calfilter.cpp:160
QVector::iterator end()
bool filterIncidence(const Incidence::Ptr &incidence) const
Applies the filter criteria to the specified Incidence.
Definition: calfilter.cpp:106
QVector::iterator erase(QVector::iterator begin, QVector::iterator end)
bool operator==(const CalFilter &filter) const
Compares this with filter for equality.
Definition: calfilter.cpp:63
void setEnabled(bool enabled)
Enables or disables the filter.
Definition: calfilter.cpp:170
@ HideCompletedTodos
Remove completed to-dos.
Definition: calfilter.h:50
QSharedPointer< X > dynamicCast() const const
@ HideNoMatchingAttendeeTodos
Remove to-dos without a matching attendee.
Definition: calfilter.h:53
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 04:00:45 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.