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 
27 #include "searchmanager.h"
28 
29 using namespace KAB;
30 
31 SearchManager::SearchManager( TDEABC::AddressBook *ab,
32  TQObject *parent, const char *name )
33  : TQObject( parent, name ), mAddressBook( ab )
34 {
35 }
36 
37 void SearchManager::search( const TQString &pattern, const TDEABC::Field::List &fields, Type type )
38 {
39  mPattern = pattern;
40  mFields = fields;
41  mType = type;
42 
43  TDEABC::Addressee::List allContacts;
44  mContacts.clear();
45 
46  TDEABC::AddresseeList list( mAddressBook->allAddressees() );
47  if ( !fields.isEmpty() )
48  list.sortByField( fields.first() );
49 
50  allContacts = list;
51 
52 #ifdef TDEPIM_NEW_DISTRLISTS
53  // Extract distribution lists from allContacts
54  mDistributionLists.clear();
55  TDEABC::Addressee::List::Iterator rmIt( allContacts.begin() );
56  const TDEABC::Addressee::List::Iterator rmEndIt( allContacts.end() );
57  while ( rmIt != rmEndIt ) {
58  if ( KPIM::DistributionList::isDistributionList( *rmIt ) ) {
59  mDistributionLists.append( static_cast<KPIM::DistributionList>( *rmIt ) );
60  rmIt = allContacts.remove( rmIt );
61  } else
62  ++rmIt;
63  }
64 
65  typedef KPIM::DistributionList::Entry Entry;
66  if ( !mSelectedDistributionList.isNull() ) {
67  const KPIM::DistributionList dl = KPIM::DistributionList::findByName( mAddressBook, mSelectedDistributionList );
68  if ( !dl.isEmpty() ) {
69  allContacts.clear();
70  const Entry::List entries = dl.entries( mAddressBook );
71  const Entry::List::ConstIterator end = entries.end();
72  for ( Entry::List::ConstIterator it = entries.begin(); it != end; ++it ) {
73  allContacts.append( (*it).addressee );
74  }
75  }
76  }
77 
78 #endif
79 
80  if ( mPattern.isEmpty() ) { // no pattern, return all
81  mContacts = allContacts;
82 
83  emit contactsUpdated();
84 
85  return;
86  }
87 
88  const TDEABC::Field::List fieldList = !mFields.isEmpty() ? mFields : TDEABC::Field::allFields();
89 
90  TDEABC::Addressee::List::ConstIterator it( allContacts.begin() );
91  const TDEABC::Addressee::List::ConstIterator endIt( allContacts.end() );
92  for ( ; it != endIt; ++it ) {
93 #ifdef TDEPIM_NEW_DISTRLISTS
94  if ( KPIM::DistributionList::isDistributionList( *it ) )
95  continue;
96 #endif
97 
98  bool found = false;
99  // search over all fields
100  TDEABC::Field::List::ConstIterator fieldIt( fieldList.begin() );
101  const TDEABC::Field::List::ConstIterator fieldEndIt( fieldList.end() );
102  for ( ; fieldIt != fieldEndIt; ++fieldIt ) {
103 
104  if ( type == StartsWith && (*fieldIt)->value( *it ).startsWith( pattern, false ) ) {
105  mContacts.append( *it );
106  found = true;
107  break;
108  } else if ( type == EndsWith && (*fieldIt)->value( *it ).endsWith( pattern, false ) ) {
109  mContacts.append( *it );
110  found = true;
111  break;
112  } else if ( type == Contains && (*fieldIt)->value( *it ).find( pattern, 0, false ) != -1 ) {
113  mContacts.append( *it );
114  found = true;
115  break;
116  } else if ( type == Equals && (*fieldIt)->value( *it ).localeAwareCompare( pattern ) == 0 ) {
117  mContacts.append( *it );
118  found = true;
119  break;
120  }
121  }
122 
123  if ( !found ) {
124  // search over custom fields
125  const TQStringList customs = (*it).customs();
126 
127  TQStringList::ConstIterator customIt( customs.begin() );
128  const TQStringList::ConstIterator customEndIt( customs.end() );
129  for ( ; customIt != customEndIt; ++customIt ) {
130  int pos = (*customIt).find( ':' );
131  if ( pos != -1 ) {
132  const TQString value = (*customIt).mid( pos + 1 );
133  if ( type == StartsWith && value.startsWith( pattern, false ) ) {
134  mContacts.append( *it );
135  break;
136  } else if ( type == EndsWith && value.endsWith( pattern, false ) ) {
137  mContacts.append( *it );
138  break;
139  } else if ( type == Contains && value.find( pattern, 0, false ) != -1 ) {
140  mContacts.append( *it );
141  break;
142  } else if ( type == Equals && value.localeAwareCompare( pattern ) == 0 ) {
143  mContacts.append( *it );
144  break;
145  }
146  }
147  }
148  }
149  }
150 
151  emit contactsUpdated();
152 }
153 
154 TDEABC::Addressee::List SearchManager::contacts() const
155 {
156  return mContacts;
157 }
158 
159 void SearchManager::reload()
160 {
161  search( mPattern, mFields, mType );
162 }
163 
164 #ifdef TDEPIM_NEW_DISTRLISTS
165 
166 void KAB::SearchManager::setSelectedDistributionList( const TQString &name )
167 {
168  if ( mSelectedDistributionList == name )
169  return;
170  mSelectedDistributionList = name;
171  reload();
172 }
173 
174 KPIM::DistributionList::List KAB::SearchManager::distributionLists() const
175 {
176  return mDistributionLists;
177 }
178 
179 TQStringList KAB::SearchManager::distributionListNames() const
180 {
181  TQStringList lst;
182  KPIM::DistributionList::List::ConstIterator it( mDistributionLists.begin() );
183  const KPIM::DistributionList::List::ConstIterator endIt( mDistributionLists.end() );
184  for ( ; it != endIt; ++it ) {
185  lst.append( (*it).formattedName() );
186  }
187  return lst;
188 }
189 #endif
190 
191 #include "searchmanager.moc"