-rw-r--r-- | kabc/addressbook.cpp | 148 | ||||
-rw-r--r-- | kabc/addressbook.h | 2 | ||||
-rw-r--r-- | kabc/addressee.cpp | 46 | ||||
-rw-r--r-- | kabc/addressee.h | 1 | ||||
-rw-r--r-- | kabc/phonenumber.cpp | 4 |
5 files changed, 182 insertions, 19 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp index d037d2f..ad0f702 100644 --- a/kabc/addressbook.cpp +++ b/kabc/addressbook.cpp @@ -341,17 +341,16 @@ AddressBook::~AddressBook() 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 ) { @@ -404,6 +403,75 @@ void AddressBook::export2File( QString fileName ) 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 ) { @@ -425,6 +493,9 @@ void AddressBook::importFromFile( QString fileName, bool replaceLabel, bool rem 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(); @@ -480,15 +551,59 @@ void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync ) (*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) ) { @@ -498,9 +613,6 @@ bool AddressBook::saveABphone( QString fileName ) if ( !PhoneAccess::readFromPhone( fileName) ) { return false; } - qDebug("reloading phone book... "); - if ( !load() ) - return false; return true; } bool AddressBook::saveAB() @@ -516,12 +628,19 @@ bool AddressBook::saveAB() } 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; } @@ -530,6 +649,9 @@ bool AddressBook::saveAB() if ( ticket->resource() ) { if ( ! ticket->resource()->save( ticket ) ) ok = false; + else + qDebug("StdAddressBook::saved '%s'", ticket->resource()->resourceName().latin1() ); + } else ok = false; @@ -593,7 +715,7 @@ Ticket *AddressBook::requestSaveTicket( Resource *resource ) 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-") ) { diff --git a/kabc/addressbook.h b/kabc/addressbook.h index cc755d1..df9048b 100644 --- a/kabc/addressbook.h +++ b/kabc/addressbook.h @@ -146,9 +146,11 @@ class AddressBook : public QObject void smplifyAddressees(); void preparePhoneSync( QString currentSyncDevice, bool isPreSync ); void export2File( QString fileName ); + bool export2PhoneFormat( QStringList uids ,QString fileName ); void importFromFile( QString fileName, bool replaceLabel = false, bool removeOld = false ); void setUntagged(); void removeUntagged(); + void findNewExtIds( QString fileName, QString currentSyncDevice ); /** Returns a iterator for first entry of address book. */ diff --git a/kabc/addressee.cpp b/kabc/addressee.cpp index 607ae26..548305a 100644 --- a/kabc/addressee.cpp +++ b/kabc/addressee.cpp @@ -303,7 +303,13 @@ void Addressee::mergeContact( const Addressee& ad ) if ( mData->sortString.isEmpty() ) mData->sortString = ad.mData->sortString; if ( !mData->secrecy.isValid() ) mData->secrecy = ad.mData->secrecy; if ( ( !mData->url.isValid() && ad.mData->url.isValid() ) ) mData->url = ad.mData->url ; + PhoneNumber::List phoneAD = phoneNumbers(); + PhoneNumber::List::Iterator phoneItAD; + bool found = false; + for ( phoneItAD = phoneAD.begin(); phoneItAD != phoneAD.end(); ++phoneItAD ) { + + } // pending: // merging phonenumbers // merging addresses @@ -311,7 +317,7 @@ void Addressee::mergeContact( const Addressee& ad ) // merging categories; // merging custom; // merging keys - qDebug("merge contact %s ", ad.uid().latin1()); + //qDebug("merge contact %s ", ad.uid().latin1()); setUid( ad.uid() ); setRevision( ad.revision() ); } @@ -333,14 +339,48 @@ bool Addressee::removeVoice() } return found; } + +bool Addressee::containsAdr(const Addressee& ad ) +{ + if ( ! ad.mData->familyName.isEmpty() ) if ( mData->familyName != ad.mData->familyName) return false; + if ( ! ad.mData->givenName.isEmpty() )if ( mData->givenName != ad.mData->givenName ) return false; + if ( ad.mData->url.isValid() ) if (mData->url != ad.mData->url) return false ; + if ( ! ad.mData->role.isEmpty() ) if (mData->role != ad.mData->role) return false ; + if ( ! ad.mData->organization.isEmpty() ) if (mData->organization != ad.mData->organization) return false ; + if ( ! ad.mData->note.isEmpty() ) if (mData->note != ad.mData->note) return false ; + if ( ! ad.mData->title .isEmpty() ) if (mData->title != ad.mData->title ) return false ; + + // compare phone numbers + PhoneNumber::List phoneN = ad.phoneNumbers(); + PhoneNumber::List::Iterator phoneIt; + bool found = false; + for ( phoneIt = phoneN.begin(); phoneIt != phoneN.end(); ++phoneIt ) { + bool found = false; + PhoneNumber::List phoneL = ad.phoneNumbers(); + PhoneNumber::List::Iterator phoneItL; + for ( phoneItL = phoneL.begin(); phoneItL != phoneL.end(); ++phoneItL ) { + if ( ( *phoneItL ).number() == ( *phoneIt ).number() ) { + found = true; + break; + } + } + if ( ! found ) + return false; + } + return true; + +} void Addressee::simplifyAddresses() { - if ( mData->addresses.count() < 3 ) return ; + int max = 2; + if ( mData->url.isValid() ) + max = 1; + if ( mData->addresses.count() <= max ) return ; int count = 0; Address::List list; Address::List::Iterator it; for( it = mData->addresses.begin(); it != mData->addresses.end(); ++it ) { - if ( count > 1 ) + if ( count >= max ) list.append( *it ); ++count; } diff --git a/kabc/addressee.h b/kabc/addressee.h index 0aa2c51..03138f6 100644 --- a/kabc/addressee.h +++ b/kabc/addressee.h @@ -122,6 +122,7 @@ class Addressee void simplifyPhoneNumbers(); void simplifyPhoneNumberTypes(); bool removeVoice(); + bool containsAdr(const Addressee& addr ); /** Set unique identifier. diff --git a/kabc/phonenumber.cpp b/kabc/phonenumber.cpp index e5abc0e..6e94c7e 100644 --- a/kabc/phonenumber.cpp +++ b/kabc/phonenumber.cpp @@ -72,10 +72,8 @@ bool PhoneNumber::simplifyNumber() int i; Number = mNumber.stripWhiteSpace (); mNumber = ""; - if ( Number.at(0) == '+' ) - mNumber += "+"; for ( i = 0; i < Number.length(); ++i) { - if ( Number.at(i).isDigit() ) + if ( Number.at(i).isDigit() || Number.at(i) == '+'|| Number.at(i) == '*'|| Number.at(i) == '#' ) mNumber += Number.at(i); } return ( mNumber.length() > 0 ); |