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 
00051 #if KDE_VERSION >= 319
00052   KABC::AddresseeList list( mAddressBook->allAddressees() );
00053   if ( field )
00054     list.sortByField( field );
00055 
00056   allContacts = list;
00057 #else
00058   KABC::AddressBook::Iterator abIt;
00059   for ( abIt = mAddressBook->begin(); abIt != mAddressBook->end(); ++abIt )
00060     allContacts.append( *abIt );
00061 #endif
00062 
00063   QStringList::ConstIterator it;
00064   for ( it = mJumpButtonPatterns.begin(); it != mJumpButtonPatterns.end(); ++it )
00065     doSearch( *it, mJumpButtonField, StartsWith, allContacts );
00066 
00067   allContacts = mContacts;
00068   mContacts.clear();
00069 
00070   doSearch( mLastPattern, mLastField, mLastType, allContacts );
00071   emit contactsUpdated();
00072 }
00073 
00074 void SearchManager::setJumpButtonFilter( const QStringList &patterns, KABC::Field *field )
00075 {
00076   mJumpButtonPatterns = patterns;
00077   mJumpButtonField = field;
00078 
00079   search( mLastPattern, mLastField, mLastType );
00080 }
00081 
00082 void SearchManager::reconfigure()
00083 {
00084   KConfig config( "kabcrc", false, false );
00085   config.setGroup( "General" );
00086 
00087   mLimitContactDisplay = config.readBoolEntry( "LimitContactDisplay", true );
00088 
00089   reload();
00090 }
00091 
00092 void SearchManager::doSearch( const QString &pattern, KABC::Field *field, Type type,
00093                               const KABC::Addressee::List &list )
00094 {
00095   if ( pattern.isEmpty() ) {
00096     mContacts = list;
00097 // Don't delete the contacts. There are addressbooks with more than 100 entres
00098 // and there doesn't seem to be a way to get the deleted contacts back with
00099 // another search
00100 #if 0
00101     if ( mLimitContactDisplay && mContacts.count() > 100 ) { // show only 100 contacts
00102       KABC::Addressee::List::Iterator it = mContacts.at( 100 );
00103       while ( it != mContacts.end() )
00104         it = mContacts.remove( it );
00105     }
00106 #endif
00107 
00108     return;
00109   }
00110 
00111   if ( field ) {
00112     KABC::Addressee::List::ConstIterator it;
00113     for ( it = list.begin(); it != list.end(); ++it ) {
00114       if ( type == StartsWith && field->value( *it ).startsWith( pattern, false ) )
00115         mContacts.append( *it );
00116       else if ( type == EndsWith && field->value( *it ).endsWith( pattern, false ) )
00117         mContacts.append( *it );
00118       else if ( type == Contains && field->value( *it ).find( pattern, 0, false ) != -1 )
00119         mContacts.append( *it );
00120       else if ( type == Equals && field->value( *it ).localeAwareCompare( pattern ) == 0 )
00121         mContacts.append( *it );
00122     }
00123   } else {
00124     KABC::Addressee::List::ConstIterator it;
00125     for ( it = list.begin(); it != list.end(); ++it ) {
00126       KABC::Field::List fieldList = KABC::Field::allFields();
00127       KABC::Field::List::ConstIterator fieldIt;
00128       for ( fieldIt = fieldList.begin(); fieldIt != fieldList.end(); ++fieldIt ) {
00129         if ( type == StartsWith && (*fieldIt)->value( *it ).startsWith( pattern, false ) ) {
00130           mContacts.append( *it );
00131           break;
00132         } else if ( type == EndsWith && (*fieldIt)->value( *it ).endsWith( pattern, false ) ) {
00133           mContacts.append( *it );
00134           break;
00135         } else if ( type == Contains && (*fieldIt)->value( *it ).find( pattern, 0, false ) != -1 ) {
00136           mContacts.append( *it );
00137           break;
00138         } else if ( type == Equals && (*fieldIt)->value( *it ).localeAwareCompare( pattern ) == 0 ) {
00139           mContacts.append( *it );
00140           break;
00141         }
00142       }
00143     }
00144   }
00145 }
00146 
00147 KABC::Addressee::List SearchManager::contacts() const
00148 {
00149   return mContacts;
00150 }
00151 
00152 void SearchManager::reload()
00153 {
00154   search( mLastPattern, mLastField, mLastType );
00155 }
00156 
00157 #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 Wed Mar 23 22:42:48 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003