kitchensync

addressbookfilter.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 <qgroupbox.h>
00023 #include <qheader.h>
00024 #include <qlabel.h>
00025 #include <qlayout.h>
00026 #include <qwhatsthis.h>
00027 #include <qwidget.h>
00028 
00029 #include <kgenericfactory.h>
00030 #include <klistview.h>
00031 #include <klocale.h>
00032 #include <libkdepim/kpimprefs.h>
00033 
00034 #include "addressbooksyncee.h"
00035 
00036 #include "addressbookfilter.h"
00037 
00038 using namespace KSync;
00039 
00040 K_EXPORT_KS_FILTER( libksfilter_addressbook, AddressBookFilter )
00041 
00042 AddressBookFilter::AddressBookFilter( QObject *parent )
00043   : Filter( parent, "AddressBookFilter" )
00044 {
00045   setName( i18n( "Address Book Filter" ) );
00046 }
00047 
00048 AddressBookFilter::~AddressBookFilter()
00049 {
00050 }
00051 
00052 bool AddressBookFilter::supports( Syncee *syncee )
00053 {
00054   return (dynamic_cast<AddressBookSyncee*>( syncee ) != 0);
00055 }
00056 
00057 QWidget *AddressBookFilter::configWidget( QWidget *parent )
00058 {
00059   AddressBookConfigWidget *wdg = new AddressBookConfigWidget( parent, "AddressBookConfigWidget" );
00060 
00061   KPimPrefs prefs;
00062   prefs.usrReadConfig();
00063   wdg->setCategories( prefs.mCustomCategories );
00064   wdg->setSelectedCategories( mSelectedCategories );
00065 
00066   return wdg;
00067 }
00068 
00069 void AddressBookFilter::configWidgetClosed( QWidget *widget )
00070 {
00071   AddressBookConfigWidget *wdg = static_cast<AddressBookConfigWidget*>( widget );
00072   mSelectedCategories = wdg->selectedCategories();
00073 }
00074 
00075 void AddressBookFilter::convert( Syncee *syncee )
00076 {
00077   filterSyncee( dynamic_cast<AddressBookSyncee*>( syncee ), mSelectedCategories );
00078 }
00079 
00080 void AddressBookFilter::reconvert( Syncee *syncee )
00081 {
00082   unfilterSyncee( dynamic_cast<AddressBookSyncee*>( syncee ) );
00083 }
00084 
00085 void AddressBookFilter::doLoad()
00086 {
00087   mSelectedCategories = config()->readListEntry( "SelectedCategories" );
00088 }
00089 
00090 void AddressBookFilter::doSave()
00091 {
00092   config()->writeEntry( "SelectedCategories", mSelectedCategories );
00093 }
00094 
00095 void AddressBookFilter::filterSyncee( AddressBookSyncee *syncee, const QStringList &categories )
00096 {
00097   mFilteredEntries.clear();
00098 
00099   if ( categories.isEmpty() ) // do not filter
00100     return;
00101 
00102   QStringList::ConstIterator it;
00103   
00104   AddressBookSyncEntry *entry;
00105   for ( entry = syncee->firstEntry(); entry; entry = syncee->nextEntry() ) {
00106     bool found = false;
00107     for ( it = categories.begin(); it != categories.end(); ++it )
00108       if ( entry->addressee().categories().contains( *it ) ) {
00109         found = true;
00110         break;
00111       }
00112 
00113     if ( !found )
00114       mFilteredEntries.append( entry );
00115   }
00116 
00117   QPtrListIterator<AddressBookSyncEntry> entryIt( mFilteredEntries );
00118   while ( entryIt.current() ) {
00119     syncee->removeEntry( entryIt.current() );
00120     ++entryIt;
00121   }
00122 }
00123 
00124 void AddressBookFilter::unfilterSyncee( AddressBookSyncee *syncee )
00125 {
00126   QPtrListIterator<AddressBookSyncEntry> entryIt( mFilteredEntries );
00127   while ( entryIt.current() ) {
00128     syncee->addEntry( entryIt.current() );
00129     ++entryIt;
00130   }
00131 }
00132 
00133 
00134 
00135 AddressBookConfigWidget::AddressBookConfigWidget( QWidget *parent, const char *name )
00136   : QWidget( parent, name )
00137 {
00138   QVBoxLayout *layout = new QVBoxLayout( this );
00139 
00140   QGroupBox *box = new QGroupBox( 1, Qt::Vertical, i18n( "Contacts" ), this );
00141   layout->addWidget( box );
00142 
00143   mView = new KListView( box );
00144   mView->addColumn( i18n( "Categories" ) );
00145   mView->setFullWidth( true );
00146 
00147   QWhatsThis::add( mView, i18n( "Select the categories for which the contacts shall be synchronized. When no category is selected, all contacts will be included." ) );
00148 }
00149 
00150 void AddressBookConfigWidget::setCategories( const QStringList &categories )
00151 {
00152   mView->clear();
00153 
00154   QStringList::ConstIterator it;
00155   for ( it = categories.begin(); it != categories.end(); ++it )
00156     new QCheckListItem( mView, *it, QCheckListItem::CheckBox );
00157 }
00158 
00159 void AddressBookConfigWidget::setSelectedCategories( const QStringList &categories )
00160 {
00161   QListViewItemIterator itemIt( mView );
00162   QStringList::ConstIterator it;
00163 
00164   while ( itemIt.current() ) {
00165     bool found = false;
00166     for ( it = categories.begin(); it != categories.end(); ++it ) {
00167       if ( itemIt.current()->text( 0 ) == *it ) {
00168         found = true;
00169         break;
00170       }
00171     }
00172 
00173     QCheckListItem *item = static_cast<QCheckListItem*>( itemIt.current() );
00174     item->setOn( found );
00175 
00176     ++itemIt;
00177   }
00178 }
00179 
00180 QStringList AddressBookConfigWidget::selectedCategories() const
00181 {
00182   QStringList categories;
00183 
00184   QListViewItemIterator itemIt( mView, QListViewItemIterator::Checked );
00185   while ( itemIt.current() )
00186     categories.append( itemIt.current()->text( 0 ) );
00187 
00188   return categories;
00189 }
KDE Home | KDE Accessibility Home | Description of Access Keys