00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00098
00099
00100
#if 0
00101
if ( mLimitContactDisplay && mContacts.count() > 100 ) {
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"