kaddressbook

searchmanager.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <kabc/addresseelist.h>
00025 #include <kdeversion.h>
00026 
00027 #include "searchmanager.h"
00028 
00029 using namespace KAB;
00030 
00031 SearchManager::SearchManager( KABC::AddressBook *ab,
00032                               QObject *parent, const char *name )
00033   : QObject( parent, name ), mAddressBook( ab )
00034 {
00035 }
00036 
00037 void SearchManager::search( const QString &pattern, const KABC::Field::List &fields, Type type )
00038 {
00039   mPattern = pattern;
00040   mFields = fields;
00041   mType = type;
00042 
00043   KABC::Addressee::List allContacts;
00044   mContacts.clear();
00045 
00046 #if KDE_VERSION >= 319
00047   KABC::AddresseeList list( mAddressBook->allAddressees() );
00048   if ( !fields.isEmpty() )
00049     list.sortByField( fields.first() );
00050 
00051   allContacts = list;
00052 #else
00053   KABC::AddressBook::ConstIterator abIt( mAddressBook->begin() );
00054   const KABC::AddressBook::ConstIterator abEndIt( mAddressBook->end() );
00055   for ( ; abIt != abEndIt; ++abIt )
00056     allContacts.append( *abIt );
00057 #endif
00058 
00059 #ifdef KDEPIM_NEW_DISTRLISTS
00060   // Extract distribution lists from allContacts
00061   mDistributionLists.clear();
00062   KABC::Addressee::List::Iterator rmIt( allContacts.begin() );
00063   const KABC::Addressee::List::Iterator rmEndIt( allContacts.end() );
00064   while ( rmIt != rmEndIt ) {
00065     if ( KPIM::DistributionList::isDistributionList( *rmIt ) ) {
00066       mDistributionLists.append( static_cast<KPIM::DistributionList>( *rmIt ) );
00067       rmIt = allContacts.remove( rmIt );
00068     } else
00069       ++rmIt;
00070   }
00071 #endif
00072 
00073   if ( mPattern.isEmpty() ) { // no pattern, return all
00074     mContacts = allContacts;
00075 
00076     emit contactsUpdated();
00077 
00078     return;
00079   }
00080 
00081   const KABC::Field::List fieldList = !mFields.isEmpty() ? mFields : KABC::Field::allFields();
00082 
00083   KABC::Addressee::List::ConstIterator it( allContacts.begin() );
00084   const KABC::Addressee::List::ConstIterator endIt( allContacts.end() );
00085   for ( ; it != endIt; ++it ) {
00086 #ifdef KDEPIM_NEW_DISTRLISTS
00087     if ( KPIM::DistributionList::isDistributionList( *it ) )
00088       continue;
00089 #endif
00090     KABC::Field::List::ConstIterator fieldIt( fieldList.begin() );
00091     const KABC::Field::List::ConstIterator fieldEndIt( fieldList.end() );
00092     for ( ; fieldIt != fieldEndIt; ++fieldIt ) {
00093 
00094       if ( type == StartsWith && (*fieldIt)->value( *it ).startsWith( pattern, false ) ) {
00095         mContacts.append( *it );
00096         break;
00097       } else if ( type == EndsWith && (*fieldIt)->value( *it ).endsWith( pattern, false ) ) {
00098         mContacts.append( *it );
00099         break;
00100       } else if ( type == Contains && (*fieldIt)->value( *it ).find( pattern, 0, false ) != -1 ) {
00101         mContacts.append( *it );
00102         break;
00103       } else if ( type == Equals && (*fieldIt)->value( *it ).localeAwareCompare( pattern ) == 0 ) {
00104         mContacts.append( *it );
00105         break;
00106       }
00107     }
00108   }
00109 
00110   emit contactsUpdated();
00111 }
00112 
00113 KABC::Addressee::List SearchManager::contacts() const
00114 {
00115   return mContacts;
00116 }
00117 
00118 void SearchManager::reload()
00119 {
00120   search( mPattern, mFields, mType );
00121 }
00122 
00123 #ifdef KDEPIM_NEW_DISTRLISTS
00124 KPIM::DistributionList::List KAB::SearchManager::distributionLists() const
00125 {
00126   return mDistributionLists;
00127 }
00128 
00129 QStringList KAB::SearchManager::distributionListNames() const
00130 {
00131   QStringList lst;
00132   KPIM::DistributionList::List::ConstIterator it( mDistributionLists.begin() );
00133   const KPIM::DistributionList::List::ConstIterator endIt( mDistributionLists.end() );
00134   for ( ; it != endIt; ++it ) {
00135     lst.append( (*it).formattedName() );
00136   }
00137   return lst;
00138 }
00139 #endif
00140 
00141 #include "searchmanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys