author | zautrix <zautrix> | 2004-09-18 22:33:39 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-09-18 22:33:39 (UTC) |
commit | f370d0f89bcaeeb68bd60152a9812a9cd55e5d8a (patch) (side-by-side diff) | |
tree | c1a9de8b305200a7cec885764a0afb4beb92dabb | |
parent | 28f894fd2a0dfbd5f09f9e530f359981efe0198c (diff) | |
download | kdepimpi-f370d0f89bcaeeb68bd60152a9812a9cd55e5d8a.zip kdepimpi-f370d0f89bcaeeb68bd60152a9812a9cd55e5d8a.tar.gz kdepimpi-f370d0f89bcaeeb68bd60152a9812a9cd55e5d8a.tar.bz2 |
more AB sync
-rw-r--r-- | kabc/address.cpp | 13 | ||||
-rw-r--r-- | kabc/address.h | 1 | ||||
-rw-r--r-- | kabc/addressbook.cpp | 133 | ||||
-rw-r--r-- | kabc/addressbook.h | 4 | ||||
-rw-r--r-- | kabc/addressee.cpp | 99 | ||||
-rw-r--r-- | kabc/addressee.h | 2 | ||||
-rw-r--r-- | kaddressbook/kabcore.cpp | 36 |
7 files changed, 245 insertions, 43 deletions
diff --git a/kabc/address.cpp b/kabc/address.cpp index c820a6c..5ffe511 100644 --- a/kabc/address.cpp +++ b/kabc/address.cpp @@ -83,16 +83,29 @@ bool Address::isEmpty() const mPostalCode.isEmpty() && mCountry.isEmpty() && mLabel.isEmpty() ) { return true; } return false; } +QStringList Address::asList() +{ + QStringList result; + if ( ! mPostOfficeBox.isEmpty() )result.append(mPostOfficeBox); + if ( ! mExtended.isEmpty())result.append(mExtended); + if ( ! mStreet.isEmpty())result.append(mStreet); + if ( ! mLocality.isEmpty() )result.append(mLocality); + if ( ! mRegion.isEmpty())result.append(mRegion); + if ( ! mPostalCode.isEmpty())result.append(mPostalCode); + if ( ! mCountry.isEmpty())result.append(mCountry); + if ( ! mLabel.isEmpty() )result.append(mLabel); + return result; +} void Address::clear() { *this = Address(); } void Address::setId( const QString &id ) { mEmpty = false; diff --git a/kabc/address.h b/kabc/address.h index ad132a7..6b53c7e 100644 --- a/kabc/address.h +++ b/kabc/address.h @@ -102,16 +102,17 @@ class Address Returns true, if the address is empty. */ bool isEmpty() const; /** Clears all entries of the address. */ void clear(); + QStringList asList(); /** Sets the unique id. */ void setId( const QString & ); /* Returns the unique id. diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp index 16e1653..ec9f893 100644 --- a/kabc/addressbook.cpp +++ b/kabc/addressbook.cpp @@ -229,16 +229,17 @@ AddressBook::AddressBook( const QString &config, const QString &family ) { init(config, family); } // the default family is "contact" void AddressBook::init(const QString &config, const QString &family ) { + blockLSEchange = false; d = new AddressBookData; QString fami = family; qDebug("new ab "); if (config != 0) { qDebug("config != 0 "); if ( family == "syncContact" ) { qDebug("creating sync config "); fami = "contact"; @@ -340,16 +341,17 @@ bool AddressBook::load() ok = false; } // mark all addressees as unchanged Addressee::List::Iterator addrIt; for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) (*addrIt).setChanged( false ); + blockLSEchange = true; return ok; } bool AddressBook::save( Ticket *ticket ) { kdDebug(5700) << "AddressBook::save()"<< endl; if ( ticket->resource() ) { @@ -442,63 +444,120 @@ Ticket *AddressBook::requestSaveTicket( Resource *resource ) } } return 0; } void AddressBook::insertAddressee( const Addressee &a, bool setRev ) { - Addressee::List::Iterator it; - for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { - if ( a.uid() == (*it).uid() ) { - if ( setRev && (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { - return; - } - bool changed = false; - Addressee addr = a; - if ( addr != (*it) ) - changed = true; - - (*it) = a; - if ( (*it).resource() == 0 ) - (*it).setResource( standardResource() ); - - if ( changed ) { - if ( setRev ) { + if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { + return; + } + bool found = false; + Addressee::List::Iterator it; + for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { + if ( a.uid() == (*it).uid() ) { + + bool changed = false; + Addressee addr = a; + if ( addr != (*it) ) + changed = true; + + (*it) = a; + if ( (*it).resource() == 0 ) + (*it).setResource( standardResource() ); + + if ( changed ) { + if ( setRev ) { - // get rid of micro seconds - QDateTime dt = QDateTime::currentDateTime(); - QTime t = dt.time(); - dt.setTime( QTime (t.hour (), t.minute (), t.second () ) ); - (*it).setRevision( dt ); - } - (*it).setChanged( true ); - } - - return; + // get rid of micro seconds + QDateTime dt = QDateTime::currentDateTime(); + QTime t = dt.time(); + dt.setTime( QTime (t.hour (), t.minute (), t.second () ) ); + (*it).setRevision( dt ); + } + (*it).setChanged( true ); + } + + found = true; + } else { + if ( (*it).uid() == QString("last-syncAddressee-") ) { + QString name = (*it).uid().mid( 19 ); + Addressee b = a; + QString id = b.getID( name ); + if ( ! id.isEmpty() ) { + QString des = (*it).note(); + int startN; + if( (startN = des.find( id ) ) >= 0 ) { + int endN = des.find( ",", startN+1 ); + des = des.left( startN ) + des.mid( endN+1 ); + (*it).setNote( des ); + } + } + } + } } - } - d->mAddressees.append( a ); - Addressee& addr = d->mAddressees.last(); - if ( addr.resource() == 0 ) - addr.setResource( standardResource() ); + if ( found ) + return; + d->mAddressees.append( a ); + Addressee& addr = d->mAddressees.last(); + if ( addr.resource() == 0 ) + addr.setResource( standardResource() ); - addr.setChanged( true ); + addr.setChanged( true ); } void AddressBook::removeAddressee( const Addressee &a ) { Iterator it; + Iterator it2; + bool found = false; for ( it = begin(); it != end(); ++it ) { if ( a.uid() == (*it).uid() ) { - removeAddressee( it ); - return; + found = true; + it2 = it; + } else { + if ( (*it).uid() == QString("last-syncAddressee-") ) { + QString name = (*it).uid().mid( 19 ); + Addressee b = a; + QString id = b.getID( name ); + if ( ! id.isEmpty() ) { + QString des = (*it).note(); + if( des.find( id ) < 0 ) { + des += id + ","; + (*it).setNote( des ); + } + } + } + } } + + if ( found ) + removeAddressee( it2 ); + +} + +void AddressBook::removeDeletedAddressees() +{ + deleteRemovedAddressees(); + Iterator it = begin(); + Iterator it2 ; + QDateTime dt ( QDate( 2004,1,1) ); + while ( it != end() ) { + (*it).setRevision( dt ); + if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { + it2 = it; + ++it; + removeAddressee( it2 ); + } else + ++it; + } + deleteRemovedAddressees(); } void AddressBook::removeAddressee( const Iterator &it ) { d->mRemovedAddressees.append( (*it) ); d->mAddressees.remove( it.d->mIt ); } @@ -518,30 +577,32 @@ Addressee AddressBook::findByUid( const QString &uid ) Iterator it; for ( it = begin(); it != end(); ++it ) { if ( uid == (*it).uid() ) { return *it; } } return Addressee(); } +#if 0 Addressee::List AddressBook::getExternLastSyncAddressees() { Addressee::List results; Iterator it; for ( it = begin(); it != end(); ++it ) { if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) { - if ( (*it).familyName().left(3) == "E: " ) + if ( (*it).familyName().left(4) == "!E: " ) results.append( *it ); } } return results; } +#endif void AddressBook::resetTempSyncStat() { Iterator it; for ( it = begin(); it != end(); ++it ) { (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL ); } } diff --git a/kabc/addressbook.h b/kabc/addressbook.h index 253de68..2f2678b 100644 --- a/kabc/addressbook.h +++ b/kabc/addressbook.h @@ -284,19 +284,20 @@ class AddressBook : public QObject void error( const QString& ); /** Query all resources to clean up their lock files */ void cleanUp(); // sync stuff - Addressee::List getExternLastSyncAddressees(); + //Addressee::List getExternLastSyncAddressees(); void resetTempSyncStat(); QStringList uidList(); + void removeDeletedAddressees(); signals: /** Emitted, when the address book has changed on disk. */ void addressBookChanged( AddressBook * ); @@ -319,16 +320,17 @@ class AddressBook : public QObject void init(const QString &config, const QString &family); private: //US QPtrList<Resource> mDummy; // Remove in KDE 4 struct AddressBookData; AddressBookData *d; + bool blockLSEchange; }; QDataStream &operator<<( QDataStream &, const AddressBook & ); QDataStream &operator>>( QDataStream &, AddressBook & ); } #endif diff --git a/kabc/addressee.cpp b/kabc/addressee.cpp index 7f04d8f..0f5d605 100644 --- a/kabc/addressee.cpp +++ b/kabc/addressee.cpp @@ -169,16 +169,112 @@ bool Addressee::operator!=( const Addressee &a ) const { return !( a == *this ); } bool Addressee::isEmpty() const { return mData->empty; } +ulong Addressee::getCsum4List( const QStringList & attList) +{ + int max = attList.count(); + ulong cSum = 0; + int j,k,i; + int add; + for ( i = 0; i < max ; ++i ) { + QString s = attList[i]; + if ( ! s.isEmpty() ){ + j = s.length(); + for ( k = 0; k < j; ++k ) { + int mul = k +1; + add = s[k].unicode (); + if ( k < 16 ) + mul = mul * mul; + int ii = i+1; + add = add * mul *ii*ii*ii; + cSum += add; + } + } + + } + //QString dump = attList.join(","); + //qDebug("csum: %d %s", cSum,dump.latin1()); + + return cSum; + +} +void Addressee::computeCsum(const QString &dev) +{ + QStringList l; + if ( !mData->name.isEmpty() ) l.append(mData->name); + if ( !mData->formattedName.isEmpty() ) l.append(mData->formattedName ); + if ( !mData->familyName.isEmpty() ) l.append( mData->familyName ); + if ( !mData->givenName.isEmpty() ) l.append(mData->givenName ); + if ( !mData->additionalName ) l.append( mData->additionalName ); + if ( !mData->prefix.isEmpty() ) l.append( mData->prefix ); + if ( !mData->suffix.isEmpty() ) l.append( mData->suffix ); + if ( !mData->nickName.isEmpty() ) l.append( mData->nickName ); + if ( mData->birthday.isValid() ) l.append( mData->birthday.toString() ); + if ( !mData->mailer.isEmpty() ) l.append( mData->mailer ); + if ( mData->timeZone.isValid() ) l.append( mData->timeZone.asString() ); + if ( mData->geo.isValid() ) l.append( mData->geo.asString() ); + if ( !mData->title .isEmpty() ) l.append( mData->title ); + if ( !mData->role.isEmpty() ) l.append( mData->role ); + if ( !mData->organization.isEmpty() ) l.append( mData->organization ); + if ( !mData->note.isEmpty() ) l.append( mData->note ); + if ( !mData->productId.isEmpty() ) l.append(mData->productId ); + if ( !mData->sortString.isEmpty() ) l.append( mData->sortString ); + if ( mData->secrecy.isValid() ) l.append( mData->secrecy.asString()); + // if ( !mData->logo.isEmpty() ) l.append( ); + //if ( !mData->photo.isEmpty() ) l.append( ); + //if ( !mData->sound.isEmpty() ) l.append( ); + //if ( !mData->agent.isEmpty() ) l.append( ); + //if ( mData->url.isValid() ) l.append( ); +#if 0 + if ( !mData->phoneNumbers.isEmpty() ) l.append( ); + if ( !mData->addresses.isEmpty() ) l.append( ); + //if ( !mData->keys.isEmpty() ) l.append( ); + if ( !mData->emails.isEmpty() ) l.append( ); + if ( !mData->categories .isEmpty() ) l.append( ); + if ( !mData->custom.isEmpty() ) l.append( ); +#endif + KABC::PhoneNumber::List phoneNumbers; + KABC::PhoneNumber::List::Iterator phoneIter; + + QStringList t; + for ( phoneIter = mData->phoneNumbers.begin(); phoneIter != mData->phoneNumbers.end(); + ++phoneIter ) + t.append( ( *phoneIter ).number()+QString::number( ( *phoneIter ).type() ) ); + t.sort(); + uint iii; + for ( iii = 0; iii < t.count(); ++iii) + l.append( t[iii] ); + t = mData->emails; + t.sort(); + for ( iii = 0; iii < t.count(); ++iii) + l.append( t[iii] ); + t = mData->categories; + t.sort(); + for ( iii = 0; iii < t.count(); ++iii) + l.append( t[iii] ); + t = mData->custom; + t.sort(); + for ( iii = 0; iii < t.count(); ++iii) + l.append( t[iii] ); + KABC::Address::List::Iterator addressIter; + for ( addressIter = mData->addresses.begin(); addressIter != mData->addresses.end(); + ++addressIter ) { + t = (*addressIter).asList(); + t.sort(); + for ( iii = 0; iii < t.count(); ++iii) + l.append( t[iii] ); + } + setCsum( dev, QString::number (getCsum4List(l)) ); +} void Addressee::removeID(const QString &prof) { detach(); mData->mExternalId = KIdManager::removeId ( mData->mExternalId, prof); } void Addressee::setID( const QString & prof , const QString & id ) { @@ -1445,21 +1541,20 @@ void Addressee::insertCustom( const QString &app, const QString &name, detach(); mData->empty = false; QString qualifiedName = app + "-" + name + ":"; QStringList::Iterator it; for( it = mData->custom.begin(); it != mData->custom.end(); ++it ) { if ( (*it).startsWith( qualifiedName ) ) { - (*it) = qualifiedName + value; + (*it) = qualifiedName + value; return; } } - mData->custom.append( qualifiedName + value ); } void Addressee::removeCustom( const QString &app, const QString &name) { detach(); QString qualifiedName = app + "-" + name + ":"; diff --git a/kabc/addressee.h b/kabc/addressee.h index f098371..0805458 100644 --- a/kabc/addressee.h +++ b/kabc/addressee.h @@ -101,16 +101,18 @@ class Addressee int tempSyncStat() const; void setIDStr( const QString & ); QString IDStr() const; void setID( const QString &, const QString & ); QString getID( const QString & ); void setCsum( const QString &, const QString & ); QString getCsum( const QString & ); void removeID(const QString &); + void computeCsum(const QString &dev); + ulong getCsum4List( const QStringList & attList); /** Return, if the address book entry is empty. */ bool isEmpty() const; /** Set unique identifier. */ diff --git a/kaddressbook/kabcore.cpp b/kaddressbook/kabcore.cpp index cc8eb52..8776b53 100644 --- a/kaddressbook/kabcore.cpp +++ b/kaddressbook/kabcore.cpp @@ -2664,18 +2664,18 @@ bool KABCore::synchronizeAddressbooks( KABC::AddressBook* local, KABC::AddressBo int changedRemote = 0; //QPtrList<Addressee> el = local->rawAddressees(); Addressee addresseeR; QString uid; int take; Addressee addresseeL; Addressee addresseeRSync; Addressee addresseeLSync; - KABC::Addressee::List addresseeRSyncSharp = remote->getExternLastSyncAddressees(); - KABC::Addressee::List addresseeLSyncSharp = local->getExternLastSyncAddressees(); + // KABC::Addressee::List addresseeRSyncSharp = remote->getExternLastSyncAddressees(); + //KABC::Addressee::List addresseeLSyncSharp = local->getExternLastSyncAddressees(); bool fullDateRange = false; local->resetTempSyncStat(); mLastAddressbookSync = QDateTime::currentDateTime(); QDateTime modifiedCalendar = mLastAddressbookSync;; addresseeLSync = getLastSyncAddressee(); qDebug("Last Sync %s ", addresseeLSync.revision().toString().latin1()); addresseeR = remote->findByUid("last-syncAddressee-"+mCurrentSyncName ); if ( !addresseeR.isEmpty() ) { @@ -2704,17 +2704,17 @@ bool KABCore::synchronizeAddressbooks( KABC::AddressBook* local, KABC::AddressBo if ( addresseeLSync.revision() != addresseeRSync.revision() ) { // qDebug("set fulldate to true %s %s" ,addresseeLSync->dtStart().toString().latin1(), addresseeRSync->dtStart().toString().latin1() ); //qDebug("%d %d %d %d ", addresseeLSync->dtStart().time().second(), addresseeLSync->dtStart().time().msec() , addresseeRSync->dtStart().time().second(), addresseeRSync->dtStart().time().msec()); fullDateRange = true; qDebug("FULLDATE 3 %s %s", addresseeLSync.revision().toString().latin1() , addresseeRSync.revision().toString().latin1() ); } } - fullDateRange = true; // debug only! + // fullDateRange = true; // debug only! if ( fullDateRange ) mLastAddressbookSync = QDateTime::currentDateTime().addDays( -100*365); else mLastAddressbookSync = addresseeLSync.revision(); // for resyncing if own file has changed // PENDING fixme later when implemented #if 0 if ( mCurrentSyncDevice == "deleteaftersync" ) { @@ -2913,25 +2913,53 @@ bool KABCore::syncAB(QString filename, int mode) //pending prepare addresseeview for output //pending detect, if remote file has REV field. if not switch to external sync mGlobalSyncMode = SYNC_MODE_NORMAL; AddressBook abLocal(filename,"syncContact"); bool syncOK = false; if ( abLocal.load() ) { qDebug("AB loaded %s mode %d",filename.latin1(), mode ); - AddressBook::Iterator it; + bool external = false; + Addressee lse = mAddressBook->findByUid( "last-syncAddressee-"+mCurrentSyncDevice ); + if ( ! lse.isEmpty() ) { + if ( lse.familyName().left(4) == "!E: " ) + external = true; + } else { + bool found = false; + QDateTime dt( QDate( 2004,1,1)); + AddressBook::Iterator it; + for ( it = abLocal.begin(); it != abLocal.end(); ++it ) { + if ( (*it).revision() != dt ) { + found = true; + break; + } + } + external = ! found; + } + + if ( external ) { + mGlobalSyncMode = SYNC_MODE_EXTERNAL; + AddressBook::Iterator it; + for ( it = abLocal.begin(); it != abLocal.end(); ++it ) { + (*it).setID( mCurrentSyncDevice, (*it).uid() ); + (*it).computeCsum( mCurrentSyncDevice ); + } + } + //AddressBook::Iterator it; //QStringList vcards; //for ( it = abLocal.begin(); it != abLocal.end(); ++it ) { // qDebug("Name %s ", (*it).familyName().latin1()); //} syncOK = synchronizeAddressbooks( mAddressBook, &abLocal, mode ); if ( syncOK ) { if ( KABPrefs::instance()->mWriteBackFile ) { + if ( external ) + abLocal.removeDeletedAddressees(); qDebug("saving remote AB "); abLocal.saveAB(); } } setModified(); } if ( syncOK ) |