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 if (!incidence) {
113 return true;
114 }
115
116 Todo::Ptr todo = incidence.dynamicCast<Todo>();
117 if (todo) {
118 if ((d->mCriteria & HideCompletedTodos) && todo->isCompleted()) {
119 // Check if completion date is suffently long ago:
120 if (todo->completed().addDays(d->mCompletedTimeSpan) < QDateTime::currentDateTimeUtc()) {
121 return false;
122 }
123 }
124
125 if ((d->mCriteria & HideInactiveTodos) && ((todo->hasStartDate() && QDateTime::currentDateTimeUtc() < todo->dtStart()) || todo->isCompleted())) {
126 return false;
127 }
128
129 if (d->mCriteria & HideNoMatchingAttendeeTodos) {
130 bool iAmOneOfTheAttendees = false;
131 const Attendee::List &attendees = todo->attendees();
132 if (!attendees.isEmpty()) {
133 iAmOneOfTheAttendees = std::any_of(attendees.cbegin(), attendees.cend(), [this](const Attendee &att) {
134 return d->mEmailList.contains(att.email());
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 const QStringList incidenceCategories = incidence->categories();
153 bool isFound = false;
154 for (const auto &category : std::as_const(d->mCategoryList)) {
155 if (incidenceCategories.contains(category)) {
156 isFound = true;
157 break;
158 }
159 }
160
161 return (d->mCriteria & ShowCategories) ? isFound : !isFound;
162}
163
165{
166 d->mName = name;
167}
168
170{
171 return d->mName;
172}
173
174void CalFilter::setEnabled(bool enabled)
175{
176 d->mEnabled = enabled;
177}
178
180{
181 return d->mEnabled;
182}
183
185{
186 d->mCriteria = criteria;
187}
188
190{
191 return d->mCriteria;
192}
193
195{
196 d->mCategoryList = categoryList;
197}
198
200{
201 return d->mCategoryList;
202}
203
205{
206 d->mEmailList = emailList;
207}
208
210{
211 return d->mEmailList;
212}
213
215{
216 d->mCompletedTimeSpan = timespan;
217}
218
220{
221 return d->mCompletedTimeSpan;
222}
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
QList< Attendee > List
List of attendees.
Definition attendee.h:109
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.
QList< Ptr > List
List of events.
Definition event.h:55
QSharedPointer< Incidence > Ptr
A shared pointer to an Incidence.
Definition incidence.h:117
QList< Ptr > List
List of journals.
Definition journal.h:43
Provides a To-do in the sense of RFC2445.
Definition todo.h:34
QSharedPointer< Todo > Ptr
A shared pointer to a Todo object.
Definition todo.h:39
QList< Ptr > List
List of to-dos.
Definition todo.h:44
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-2025 The KDE developers.
Generated on Fri May 2 2025 11:53:22 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.