kitchensync

calendarfilter.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include <qcheckbox.h>
00023 #include <qgroupbox.h>
00024 #include <qheader.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qwhatsthis.h>
00028 #include <qwidget.h>
00029 
00030 #include <kdialog.h>
00031 #include <kgenericfactory.h>
00032 #include <klistview.h>
00033 #include <klocale.h>
00034 #include <libkdepim/kdateedit.h>
00035 #include <libkdepim/kpimprefs.h>
00036 
00037 #include "calendarsyncee.h"
00038 
00039 #include "calendarfilter.h"
00040 
00041 using namespace KSync;
00042 
00043 K_EXPORT_KS_FILTER( libksfilter_calendar, CalendarFilter )
00044 
00045 CalendarFilter::CalendarFilter( QObject *parent )
00046   : Filter( parent, "CalendarFilter" )
00047 {
00048   setName( i18n( "Calendar Filter" ) );
00049 }
00050 
00051 CalendarFilter::~CalendarFilter()
00052 {
00053 }
00054 
00055 bool CalendarFilter::supports( Syncee *syncee )
00056 {
00057   return (dynamic_cast<CalendarSyncee*>( syncee ) != 0);
00058 }
00059 
00060 QWidget *CalendarFilter::configWidget( QWidget *parent )
00061 {
00062   CalendarConfigWidget *wdg = new CalendarConfigWidget( parent, "CalendarConfigWidget" );
00063 
00064   KPimPrefs prefs;
00065   prefs.usrReadConfig();
00066   wdg->setCategories( prefs.mCustomCategories );
00067   wdg->setSelectedCategories( mSelectedCategories );
00068   wdg->setUseDate( mFilterByDate );
00069   wdg->setStartDate( mStartDate );
00070   wdg->setEndDate( mEndDate );
00071 
00072   return wdg;
00073 }
00074 
00075 void CalendarFilter::configWidgetClosed( QWidget *widget )
00076 {
00077   CalendarConfigWidget *wdg = static_cast<CalendarConfigWidget*>( widget );
00078   mSelectedCategories = wdg->selectedCategories();
00079   mFilterByDate = wdg->useDate();
00080   mStartDate = wdg->startDate();
00081   mEndDate = wdg->endDate();
00082 }
00083 
00084 void CalendarFilter::convert( Syncee *syncee )
00085 {
00086   filterSyncee( dynamic_cast<CalendarSyncee*>( syncee ), mSelectedCategories, mStartDate, mEndDate );
00087 }
00088 
00089 void CalendarFilter::reconvert( Syncee *syncee )
00090 {
00091   unfilterSyncee( dynamic_cast<CalendarSyncee*>( syncee ) );
00092 }
00093 
00094 void CalendarFilter::doLoad()
00095 {
00096   mSelectedCategories = config()->readListEntry( "SelectedCategories" );
00097   mFilterByDate = config()->readBoolEntry( "FilterByDate", false );
00098   mStartDate = config()->readDateTimeEntry( "StartDate" ).date();
00099   mEndDate = config()->readDateTimeEntry( "EndDate" ).date();
00100 }
00101 
00102 void CalendarFilter::doSave()
00103 {
00104   config()->writeEntry( "SelectedCategories", mSelectedCategories );
00105   config()->writeEntry( "FilterByDate", mFilterByDate );
00106   config()->writeEntry( "StartDate", QDateTime( mStartDate ) );
00107   config()->writeEntry( "EndDate", QDateTime( mEndDate ) );
00108 }
00109 
00110 void CalendarFilter::filterSyncee( CalendarSyncee *syncee, const QStringList &categories,
00111                                    const QDate &startDate, const QDate &endDate )
00112 {
00113   mFilteredEntries.clear();
00114 
00115   if ( categories.isEmpty() ) // do not filter
00116     return;
00117 
00118   QStringList::ConstIterator it;
00119   
00120   CalendarSyncEntry *entry;
00121   for ( entry = syncee->firstEntry(); entry; entry = syncee->nextEntry() ) {
00122     bool found = false;
00123     for ( it = categories.begin(); it != categories.end(); ++it )
00124       if ( entry->incidence()->categories().contains( *it ) ) {
00125         if ( mFilterByDate ) {
00126           if ( entry->incidence()->dtStart().date() >= startDate &&
00127                entry->incidence()->dtStart().date() <= endDate )
00128             found = true;
00129         } else
00130           found = true;
00131         break;
00132       }
00133 
00134     if ( !found )
00135       mFilteredEntries.append( entry );
00136   }
00137 
00138   QPtrListIterator<CalendarSyncEntry> entryIt( mFilteredEntries );
00139   while ( entryIt.current() ) {
00140     syncee->removeEntry( entryIt.current() );
00141     ++entryIt;
00142   }
00143 }
00144 
00145 void CalendarFilter::unfilterSyncee( CalendarSyncee *syncee )
00146 {
00147   QPtrListIterator<CalendarSyncEntry> entryIt( mFilteredEntries );
00148   while ( entryIt.current() ) {
00149     syncee->addEntry( entryIt.current() );
00150     ++entryIt;
00151   }
00152 }
00153 
00154 
00155 
00156 CalendarConfigWidget::CalendarConfigWidget( QWidget *parent, const char *name )
00157   : QWidget( parent, name )
00158 {
00159   QVBoxLayout *layout = new QVBoxLayout( this );
00160 
00161   QGroupBox *box = new QGroupBox( 2, Qt::Vertical, i18n( "Events && Todos" ), this );
00162 
00163   mView = new KListView( box );
00164   mView->addColumn( i18n( "Categories" ) );
00165   mView->setFullWidth( true );
00166 
00167   QWhatsThis::add( mView, i18n( "Select the categories for which the events and todos shall be synchronized. When no category is selected, all events and todos will be included." ) );
00168 
00169   QWidget *timeWidget = new QWidget( box );
00170   QGridLayout *wdgLayout = new QGridLayout( timeWidget, 3, 3, KDialog::marginHint(),
00171                                             KDialog::spacingHint() );
00172 
00173   mUseDate = new QCheckBox( i18n( "Use time range" ), timeWidget );
00174   wdgLayout->addMultiCellWidget( mUseDate, 0, 0, 0, 1 );
00175 
00176   QWhatsThis::add( mUseDate, i18n( "Synchronize only events and todos in a special time range." ) );
00177 
00178   mStartLabel = new QLabel( i18n( "start date", "From:" ), timeWidget );
00179   mStartDate = new KDateEdit( timeWidget );
00180   mStartLabel->setBuddy( mStartDate );
00181 
00182   mEndLabel = new QLabel( i18n( "end date", "Till:" ), timeWidget );
00183   mEndDate = new KDateEdit( timeWidget );
00184   mEndLabel->setBuddy( mEndDate );
00185 
00186   wdgLayout->addWidget( mStartLabel, 1, 0, Qt::AlignRight );
00187   wdgLayout->addWidget( mStartDate, 1, 1 );
00188   wdgLayout->addWidget( mEndLabel, 2, 0, Qt::AlignRight );
00189   wdgLayout->addWidget( mEndDate, 2, 1 );
00190   wdgLayout->setColStretch( 2, 10 );
00191 
00192   QWhatsThis::add( box, i18n( "Only the events and todos in the given date range will be synchronized." ) );
00193 
00194   layout->addWidget( box );
00195 
00196   connect( mUseDate, SIGNAL( toggled( bool ) ), this, SLOT( useDateChanged( bool ) ) );
00197 
00198 }
00199 
00200 void CalendarConfigWidget::setCategories( const QStringList &categories )
00201 {
00202   mView->clear();
00203 
00204   QStringList::ConstIterator it;
00205   for ( it = categories.begin(); it != categories.end(); ++it )
00206     new QCheckListItem( mView, *it, QCheckListItem::CheckBox );
00207 }
00208 
00209 void CalendarConfigWidget::setSelectedCategories( const QStringList &categories )
00210 {
00211   QListViewItemIterator itemIt( mView );
00212   QStringList::ConstIterator it;
00213 
00214   while ( itemIt.current() ) {
00215     bool found = false;
00216     for ( it = categories.begin(); it != categories.end(); ++it ) {
00217       if ( itemIt.current()->text( 0 ) == *it ) {
00218         found = true;
00219         break;
00220       }
00221     }
00222 
00223     QCheckListItem *item = static_cast<QCheckListItem*>( itemIt.current() );
00224     item->setOn( found );
00225 
00226     ++itemIt;
00227   }
00228 }
00229 
00230 QStringList CalendarConfigWidget::selectedCategories() const
00231 {
00232   QStringList categories;
00233 
00234   QListViewItemIterator itemIt( mView, QListViewItemIterator::Checked );
00235   while ( itemIt.current() )
00236     categories.append( itemIt.current()->text( 0 ) );
00237 
00238   return categories;
00239 }
00240 
00241 void CalendarConfigWidget::setStartDate( const QDate &date )
00242 {
00243   mStartDate->setDate( date );
00244 }
00245 
00246 QDate CalendarConfigWidget::startDate() const
00247 {
00248   return mStartDate->date();
00249 }
00250 
00251 void CalendarConfigWidget::setEndDate( const QDate &date )
00252 {
00253   mEndDate->setDate( date );
00254 }
00255 
00256 QDate CalendarConfigWidget::endDate() const
00257 {
00258   return mEndDate->date();
00259 }
00260 
00261 void CalendarConfigWidget::setUseDate( bool value )
00262 {
00263   mUseDate->setChecked( value );
00264   useDateChanged( value );
00265 }
00266 
00267 bool CalendarConfigWidget::useDate() const
00268 {
00269   return mUseDate->isChecked();
00270 }
00271 
00272 void CalendarConfigWidget::useDateChanged( bool value )
00273 {
00274   mStartLabel->setEnabled( value );
00275   mStartDate->setEnabled( value );
00276   mEndDate->setEnabled( value );
00277   mEndLabel->setEnabled( value );
00278 }
00279 
00280 #include "calendarfilter.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys