1/*
2 This file is part of the kcal 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
38#include <kdebug.h>
39
40using namespace KCal;
41
42/**
43 Private class that helps to provide binary compatibility between releases.
44 @internal
45*/
46//@cond PRIVATE
47class KCal::CalFilter::Private
48{
49 public:
50 Private()
51 : mCriteria( 0 ),
52 mCompletedTimeSpan( 0 ),
53 mEnabled( true )
54 {}
55 QString mName; // filter name
56 QStringList mCategoryList;
57 QStringList mEmailList;
58 int mCriteria;
59 int mCompletedTimeSpan;
60 bool mEnabled;
61
62};
63//@endcond
64
65CalFilter::CalFilter() : d( new KCal::CalFilter::Private )
66{
67}
68
69CalFilter::CalFilter( const QString &name )
70 : d( new KCal::CalFilter::Private )
71{
72 d->mName = name;
73}
74
75CalFilter::~CalFilter()
76{
77 delete d;
78}
79
80bool KCal::CalFilter::operator==( const CalFilter &filter )
81{
82 return d->mName == filter.d->mName &&
83 d->mCriteria == filter.d->mCriteria &&
84 d->mCategoryList == filter.d->mCategoryList &&
85 d->mEmailList == filter.d->mEmailList &&
86 d->mCompletedTimeSpan == filter.d->mCompletedTimeSpan;
87}
88
89void CalFilter::apply( Event::List *eventList ) const
90{
91 if ( !d->mEnabled ) {
92 return;
93 }
94
95 Event::List::Iterator it = eventList->begin();
96 while ( it != eventList->end() ) {
97 if ( !filterIncidence( *it ) ) {
98 it = eventList->erase( it );
99 } else {
100 ++it;
101 }
102 }
103}
104
105// TODO: avoid duplicating apply() code
106void CalFilter::apply( Todo::List *todoList ) const
107{
108 if ( !d->mEnabled ) {
109 return;
110 }
111
112 Todo::List::Iterator it = todoList->begin();
113 while ( it != todoList->end() ) {
114 if ( !filterIncidence( *it ) ) {
115 it = todoList->erase( it );
116 } else {
117 ++it;
118 }
119 }
120}
121
122void CalFilter::apply( Journal::List *journalList ) const
123{
124 if ( !d->mEnabled ) {
125 return;
126 }
127
128 Journal::List::Iterator it = journalList->begin();
129 while ( it != journalList->end() ) {
130 if ( !filterIncidence( *it ) ) {
131 it = journalList->erase( it );
132 } else {
133 ++it;
134 }
135 }
136}
137
138bool CalFilter::filterIncidence( Incidence *incidence ) const
139{
140 if ( !d->mEnabled ) {
141 return true;
142 }
143
144 Todo *todo = dynamic_cast<Todo *>( incidence );
145 if ( todo ) {
146 if ( ( d->mCriteria & HideCompletedTodos ) && todo->isCompleted() ) {
147 // Check if completion date is suffently long ago:
148 if ( todo->completed().addDays( d->mCompletedTimeSpan ) <
149 KDateTime::currentUtcDateTime() ) {
150 return false;
151 }
152 }
153
154 if ( ( d->mCriteria & HideInactiveTodos ) &&
155 ( ( todo->hasStartDate() &&
156 KDateTime::currentUtcDateTime() < todo->dtStart() ) ||
157 todo->isCompleted() ) ) {
158 return false;
159 }
160
161 if ( d->mCriteria & HideNoMatchingAttendeeTodos ) {
162 bool iAmOneOfTheAttendees = false;
163 const Attendee::List &attendees = todo->attendees();
164 if ( !todo->attendees().isEmpty() ) {
165 Attendee::List::ConstIterator it;
166 for ( it = attendees.begin(); it != attendees.end(); ++it ) {
167 if ( d->mEmailList.contains( (*it)->email() ) ) {
168 iAmOneOfTheAttendees = true;
169 break;
170 }
171 }
172 } else {
173 // no attendees, must be me only
174 iAmOneOfTheAttendees = true;
175 }
176 if ( !iAmOneOfTheAttendees ) {
177 return false;
178 }
179 }
180 }
181
182 if ( d->mCriteria & HideRecurring ) {
183 if ( incidence->recurs() ) {
184 return false;
185 }
186 }
187
188 if ( d->mCriteria & ShowCategories ) {
189 for ( QStringList::ConstIterator it = d->mCategoryList.constBegin();
190 it != d->mCategoryList.constEnd(); ++it ) {
191 QStringList incidenceCategories = incidence->categories();
192 for ( QStringList::ConstIterator it2 = incidenceCategories.constBegin();
193 it2 != incidenceCategories.constEnd(); ++it2 ) {
194 if ( (*it) == (*it2) ) {
195 return true;
196 }
197 }
198 }
199 return false;
200 } else {
201 for ( QStringList::ConstIterator it = d->mCategoryList.constBegin();
202 it != d->mCategoryList.constEnd(); ++it ) {
203 QStringList incidenceCategories = incidence->categories();
204 for ( QStringList::ConstIterator it2 = incidenceCategories.constBegin();
205 it2 != incidenceCategories.constEnd(); ++it2 ) {
206 if ( (*it) == (*it2) ) {
207 return false;
208 }
209 }
210 }
211 return true;
212 }
213
214 return true;
215}
216
217void CalFilter::setName( const QString &name )
218{
219 d->mName = name;
220}
221
222QString CalFilter::name() const
223{
224 return d->mName;
225}
226
227void CalFilter::setEnabled( bool enabled )
228{
229 d->mEnabled = enabled;
230}
231
232bool CalFilter::isEnabled() const
233{
234 return d->mEnabled;
235}
236
237void CalFilter::setCriteria( int criteria )
238{
239 d->mCriteria = criteria;
240}
241
242int CalFilter::criteria() const
243{
244 return d->mCriteria;
245}
246
247void CalFilter::setCategoryList( const QStringList &categoryList )
248{
249 d->mCategoryList = categoryList;
250}
251
252QStringList CalFilter::categoryList() const
253{
254 return d->mCategoryList;
255}
256
257void CalFilter::setEmailList( const QStringList &emailList )
258{
259 d->mEmailList = emailList;
260}
261
262QStringList CalFilter::emailList() const
263{
264 return d->mEmailList;
265}
266
267void CalFilter::setCompletedTimeSpan( int timespan )
268{
269 d->mCompletedTimeSpan = timespan;
270}
271
272int CalFilter::completedTimeSpan() const
273{
274 return d->mCompletedTimeSpan;
275}
276