libkcal

calfilter.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of libkcal.
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 */
00023 
00024 #include <kdebug.h>
00025 
00026 #include "calfilter.h"
00027 
00028 using namespace KCal;
00029 
00030 CalFilter::CalFilter()
00031 {
00032   mEnabled = true;
00033   mCriteria = 0;
00034   mCompletedTimeSpan = 0;
00035 }
00036 
00037 CalFilter::CalFilter(const QString &name)
00038 {
00039   mName = name;
00040   mEnabled = true;
00041   mCriteria = 0;
00042   mCompletedTimeSpan = 0;
00043 }
00044 
00045 CalFilter::~CalFilter()
00046 {
00047 }
00048 
00049 void CalFilter::apply( Event::List *eventlist ) const
00050 {
00051   if ( !mEnabled ) return;
00052 
00053 //  kdDebug(5800) << "CalFilter::apply()" << endl;
00054 
00055   Event::List::Iterator it = eventlist->begin();
00056   while( it != eventlist->end() ) {
00057     if ( !filterIncidence( *it ) ) {
00058       it = eventlist->remove( it );
00059     } else {
00060       ++it;
00061     }
00062   }
00063 
00064 //  kdDebug(5800) << "CalFilter::apply() done" << endl;
00065 }
00066 
00067 // TODO: avoid duplicating apply() code
00068 void CalFilter::apply( Todo::List *todolist ) const
00069 {
00070   if ( !mEnabled ) return;
00071 
00072 //  kdDebug(5800) << "CalFilter::apply()" << endl;
00073 
00074   Todo::List::Iterator it = todolist->begin();
00075   while( it != todolist->end() ) {
00076     if ( !filterIncidence( *it ) ) {
00077       it = todolist->remove( it );
00078     } else {
00079       ++it;
00080     }
00081   }
00082 
00083 //  kdDebug(5800) << "CalFilter::apply() done" << endl;
00084 }
00085 
00086 void CalFilter::apply( Journal::List *journallist ) const
00087 {
00088   if ( !mEnabled ) return;
00089 
00090   Journal::List::Iterator it = journallist->begin();
00091   while( it != journallist->end() ) {
00092     if ( !filterIncidence( *it ) ) {
00093       it = journallist->remove( it );
00094     } else {
00095       ++it;
00096     }
00097   }
00098 }
00099 
00100 bool CalFilter::filterIncidence(Incidence *incidence) const
00101 {
00102 //  kdDebug(5800) << "CalFilter::filterIncidence(): " << incidence->summary() << endl;
00103 
00104   if ( !mEnabled ) return true;
00105 
00106   Todo *todo = dynamic_cast<Todo *>(incidence);
00107   if( todo ) {
00108     if ( (mCriteria & HideCompleted) && todo->isCompleted() ) {
00109       // Check if completion date is suffently long ago:
00110       if ( todo->completed().addDays( mCompletedTimeSpan ) <
00111            QDateTime::currentDateTime() ) {
00112         return false;
00113       }
00114     }
00115 
00116     if( ( mCriteria & HideInactiveTodos ) &&
00117         ( todo->hasStartDate() &&
00118           QDateTime::currentDateTime() < todo->dtStart() ||
00119           todo->isCompleted() ) )
00120       return false;
00121 
00122     if ( mCriteria & HideTodosWithoutAttendeeInEmailList ) {
00123       bool iAmOneOfTheAttendees = false;
00124       const Attendee::List &attendees = todo->attendees();
00125       if ( !todo->attendees().isEmpty() ) {
00126         Attendee::List::ConstIterator it;
00127         for( it = attendees.begin(); it != attendees.end(); ++it ) {
00128           if ( mEmailList.find( (*it)->email() ) != mEmailList.end() ) {
00129             iAmOneOfTheAttendees = true;
00130             break;
00131           }
00132         }
00133       } else {
00134         // no attendees, must be me only
00135         iAmOneOfTheAttendees = true;
00136       }
00137       if ( !iAmOneOfTheAttendees )
00138         return false;
00139     }
00140   }
00141 
00142 
00143   if (mCriteria & HideRecurring) {
00144     if (incidence->doesRecur()) return false;
00145   }
00146 
00147   if (mCriteria & ShowCategories) {
00148     for (QStringList::ConstIterator it = mCategoryList.constBegin();
00149          it != mCategoryList.constEnd(); ++it ) {
00150       QStringList incidenceCategories = incidence->categories();
00151       for (QStringList::ConstIterator it2 = incidenceCategories.constBegin();
00152            it2 != incidenceCategories.constEnd(); ++it2 ) {
00153         if ((*it) == (*it2)) {
00154           return true;
00155         }
00156       }
00157     }
00158     return false;
00159   } else {
00160     for (QStringList::ConstIterator it = mCategoryList.constBegin();
00161          it != mCategoryList.constEnd(); ++it ) {
00162       QStringList incidenceCategories = incidence->categories();
00163       for (QStringList::ConstIterator it2 = incidenceCategories.constBegin();
00164            it2 != incidenceCategories.constEnd(); ++it2 ) {
00165         if ((*it) == (*it2)) {
00166           return false;
00167         }
00168       }
00169     }
00170     return true;
00171   }
00172 
00173 //  kdDebug(5800) << "CalFilter::filterIncidence(): passed" << endl;
00174 
00175   return true;
00176 }
00177 
00178 void CalFilter::setEnabled(bool enabled)
00179 {
00180   mEnabled = enabled;
00181 }
00182 
00183 bool CalFilter::isEnabled() const
00184 {
00185   return mEnabled;
00186 }
00187 
00188 void CalFilter::setCriteria(int criteria)
00189 {
00190   mCriteria = criteria;
00191 }
00192 
00193 int CalFilter::criteria() const
00194 {
00195   return mCriteria;
00196 }
00197 
00198 void CalFilter::setCategoryList(const QStringList &categoryList)
00199 {
00200   mCategoryList = categoryList;
00201 }
00202 
00203 QStringList CalFilter::categoryList() const
00204 {
00205   return mCategoryList;
00206 }
00207 
00208 void CalFilter::setEmailList(const QStringList &emailList)
00209 {
00210   mEmailList = emailList;
00211 }
00212 
00213 QStringList CalFilter::emailList() const
00214 {
00215   return mEmailList;
00216 }
00217 
00218 void CalFilter::setCompletedTimeSpan( int timespan )
00219 {
00220   mCompletedTimeSpan = timespan;
00221 }
00222 
00223 int CalFilter::completedTimeSpan() const
00224 {
00225   return mCompletedTimeSpan;
00226 }