1/*
2 This file is part of the kcalcore library.
3
4 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
5 Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 Copyright (C) 2004 Bram Schoenmakers <bramschoenmakers@kde.nl>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22*/
23/**
24 @file
25 This file is part of the API for handling calendar data and
26 defines the CalFilter class.
27
28 @brief
29 Provides a filter for calendars.
30
31 @author Cornelius Schumacher \<schumacher@kde.org\>
32 @author Reinhold Kainhofer \<reinhold@kainhofer.com\>
33 @author Bram Schoenmakers \<bramschoenmakers@kde.nl\>
34*/
35
36#include "calfilter.h"
37
38using namespace KCalCore;
39
40/**
41 Private class that helps to provide binary compatibility between releases.
42 @internal
43*/
44//@cond PRIVATE
45class KCalCore::CalFilter::Private
46{
47public:
48 Private()
49 : mCriteria(0),
50 mCompletedTimeSpan(0),
51 mEnabled(true)
52 {}
53 QString mName; // filter name
54 QStringList mCategoryList;
55 QStringList mEmailList;
56 int mCriteria;
57 int mCompletedTimeSpan;
58 bool mEnabled;
59
60};
61//@endcond
62
63CalFilter::CalFilter() : d(new KCalCore::CalFilter::Private)
64{
65}
66
67CalFilter::CalFilter(const QString &name)
68 : d(new KCalCore::CalFilter::Private)
69{
70 d->mName = name;
71}
72
73CalFilter::~CalFilter()
74{
75 delete d;
76}
77
78bool KCalCore::CalFilter::operator==(const CalFilter &filter) const
79{
80 return d->mName == filter.d->mName &&
81 d->mCriteria == filter.d->mCriteria &&
82 d->mCategoryList == filter.d->mCategoryList &&
83 d->mEmailList == filter.d->mEmailList &&
84 d->mCompletedTimeSpan == filter.d->mCompletedTimeSpan;
85}
86
87void CalFilter::apply(Event::List *eventList) const
88{
89 if (!d->mEnabled) {
90 return;
91 }
92
93 Event::List::Iterator it = eventList->begin();
94 while (it != eventList->end()) {
95 if (!filterIncidence(*it)) {
96 it = eventList->erase(it);
97 } else {
98 ++it;
99 }
100 }
101}
102
103// TODO: avoid duplicating apply() code
104void CalFilter::apply(Todo::List *todoList) const
105{
106 if (!d->mEnabled) {
107 return;
108 }
109
110 Todo::List::Iterator it = todoList->begin();
111 while (it != todoList->end()) {
112 if (!filterIncidence(*it)) {
113 it = todoList->erase(it);
114 } else {
115 ++it;
116 }
117 }
118}
119
120void CalFilter::apply(Journal::List *journalList) const
121{
122 if (!d->mEnabled) {
123 return;
124 }
125
126 Journal::List::Iterator it = journalList->begin();
127 while (it != journalList->end()) {
128 if (!filterIncidence(*it)) {
129 it = journalList->erase(it);
130 } else {
131 ++it;
132 }
133 }
134}
135
136bool CalFilter::filterIncidence(Incidence::Ptr incidence) const
137{
138 if (!d->mEnabled) {
139 return true;
140 }
141
142 Todo::Ptr todo = incidence.dynamicCast<Todo>();
143 if (todo) {
144 if ((d->mCriteria & HideCompletedTodos) && todo->isCompleted()) {
145 // Check if completion date is suffently long ago:
146 if (todo->completed().addDays(d->mCompletedTimeSpan) <
147 KDateTime::currentUtcDateTime()) {
148 return false;
149 }
150 }
151
152 if ((d->mCriteria & HideInactiveTodos) &&
153 ((todo->hasStartDate() &&
154 KDateTime::currentUtcDateTime() < todo->dtStart()) ||
155 todo->isCompleted())) {
156 return false;
157 }
158
159 if (d->mCriteria & HideNoMatchingAttendeeTodos) {
160 bool iAmOneOfTheAttendees = false;
161 const Attendee::List &attendees = todo->attendees();
162 if (!todo->attendees().isEmpty()) {
163 Attendee::List::ConstIterator it;
164 for (it = attendees.begin(); it != attendees.end(); ++it) {
165 if (d->mEmailList.contains((*it)->email())) {
166 iAmOneOfTheAttendees = true;
167 break;
168 }
169 }
170 } else {
171 // no attendees, must be me only
172 iAmOneOfTheAttendees = true;
173 }
174 if (!iAmOneOfTheAttendees) {
175 return false;
176 }
177 }
178 }
179
180 if (d->mCriteria & HideRecurring) {
181 if (incidence->recurs() || incidence->hasRecurrenceId()) {
182 return false;
183 }
184 }
185
186 if (d->mCriteria & ShowCategories) {
187 for (QStringList::ConstIterator it = d->mCategoryList.constBegin();
188 it != d->mCategoryList.constEnd(); ++it) {
189 QStringList incidenceCategories = incidence->categories();
190 for (QStringList::ConstIterator it2 = incidenceCategories.constBegin();
191 it2 != incidenceCategories.constEnd(); ++it2) {
192 if ((*it) == (*it2)) {
193 return true;
194 }
195 }
196 }
197 return false;
198 } else {
199 for (QStringList::ConstIterator it = d->mCategoryList.constBegin();
200 it != d->mCategoryList.constEnd(); ++it) {
201 QStringList incidenceCategories = incidence->categories();
202 for (QStringList::ConstIterator it2 = incidenceCategories.constBegin();
203 it2 != incidenceCategories.constEnd(); ++it2) {
204 if ((*it) == (*it2)) {
205 return false;
206 }
207 }
208 }
209 return true;
210 }
211
212 return true;
213}
214
215void CalFilter::setName(const QString &name)
216{
217 d->mName = name;
218}
219
220QString CalFilter::name() const
221{
222 return d->mName;
223}
224
225void CalFilter::setEnabled(bool enabled)
226{
227 d->mEnabled = enabled;
228}
229
230bool CalFilter::isEnabled() const
231{
232 return d->mEnabled;
233}
234
235void CalFilter::setCriteria(int criteria)
236{
237 d->mCriteria = criteria;
238}
239
240int CalFilter::criteria() const
241{
242 return d->mCriteria;
243}
244
245void CalFilter::setCategoryList(const QStringList &categoryList)
246{
247 d->mCategoryList = categoryList;
248}
249
250QStringList CalFilter::categoryList() const
251{
252 return d->mCategoryList;
253}
254
255void CalFilter::setEmailList(const QStringList &emailList)
256{
257 d->mEmailList = emailList;
258}
259
260QStringList CalFilter::emailList() const
261{
262 return d->mEmailList;
263}
264
265void CalFilter::setCompletedTimeSpan(int timespan)
266{
267 d->mCompletedTimeSpan = timespan;
268}
269
270int CalFilter::completedTimeSpan() const
271{
272 return d->mCompletedTimeSpan;
273}
274