-rw-r--r-- | kabc/addressbook.cpp | 148 |
1 files changed, 135 insertions, 13 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp index d037d2f..ad0f702 100644 --- a/kabc/addressbook.cpp +++ b/kabc/addressbook.cpp @@ -338,23 +338,22 @@ AddressBook::~AddressBook() delete d; d = 0; } bool AddressBook::load() { - clear(); - KRES::Manager<Resource>::ActiveIterator it; bool ok = true; for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) if ( !(*it)->load() ) { - error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); + qDebug( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); ok = false; + } else { + qDebug( i18n("Resource loaded: '%1'").arg( (*it)->resourceName() ) ); } - // mark all addressees as unchanged Addressee::List::Iterator addrIt; for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) { (*addrIt).setChanged( false ); QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" ); if ( !id.isEmpty() ) { @@ -401,12 +400,81 @@ void AddressBook::export2File( QString fileName ) converter.addresseeToVCard( *it, vcard, version ); t << vcard << "\r\n"; } t << "\r\n\r\n"; outFile.close(); } +// if QStringList uids is empty, all are exported +bool AddressBook::export2PhoneFormat( QStringList uids ,QString fileName ) +{ + KABC::VCardConverter converter; + QString datastream; + Iterator it; + bool all = uids.isEmpty(); + for ( it = begin(); it != end(); ++it ) { + // for( QStringList::ConstIterator it = uids.begin(); it != uids.end(); ++it ) { + if ( ! all ) { + if ( ! ( uids.contains((*it).uid() ) )) + continue; + } + KABC::Addressee a = ( *it ); + if ( a.isEmpty() ) + continue; + a.simplifyEmails(); + a.simplifyPhoneNumbers(); + a.simplifyPhoneNumberTypes(); + a.simplifyAddresses(); + + QString vcard; + QString vcardnew; + converter.addresseeToVCard( a, vcard ); + int start = 0; + int next; + while ( (next = vcard.find("TYPE=", start) )>= 0 ) { + int semi = vcard.find(";", next); + int dopp = vcard.find(":", next); + int sep; + if ( semi < dopp && semi >= 0 ) + sep = semi ; + else + sep = dopp; + vcardnew +=vcard.mid( start, next - start); + vcardnew +=vcard.mid( next+5,sep -next -5 ).upper(); + start = sep; + } + vcardnew += vcard.mid( start,vcard.length() ); + vcard = ""; + start = 0; + while ( (next = vcardnew.find("ADR", start) )>= 0 ) { + int sep = vcardnew.find(":", next); + vcard +=vcardnew.mid( start, next - start+3); + start = sep; + } + vcard += vcardnew.mid( start,vcardnew.length() ); + vcard.replace ( QRegExp(";;;") , "" ); + vcard.replace ( QRegExp(";;") , "" ); + datastream += vcard; + + } + + QFile outFile(fileName); + if ( outFile.open(IO_WriteOnly) ) { + datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" ); + QTextStream t( &outFile ); // use a text stream + t.setEncoding( QTextStream::UnicodeUTF8 ); + t <<datastream; + t << "\r\n\r\n"; + outFile.close(); + + } else { + qDebug("Error open temp file "); + return false; + } + return true; + +} void AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld ) { if ( removeOld ) setUntagged(); KABC::Addressee::List list; @@ -422,12 +490,15 @@ void AddressBook::importFromFile( QString fileName, bool replaceLabel, bool rem } else data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); KABC::VCardTool tool; list = tool.parseVCards( data ); KABC::Addressee::List::Iterator it; for ( it = list.begin(); it != list.end(); ++it ) { + QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); + if ( !id.isEmpty() ) + (*it).setIDStr(id ); (*it).setResource( 0 ); if ( replaceLabel ) (*it).removeVoice(); if ( removeOld ) (*it).setTagged( true ); insertAddressee( (*it), false, true ); @@ -477,33 +548,74 @@ void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync ) QString id = (*ait).IDStr(); (*ait).setIDStr( ":"); (*ait).setExternalUID( id ); (*ait).setOriginalExternalUID( id ); if ( isPreSync ) (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL ); - else + else { (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); + (*ait).setID( currentSyncDevice,id ); + + } + } +} +void AddressBook::findNewExtIds( QString fileName, QString currentSyncDevice ) +{ + + setUntagged(); + KABC::Addressee::List list; + QFile file( fileName ); + file.open( IO_ReadOnly ); + QByteArray rawData = file.readAll(); + file.close(); + QString data; + + data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); + KABC::VCardTool tool; + list = tool.parseVCards( data ); + KABC::Addressee::List::Iterator it; + for ( it = list.begin(); it != list.end(); ++it ) { + Iterator ait; + for ( ait = begin(); ait != end(); ++ait ) { + if ( !(*ait).tagged() ) { + if ( (*ait).containsAdr(*it)) { + (*ait).setTagged(true); + QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); + (*it).setIDStr( ":"); + (*it).setID( currentSyncDevice,id ); + (*it).setExternalUID( id ); + (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); + (*it).setUid( ( (*ait).uid() )); + break; + } + } + + } + if ( ait == end() ) + qDebug("ERROR:: no ext ID found for uid: %s", (*it).uid().latin1()); + } + clear(); + for ( it = list.begin(); it != list.end(); ++it ) { + insertAddressee( (*it) ); } } + bool AddressBook::saveABphone( QString fileName ) { - smplifyAddressees(); + //smplifyAddressees(); qDebug("saveABphone:: saving AB... "); - if ( ! saveAB() ) + if ( ! export2PhoneFormat( QStringList() ,fileName ) ) return false; qDebug("saveABphone:: writing to phone... "); if ( !PhoneAccess::writeToPhone( fileName) ) { return false; } qDebug("saveABphone:: re-reading from phone... "); if ( !PhoneAccess::readFromPhone( fileName) ) { return false; } - qDebug("reloading phone book... "); - if ( !load() ) - return false; return true; } bool AddressBook::saveAB() { bool ok = true; @@ -513,26 +625,36 @@ bool AddressBook::saveAB() if ( !(*ait).IDStr().isEmpty() ) { (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); } } KRES::Manager<Resource>::ActiveIterator it; KRES::Manager<Resource> *manager = d->mManager; + qDebug("SaveAB::saving..." ); for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { + qDebug("SaveAB::checking resource..." ); + if ( (*it)->readOnly() ) + qDebug("readonly." ); + if ( (*it)->isOpen() ) + qDebug("open" ); + if ( !(*it)->readOnly() && (*it)->isOpen() ) { Ticket *ticket = requestSaveTicket( *it ); -// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); + qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); if ( !ticket ) { - error( i18n( "Unable to save to resource '%1'. It is locked." ) + qDebug( i18n( "Unable to save to resource '%1'. It is locked." ) .arg( (*it)->resourceName() ) ); return false; } //if ( !save( ticket ) ) if ( ticket->resource() ) { if ( ! ticket->resource()->save( ticket ) ) ok = false; + else + qDebug("StdAddressBook::saved '%s'", ticket->resource()->resourceName().latin1() ); + } else ok = false; } } return ok; @@ -590,13 +712,13 @@ Ticket *AddressBook::requestSaveTicket( Resource *resource ) return (*it)->requestSaveTicket(); } } return 0; } - +//void insertAddressee( const Addressee &, bool setRev = true, bool takeResource = false); void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource ) { if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { //qDebug("block insert "); return; } |