summaryrefslogtreecommitdiffabout
path: root/kabc/addressbook.cpp
Unidiff
Diffstat (limited to 'kabc/addressbook.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/addressbook.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp
index 64832f1..86dc7c2 100644
--- a/kabc/addressbook.cpp
+++ b/kabc/addressbook.cpp
@@ -221,670 +221,702 @@ AddressBook::AddressBook()
221} 221}
222 222
223AddressBook::AddressBook( const QString &config ) 223AddressBook::AddressBook( const QString &config )
224{ 224{
225 init(config, "contact"); 225 init(config, "contact");
226} 226}
227 227
228AddressBook::AddressBook( const QString &config, const QString &family ) 228AddressBook::AddressBook( const QString &config, const QString &family )
229{ 229{
230 init(config, family); 230 init(config, family);
231 231
232} 232}
233 233
234// the default family is "contact" 234// the default family is "contact"
235void AddressBook::init(const QString &config, const QString &family ) 235void AddressBook::init(const QString &config, const QString &family )
236{ 236{
237 blockLSEchange = false; 237 blockLSEchange = false;
238 d = new AddressBookData; 238 d = new AddressBookData;
239 QString fami = family; 239 QString fami = family;
240 qDebug("new ab "); 240 qDebug("new ab ");
241 if (config != 0) { 241 if (config != 0) {
242 qDebug("config != 0 "); 242 qDebug("config != 0 ");
243 if ( family == "syncContact" ) { 243 if ( family == "syncContact" ) {
244 qDebug("creating sync config "); 244 qDebug("creating sync config ");
245 fami = "contact"; 245 fami = "contact";
246 KConfig* con = new KConfig( locateLocal("config", "syncContactrc") ); 246 KConfig* con = new KConfig( locateLocal("config", "syncContactrc") );
247 con->setGroup( "General" ); 247 con->setGroup( "General" );
248 con->writeEntry( "ResourceKeys", QString("sync") ); 248 con->writeEntry( "ResourceKeys", QString("sync") );
249 con->writeEntry( "Standard", QString("sync") ); 249 con->writeEntry( "Standard", QString("sync") );
250 con->setGroup( "Resource_sync" ); 250 con->setGroup( "Resource_sync" );
251 con->writeEntry( "FileName", config ); 251 con->writeEntry( "FileName", config );
252 con->writeEntry( "FileFormat", QString("vcard") ); 252 con->writeEntry( "FileFormat", QString("vcard") );
253 con->writeEntry( "ResourceIdentifier", QString("sync") ); 253 con->writeEntry( "ResourceIdentifier", QString("sync") );
254 con->writeEntry( "ResourceName", QString("sync_res") ); 254 con->writeEntry( "ResourceName", QString("sync_res") );
255 if ( config.right(4) == ".xml" ) 255 if ( config.right(4) == ".xml" )
256 con->writeEntry( "ResourceType", QString("qtopia") ); 256 con->writeEntry( "ResourceType", QString("qtopia") );
257 else 257 else
258 con->writeEntry( "ResourceType", QString("file") ); 258 con->writeEntry( "ResourceType", QString("file") );
259 //con->sync(); 259 //con->sync();
260 d->mConfig = con; 260 d->mConfig = con;
261 } 261 }
262 else 262 else
263 d->mConfig = new KConfig( locateLocal("config", config) ); 263 d->mConfig = new KConfig( locateLocal("config", config) );
264// qDebug("AddressBook::init 1 config=%s",config.latin1() ); 264// qDebug("AddressBook::init 1 config=%s",config.latin1() );
265 } 265 }
266 else { 266 else {
267 d->mConfig = 0; 267 d->mConfig = 0;
268// qDebug("AddressBook::init 1 config=0"); 268// qDebug("AddressBook::init 1 config=0");
269 } 269 }
270 270
271//US d->mErrorHandler = 0; 271//US d->mErrorHandler = 0;
272 d->mManager = new KRES::Manager<Resource>( fami, false ); 272 d->mManager = new KRES::Manager<Resource>( fami, false );
273 d->mManager->readConfig( d->mConfig ); 273 d->mManager->readConfig( d->mConfig );
274 if ( family == "syncContact" ) { 274 if ( family == "syncContact" ) {
275 KRES::Manager<Resource> *manager = d->mManager; 275 KRES::Manager<Resource> *manager = d->mManager;
276 KRES::Manager<Resource>::ActiveIterator it; 276 KRES::Manager<Resource>::ActiveIterator it;
277 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 277 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
278 (*it)->setAddressBook( this ); 278 (*it)->setAddressBook( this );
279 if ( !(*it)->open() ) 279 if ( !(*it)->open() )
280 error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) ); 280 error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) );
281 } 281 }
282 Resource *res = standardResource(); 282 Resource *res = standardResource();
283 if ( !res ) { 283 if ( !res ) {
284 qDebug("ERROR: no standard resource"); 284 qDebug("ERROR: no standard resource");
285 res = manager->createResource( "file" ); 285 res = manager->createResource( "file" );
286 if ( res ) 286 if ( res )
287 { 287 {
288 addResource( res ); 288 addResource( res );
289 } 289 }
290 else 290 else
291 qDebug(" No resource available!!!"); 291 qDebug(" No resource available!!!");
292 } 292 }
293 setStandardResource( res ); 293 setStandardResource( res );
294 manager->writeConfig(); 294 manager->writeConfig();
295 } 295 }
296 addCustomField( i18n( "Department" ), KABC::Field::Organization, 296 addCustomField( i18n( "Department" ), KABC::Field::Organization,
297 "X-Department", "KADDRESSBOOK" ); 297 "X-Department", "KADDRESSBOOK" );
298 addCustomField( i18n( "Profession" ), KABC::Field::Organization, 298 addCustomField( i18n( "Profession" ), KABC::Field::Organization,
299 "X-Profession", "KADDRESSBOOK" ); 299 "X-Profession", "KADDRESSBOOK" );
300 addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization, 300 addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization,
301 "X-AssistantsName", "KADDRESSBOOK" ); 301 "X-AssistantsName", "KADDRESSBOOK" );
302 addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization, 302 addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization,
303 "X-ManagersName", "KADDRESSBOOK" ); 303 "X-ManagersName", "KADDRESSBOOK" );
304 addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal, 304 addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal,
305 "X-SpousesName", "KADDRESSBOOK" ); 305 "X-SpousesName", "KADDRESSBOOK" );
306 addCustomField( i18n( "Office" ), KABC::Field::Personal, 306 addCustomField( i18n( "Office" ), KABC::Field::Personal,
307 "X-Office", "KADDRESSBOOK" ); 307 "X-Office", "KADDRESSBOOK" );
308 addCustomField( i18n( "IM Address" ), KABC::Field::Personal, 308 addCustomField( i18n( "IM Address" ), KABC::Field::Personal,
309 "X-IMAddress", "KADDRESSBOOK" ); 309 "X-IMAddress", "KADDRESSBOOK" );
310 addCustomField( i18n( "Anniversary" ), KABC::Field::Personal, 310 addCustomField( i18n( "Anniversary" ), KABC::Field::Personal,
311 "X-Anniversary", "KADDRESSBOOK" ); 311 "X-Anniversary", "KADDRESSBOOK" );
312 312
313 //US added this field to become compatible with Opie/qtopia addressbook 313 //US added this field to become compatible with Opie/qtopia addressbook
314 // values can be "female" or "male" or "". An empty field represents undefined. 314 // values can be "female" or "male" or "". An empty field represents undefined.
315 addCustomField( i18n( "Gender" ), KABC::Field::Personal, 315 addCustomField( i18n( "Gender" ), KABC::Field::Personal,
316 "X-Gender", "KADDRESSBOOK" ); 316 "X-Gender", "KADDRESSBOOK" );
317 addCustomField( i18n( "Children" ), KABC::Field::Personal, 317 addCustomField( i18n( "Children" ), KABC::Field::Personal,
318 "X-Children", "KADDRESSBOOK" ); 318 "X-Children", "KADDRESSBOOK" );
319 addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal, 319 addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal,
320 "X-FreeBusyUrl", "KADDRESSBOOK" ); 320 "X-FreeBusyUrl", "KADDRESSBOOK" );
321 addCustomField( i18n( "ExternalID" ), KABC::Field::Personal, 321 addCustomField( i18n( "ExternalID" ), KABC::Field::Personal,
322 "X-ExternalID", "KADDRESSBOOK" ); 322 "X-ExternalID", "KADDRESSBOOK" );
323} 323}
324 324
325AddressBook::~AddressBook() 325AddressBook::~AddressBook()
326{ 326{
327 delete d->mConfig; d->mConfig = 0; 327 delete d->mConfig; d->mConfig = 0;
328 delete d->mManager; d->mManager = 0; 328 delete d->mManager; d->mManager = 0;
329//US delete d->mErrorHandler; d->mErrorHandler = 0; 329//US delete d->mErrorHandler; d->mErrorHandler = 0;
330 delete d; d = 0; 330 delete d; d = 0;
331} 331}
332 332
333bool AddressBook::load() 333bool AddressBook::load()
334{ 334{
335 335
336 336
337 clear(); 337 clear();
338 338
339 KRES::Manager<Resource>::ActiveIterator it; 339 KRES::Manager<Resource>::ActiveIterator it;
340 bool ok = true; 340 bool ok = true;
341 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 341 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
342 if ( !(*it)->load() ) { 342 if ( !(*it)->load() ) {
343 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); 343 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) );
344 ok = false; 344 ok = false;
345 } 345 }
346 346
347 // mark all addressees as unchanged 347 // mark all addressees as unchanged
348 Addressee::List::Iterator addrIt; 348 Addressee::List::Iterator addrIt;
349 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) { 349 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) {
350 (*addrIt).setChanged( false ); 350 (*addrIt).setChanged( false );
351 QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" ); 351 QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" );
352 if ( !id.isEmpty() ) { 352 if ( !id.isEmpty() ) {
353 //qDebug("setId aa %s ", id.latin1()); 353 //qDebug("setId aa %s ", id.latin1());
354 (*addrIt).setIDStr(id ); 354 (*addrIt).setIDStr(id );
355 } 355 }
356 } 356 }
357 blockLSEchange = true; 357 blockLSEchange = true;
358 return ok; 358 return ok;
359} 359}
360 360
361bool AddressBook::save( Ticket *ticket ) 361bool AddressBook::save( Ticket *ticket )
362{ 362{
363 kdDebug(5700) << "AddressBook::save()"<< endl; 363 kdDebug(5700) << "AddressBook::save()"<< endl;
364 364
365 if ( ticket->resource() ) { 365 if ( ticket->resource() ) {
366 deleteRemovedAddressees(); 366 deleteRemovedAddressees();
367 return ticket->resource()->save( ticket ); 367 return ticket->resource()->save( ticket );
368 } 368 }
369 369
370 return false; 370 return false;
371} 371}
372bool AddressBook::saveAB() 372bool AddressBook::saveAB()
373{ 373{
374 bool ok = true; 374 bool ok = true;
375 375
376 deleteRemovedAddressees(); 376 deleteRemovedAddressees();
377 Iterator ait; 377 Iterator ait;
378 for ( ait = begin(); ait != end(); ++ait ) { 378 for ( ait = begin(); ait != end(); ++ait ) {
379 if ( !(*ait).IDStr().isEmpty() ) { 379 if ( !(*ait).IDStr().isEmpty() ) {
380 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); 380 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() );
381 } 381 }
382 } 382 }
383 KRES::Manager<Resource>::ActiveIterator it; 383 KRES::Manager<Resource>::ActiveIterator it;
384 KRES::Manager<Resource> *manager = d->mManager; 384 KRES::Manager<Resource> *manager = d->mManager;
385 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 385 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
386 if ( !(*it)->readOnly() && (*it)->isOpen() ) { 386 if ( !(*it)->readOnly() && (*it)->isOpen() ) {
387 Ticket *ticket = requestSaveTicket( *it ); 387 Ticket *ticket = requestSaveTicket( *it );
388// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); 388// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() );
389 if ( !ticket ) { 389 if ( !ticket ) {
390 error( i18n( "Unable to save to resource '%1'. It is locked." ) 390 error( i18n( "Unable to save to resource '%1'. It is locked." )
391 .arg( (*it)->resourceName() ) ); 391 .arg( (*it)->resourceName() ) );
392 return false; 392 return false;
393 } 393 }
394 394
395 //if ( !save( ticket ) ) 395 //if ( !save( ticket ) )
396 if ( ticket->resource() ) { 396 if ( ticket->resource() ) {
397 if ( ! ticket->resource()->save( ticket ) ) 397 if ( ! ticket->resource()->save( ticket ) )
398 ok = false; 398 ok = false;
399 } else 399 } else
400 ok = false; 400 ok = false;
401 401
402 } 402 }
403 } 403 }
404 return ok; 404 return ok;
405} 405}
406 406
407AddressBook::Iterator AddressBook::begin() 407AddressBook::Iterator AddressBook::begin()
408{ 408{
409 Iterator it = Iterator(); 409 Iterator it = Iterator();
410 it.d->mIt = d->mAddressees.begin(); 410 it.d->mIt = d->mAddressees.begin();
411 return it; 411 return it;
412} 412}
413 413
414AddressBook::ConstIterator AddressBook::begin() const 414AddressBook::ConstIterator AddressBook::begin() const
415{ 415{
416 ConstIterator it = ConstIterator(); 416 ConstIterator it = ConstIterator();
417 it.d->mIt = d->mAddressees.begin(); 417 it.d->mIt = d->mAddressees.begin();
418 return it; 418 return it;
419} 419}
420 420
421AddressBook::Iterator AddressBook::end() 421AddressBook::Iterator AddressBook::end()
422{ 422{
423 Iterator it = Iterator(); 423 Iterator it = Iterator();
424 it.d->mIt = d->mAddressees.end(); 424 it.d->mIt = d->mAddressees.end();
425 return it; 425 return it;
426} 426}
427 427
428AddressBook::ConstIterator AddressBook::end() const 428AddressBook::ConstIterator AddressBook::end() const
429{ 429{
430 ConstIterator it = ConstIterator(); 430 ConstIterator it = ConstIterator();
431 it.d->mIt = d->mAddressees.end(); 431 it.d->mIt = d->mAddressees.end();
432 return it; 432 return it;
433} 433}
434 434
435void AddressBook::clear() 435void AddressBook::clear()
436{ 436{
437 d->mAddressees.clear(); 437 d->mAddressees.clear();
438} 438}
439 439
440Ticket *AddressBook::requestSaveTicket( Resource *resource ) 440Ticket *AddressBook::requestSaveTicket( Resource *resource )
441{ 441{
442 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; 442 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl;
443 443
444 if ( !resource ) 444 if ( !resource )
445 { 445 {
446 qDebug("AddressBook::requestSaveTicket no resource" ); 446 qDebug("AddressBook::requestSaveTicket no resource" );
447 resource = standardResource(); 447 resource = standardResource();
448 } 448 }
449 449
450 KRES::Manager<Resource>::ActiveIterator it; 450 KRES::Manager<Resource>::ActiveIterator it;
451 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 451 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
452 if ( (*it) == resource ) { 452 if ( (*it) == resource ) {
453 if ( (*it)->readOnly() || !(*it)->isOpen() ) 453 if ( (*it)->readOnly() || !(*it)->isOpen() )
454 return 0; 454 return 0;
455 else 455 else
456 return (*it)->requestSaveTicket(); 456 return (*it)->requestSaveTicket();
457 } 457 }
458 } 458 }
459 459
460 return 0; 460 return 0;
461} 461}
462 462
463void AddressBook::insertAddressee( const Addressee &a, bool setRev ) 463void AddressBook::insertAddressee( const Addressee &a, bool setRev )
464{ 464{
465 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { 465 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) {
466 //qDebug("block insert "); 466 //qDebug("block insert ");
467 return; 467 return;
468 } 468 }
469 //qDebug("inserting.... %s ",a.uid().latin1() ); 469 //qDebug("inserting.... %s ",a.uid().latin1() );
470 bool found = false; 470 bool found = false;
471 Addressee::List::Iterator it; 471 Addressee::List::Iterator it;
472 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { 472 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) {
473 if ( a.uid() == (*it).uid() ) { 473 if ( a.uid() == (*it).uid() ) {
474 474
475 bool changed = false; 475 bool changed = false;
476 Addressee addr = a; 476 Addressee addr = a;
477 if ( addr != (*it) ) 477 if ( addr != (*it) )
478 changed = true; 478 changed = true;
479 479
480 (*it) = a; 480 (*it) = a;
481 if ( (*it).resource() == 0 ) 481 if ( (*it).resource() == 0 )
482 (*it).setResource( standardResource() ); 482 (*it).setResource( standardResource() );
483 483
484 if ( changed ) { 484 if ( changed ) {
485 if ( setRev ) { 485 if ( setRev ) {
486 486
487 // get rid of micro seconds 487 // get rid of micro seconds
488 QDateTime dt = QDateTime::currentDateTime(); 488 QDateTime dt = QDateTime::currentDateTime();
489 QTime t = dt.time(); 489 QTime t = dt.time();
490 dt.setTime( QTime (t.hour (), t.minute (), t.second () ) ); 490 dt.setTime( QTime (t.hour (), t.minute (), t.second () ) );
491 (*it).setRevision( dt ); 491 (*it).setRevision( dt );
492 } 492 }
493 (*it).setChanged( true ); 493 (*it).setChanged( true );
494 } 494 }
495 495
496 found = true; 496 found = true;
497 } else { 497 } else {
498 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 498 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) {
499 QString name = (*it).uid().mid( 19 ); 499 QString name = (*it).uid().mid( 19 );
500 Addressee b = a; 500 Addressee b = a;
501 QString id = b.getID( name ); 501 QString id = b.getID( name );
502 if ( ! id.isEmpty() ) { 502 if ( ! id.isEmpty() ) {
503 QString des = (*it).note(); 503 QString des = (*it).note();
504 int startN; 504 int startN;
505 if( (startN = des.find( id ) ) >= 0 ) { 505 if( (startN = des.find( id ) ) >= 0 ) {
506 int endN = des.find( ",", startN+1 ); 506 int endN = des.find( ",", startN+1 );
507 des = des.left( startN ) + des.mid( endN+1 ); 507 des = des.left( startN ) + des.mid( endN+1 );
508 (*it).setNote( des ); 508 (*it).setNote( des );
509 } 509 }
510 } 510 }
511 } 511 }
512 } 512 }
513 } 513 }
514 if ( found ) 514 if ( found )
515 return; 515 return;
516 d->mAddressees.append( a ); 516 d->mAddressees.append( a );
517 Addressee& addr = d->mAddressees.last(); 517 Addressee& addr = d->mAddressees.last();
518 if ( addr.resource() == 0 ) 518 if ( addr.resource() == 0 )
519 addr.setResource( standardResource() ); 519 addr.setResource( standardResource() );
520 520
521 addr.setChanged( true ); 521 addr.setChanged( true );
522} 522}
523 523
524void AddressBook::removeAddressee( const Addressee &a ) 524void AddressBook::removeAddressee( const Addressee &a )
525{ 525{
526 Iterator it; 526 Iterator it;
527 Iterator it2; 527 Iterator it2;
528 bool found = false; 528 bool found = false;
529 for ( it = begin(); it != end(); ++it ) { 529 for ( it = begin(); it != end(); ++it ) {
530 if ( a.uid() == (*it).uid() ) { 530 if ( a.uid() == (*it).uid() ) {
531 found = true; 531 found = true;
532 it2 = it; 532 it2 = it;
533 } else { 533 } else {
534 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 534 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) {
535 QString name = (*it).uid().mid( 19 ); 535 QString name = (*it).uid().mid( 19 );
536 Addressee b = a; 536 Addressee b = a;
537 QString id = b.getID( name ); 537 QString id = b.getID( name );
538 if ( ! id.isEmpty() ) { 538 if ( ! id.isEmpty() ) {
539 QString des = (*it).note(); 539 QString des = (*it).note();
540 if( des.find( id ) < 0 ) { 540 if( des.find( id ) < 0 ) {
541 des += id + ","; 541 des += id + ",";
542 (*it).setNote( des ); 542 (*it).setNote( des );
543 } 543 }
544 } 544 }
545 } 545 }
546 546
547 } 547 }
548 } 548 }
549 549
550 if ( found ) 550 if ( found )
551 removeAddressee( it2 ); 551 removeAddressee( it2 );
552 552
553} 553}
554 554
555void AddressBook::removeDeletedAddressees() 555void AddressBook::removeDeletedAddressees()
556{ 556{
557 deleteRemovedAddressees(); 557 deleteRemovedAddressees();
558 Iterator it = begin(); 558 Iterator it = begin();
559 Iterator it2 ; 559 Iterator it2 ;
560 QDateTime dt ( QDate( 2004,1,1) ); 560 QDateTime dt ( QDate( 2004,1,1) );
561 while ( it != end() ) { 561 while ( it != end() ) {
562 (*it).setRevision( dt ); 562 (*it).setRevision( dt );
563 (*it).removeCustom( "KADDRESSBOOK", "X-ExternalID" ); 563 (*it).removeCustom( "KADDRESSBOOK", "X-ExternalID" );
564 (*it).setIDStr(""); 564 (*it).setIDStr("");
565 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE || (*it).uid().left( 19 ) == QString("last-syncAddressee-")) { 565 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE || (*it).uid().left( 19 ) == QString("last-syncAddressee-")) {
566 it2 = it; 566 it2 = it;
567 //qDebug("removing %s ",(*it).uid().latin1() ); 567 //qDebug("removing %s ",(*it).uid().latin1() );
568 ++it; 568 ++it;
569 removeAddressee( it2 ); 569 removeAddressee( it2 );
570 } else { 570 } else {
571 //qDebug("skipping %s ",(*it).uid().latin1() ); 571 //qDebug("skipping %s ",(*it).uid().latin1() );
572 ++it; 572 ++it;
573 } 573 }
574 } 574 }
575 deleteRemovedAddressees(); 575 deleteRemovedAddressees();
576} 576}
577 577
578void AddressBook::removeAddressee( const Iterator &it ) 578void AddressBook::removeAddressee( const Iterator &it )
579{ 579{
580 d->mRemovedAddressees.append( (*it) ); 580 d->mRemovedAddressees.append( (*it) );
581 d->mAddressees.remove( it.d->mIt ); 581 d->mAddressees.remove( it.d->mIt );
582} 582}
583 583
584AddressBook::Iterator AddressBook::find( const Addressee &a ) 584AddressBook::Iterator AddressBook::find( const Addressee &a )
585{ 585{
586 Iterator it; 586 Iterator it;
587 for ( it = begin(); it != end(); ++it ) { 587 for ( it = begin(); it != end(); ++it ) {
588 if ( a.uid() == (*it).uid() ) { 588 if ( a.uid() == (*it).uid() ) {
589 return it; 589 return it;
590 } 590 }
591 } 591 }
592 return end(); 592 return end();
593} 593}
594 594
595Addressee AddressBook::findByUid( const QString &uid ) 595Addressee AddressBook::findByUid( const QString &uid )
596{ 596{
597 Iterator it; 597 Iterator it;
598 for ( it = begin(); it != end(); ++it ) { 598 for ( it = begin(); it != end(); ++it ) {
599 if ( uid == (*it).uid() ) { 599 if ( uid == (*it).uid() ) {
600 return *it; 600 return *it;
601 } 601 }
602 } 602 }
603 return Addressee(); 603 return Addressee();
604} 604}
605void AddressBook::preExternSync( AddressBook* aBook, const QString& csd )
606{
607 qDebug("AddressBook::preExternSync ");
608 AddressBook::Iterator it;
609 for ( it = begin(); it != end(); ++it ) {
610 (*it).setID( csd, (*it).externalUID() );
611 (*it).computeCsum( csd );
612 }
613 mergeAB( aBook ,csd );
614}
615void AddressBook::postExternSync( AddressBook* aBook , const QString& csd)
616{
617 qDebug("AddressBook::postExternSync ");
618 AddressBook::Iterator it;
619 for ( it = begin(); it != end(); ++it ) {
620 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID ||
621 (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_CSUM ) {
622 Addressee ad = aBook->findByUid( ( (*it).uid() ));
623 if ( ad.isEmpty() ) {
624 qDebug("ERROR ad empty ");
625 } else {
626 (*it).computeCsum( csd );
627 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID )
628 ad.setID( csd, (*it).externalUID() );
629 ad.setCsum( csd, (*it).getCsum( csd ) );
630 aBook->insertAddressee( ad );
631 }
632 }
633 }
634}
635
636
605Addressee AddressBook::findByExternUid( const QString& uid , const QString& profile ) 637Addressee AddressBook::findByExternUid( const QString& uid , const QString& profile )
606{ 638{
607 Iterator it; 639 Iterator it;
608 for ( it = begin(); it != end(); ++it ) { 640 for ( it = begin(); it != end(); ++it ) {
609 if ( uid == (*it).getID( profile ) ) 641 if ( uid == (*it).getID( profile ) )
610 return (*it); 642 return (*it);
611 } 643 }
612 return Addressee(); 644 return Addressee();
613} 645}
614void AddressBook::mergeAB( AddressBook *aBook, const QString& profile ) 646void AddressBook::mergeAB( AddressBook *aBook, const QString& profile )
615{ 647{
616 Iterator it; 648 Iterator it;
617 Addressee ad; 649 Addressee ad;
618 for ( it = begin(); it != end(); ++it ) { 650 for ( it = begin(); it != end(); ++it ) {
619 ad = aBook->findByExternUid( (*it).externalUID(), profile ); 651 ad = aBook->findByExternUid( (*it).externalUID(), profile );
620 if ( !ad.isEmpty() ) { 652 if ( !ad.isEmpty() ) {
621 (*it).mergeContact( ad ); 653 (*it).mergeContact( ad );
622 } 654 }
623 } 655 }
624} 656}
625 657
626#if 0 658#if 0
627Addressee::List AddressBook::getExternLastSyncAddressees() 659Addressee::List AddressBook::getExternLastSyncAddressees()
628{ 660{
629 Addressee::List results; 661 Addressee::List results;
630 662
631 Iterator it; 663 Iterator it;
632 for ( it = begin(); it != end(); ++it ) { 664 for ( it = begin(); it != end(); ++it ) {
633 if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) { 665 if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) {
634 if ( (*it).familyName().left(4) == "!E: " ) 666 if ( (*it).familyName().left(4) == "!E: " )
635 results.append( *it ); 667 results.append( *it );
636 } 668 }
637 } 669 }
638 670
639 return results; 671 return results;
640} 672}
641#endif 673#endif
642void AddressBook::resetTempSyncStat() 674void AddressBook::resetTempSyncStat()
643{ 675{
644 Iterator it; 676 Iterator it;
645 for ( it = begin(); it != end(); ++it ) { 677 for ( it = begin(); it != end(); ++it ) {
646 (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL ); 678 (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL );
647 } 679 }
648 680
649} 681}
650 682
651QStringList AddressBook:: uidList() 683QStringList AddressBook:: uidList()
652{ 684{
653 QStringList results; 685 QStringList results;
654 Iterator it; 686 Iterator it;
655 for ( it = begin(); it != end(); ++it ) { 687 for ( it = begin(); it != end(); ++it ) {
656 results.append( (*it).uid() ); 688 results.append( (*it).uid() );
657 } 689 }
658 return results; 690 return results;
659} 691}
660 692
661 693
662Addressee::List AddressBook::allAddressees() 694Addressee::List AddressBook::allAddressees()
663{ 695{
664 return d->mAddressees; 696 return d->mAddressees;
665 697
666} 698}
667 699
668Addressee::List AddressBook::findByName( const QString &name ) 700Addressee::List AddressBook::findByName( const QString &name )
669{ 701{
670 Addressee::List results; 702 Addressee::List results;
671 703
672 Iterator it; 704 Iterator it;
673 for ( it = begin(); it != end(); ++it ) { 705 for ( it = begin(); it != end(); ++it ) {
674 if ( name == (*it).realName() ) { 706 if ( name == (*it).realName() ) {
675 results.append( *it ); 707 results.append( *it );
676 } 708 }
677 } 709 }
678 710
679 return results; 711 return results;
680} 712}
681 713
682Addressee::List AddressBook::findByEmail( const QString &email ) 714Addressee::List AddressBook::findByEmail( const QString &email )
683{ 715{
684 Addressee::List results; 716 Addressee::List results;
685 QStringList mailList; 717 QStringList mailList;
686 718
687 Iterator it; 719 Iterator it;
688 for ( it = begin(); it != end(); ++it ) { 720 for ( it = begin(); it != end(); ++it ) {
689 mailList = (*it).emails(); 721 mailList = (*it).emails();
690 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) { 722 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) {
691 if ( email == (*ite) ) { 723 if ( email == (*ite) ) {
692 results.append( *it ); 724 results.append( *it );
693 } 725 }
694 } 726 }
695 } 727 }
696 728
697 return results; 729 return results;
698} 730}
699 731
700Addressee::List AddressBook::findByCategory( const QString &category ) 732Addressee::List AddressBook::findByCategory( const QString &category )
701{ 733{
702 Addressee::List results; 734 Addressee::List results;
703 735
704 Iterator it; 736 Iterator it;
705 for ( it = begin(); it != end(); ++it ) { 737 for ( it = begin(); it != end(); ++it ) {
706 if ( (*it).hasCategory( category) ) { 738 if ( (*it).hasCategory( category) ) {
707 results.append( *it ); 739 results.append( *it );
708 } 740 }
709 } 741 }
710 742
711 return results; 743 return results;
712} 744}
713 745
714void AddressBook::dump() const 746void AddressBook::dump() const
715{ 747{
716 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl; 748 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl;
717 749
718 ConstIterator it; 750 ConstIterator it;
719 for( it = begin(); it != end(); ++it ) { 751 for( it = begin(); it != end(); ++it ) {
720 (*it).dump(); 752 (*it).dump();
721 } 753 }
722 754
723 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl; 755 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl;
724} 756}
725 757
726QString AddressBook::identifier() 758QString AddressBook::identifier()
727{ 759{
728 QStringList identifier; 760 QStringList identifier;
729 761
730 762
731 KRES::Manager<Resource>::ActiveIterator it; 763 KRES::Manager<Resource>::ActiveIterator it;
732 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 764 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
733 if ( !(*it)->identifier().isEmpty() ) 765 if ( !(*it)->identifier().isEmpty() )
734 identifier.append( (*it)->identifier() ); 766 identifier.append( (*it)->identifier() );
735 } 767 }
736 768
737 return identifier.join( ":" ); 769 return identifier.join( ":" );
738} 770}
739 771
740Field::List AddressBook::fields( int category ) 772Field::List AddressBook::fields( int category )
741{ 773{
742 if ( d->mAllFields.isEmpty() ) { 774 if ( d->mAllFields.isEmpty() ) {
743 d->mAllFields = Field::allFields(); 775 d->mAllFields = Field::allFields();
744 } 776 }
745 777
746 if ( category == Field::All ) return d->mAllFields; 778 if ( category == Field::All ) return d->mAllFields;
747 779
748 Field::List result; 780 Field::List result;
749 Field::List::ConstIterator it; 781 Field::List::ConstIterator it;
750 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) { 782 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) {
751 if ( (*it)->category() & category ) result.append( *it ); 783 if ( (*it)->category() & category ) result.append( *it );
752 } 784 }
753 785
754 return result; 786 return result;
755} 787}
756 788
757bool AddressBook::addCustomField( const QString &label, int category, 789bool AddressBook::addCustomField( const QString &label, int category,
758 const QString &key, const QString &app ) 790 const QString &key, const QString &app )
759{ 791{
760 if ( d->mAllFields.isEmpty() ) { 792 if ( d->mAllFields.isEmpty() ) {
761 d->mAllFields = Field::allFields(); 793 d->mAllFields = Field::allFields();
762 } 794 }
763//US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app; 795//US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app;
764 QString a = app.isNull() ? KGlobal::getAppName() : app; 796 QString a = app.isNull() ? KGlobal::getAppName() : app;
765 797
766 QString k = key.isNull() ? label : key; 798 QString k = key.isNull() ? label : key;
767 799
768 Field *field = Field::createCustomField( label, category, k, a ); 800 Field *field = Field::createCustomField( label, category, k, a );
769 801
770 if ( !field ) return false; 802 if ( !field ) return false;
771 803
772 d->mAllFields.append( field ); 804 d->mAllFields.append( field );
773 805
774 return true; 806 return true;
775} 807}
776 808
777QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab ) 809QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab )
778{ 810{
779 if (!ab.d) return s; 811 if (!ab.d) return s;
780 812
781 return s << ab.d->mAddressees; 813 return s << ab.d->mAddressees;
782} 814}
783 815
784QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab ) 816QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab )
785{ 817{
786 if (!ab.d) return s; 818 if (!ab.d) return s;
787 819
788 s >> ab.d->mAddressees; 820 s >> ab.d->mAddressees;
789 821
790 return s; 822 return s;
791} 823}
792 824
793bool AddressBook::addResource( Resource *resource ) 825bool AddressBook::addResource( Resource *resource )
794{ 826{
795 if ( !resource->open() ) { 827 if ( !resource->open() ) {
796 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl; 828 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl;
797 return false; 829 return false;
798 } 830 }
799 831
800 resource->setAddressBook( this ); 832 resource->setAddressBook( this );
801 833
802 d->mManager->add( resource ); 834 d->mManager->add( resource );
803 return true; 835 return true;
804} 836}
805 837
806bool AddressBook::removeResource( Resource *resource ) 838bool AddressBook::removeResource( Resource *resource )
807{ 839{
808 resource->close(); 840 resource->close();
809 841
810 if ( resource == standardResource() ) 842 if ( resource == standardResource() )
811 d->mManager->setStandardResource( 0 ); 843 d->mManager->setStandardResource( 0 );
812 844
813 resource->setAddressBook( 0 ); 845 resource->setAddressBook( 0 );
814 846
815 d->mManager->remove( resource ); 847 d->mManager->remove( resource );
816 return true; 848 return true;
817} 849}
818 850
819QPtrList<Resource> AddressBook::resources() 851QPtrList<Resource> AddressBook::resources()
820{ 852{
821 QPtrList<Resource> list; 853 QPtrList<Resource> list;
822 854
823// qDebug("AddressBook::resources() 1"); 855// qDebug("AddressBook::resources() 1");
824 856
825 KRES::Manager<Resource>::ActiveIterator it; 857 KRES::Manager<Resource>::ActiveIterator it;
826 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 858 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
827 list.append( *it ); 859 list.append( *it );
828 860
829 return list; 861 return list;
830} 862}
831 863
832/*US 864/*US
833void AddressBook::setErrorHandler( ErrorHandler *handler ) 865void AddressBook::setErrorHandler( ErrorHandler *handler )
834{ 866{
835 delete d->mErrorHandler; 867 delete d->mErrorHandler;
836 d->mErrorHandler = handler; 868 d->mErrorHandler = handler;
837} 869}
838*/ 870*/
839 871
840void AddressBook::error( const QString& msg ) 872void AddressBook::error( const QString& msg )
841{ 873{
842/*US 874/*US
843 if ( !d->mErrorHandler ) // create default error handler 875 if ( !d->mErrorHandler ) // create default error handler
844 d->mErrorHandler = new ConsoleErrorHandler; 876 d->mErrorHandler = new ConsoleErrorHandler;
845 877
846 if ( d->mErrorHandler ) 878 if ( d->mErrorHandler )
847 d->mErrorHandler->error( msg ); 879 d->mErrorHandler->error( msg );
848 else 880 else
849 kdError(5700) << "no error handler defined" << endl; 881 kdError(5700) << "no error handler defined" << endl;
850*/ 882*/
851 kdDebug(5700) << "msg" << endl; 883 kdDebug(5700) << "msg" << endl;
852 qDebug(msg); 884 qDebug(msg);
853} 885}
854 886
855void AddressBook::deleteRemovedAddressees() 887void AddressBook::deleteRemovedAddressees()
856{ 888{
857 Addressee::List::Iterator it; 889 Addressee::List::Iterator it;
858 for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) { 890 for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) {
859 Resource *resource = (*it).resource(); 891 Resource *resource = (*it).resource();
860 if ( resource && !resource->readOnly() && resource->isOpen() ) 892 if ( resource && !resource->readOnly() && resource->isOpen() )
861 resource->removeAddressee( *it ); 893 resource->removeAddressee( *it );
862 } 894 }
863 895
864 d->mRemovedAddressees.clear(); 896 d->mRemovedAddressees.clear();
865} 897}
866 898
867void AddressBook::setStandardResource( Resource *resource ) 899void AddressBook::setStandardResource( Resource *resource )
868{ 900{
869// qDebug("AddressBook::setStandardResource 1"); 901// qDebug("AddressBook::setStandardResource 1");
870 d->mManager->setStandardResource( resource ); 902 d->mManager->setStandardResource( resource );
871} 903}
872 904
873Resource *AddressBook::standardResource() 905Resource *AddressBook::standardResource()
874{ 906{
875 return d->mManager->standardResource(); 907 return d->mManager->standardResource();
876} 908}
877 909
878KRES::Manager<Resource> *AddressBook::resourceManager() 910KRES::Manager<Resource> *AddressBook::resourceManager()
879{ 911{
880 return d->mManager; 912 return d->mManager;
881} 913}
882 914
883void AddressBook::cleanUp() 915void AddressBook::cleanUp()
884{ 916{
885 KRES::Manager<Resource>::ActiveIterator it; 917 KRES::Manager<Resource>::ActiveIterator it;
886 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 918 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
887 if ( !(*it)->readOnly() && (*it)->isOpen() ) 919 if ( !(*it)->readOnly() && (*it)->isOpen() )
888 (*it)->cleanUp(); 920 (*it)->cleanUp();
889 } 921 }
890} 922}