summaryrefslogtreecommitdiffabout
path: root/kabc/addressbook.cpp
Unidiff
Diffstat (limited to 'kabc/addressbook.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/addressbook.cpp35
1 files changed, 20 insertions, 15 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp
index 0838157..20310a0 100644
--- a/kabc/addressbook.cpp
+++ b/kabc/addressbook.cpp
@@ -187,126 +187,133 @@ AddressBook::ConstIterator &AddressBook::ConstIterator::operator++()
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); 219 init(0, "contact");
220} 220}
221 221
222AddressBook::AddressBook( const QString &config ) 222AddressBook::AddressBook( const QString &config )
223{ 223{
224 init(config); 224 init(config, "contact");
225} 225}
226 226
227void AddressBook::init(const QString &config) 227AddressBook::AddressBook( const QString &config, const QString &family )
228{
229 init(config, family);
230
231}
232
233// the default family is "contact"
234void AddressBook::init(const QString &config, const QString &family )
228{ 235{
229 d = new AddressBookData; 236 d = new AddressBookData;
230 if (config != 0) { 237 if (config != 0) {
231 d->mConfig = new KConfig( config ); 238 d->mConfig = new KConfig( config );
232// qDebug("AddressBook::init 1 config=%s",config.latin1() ); 239// qDebug("AddressBook::init 1 config=%s",config.latin1() );
233 } 240 }
234 else { 241 else {
235 d->mConfig = 0; 242 d->mConfig = 0;
236// qDebug("AddressBook::init 1 config=0"); 243// qDebug("AddressBook::init 1 config=0");
237 } 244 }
238 245
239//US d->mErrorHandler = 0; 246//US d->mErrorHandler = 0;
240 d->mManager = new KRES::Manager<Resource>( "contact" ); 247 d->mManager = new KRES::Manager<Resource>( family, false );
241 d->mManager->readConfig( d->mConfig ); 248 d->mManager->readConfig( d->mConfig );
242} 249}
243 250
244AddressBook::~AddressBook() 251AddressBook::~AddressBook()
245{ 252{
246 delete d->mConfig; d->mConfig = 0; 253 delete d->mConfig; d->mConfig = 0;
247 delete d->mManager; d->mManager = 0; 254 delete d->mManager; d->mManager = 0;
248//US delete d->mErrorHandler; d->mErrorHandler = 0; 255//US delete d->mErrorHandler; d->mErrorHandler = 0;
249 delete d; d = 0; 256 delete d; d = 0;
250} 257}
251 258
252bool AddressBook::load() 259bool AddressBook::load()
253{ 260{
254 261
255 262
256 clear(); 263 clear();
257 264
258 KRES::Manager<Resource>::ActiveIterator it; 265 KRES::Manager<Resource>::ActiveIterator it;
259 bool ok = true; 266 bool ok = true;
260 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 267 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
261 if ( !(*it)->load() ) { 268 if ( !(*it)->load() ) {
262 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); 269 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) );
263 ok = false; 270 ok = false;
264 } 271 }
265 272
266 // mark all addressees as unchanged 273 // mark all addressees as unchanged
267 Addressee::List::Iterator addrIt; 274 Addressee::List::Iterator addrIt;
268 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) 275 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt )
269 (*addrIt).setChanged( false ); 276 (*addrIt).setChanged( false );
270 277
271 return ok; 278 return ok;
272} 279}
273 280
274bool AddressBook::save( Ticket *ticket ) 281bool AddressBook::save( Ticket *ticket )
275{ 282{
276 kdDebug(5700) << "AddressBook::save()"<< endl; 283 kdDebug(5700) << "AddressBook::save()"<< endl;
277 284
278 if ( ticket->resource() ) { 285 if ( ticket->resource() ) {
279 deleteRemovedAddressees(); 286 deleteRemovedAddressees();
280 287
281 return ticket->resource()->save( ticket ); 288 return ticket->resource()->save( ticket );
282 } 289 }
283 290
284 return false; 291 return false;
285} 292}
286 293
287AddressBook::Iterator AddressBook::begin() 294AddressBook::Iterator AddressBook::begin()
288{ 295{
289 Iterator it = Iterator(); 296 Iterator it = Iterator();
290 it.d->mIt = d->mAddressees.begin(); 297 it.d->mIt = d->mAddressees.begin();
291 return it; 298 return it;
292} 299}
293 300
294AddressBook::ConstIterator AddressBook::begin() const 301AddressBook::ConstIterator AddressBook::begin() const
295{ 302{
296 ConstIterator it = ConstIterator(); 303 ConstIterator it = ConstIterator();
297 it.d->mIt = d->mAddressees.begin(); 304 it.d->mIt = d->mAddressees.begin();
298 return it; 305 return it;
299} 306}
300 307
301AddressBook::Iterator AddressBook::end() 308AddressBook::Iterator AddressBook::end()
302{ 309{
303 Iterator it = Iterator(); 310 Iterator it = Iterator();
304 it.d->mIt = d->mAddressees.end(); 311 it.d->mIt = d->mAddressees.end();
305 return it; 312 return it;
306} 313}
307 314
308AddressBook::ConstIterator AddressBook::end() const 315AddressBook::ConstIterator AddressBook::end() const
309{ 316{
310 ConstIterator it = ConstIterator(); 317 ConstIterator it = ConstIterator();
311 it.d->mIt = d->mAddressees.end(); 318 it.d->mIt = d->mAddressees.end();
312 return it; 319 return it;
@@ -327,65 +334,65 @@ Ticket *AddressBook::requestSaveTicket( Resource *resource )
327 resource = standardResource(); 334 resource = standardResource();
328 } 335 }
329 336
330 KRES::Manager<Resource>::ActiveIterator it; 337 KRES::Manager<Resource>::ActiveIterator it;
331 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 338 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
332 if ( (*it) == resource ) { 339 if ( (*it) == resource ) {
333 if ( (*it)->readOnly() || !(*it)->isOpen() ) 340 if ( (*it)->readOnly() || !(*it)->isOpen() )
334 return 0; 341 return 0;
335 else 342 else
336 return (*it)->requestSaveTicket(); 343 return (*it)->requestSaveTicket();
337 } 344 }
338 } 345 }
339 346
340 return 0; 347 return 0;
341} 348}
342 349
343void AddressBook::insertAddressee( const Addressee &a ) 350void AddressBook::insertAddressee( const Addressee &a )
344{ 351{
345 Addressee::List::Iterator it; 352 Addressee::List::Iterator it;
346 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { 353 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) {
347 if ( a.uid() == (*it).uid() ) { 354 if ( a.uid() == (*it).uid() ) {
348 bool changed = false; 355 bool changed = false;
349 Addressee addr = a; 356 Addressee addr = a;
350 if ( addr != (*it) ) 357 if ( addr != (*it) )
351 changed = true; 358 changed = true;
352 359
353 (*it) = a; 360 (*it) = a;
354 if ( (*it).resource() == 0 ) 361 if ( (*it).resource() == 0 )
355 (*it).setResource( standardResource() ); 362 (*it).setResource( standardResource() );
356 363
357 if ( changed ) { 364 if ( changed ) {
358 (*it).setRevision( QDateTime::currentDateTime() ); 365 (*it).setRevision( QDateTime::currentDateTime() );
359 (*it).setChanged( true ); 366 (*it).setChanged( true );
360 } 367 }
361 368
362 return; 369 return;
363 } 370 }
364 } 371 }
365 d->mAddressees.append( a ); 372 d->mAddressees.append( a );
366 Addressee& addr = d->mAddressees.last(); 373 Addressee& addr = d->mAddressees.last();
367 if ( addr.resource() == 0 ) 374 if ( addr.resource() == 0 )
368 addr.setResource( standardResource() ); 375 addr.setResource( standardResource() );
369 376
370 addr.setChanged( true ); 377 addr.setChanged( true );
371} 378}
372 379
373void AddressBook::removeAddressee( const Addressee &a ) 380void AddressBook::removeAddressee( const Addressee &a )
374{ 381{
375 Iterator it; 382 Iterator it;
376 for ( it = begin(); it != end(); ++it ) { 383 for ( it = begin(); it != end(); ++it ) {
377 if ( a.uid() == (*it).uid() ) { 384 if ( a.uid() == (*it).uid() ) {
378 removeAddressee( it ); 385 removeAddressee( it );
379 return; 386 return;
380 } 387 }
381 } 388 }
382} 389}
383 390
384void AddressBook::removeAddressee( const Iterator &it ) 391void AddressBook::removeAddressee( const Iterator &it )
385{ 392{
386 d->mRemovedAddressees.append( (*it) ); 393 d->mRemovedAddressees.append( (*it) );
387 d->mAddressees.remove( it.d->mIt ); 394 d->mAddressees.remove( it.d->mIt );
388} 395}
389 396
390AddressBook::Iterator AddressBook::find( const Addressee &a ) 397AddressBook::Iterator AddressBook::find( const Addressee &a )
391{ 398{
@@ -406,202 +413,200 @@ Addressee AddressBook::findByUid( const QString &uid )
406 return *it; 413 return *it;
407 } 414 }
408 } 415 }
409 return Addressee(); 416 return Addressee();
410} 417}
411 418
412Addressee::List AddressBook::allAddressees() 419Addressee::List AddressBook::allAddressees()
413{ 420{
414 return d->mAddressees; 421 return d->mAddressees;
415} 422}
416 423
417Addressee::List AddressBook::findByName( const QString &name ) 424Addressee::List AddressBook::findByName( const QString &name )
418{ 425{
419 Addressee::List results; 426 Addressee::List results;
420 427
421 Iterator it; 428 Iterator it;
422 for ( it = begin(); it != end(); ++it ) { 429 for ( it = begin(); it != end(); ++it ) {
423 if ( name == (*it).name() ) { 430 if ( name == (*it).name() ) {
424 results.append( *it ); 431 results.append( *it );
425 } 432 }
426 } 433 }
427 434
428 return results; 435 return results;
429} 436}
430 437
431Addressee::List AddressBook::findByEmail( const QString &email ) 438Addressee::List AddressBook::findByEmail( const QString &email )
432{ 439{
433 Addressee::List results; 440 Addressee::List results;
434 QStringList mailList; 441 QStringList mailList;
435 442
436 Iterator it; 443 Iterator it;
437 for ( it = begin(); it != end(); ++it ) { 444 for ( it = begin(); it != end(); ++it ) {
438 mailList = (*it).emails(); 445 mailList = (*it).emails();
439 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) { 446 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) {
440 if ( email == (*ite) ) { 447 if ( email == (*ite) ) {
441 results.append( *it ); 448 results.append( *it );
442 } 449 }
443 } 450 }
444 } 451 }
445 452
446 return results; 453 return results;
447} 454}
448 455
449Addressee::List AddressBook::findByCategory( const QString &category ) 456Addressee::List AddressBook::findByCategory( const QString &category )
450{ 457{
451 Addressee::List results; 458 Addressee::List results;
452 459
453 Iterator it; 460 Iterator it;
454 for ( it = begin(); it != end(); ++it ) { 461 for ( it = begin(); it != end(); ++it ) {
455 if ( (*it).hasCategory( category) ) { 462 if ( (*it).hasCategory( category) ) {
456 results.append( *it ); 463 results.append( *it );
457 } 464 }
458 } 465 }
459 466
460 return results; 467 return results;
461} 468}
462 469
463void AddressBook::dump() const 470void AddressBook::dump() const
464{ 471{
465 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl; 472 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl;
466 473
467 ConstIterator it; 474 ConstIterator it;
468 for( it = begin(); it != end(); ++it ) { 475 for( it = begin(); it != end(); ++it ) {
469 (*it).dump(); 476 (*it).dump();
470 } 477 }
471 478
472 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl; 479 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl;
473} 480}
474 481
475QString AddressBook::identifier() 482QString AddressBook::identifier()
476{ 483{
477 QStringList identifier; 484 QStringList identifier;
478 485
479 486
480 KRES::Manager<Resource>::ActiveIterator it; 487 KRES::Manager<Resource>::ActiveIterator it;
481 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 488 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
482 if ( !(*it)->identifier().isEmpty() ) 489 if ( !(*it)->identifier().isEmpty() )
483 identifier.append( (*it)->identifier() ); 490 identifier.append( (*it)->identifier() );
484 } 491 }
485 492
486 return identifier.join( ":" ); 493 return identifier.join( ":" );
487} 494}
488 495
489Field::List AddressBook::fields( int category ) 496Field::List AddressBook::fields( int category )
490{ 497{
491 if ( d->mAllFields.isEmpty() ) { 498 if ( d->mAllFields.isEmpty() ) {
492 d->mAllFields = Field::allFields(); 499 d->mAllFields = Field::allFields();
493 } 500 }
494 501
495 if ( category == Field::All ) return d->mAllFields; 502 if ( category == Field::All ) return d->mAllFields;
496 503
497 Field::List result; 504 Field::List result;
498 Field::List::ConstIterator it; 505 Field::List::ConstIterator it;
499 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) { 506 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) {
500 if ( (*it)->category() & category ) result.append( *it ); 507 if ( (*it)->category() & category ) result.append( *it );
501 } 508 }
502 509
503 return result; 510 return result;
504} 511}
505 512
506bool AddressBook::addCustomField( const QString &label, int category, 513bool AddressBook::addCustomField( const QString &label, int category,
507 const QString &key, const QString &app ) 514 const QString &key, const QString &app )
508{ 515{
509 if ( d->mAllFields.isEmpty() ) { 516 if ( d->mAllFields.isEmpty() ) {
510 d->mAllFields = Field::allFields(); 517 d->mAllFields = Field::allFields();
511 } 518 }
512//US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app; 519//US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app;
513 QString a = app.isNull() ? KGlobal::getAppName() : app; 520 QString a = app.isNull() ? KGlobal::getAppName() : app;
514 521
515 QString k = key.isNull() ? label : key; 522 QString k = key.isNull() ? label : key;
516 523
517 Field *field = Field::createCustomField( label, category, k, a ); 524 Field *field = Field::createCustomField( label, category, k, a );
518 525
519 if ( !field ) return false; 526 if ( !field ) return false;
520 527
521 d->mAllFields.append( field ); 528 d->mAllFields.append( field );
522 529
523 return true; 530 return true;
524} 531}
525 532
526QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab ) 533QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab )
527{ 534{
528 if (!ab.d) return s; 535 if (!ab.d) return s;
529 536
530 return s << ab.d->mAddressees; 537 return s << ab.d->mAddressees;
531} 538}
532 539
533QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab ) 540QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab )
534{ 541{
535 if (!ab.d) return s; 542 if (!ab.d) return s;
536 543
537 s >> ab.d->mAddressees; 544 s >> ab.d->mAddressees;
538 545
539 return s; 546 return s;
540} 547}
541 548
542bool AddressBook::addResource( Resource *resource ) 549bool AddressBook::addResource( Resource *resource )
543{ 550{
544 qDebug("AddressBook::addResource 1");
545
546 if ( !resource->open() ) { 551 if ( !resource->open() ) {
547 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl; 552 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl;
548 return false; 553 return false;
549 } 554 }
550 555
551 resource->setAddressBook( this ); 556 resource->setAddressBook( this );
552 557
553 d->mManager->add( resource ); 558 d->mManager->add( resource );
554 return true; 559 return true;
555} 560}
556 561
557bool AddressBook::removeResource( Resource *resource ) 562bool AddressBook::removeResource( Resource *resource )
558{ 563{
559 resource->close(); 564 resource->close();
560 565
561 if ( resource == standardResource() ) 566 if ( resource == standardResource() )
562 d->mManager->setStandardResource( 0 ); 567 d->mManager->setStandardResource( 0 );
563 568
564 resource->setAddressBook( 0 ); 569 resource->setAddressBook( 0 );
565 570
566 d->mManager->remove( resource ); 571 d->mManager->remove( resource );
567 return true; 572 return true;
568} 573}
569 574
570QPtrList<Resource> AddressBook::resources() 575QPtrList<Resource> AddressBook::resources()
571{ 576{
572 QPtrList<Resource> list; 577 QPtrList<Resource> list;
573 578
574// qDebug("AddressBook::resources() 1"); 579// qDebug("AddressBook::resources() 1");
575 580
576 KRES::Manager<Resource>::ActiveIterator it; 581 KRES::Manager<Resource>::ActiveIterator it;
577 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 582 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
578 list.append( *it ); 583 list.append( *it );
579 584
580 return list; 585 return list;
581} 586}
582 587
583/*US 588/*US
584void AddressBook::setErrorHandler( ErrorHandler *handler ) 589void AddressBook::setErrorHandler( ErrorHandler *handler )
585{ 590{
586 delete d->mErrorHandler; 591 delete d->mErrorHandler;
587 d->mErrorHandler = handler; 592 d->mErrorHandler = handler;
588} 593}
589*/ 594*/
590 595
591void AddressBook::error( const QString& msg ) 596void AddressBook::error( const QString& msg )
592{ 597{
593/*US 598/*US
594 if ( !d->mErrorHandler ) // create default error handler 599 if ( !d->mErrorHandler ) // create default error handler
595 d->mErrorHandler = new ConsoleErrorHandler; 600 d->mErrorHandler = new ConsoleErrorHandler;
596 601
597 if ( d->mErrorHandler ) 602 if ( d->mErrorHandler )
598 d->mErrorHandler->error( msg ); 603 d->mErrorHandler->error( msg );
599 else 604 else
600 kdError(5700) << "no error handler defined" << endl; 605 kdError(5700) << "no error handler defined" << endl;
601*/ 606*/
602 kdDebug(5700) << "msg" << endl; 607 kdDebug(5700) << "msg" << endl;
603 qDebug(msg); 608 qDebug(msg);
604} 609}
605 610
606void AddressBook::deleteRemovedAddressees() 611void AddressBook::deleteRemovedAddressees()
607{ 612{