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 <schumacher@kde.org>
5 SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 SPDX-FileCopyrightText: 2004 Bram Schoenmakers <bramschoenmakers@kde.nl>
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 <schumacher@kde.org>
19 @author Reinhold Kainhofer <reinhold@kainhofer.com>
20 @author Bram Schoenmakers <bramschoenmakers@kde.nl>
21*/
22
23#include "calfilter.h"
24
25using namespace KCalendarCore;
26
27/**
28 Private class that helps to provide binary compatibility between releases.
29 @internal
30*/
31//@cond PRIVATE
32class Q_DECL_HIDDEN KCalendarCore::CalFilter::Private
33{
34public:
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
69void 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(), [this](const Incidence::Ptr &incidence) {
76 return !filterIncidence(incidence);
77 });
78 eventList->erase(it, eventList->end());
79}
80
81// TODO: avoid duplicating apply() code
82void 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(), [this](const Incidence::Ptr &incidence) {
89 return !filterIncidence(incidence);
90 });
91 todoList->erase(it, todoList->end());
92}
93
94void 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(), [this](const Incidence::Ptr &incidence) {
101 return !filterIncidence(incidence);
102 });
103 journalList->erase(it, journalList->end());
104}
105
106bool 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
161{
162 d->mName = name;
163}
164
166{
167 return d->mName;
168}
169
170void CalFilter::setEnabled(bool enabled)
171{
172 d->mEnabled = enabled;
173}
174
176{
177 return d->mEnabled;
178}
179
180void CalFilter::setCriteria(int criteria)
181{
182 d->mCriteria = criteria;
183}
184
186{
187 return d->mCriteria;
188}
189
191{
192 d->mCategoryList = categoryList;
193}
194
196{
197 return d->mCategoryList;
198}
199
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}
This file is part of the API for handling calendar data and defines the CalFilter class.
Represents information related to an attendee of an Calendar Incidence, typically a meeting or task (...
Definition attendee.h:45
Provides a filter for calendars.
Definition calfilter.h:43
QStringList categoryList() const
Returns the category list for this filter.
void apply(Event::List *eventList) const
Applies the filter to a list of Events.
Definition calfilter.cpp:69
void setCompletedTimeSpan(int timespan)
Sets the number of days for the HideCompletedTodos criteria.
void setCriteria(int criteria)
Sets the criteria which must be fulfilled for an Incidence to pass the filter.
void setCategoryList(const QStringList &categoryList)
Sets the list of categories to be considered when filtering incidences according to the ShowCategorie...
void setEmailList(const QStringList &emailList)
Sets the list of email addresses to be considered when filtering incidences according to the HideNoMa...
int criteria() const
Returns the inclusive filter criteria.
bool isEnabled() const
Returns whether the filter is enabled or not.
void setEnabled(bool enabled)
Enables or disables the filter.
QString name() const
Returns the filter name.
bool operator==(const CalFilter &filter) const
Compares this with filter for equality.
Definition calfilter.cpp:63
@ HideCompletedTodos
Remove completed to-dos.
Definition calfilter.h:50
@ HideNoMatchingAttendeeTodos
Remove to-dos without a matching attendee.
Definition calfilter.h:53
@ ShowCategories
Show incidences with at least one matching category.
Definition calfilter.h:51
@ HideInactiveTodos
Remove to-dos that haven't started yet.
Definition calfilter.h:52
@ HideRecurring
Remove incidences that recur.
Definition calfilter.h:49
~CalFilter()
Destroys this filter.
Definition calfilter.cpp:58
CalFilter()
Constructs an empty filter – a filter without a name or criteria.
Definition calfilter.cpp:47
bool filterIncidence(const Incidence::Ptr &incidence) const
Applies the filter criteria to the specified Incidence.
QStringList emailList() const
Returns the email list for this filter.
void setName(const QString &name)
Sets the filter name.
int completedTimeSpan() const
Returns the completed time span for this filter.
Provides a To-do in the sense of RFC2445.
Definition todo.h:34
Namespace for all KCalendarCore types.
Definition alarm.h:37
QDateTime currentDateTimeUtc()
iterator begin()
const_iterator cbegin() const const
const_iterator cend() const const
iterator end()
iterator erase(const_iterator begin, const_iterator end)
bool isEmpty() const const
QSharedPointer< X > dynamicCast() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 12 2024 11:51:09 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.