-rw-r--r-- | kabc/addressbook.cpp | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp index e04f4b1..bdc2762 100644 --- a/kabc/addressbook.cpp +++ b/kabc/addressbook.cpp | |||
@@ -438,142 +438,166 @@ bool AddressBook::export2PhoneFormat( QStringList uids ,QString fileName ) | |||
438 | QString vcard; | 438 | QString vcard; |
439 | QString vcardnew; | 439 | QString vcardnew; |
440 | converter.addresseeToVCard( a, vcard ); | 440 | converter.addresseeToVCard( a, vcard ); |
441 | int start = 0; | 441 | int start = 0; |
442 | int next; | 442 | int next; |
443 | while ( (next = vcard.find("TYPE=", start) )>= 0 ) { | 443 | while ( (next = vcard.find("TYPE=", start) )>= 0 ) { |
444 | int semi = vcard.find(";", next); | 444 | int semi = vcard.find(";", next); |
445 | int dopp = vcard.find(":", next); | 445 | int dopp = vcard.find(":", next); |
446 | int sep; | 446 | int sep; |
447 | if ( semi < dopp && semi >= 0 ) | 447 | if ( semi < dopp && semi >= 0 ) |
448 | sep = semi ; | 448 | sep = semi ; |
449 | else | 449 | else |
450 | sep = dopp; | 450 | sep = dopp; |
451 | vcardnew +=vcard.mid( start, next - start); | 451 | vcardnew +=vcard.mid( start, next - start); |
452 | vcardnew +=vcard.mid( next+5,sep -next -5 ).upper(); | 452 | vcardnew +=vcard.mid( next+5,sep -next -5 ).upper(); |
453 | start = sep; | 453 | start = sep; |
454 | } | 454 | } |
455 | vcardnew += vcard.mid( start,vcard.length() ); | 455 | vcardnew += vcard.mid( start,vcard.length() ); |
456 | vcard = ""; | 456 | vcard = ""; |
457 | start = 0; | 457 | start = 0; |
458 | while ( (next = vcardnew.find("ADR", start) )>= 0 ) { | 458 | while ( (next = vcardnew.find("ADR", start) )>= 0 ) { |
459 | int sep = vcardnew.find(":", next); | 459 | int sep = vcardnew.find(":", next); |
460 | vcard +=vcardnew.mid( start, next - start+3); | 460 | vcard +=vcardnew.mid( start, next - start+3); |
461 | start = sep; | 461 | start = sep; |
462 | } | 462 | } |
463 | vcard += vcardnew.mid( start,vcardnew.length() ); | 463 | vcard += vcardnew.mid( start,vcardnew.length() ); |
464 | vcard.replace ( QRegExp(";;;") , "" ); | 464 | vcard.replace ( QRegExp(";;;") , "" ); |
465 | vcard.replace ( QRegExp(";;") , "" ); | 465 | vcard.replace ( QRegExp(";;") , "" ); |
466 | datastream += vcard; | 466 | datastream += vcard; |
467 | 467 | ||
468 | } | 468 | } |
469 | 469 | ||
470 | QFile outFile(fileName); | 470 | QFile outFile(fileName); |
471 | if ( outFile.open(IO_WriteOnly) ) { | 471 | if ( outFile.open(IO_WriteOnly) ) { |
472 | datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" ); | 472 | datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" ); |
473 | QTextStream t( &outFile ); // use a text stream | 473 | QTextStream t( &outFile ); // use a text stream |
474 | t.setEncoding( QTextStream::UnicodeUTF8 ); | 474 | t.setEncoding( QTextStream::UnicodeUTF8 ); |
475 | t <<datastream; | 475 | t <<datastream; |
476 | t << "\r\n\r\n"; | 476 | t << "\r\n\r\n"; |
477 | outFile.close(); | 477 | outFile.close(); |
478 | 478 | ||
479 | } else { | 479 | } else { |
480 | qDebug("Error open temp file "); | 480 | qDebug("Error open temp file "); |
481 | return false; | 481 | return false; |
482 | } | 482 | } |
483 | return true; | 483 | return true; |
484 | 484 | ||
485 | } | 485 | } |
486 | int AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld ) | 486 | int AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld, QString resource ) |
487 | { | 487 | { |
488 | 488 | ||
489 | if ( removeOld ) | 489 | if ( removeOld ) |
490 | setUntagged( true ); | 490 | setUntagged( true, resource ); |
491 | KABC::Addressee::List list; | 491 | KABC::Addressee::List list; |
492 | QFile file( fileName ); | 492 | QFile file( fileName ); |
493 | file.open( IO_ReadOnly ); | 493 | file.open( IO_ReadOnly ); |
494 | QByteArray rawData = file.readAll(); | 494 | QByteArray rawData = file.readAll(); |
495 | file.close(); | 495 | file.close(); |
496 | QString data; | 496 | QString data; |
497 | if ( replaceLabel ) { | 497 | if ( replaceLabel ) { |
498 | data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); | 498 | data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); |
499 | data.replace ( QRegExp("LABEL") , "ADR" ); | 499 | data.replace ( QRegExp("LABEL") , "ADR" ); |
500 | data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); | 500 | data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); |
501 | } else | 501 | } else |
502 | data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); | 502 | data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); |
503 | KABC::VCardTool tool; | 503 | KABC::VCardTool tool; |
504 | list = tool.parseVCards( data ); | 504 | list = tool.parseVCards( data ); |
505 | KABC::Addressee::List::Iterator it; | 505 | KABC::Addressee::List::Iterator it; |
506 | |||
507 | Resource * setRes = 0; | ||
508 | if ( !resource.isEmpty() ) { | ||
509 | KRES::Manager<Resource>::ActiveIterator it; | ||
510 | KRES::Manager<Resource> *manager = d->mManager; | ||
511 | for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { | ||
512 | qDebug("SaveAB::checking resource..." ); | ||
513 | if ( (*it)->name() == resource ) { | ||
514 | setRes = (*it); | ||
515 | qDebug("Inserting imported contacs to resource %s", resource.latin1()); | ||
516 | break; | ||
517 | } | ||
518 | } | ||
519 | } | ||
506 | for ( it = list.begin(); it != list.end(); ++it ) { | 520 | for ( it = list.begin(); it != list.end(); ++it ) { |
507 | QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); | 521 | QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); |
508 | if ( !id.isEmpty() ) | 522 | if ( !id.isEmpty() ) |
509 | (*it).setIDStr(id ); | 523 | (*it).setIDStr(id ); |
510 | (*it).setResource( 0 ); | 524 | (*it).setResource( setRes ); |
511 | if ( replaceLabel ) | 525 | if ( replaceLabel ) |
512 | (*it).removeVoice(); | 526 | (*it).removeVoice(); |
513 | if ( removeOld ) | 527 | if ( removeOld ) |
514 | (*it).setTagged( true ); | 528 | (*it).setTagged( true ); |
515 | insertAddressee( (*it), false, true ); | 529 | insertAddressee( (*it), false, true ); |
516 | } | 530 | } |
517 | if ( removeOld ) | 531 | if ( removeOld ) |
518 | removeUntagged(); | 532 | removeUntagged(); |
519 | return list.count(); | 533 | return list.count(); |
520 | } | 534 | } |
521 | void AddressBook::setUntagged(bool setNonSyncTagged) // = false) | 535 | void AddressBook::setUntagged(bool setNonSyncTagged, QString resource) // = false , "") |
522 | { | 536 | { |
523 | Iterator ait; | 537 | Iterator ait; |
524 | for ( ait = begin(); ait != end(); ++ait ) { | 538 | if ( !resource.isEmpty() ) { |
525 | if ( setNonSyncTagged ) { | 539 | for ( ait = begin(); ait != end(); ++ait ) { |
526 | if ( (*ait).resource() && ! (*ait).resource()->includeInSync() ) { | 540 | if ( (*ait).resource() && (*ait).resource()->name() == resource ) { |
541 | (*ait).setTagged( false ); | ||
542 | } | ||
543 | else | ||
527 | (*ait).setTagged( true ); | 544 | (*ait).setTagged( true ); |
545 | } | ||
546 | } else { | ||
547 | for ( ait = begin(); ait != end(); ++ait ) { | ||
548 | if ( setNonSyncTagged ) { | ||
549 | if ( (*ait).resource() && ! (*ait).resource()->includeInSync() ) { | ||
550 | (*ait).setTagged( true ); | ||
551 | } else | ||
552 | (*ait).setTagged( false ); | ||
528 | } else | 553 | } else |
529 | (*ait).setTagged( false ); | 554 | (*ait).setTagged( false ); |
530 | } else | 555 | } |
531 | (*ait).setTagged( false ); | ||
532 | } | 556 | } |
533 | } | 557 | } |
534 | void AddressBook::removeUntagged() | 558 | void AddressBook::removeUntagged() |
535 | { | 559 | { |
536 | Iterator ait; | 560 | Iterator ait; |
537 | bool todelete = false; | 561 | bool todelete = false; |
538 | Iterator todel; | 562 | Iterator todel; |
539 | for ( ait = begin(); ait != end(); ++ait ) { | 563 | for ( ait = begin(); ait != end(); ++ait ) { |
540 | if ( todelete ) | 564 | if ( todelete ) |
541 | removeAddressee( todel ); | 565 | removeAddressee( todel ); |
542 | if (!(*ait).tagged()) { | 566 | if (!(*ait).tagged()) { |
543 | todelete = true; | 567 | todelete = true; |
544 | todel = ait; | 568 | todel = ait; |
545 | } else | 569 | } else |
546 | todelete = false; | 570 | todelete = false; |
547 | } | 571 | } |
548 | if ( todelete ) | 572 | if ( todelete ) |
549 | removeAddressee( todel ); | 573 | removeAddressee( todel ); |
550 | deleteRemovedAddressees(); | 574 | deleteRemovedAddressees(); |
551 | } | 575 | } |
552 | void AddressBook::smplifyAddressees() | 576 | void AddressBook::smplifyAddressees() |
553 | { | 577 | { |
554 | Iterator ait; | 578 | Iterator ait; |
555 | for ( ait = begin(); ait != end(); ++ait ) { | 579 | for ( ait = begin(); ait != end(); ++ait ) { |
556 | (*ait).simplifyEmails(); | 580 | (*ait).simplifyEmails(); |
557 | (*ait).simplifyPhoneNumbers(); | 581 | (*ait).simplifyPhoneNumbers(); |
558 | (*ait).simplifyPhoneNumberTypes(); | 582 | (*ait).simplifyPhoneNumberTypes(); |
559 | (*ait).simplifyAddresses(); | 583 | (*ait).simplifyAddresses(); |
560 | } | 584 | } |
561 | } | 585 | } |
562 | void AddressBook::removeSyncInfo( QString syncProfile) | 586 | void AddressBook::removeSyncInfo( QString syncProfile) |
563 | { | 587 | { |
564 | Iterator ait; | 588 | Iterator ait; |
565 | for ( ait = begin(); ait != end(); ++ait ) { | 589 | for ( ait = begin(); ait != end(); ++ait ) { |
566 | (*ait).removeID( syncProfile ); | 590 | (*ait).removeID( syncProfile ); |
567 | } | 591 | } |
568 | if ( syncProfile.isEmpty() ) { | 592 | if ( syncProfile.isEmpty() ) { |
569 | Iterator it = begin(); | 593 | Iterator it = begin(); |
570 | Iterator it2 ; | 594 | Iterator it2 ; |
571 | while ( it != end() ) { | 595 | while ( it != end() ) { |
572 | if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { | 596 | if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { |
573 | it2 = it; | 597 | it2 = it; |
574 | //qDebug("removing %s ",(*it).uid().latin1() ); | 598 | //qDebug("removing %s ",(*it).uid().latin1() ); |
575 | ++it; | 599 | ++it; |
576 | removeAddressee( it2 ); | 600 | removeAddressee( it2 ); |
577 | } else { | 601 | } else { |
578 | //qDebug("skipping %s ",(*it).uid().latin1() ); | 602 | //qDebug("skipping %s ",(*it).uid().latin1() ); |
579 | ++it; | 603 | ++it; |