summaryrefslogtreecommitdiffabout
path: root/kabc/addressbook.cpp
Unidiff
Diffstat (limited to 'kabc/addressbook.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/addressbook.cpp101
1 files changed, 99 insertions, 2 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp
index 47d298a..70eda1b 100644
--- a/kabc/addressbook.cpp
+++ b/kabc/addressbook.cpp
@@ -1,646 +1,743 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28/*US 28/*US
29 29
30#include <qfile.h> 30#include <qfile.h>
31#include <qregexp.h> 31#include <qregexp.h>
32#include <qtimer.h> 32#include <qtimer.h>
33 33
34#include <kapplication.h> 34#include <kapplication.h>
35#include <kinstance.h> 35#include <kinstance.h>
36#include <kstandarddirs.h> 36#include <kstandarddirs.h>
37 37
38#include "errorhandler.h" 38#include "errorhandler.h"
39*/ 39*/
40#include <qptrlist.h> 40#include <qptrlist.h>
41 41
42#include <kglobal.h> 42#include <kglobal.h>
43#include <klocale.h> 43#include <klocale.h>
44#include <kdebug.h> 44#include <kdebug.h>
45#include "addressbook.h" 45#include "addressbook.h"
46#include "resource.h" 46#include "resource.h"
47 47
48//US #include "addressbook.moc" 48//US #include "addressbook.moc"
49 49
50using namespace KABC; 50using namespace KABC;
51 51
52struct AddressBook::AddressBookData 52struct AddressBook::AddressBookData
53{ 53{
54 Addressee::List mAddressees; 54 Addressee::List mAddressees;
55 Addressee::List mRemovedAddressees; 55 Addressee::List mRemovedAddressees;
56 Field::List mAllFields; 56 Field::List mAllFields;
57 KConfig *mConfig; 57 KConfig *mConfig;
58 KRES::Manager<Resource> *mManager; 58 KRES::Manager<Resource> *mManager;
59//US ErrorHandler *mErrorHandler; 59//US ErrorHandler *mErrorHandler;
60}; 60};
61 61
62struct AddressBook::Iterator::IteratorData 62struct AddressBook::Iterator::IteratorData
63{ 63{
64 Addressee::List::Iterator mIt; 64 Addressee::List::Iterator mIt;
65}; 65};
66 66
67struct AddressBook::ConstIterator::ConstIteratorData 67struct AddressBook::ConstIterator::ConstIteratorData
68{ 68{
69 Addressee::List::ConstIterator mIt; 69 Addressee::List::ConstIterator mIt;
70}; 70};
71 71
72AddressBook::Iterator::Iterator() 72AddressBook::Iterator::Iterator()
73{ 73{
74 d = new IteratorData; 74 d = new IteratorData;
75} 75}
76 76
77AddressBook::Iterator::Iterator( const AddressBook::Iterator &i ) 77AddressBook::Iterator::Iterator( const AddressBook::Iterator &i )
78{ 78{
79 d = new IteratorData; 79 d = new IteratorData;
80 d->mIt = i.d->mIt; 80 d->mIt = i.d->mIt;
81} 81}
82 82
83AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i ) 83AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i )
84{ 84{
85 if( this == &i ) return *this; // guard against self assignment 85 if( this == &i ) return *this; // guard against self assignment
86 delete d; // delete the old data the Iterator was completely constructed before 86 delete d; // delete the old data the Iterator was completely constructed before
87 d = new IteratorData; 87 d = new IteratorData;
88 d->mIt = i.d->mIt; 88 d->mIt = i.d->mIt;
89 return *this; 89 return *this;
90} 90}
91 91
92AddressBook::Iterator::~Iterator() 92AddressBook::Iterator::~Iterator()
93{ 93{
94 delete d; 94 delete d;
95} 95}
96 96
97const Addressee &AddressBook::Iterator::operator*() const 97const Addressee &AddressBook::Iterator::operator*() const
98{ 98{
99 return *(d->mIt); 99 return *(d->mIt);
100} 100}
101 101
102Addressee &AddressBook::Iterator::operator*() 102Addressee &AddressBook::Iterator::operator*()
103{ 103{
104 return *(d->mIt); 104 return *(d->mIt);
105} 105}
106 106
107Addressee *AddressBook::Iterator::operator->() 107Addressee *AddressBook::Iterator::operator->()
108{ 108{
109 return &(*(d->mIt)); 109 return &(*(d->mIt));
110} 110}
111 111
112AddressBook::Iterator &AddressBook::Iterator::operator++() 112AddressBook::Iterator &AddressBook::Iterator::operator++()
113{ 113{
114 (d->mIt)++; 114 (d->mIt)++;
115 return *this; 115 return *this;
116} 116}
117 117
118AddressBook::Iterator &AddressBook::Iterator::operator++(int) 118AddressBook::Iterator &AddressBook::Iterator::operator++(int)
119{ 119{
120 (d->mIt)++; 120 (d->mIt)++;
121 return *this; 121 return *this;
122} 122}
123 123
124AddressBook::Iterator &AddressBook::Iterator::operator--() 124AddressBook::Iterator &AddressBook::Iterator::operator--()
125{ 125{
126 (d->mIt)--; 126 (d->mIt)--;
127 return *this; 127 return *this;
128} 128}
129 129
130AddressBook::Iterator &AddressBook::Iterator::operator--(int) 130AddressBook::Iterator &AddressBook::Iterator::operator--(int)
131{ 131{
132 (d->mIt)--; 132 (d->mIt)--;
133 return *this; 133 return *this;
134} 134}
135 135
136bool AddressBook::Iterator::operator==( const Iterator &it ) 136bool AddressBook::Iterator::operator==( const Iterator &it )
137{ 137{
138 return ( d->mIt == it.d->mIt ); 138 return ( d->mIt == it.d->mIt );
139} 139}
140 140
141bool AddressBook::Iterator::operator!=( const Iterator &it ) 141bool AddressBook::Iterator::operator!=( const Iterator &it )
142{ 142{
143 return ( d->mIt != it.d->mIt ); 143 return ( d->mIt != it.d->mIt );
144} 144}
145 145
146 146
147AddressBook::ConstIterator::ConstIterator() 147AddressBook::ConstIterator::ConstIterator()
148{ 148{
149 d = new ConstIteratorData; 149 d = new ConstIteratorData;
150} 150}
151 151
152AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i ) 152AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i )
153{ 153{
154 d = new ConstIteratorData; 154 d = new ConstIteratorData;
155 d->mIt = i.d->mIt; 155 d->mIt = i.d->mIt;
156} 156}
157 157
158AddressBook::ConstIterator &AddressBook::ConstIterator::operator=( const AddressBook::ConstIterator &i ) 158AddressBook::ConstIterator &AddressBook::ConstIterator::operator=( const AddressBook::ConstIterator &i )
159{ 159{
160 if( this == &i ) return *this; // guard for self assignment 160 if( this == &i ) return *this; // guard for self assignment
161 delete d; // delete the old data because the Iterator was really constructed before 161 delete d; // delete the old data because the Iterator was really constructed before
162 d = new ConstIteratorData; 162 d = new ConstIteratorData;
163 d->mIt = i.d->mIt; 163 d->mIt = i.d->mIt;
164 return *this; 164 return *this;
165} 165}
166 166
167AddressBook::ConstIterator::~ConstIterator() 167AddressBook::ConstIterator::~ConstIterator()
168{ 168{
169 delete d; 169 delete d;
170} 170}
171 171
172const Addressee &AddressBook::ConstIterator::operator*() const 172const Addressee &AddressBook::ConstIterator::operator*() const
173{ 173{
174 return *(d->mIt); 174 return *(d->mIt);
175} 175}
176 176
177const Addressee* AddressBook::ConstIterator::operator->() const 177const Addressee* AddressBook::ConstIterator::operator->() const
178{ 178{
179 return &(*(d->mIt)); 179 return &(*(d->mIt));
180} 180}
181 181
182AddressBook::ConstIterator &AddressBook::ConstIterator::operator++() 182AddressBook::ConstIterator &AddressBook::ConstIterator::operator++()
183{ 183{
184 (d->mIt)++; 184 (d->mIt)++;
185 return *this; 185 return *this;
186} 186}
187 187
188AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int) 188AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int)
189{ 189{
190 (d->mIt)++; 190 (d->mIt)++;
191 return *this; 191 return *this;
192} 192}
193 193
194AddressBook::ConstIterator &AddressBook::ConstIterator::operator--() 194AddressBook::ConstIterator &AddressBook::ConstIterator::operator--()
195{ 195{
196 (d->mIt)--; 196 (d->mIt)--;
197 return *this; 197 return *this;
198} 198}
199 199
200AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int) 200AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int)
201{ 201{
202 (d->mIt)--; 202 (d->mIt)--;
203 return *this; 203 return *this;
204} 204}
205 205
206bool AddressBook::ConstIterator::operator==( const ConstIterator &it ) 206bool AddressBook::ConstIterator::operator==( const ConstIterator &it )
207{ 207{
208 return ( d->mIt == it.d->mIt ); 208 return ( d->mIt == it.d->mIt );
209} 209}
210 210
211bool AddressBook::ConstIterator::operator!=( const ConstIterator &it ) 211bool AddressBook::ConstIterator::operator!=( const ConstIterator &it )
212{ 212{
213 return ( d->mIt != it.d->mIt ); 213 return ( d->mIt != it.d->mIt );
214} 214}
215 215
216 216
217AddressBook::AddressBook() 217AddressBook::AddressBook()
218{ 218{
219 init(0, "contact"); 219 init(0, "contact");
220} 220}
221 221
222AddressBook::AddressBook( const QString &config ) 222AddressBook::AddressBook( const QString &config )
223{ 223{
224 init(config, "contact"); 224 init(config, "contact");
225} 225}
226 226
227AddressBook::AddressBook( const QString &config, const QString &family ) 227AddressBook::AddressBook( const QString &config, const QString &family )
228{ 228{
229 init(config, family); 229 init(config, family);
230 230
231} 231}
232 232
233// the default family is "contact" 233// the default family is "contact"
234void AddressBook::init(const QString &config, const QString &family ) 234void AddressBook::init(const QString &config, const QString &family )
235{ 235{
236 d = new AddressBookData; 236 d = new AddressBookData;
237 QString fami = family;
238 qDebug("new ab ");
237 if (config != 0) { 239 if (config != 0) {
240 qDebug("config != 0 ");
241 if ( family == "syncContact" ) {
242 qDebug("creating sync config ");
243 fami = "contact";
244 KConfig* con = new KConfig( locateLocal("config", "syncContactrc") );
245 con->setGroup( "General" );
246 con->writeEntry( "ResourceKeys", QString("sync") );
247 con->writeEntry( "Standard", QString("sync") );
248 con->setGroup( "Resource_sync" );
249 con->writeEntry( "FileFormat", QString("vcard") );
250 con->writeEntry( "FileName", config );
251 con->writeEntry( "ResourceIdentifier", QString("sync") );
252 con->writeEntry( "ResourceName", QString("sync_res") );
253 con->writeEntry( "ResourceType", QString("file") );
254 //con->sync();
255 d->mConfig = con;
256 }
257 else
238 d->mConfig = new KConfig( locateLocal("config", config) ); 258 d->mConfig = new KConfig( locateLocal("config", config) );
239// qDebug("AddressBook::init 1 config=%s",config.latin1() ); 259// qDebug("AddressBook::init 1 config=%s",config.latin1() );
240 } 260 }
241 else { 261 else {
242 d->mConfig = 0; 262 d->mConfig = 0;
243// qDebug("AddressBook::init 1 config=0"); 263// qDebug("AddressBook::init 1 config=0");
244 } 264 }
245 265
246//US d->mErrorHandler = 0; 266//US d->mErrorHandler = 0;
247 d->mManager = new KRES::Manager<Resource>( family, false ); 267 d->mManager = new KRES::Manager<Resource>( fami, false );
248 d->mManager->readConfig( d->mConfig ); 268 d->mManager->readConfig( d->mConfig );
269 if ( family == "syncContact" ) {
270 KRES::Manager<Resource> *manager = d->mManager;
271 KRES::Manager<Resource>::ActiveIterator it;
272 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
273 (*it)->setAddressBook( this );
274 if ( !(*it)->open() )
275 error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) );
276 }
277 Resource *res = standardResource();
278 if ( !res ) {
279 qDebug("ERROR: no standard resource");
280 res = manager->createResource( "file" );
281 if ( res )
282 {
283 addResource( res );
284 }
285 else
286 qDebug(" No resource available!!!");
287 }
288 setStandardResource( res );
289 manager->writeConfig();
290 }
291 addCustomField( i18n( "Department" ), KABC::Field::Organization,
292 "X-Department", "KADDRESSBOOK" );
293 addCustomField( i18n( "Profession" ), KABC::Field::Organization,
294 "X-Profession", "KADDRESSBOOK" );
295 addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization,
296 "X-AssistantsName", "KADDRESSBOOK" );
297 addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization,
298 "X-ManagersName", "KADDRESSBOOK" );
299 addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal,
300 "X-SpousesName", "KADDRESSBOOK" );
301 addCustomField( i18n( "Office" ), KABC::Field::Personal,
302 "X-Office", "KADDRESSBOOK" );
303 addCustomField( i18n( "IM Address" ), KABC::Field::Personal,
304 "X-IMAddress", "KADDRESSBOOK" );
305 addCustomField( i18n( "Anniversary" ), KABC::Field::Personal,
306 "X-Anniversary", "KADDRESSBOOK" );
307
308 //US added this field to become compatible with Opie/qtopia addressbook
309 // values can be "female" or "male" or "". An empty field represents undefined.
310 addCustomField( i18n( "Gender" ), KABC::Field::Personal,
311 "X-Gender", "KADDRESSBOOK" );
312 addCustomField( i18n( "Children" ), KABC::Field::Personal,
313 "X-Children", "KADDRESSBOOK" );
314 addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal,
315 "X-FreeBusyUrl", "KADDRESSBOOK" );
316 addCustomField( i18n( "ExternalID" ), KABC::Field::Personal,
317 "X-ExternalID", "KADDRESSBOOK" );
249} 318}
250 319
251AddressBook::~AddressBook() 320AddressBook::~AddressBook()
252{ 321{
253 delete d->mConfig; d->mConfig = 0; 322 delete d->mConfig; d->mConfig = 0;
254 delete d->mManager; d->mManager = 0; 323 delete d->mManager; d->mManager = 0;
255//US delete d->mErrorHandler; d->mErrorHandler = 0; 324//US delete d->mErrorHandler; d->mErrorHandler = 0;
256 delete d; d = 0; 325 delete d; d = 0;
257} 326}
258 327
259bool AddressBook::load() 328bool AddressBook::load()
260{ 329{
261 330
262 331
263 clear(); 332 clear();
264 333
265 KRES::Manager<Resource>::ActiveIterator it; 334 KRES::Manager<Resource>::ActiveIterator it;
266 bool ok = true; 335 bool ok = true;
267 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 336 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
268 if ( !(*it)->load() ) { 337 if ( !(*it)->load() ) {
269 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); 338 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) );
270 ok = false; 339 ok = false;
271 } 340 }
272 341
273 // mark all addressees as unchanged 342 // mark all addressees as unchanged
274 Addressee::List::Iterator addrIt; 343 Addressee::List::Iterator addrIt;
275 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) 344 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt )
276 (*addrIt).setChanged( false ); 345 (*addrIt).setChanged( false );
277 346
278 return ok; 347 return ok;
279} 348}
280 349
281bool AddressBook::save( Ticket *ticket ) 350bool AddressBook::save( Ticket *ticket )
282{ 351{
283 kdDebug(5700) << "AddressBook::save()"<< endl; 352 kdDebug(5700) << "AddressBook::save()"<< endl;
284 353
285 if ( ticket->resource() ) { 354 if ( ticket->resource() ) {
286 deleteRemovedAddressees(); 355 deleteRemovedAddressees();
287
288 return ticket->resource()->save( ticket ); 356 return ticket->resource()->save( ticket );
289 } 357 }
290 358
291 return false; 359 return false;
292} 360}
361bool AddressBook::saveAB()
362{
363 bool ok = true;
364
365 deleteRemovedAddressees();
366
367 KRES::Manager<Resource>::ActiveIterator it;
368 KRES::Manager<Resource> *manager = d->mManager;
369 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
370 if ( !(*it)->readOnly() && (*it)->isOpen() ) {
371 Ticket *ticket = requestSaveTicket( *it );
372// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() );
373 if ( !ticket ) {
374 error( i18n( "Unable to save to resource '%1'. It is locked." )
375 .arg( (*it)->resourceName() ) );
376 return false;
377 }
378
379 //if ( !save( ticket ) )
380 if ( ticket->resource() ) {
381 if ( ! ticket->resource()->save( ticket ) )
382 ok = false;
383 } else
384 ok = false;
385
386 }
387 }
388 return ok;
389}
293 390
294AddressBook::Iterator AddressBook::begin() 391AddressBook::Iterator AddressBook::begin()
295{ 392{
296 Iterator it = Iterator(); 393 Iterator it = Iterator();
297 it.d->mIt = d->mAddressees.begin(); 394 it.d->mIt = d->mAddressees.begin();
298 return it; 395 return it;
299} 396}
300 397
301AddressBook::ConstIterator AddressBook::begin() const 398AddressBook::ConstIterator AddressBook::begin() const
302{ 399{
303 ConstIterator it = ConstIterator(); 400 ConstIterator it = ConstIterator();
304 it.d->mIt = d->mAddressees.begin(); 401 it.d->mIt = d->mAddressees.begin();
305 return it; 402 return it;
306} 403}
307 404
308AddressBook::Iterator AddressBook::end() 405AddressBook::Iterator AddressBook::end()
309{ 406{
310 Iterator it = Iterator(); 407 Iterator it = Iterator();
311 it.d->mIt = d->mAddressees.end(); 408 it.d->mIt = d->mAddressees.end();
312 return it; 409 return it;
313} 410}
314 411
315AddressBook::ConstIterator AddressBook::end() const 412AddressBook::ConstIterator AddressBook::end() const
316{ 413{
317 ConstIterator it = ConstIterator(); 414 ConstIterator it = ConstIterator();
318 it.d->mIt = d->mAddressees.end(); 415 it.d->mIt = d->mAddressees.end();
319 return it; 416 return it;
320} 417}
321 418
322void AddressBook::clear() 419void AddressBook::clear()
323{ 420{
324 d->mAddressees.clear(); 421 d->mAddressees.clear();
325} 422}
326 423
327Ticket *AddressBook::requestSaveTicket( Resource *resource ) 424Ticket *AddressBook::requestSaveTicket( Resource *resource )
328{ 425{
329 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; 426 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl;
330 427
331 if ( !resource ) 428 if ( !resource )
332 { 429 {
333 qDebug("AddressBook::requestSaveTicket no resource" ); 430 qDebug("AddressBook::requestSaveTicket no resource" );
334 resource = standardResource(); 431 resource = standardResource();
335 } 432 }
336 433
337 KRES::Manager<Resource>::ActiveIterator it; 434 KRES::Manager<Resource>::ActiveIterator it;
338 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 435 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
339 if ( (*it) == resource ) { 436 if ( (*it) == resource ) {
340 if ( (*it)->readOnly() || !(*it)->isOpen() ) 437 if ( (*it)->readOnly() || !(*it)->isOpen() )
341 return 0; 438 return 0;
342 else 439 else
343 return (*it)->requestSaveTicket(); 440 return (*it)->requestSaveTicket();
344 } 441 }
345 } 442 }
346 443
347 return 0; 444 return 0;
348} 445}
349 446
350void AddressBook::insertAddressee( const Addressee &a ) 447void AddressBook::insertAddressee( const Addressee &a )
351{ 448{
352 Addressee::List::Iterator it; 449 Addressee::List::Iterator it;
353 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { 450 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) {
354 if ( a.uid() == (*it).uid() ) { 451 if ( a.uid() == (*it).uid() ) {
355 bool changed = false; 452 bool changed = false;
356 Addressee addr = a; 453 Addressee addr = a;
357 if ( addr != (*it) ) 454 if ( addr != (*it) )
358 changed = true; 455 changed = true;
359 456
360 (*it) = a; 457 (*it) = a;
361 if ( (*it).resource() == 0 ) 458 if ( (*it).resource() == 0 )
362 (*it).setResource( standardResource() ); 459 (*it).setResource( standardResource() );
363 460
364 if ( changed ) { 461 if ( changed ) {
365 (*it).setRevision( QDateTime::currentDateTime() ); 462 (*it).setRevision( QDateTime::currentDateTime() );
366 (*it).setChanged( true ); 463 (*it).setChanged( true );
367 } 464 }
368 465
369 return; 466 return;
370 } 467 }
371 } 468 }
372 d->mAddressees.append( a ); 469 d->mAddressees.append( a );
373 Addressee& addr = d->mAddressees.last(); 470 Addressee& addr = d->mAddressees.last();
374 if ( addr.resource() == 0 ) 471 if ( addr.resource() == 0 )
375 addr.setResource( standardResource() ); 472 addr.setResource( standardResource() );
376 473
377 addr.setChanged( true ); 474 addr.setChanged( true );
378} 475}
379 476
380void AddressBook::removeAddressee( const Addressee &a ) 477void AddressBook::removeAddressee( const Addressee &a )
381{ 478{
382 Iterator it; 479 Iterator it;
383 for ( it = begin(); it != end(); ++it ) { 480 for ( it = begin(); it != end(); ++it ) {
384 if ( a.uid() == (*it).uid() ) { 481 if ( a.uid() == (*it).uid() ) {
385 removeAddressee( it ); 482 removeAddressee( it );
386 return; 483 return;
387 } 484 }
388 } 485 }
389} 486}
390 487
391void AddressBook::removeAddressee( const Iterator &it ) 488void AddressBook::removeAddressee( const Iterator &it )
392{ 489{
393 d->mRemovedAddressees.append( (*it) ); 490 d->mRemovedAddressees.append( (*it) );
394 d->mAddressees.remove( it.d->mIt ); 491 d->mAddressees.remove( it.d->mIt );
395} 492}
396 493
397AddressBook::Iterator AddressBook::find( const Addressee &a ) 494AddressBook::Iterator AddressBook::find( const Addressee &a )
398{ 495{
399 Iterator it; 496 Iterator it;
400 for ( it = begin(); it != end(); ++it ) { 497 for ( it = begin(); it != end(); ++it ) {
401 if ( a.uid() == (*it).uid() ) { 498 if ( a.uid() == (*it).uid() ) {
402 return it; 499 return it;
403 } 500 }
404 } 501 }
405 return end(); 502 return end();
406} 503}
407 504
408Addressee AddressBook::findByUid( const QString &uid ) 505Addressee AddressBook::findByUid( const QString &uid )
409{ 506{
410 Iterator it; 507 Iterator it;
411 for ( it = begin(); it != end(); ++it ) { 508 for ( it = begin(); it != end(); ++it ) {
412 if ( uid == (*it).uid() ) { 509 if ( uid == (*it).uid() ) {
413 return *it; 510 return *it;
414 } 511 }
415 } 512 }
416 return Addressee(); 513 return Addressee();
417} 514}
418 515
419Addressee::List AddressBook::allAddressees() 516Addressee::List AddressBook::allAddressees()
420{ 517{
421 return d->mAddressees; 518 return d->mAddressees;
422} 519}
423 520
424Addressee::List AddressBook::findByName( const QString &name ) 521Addressee::List AddressBook::findByName( const QString &name )
425{ 522{
426 Addressee::List results; 523 Addressee::List results;
427 524
428 Iterator it; 525 Iterator it;
429 for ( it = begin(); it != end(); ++it ) { 526 for ( it = begin(); it != end(); ++it ) {
430 if ( name == (*it).realName() ) { 527 if ( name == (*it).realName() ) {
431 results.append( *it ); 528 results.append( *it );
432 } 529 }
433 } 530 }
434 531
435 return results; 532 return results;
436} 533}
437 534
438Addressee::List AddressBook::findByEmail( const QString &email ) 535Addressee::List AddressBook::findByEmail( const QString &email )
439{ 536{
440 Addressee::List results; 537 Addressee::List results;
441 QStringList mailList; 538 QStringList mailList;
442 539
443 Iterator it; 540 Iterator it;
444 for ( it = begin(); it != end(); ++it ) { 541 for ( it = begin(); it != end(); ++it ) {
445 mailList = (*it).emails(); 542 mailList = (*it).emails();
446 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) { 543 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) {
447 if ( email == (*ite) ) { 544 if ( email == (*ite) ) {
448 results.append( *it ); 545 results.append( *it );
449 } 546 }
450 } 547 }
451 } 548 }
452 549
453 return results; 550 return results;
454} 551}
455 552
456Addressee::List AddressBook::findByCategory( const QString &category ) 553Addressee::List AddressBook::findByCategory( const QString &category )
457{ 554{
458 Addressee::List results; 555 Addressee::List results;
459 556
460 Iterator it; 557 Iterator it;
461 for ( it = begin(); it != end(); ++it ) { 558 for ( it = begin(); it != end(); ++it ) {
462 if ( (*it).hasCategory( category) ) { 559 if ( (*it).hasCategory( category) ) {
463 results.append( *it ); 560 results.append( *it );
464 } 561 }
465 } 562 }
466 563
467 return results; 564 return results;
468} 565}
469 566
470void AddressBook::dump() const 567void AddressBook::dump() const
471{ 568{
472 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl; 569 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl;
473 570
474 ConstIterator it; 571 ConstIterator it;
475 for( it = begin(); it != end(); ++it ) { 572 for( it = begin(); it != end(); ++it ) {
476 (*it).dump(); 573 (*it).dump();
477 } 574 }
478 575
479 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl; 576 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl;
480} 577}
481 578
482QString AddressBook::identifier() 579QString AddressBook::identifier()
483{ 580{
484 QStringList identifier; 581 QStringList identifier;
485 582
486 583
487 KRES::Manager<Resource>::ActiveIterator it; 584 KRES::Manager<Resource>::ActiveIterator it;
488 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 585 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
489 if ( !(*it)->identifier().isEmpty() ) 586 if ( !(*it)->identifier().isEmpty() )
490 identifier.append( (*it)->identifier() ); 587 identifier.append( (*it)->identifier() );
491 } 588 }
492 589
493 return identifier.join( ":" ); 590 return identifier.join( ":" );
494} 591}
495 592
496Field::List AddressBook::fields( int category ) 593Field::List AddressBook::fields( int category )
497{ 594{
498 if ( d->mAllFields.isEmpty() ) { 595 if ( d->mAllFields.isEmpty() ) {
499 d->mAllFields = Field::allFields(); 596 d->mAllFields = Field::allFields();
500 } 597 }
501 598
502 if ( category == Field::All ) return d->mAllFields; 599 if ( category == Field::All ) return d->mAllFields;
503 600
504 Field::List result; 601 Field::List result;
505 Field::List::ConstIterator it; 602 Field::List::ConstIterator it;
506 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) { 603 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) {
507 if ( (*it)->category() & category ) result.append( *it ); 604 if ( (*it)->category() & category ) result.append( *it );
508 } 605 }
509 606
510 return result; 607 return result;
511} 608}
512 609
513bool AddressBook::addCustomField( const QString &label, int category, 610bool AddressBook::addCustomField( const QString &label, int category,
514 const QString &key, const QString &app ) 611 const QString &key, const QString &app )
515{ 612{
516 if ( d->mAllFields.isEmpty() ) { 613 if ( d->mAllFields.isEmpty() ) {
517 d->mAllFields = Field::allFields(); 614 d->mAllFields = Field::allFields();
518 } 615 }
519//US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app; 616//US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app;
520 QString a = app.isNull() ? KGlobal::getAppName() : app; 617 QString a = app.isNull() ? KGlobal::getAppName() : app;
521 618
522 QString k = key.isNull() ? label : key; 619 QString k = key.isNull() ? label : key;
523 620
524 Field *field = Field::createCustomField( label, category, k, a ); 621 Field *field = Field::createCustomField( label, category, k, a );
525 622
526 if ( !field ) return false; 623 if ( !field ) return false;
527 624
528 d->mAllFields.append( field ); 625 d->mAllFields.append( field );
529 626
530 return true; 627 return true;
531} 628}
532 629
533QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab ) 630QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab )
534{ 631{
535 if (!ab.d) return s; 632 if (!ab.d) return s;
536 633
537 return s << ab.d->mAddressees; 634 return s << ab.d->mAddressees;
538} 635}
539 636
540QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab ) 637QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab )
541{ 638{
542 if (!ab.d) return s; 639 if (!ab.d) return s;
543 640
544 s >> ab.d->mAddressees; 641 s >> ab.d->mAddressees;
545 642
546 return s; 643 return s;
547} 644}
548 645
549bool AddressBook::addResource( Resource *resource ) 646bool AddressBook::addResource( Resource *resource )
550{ 647{
551 if ( !resource->open() ) { 648 if ( !resource->open() ) {
552 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl; 649 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl;
553 return false; 650 return false;
554 } 651 }
555 652
556 resource->setAddressBook( this ); 653 resource->setAddressBook( this );
557 654
558 d->mManager->add( resource ); 655 d->mManager->add( resource );
559 return true; 656 return true;
560} 657}
561 658
562bool AddressBook::removeResource( Resource *resource ) 659bool AddressBook::removeResource( Resource *resource )
563{ 660{
564 resource->close(); 661 resource->close();
565 662
566 if ( resource == standardResource() ) 663 if ( resource == standardResource() )
567 d->mManager->setStandardResource( 0 ); 664 d->mManager->setStandardResource( 0 );
568 665
569 resource->setAddressBook( 0 ); 666 resource->setAddressBook( 0 );
570 667
571 d->mManager->remove( resource ); 668 d->mManager->remove( resource );
572 return true; 669 return true;
573} 670}
574 671
575QPtrList<Resource> AddressBook::resources() 672QPtrList<Resource> AddressBook::resources()
576{ 673{
577 QPtrList<Resource> list; 674 QPtrList<Resource> list;
578 675
579// qDebug("AddressBook::resources() 1"); 676// qDebug("AddressBook::resources() 1");
580 677
581 KRES::Manager<Resource>::ActiveIterator it; 678 KRES::Manager<Resource>::ActiveIterator it;
582 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 679 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
583 list.append( *it ); 680 list.append( *it );
584 681
585 return list; 682 return list;
586} 683}
587 684
588/*US 685/*US
589void AddressBook::setErrorHandler( ErrorHandler *handler ) 686void AddressBook::setErrorHandler( ErrorHandler *handler )
590{ 687{
591 delete d->mErrorHandler; 688 delete d->mErrorHandler;
592 d->mErrorHandler = handler; 689 d->mErrorHandler = handler;
593} 690}
594*/ 691*/
595 692
596void AddressBook::error( const QString& msg ) 693void AddressBook::error( const QString& msg )
597{ 694{
598/*US 695/*US
599 if ( !d->mErrorHandler ) // create default error handler 696 if ( !d->mErrorHandler ) // create default error handler
600 d->mErrorHandler = new ConsoleErrorHandler; 697 d->mErrorHandler = new ConsoleErrorHandler;
601 698
602 if ( d->mErrorHandler ) 699 if ( d->mErrorHandler )
603 d->mErrorHandler->error( msg ); 700 d->mErrorHandler->error( msg );
604 else 701 else
605 kdError(5700) << "no error handler defined" << endl; 702 kdError(5700) << "no error handler defined" << endl;
606*/ 703*/
607 kdDebug(5700) << "msg" << endl; 704 kdDebug(5700) << "msg" << endl;
608 qDebug(msg); 705 qDebug(msg);
609} 706}
610 707
611void AddressBook::deleteRemovedAddressees() 708void AddressBook::deleteRemovedAddressees()
612{ 709{
613 Addressee::List::Iterator it; 710 Addressee::List::Iterator it;
614 for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) { 711 for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) {
615 Resource *resource = (*it).resource(); 712 Resource *resource = (*it).resource();
616 if ( resource && !resource->readOnly() && resource->isOpen() ) 713 if ( resource && !resource->readOnly() && resource->isOpen() )
617 resource->removeAddressee( *it ); 714 resource->removeAddressee( *it );
618 } 715 }
619 716
620 d->mRemovedAddressees.clear(); 717 d->mRemovedAddressees.clear();
621} 718}
622 719
623void AddressBook::setStandardResource( Resource *resource ) 720void AddressBook::setStandardResource( Resource *resource )
624{ 721{
625// qDebug("AddressBook::setStandardResource 1"); 722// qDebug("AddressBook::setStandardResource 1");
626 d->mManager->setStandardResource( resource ); 723 d->mManager->setStandardResource( resource );
627} 724}
628 725
629Resource *AddressBook::standardResource() 726Resource *AddressBook::standardResource()
630{ 727{
631 return d->mManager->standardResource(); 728 return d->mManager->standardResource();
632} 729}
633 730
634KRES::Manager<Resource> *AddressBook::resourceManager() 731KRES::Manager<Resource> *AddressBook::resourceManager()
635{ 732{
636 return d->mManager; 733 return d->mManager;
637} 734}
638 735
639void AddressBook::cleanUp() 736void AddressBook::cleanUp()
640{ 737{
641 KRES::Manager<Resource>::ActiveIterator it; 738 KRES::Manager<Resource>::ActiveIterator it;
642 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 739 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
643 if ( !(*it)->readOnly() && (*it)->isOpen() ) 740 if ( !(*it)->readOnly() && (*it)->isOpen() )
644 (*it)->cleanUp(); 741 (*it)->cleanUp();
645 } 742 }
646} 743}