-rw-r--r-- | kabc/addressbook.cpp | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp index 4c4ae09..8487ff3 100644 --- a/kabc/addressbook.cpp +++ b/kabc/addressbook.cpp | |||
@@ -395,854 +395,865 @@ void AddressBook::export2File( QString fileName ) | |||
395 | if ( (*it).resource() && (*it).resource()->includeInSync() ) { | 395 | if ( (*it).resource() && (*it).resource()->includeInSync() ) { |
396 | if ( !(*it).IDStr().isEmpty() ) { | 396 | if ( !(*it).IDStr().isEmpty() ) { |
397 | (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() ); | 397 | (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() ); |
398 | } | 398 | } |
399 | KABC::VCardConverter converter; | 399 | KABC::VCardConverter converter; |
400 | QString vcard; | 400 | QString vcard; |
401 | //Resource *resource() const; | 401 | //Resource *resource() const; |
402 | converter.addresseeToVCard( *it, vcard, version ); | 402 | converter.addresseeToVCard( *it, vcard, version ); |
403 | t << vcard << "\r\n"; | 403 | t << vcard << "\r\n"; |
404 | } | 404 | } |
405 | } | 405 | } |
406 | t << "\r\n\r\n"; | 406 | t << "\r\n\r\n"; |
407 | outFile.close(); | 407 | outFile.close(); |
408 | } | 408 | } |
409 | // if QStringList uids is empty, all are exported | 409 | // if QStringList uids is empty, all are exported |
410 | bool AddressBook::export2PhoneFormat( QStringList uids ,QString fileName ) | 410 | bool AddressBook::export2PhoneFormat( QStringList uids ,QString fileName ) |
411 | { | 411 | { |
412 | KABC::VCardConverter converter; | 412 | KABC::VCardConverter converter; |
413 | QString datastream; | 413 | QString datastream; |
414 | Iterator it; | 414 | Iterator it; |
415 | bool all = uids.isEmpty(); | 415 | bool all = uids.isEmpty(); |
416 | for ( it = begin(); it != end(); ++it ) { | 416 | for ( it = begin(); it != end(); ++it ) { |
417 | // for( QStringList::ConstIterator it = uids.begin(); it != uids.end(); ++it ) { | 417 | // for( QStringList::ConstIterator it = uids.begin(); it != uids.end(); ++it ) { |
418 | if ( ! all ) { | 418 | if ( ! all ) { |
419 | if ( ! ( uids.contains((*it).uid() ) )) | 419 | if ( ! ( uids.contains((*it).uid() ) )) |
420 | continue; | 420 | continue; |
421 | } | 421 | } |
422 | KABC::Addressee a = ( *it ); | 422 | KABC::Addressee a = ( *it ); |
423 | if ( a.isEmpty() ) | 423 | if ( a.isEmpty() ) |
424 | continue; | 424 | continue; |
425 | if ( all && a.resource() && !a.resource()->includeInSync() ) | 425 | if ( all && a.resource() && !a.resource()->includeInSync() ) |
426 | continue; | 426 | continue; |
427 | a.simplifyEmails(); | 427 | a.simplifyEmails(); |
428 | a.simplifyPhoneNumbers(); | 428 | a.simplifyPhoneNumbers(); |
429 | a.simplifyPhoneNumberTypes(); | 429 | a.simplifyPhoneNumberTypes(); |
430 | a.simplifyAddresses(); | 430 | a.simplifyAddresses(); |
431 | 431 | ||
432 | QString vcard; | 432 | QString vcard; |
433 | QString vcardnew; | 433 | QString vcardnew; |
434 | converter.addresseeToVCard( a, vcard ); | 434 | converter.addresseeToVCard( a, vcard ); |
435 | int start = 0; | 435 | int start = 0; |
436 | int next; | 436 | int next; |
437 | while ( (next = vcard.find("TYPE=", start) )>= 0 ) { | 437 | while ( (next = vcard.find("TYPE=", start) )>= 0 ) { |
438 | int semi = vcard.find(";", next); | 438 | int semi = vcard.find(";", next); |
439 | int dopp = vcard.find(":", next); | 439 | int dopp = vcard.find(":", next); |
440 | int sep; | 440 | int sep; |
441 | if ( semi < dopp && semi >= 0 ) | 441 | if ( semi < dopp && semi >= 0 ) |
442 | sep = semi ; | 442 | sep = semi ; |
443 | else | 443 | else |
444 | sep = dopp; | 444 | sep = dopp; |
445 | vcardnew +=vcard.mid( start, next - start); | 445 | vcardnew +=vcard.mid( start, next - start); |
446 | vcardnew +=vcard.mid( next+5,sep -next -5 ).upper(); | 446 | vcardnew +=vcard.mid( next+5,sep -next -5 ).upper(); |
447 | start = sep; | 447 | start = sep; |
448 | } | 448 | } |
449 | vcardnew += vcard.mid( start,vcard.length() ); | 449 | vcardnew += vcard.mid( start,vcard.length() ); |
450 | vcard = ""; | 450 | vcard = ""; |
451 | start = 0; | 451 | start = 0; |
452 | while ( (next = vcardnew.find("ADR", start) )>= 0 ) { | 452 | while ( (next = vcardnew.find("ADR", start) )>= 0 ) { |
453 | int sep = vcardnew.find(":", next); | 453 | int sep = vcardnew.find(":", next); |
454 | vcard +=vcardnew.mid( start, next - start+3); | 454 | vcard +=vcardnew.mid( start, next - start+3); |
455 | start = sep; | 455 | start = sep; |
456 | } | 456 | } |
457 | vcard += vcardnew.mid( start,vcardnew.length() ); | 457 | vcard += vcardnew.mid( start,vcardnew.length() ); |
458 | vcard.replace ( QRegExp(";;;") , "" ); | 458 | vcard.replace ( QRegExp(";;;") , "" ); |
459 | vcard.replace ( QRegExp(";;") , "" ); | 459 | vcard.replace ( QRegExp(";;") , "" ); |
460 | datastream += vcard; | 460 | datastream += vcard; |
461 | 461 | ||
462 | } | 462 | } |
463 | 463 | ||
464 | QFile outFile(fileName); | 464 | QFile outFile(fileName); |
465 | if ( outFile.open(IO_WriteOnly) ) { | 465 | if ( outFile.open(IO_WriteOnly) ) { |
466 | datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" ); | 466 | datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" ); |
467 | QTextStream t( &outFile ); // use a text stream | 467 | QTextStream t( &outFile ); // use a text stream |
468 | t.setEncoding( QTextStream::UnicodeUTF8 ); | 468 | t.setEncoding( QTextStream::UnicodeUTF8 ); |
469 | t <<datastream; | 469 | t <<datastream; |
470 | t << "\r\n\r\n"; | 470 | t << "\r\n\r\n"; |
471 | outFile.close(); | 471 | outFile.close(); |
472 | 472 | ||
473 | } else { | 473 | } else { |
474 | qDebug("Error open temp file "); | 474 | qDebug("Error open temp file "); |
475 | return false; | 475 | return false; |
476 | } | 476 | } |
477 | return true; | 477 | return true; |
478 | 478 | ||
479 | } | 479 | } |
480 | int AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld ) | 480 | int AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld ) |
481 | { | 481 | { |
482 | 482 | ||
483 | if ( removeOld ) | 483 | if ( removeOld ) |
484 | setUntagged( true ); | 484 | setUntagged( true ); |
485 | KABC::Addressee::List list; | 485 | KABC::Addressee::List list; |
486 | QFile file( fileName ); | 486 | QFile file( fileName ); |
487 | file.open( IO_ReadOnly ); | 487 | file.open( IO_ReadOnly ); |
488 | QByteArray rawData = file.readAll(); | 488 | QByteArray rawData = file.readAll(); |
489 | file.close(); | 489 | file.close(); |
490 | QString data; | 490 | QString data; |
491 | if ( replaceLabel ) { | 491 | if ( replaceLabel ) { |
492 | data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); | 492 | data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); |
493 | data.replace ( QRegExp("LABEL") , "ADR" ); | 493 | data.replace ( QRegExp("LABEL") , "ADR" ); |
494 | data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); | 494 | data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); |
495 | } else | 495 | } else |
496 | data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); | 496 | data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); |
497 | KABC::VCardTool tool; | 497 | KABC::VCardTool tool; |
498 | list = tool.parseVCards( data ); | 498 | list = tool.parseVCards( data ); |
499 | KABC::Addressee::List::Iterator it; | 499 | KABC::Addressee::List::Iterator it; |
500 | for ( it = list.begin(); it != list.end(); ++it ) { | 500 | for ( it = list.begin(); it != list.end(); ++it ) { |
501 | QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); | 501 | QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); |
502 | if ( !id.isEmpty() ) | 502 | if ( !id.isEmpty() ) |
503 | (*it).setIDStr(id ); | 503 | (*it).setIDStr(id ); |
504 | (*it).setResource( 0 ); | 504 | (*it).setResource( 0 ); |
505 | if ( replaceLabel ) | 505 | if ( replaceLabel ) |
506 | (*it).removeVoice(); | 506 | (*it).removeVoice(); |
507 | if ( removeOld ) | 507 | if ( removeOld ) |
508 | (*it).setTagged( true ); | 508 | (*it).setTagged( true ); |
509 | insertAddressee( (*it), false, true ); | 509 | insertAddressee( (*it), false, true ); |
510 | } | 510 | } |
511 | if ( removeOld ) | 511 | if ( removeOld ) |
512 | removeUntagged(); | 512 | removeUntagged(); |
513 | return list.count(); | 513 | return list.count(); |
514 | } | 514 | } |
515 | void AddressBook::setUntagged(bool setNonSyncTagged) // = false) | 515 | void AddressBook::setUntagged(bool setNonSyncTagged) // = false) |
516 | { | 516 | { |
517 | Iterator ait; | 517 | Iterator ait; |
518 | for ( ait = begin(); ait != end(); ++ait ) { | 518 | for ( ait = begin(); ait != end(); ++ait ) { |
519 | if ( setNonSyncTagged ) { | 519 | if ( setNonSyncTagged ) { |
520 | if ( (*ait).resource() && ! (*ait).resource()->includeInSync() ) { | 520 | if ( (*ait).resource() && ! (*ait).resource()->includeInSync() ) { |
521 | (*ait).setTagged( true ); | 521 | (*ait).setTagged( true ); |
522 | } else | 522 | } else |
523 | (*ait).setTagged( false ); | 523 | (*ait).setTagged( false ); |
524 | } else | 524 | } else |
525 | (*ait).setTagged( false ); | 525 | (*ait).setTagged( false ); |
526 | } | 526 | } |
527 | } | 527 | } |
528 | void AddressBook::removeUntagged() | 528 | void AddressBook::removeUntagged() |
529 | { | 529 | { |
530 | Iterator ait; | 530 | Iterator ait; |
531 | bool todelete = false; | 531 | bool todelete = false; |
532 | Iterator todel; | 532 | Iterator todel; |
533 | for ( ait = begin(); ait != end(); ++ait ) { | 533 | for ( ait = begin(); ait != end(); ++ait ) { |
534 | if ( todelete ) | 534 | if ( todelete ) |
535 | removeAddressee( todel ); | 535 | removeAddressee( todel ); |
536 | if (!(*ait).tagged()) { | 536 | if (!(*ait).tagged()) { |
537 | todelete = true; | 537 | todelete = true; |
538 | todel = ait; | 538 | todel = ait; |
539 | } else | 539 | } else |
540 | todelete = false; | 540 | todelete = false; |
541 | } | 541 | } |
542 | if ( todelete ) | 542 | if ( todelete ) |
543 | removeAddressee( todel ); | 543 | removeAddressee( todel ); |
544 | deleteRemovedAddressees(); | 544 | deleteRemovedAddressees(); |
545 | } | 545 | } |
546 | void AddressBook::smplifyAddressees() | 546 | void AddressBook::smplifyAddressees() |
547 | { | 547 | { |
548 | Iterator ait; | 548 | Iterator ait; |
549 | for ( ait = begin(); ait != end(); ++ait ) { | 549 | for ( ait = begin(); ait != end(); ++ait ) { |
550 | (*ait).simplifyEmails(); | 550 | (*ait).simplifyEmails(); |
551 | (*ait).simplifyPhoneNumbers(); | 551 | (*ait).simplifyPhoneNumbers(); |
552 | (*ait).simplifyPhoneNumberTypes(); | 552 | (*ait).simplifyPhoneNumberTypes(); |
553 | (*ait).simplifyAddresses(); | 553 | (*ait).simplifyAddresses(); |
554 | } | 554 | } |
555 | } | 555 | } |
556 | void AddressBook::removeSyncInfo( QString syncProfile) | 556 | void AddressBook::removeSyncInfo( QString syncProfile) |
557 | { | 557 | { |
558 | Iterator ait; | 558 | Iterator ait; |
559 | for ( ait = begin(); ait != end(); ++ait ) { | 559 | for ( ait = begin(); ait != end(); ++ait ) { |
560 | (*ait).removeID( syncProfile ); | 560 | (*ait).removeID( syncProfile ); |
561 | } | 561 | } |
562 | if ( syncProfile.isEmpty() ) { | 562 | if ( syncProfile.isEmpty() ) { |
563 | Iterator it = begin(); | 563 | Iterator it = begin(); |
564 | Iterator it2 ; | 564 | Iterator it2 ; |
565 | while ( it != end() ) { | 565 | while ( it != end() ) { |
566 | if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { | 566 | if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { |
567 | it2 = it; | 567 | it2 = it; |
568 | //qDebug("removing %s ",(*it).uid().latin1() ); | 568 | //qDebug("removing %s ",(*it).uid().latin1() ); |
569 | ++it; | 569 | ++it; |
570 | removeAddressee( it2 ); | 570 | removeAddressee( it2 ); |
571 | } else { | 571 | } else { |
572 | //qDebug("skipping %s ",(*it).uid().latin1() ); | 572 | //qDebug("skipping %s ",(*it).uid().latin1() ); |
573 | ++it; | 573 | ++it; |
574 | } | 574 | } |
575 | } | 575 | } |
576 | } else { | 576 | } else { |
577 | Addressee lse; | 577 | Addressee lse; |
578 | lse = findByUid( "last-syncAddressee-"+ syncProfile ); | 578 | lse = findByUid( "last-syncAddressee-"+ syncProfile ); |
579 | if ( ! lse.isEmpty() ) | 579 | if ( ! lse.isEmpty() ) |
580 | removeAddressee( lse ); | 580 | removeAddressee( lse ); |
581 | } | 581 | } |
582 | 582 | ||
583 | } | 583 | } |
584 | void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync ) | 584 | void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync ) |
585 | { | 585 | { |
586 | Iterator ait; | 586 | Iterator ait; |
587 | for ( ait = begin(); ait != end(); ++ait ) { | 587 | for ( ait = begin(); ait != end(); ++ait ) { |
588 | QString id = (*ait).IDStr(); | 588 | QString id = (*ait).IDStr(); |
589 | (*ait).setIDStr( ":"); | 589 | (*ait).setIDStr( ":"); |
590 | (*ait).setExternalUID( id ); | 590 | (*ait).setExternalUID( id ); |
591 | (*ait).setOriginalExternalUID( id ); | 591 | (*ait).setOriginalExternalUID( id ); |
592 | if ( isPreSync ) | 592 | if ( isPreSync ) |
593 | (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL ); | 593 | (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL ); |
594 | else { | 594 | else { |
595 | (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); | 595 | (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); |
596 | (*ait).setID( currentSyncDevice,id ); | 596 | (*ait).setID( currentSyncDevice,id ); |
597 | 597 | ||
598 | } | 598 | } |
599 | } | 599 | } |
600 | } | 600 | } |
601 | void AddressBook::findNewExtIds( QString fileName, QString currentSyncDevice ) | 601 | void AddressBook::findNewExtIds( QString fileName, QString currentSyncDevice ) |
602 | { | 602 | { |
603 | 603 | ||
604 | setUntagged(); | 604 | setUntagged(); |
605 | KABC::Addressee::List list; | 605 | KABC::Addressee::List list; |
606 | QFile file( fileName ); | 606 | QFile file( fileName ); |
607 | file.open( IO_ReadOnly ); | 607 | file.open( IO_ReadOnly ); |
608 | QByteArray rawData = file.readAll(); | 608 | QByteArray rawData = file.readAll(); |
609 | file.close(); | 609 | file.close(); |
610 | QString data; | 610 | QString data; |
611 | 611 | ||
612 | data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); | 612 | data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); |
613 | KABC::VCardTool tool; | 613 | KABC::VCardTool tool; |
614 | list = tool.parseVCards( data ); | 614 | list = tool.parseVCards( data ); |
615 | KABC::Addressee::List::Iterator it; | 615 | KABC::Addressee::List::Iterator it; |
616 | for ( it = list.begin(); it != list.end(); ++it ) { | 616 | for ( it = list.begin(); it != list.end(); ++it ) { |
617 | Iterator ait; | 617 | Iterator ait; |
618 | for ( ait = begin(); ait != end(); ++ait ) { | 618 | for ( ait = begin(); ait != end(); ++ait ) { |
619 | if ( !(*ait).tagged() ) { | 619 | if ( !(*ait).tagged() ) { |
620 | if ( (*ait).containsAdr(*it)) { | 620 | if ( (*ait).containsAdr(*it)) { |
621 | (*ait).setTagged(true); | 621 | (*ait).setTagged(true); |
622 | QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); | 622 | QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); |
623 | (*it).setIDStr( ":"); | 623 | (*it).setIDStr( ":"); |
624 | (*it).setID( currentSyncDevice,id ); | 624 | (*it).setID( currentSyncDevice,id ); |
625 | (*it).setExternalUID( id ); | 625 | (*it).setExternalUID( id ); |
626 | (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); | 626 | (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); |
627 | (*it).setUid( ( (*ait).uid() )); | 627 | (*it).setUid( ( (*ait).uid() )); |
628 | break; | 628 | break; |
629 | } | 629 | } |
630 | } | 630 | } |
631 | 631 | ||
632 | } | 632 | } |
633 | if ( ait == end() ) | 633 | if ( ait == end() ) |
634 | qDebug("ERROR:: no ext ID found for uid: %s", (*it).uid().latin1()); | 634 | qDebug("ERROR:: no ext ID found for uid: %s", (*it).uid().latin1()); |
635 | } | 635 | } |
636 | clear(); | 636 | clear(); |
637 | for ( it = list.begin(); it != list.end(); ++it ) { | 637 | for ( it = list.begin(); it != list.end(); ++it ) { |
638 | insertAddressee( (*it) ); | 638 | insertAddressee( (*it) ); |
639 | } | 639 | } |
640 | } | 640 | } |
641 | 641 | ||
642 | bool AddressBook::saveABphone( QString fileName ) | 642 | bool AddressBook::saveABphone( QString fileName ) |
643 | { | 643 | { |
644 | //smplifyAddressees(); | 644 | //smplifyAddressees(); |
645 | qDebug("saveABphone:: saving AB... "); | 645 | qDebug("saveABphone:: saving AB... "); |
646 | if ( ! export2PhoneFormat( QStringList() ,fileName ) ) | 646 | if ( ! export2PhoneFormat( QStringList() ,fileName ) ) |
647 | return false; | 647 | return false; |
648 | qDebug("saveABphone:: writing to phone... "); | 648 | qDebug("saveABphone:: writing to phone... "); |
649 | if ( !PhoneAccess::writeToPhone( fileName) ) { | 649 | if ( !PhoneAccess::writeToPhone( fileName) ) { |
650 | return false; | 650 | return false; |
651 | } | 651 | } |
652 | qDebug("saveABphone:: re-reading from phone... "); | 652 | qDebug("saveABphone:: re-reading from phone... "); |
653 | if ( !PhoneAccess::readFromPhone( fileName) ) { | 653 | if ( !PhoneAccess::readFromPhone( fileName) ) { |
654 | return false; | 654 | return false; |
655 | } | 655 | } |
656 | return true; | 656 | return true; |
657 | } | 657 | } |
658 | bool AddressBook::saveAB() | 658 | bool AddressBook::saveAB() |
659 | { | 659 | { |
660 | bool ok = true; | 660 | bool ok = true; |
661 | 661 | ||
662 | deleteRemovedAddressees(); | 662 | deleteRemovedAddressees(); |
663 | Iterator ait; | 663 | Iterator ait; |
664 | for ( ait = begin(); ait != end(); ++ait ) { | 664 | for ( ait = begin(); ait != end(); ++ait ) { |
665 | if ( !(*ait).IDStr().isEmpty() ) { | 665 | if ( !(*ait).IDStr().isEmpty() ) { |
666 | (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); | 666 | (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); |
667 | } | 667 | } |
668 | } | 668 | } |
669 | KRES::Manager<Resource>::ActiveIterator it; | 669 | KRES::Manager<Resource>::ActiveIterator it; |
670 | KRES::Manager<Resource> *manager = d->mManager; | 670 | KRES::Manager<Resource> *manager = d->mManager; |
671 | qDebug("SaveAB::saving..." ); | 671 | qDebug("SaveAB::saving..." ); |
672 | for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { | 672 | for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { |
673 | qDebug("SaveAB::checking resource..." ); | 673 | qDebug("SaveAB::checking resource..." ); |
674 | if ( (*it)->readOnly() ) | 674 | if ( (*it)->readOnly() ) |
675 | qDebug("resource is readonly." ); | 675 | qDebug("resource is readonly." ); |
676 | if ( (*it)->isOpen() ) | 676 | if ( (*it)->isOpen() ) |
677 | qDebug("resource is open" ); | 677 | qDebug("resource is open" ); |
678 | 678 | ||
679 | if ( !(*it)->readOnly() && (*it)->isOpen() ) { | 679 | if ( !(*it)->readOnly() && (*it)->isOpen() ) { |
680 | Ticket *ticket = requestSaveTicket( *it ); | 680 | Ticket *ticket = requestSaveTicket( *it ); |
681 | qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); | 681 | qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); |
682 | if ( !ticket ) { | 682 | if ( !ticket ) { |
683 | qDebug( i18n( "Unable to save to resource '%1'. It is locked." ) | 683 | qDebug( i18n( "Unable to save to resource '%1'. It is locked." ) |
684 | .arg( (*it)->resourceName() ) ); | 684 | .arg( (*it)->resourceName() ) ); |
685 | return false; | 685 | return false; |
686 | } | 686 | } |
687 | 687 | ||
688 | //if ( !save( ticket ) ) | 688 | //if ( !save( ticket ) ) |
689 | if ( ticket->resource() ) { | 689 | if ( ticket->resource() ) { |
690 | QString name = ticket->resource()->resourceName(); | 690 | QString name = ticket->resource()->resourceName(); |
691 | if ( ! ticket->resource()->save( ticket ) ) | 691 | if ( ! ticket->resource()->save( ticket ) ) |
692 | ok = false; | 692 | ok = false; |
693 | else | 693 | else |
694 | qDebug("StdAddressBook::resource saved '%s'", name.latin1() ); | 694 | qDebug("StdAddressBook::resource saved '%s'", name.latin1() ); |
695 | 695 | ||
696 | } else | 696 | } else |
697 | ok = false; | 697 | ok = false; |
698 | 698 | ||
699 | } | 699 | } |
700 | } | 700 | } |
701 | return ok; | 701 | return ok; |
702 | } | 702 | } |
703 | 703 | ||
704 | AddressBook::Iterator AddressBook::begin() | 704 | AddressBook::Iterator AddressBook::begin() |
705 | { | 705 | { |
706 | Iterator it = Iterator(); | 706 | Iterator it = Iterator(); |
707 | it.d->mIt = d->mAddressees.begin(); | 707 | it.d->mIt = d->mAddressees.begin(); |
708 | return it; | 708 | return it; |
709 | } | 709 | } |
710 | 710 | ||
711 | AddressBook::ConstIterator AddressBook::begin() const | 711 | AddressBook::ConstIterator AddressBook::begin() const |
712 | { | 712 | { |
713 | ConstIterator it = ConstIterator(); | 713 | ConstIterator it = ConstIterator(); |
714 | it.d->mIt = d->mAddressees.begin(); | 714 | it.d->mIt = d->mAddressees.begin(); |
715 | return it; | 715 | return it; |
716 | } | 716 | } |
717 | 717 | ||
718 | AddressBook::Iterator AddressBook::end() | 718 | AddressBook::Iterator AddressBook::end() |
719 | { | 719 | { |
720 | Iterator it = Iterator(); | 720 | Iterator it = Iterator(); |
721 | it.d->mIt = d->mAddressees.end(); | 721 | it.d->mIt = d->mAddressees.end(); |
722 | return it; | 722 | return it; |
723 | } | 723 | } |
724 | 724 | ||
725 | AddressBook::ConstIterator AddressBook::end() const | 725 | AddressBook::ConstIterator AddressBook::end() const |
726 | { | 726 | { |
727 | ConstIterator it = ConstIterator(); | 727 | ConstIterator it = ConstIterator(); |
728 | it.d->mIt = d->mAddressees.end(); | 728 | it.d->mIt = d->mAddressees.end(); |
729 | return it; | 729 | return it; |
730 | } | 730 | } |
731 | 731 | ||
732 | void AddressBook::clear() | 732 | void AddressBook::clear() |
733 | { | 733 | { |
734 | d->mAddressees.clear(); | 734 | d->mAddressees.clear(); |
735 | } | 735 | } |
736 | 736 | ||
737 | Ticket *AddressBook::requestSaveTicket( Resource *resource ) | 737 | Ticket *AddressBook::requestSaveTicket( Resource *resource ) |
738 | { | 738 | { |
739 | kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; | 739 | kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; |
740 | 740 | ||
741 | if ( !resource ) | 741 | if ( !resource ) |
742 | { | 742 | { |
743 | qDebug("AddressBook::requestSaveTicket no resource" ); | 743 | qDebug("AddressBook::requestSaveTicket no resource" ); |
744 | resource = standardResource(); | 744 | resource = standardResource(); |
745 | } | 745 | } |
746 | 746 | ||
747 | KRES::Manager<Resource>::ActiveIterator it; | 747 | KRES::Manager<Resource>::ActiveIterator it; |
748 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { | 748 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { |
749 | if ( (*it) == resource ) { | 749 | if ( (*it) == resource ) { |
750 | if ( (*it)->readOnly() || !(*it)->isOpen() ) | 750 | if ( (*it)->readOnly() || !(*it)->isOpen() ) |
751 | return 0; | 751 | return 0; |
752 | else | 752 | else |
753 | return (*it)->requestSaveTicket(); | 753 | return (*it)->requestSaveTicket(); |
754 | } | 754 | } |
755 | } | 755 | } |
756 | 756 | ||
757 | return 0; | 757 | return 0; |
758 | } | 758 | } |
759 | //void insertAddressee( const Addressee &, bool setRev = true, bool takeResource = false); | 759 | //void insertAddressee( const Addressee &, bool setRev = true, bool takeResource = false); |
760 | void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource ) | 760 | void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource ) |
761 | { | 761 | { |
762 | if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { | 762 | if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { |
763 | //qDebug("block insert "); | 763 | //qDebug("block insert "); |
764 | return; | 764 | return; |
765 | } | 765 | } |
766 | //qDebug("inserting.... %s ",a.uid().latin1() ); | 766 | //qDebug("inserting.... %s ",a.uid().latin1() ); |
767 | bool found = false; | 767 | bool found = false; |
768 | Addressee::List::Iterator it; | 768 | Addressee::List::Iterator it; |
769 | for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { | 769 | for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { |
770 | if ( a.uid() == (*it).uid() ) { | 770 | if ( a.uid() == (*it).uid() ) { |
771 | 771 | ||
772 | bool changed = false; | 772 | bool changed = false; |
773 | Addressee addr = a; | 773 | Addressee addr = a; |
774 | if ( addr != (*it) ) | 774 | if ( addr != (*it) ) |
775 | changed = true; | 775 | changed = true; |
776 | 776 | ||
777 | if ( takeResource ) { | 777 | if ( takeResource ) { |
778 | Resource * res = (*it).resource(); | 778 | Resource * res = (*it).resource(); |
779 | (*it) = a; | 779 | (*it) = a; |
780 | (*it).setResource( res ); | 780 | (*it).setResource( res ); |
781 | } else { | 781 | } else { |
782 | (*it) = a; | 782 | (*it) = a; |
783 | if ( (*it).resource() == 0 ) | 783 | if ( (*it).resource() == 0 ) |
784 | (*it).setResource( standardResource() ); | 784 | (*it).setResource( standardResource() ); |
785 | } | 785 | } |
786 | if ( changed ) { | 786 | if ( changed ) { |
787 | if ( setRev ) { | 787 | if ( setRev ) { |
788 | (*it).setRevision( QDateTime::currentDateTime() ); | 788 | (*it).setRevision( QDateTime::currentDateTime() ); |
789 | } | 789 | } |
790 | (*it).setChanged( true ); | 790 | (*it).setChanged( true ); |
791 | } | 791 | } |
792 | 792 | ||
793 | found = true; | 793 | found = true; |
794 | } else { | 794 | } else { |
795 | if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { | 795 | if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { |
796 | QString name = (*it).uid().mid( 19 ); | 796 | QString name = (*it).uid().mid( 19 ); |
797 | Addressee b = a; | 797 | Addressee b = a; |
798 | QString id = b.getID( name ); | 798 | QString id = b.getID( name ); |
799 | if ( ! id.isEmpty() ) { | 799 | if ( ! id.isEmpty() ) { |
800 | QString des = (*it).note(); | 800 | QString des = (*it).note(); |
801 | int startN; | 801 | int startN; |
802 | if( (startN = des.find( id ) ) >= 0 ) { | 802 | if( (startN = des.find( id ) ) >= 0 ) { |
803 | int endN = des.find( ",", startN+1 ); | 803 | int endN = des.find( ",", startN+1 ); |
804 | des = des.left( startN ) + des.mid( endN+1 ); | 804 | des = des.left( startN ) + des.mid( endN+1 ); |
805 | (*it).setNote( des ); | 805 | (*it).setNote( des ); |
806 | } | 806 | } |
807 | } | 807 | } |
808 | } | 808 | } |
809 | } | 809 | } |
810 | } | 810 | } |
811 | if ( found ) | 811 | if ( found ) |
812 | return; | 812 | return; |
813 | 813 | ||
814 | d->mAddressees.append( a ); | 814 | d->mAddressees.append( a ); |
815 | Addressee& addr = d->mAddressees.last(); | 815 | Addressee& addr = d->mAddressees.last(); |
816 | if ( addr.resource() == 0 ) | 816 | if ( addr.resource() == 0 ) |
817 | addr.setResource( standardResource() ); | 817 | addr.setResource( standardResource() ); |
818 | addr.setChanged( true ); | 818 | addr.setChanged( true ); |
819 | } | 819 | } |
820 | 820 | ||
821 | void AddressBook::removeAddressee( const Addressee &a ) | 821 | void AddressBook::removeAddressee( const Addressee &a ) |
822 | { | 822 | { |
823 | Iterator it; | 823 | Iterator it; |
824 | Iterator it2; | 824 | Iterator it2; |
825 | bool found = false; | 825 | bool found = false; |
826 | for ( it = begin(); it != end(); ++it ) { | 826 | for ( it = begin(); it != end(); ++it ) { |
827 | if ( a.uid() == (*it).uid() ) { | 827 | if ( a.uid() == (*it).uid() ) { |
828 | found = true; | 828 | found = true; |
829 | it2 = it; | 829 | it2 = it; |
830 | } else { | 830 | } else { |
831 | if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { | 831 | if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { |
832 | QString name = (*it).uid().mid( 19 ); | 832 | QString name = (*it).uid().mid( 19 ); |
833 | Addressee b = a; | 833 | Addressee b = a; |
834 | QString id = b.getID( name ); | 834 | QString id = b.getID( name ); |
835 | if ( ! id.isEmpty() ) { | 835 | if ( ! id.isEmpty() ) { |
836 | QString des = (*it).note(); | 836 | QString des = (*it).note(); |
837 | if( des.find( id ) < 0 ) { | 837 | if( des.find( id ) < 0 ) { |
838 | des += id + ","; | 838 | des += id + ","; |
839 | (*it).setNote( des ); | 839 | (*it).setNote( des ); |
840 | } | 840 | } |
841 | } | 841 | } |
842 | } | 842 | } |
843 | 843 | ||
844 | } | 844 | } |
845 | } | 845 | } |
846 | 846 | ||
847 | if ( found ) | 847 | if ( found ) |
848 | removeAddressee( it2 ); | 848 | removeAddressee( it2 ); |
849 | 849 | ||
850 | } | 850 | } |
851 | 851 | ||
852 | void AddressBook::removeSyncAddressees( bool removeDeleted ) | 852 | void AddressBook::removeSyncAddressees( bool removeDeleted ) |
853 | { | 853 | { |
854 | Iterator it = begin(); | 854 | Iterator it = begin(); |
855 | Iterator it2 ; | 855 | Iterator it2 ; |
856 | QDateTime dt ( QDate( 2003,1,1) ); | 856 | QDateTime dt ( QDate( 2003,1,1) ); |
857 | while ( it != end() ) { | 857 | while ( it != end() ) { |
858 | (*it).setRevision( dt ); | 858 | (*it).setRevision( dt ); |
859 | if (( *it).IDStr() != "changed" ) { | 859 | if (( *it).IDStr() != "changed" ) { |
860 | // "changed" is used for tagging changed addressees when syncing with KDE or OL | 860 | // "changed" is used for tagging changed addressees when syncing with KDE or OL |
861 | (*it).removeCustom( "KADDRESSBOOK", "X-ExternalID" ); | 861 | (*it).removeCustom( "KADDRESSBOOK", "X-ExternalID" ); |
862 | (*it).setIDStr(":"); | 862 | (*it).setIDStr(":"); |
863 | } | 863 | } |
864 | if ( ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE && removeDeleted )|| (*it).uid().left( 19 ) == QString("last-syncAddressee-")) { | 864 | if ( ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE && removeDeleted )|| (*it).uid().left( 19 ) == QString("last-syncAddressee-")) { |
865 | it2 = it; | 865 | it2 = it; |
866 | //qDebug("removing %s ",(*it).uid().latin1() ); | 866 | //qDebug("removing %s ",(*it).uid().latin1() ); |
867 | ++it; | 867 | ++it; |
868 | removeAddressee( it2 ); | 868 | removeAddressee( it2 ); |
869 | } else { | 869 | } else { |
870 | //qDebug("skipping %s ",(*it).uid().latin1() ); | 870 | //qDebug("skipping %s ",(*it).uid().latin1() ); |
871 | if ( removeDeleted ) { | 871 | if ( removeDeleted ) { |
872 | // we have no postprocessing in the resource, we have to do it here | 872 | // we have no postprocessing in the resource, we have to do it here |
873 | // we have to compute csum for all, because it could be the first sync | 873 | // we have to compute csum for all, because it could be the first sync |
874 | (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); | 874 | (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); |
875 | } | 875 | } |
876 | ++it; | 876 | ++it; |
877 | } | 877 | } |
878 | } | 878 | } |
879 | deleteRemovedAddressees(); | 879 | deleteRemovedAddressees(); |
880 | } | 880 | } |
881 | 881 | ||
882 | void AddressBook::removeAddressee( const Iterator &it ) | 882 | void AddressBook::removeAddressee( const Iterator &it ) |
883 | { | 883 | { |
884 | d->mRemovedAddressees.append( (*it) ); | 884 | d->mRemovedAddressees.append( (*it) ); |
885 | d->mAddressees.remove( it.d->mIt ); | 885 | d->mAddressees.remove( it.d->mIt ); |
886 | } | 886 | } |
887 | 887 | ||
888 | AddressBook::Iterator AddressBook::find( const Addressee &a ) | 888 | AddressBook::Iterator AddressBook::find( const Addressee &a ) |
889 | { | 889 | { |
890 | Iterator it; | 890 | Iterator it; |
891 | for ( it = begin(); it != end(); ++it ) { | 891 | for ( it = begin(); it != end(); ++it ) { |
892 | if ( a.uid() == (*it).uid() ) { | 892 | if ( a.uid() == (*it).uid() ) { |
893 | return it; | 893 | return it; |
894 | } | 894 | } |
895 | } | 895 | } |
896 | return end(); | 896 | return end(); |
897 | } | 897 | } |
898 | 898 | ||
899 | Addressee AddressBook::findByUid( const QString &uid ) | 899 | Addressee AddressBook::findByUid( const QString &uid ) |
900 | { | 900 | { |
901 | Iterator it; | 901 | Iterator it; |
902 | for ( it = begin(); it != end(); ++it ) { | 902 | for ( it = begin(); it != end(); ++it ) { |
903 | if ( uid == (*it).uid() ) { | 903 | if ( uid == (*it).uid() ) { |
904 | return *it; | 904 | return *it; |
905 | } | 905 | } |
906 | } | 906 | } |
907 | return Addressee(); | 907 | return Addressee(); |
908 | } | 908 | } |
909 | void AddressBook::preExternSync( AddressBook* aBook, const QString& csd , bool isSubset ) | 909 | void AddressBook::preExternSync( AddressBook* aBook, const QString& csd , bool isSubset ) |
910 | { | 910 | { |
911 | //qDebug("AddressBook::preExternSync "); | 911 | //qDebug("AddressBook::preExternSync "); |
912 | AddressBook::Iterator it; | 912 | AddressBook::Iterator it; |
913 | for ( it = begin(); it != end(); ++it ) { | 913 | for ( it = begin(); it != end(); ++it ) { |
914 | (*it).setID( csd, (*it).externalUID() ); | 914 | (*it).setID( csd, (*it).externalUID() ); |
915 | (*it).computeCsum( csd ); | 915 | (*it).computeCsum( csd ); |
916 | } | 916 | } |
917 | mergeAB( aBook ,csd, isSubset ); | 917 | mergeAB( aBook ,csd, isSubset ); |
918 | } | 918 | } |
919 | void AddressBook::postExternSync( AddressBook* aBook , const QString& csd, bool setID) | 919 | void AddressBook::postExternSync( AddressBook* aBook , const QString& csd, bool setID) |
920 | { | 920 | { |
921 | //qDebug("AddressBook::postExternSync "); | 921 | //qDebug("AddressBook::postExternSync "); |
922 | AddressBook::Iterator it; | 922 | AddressBook::Iterator it; |
923 | for ( it = begin(); it != end(); ++it ) { | 923 | for ( it = begin(); it != end(); ++it ) { |
924 | //qDebug("check uid %s ", (*it).uid().latin1() ); | 924 | //qDebug("check uid %s ", (*it).uid().latin1() ); |
925 | if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID || | 925 | if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID || |
926 | (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_CSUM || | 926 | (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_CSUM || |
927 | (*it).tempSyncStat() == SYNC_TEMPSTATE_ADDED_EXTERNAL) { | 927 | (*it).tempSyncStat() == SYNC_TEMPSTATE_ADDED_EXTERNAL) { |
928 | Addressee ad = aBook->findByUid( ( (*it).uid() )); | 928 | Addressee ad = aBook->findByUid( ( (*it).uid() )); |
929 | if ( ad.isEmpty() ) { | 929 | if ( ad.isEmpty() ) { |
930 | qDebug("postExternSync:ERROR addressee is empty: %s ", (*it).uid().latin1()); | 930 | qDebug("postExternSync:ERROR addressee is empty: %s ", (*it).uid().latin1()); |
931 | } else { | 931 | } else { |
932 | (*it).setIDStr(":"); | 932 | (*it).setIDStr(":"); |
933 | if ( setID ) { | 933 | if ( setID ) { |
934 | if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID ) | 934 | if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID ) |
935 | ad.setID( csd, (*it).externalUID() ); | 935 | ad.setID( csd, (*it).externalUID() ); |
936 | } else | 936 | } else |
937 | ad.setID( csd, (*it).uid() ); | 937 | ad.setID( csd, (*it).uid() ); |
938 | (*it).computeCsum( csd ); | 938 | (*it).computeCsum( csd ); |
939 | ad.setCsum( csd, (*it).getCsum( csd ) ); | 939 | ad.setCsum( csd, (*it).getCsum( csd ) ); |
940 | //qDebug("CSUM %s ",(*it).getCsum( csd ).latin1() ); | 940 | //qDebug("CSUM %s ",(*it).getCsum( csd ).latin1() ); |
941 | aBook->insertAddressee( ad , false); | 941 | aBook->insertAddressee( ad , false); |
942 | } | 942 | } |
943 | } | 943 | } |
944 | } | 944 | } |
945 | } | 945 | } |
946 | 946 | ||
947 | bool AddressBook::containsExternalUid( const QString& uid ) | 947 | bool AddressBook::containsExternalUid( const QString& uid ) |
948 | { | 948 | { |
949 | Iterator it; | 949 | Iterator it; |
950 | for ( it = begin(); it != end(); ++it ) { | 950 | for ( it = begin(); it != end(); ++it ) { |
951 | if ( uid == (*it).externalUID( ) ) | 951 | if ( uid == (*it).externalUID( ) ) |
952 | return true; | 952 | return true; |
953 | } | 953 | } |
954 | return false; | 954 | return false; |
955 | } | 955 | } |
956 | Addressee AddressBook::findByExternUid( const QString& uid , const QString& profile ) | 956 | Addressee AddressBook::findByExternUid( const QString& uid , const QString& profile ) |
957 | { | 957 | { |
958 | Iterator it; | 958 | Iterator it; |
959 | for ( it = begin(); it != end(); ++it ) { | 959 | for ( it = begin(); it != end(); ++it ) { |
960 | if ( uid == (*it).getID( profile ) ) | 960 | if ( uid == (*it).getID( profile ) ) |
961 | return (*it); | 961 | return (*it); |
962 | } | 962 | } |
963 | return Addressee(); | 963 | return Addressee(); |
964 | } | 964 | } |
965 | void AddressBook::mergeAB( AddressBook *aBook, const QString& profile , bool isSubset ) | 965 | void AddressBook::mergeAB( AddressBook *aBook, const QString& profile , bool isSubset ) |
966 | { | 966 | { |
967 | Iterator it; | 967 | Iterator it; |
968 | Addressee ad; | 968 | Addressee ad; |
969 | for ( it = begin(); it != end(); ++it ) { | 969 | for ( it = begin(); it != end(); ++it ) { |
970 | ad = aBook->findByExternUid( (*it).externalUID(), profile ); | 970 | ad = aBook->findByExternUid( (*it).externalUID(), profile ); |
971 | if ( !ad.isEmpty() ) { | 971 | if ( !ad.isEmpty() ) { |
972 | (*it).mergeContact( ad ,isSubset); | 972 | (*it).mergeContact( ad ,isSubset); |
973 | } | 973 | } |
974 | } | 974 | } |
975 | #if 0 | 975 | #if 0 |
976 | // test only | 976 | // test only |
977 | for ( it = begin(); it != end(); ++it ) { | 977 | for ( it = begin(); it != end(); ++it ) { |
978 | 978 | ||
979 | qDebug("uid %s ", (*it).uid().latin1()); | 979 | qDebug("uid %s ", (*it).uid().latin1()); |
980 | } | 980 | } |
981 | #endif | 981 | #endif |
982 | } | 982 | } |
983 | 983 | ||
984 | #if 0 | 984 | #if 0 |
985 | Addressee::List AddressBook::getExternLastSyncAddressees() | 985 | Addressee::List AddressBook::getExternLastSyncAddressees() |
986 | { | 986 | { |
987 | Addressee::List results; | 987 | Addressee::List results; |
988 | 988 | ||
989 | Iterator it; | 989 | Iterator it; |
990 | for ( it = begin(); it != end(); ++it ) { | 990 | for ( it = begin(); it != end(); ++it ) { |
991 | if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) { | 991 | if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) { |
992 | if ( (*it).familyName().left(4) == "!E: " ) | 992 | if ( (*it).familyName().left(4) == "!E: " ) |
993 | results.append( *it ); | 993 | results.append( *it ); |
994 | } | 994 | } |
995 | } | 995 | } |
996 | 996 | ||
997 | return results; | 997 | return results; |
998 | } | 998 | } |
999 | #endif | 999 | #endif |
1000 | void AddressBook::resetTempSyncStat() | 1000 | void AddressBook::resetTempSyncStat() |
1001 | { | 1001 | { |
1002 | Iterator it; | 1002 | Iterator it; |
1003 | for ( it = begin(); it != end(); ++it ) { | 1003 | for ( it = begin(); it != end(); ++it ) { |
1004 | (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL ); | 1004 | (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL ); |
1005 | } | 1005 | } |
1006 | 1006 | ||
1007 | } | 1007 | } |
1008 | 1008 | ||
1009 | QStringList AddressBook:: uidList() | 1009 | QStringList AddressBook:: uidList() |
1010 | { | 1010 | { |
1011 | QStringList results; | 1011 | QStringList results; |
1012 | Iterator it; | 1012 | Iterator it; |
1013 | for ( it = begin(); it != end(); ++it ) { | 1013 | for ( it = begin(); it != end(); ++it ) { |
1014 | results.append( (*it).uid() ); | 1014 | results.append( (*it).uid() ); |
1015 | } | 1015 | } |
1016 | return results; | 1016 | return results; |
1017 | } | 1017 | } |
1018 | 1018 | ||
1019 | 1019 | ||
1020 | Addressee::List AddressBook::allAddressees() | 1020 | Addressee::List AddressBook::allAddressees() |
1021 | { | 1021 | { |
1022 | return d->mAddressees; | 1022 | return d->mAddressees; |
1023 | 1023 | ||
1024 | } | 1024 | } |
1025 | 1025 | ||
1026 | Addressee::List AddressBook::findByName( const QString &name ) | 1026 | Addressee::List AddressBook::findByName( const QString &name ) |
1027 | { | 1027 | { |
1028 | Addressee::List results; | 1028 | Addressee::List results; |
1029 | 1029 | ||
1030 | Iterator it; | 1030 | Iterator it; |
1031 | for ( it = begin(); it != end(); ++it ) { | 1031 | for ( it = begin(); it != end(); ++it ) { |
1032 | if ( name == (*it).realName() ) { | 1032 | if ( name == (*it).realName() ) { |
1033 | results.append( *it ); | 1033 | results.append( *it ); |
1034 | } | 1034 | } |
1035 | } | 1035 | } |
1036 | 1036 | ||
1037 | return results; | 1037 | return results; |
1038 | } | 1038 | } |
1039 | 1039 | ||
1040 | Addressee::List AddressBook::findByEmail( const QString &email ) | 1040 | Addressee::List AddressBook::findByEmail( const QString &email ) |
1041 | { | 1041 | { |
1042 | Addressee::List results; | 1042 | Addressee::List results; |
1043 | QStringList mailList; | 1043 | QStringList mailList; |
1044 | 1044 | ||
1045 | Iterator it; | 1045 | Iterator it; |
1046 | for ( it = begin(); it != end(); ++it ) { | 1046 | for ( it = begin(); it != end(); ++it ) { |
1047 | mailList = (*it).emails(); | 1047 | mailList = (*it).emails(); |
1048 | for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) { | 1048 | for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) { |
1049 | if ( email == (*ite) ) { | 1049 | if ( email == (*ite) ) { |
1050 | results.append( *it ); | 1050 | results.append( *it ); |
1051 | } | 1051 | } |
1052 | } | 1052 | } |
1053 | } | 1053 | } |
1054 | 1054 | ||
1055 | return results; | 1055 | return results; |
1056 | } | 1056 | } |
1057 | 1057 | ||
1058 | Addressee::List AddressBook::findByCategory( const QString &category ) | 1058 | Addressee::List AddressBook::findByCategory( const QString &category ) |
1059 | { | 1059 | { |
1060 | Addressee::List results; | 1060 | Addressee::List results; |
1061 | 1061 | ||
1062 | Iterator it; | 1062 | Iterator it; |
1063 | for ( it = begin(); it != end(); ++it ) { | 1063 | for ( it = begin(); it != end(); ++it ) { |
1064 | if ( (*it).hasCategory( category) ) { | 1064 | if ( (*it).hasCategory( category) ) { |
1065 | results.append( *it ); | 1065 | results.append( *it ); |
1066 | } | 1066 | } |
1067 | } | 1067 | } |
1068 | 1068 | ||
1069 | return results; | 1069 | return results; |
1070 | } | 1070 | } |
1071 | 1071 | ||
1072 | void AddressBook::dump() const | 1072 | void AddressBook::dump() const |
1073 | { | 1073 | { |
1074 | kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl; | 1074 | kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl; |
1075 | 1075 | ||
1076 | ConstIterator it; | 1076 | ConstIterator it; |
1077 | for( it = begin(); it != end(); ++it ) { | 1077 | for( it = begin(); it != end(); ++it ) { |
1078 | (*it).dump(); | 1078 | (*it).dump(); |
1079 | } | 1079 | } |
1080 | 1080 | ||
1081 | kdDebug(5700) << "AddressBook::dump() --- end ---" << endl; | 1081 | kdDebug(5700) << "AddressBook::dump() --- end ---" << endl; |
1082 | } | 1082 | } |
1083 | 1083 | ||
1084 | QString AddressBook::identifier() | 1084 | QString AddressBook::identifier() |
1085 | { | 1085 | { |
1086 | QStringList identifier; | 1086 | QStringList identifier; |
1087 | 1087 | ||
1088 | 1088 | ||
1089 | KRES::Manager<Resource>::ActiveIterator it; | 1089 | KRES::Manager<Resource>::ActiveIterator it; |
1090 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { | 1090 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { |
1091 | if ( !(*it)->identifier().isEmpty() ) | 1091 | if ( !(*it)->identifier().isEmpty() ) |
1092 | identifier.append( (*it)->identifier() ); | 1092 | identifier.append( (*it)->identifier() ); |
1093 | } | 1093 | } |
1094 | 1094 | ||
1095 | return identifier.join( ":" ); | 1095 | return identifier.join( ":" ); |
1096 | } | 1096 | } |
1097 | 1097 | ||
1098 | Field::List AddressBook::fields( int category ) | 1098 | Field::List AddressBook::fields( int category ) |
1099 | { | 1099 | { |
1100 | if ( d->mAllFields.isEmpty() ) { | 1100 | if ( d->mAllFields.isEmpty() ) { |
1101 | d->mAllFields = Field::allFields(); | 1101 | d->mAllFields = Field::allFields(); |
1102 | } | 1102 | } |
1103 | 1103 | ||
1104 | if ( category == Field::All ) return d->mAllFields; | 1104 | if ( category == Field::All ) return d->mAllFields; |
1105 | 1105 | ||
1106 | Field::List result; | 1106 | Field::List result; |
1107 | Field::List::ConstIterator it; | 1107 | Field::List::ConstIterator it; |
1108 | for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) { | 1108 | for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) { |
1109 | if ( (*it)->category() & category ) result.append( *it ); | 1109 | if ( (*it)->category() & category ) result.append( *it ); |
1110 | } | 1110 | } |
1111 | 1111 | ||
1112 | return result; | 1112 | return result; |
1113 | } | 1113 | } |
1114 | 1114 | ||
1115 | bool AddressBook::addCustomField( const QString &label, int category, | 1115 | bool AddressBook::addCustomField( const QString &label, int category, |
1116 | const QString &key, const QString &app ) | 1116 | const QString &key, const QString &app ) |
1117 | { | 1117 | { |
1118 | if ( d->mAllFields.isEmpty() ) { | 1118 | if ( d->mAllFields.isEmpty() ) { |
1119 | d->mAllFields = Field::allFields(); | 1119 | d->mAllFields = Field::allFields(); |
1120 | } | 1120 | } |
1121 | //US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app; | 1121 | //US QString a = app.isNull() ? KGlobal::instance()->instanceName() : app; |
1122 | QString a = app.isNull() ? KGlobal::getAppName() : app; | 1122 | QString a = app.isNull() ? KGlobal::getAppName() : app; |
1123 | 1123 | ||
1124 | QString k = key.isNull() ? label : key; | 1124 | QString k = key.isNull() ? label : key; |
1125 | 1125 | ||
1126 | Field *field = Field::createCustomField( label, category, k, a ); | 1126 | Field *field = Field::createCustomField( label, category, k, a ); |
1127 | 1127 | ||
1128 | if ( !field ) return false; | 1128 | if ( !field ) return false; |
1129 | 1129 | ||
1130 | d->mAllFields.append( field ); | 1130 | d->mAllFields.append( field ); |
1131 | 1131 | ||
1132 | return true; | 1132 | return true; |
1133 | } | 1133 | } |
1134 | 1134 | ||
1135 | QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab ) | 1135 | QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab ) |
1136 | { | 1136 | { |
1137 | if (!ab.d) return s; | 1137 | if (!ab.d) return s; |
1138 | 1138 | ||
1139 | return s << ab.d->mAddressees; | 1139 | return s << ab.d->mAddressees; |
1140 | } | 1140 | } |
1141 | 1141 | ||
1142 | QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab ) | 1142 | QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab ) |
1143 | { | 1143 | { |
1144 | if (!ab.d) return s; | 1144 | if (!ab.d) return s; |
1145 | 1145 | ||
1146 | s >> ab.d->mAddressees; | 1146 | s >> ab.d->mAddressees; |
1147 | 1147 | ||
1148 | return s; | 1148 | return s; |
1149 | } | 1149 | } |
1150 | 1150 | ||
1151 | bool AddressBook::addResource( Resource *resource ) | 1151 | bool AddressBook::addResource( Resource *resource ) |
1152 | { | 1152 | { |
1153 | if ( !resource->open() ) { | 1153 | if ( !resource->open() ) { |
1154 | kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl; | 1154 | kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl; |
1155 | return false; | 1155 | return false; |
1156 | } | 1156 | } |
1157 | 1157 | ||
1158 | resource->setAddressBook( this ); | 1158 | resource->setAddressBook( this ); |
1159 | 1159 | ||
1160 | d->mManager->add( resource ); | 1160 | d->mManager->add( resource ); |
1161 | return true; | 1161 | return true; |
1162 | } | 1162 | } |
1163 | void AddressBook::removeResources() | ||
1164 | { | ||
1165 | //remove all possible resources. This should cleanup the configfile. | ||
1166 | QPtrList<KABC::Resource> mResources = resources(); | ||
1167 | |||
1168 | QPtrListIterator<KABC::Resource> it(mResources); | ||
1169 | for ( ; it.current(); ++it ) { | ||
1170 | KABC::Resource *res = it.current(); | ||
1171 | removeResource(res); | ||
1172 | } | ||
1173 | } | ||
1163 | 1174 | ||
1164 | bool AddressBook::removeResource( Resource *resource ) | 1175 | bool AddressBook::removeResource( Resource *resource ) |
1165 | { | 1176 | { |
1166 | resource->close(); | 1177 | resource->close(); |
1167 | 1178 | ||
1168 | if ( resource == standardResource() ) | 1179 | if ( resource == standardResource() ) |
1169 | d->mManager->setStandardResource( 0 ); | 1180 | d->mManager->setStandardResource( 0 ); |
1170 | 1181 | ||
1171 | resource->setAddressBook( 0 ); | 1182 | resource->setAddressBook( 0 ); |
1172 | 1183 | ||
1173 | d->mManager->remove( resource ); | 1184 | d->mManager->remove( resource ); |
1174 | return true; | 1185 | return true; |
1175 | } | 1186 | } |
1176 | 1187 | ||
1177 | QPtrList<Resource> AddressBook::resources() | 1188 | QPtrList<Resource> AddressBook::resources() |
1178 | { | 1189 | { |
1179 | QPtrList<Resource> list; | 1190 | QPtrList<Resource> list; |
1180 | 1191 | ||
1181 | // qDebug("AddressBook::resources() 1"); | 1192 | // qDebug("AddressBook::resources() 1"); |
1182 | 1193 | ||
1183 | KRES::Manager<Resource>::ActiveIterator it; | 1194 | KRES::Manager<Resource>::ActiveIterator it; |
1184 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) | 1195 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) |
1185 | list.append( *it ); | 1196 | list.append( *it ); |
1186 | 1197 | ||
1187 | return list; | 1198 | return list; |
1188 | } | 1199 | } |
1189 | 1200 | ||
1190 | /*US | 1201 | /*US |
1191 | void AddressBook::setErrorHandler( ErrorHandler *handler ) | 1202 | void AddressBook::setErrorHandler( ErrorHandler *handler ) |
1192 | { | 1203 | { |
1193 | delete d->mErrorHandler; | 1204 | delete d->mErrorHandler; |
1194 | d->mErrorHandler = handler; | 1205 | d->mErrorHandler = handler; |
1195 | } | 1206 | } |
1196 | */ | 1207 | */ |
1197 | 1208 | ||
1198 | void AddressBook::error( const QString& msg ) | 1209 | void AddressBook::error( const QString& msg ) |
1199 | { | 1210 | { |
1200 | /*US | 1211 | /*US |
1201 | if ( !d->mErrorHandler ) // create default error handler | 1212 | if ( !d->mErrorHandler ) // create default error handler |
1202 | d->mErrorHandler = new ConsoleErrorHandler; | 1213 | d->mErrorHandler = new ConsoleErrorHandler; |
1203 | 1214 | ||
1204 | if ( d->mErrorHandler ) | 1215 | if ( d->mErrorHandler ) |
1205 | d->mErrorHandler->error( msg ); | 1216 | d->mErrorHandler->error( msg ); |
1206 | else | 1217 | else |
1207 | kdError(5700) << "no error handler defined" << endl; | 1218 | kdError(5700) << "no error handler defined" << endl; |
1208 | */ | 1219 | */ |
1209 | kdDebug(5700) << "msg" << endl; | 1220 | kdDebug(5700) << "msg" << endl; |
1210 | qDebug(msg); | 1221 | qDebug(msg); |
1211 | } | 1222 | } |
1212 | 1223 | ||
1213 | void AddressBook::deleteRemovedAddressees() | 1224 | void AddressBook::deleteRemovedAddressees() |
1214 | { | 1225 | { |
1215 | Addressee::List::Iterator it; | 1226 | Addressee::List::Iterator it; |
1216 | for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) { | 1227 | for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) { |
1217 | Resource *resource = (*it).resource(); | 1228 | Resource *resource = (*it).resource(); |
1218 | if ( resource && !resource->readOnly() && resource->isOpen() ) | 1229 | if ( resource && !resource->readOnly() && resource->isOpen() ) |
1219 | resource->removeAddressee( *it ); | 1230 | resource->removeAddressee( *it ); |
1220 | } | 1231 | } |
1221 | 1232 | ||
1222 | d->mRemovedAddressees.clear(); | 1233 | d->mRemovedAddressees.clear(); |
1223 | } | 1234 | } |
1224 | 1235 | ||
1225 | void AddressBook::setStandardResource( Resource *resource ) | 1236 | void AddressBook::setStandardResource( Resource *resource ) |
1226 | { | 1237 | { |
1227 | // qDebug("AddressBook::setStandardResource 1"); | 1238 | // qDebug("AddressBook::setStandardResource 1"); |
1228 | d->mManager->setStandardResource( resource ); | 1239 | d->mManager->setStandardResource( resource ); |
1229 | } | 1240 | } |
1230 | 1241 | ||
1231 | Resource *AddressBook::standardResource() | 1242 | Resource *AddressBook::standardResource() |
1232 | { | 1243 | { |
1233 | return d->mManager->standardResource(); | 1244 | return d->mManager->standardResource(); |
1234 | } | 1245 | } |
1235 | 1246 | ||
1236 | KRES::Manager<Resource> *AddressBook::resourceManager() | 1247 | KRES::Manager<Resource> *AddressBook::resourceManager() |
1237 | { | 1248 | { |
1238 | return d->mManager; | 1249 | return d->mManager; |
1239 | } | 1250 | } |
1240 | 1251 | ||
1241 | void AddressBook::cleanUp() | 1252 | void AddressBook::cleanUp() |
1242 | { | 1253 | { |
1243 | KRES::Manager<Resource>::ActiveIterator it; | 1254 | KRES::Manager<Resource>::ActiveIterator it; |
1244 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { | 1255 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { |
1245 | if ( !(*it)->readOnly() && (*it)->isOpen() ) | 1256 | if ( !(*it)->readOnly() && (*it)->isOpen() ) |
1246 | (*it)->cleanUp(); | 1257 | (*it)->cleanUp(); |
1247 | } | 1258 | } |
1248 | } | 1259 | } |