-rw-r--r-- | kabc/addressbook.cpp | 70 |
1 files changed, 64 insertions, 6 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp index 5fb49eb..295cf03 100644 --- a/kabc/addressbook.cpp +++ b/kabc/addressbook.cpp | |||
@@ -29,31 +29,36 @@ $Id$ | |||
29 | 29 | ||
30 | #include <qfile.h> | 30 | #include <qfile.h> |
31 | #include <qregexp.h> | 31 | #include <qregexp.h> |
32 | #include <qtimer.h> | 32 | #include <qtimer.h> |
33 | 33 | ||
34 | #include <kapplication.h> | 34 | #include <kapplication.h> |
35 | #include <kinstance.h> | 35 | #include <kinstance.h> |
36 | #include <kstandarddirs.h> | 36 | #include <kstandarddirs.h> |
37 | 37 | ||
38 | #include "errorhandler.h" | 38 | #include "errorhandler.h" |
39 | */ | 39 | */ |
40 | #include <qptrlist.h> | 40 | #include <qptrlist.h> |
41 | #include <qtextstream.h> | ||
42 | #include <qfile.h> | ||
41 | 43 | ||
42 | #include <kglobal.h> | 44 | #include <kglobal.h> |
43 | #include <klocale.h> | 45 | #include <klocale.h>> |
46 | #include <kmessagebox.h> | ||
44 | #include <kdebug.h> | 47 | #include <kdebug.h> |
45 | #include <libkcal/syncdefines.h> | 48 | #include <libkcal/syncdefines.h> |
46 | #include "addressbook.h" | 49 | #include "addressbook.h" |
47 | #include "resource.h" | 50 | #include "resource.h" |
51 | #include "vcardconverter.h" | ||
52 | #include "vcardparser/vcardtool.h" | ||
48 | 53 | ||
49 | //US #include "addressbook.moc" | 54 | //US #include "addressbook.moc" |
50 | 55 | ||
51 | using namespace KABC; | 56 | using namespace KABC; |
52 | 57 | ||
53 | struct AddressBook::AddressBookData | 58 | struct AddressBook::AddressBookData |
54 | { | 59 | { |
55 | Addressee::List mAddressees; | 60 | Addressee::List mAddressees; |
56 | Addressee::List mRemovedAddressees; | 61 | Addressee::List mRemovedAddressees; |
57 | Field::List mAllFields; | 62 | Field::List mAllFields; |
58 | KConfig *mConfig; | 63 | KConfig *mConfig; |
59 | KRES::Manager<Resource> *mManager; | 64 | KRES::Manager<Resource> *mManager; |
@@ -361,24 +366,72 @@ bool AddressBook::load() | |||
361 | 366 | ||
362 | bool AddressBook::save( Ticket *ticket ) | 367 | bool AddressBook::save( Ticket *ticket ) |
363 | { | 368 | { |
364 | kdDebug(5700) << "AddressBook::save()"<< endl; | 369 | kdDebug(5700) << "AddressBook::save()"<< endl; |
365 | 370 | ||
366 | if ( ticket->resource() ) { | 371 | if ( ticket->resource() ) { |
367 | deleteRemovedAddressees(); | 372 | deleteRemovedAddressees(); |
368 | return ticket->resource()->save( ticket ); | 373 | return ticket->resource()->save( ticket ); |
369 | } | 374 | } |
370 | 375 | ||
371 | return false; | 376 | return false; |
372 | } | 377 | } |
378 | void AddressBook::export2File( QString fileName ) | ||
379 | { | ||
380 | |||
381 | QFile outFile( fileName ); | ||
382 | if ( !outFile.open( IO_WriteOnly ) ) { | ||
383 | QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" ); | ||
384 | KMessageBox::error( 0, text.arg( fileName ) ); | ||
385 | return ; | ||
386 | } | ||
387 | QTextStream t( &outFile ); | ||
388 | t.setEncoding( QTextStream::UnicodeUTF8 ); | ||
389 | Iterator it; | ||
390 | KABC::VCardConverter::Version version; | ||
391 | version = KABC::VCardConverter::v3_0; | ||
392 | for ( it = begin(); it != end(); ++it ) { | ||
393 | if ( !(*it).IDStr().isEmpty() ) { | ||
394 | (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() ); | ||
395 | } | ||
396 | KABC::VCardConverter converter; | ||
397 | QString vcard; | ||
398 | //Resource *resource() const; | ||
399 | converter.addresseeToVCard( *it, vcard, version ); | ||
400 | t << vcard << "\r\n"; | ||
401 | } | ||
402 | outFile.close(); | ||
403 | } | ||
404 | void AddressBook::importFromFile( QString fileName ) | ||
405 | { | ||
406 | |||
407 | KABC::Addressee::List list; | ||
408 | QFile file( fileName ); | ||
409 | |||
410 | file.open( IO_ReadOnly ); | ||
411 | QByteArray rawData = file.readAll(); | ||
412 | file.close(); | ||
413 | |||
414 | QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); | ||
415 | KABC::VCardTool tool; | ||
416 | list = tool.parseVCards( data ); | ||
417 | |||
418 | KABC::Addressee::List::Iterator it; | ||
419 | for ( it = list.begin(); it != list.end(); ++it ) { | ||
420 | (*it).setResource( 0 ); | ||
421 | insertAddressee( (*it), false, true ); | ||
422 | } | ||
423 | |||
424 | } | ||
425 | |||
373 | bool AddressBook::saveAB() | 426 | bool AddressBook::saveAB() |
374 | { | 427 | { |
375 | bool ok = true; | 428 | bool ok = true; |
376 | 429 | ||
377 | deleteRemovedAddressees(); | 430 | deleteRemovedAddressees(); |
378 | Iterator ait; | 431 | Iterator ait; |
379 | for ( ait = begin(); ait != end(); ++ait ) { | 432 | for ( ait = begin(); ait != end(); ++ait ) { |
380 | if ( !(*ait).IDStr().isEmpty() ) { | 433 | if ( !(*ait).IDStr().isEmpty() ) { |
381 | (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); | 434 | (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); |
382 | } | 435 | } |
383 | } | 436 | } |
384 | KRES::Manager<Resource>::ActiveIterator it; | 437 | KRES::Manager<Resource>::ActiveIterator it; |
@@ -452,45 +505,50 @@ Ticket *AddressBook::requestSaveTicket( Resource *resource ) | |||
452 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { | 505 | for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { |
453 | if ( (*it) == resource ) { | 506 | if ( (*it) == resource ) { |
454 | if ( (*it)->readOnly() || !(*it)->isOpen() ) | 507 | if ( (*it)->readOnly() || !(*it)->isOpen() ) |
455 | return 0; | 508 | return 0; |
456 | else | 509 | else |
457 | return (*it)->requestSaveTicket(); | 510 | return (*it)->requestSaveTicket(); |
458 | } | 511 | } |
459 | } | 512 | } |
460 | 513 | ||
461 | return 0; | 514 | return 0; |
462 | } | 515 | } |
463 | 516 | ||
464 | void AddressBook::insertAddressee( const Addressee &a, bool setRev ) | 517 | void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource ) |
465 | { | 518 | { |
466 | if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { | 519 | if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { |
467 | //qDebug("block insert "); | 520 | //qDebug("block insert "); |
468 | return; | 521 | return; |
469 | } | 522 | } |
470 | //qDebug("inserting.... %s ",a.uid().latin1() ); | 523 | //qDebug("inserting.... %s ",a.uid().latin1() ); |
471 | bool found = false; | 524 | bool found = false; |
472 | Addressee::List::Iterator it; | 525 | Addressee::List::Iterator it; |
473 | for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { | 526 | for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { |
474 | if ( a.uid() == (*it).uid() ) { | 527 | if ( a.uid() == (*it).uid() ) { |
475 | 528 | ||
476 | bool changed = false; | 529 | bool changed = false; |
477 | Addressee addr = a; | 530 | Addressee addr = a; |
478 | if ( addr != (*it) ) | 531 | if ( addr != (*it) ) |
479 | changed = true; | 532 | changed = true; |
480 | 533 | ||
481 | (*it) = a; | 534 | if ( takeResource ) { |
482 | if ( (*it).resource() == 0 ) | 535 | Resource * res = (*it).resource(); |
483 | (*it).setResource( standardResource() ); | 536 | (*it) = a; |
484 | 537 | (*it).setResource( res ); | |
538 | } else { | ||
539 | (*it) = a; | ||
540 | if ( (*it).resource() == 0 ) | ||
541 | (*it).setResource( standardResource() ); | ||
542 | } | ||
485 | if ( changed ) { | 543 | if ( changed ) { |
486 | if ( setRev ) { | 544 | if ( setRev ) { |
487 | 545 | ||
488 | // get rid of micro seconds | 546 | // get rid of micro seconds |
489 | QDateTime dt = QDateTime::currentDateTime(); | 547 | QDateTime dt = QDateTime::currentDateTime(); |
490 | QTime t = dt.time(); | 548 | QTime t = dt.time(); |
491 | dt.setTime( QTime (t.hour (), t.minute (), t.second () ) ); | 549 | dt.setTime( QTime (t.hour (), t.minute (), t.second () ) ); |
492 | (*it).setRevision( dt ); | 550 | (*it).setRevision( dt ); |
493 | } | 551 | } |
494 | (*it).setChanged( true ); | 552 | (*it).setChanged( true ); |
495 | } | 553 | } |
496 | 554 | ||