-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 @@ -248,445 +248,567 @@ void AddressBook::init(const QString &config, const QString &family ) if ( family == "syncContact" ) { qDebug("creating sync config "); fami = "contact"; KConfig* con = new KConfig( locateLocal("config", "syncContactrc") ); con->setGroup( "General" ); con->writeEntry( "ResourceKeys", QString("sync") ); con->writeEntry( "Standard", QString("sync") ); con->setGroup( "Resource_sync" ); con->writeEntry( "FileName", config ); con->writeEntry( "FileFormat", QString("vcard") ); con->writeEntry( "ResourceIdentifier", QString("sync") ); con->writeEntry( "ResourceName", QString("sync_res") ); if ( config.right(4) == ".xml" ) con->writeEntry( "ResourceType", QString("qtopia") ); else if ( config == "sharp" ) { con->writeEntry( "ResourceType", QString("sharp") ); } else { con->writeEntry( "ResourceType", QString("file") ); } //con->sync(); d->mConfig = con; } else d->mConfig = new KConfig( locateLocal("config", config) ); // qDebug("AddressBook::init 1 config=%s",config.latin1() ); } else { d->mConfig = 0; // qDebug("AddressBook::init 1 config=0"); } //US d->mErrorHandler = 0; d->mManager = new KRES::Manager<Resource>( fami, false ); d->mManager->readConfig( d->mConfig ); if ( family == "syncContact" ) { KRES::Manager<Resource> *manager = d->mManager; KRES::Manager<Resource>::ActiveIterator it; for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { (*it)->setAddressBook( this ); if ( !(*it)->open() ) error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) ); } Resource *res = standardResource(); if ( !res ) { qDebug("ERROR: no standard resource"); res = manager->createResource( "file" ); if ( res ) { addResource( res ); } else qDebug(" No resource available!!!"); } setStandardResource( res ); manager->writeConfig(); } addCustomField( i18n( "Department" ), KABC::Field::Organization, "X-Department", "KADDRESSBOOK" ); addCustomField( i18n( "Profession" ), KABC::Field::Organization, "X-Profession", "KADDRESSBOOK" ); addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization, "X-AssistantsName", "KADDRESSBOOK" ); addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization, "X-ManagersName", "KADDRESSBOOK" ); addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal, "X-SpousesName", "KADDRESSBOOK" ); addCustomField( i18n( "Office" ), KABC::Field::Personal, "X-Office", "KADDRESSBOOK" ); addCustomField( i18n( "IM Address" ), KABC::Field::Personal, "X-IMAddress", "KADDRESSBOOK" ); addCustomField( i18n( "Anniversary" ), KABC::Field::Personal, "X-Anniversary", "KADDRESSBOOK" ); //US added this field to become compatible with Opie/qtopia addressbook // values can be "female" or "male" or "". An empty field represents undefined. addCustomField( i18n( "Gender" ), KABC::Field::Personal, "X-Gender", "KADDRESSBOOK" ); addCustomField( i18n( "Children" ), KABC::Field::Personal, "X-Children", "KADDRESSBOOK" ); addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal, "X-FreeBusyUrl", "KADDRESSBOOK" ); addCustomField( i18n( "ExternalID" ), KABC::Field::Personal, "X-ExternalID", "KADDRESSBOOK" ); } AddressBook::~AddressBook() { delete d->mConfig; d->mConfig = 0; delete d->mManager; d->mManager = 0; //US delete d->mErrorHandler; d->mErrorHandler = 0; 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() ) { //qDebug("setId aa %s ", id.latin1()); (*addrIt).setIDStr(id ); } } blockLSEchange = true; return ok; } bool AddressBook::save( Ticket *ticket ) { kdDebug(5700) << "AddressBook::save()"<< endl; if ( ticket->resource() ) { deleteRemovedAddressees(); return ticket->resource()->save( ticket ); } return false; } void AddressBook::export2File( QString fileName ) { QFile outFile( fileName ); if ( !outFile.open( IO_WriteOnly ) ) { QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" ); KMessageBox::error( 0, text.arg( fileName ) ); return ; } QTextStream t( &outFile ); t.setEncoding( QTextStream::UnicodeUTF8 ); Iterator it; KABC::VCardConverter::Version version; version = KABC::VCardConverter::v3_0; for ( it = begin(); it != end(); ++it ) { if ( !(*it).IDStr().isEmpty() ) { (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() ); } KABC::VCardConverter converter; QString vcard; //Resource *resource() const; 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; QFile file( fileName ); file.open( IO_ReadOnly ); QByteArray rawData = file.readAll(); file.close(); QString data; if ( replaceLabel ) { data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); data.replace ( QRegExp("LABEL") , "ADR" ); data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); } 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 ); } if ( removeOld ) removeUntagged(); } void AddressBook::setUntagged() { Iterator ait; for ( ait = begin(); ait != end(); ++ait ) { (*ait).setTagged( false ); } } void AddressBook::removeUntagged() { Iterator ait; bool todelete = false; Iterator todel; for ( ait = begin(); ait != end(); ++ait ) { if ( todelete ) removeAddressee( todel ); if (!(*ait).tagged()) { todelete = true; todel = ait; } else todelete = false; } if ( todelete ) removeAddressee( todel ); deleteRemovedAddressees(); } void AddressBook::smplifyAddressees() { Iterator ait; for ( ait = begin(); ait != end(); ++ait ) { (*ait).simplifyEmails(); (*ait).simplifyPhoneNumbers(); (*ait).simplifyPhoneNumberTypes(); (*ait).simplifyAddresses(); } } void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync ) { Iterator ait; for ( ait = begin(); ait != end(); ++ait ) { 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; deleteRemovedAddressees(); Iterator ait; for ( ait = begin(); ait != end(); ++ait ) { 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; } AddressBook::Iterator AddressBook::begin() { Iterator it = Iterator(); it.d->mIt = d->mAddressees.begin(); return it; } AddressBook::ConstIterator AddressBook::begin() const { ConstIterator it = ConstIterator(); it.d->mIt = d->mAddressees.begin(); return it; } AddressBook::Iterator AddressBook::end() { Iterator it = Iterator(); it.d->mIt = d->mAddressees.end(); return it; } AddressBook::ConstIterator AddressBook::end() const { ConstIterator it = ConstIterator(); it.d->mIt = d->mAddressees.end(); return it; } void AddressBook::clear() { d->mAddressees.clear(); } Ticket *AddressBook::requestSaveTicket( Resource *resource ) { kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; if ( !resource ) { qDebug("AddressBook::requestSaveTicket no resource" ); resource = standardResource(); } KRES::Manager<Resource>::ActiveIterator it; for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { if ( (*it) == resource ) { if ( (*it)->readOnly() || !(*it)->isOpen() ) return 0; else 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; } //qDebug("inserting.... %s ",a.uid().latin1() ); 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; if ( takeResource ) { Resource * res = (*it).resource(); (*it) = a; (*it).setResource( res ); } else { (*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 ); } found = true; } else { if ( (*it).uid().left( 19 ) == 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 ); } } } } } if ( found ) return; d->mAddressees.append( a ); Addressee& addr = d->mAddressees.last(); if ( addr.resource() == 0 ) addr.setResource( standardResource() ); 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() ) { found = true; it2 = it; } else { if ( (*it).uid().left( 19 ) == 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 ); } diff --git a/kabc/addressbook.h b/kabc/addressbook.h index cc755d1..df9048b 100644 --- a/kabc/addressbook.h +++ b/kabc/addressbook.h @@ -53,195 +53,197 @@ class AddressBook : public QObject friend QDataStream &operator<<( QDataStream &, const AddressBook & ); friend QDataStream &operator>>( QDataStream &, AddressBook & ); friend class StdAddressBook; public: /** @short Address Book Iterator This class provides an iterator for address book entries. */ class Iterator { public: Iterator(); Iterator( const Iterator & ); ~Iterator(); Iterator &operator=( const Iterator & ); const Addressee &operator*() const; Addressee &operator*(); Addressee* operator->(); Iterator &operator++(); Iterator &operator++(int); Iterator &operator--(); Iterator &operator--(int); bool operator==( const Iterator &it ); bool operator!=( const Iterator &it ); struct IteratorData; IteratorData *d; }; /** @short Address Book Const Iterator This class provides a const iterator for address book entries. */ class ConstIterator { public: ConstIterator(); ConstIterator( const ConstIterator & ); ~ConstIterator(); ConstIterator &operator=( const ConstIterator & ); const Addressee &operator*() const; const Addressee* operator->() const; ConstIterator &operator++(); ConstIterator &operator++(int); ConstIterator &operator--(); ConstIterator &operator--(int); bool operator==( const ConstIterator &it ); bool operator!=( const ConstIterator &it ); struct ConstIteratorData; ConstIteratorData *d; }; /** Constructs a address book object. @param format File format class. */ AddressBook(); AddressBook( const QString &config ); AddressBook( const QString &config, const QString &family ); virtual ~AddressBook(); /** Requests a ticket for saving the addressbook. Calling this function locks the addressbook for all other processes. If the address book is already locked the function returns 0. You need the returned @ref Ticket object for calling the @ref save() function. @see save() */ Ticket *requestSaveTicket( Resource *resource=0 ); /** Load address book from file. */ bool load(); /** Save address book. The address book is saved to the file, the Ticket object has been requested for by @ref requestSaveTicket(). @param ticket a ticket object returned by @ref requestSaveTicket() */ bool save( Ticket *ticket ); bool saveAB( ); bool saveABphone( QString fileName ); 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. */ Iterator begin(); /** Returns a const iterator for first entry of address book. */ ConstIterator begin() const; /** Returns a iterator for first entry of address book. */ Iterator end(); /** Returns a const iterator for first entry of address book. */ ConstIterator end() const; /** Removes all entries from address book. */ void clear(); /** Insert an Addressee object into address book. If an object with the same unique id already exists in the address book it it replaced by the new one. If not the new object is appended to the address book. */ void insertAddressee( const Addressee &, bool setRev = true, bool takeResource = false); /** Removes entry from the address book. */ void removeAddressee( const Addressee & ); /** This is like @ref removeAddressee() just above, with the difference that the first element is a iterator, returned by @ref begin(). */ void removeAddressee( const Iterator & ); /** Find the specified entry in address book. Returns end(), if the entry couldn't be found. */ Iterator find( const Addressee & ); /** Find the entry specified by an unique id. Returns an empty Addressee object, if the address book does not contain an entry with this id. */ Addressee findByUid( const QString & ); /** Returns a list of all addressees in the address book. This list can be sorted with @ref KABC::AddresseeList for example. */ Addressee::List allAddressees(); /** Find all entries with the specified name in the address book. Returns an empty list, if no entries could be found. */ Addressee::List findByName( const QString & ); /** Find all entries with the specified email address in the address book. Returns an empty list, if no entries could be found. */ Addressee::List findByEmail( const QString & ); /** Find all entries wich have the specified category in the address book. Returns an empty list, if no entries could be found. */ Addressee::List findByCategory( const QString & ); /** Return a string identifying this addressbook. */ virtual QString identifier(); /** Used for debug output. */ void dump() const; void emitAddressBookLocked() { emit addressBookLocked( this ); } void emitAddressBookUnlocked() { emit addressBookUnlocked( this ); } void emitAddressBookChanged() { emit addressBookChanged( this ); } /** Return list of all Fields known to the address book which are associated diff --git a/kabc/addressee.cpp b/kabc/addressee.cpp index 607ae26..548305a 100644 --- a/kabc/addressee.cpp +++ b/kabc/addressee.cpp @@ -210,230 +210,270 @@ 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] ); } uint cs = getCsum4List(l); // qDebug("CSUM computed %d %s %s", cs,QString::number (cs ).latin1(), uid().latin1() ); setCsum( dev, QString::number (cs )); } void Addressee::mergeContact( const Addressee& ad ) { detach(); if ( mData->name.isEmpty() ) mData->name = ad.mData->name; if ( mData->formattedName.isEmpty() ) mData->formattedName = ad.mData->formattedName; if ( mData->familyName.isEmpty() ) mData->familyName = ad.mData->familyName; if ( mData->givenName.isEmpty() ) mData->givenName = ad.mData->givenName ; if ( mData->additionalName ) mData->additionalName = ad.mData->additionalName; if ( mData->prefix.isEmpty() ) mData->prefix = ad.mData->prefix; if ( mData->suffix.isEmpty() ) mData->suffix = ad.mData->suffix; if ( mData->nickName.isEmpty() ) mData->nickName = ad.mData->nickName; if ( !mData->birthday.isValid() ) if ( ad.mData->birthday.isValid()) mData->birthday = ad.mData->birthday; if ( mData->mailer.isEmpty() ) mData->mailer = ad.mData->mailer; if ( !mData->timeZone.isValid() ) mData->timeZone = ad.mData->timeZone; if ( !mData->geo.isValid() ) mData->geo = ad.mData->geo; if ( mData->title .isEmpty() ) mData->title = ad.mData->title ; if ( mData->role.isEmpty() ) mData->role = ad.mData->role ; if ( mData->organization.isEmpty() ) mData->organization = ad.mData->organization ; if ( mData->note.isEmpty() ) mData->note = ad.mData->note ; if ( mData->productId.isEmpty() ) mData->productId = ad.mData->productId; 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 // merging emails; // 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() ); } bool Addressee::removeVoice() { PhoneNumber::List phoneN = phoneNumbers(); PhoneNumber::List::Iterator phoneIt; bool found = false; for ( phoneIt = phoneN.begin(); phoneIt != phoneN.end(); ++phoneIt ) { if ( (*phoneIt).type() & PhoneNumber::Voice) { // voice found if ((*phoneIt).type() - PhoneNumber::Voice ) { (*phoneIt).setType((*phoneIt).type() - PhoneNumber::Voice ); insertPhoneNumber( (*phoneIt) ); found = true; } } } 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; } for( it = list.begin(); it != list.end(); ++it ) { removeAddress( (*it) ); } } // removes all emails but the first // needed by phone sync void Addressee::simplifyEmails() { if ( mData->emails.count() == 0 ) return ; QString email = mData->emails.first(); detach(); mData->emails.clear(); mData->emails.append( email ); } void Addressee::simplifyPhoneNumbers() { KABC::PhoneNumber::List removeNumbers; KABC::PhoneNumber::List::Iterator phoneIter; for ( phoneIter = mData->phoneNumbers.begin(); phoneIter != mData->phoneNumbers.end(); ++phoneIter ) { if ( ! ( *phoneIter ).simplifyNumber() ) removeNumbers.append( ( *phoneIter ) ); } for ( phoneIter = removeNumbers.begin(); phoneIter != removeNumbers.end(); ++phoneIter ) { removePhoneNumber(( *phoneIter )); } } void Addressee::simplifyPhoneNumberTypes() { KABC::PhoneNumber::List::Iterator phoneIter; for ( phoneIter = mData->phoneNumbers.begin(); phoneIter != mData->phoneNumbers.end(); ++phoneIter ) ( *phoneIter ).simplifyType(); } void Addressee::removeID(const QString &prof) { detach(); mData->mExternalId = KIdManager::removeId ( mData->mExternalId, prof); } void Addressee::setID( const QString & prof , const QString & id ) { detach(); mData->mExternalId = KIdManager::setId ( mData->mExternalId, prof, id ); //qDebug("setID2 %s %s %s",mData->mExternalId.latin1(), prof.latin1(), id.latin1() ); } void Addressee::setTempSyncStat( int id ) { if ( mData->mTempSyncStat == id ) return; detach(); mData->mTempSyncStat = id; } int Addressee::tempSyncStat() const { return mData->mTempSyncStat; } QString Addressee::getID( const QString & prof) { return KIdManager::getId ( mData->mExternalId, prof ); } void Addressee::setCsum( const QString & prof , const QString & id ) { detach(); //qDebug("setcsum1 %s %s %s",mData->mExternalId.latin1(), prof.latin1(), id.latin1() ); mData->mExternalId = KIdManager::setCsum ( mData->mExternalId, prof, id ); //qDebug("setcsum2 %s ",mData->mExternalId.latin1() ); } QString Addressee::getCsum( const QString & prof) { return KIdManager::getCsum ( mData->mExternalId, prof ); } void Addressee::setIDStr( const QString & s ) { detach(); mData->mExternalId = s; } QString Addressee::IDStr() const { return mData->mExternalId; } void Addressee::setExternalUID( const QString &id ) { if ( id == mData->externalUID ) return; detach(); diff --git a/kabc/addressee.h b/kabc/addressee.h index 0aa2c51..03138f6 100644 --- a/kabc/addressee.h +++ b/kabc/addressee.h @@ -29,192 +29,193 @@ $Id$ #ifndef KABC_ADDRESSEE_H #define KABC_ADDRESSEE_H #include <qdatetime.h> #include <qstring.h> #include <qstringlist.h> #include <qvaluelist.h> #include <ksharedptr.h> #include <kurl.h> #include "address.h" #include "agent.h" #include "geo.h" #include "key.h" #include "phonenumber.h" #include "picture.h" #include "secrecy.h" #include "sound.h" #include "timezone.h" namespace KABC { class Resource; /** @short address book entry This class represents an entry in the address book. The data of this class is implicitly shared. You can pass this class by value. If you need the name of a field for presenting it to the user you should use the functions ending in Label(). They return a translated string which can be used as label for the corresponding field. About the name fields: givenName() is the first name and familyName() the last name. In some countries the family name comes first, that's the reason for the naming. formattedName() is the full name with the correct formatting. It is used as an override, when the correct formatting can't be generated from the other name fields automatically. realName() returns a fully formatted name(). It uses formattedName, if set, otherwise it constucts the name from the name fields. As fallback, if nothing else is set it uses name(). name() is the NAME type of RFC2426. It can be used as internal name for the data enty, but shouldn't be used for displaying the data to the user. */ class Addressee { friend QDataStream &operator<<( QDataStream &, const Addressee & ); friend QDataStream &operator>>( QDataStream &, Addressee & ); public: typedef QValueList<Addressee> List; /** Construct an empty address book entry. */ Addressee(); ~Addressee(); Addressee( const Addressee & ); Addressee &operator=( const Addressee & ); bool operator==( const Addressee & ) const; bool operator!=( const Addressee & ) const; // sync stuff void setTempSyncStat(int id); 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; void setExternalUID( const QString &id ); QString externalUID() const; void setOriginalExternalUID( const QString &id ); QString originalExternalUID() const; void mergeContact( const Addressee& ad ); void simplifyEmails(); void simplifyAddresses(); void simplifyPhoneNumbers(); void simplifyPhoneNumberTypes(); bool removeVoice(); + bool containsAdr(const Addressee& addr ); /** Set unique identifier. */ void setUid( const QString &uid ); /** Return unique identifier. */ QString uid() const; /** Return translated label for uid field. */ static QString uidLabel(); /** Set name. */ void setName( const QString &name ); /** Return name. */ QString name() const; /** Return translated label for name field. */ static QString nameLabel(); /** Set formatted name. */ void setFormattedName( const QString &formattedName ); /** Return formatted name. */ QString formattedName() const; /** Return translated label for formattedName field. */ static QString formattedNameLabel(); /** Set family name. */ void setFamilyName( const QString &familyName ); /** Return family name. */ QString familyName() const; /** Return translated label for familyName field. */ static QString familyNameLabel(); /** Set given name. */ void setGivenName( const QString &givenName ); /** Return given name. */ QString givenName() const; /** Return translated label for givenName field. */ static QString givenNameLabel(); /** Set additional names. */ void setAdditionalName( const QString &additionalName ); /** Return additional names. */ QString additionalName() const; /** Return translated label for additionalName field. */ static QString additionalNameLabel(); /** Set honorific prefixes. */ void setPrefix( const QString &prefix ); /** Return honorific prefixes. */ QString prefix() const; /** Return translated label for prefix field. */ static QString prefixLabel(); /** Set honorific suffixes. */ void setSuffix( const QString &suffix ); diff --git a/kabc/phonenumber.cpp b/kabc/phonenumber.cpp index e5abc0e..6e94c7e 100644 --- a/kabc/phonenumber.cpp +++ b/kabc/phonenumber.cpp @@ -1,174 +1,172 @@ /* This file is part of libkabc. Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* Enhanced Version of the file for platform independent KDE tools. Copyright (c) 2004 Ulf Schenk $Id$ */ #include <kapplication.h> #include <klocale.h> #include "phonenumber.h" using namespace KABC; PhoneNumber::PhoneNumber() : mType( Home ) { init(); } PhoneNumber::PhoneNumber( const QString &number, int type ) : mType( type ), mNumber( number ) { init(); } PhoneNumber::~PhoneNumber() { } void PhoneNumber::init() { mId = KApplication::randomString( 8 ); } bool PhoneNumber::operator==( const PhoneNumber &p ) const { if ( mNumber != p.mNumber ) return false; if ( mType != p.mType ) return false; return true; } bool PhoneNumber::operator!=( const PhoneNumber &p ) const { return !( p == *this ); } bool PhoneNumber::simplifyNumber() { QString Number; 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 ); } // make cellphone compatible void PhoneNumber::simplifyType() { if ( mType & Fax ) mType = Fax; else if ( mType & Cell ) mType = Cell; else if ( mType & Work ) mType = Work ; else if ( mType & Home ) mType = Home; else mType = Pref; } void PhoneNumber::setId( const QString &id ) { mId = id; } QString PhoneNumber::id() const { return mId; } void PhoneNumber::setNumber( const QString &number ) { mNumber = number; } QString PhoneNumber::number() const { return mNumber; } void PhoneNumber::setType( int type ) { mType = type; } int PhoneNumber::type() const { return mType; } QString PhoneNumber::typeLabel() const { QString label; bool first = true; TypeList list = typeList(); TypeList::Iterator it; for ( it = list.begin(); it != list.end(); ++it ) { if ( ( type() & (*it) ) && ( (*it) != Pref ) ) { label.append( ( first ? "" : "/" ) + typeLabel( *it ) ); if ( first ) first = false; } } return label; } QString PhoneNumber::label() const { return typeLabel( type() ); } PhoneNumber::TypeList PhoneNumber::typeList() { TypeList list; list << Home << Work << Msg << Pref << Voice << Fax << Cell << Video << Bbs << Modem << Car << Isdn << Pcs << Pager << Sip; return list; } QString PhoneNumber::label( int type ) { return typeLabel( type ); } QString PhoneNumber::typeLabel( int type ) { QString typeString; if ((type & Home) == Home) typeString += i18n("Home"); else if ((type & Work) == Work) typeString += i18n("Work"); if (!typeString.isEmpty()) typeString += " "; if ((type & Cell) == Cell) typeString += i18n("Mobile"); |