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 TQt, and distribute the resulting executable,
00021     without including the source code for TQt in the source distribution.
00022 */
00023 #include <config.h> // FOR KDEPIM_NEW_DISTRLISTS
00024 
00025 #include <kabc/addresseelist.h>
00026 #include <kdeversion.h>
00027 
00028 #include "searchmanager.h"
00029 
00030 using namespace KAB;
00031 
00032 SearchManager::SearchManager( KABC::AddressBook *ab,
00033                               TQObject *parent, const char *name )
00034   : TQObject( parent, name ), mAddressBook( ab )
00035 {
00036 }
00037 
00038 void SearchManager::search( const TQString &pattern, const KABC::Field::List &fields, Type type )
00039 {
00040   mPattern = pattern;
00041   mFields = fields;
00042   mType = type;
00043 
00044   KABC::Addressee::List allContacts;
00045   mContacts.clear();
00046 
00047 #if KDE_VERSION >= 319
00048   KABC::AddresseeList list( mAddressBook->allAddressees() );
00049   if ( !fields.isEmpty() )
00050     list.sortByField( fields.first() );
00051 
00052   allContacts = list;
00053 #else
00054   KABC::AddressBook::ConstIterator abIt( mAddressBook->begin() );
00055   const KABC::AddressBook::ConstIterator abEndIt( mAddressBook->end() );
00056   for ( ; abIt != abEndIt; ++abIt )
00057     allContacts.append( *abIt );
00058 #endif
00059 
00060 #ifdef KDEPIM_NEW_DISTRLISTS
00061   // Extract distribution lists from allContacts
00062   mDistributionLists.clear();
00063   KABC::Addressee::List::Iterator rmIt( allContacts.begin() );
00064   const KABC::Addressee::List::Iterator rmEndIt( allContacts.end() );
00065   while ( rmIt != rmEndIt ) {
00066     if ( KPIM::DistributionList::isDistributionList( *rmIt ) ) {
00067       mDistributionLists.append( static_cast<KPIM::DistributionList>( *rmIt ) );
00068       rmIt = allContacts.remove( rmIt );
00069     } else
00070       ++rmIt;
00071   }
00072 
00073   typedef KPIM::DistributionList::Entry Entry;
00074   if ( !mSelectedDistributionList.isNull() ) {
00075     const KPIM::DistributionList dl = KPIM::DistributionList::findByName( mAddressBook, mSelectedDistributionList );
00076     if ( !dl.isEmpty() ) {
00077       allContacts.clear();
00078       const Entry::List entries = dl.entries( mAddressBook );
00079       const Entry::List::ConstIterator end = entries.end();
00080       for ( Entry::List::ConstIterator it = entries.begin(); it != end; ++it ) {
00081         allContacts.append( (*it).addressee ); 
00082       }
00083     }
00084   }
00085 
00086 #endif
00087 
00088   if ( mPattern.isEmpty() ) { // no pattern, return all
00089     mContacts = allContacts;
00090 
00091     emit contactsUpdated();
00092 
00093     return;
00094   }
00095 
00096   const KABC::Field::List fieldList = !mFields.isEmpty() ? mFields : KABC::Field::allFields();
00097 
00098   KABC::Addressee::List::ConstIterator it( allContacts.begin() );
00099   const KABC::Addressee::List::ConstIterator endIt( allContacts.end() );
00100   for ( ; it != endIt; ++it ) {
00101 #ifdef KDEPIM_NEW_DISTRLISTS
00102     if ( KPIM::DistributionList::isDistributionList( *it ) )
00103       continue;
00104 #endif
00105 
00106     bool found = false;
00107     // search over all fields
00108     KABC::Field::List::ConstIterator fieldIt( fieldList.begin() );
00109     const KABC::Field::List::ConstIterator fieldEndIt( fieldList.end() );
00110     for ( ; fieldIt != fieldEndIt; ++fieldIt ) {
00111 
00112       if ( type == StartsWith && (*fieldIt)->value( *it ).startsWith( pattern, false ) ) {
00113         mContacts.append( *it );
00114         found = true;
00115         break;
00116       } else if ( type == EndsWith && (*fieldIt)->value( *it ).endsWith( pattern, false ) ) {
00117         mContacts.append( *it );
00118         found = true;
00119         break;
00120       } else if ( type == Contains && (*fieldIt)->value( *it ).find( pattern, 0, false ) != -1 ) {
00121         mContacts.append( *it );
00122         found = true;
00123         break;
00124       } else if ( type == Equals && (*fieldIt)->value( *it ).localeAwareCompare( pattern ) == 0 ) {
00125         mContacts.append( *it );
00126         found = true;
00127         break;
00128       }
00129     }
00130 
00131     if ( !found ) {
00132       // search over custom fields
00133       const TQStringList customs = (*it).customs();
00134 
00135       TQStringList::ConstIterator customIt( customs.begin() );
00136       const TQStringList::ConstIterator customEndIt( customs.end() );
00137       for ( ; customIt != customEndIt; ++customIt ) {
00138         int pos = (*customIt).find( ':' );
00139         if ( pos != -1 ) {
00140           const TQString value = (*customIt).mid( pos + 1 );
00141           if ( type == StartsWith && value.startsWith( pattern, false ) ) {
00142             mContacts.append( *it );
00143             break;
00144           } else if ( type == EndsWith && value.endsWith( pattern, false ) ) {
00145             mContacts.append( *it );
00146             break;
00147           } else if ( type == Contains && value.find( pattern, 0, false ) != -1 ) {
00148             mContacts.append( *it );
00149             break;
00150           } else if ( type == Equals && value.localeAwareCompare( pattern ) == 0 ) {
00151             mContacts.append( *it );
00152             break;
00153           }
00154         }
00155       }
00156     }
00157   }
00158 
00159   emit contactsUpdated();
00160 }
00161 
00162 KABC::Addressee::List SearchManager::contacts() const
00163 {
00164   return mContacts;
00165 }
00166 
00167 void SearchManager::reload()
00168 {
00169   search( mPattern, mFields, mType );
00170 }
00171 
00172 #ifdef KDEPIM_NEW_DISTRLISTS
00173 
00174 void KAB::SearchManager::setSelectedDistributionList( const TQString &name )
00175 {
00176   if ( mSelectedDistributionList == name )
00177     return;     
00178   mSelectedDistributionList = name;
00179   reload();
00180 }
00181 
00182 KPIM::DistributionList::List KAB::SearchManager::distributionLists() const
00183 {
00184   return mDistributionLists;
00185 }
00186 
00187 TQStringList KAB::SearchManager::distributionListNames() const
00188 {
00189   TQStringList lst;
00190   KPIM::DistributionList::List::ConstIterator it( mDistributionLists.begin() );
00191   const KPIM::DistributionList::List::ConstIterator endIt( mDistributionLists.end() );
00192   for ( ; it != endIt; ++it ) {
00193     lst.append( (*it).formattedName() );
00194   }
00195   return lst;
00196 }
00197 #endif
00198 
00199 #include "searchmanager.moc"