-rw-r--r-- | kabc/addressbook.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp index 16927e2..d037d2f 100644 --- a/kabc/addressbook.cpp +++ b/kabc/addressbook.cpp | |||
@@ -2,96 +2,97 @@ | |||
2 | This file is part of libkabc. | 2 | This file is part of libkabc. |
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> | 3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> |
4 | 4 | ||
5 | This library is free software; you can redistribute it and/or | 5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public | 6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either | 7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. | 8 | version 2 of the License, or (at your option) any later version. |
9 | 9 | ||
10 | This library is distributed in the hope that it will be useful, | 10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. | 13 | Library General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Library General Public License | 15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to | 16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /* | 21 | /* |
22 | Enhanced Version of the file for platform independent KDE tools. | 22 | Enhanced Version of the file for platform independent KDE tools. |
23 | Copyright (c) 2004 Ulf Schenk | 23 | Copyright (c) 2004 Ulf Schenk |
24 | 24 | ||
25 | $Id$ | 25 | $Id$ |
26 | */ | 26 | */ |
27 | 27 | ||
28 | /*US | 28 | /*US |
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> | 41 | #include <qtextstream.h> |
42 | #include <qfile.h> | 42 | #include <qfile.h> |
43 | #include <qregexp.h> | 43 | #include <qregexp.h> |
44 | 44 | ||
45 | #include <kglobal.h> | 45 | #include <kglobal.h> |
46 | #include <klocale.h> | 46 | #include <klocale.h> |
47 | #include <kmessagebox.h> | 47 | #include <kmessagebox.h> |
48 | #include <kdebug.h> | 48 | #include <kdebug.h> |
49 | #include <libkcal/syncdefines.h> | 49 | #include <libkcal/syncdefines.h> |
50 | #include <libkdepim/phoneaccess.h> | ||
50 | #include "addressbook.h" | 51 | #include "addressbook.h" |
51 | #include "resource.h" | 52 | #include "resource.h" |
52 | #include "vcardconverter.h" | 53 | #include "vcardconverter.h" |
53 | #include "vcardparser/vcardtool.h" | 54 | #include "vcardparser/vcardtool.h" |
54 | 55 | ||
55 | //US #include "addressbook.moc" | 56 | //US #include "addressbook.moc" |
56 | 57 | ||
57 | using namespace KABC; | 58 | using namespace KABC; |
58 | 59 | ||
59 | struct AddressBook::AddressBookData | 60 | struct AddressBook::AddressBookData |
60 | { | 61 | { |
61 | Addressee::List mAddressees; | 62 | Addressee::List mAddressees; |
62 | Addressee::List mRemovedAddressees; | 63 | Addressee::List mRemovedAddressees; |
63 | Field::List mAllFields; | 64 | Field::List mAllFields; |
64 | KConfig *mConfig; | 65 | KConfig *mConfig; |
65 | KRES::Manager<Resource> *mManager; | 66 | KRES::Manager<Resource> *mManager; |
66 | //US ErrorHandler *mErrorHandler; | 67 | //US ErrorHandler *mErrorHandler; |
67 | }; | 68 | }; |
68 | 69 | ||
69 | struct AddressBook::Iterator::IteratorData | 70 | struct AddressBook::Iterator::IteratorData |
70 | { | 71 | { |
71 | Addressee::List::Iterator mIt; | 72 | Addressee::List::Iterator mIt; |
72 | }; | 73 | }; |
73 | 74 | ||
74 | struct AddressBook::ConstIterator::ConstIteratorData | 75 | struct AddressBook::ConstIterator::ConstIteratorData |
75 | { | 76 | { |
76 | Addressee::List::ConstIterator mIt; | 77 | Addressee::List::ConstIterator mIt; |
77 | }; | 78 | }; |
78 | 79 | ||
79 | AddressBook::Iterator::Iterator() | 80 | AddressBook::Iterator::Iterator() |
80 | { | 81 | { |
81 | d = new IteratorData; | 82 | d = new IteratorData; |
82 | } | 83 | } |
83 | 84 | ||
84 | AddressBook::Iterator::Iterator( const AddressBook::Iterator &i ) | 85 | AddressBook::Iterator::Iterator( const AddressBook::Iterator &i ) |
85 | { | 86 | { |
86 | d = new IteratorData; | 87 | d = new IteratorData; |
87 | d->mIt = i.d->mIt; | 88 | d->mIt = i.d->mIt; |
88 | } | 89 | } |
89 | 90 | ||
90 | AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i ) | 91 | AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i ) |
91 | { | 92 | { |
92 | if( this == &i ) return *this; // guard against self assignment | 93 | if( this == &i ) return *this; // guard against self assignment |
93 | delete d; // delete the old data the Iterator was completely constructed before | 94 | delete d; // delete the old data the Iterator was completely constructed before |
94 | d = new IteratorData; | 95 | d = new IteratorData; |
95 | d->mIt = i.d->mIt; | 96 | d->mIt = i.d->mIt; |
96 | return *this; | 97 | return *this; |
97 | } | 98 | } |
@@ -414,96 +415,139 @@ void AddressBook::importFromFile( QString fileName, bool replaceLabel, bool rem | |||
414 | QByteArray rawData = file.readAll(); | 415 | QByteArray rawData = file.readAll(); |
415 | file.close(); | 416 | file.close(); |
416 | QString data; | 417 | QString data; |
417 | if ( replaceLabel ) { | 418 | if ( replaceLabel ) { |
418 | data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); | 419 | data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); |
419 | data.replace ( QRegExp("LABEL") , "ADR" ); | 420 | data.replace ( QRegExp("LABEL") , "ADR" ); |
420 | data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); | 421 | data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); |
421 | } else | 422 | } else |
422 | data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); | 423 | data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); |
423 | KABC::VCardTool tool; | 424 | KABC::VCardTool tool; |
424 | list = tool.parseVCards( data ); | 425 | list = tool.parseVCards( data ); |
425 | KABC::Addressee::List::Iterator it; | 426 | KABC::Addressee::List::Iterator it; |
426 | for ( it = list.begin(); it != list.end(); ++it ) { | 427 | for ( it = list.begin(); it != list.end(); ++it ) { |
427 | (*it).setResource( 0 ); | 428 | (*it).setResource( 0 ); |
428 | if ( replaceLabel ) | 429 | if ( replaceLabel ) |
429 | (*it).removeVoice(); | 430 | (*it).removeVoice(); |
430 | if ( removeOld ) | 431 | if ( removeOld ) |
431 | (*it).setTagged( true ); | 432 | (*it).setTagged( true ); |
432 | insertAddressee( (*it), false, true ); | 433 | insertAddressee( (*it), false, true ); |
433 | } | 434 | } |
434 | if ( removeOld ) | 435 | if ( removeOld ) |
435 | removeUntagged(); | 436 | removeUntagged(); |
436 | } | 437 | } |
437 | void AddressBook::setUntagged() | 438 | void AddressBook::setUntagged() |
438 | { | 439 | { |
439 | Iterator ait; | 440 | Iterator ait; |
440 | for ( ait = begin(); ait != end(); ++ait ) { | 441 | for ( ait = begin(); ait != end(); ++ait ) { |
441 | (*ait).setTagged( false ); | 442 | (*ait).setTagged( false ); |
442 | } | 443 | } |
443 | } | 444 | } |
444 | void AddressBook::removeUntagged() | 445 | void AddressBook::removeUntagged() |
445 | { | 446 | { |
446 | Iterator ait; | 447 | Iterator ait; |
447 | bool todelete = false; | 448 | bool todelete = false; |
448 | Iterator todel; | 449 | Iterator todel; |
449 | for ( ait = begin(); ait != end(); ++ait ) { | 450 | for ( ait = begin(); ait != end(); ++ait ) { |
450 | if ( todelete ) | 451 | if ( todelete ) |
451 | removeAddressee( todel ); | 452 | removeAddressee( todel ); |
452 | if (!(*ait).tagged()) { | 453 | if (!(*ait).tagged()) { |
453 | todelete = true; | 454 | todelete = true; |
454 | todel = ait; | 455 | todel = ait; |
455 | } else | 456 | } else |
456 | todelete = false; | 457 | todelete = false; |
457 | } | 458 | } |
458 | if ( todelete ) | 459 | if ( todelete ) |
459 | removeAddressee( todel ); | 460 | removeAddressee( todel ); |
460 | deleteRemovedAddressees(); | 461 | deleteRemovedAddressees(); |
461 | } | 462 | } |
463 | void AddressBook::smplifyAddressees() | ||
464 | { | ||
465 | Iterator ait; | ||
466 | for ( ait = begin(); ait != end(); ++ait ) { | ||
467 | (*ait).simplifyEmails(); | ||
468 | (*ait).simplifyPhoneNumbers(); | ||
469 | (*ait).simplifyPhoneNumberTypes(); | ||
470 | (*ait).simplifyAddresses(); | ||
471 | } | ||
472 | } | ||
473 | void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync ) | ||
474 | { | ||
475 | Iterator ait; | ||
476 | for ( ait = begin(); ait != end(); ++ait ) { | ||
477 | QString id = (*ait).IDStr(); | ||
478 | (*ait).setIDStr( ":"); | ||
479 | (*ait).setExternalUID( id ); | ||
480 | (*ait).setOriginalExternalUID( id ); | ||
481 | if ( isPreSync ) | ||
482 | (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL ); | ||
483 | else | ||
484 | (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); | ||
485 | } | ||
486 | } | ||
487 | bool AddressBook::saveABphone( QString fileName ) | ||
488 | { | ||
489 | smplifyAddressees(); | ||
490 | qDebug("saveABphone:: saving AB... "); | ||
491 | if ( ! saveAB() ) | ||
492 | return false; | ||
493 | qDebug("saveABphone:: writing to phone... "); | ||
494 | if ( !PhoneAccess::writeToPhone( fileName) ) { | ||
495 | return false; | ||
496 | } | ||
497 | qDebug("saveABphone:: re-reading from phone... "); | ||
498 | if ( !PhoneAccess::readFromPhone( fileName) ) { | ||
499 | return false; | ||
500 | } | ||
501 | qDebug("reloading phone book... "); | ||
502 | if ( !load() ) | ||
503 | return false; | ||
504 | return true; | ||
505 | } | ||
462 | bool AddressBook::saveAB() | 506 | bool AddressBook::saveAB() |
463 | { | 507 | { |
464 | bool ok = true; | 508 | bool ok = true; |
465 | 509 | ||
466 | deleteRemovedAddressees(); | 510 | deleteRemovedAddressees(); |
467 | Iterator ait; | 511 | Iterator ait; |
468 | for ( ait = begin(); ait != end(); ++ait ) { | 512 | for ( ait = begin(); ait != end(); ++ait ) { |
469 | if ( !(*ait).IDStr().isEmpty() ) { | 513 | if ( !(*ait).IDStr().isEmpty() ) { |
470 | (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); | 514 | (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); |
471 | } | 515 | } |
472 | } | 516 | } |
473 | KRES::Manager<Resource>::ActiveIterator it; | 517 | KRES::Manager<Resource>::ActiveIterator it; |
474 | KRES::Manager<Resource> *manager = d->mManager; | 518 | KRES::Manager<Resource> *manager = d->mManager; |
475 | for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { | 519 | for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { |
476 | if ( !(*it)->readOnly() && (*it)->isOpen() ) { | 520 | if ( !(*it)->readOnly() && (*it)->isOpen() ) { |
477 | Ticket *ticket = requestSaveTicket( *it ); | 521 | Ticket *ticket = requestSaveTicket( *it ); |
478 | // qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); | 522 | // qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); |
479 | if ( !ticket ) { | 523 | if ( !ticket ) { |
480 | error( i18n( "Unable to save to resource '%1'. It is locked." ) | 524 | error( i18n( "Unable to save to resource '%1'. It is locked." ) |
481 | .arg( (*it)->resourceName() ) ); | 525 | .arg( (*it)->resourceName() ) ); |
482 | return false; | 526 | return false; |
483 | } | 527 | } |
484 | 528 | ||
485 | //if ( !save( ticket ) ) | 529 | //if ( !save( ticket ) ) |
486 | if ( ticket->resource() ) { | 530 | if ( ticket->resource() ) { |
487 | if ( ! ticket->resource()->save( ticket ) ) | 531 | if ( ! ticket->resource()->save( ticket ) ) |
488 | ok = false; | 532 | ok = false; |
489 | } else | 533 | } else |
490 | ok = false; | 534 | ok = false; |
491 | 535 | ||
492 | } | 536 | } |
493 | } | 537 | } |
494 | return ok; | 538 | return ok; |
495 | } | 539 | } |
496 | 540 | ||
497 | AddressBook::Iterator AddressBook::begin() | 541 | AddressBook::Iterator AddressBook::begin() |
498 | { | 542 | { |
499 | Iterator it = Iterator(); | 543 | Iterator it = Iterator(); |
500 | it.d->mIt = d->mAddressees.begin(); | 544 | it.d->mIt = d->mAddressees.begin(); |
501 | return it; | 545 | return it; |
502 | } | 546 | } |
503 | 547 | ||
504 | AddressBook::ConstIterator AddressBook::begin() const | 548 | AddressBook::ConstIterator AddressBook::begin() const |
505 | { | 549 | { |
506 | ConstIterator it = ConstIterator(); | 550 | ConstIterator it = ConstIterator(); |
507 | it.d->mIt = d->mAddressees.begin(); | 551 | it.d->mIt = d->mAddressees.begin(); |
508 | return it; | 552 | return it; |
509 | } | 553 | } |