KCal Library
calfilter.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of the kcal library. 00003 00004 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 00005 Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com> 00006 Copyright (C) 2004 Bram Schoenmakers <bramschoenmakers@kde.nl> 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Library General Public 00010 License as published by the Free Software Foundation; either 00011 version 2 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Library General Public License for more details. 00017 00018 You should have received a copy of the GNU Library General Public License 00019 along with this library; see the file COPYING.LIB. If not, write to 00020 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00021 Boston, MA 02110-1301, USA. 00022 */ 00036 #include "calfilter.h" 00037 00038 #include <kdebug.h> 00039 00040 using namespace KCal; 00041 00046 //@cond PRIVATE 00047 class KCal::CalFilter::Private 00048 { 00049 public: 00050 Private() 00051 : mCriteria( 0 ), 00052 mCompletedTimeSpan( 0 ), 00053 mEnabled( true ) 00054 {} 00055 QString mName; // filter name 00056 QStringList mCategoryList; 00057 QStringList mEmailList; 00058 int mCriteria; 00059 int mCompletedTimeSpan; 00060 bool mEnabled; 00061 00062 }; 00063 //@endcond 00064 00065 CalFilter::CalFilter() : d( new KCal::CalFilter::Private ) 00066 { 00067 } 00068 00069 CalFilter::CalFilter( const QString &name ) 00070 : d( new KCal::CalFilter::Private ) 00071 { 00072 d->mName = name; 00073 } 00074 00075 CalFilter::~CalFilter() 00076 { 00077 delete d; 00078 } 00079 00080 bool KCal::CalFilter::operator==( const CalFilter &filter ) 00081 { 00082 return d->mName == filter.d->mName && 00083 d->mCriteria == filter.d->mCriteria && 00084 d->mCategoryList == filter.d->mCategoryList && 00085 d->mEmailList == filter.d->mEmailList && 00086 d->mCompletedTimeSpan == filter.d->mCompletedTimeSpan; 00087 } 00088 00089 void CalFilter::apply( Event::List *eventList ) const 00090 { 00091 if ( !d->mEnabled ) { 00092 return; 00093 } 00094 00095 Event::List::Iterator it = eventList->begin(); 00096 while ( it != eventList->end() ) { 00097 if ( !filterIncidence( *it ) ) { 00098 it = eventList->erase( it ); 00099 } else { 00100 ++it; 00101 } 00102 } 00103 } 00104 00105 // TODO: avoid duplicating apply() code 00106 void CalFilter::apply( Todo::List *todoList ) const 00107 { 00108 if ( !d->mEnabled ) { 00109 return; 00110 } 00111 00112 Todo::List::Iterator it = todoList->begin(); 00113 while ( it != todoList->end() ) { 00114 if ( !filterIncidence( *it ) ) { 00115 it = todoList->erase( it ); 00116 } else { 00117 ++it; 00118 } 00119 } 00120 } 00121 00122 void CalFilter::apply( Journal::List *journalList ) const 00123 { 00124 if ( !d->mEnabled ) { 00125 return; 00126 } 00127 00128 Journal::List::Iterator it = journalList->begin(); 00129 while ( it != journalList->end() ) { 00130 if ( !filterIncidence( *it ) ) { 00131 it = journalList->erase( it ); 00132 } else { 00133 ++it; 00134 } 00135 } 00136 } 00137 00138 bool CalFilter::filterIncidence( Incidence *incidence ) const 00139 { 00140 if ( !d->mEnabled ) { 00141 return true; 00142 } 00143 00144 Todo *todo = dynamic_cast<Todo *>( incidence ); 00145 if ( todo ) { 00146 if ( ( d->mCriteria & HideCompletedTodos ) && todo->isCompleted() ) { 00147 // Check if completion date is suffently long ago: 00148 if ( todo->completed().addDays( d->mCompletedTimeSpan ) < 00149 KDateTime::currentUtcDateTime() ) { 00150 return false; 00151 } 00152 } 00153 00154 if ( ( d->mCriteria & HideInactiveTodos ) && 00155 ( ( todo->hasStartDate() && 00156 KDateTime::currentUtcDateTime() < todo->dtStart() ) || 00157 todo->isCompleted() ) ) { 00158 return false; 00159 } 00160 00161 if ( d->mCriteria & HideNoMatchingAttendeeTodos ) { 00162 bool iAmOneOfTheAttendees = false; 00163 const Attendee::List &attendees = todo->attendees(); 00164 if ( !todo->attendees().isEmpty() ) { 00165 Attendee::List::ConstIterator it; 00166 for ( it = attendees.begin(); it != attendees.end(); ++it ) { 00167 if ( d->mEmailList.contains( (*it)->email() ) ) { 00168 iAmOneOfTheAttendees = true; 00169 break; 00170 } 00171 } 00172 } else { 00173 // no attendees, must be me only 00174 iAmOneOfTheAttendees = true; 00175 } 00176 if ( !iAmOneOfTheAttendees ) { 00177 return false; 00178 } 00179 } 00180 } 00181 00182 if ( d->mCriteria & HideRecurring ) { 00183 if ( incidence->recurs() ) { 00184 return false; 00185 } 00186 } 00187 00188 if ( d->mCriteria & ShowCategories ) { 00189 for ( QStringList::ConstIterator it = d->mCategoryList.constBegin(); 00190 it != d->mCategoryList.constEnd(); ++it ) { 00191 QStringList incidenceCategories = incidence->categories(); 00192 for ( QStringList::ConstIterator it2 = incidenceCategories.constBegin(); 00193 it2 != incidenceCategories.constEnd(); ++it2 ) { 00194 if ( (*it) == (*it2) ) { 00195 return true; 00196 } 00197 } 00198 } 00199 return false; 00200 } else { 00201 for ( QStringList::ConstIterator it = d->mCategoryList.constBegin(); 00202 it != d->mCategoryList.constEnd(); ++it ) { 00203 QStringList incidenceCategories = incidence->categories(); 00204 for ( QStringList::ConstIterator it2 = incidenceCategories.constBegin(); 00205 it2 != incidenceCategories.constEnd(); ++it2 ) { 00206 if ( (*it) == (*it2) ) { 00207 return false; 00208 } 00209 } 00210 } 00211 return true; 00212 } 00213 00214 return true; 00215 } 00216 00217 void CalFilter::setName( const QString &name ) 00218 { 00219 d->mName = name; 00220 } 00221 00222 QString CalFilter::name() const 00223 { 00224 return d->mName; 00225 } 00226 00227 void CalFilter::setEnabled( bool enabled ) 00228 { 00229 d->mEnabled = enabled; 00230 } 00231 00232 bool CalFilter::isEnabled() const 00233 { 00234 return d->mEnabled; 00235 } 00236 00237 void CalFilter::setCriteria( int criteria ) 00238 { 00239 d->mCriteria = criteria; 00240 } 00241 00242 int CalFilter::criteria() const 00243 { 00244 return d->mCriteria; 00245 } 00246 00247 void CalFilter::setCategoryList( const QStringList &categoryList ) 00248 { 00249 d->mCategoryList = categoryList; 00250 } 00251 00252 QStringList CalFilter::categoryList() const 00253 { 00254 return d->mCategoryList; 00255 } 00256 00257 void CalFilter::setEmailList( const QStringList &emailList ) 00258 { 00259 d->mEmailList = emailList; 00260 } 00261 00262 QStringList CalFilter::emailList() const 00263 { 00264 return d->mEmailList; 00265 } 00266 00267 void CalFilter::setCompletedTimeSpan( int timespan ) 00268 { 00269 d->mCompletedTimeSpan = timespan; 00270 } 00271 00272 int CalFilter::completedTimeSpan() const 00273 { 00274 return d->mCompletedTimeSpan; 00275 }