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 
00091     bool found = false;
00092     // search over all fields
00093     KABC::Field::List::ConstIterator fieldIt( fieldList.begin() );
00094     const KABC::Field::List::ConstIterator fieldEndIt( fieldList.end() );
00095     for ( ; fieldIt != fieldEndIt; ++fieldIt ) {
00096 
00097       if ( type == StartsWith && (*fieldIt)->value( *it ).startsWith( pattern, false ) ) {
00098         mContacts.append( *it );
00099         found = true;
00100         break;
00101       } else if ( type == EndsWith && (*fieldIt)->value( *it ).endsWith( pattern, false ) ) {
00102         mContacts.append( *it );
00103         found = true;
00104         break;
00105       } else if ( type == Contains && (*fieldIt)->value( *it ).find( pattern, 0, false ) != -1 ) {
00106         mContacts.append( *it );
00107         found = true;
00108         break;
00109       } else if ( type == Equals && (*fieldIt)->value( *it ).localeAwareCompare( pattern ) == 0 ) {
00110         mContacts.append( *it );
00111         found = true;
00112         break;
00113       }
00114     }
00115 
00116     if ( !found ) {
00117       // search over custom fields
00118       const QStringList customs = (*it).customs();
00119 
00120       QStringList::ConstIterator customIt( customs.begin() );
00121       const QStringList::ConstIterator customEndIt( customs.end() );
00122       for ( ; customIt != customEndIt; ++customIt ) {
00123         int pos = (*customIt).find( ':' );
00124         if ( pos != -1 ) {
00125           const QString value = (*customIt).mid( pos + 1 );
00126           if ( type == StartsWith && value.startsWith( pattern, false ) ) {
00127             mContacts.append( *it );
00128             break;
00129           } else if ( type == EndsWith && value.endsWith( pattern, false ) ) {
00130             mContacts.append( *it );
00131             break;
00132           } else if ( type == Contains && value.find( pattern, 0, false ) != -1 ) {
00133             mContacts.append( *it );
00134             break;
00135           } else if ( type == Equals && value.localeAwareCompare( pattern ) == 0 ) {
00136             mContacts.append( *it );
00137             break;
00138           }
00139         }
00140       }
00141     }
00142   }
00143 
00144   emit contactsUpdated();
00145 }
00146 
00147 KABC::Addressee::List SearchManager::contacts() const
00148 {
00149   return mContacts;
00150 }
00151 
00152 void SearchManager::reload()
00153 {
00154   search( mPattern, mFields, mType );
00155 }
00156 
00157 #ifdef KDEPIM_NEW_DISTRLISTS
00158 KPIM::DistributionList::List KAB::SearchManager::distributionLists() const
00159 {
00160   return mDistributionLists;
00161 }
00162 
00163 QStringList KAB::SearchManager::distributionListNames() const
00164 {
00165   QStringList lst;
00166   KPIM::DistributionList::List::ConstIterator it( mDistributionLists.begin() );
00167   const KPIM::DistributionList::List::ConstIterator endIt( mDistributionLists.end() );
00168   for ( ; it != endIt; ++it ) {
00169     lst.append( (*it).formattedName() );
00170   }
00171   return lst;
00172 }
00173 #endif
00174 
00175 #include "searchmanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys