kaddressbook

searchmanager.cpp
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19  As a special exception, permission is given to link this program
20  with any edition of TQt, and distribute the resulting executable,
21  without including the source code for TQt in the source distribution.
22 */
23 #include <config.h> // FOR TDEPIM_NEW_DISTRLISTS
24 
25 #include <tdeabc/addresseelist.h>
26 #include <tdeversion.h>
27 
28 #include "searchmanager.h"
29 
30 using namespace KAB;
31 
32 SearchManager::SearchManager( TDEABC::AddressBook *ab,
33  TQObject *parent, const char *name )
34  : TQObject( parent, name ), mAddressBook( ab )
35 {
36 }
37 
38 void SearchManager::search( const TQString &pattern, const TDEABC::Field::List &fields, Type type )
39 {
40  mPattern = pattern;
41  mFields = fields;
42  mType = type;
43 
44  TDEABC::Addressee::List allContacts;
45  mContacts.clear();
46 
47 #if TDE_VERSION >= 319
48  TDEABC::AddresseeList list( mAddressBook->allAddressees() );
49  if ( !fields.isEmpty() )
50  list.sortByField( fields.first() );
51 
52  allContacts = list;
53 #else
54  TDEABC::AddressBook::ConstIterator abIt( mAddressBook->begin() );
55  const TDEABC::AddressBook::ConstIterator abEndIt( mAddressBook->end() );
56  for ( ; abIt != abEndIt; ++abIt )
57  allContacts.append( *abIt );
58 #endif
59 
60 #ifdef TDEPIM_NEW_DISTRLISTS
61  // Extract distribution lists from allContacts
62  mDistributionLists.clear();
63  TDEABC::Addressee::List::Iterator rmIt( allContacts.begin() );
64  const TDEABC::Addressee::List::Iterator rmEndIt( allContacts.end() );
65  while ( rmIt != rmEndIt ) {
66  if ( KPIM::DistributionList::isDistributionList( *rmIt ) ) {
67  mDistributionLists.append( static_cast<KPIM::DistributionList>( *rmIt ) );
68  rmIt = allContacts.remove( rmIt );
69  } else
70  ++rmIt;
71  }
72 
73  typedef KPIM::DistributionList::Entry Entry;
74  if ( !mSelectedDistributionList.isNull() ) {
75  const KPIM::DistributionList dl = KPIM::DistributionList::findByName( mAddressBook, mSelectedDistributionList );
76  if ( !dl.isEmpty() ) {
77  allContacts.clear();
78  const Entry::List entries = dl.entries( mAddressBook );
79  const Entry::List::ConstIterator end = entries.end();
80  for ( Entry::List::ConstIterator it = entries.begin(); it != end; ++it ) {
81  allContacts.append( (*it).addressee );
82  }
83  }
84  }
85 
86 #endif
87 
88  if ( mPattern.isEmpty() ) { // no pattern, return all
89  mContacts = allContacts;
90 
91  emit contactsUpdated();
92 
93  return;
94  }
95 
96  const TDEABC::Field::List fieldList = !mFields.isEmpty() ? mFields : TDEABC::Field::allFields();
97 
98  TDEABC::Addressee::List::ConstIterator it( allContacts.begin() );
99  const TDEABC::Addressee::List::ConstIterator endIt( allContacts.end() );
100  for ( ; it != endIt; ++it ) {
101 #ifdef TDEPIM_NEW_DISTRLISTS
102  if ( KPIM::DistributionList::isDistributionList( *it ) )
103  continue;
104 #endif
105 
106  bool found = false;
107  // search over all fields
108  TDEABC::Field::List::ConstIterator fieldIt( fieldList.begin() );
109  const TDEABC::Field::List::ConstIterator fieldEndIt( fieldList.end() );
110  for ( ; fieldIt != fieldEndIt; ++fieldIt ) {
111 
112  if ( type == StartsWith && (*fieldIt)->value( *it ).startsWith( pattern, false ) ) {
113  mContacts.append( *it );
114  found = true;
115  break;
116  } else if ( type == EndsWith && (*fieldIt)->value( *it ).endsWith( pattern, false ) ) {
117  mContacts.append( *it );
118  found = true;
119  break;
120  } else if ( type == Contains && (*fieldIt)->value( *it ).find( pattern, 0, false ) != -1 ) {
121  mContacts.append( *it );
122  found = true;
123  break;
124  } else if ( type == Equals && (*fieldIt)->value( *it ).localeAwareCompare( pattern ) == 0 ) {
125  mContacts.append( *it );
126  found = true;
127  break;
128  }
129  }
130 
131  if ( !found ) {
132  // search over custom fields
133  const TQStringList customs = (*it).customs();
134 
135  TQStringList::ConstIterator customIt( customs.begin() );
136  const TQStringList::ConstIterator customEndIt( customs.end() );
137  for ( ; customIt != customEndIt; ++customIt ) {
138  int pos = (*customIt).find( ':' );
139  if ( pos != -1 ) {
140  const TQString value = (*customIt).mid( pos + 1 );
141  if ( type == StartsWith && value.startsWith( pattern, false ) ) {
142  mContacts.append( *it );
143  break;
144  } else if ( type == EndsWith && value.endsWith( pattern, false ) ) {
145  mContacts.append( *it );
146  break;
147  } else if ( type == Contains && value.find( pattern, 0, false ) != -1 ) {
148  mContacts.append( *it );
149  break;
150  } else if ( type == Equals && value.localeAwareCompare( pattern ) == 0 ) {
151  mContacts.append( *it );
152  break;
153  }
154  }
155  }
156  }
157  }
158 
159  emit contactsUpdated();
160 }
161 
162 TDEABC::Addressee::List SearchManager::contacts() const
163 {
164  return mContacts;
165 }
166 
167 void SearchManager::reload()
168 {
169  search( mPattern, mFields, mType );
170 }
171 
172 #ifdef TDEPIM_NEW_DISTRLISTS
173 
174 void KAB::SearchManager::setSelectedDistributionList( const TQString &name )
175 {
176  if ( mSelectedDistributionList == name )
177  return;
178  mSelectedDistributionList = name;
179  reload();
180 }
181 
182 KPIM::DistributionList::List KAB::SearchManager::distributionLists() const
183 {
184  return mDistributionLists;
185 }
186 
187 TQStringList KAB::SearchManager::distributionListNames() const
188 {
189  TQStringList lst;
190  KPIM::DistributionList::List::ConstIterator it( mDistributionLists.begin() );
191  const KPIM::DistributionList::List::ConstIterator endIt( mDistributionLists.end() );
192  for ( ; it != endIt; ++it ) {
193  lst.append( (*it).formattedName() );
194  }
195  return lst;
196 }
197 #endif
198 
199 #include "searchmanager.moc"