kitchensync

addresseediffalgo.cpp

00001 /*
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include <kabc/vcardconverter.h>
00023 
00024 #include "addresseediffalgo.h"
00025 
00026 using namespace KSync;
00027 
00028 static bool compareString( const QString &left, const QString &right )
00029 {
00030   if ( left.isEmpty() && right.isEmpty() )
00031     return true;
00032   else
00033     return left == right;
00034 }
00035 
00036 AddresseeDiffAlgo::AddresseeDiffAlgo( const KABC::Addressee &leftAddressee,
00037                                       const KABC::Addressee &rightAddressee )
00038   : mLeftAddressee( leftAddressee ), mRightAddressee( rightAddressee )
00039 {
00040 }
00041 
00042 AddresseeDiffAlgo::AddresseeDiffAlgo( const QString &leftAddressee,
00043                                       const QString &rightAddressee )
00044 {
00045   KABC::VCardConverter converter;
00046 
00047   mLeftAddressee = converter.parseVCard( leftAddressee );
00048   mRightAddressee = converter.parseVCard( rightAddressee );
00049 }
00050 
00051 void AddresseeDiffAlgo::run()
00052 {
00053   begin();
00054 
00055   if ( !compareString( mLeftAddressee.uid(), mRightAddressee.uid() ) )
00056     conflictField( KABC::Addressee::uidLabel(), mLeftAddressee.uid(), mRightAddressee.uid() );
00057 
00058   if ( !compareString( mLeftAddressee.name(), mRightAddressee.name() ) )
00059     conflictField( KABC::Addressee::nameLabel(), mLeftAddressee.name(), mRightAddressee.name() );
00060 
00061   if ( !compareString( mLeftAddressee.formattedName(), mRightAddressee.formattedName() ) )
00062     conflictField( KABC::Addressee::formattedNameLabel(), mLeftAddressee.formattedName(), mRightAddressee.formattedName() );
00063 
00064   if ( !compareString( mLeftAddressee.familyName(), mRightAddressee.familyName() ) )
00065     conflictField( KABC::Addressee::familyNameLabel(), mLeftAddressee.familyName(), mRightAddressee.familyName() );
00066 
00067   if ( !compareString( mLeftAddressee.givenName(), mRightAddressee.givenName() ) )
00068     conflictField( KABC::Addressee::givenNameLabel(), mLeftAddressee.givenName(), mRightAddressee.givenName() );
00069 
00070   if ( !compareString( mLeftAddressee.additionalName(), mRightAddressee.additionalName() ) )
00071     conflictField( KABC::Addressee::additionalNameLabel(), mLeftAddressee.additionalName(), mRightAddressee.additionalName() );
00072 
00073   if ( !compareString( mLeftAddressee.prefix(), mRightAddressee.prefix() ) )
00074     conflictField( KABC::Addressee::prefixLabel(), mLeftAddressee.prefix(), mRightAddressee.prefix() );
00075 
00076   if ( !compareString( mLeftAddressee.suffix(), mRightAddressee.suffix() ) )
00077     conflictField( KABC::Addressee::suffixLabel(), mLeftAddressee.suffix(), mRightAddressee.suffix() );
00078 
00079   if ( !compareString( mLeftAddressee.nickName(), mRightAddressee.nickName() ) )
00080     conflictField( KABC::Addressee::nickNameLabel(), mLeftAddressee.nickName(), mRightAddressee.nickName() );
00081 
00082   if ( mLeftAddressee.birthday() != mRightAddressee.birthday() )
00083     conflictField( KABC::Addressee::birthdayLabel(), mLeftAddressee.birthday().toString(),
00084                    mRightAddressee.birthday().toString() );
00085 
00086   if ( !compareString( mLeftAddressee.mailer(), mRightAddressee.mailer() ) )
00087     conflictField( KABC::Addressee::mailerLabel(), mLeftAddressee.mailer(), mRightAddressee.mailer() );
00088 
00089   if ( mLeftAddressee.timeZone() != mRightAddressee.timeZone() )
00090     conflictField( KABC::Addressee::timeZoneLabel(), mLeftAddressee.timeZone().asString(), mRightAddressee.timeZone().asString() );
00091 
00092   if ( mLeftAddressee.geo() != mRightAddressee.geo() )
00093     conflictField( KABC::Addressee::geoLabel(), mLeftAddressee.geo().asString(), mRightAddressee.geo().asString() );
00094 
00095   if ( !compareString( mLeftAddressee.title(), mRightAddressee.title() ) )
00096     conflictField( KABC::Addressee::titleLabel(), mLeftAddressee.title(), mRightAddressee.title() );
00097 
00098   if ( !compareString( mLeftAddressee.role(), mRightAddressee.role() ) )
00099     conflictField( KABC::Addressee::roleLabel(), mLeftAddressee.role(), mRightAddressee.role() );
00100 
00101   if ( !compareString( mLeftAddressee.organization(), mRightAddressee.organization() ) )
00102     conflictField( KABC::Addressee::organizationLabel(), mLeftAddressee.organization(), mRightAddressee.organization() );
00103 
00104   if ( !compareString( mLeftAddressee.note(), mRightAddressee.note() ) )
00105     conflictField( KABC::Addressee::noteLabel(), mLeftAddressee.note(), mRightAddressee.note() );
00106 
00107   if ( !compareString( mLeftAddressee.productId(), mRightAddressee.productId() ) )
00108     conflictField( KABC::Addressee::productIdLabel(), mLeftAddressee.productId(), mRightAddressee.productId() );
00109 
00110   if ( !compareString( mLeftAddressee.sortString(), mRightAddressee.sortString() ) )
00111     conflictField( KABC::Addressee::sortStringLabel(), mLeftAddressee.sortString(), mRightAddressee.sortString() );
00112 
00113   if ( mLeftAddressee.secrecy() != mRightAddressee.secrecy() ) {
00114     conflictField( KABC::Addressee::secrecyLabel(), mLeftAddressee.secrecy().asString(), mRightAddressee.secrecy().asString() );
00115   }
00116   if ( mLeftAddressee.url()!= mRightAddressee.url() )
00117     conflictField( KABC::Addressee::urlLabel(), mLeftAddressee.url().prettyURL(),
00118       mRightAddressee.url().prettyURL() );
00119     
00120   if ( mLeftAddressee.logo() != mRightAddressee.logo() ) {
00121   }
00122 
00123   if ( mLeftAddressee.photo() != mRightAddressee.photo() ) {
00124   }
00125 
00126   diffList( "emails", mLeftAddressee.emails(), mRightAddressee.emails() );
00127 
00128   diffList( "Phone Numbers", mLeftAddressee.phoneNumbers(), mRightAddressee.phoneNumbers() );
00129   diffList( "Addresses", mLeftAddressee.addresses(), mRightAddressee.addresses() );
00130 
00131   end();
00132 }
00133 
00134 QString AddresseeDiffAlgo::toString( const KABC::PhoneNumber &number )
00135 {
00136   return number.number();
00137 }
00138 
00139 QString AddresseeDiffAlgo::toString( const KABC::Address &addr )
00140 {
00141   return addr.formattedAddress();
00142 }
00143 
00144 template <class L>
00145 void AddresseeDiffAlgo::diffList( const QString &id,
00146                                   const QValueList<L> &left, const QValueList<L> &right )
00147 {
00148   for ( uint i = 0; i < left.count(); ++i ) {
00149     if ( right.find( left[ i ] ) == right.end() )
00150       additionalLeftField( id, toString( left[ i ] ) );
00151   }
00152 
00153   for ( uint i = 0; i < right.count(); ++i ) {
00154     if ( left.find( right[ i ] ) == left.end() )
00155       additionalRightField( id, toString( right[ i ] ) );
00156   }
00157 }