kaddressbook Library API Documentation

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 ),
00034     mAddressBook( ab ), mLastField( 0 ), mLastType( Contains ),
00035     mJumpButtonField( 0 )
00036 {
00037   mJumpButtonPatterns.append( "" );
00038 
00039   reconfigure();
00040 }
00041 
00042 void SearchManager::search( const QString &pattern, KABC::Field *field, Type type )
00043 {
00044   mLastPattern = pattern;
00045   mLastField = field;
00046   mLastType = type;
00047 
00048   KABC::Addressee::List allContacts;
00049   mContacts.clear();
00050   mDistributionLists.clear();
00051 
00052 #if KDE_VERSION >= 319
00053   KABC::AddresseeList list( mAddressBook->allAddressees() );
00054   if ( field )
00055     list.sortByField( field );
00056 
00057   allContacts = list;
00058 #else
00059   KABC::AddressBook::Iterator abIt;
00060   for ( abIt = mAddressBook->begin(); abIt != mAddressBook->end(); ++abIt )
00061     allContacts.append( *abIt );
00062 #endif
00063 
00064   sortOutDistributionLists( allContacts, mDistributionLists );
00065 
00066   QStringList::ConstIterator it;
00067   for ( it = mJumpButtonPatterns.begin(); it != mJumpButtonPatterns.end(); ++it )
00068     doSearch( *it, mJumpButtonField, StartsWith, allContacts );
00069 
00070   allContacts = mContacts;
00071   mContacts.clear();
00072 
00073   doSearch( mLastPattern, mLastField, mLastType, allContacts );
00074 
00075   // Remove distr. lists from the search results
00076   KPIM::DistributionList::List dummy;
00077   sortOutDistributionLists( mContacts, dummy );
00078 
00079   emit contactsUpdated();
00080 }
00081 
00082 void SearchManager::setJumpButtonFilter( const QStringList &patterns, KABC::Field *field )
00083 {
00084   mJumpButtonPatterns = patterns;
00085   mJumpButtonField = field;
00086 
00087   search( mLastPattern, mLastField, mLastType );
00088 }
00089 
00090 void SearchManager::reconfigure()
00091 {
00092   KConfig config( "kabcrc", false, false );
00093   config.setGroup( "General" );
00094 
00095   mLimitContactDisplay = config.readBoolEntry( "LimitContactDisplay", true );
00096 
00097   reload();
00098 }
00099 
00100 void SearchManager::doSearch( const QString &pattern, KABC::Field *field, Type type,
00101                               const KABC::Addressee::List &list )
00102 {
00103   if ( pattern.isEmpty() ) {
00104     mContacts = list;
00105 // Don't delete the contacts. There are addressbooks with more than 100 entres
00106 // and there doesn't seem to be a way to get the deleted contacts back with
00107 // another search
00108 #if 0
00109     if ( mLimitContactDisplay && mContacts.count() > 100 ) { // show only 100 contacts
00110       KABC::Addressee::List::Iterator it = mContacts.at( 100 );
00111       while ( it != mContacts.end() )
00112         it = mContacts.remove( it );
00113     }
00114 #endif
00115 
00116     return;
00117   }
00118 
00119   if ( field ) {
00120     KABC::Addressee::List::ConstIterator it;
00121     for ( it = list.begin(); it != list.end(); ++it ) {
00122       if ( type == StartsWith && field->value( *it ).startsWith( pattern, false ) )
00123         mContacts.append( *it );
00124       else if ( type == EndsWith && field->value( *it ).endsWith( pattern, false ) )
00125         mContacts.append( *it );
00126       else if ( type == Contains && field->value( *it ).find( pattern, 0, false ) != -1 )
00127         mContacts.append( *it );
00128       else if ( type == Equals && field->value( *it ).localeAwareCompare( pattern ) == 0 )
00129         mContacts.append( *it );
00130     }
00131   } else {
00132     KABC::Addressee::List::ConstIterator it;
00133     for ( it = list.begin(); it != list.end(); ++it ) {
00134       KABC::Field::List fieldList = KABC::Field::allFields();
00135       KABC::Field::List::ConstIterator fieldIt;
00136       for ( fieldIt = fieldList.begin(); fieldIt != fieldList.end(); ++fieldIt ) {
00137         if ( type == StartsWith && (*fieldIt)->value( *it ).startsWith( pattern, false ) ) {
00138           mContacts.append( *it );
00139           break;
00140         } else if ( type == EndsWith && (*fieldIt)->value( *it ).endsWith( pattern, false ) ) {
00141           mContacts.append( *it );
00142           break;
00143         } else if ( type == Contains && (*fieldIt)->value( *it ).find( pattern, 0, false ) != -1 ) {
00144           mContacts.append( *it );
00145           break;
00146         } else if ( type == Equals && (*fieldIt)->value( *it ).localeAwareCompare( pattern ) == 0 ) {
00147           mContacts.append( *it );
00148           break;
00149         }
00150       }
00151     }
00152   }
00153 }
00154 
00155 KABC::Addressee::List SearchManager::contacts() const
00156 {
00157   return mContacts;
00158 }
00159 
00160 void SearchManager::reload()
00161 {
00162   search( mLastPattern, mLastField, mLastType );
00163 }
00164 
00165 void SearchManager::sortOutDistributionLists( KABC::Addressee::List& list,
00166                                               KPIM::DistributionList::List& distrlists )
00167 {
00168   KABC::Addressee::List::Iterator it = list.begin();
00169   while ( it != list.end() ) {
00170     //kdDebug() << (*it).formattedName() << "   distrlist=" << KPIM::DistributionList::isDistributionList( *it ) << endl;
00171     if ( KPIM::DistributionList::isDistributionList( *it ) ) {
00172       distrlists.append( static_cast<KPIM::DistributionList>( *it ) );
00173       it = list.remove( it );
00174     } else
00175       ++it;
00176   }
00177 }
00178 
00179 
00180 KPIM::DistributionList::List KAB::SearchManager::distributionLists() const
00181 {
00182   return mDistributionLists;
00183 }
00184 
00185 QStringList KAB::SearchManager::distributionListNames() const
00186 {
00187   QStringList lst;
00188   KPIM::DistributionList::List::ConstIterator it;
00189   for ( it = mDistributionLists.begin(); it != mDistributionLists.end(); ++it ) {
00190     lst.append( (*it).formattedName() );
00191   }
00192   return lst;
00193 }
00194 
00195 #include "searchmanager.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 4 14:41:40 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003