kaddressbook
searchmanager.cpp00001
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 ), 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
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
00072 typedef KPIM::DistributionList::Entry Entry;
00073 if ( !mSelectedDistributionList.isNull() ) {
00074 const KPIM::DistributionList dl = KPIM::DistributionList::findByName( mAddressBook, mSelectedDistributionList );
00075 if ( !dl.isEmpty() ) {
00076 allContacts.clear();
00077 const Entry::List entries = dl.entries( mAddressBook );
00078 const Entry::List::ConstIterator end = entries.end();
00079 for ( Entry::List::ConstIterator it = entries.begin(); it != end; ++it ) {
00080 allContacts.append( (*it).addressee );
00081 }
00082 }
00083 }
00084
00085 #endif
00086
00087 if ( mPattern.isEmpty() ) {
00088 mContacts = allContacts;
00089
00090 emit contactsUpdated();
00091
00092 return;
00093 }
00094
00095 const KABC::Field::List fieldList = !mFields.isEmpty() ? mFields : KABC::Field::allFields();
00096
00097 KABC::Addressee::List::ConstIterator it( allContacts.begin() );
00098 const KABC::Addressee::List::ConstIterator endIt( allContacts.end() );
00099 for ( ; it != endIt; ++it ) {
00100 #ifdef KDEPIM_NEW_DISTRLISTS
00101 if ( KPIM::DistributionList::isDistributionList( *it ) )
00102 continue;
00103 #endif
00104
00105 bool found = false;
00106
00107 KABC::Field::List::ConstIterator fieldIt( fieldList.begin() );
00108 const KABC::Field::List::ConstIterator fieldEndIt( fieldList.end() );
00109 for ( ; fieldIt != fieldEndIt; ++fieldIt ) {
00110
00111 if ( type == StartsWith && (*fieldIt)->value( *it ).startsWith( pattern, false ) ) {
00112 mContacts.append( *it );
00113 found = true;
00114 break;
00115 } else if ( type == EndsWith && (*fieldIt)->value( *it ).endsWith( pattern, false ) ) {
00116 mContacts.append( *it );
00117 found = true;
00118 break;
00119 } else if ( type == Contains && (*fieldIt)->value( *it ).find( pattern, 0, false ) != -1 ) {
00120 mContacts.append( *it );
00121 found = true;
00122 break;
00123 } else if ( type == Equals && (*fieldIt)->value( *it ).localeAwareCompare( pattern ) == 0 ) {
00124 mContacts.append( *it );
00125 found = true;
00126 break;
00127 }
00128 }
00129
00130 if ( !found ) {
00131
00132 const QStringList customs = (*it).customs();
00133
00134 QStringList::ConstIterator customIt( customs.begin() );
00135 const QStringList::ConstIterator customEndIt( customs.end() );
00136 for ( ; customIt != customEndIt; ++customIt ) {
00137 int pos = (*customIt).find( ':' );
00138 if ( pos != -1 ) {
00139 const QString value = (*customIt).mid( pos + 1 );
00140 if ( type == StartsWith && value.startsWith( pattern, false ) ) {
00141 mContacts.append( *it );
00142 break;
00143 } else if ( type == EndsWith && value.endsWith( pattern, false ) ) {
00144 mContacts.append( *it );
00145 break;
00146 } else if ( type == Contains && value.find( pattern, 0, false ) != -1 ) {
00147 mContacts.append( *it );
00148 break;
00149 } else if ( type == Equals && value.localeAwareCompare( pattern ) == 0 ) {
00150 mContacts.append( *it );
00151 break;
00152 }
00153 }
00154 }
00155 }
00156 }
00157
00158 emit contactsUpdated();
00159 }
00160
00161 KABC::Addressee::List SearchManager::contacts() const
00162 {
00163 return mContacts;
00164 }
00165
00166 void SearchManager::reload()
00167 {
00168 search( mPattern, mFields, mType );
00169 }
00170
00171 #ifdef KDEPIM_NEW_DISTRLISTS
00172
00173 void KAB::SearchManager::setSelectedDistributionList( const QString &name )
00174 {
00175 if ( mSelectedDistributionList == name )
00176 return;
00177 mSelectedDistributionList = name;
00178 reload();
00179 }
00180
00181 KPIM::DistributionList::List KAB::SearchManager::distributionLists() const
00182 {
00183 return mDistributionLists;
00184 }
00185
00186 QStringList KAB::SearchManager::distributionListNames() const
00187 {
00188 QStringList lst;
00189 KPIM::DistributionList::List::ConstIterator it( mDistributionLists.begin() );
00190 const KPIM::DistributionList::List::ConstIterator endIt( mDistributionLists.end() );
00191 for ( ; it != endIt; ++it ) {
00192 lst.append( (*it).formattedName() );
00193 }
00194 return lst;
00195 }
00196 #endif
00197
00198 #include "searchmanager.moc"
|