phonebookui/Phonebook2/CommandsExtension/src/CPbk2MergeResolver.cpp
branchRCL_3
changeset 20 f4a778e096c2
child 21 9da50d567e3c
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:  Phonebook 2 merge contacts resolver.
       
    15  *
       
    16  */
       
    17 
       
    18 // INCLUDE FILES
       
    19 
       
    20 //Phonebook2
       
    21 #include "CPbk2MergeResolver.h"
       
    22 #include <CPbk2PresentationContact.h>
       
    23 #include <CPbk2PresentationContactFieldCollection.h>
       
    24 #include <CPbk2PresentationContactField.h>
       
    25 #include <MPbk2FieldProperty.h>
       
    26 
       
    27 //Virtual Phonebook
       
    28 #include <MVPbkStoreContact.h>
       
    29 #include <MVPbkStoreContactFieldCollection.h>
       
    30 #include <MVPbkContactFieldTextData.h>
       
    31 #include <MVPbkContactFieldDateTimeData.h>
       
    32 #include <MVPbkContactFieldUriData.h>
       
    33 #include <MVPbkContactFieldBinaryData.h>
       
    34 #include <vpbkeng.rsg>
       
    35 #include <VPbkSyncConstants.h>
       
    36 #include <Pbk2UIControls.rsg>
       
    37 
       
    38 // System includes
       
    39 #include <AknUtils.h>
       
    40 
       
    41 // Debugging headers
       
    42 #include <Pbk2Debug.h>
       
    43 #include <Pbk2Profile.h>
       
    44 
       
    45 /// Unnamed namespace for local definitions
       
    46 namespace 
       
    47     {
       
    48     enum TPbk2PanicCodes
       
    49         {
       
    50         EPbk2WrongArgument,
       
    51         EPbk2FieldTypeNotExists
       
    52         };
       
    53     
       
    54     void Panic(TPbk2PanicCodes aReason)
       
    55         {
       
    56         _LIT( KPanicText, "CPbk2_Merge_Resolver" );
       
    57         User::Panic(KPanicText,aReason);
       
    58         }
       
    59     
       
    60 #ifdef _DEBUG
       
    61     #define DEBUG_PRINT_FIELD( field ) PrintFieldL( field );
       
    62     const TInt KEndLine = '\0';
       
    63 #else
       
    64     #define DEBUG_PRINT_FIELD( field ) 
       
    65 #endif
       
    66     
       
    67     } /// namespace
       
    68 
       
    69 
       
    70 
       
    71 // --------------------------------------------------------------------------
       
    72 // CPbk2MergeResolver::CPbk2MergeResolver
       
    73 // --------------------------------------------------------------------------
       
    74 //
       
    75 CPbk2MergeResolver::CPbk2MergeResolver(
       
    76         CPbk2PresentationContact* aFirstContact, 
       
    77         CPbk2PresentationContact* aSecondContact )
       
    78     :iFirstContact( aFirstContact ), 
       
    79      iSecondContact( aSecondContact )
       
    80     {
       
    81     }
       
    82 
       
    83 // --------------------------------------------------------------------------
       
    84 // CPbk2MergeResolver::~CPbk2MergeResolver
       
    85 // --------------------------------------------------------------------------
       
    86 //
       
    87 CPbk2MergeResolver::~CPbk2MergeResolver()
       
    88     {
       
    89     iConflicts.ResetAndDestroy();
       
    90     iMerged.Close();
       
    91     delete iFirstContact;
       
    92     delete iSecondContact;
       
    93     }
       
    94 
       
    95 // --------------------------------------------------------------------------
       
    96 // CPbk2MergeResolver::NewL
       
    97 // --------------------------------------------------------------------------
       
    98 //
       
    99 CPbk2MergeResolver* CPbk2MergeResolver::NewL( 
       
   100         CPbk2PresentationContact* aFirstContact, 
       
   101         CPbk2PresentationContact* aSecondContact )
       
   102     {
       
   103     CPbk2MergeResolver* self = 
       
   104         new ( ELeave ) CPbk2MergeResolver( aFirstContact, aSecondContact );
       
   105     return self;
       
   106     }
       
   107 
       
   108 // --------------------------------------------------------------------------
       
   109 // CPbk2MergeResolver::MergeL
       
   110 // --------------------------------------------------------------------------
       
   111 //
       
   112 void CPbk2MergeResolver::MergeL()
       
   113     {
       
   114     iConflicts.ResetAndDestroy();
       
   115     iMerged.Close();
       
   116     
       
   117     CPbk2PresentationContactFieldCollection & firstCollection = iFirstContact->PresentationFields();
       
   118     CPbk2PresentationContactFieldCollection & secondCollection = iSecondContact->PresentationFields();
       
   119     
       
   120     RArray<TInt> duplicatesInFirst;
       
   121     CleanupClosePushL(duplicatesInFirst);
       
   122     RArray<TInt> duplicatesInSecond;
       
   123     CleanupClosePushL(duplicatesInSecond);
       
   124     
       
   125     TInt countFirst = firstCollection.FieldCount();
       
   126     TInt countSecond = secondCollection.FieldCount();
       
   127     TBool addressesMerged = EFalse;
       
   128     
       
   129     for( TInt idxFirst = 0; idxFirst < countFirst; idxFirst++)
       
   130         {
       
   131         const MVPbkStoreContactField& fieldFirst = firstCollection.FieldAt( idxFirst );
       
   132        
       
   133         for( TInt idxSecond = 0; idxSecond < countSecond; idxSecond++)
       
   134             {
       
   135             const MVPbkStoreContactField& fieldSecond = secondCollection.FieldAt( idxSecond );
       
   136             
       
   137             if( AddressField( fieldFirst ) )
       
   138                 {
       
   139                 if( !addressesMerged )
       
   140                     {
       
   141                     MergeAddressesL();
       
   142                     addressesMerged = ETrue;
       
   143                     }
       
   144                 continue;
       
   145                 }
       
   146             
       
   147             if( AddressField( fieldSecond ) )
       
   148                 {
       
   149                 continue;
       
   150                 }
       
   151                 
       
   152             
       
   153             TBool sameTypes = AreFieldsTypeSame( fieldFirst, fieldSecond );
       
   154             TBool sameValues = EqualsValues( fieldFirst, fieldSecond );
       
   155             
       
   156             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   157                 ("CPbk2MergeResolver::MergeL - Fields:") );
       
   158             DEBUG_PRINT_FIELD( fieldFirst )
       
   159             DEBUG_PRINT_FIELD( fieldSecond )
       
   160             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   161                 ("CPbk2MergeResolver::MergeL - Fields are the same:%d"), sameTypes);
       
   162             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   163                 ("CPbk2MergeResolver::MergeL - Fields have the same values:%d"), sameValues);
       
   164             
       
   165             if( sameTypes )
       
   166                 {
       
   167                 if( IsTopContactField( fieldFirst ) )
       
   168                     {
       
   169                     if( CompareTopConactFields( fieldFirst, fieldSecond ) <= 0 )
       
   170                         {
       
   171                         duplicatesInSecond.AppendL( idxSecond );
       
   172                         }
       
   173                     else
       
   174                         {
       
   175                         duplicatesInFirst.AppendL( idxFirst );
       
   176                         }
       
   177                     }
       
   178                 else
       
   179                     {
       
   180                     if( !sameValues && FieldMultiplicity( fieldFirst ) == EPbk2FieldMultiplicityOne  )
       
   181                         {
       
   182                         AddConflictL( fieldFirst, fieldSecond );
       
   183                         }
       
   184                     if( sameValues )
       
   185                         {
       
   186                         duplicatesInSecond.AppendL( idxSecond );
       
   187                         }
       
   188                     }
       
   189                 }
       
   190             }
       
   191         }
       
   192     
       
   193     if( !addressesMerged )
       
   194         {
       
   195         MergeAddressesL();
       
   196         }
       
   197     
       
   198     CompleteMergeArrL( duplicatesInFirst, duplicatesInSecond );
       
   199     
       
   200     CleanupStack::PopAndDestroy( &duplicatesInSecond );
       
   201     CleanupStack::PopAndDestroy( &duplicatesInFirst );
       
   202     }
       
   203 
       
   204 // --------------------------------------------------------------------------
       
   205 // CPbk2MergeResolver::MergeAddressesL
       
   206 // --------------------------------------------------------------------------
       
   207 //
       
   208 void CPbk2MergeResolver::MergeAddressesL()
       
   209     {
       
   210     for (TInt groupId = EPbk2FieldGroupIdPostalAddress; groupId
       
   211             <= EPbk2FieldGroupIdCompanyAddress; groupId++)
       
   212         {
       
   213         ECompareAddress compareAddresses = CompareAddresses(
       
   214                 ( TPbk2FieldGroupId) groupId );
       
   215         switch ( compareAddresses )
       
   216             {
       
   217             case ECASame:
       
   218                 CopyAddressToMergedL( *iFirstContact, 
       
   219                         ( TPbk2FieldGroupId) groupId );
       
   220                 break;
       
   221             case ECAFirstIncludesSecond:
       
   222                 CopyAddressToMergedL( *iFirstContact,
       
   223                         ( TPbk2FieldGroupId) groupId );
       
   224                 break;
       
   225             case ECASecondIncludesFirst:
       
   226                 CopyAddressToMergedL( *iSecondContact,
       
   227                         ( TPbk2FieldGroupId) groupId );
       
   228                 break;
       
   229             case ECADifferent:
       
   230                 AddAddressConflictL( (TPbk2FieldGroupId) groupId );
       
   231                 break;
       
   232             case ECAUndefined:
       
   233                 // Both addresses don't exist
       
   234                 break;
       
   235             default:
       
   236                 Panic( EPbk2WrongArgument );
       
   237             }
       
   238         }
       
   239     }
       
   240 
       
   241 // --------------------------------------------------------------------------
       
   242 // CPbk2MergeResolver::CompleteMergeArrL
       
   243 // --------------------------------------------------------------------------
       
   244 //
       
   245 void CPbk2MergeResolver::CompleteMergeArrL( 
       
   246         RArray<TInt>& aDuplicatesInFirst, 
       
   247         RArray<TInt>& aDuplicatesInSecond )
       
   248     {
       
   249     CPbk2PresentationContactFieldCollection & firstCollection = iFirstContact->PresentationFields();
       
   250     CPbk2PresentationContactFieldCollection & secondCollection = iSecondContact->PresentationFields();
       
   251         
       
   252     TInt countFirst = firstCollection.FieldCount();
       
   253     TInt countSecond = secondCollection.FieldCount();
       
   254 
       
   255     for( TInt idxFirst = 0; idxFirst < countFirst; idxFirst++)
       
   256         {
       
   257         const MVPbkStoreContactField& fieldFirst = firstCollection.FieldAt( idxFirst );
       
   258         if( FindConflictByFieldL( fieldFirst ) == KErrNotFound &&
       
   259             aDuplicatesInFirst.Find( idxFirst ) == KErrNotFound  &&
       
   260             !AddressField( fieldFirst ) )
       
   261             {
       
   262             iMerged.AppendL( &fieldFirst );
       
   263             
       
   264             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   265                 ("CPbk2MergeResolver::MergeL - Field added to merged(first contact):"));
       
   266             DEBUG_PRINT_FIELD( fieldFirst )
       
   267             }
       
   268         }
       
   269     
       
   270     for( TInt idxSecond = 0; idxSecond < countSecond; idxSecond++)
       
   271         {
       
   272         const MVPbkStoreContactField& fieldSecond = secondCollection.FieldAt( idxSecond );
       
   273         if( FindConflictByFieldL( fieldSecond ) == KErrNotFound && 
       
   274             aDuplicatesInSecond.Find( idxSecond ) == KErrNotFound  && 
       
   275             !AddressField( fieldSecond ) )
       
   276             {
       
   277             iMerged.AppendL( &fieldSecond );
       
   278             
       
   279             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   280                 ("CPbk2MergeResolver::MergeL - Field added to merged(second contact):"));
       
   281             DEBUG_PRINT_FIELD( fieldSecond )
       
   282             }
       
   283         }
       
   284     }
       
   285 
       
   286 // --------------------------------------------------------------------------
       
   287 // CPbk2MergeResolver::IsTopContactField
       
   288 // --------------------------------------------------------------------------
       
   289 //
       
   290 TBool CPbk2MergeResolver::IsTopContactField( 
       
   291         const MVPbkStoreContactField& aField )
       
   292     {
       
   293     const MVPbkFieldType* fieldType = aField.BestMatchingFieldType();
       
   294     TBool topContact( EFalse );
       
   295     
       
   296     if ( fieldType )
       
   297         {
       
   298         TArray<TVPbkFieldVersitProperty> versitProp = fieldType->VersitProperties();        
       
   299         for( TInt idx = 0; idx < versitProp.Count() && !topContact; idx++ )
       
   300             {
       
   301             TVPbkFieldVersitProperty versitPropFirst = versitProp[idx];
       
   302             if( versitPropFirst.Name() == EVPbkVersitNameTopContact )
       
   303                 {
       
   304                 topContact = ETrue;
       
   305                 }
       
   306             }
       
   307         }
       
   308     
       
   309     return topContact;
       
   310     }
       
   311 
       
   312 // --------------------------------------------------------------------------
       
   313 // CPbk2MergeResolver::CompareTopConactFields
       
   314 // --------------------------------------------------------------------------
       
   315 //
       
   316 TInt CPbk2MergeResolver::CompareTopConactFields(
       
   317         const MVPbkStoreContactField& aFieldFirst,
       
   318         const MVPbkStoreContactField& aFieldSecond )
       
   319     {
       
   320     const MVPbkContactFieldData& dataFirst = aFieldFirst.FieldData();
       
   321     TVPbkFieldStorageType storageTypeFirst = dataFirst.DataType();
       
   322     
       
   323     const MVPbkContactFieldData& dataSecond = aFieldSecond.FieldData();
       
   324     TVPbkFieldStorageType storageTypeSecond = dataSecond.DataType();
       
   325         
       
   326     TInt ret = KErrGeneral;
       
   327     
       
   328     if( storageTypeFirst != storageTypeSecond ||
       
   329         storageTypeFirst != EVPbkFieldStorageTypeText )
       
   330         {
       
   331         return ret;
       
   332         }
       
   333     
       
   334     TInt valueFirst;
       
   335     TInt valueSecond;
       
   336     TInt errorCode;
       
   337     TLex lex( MVPbkContactFieldTextData::Cast( dataFirst ).Text() );
       
   338     errorCode = lex.Val( valueFirst );
       
   339     
       
   340     if( errorCode == KErrNone )
       
   341         {
       
   342         TLex lex( MVPbkContactFieldTextData::Cast( dataSecond ).Text() );
       
   343         errorCode = lex.Val( valueSecond );
       
   344         }
       
   345     
       
   346     if( errorCode == KErrNone )
       
   347         {
       
   348         if( valueFirst > valueSecond )
       
   349             {
       
   350             ret = 1;
       
   351             }
       
   352         else if( valueFirst < valueSecond )
       
   353             {
       
   354             ret = -1;
       
   355             }
       
   356         else
       
   357             {
       
   358             ret = 0;
       
   359             }
       
   360         }
       
   361     
       
   362     return ret;
       
   363     }
       
   364 
       
   365 // --------------------------------------------------------------------------
       
   366 // CPbk2MergeResolver::CountConflicts
       
   367 // --------------------------------------------------------------------------
       
   368 //
       
   369 TInt CPbk2MergeResolver::CountConflicts()
       
   370     {
       
   371     return iConflicts.Count();
       
   372     }
       
   373 
       
   374 // --------------------------------------------------------------------------
       
   375 // CPbk2MergeResolver::GetConflictAt
       
   376 // --------------------------------------------------------------------------
       
   377 //
       
   378 MPbk2MergeConflict& CPbk2MergeResolver::GetConflictAt( TInt aConflict )
       
   379     {
       
   380     __ASSERT_ALWAYS( aConflict >= 0 && aConflict < CountConflicts(), Panic( EPbk2WrongArgument ) );
       
   381     
       
   382     return *iConflicts[aConflict];
       
   383     }
       
   384 
       
   385 // --------------------------------------------------------------------------
       
   386 // CPbk2MergeResolver::CountMerged
       
   387 // --------------------------------------------------------------------------
       
   388 //
       
   389 TInt CPbk2MergeResolver::CountMerged()
       
   390     {
       
   391     return iMerged.Count();
       
   392     }
       
   393 
       
   394 // --------------------------------------------------------------------------
       
   395 // CPbk2MergeResolver::GetMergedAtL
       
   396 // --------------------------------------------------------------------------
       
   397 //
       
   398 MVPbkStoreContactField& CPbk2MergeResolver::GetMergedAt( TInt aMarged )
       
   399     {
       
   400     __ASSERT_ALWAYS( aMarged >= 0 && aMarged < CountMerged(), Panic( EPbk2WrongArgument ) );
       
   401     
       
   402     return *iMerged[aMarged];
       
   403     }
       
   404 
       
   405 // --------------------------------------------------------------------------
       
   406 // CPbk2MergeResolver::CompareAddresses
       
   407 // --------------------------------------------------------------------------
       
   408 //
       
   409 CPbk2MergeResolver::ECompareAddress CPbk2MergeResolver::CompareAddresses( 
       
   410         TPbk2FieldGroupId aGroupId )
       
   411     {
       
   412     ECompareAddress retCompare = ECAUndefined;
       
   413     
       
   414     TInt countEquals = 0;
       
   415     
       
   416     CPbk2PresentationContactFieldCollection& firstCollection = iFirstContact->PresentationFields();
       
   417     CPbk2PresentationContactFieldCollection& secondCollection = iSecondContact->PresentationFields();
       
   418     
       
   419     TInt countFirst = firstCollection.FieldCount();
       
   420     TInt countSecond = secondCollection.FieldCount();
       
   421     
       
   422     TInt countAddressFieldsFirst = 0;
       
   423     TInt countAddressFieldsSecond = 0;
       
   424     
       
   425     for( TInt idxFirst = 0; idxFirst < countFirst && retCompare != ECADifferent; idxFirst++)
       
   426         {
       
   427         const CPbk2PresentationContactField& fieldFirst = firstCollection.At( idxFirst );
       
   428         const MPbk2FieldProperty& propFirst = fieldFirst.FieldProperty();
       
   429         
       
   430         if( !AddressField( fieldFirst ) || propFirst.GroupId() != aGroupId )
       
   431             {
       
   432             continue;
       
   433             }
       
   434         
       
   435         countAddressFieldsFirst++;
       
   436         
       
   437         for( TInt idxSecond = 0; idxSecond < countSecond; idxSecond++)
       
   438             {
       
   439             const CPbk2PresentationContactField& fieldSecond = secondCollection.At( idxSecond );
       
   440             const MPbk2FieldProperty& propSecond = fieldSecond.FieldProperty();
       
   441             
       
   442             if( !AddressField( fieldSecond ) || propSecond.GroupId() != aGroupId )
       
   443                 {
       
   444                 continue;
       
   445                 }
       
   446             
       
   447             if( countAddressFieldsFirst == 1 )
       
   448                 {
       
   449                 countAddressFieldsSecond++;  
       
   450                 }
       
   451             
       
   452             if( !AreAddressesSubTypeSame( fieldFirst, fieldSecond ) )
       
   453                 {
       
   454                 continue;
       
   455                 }
       
   456             
       
   457             if( EqualsValues( fieldFirst, fieldSecond ) )
       
   458                 {
       
   459                 countEquals++;
       
   460                 }
       
   461             else
       
   462                 {
       
   463                 return ECADifferent;
       
   464                 }
       
   465             }
       
   466         }
       
   467     
       
   468     if( countAddressFieldsFirst == 0 )
       
   469         {
       
   470         for( TInt idxSecond = 0; idxSecond < countSecond; idxSecond++)
       
   471             {
       
   472             const CPbk2PresentationContactField& fieldSecond = secondCollection.At( idxSecond );
       
   473             const MPbk2FieldProperty& propSecond = fieldSecond.FieldProperty();
       
   474             
       
   475             if( AddressField( fieldSecond ) && propSecond.GroupId() == aGroupId )
       
   476                 {
       
   477                 countAddressFieldsSecond++; 
       
   478                 }
       
   479             }
       
   480         }
       
   481 
       
   482     return DoCompareAddresses( countEquals, countAddressFieldsFirst, countAddressFieldsSecond );
       
   483     }
       
   484 
       
   485 // --------------------------------------------------------------------------
       
   486 // CPbk2MergeResolver::CompareAddresses
       
   487 // --------------------------------------------------------------------------
       
   488 //
       
   489 CPbk2MergeResolver::ECompareAddress CPbk2MergeResolver::DoCompareAddresses( 
       
   490         TInt aCountEquals, 
       
   491         TInt aCountAddressFieldsFirst,
       
   492         TInt aCountAddressFieldsSecond )
       
   493     {
       
   494     ECompareAddress retCompare = ECAUndefined;
       
   495     
       
   496     if( aCountAddressFieldsFirst == 0 && aCountAddressFieldsSecond != 0 )
       
   497         {
       
   498         retCompare = ECASecondIncludesFirst;
       
   499         }
       
   500     else if( aCountAddressFieldsFirst != 0 && aCountAddressFieldsSecond == 0 )
       
   501         {
       
   502         retCompare = ECAFirstIncludesSecond;
       
   503         }
       
   504     else if( aCountAddressFieldsFirst != 0 && aCountAddressFieldsSecond != 0 )
       
   505         {
       
   506         if( aCountEquals == aCountAddressFieldsFirst && aCountEquals == aCountAddressFieldsSecond )
       
   507             {
       
   508             retCompare = ECASame;
       
   509             }
       
   510         else if( aCountEquals == aCountAddressFieldsFirst )
       
   511             {
       
   512             retCompare = ECASecondIncludesFirst;
       
   513             }
       
   514         else if( aCountEquals == aCountAddressFieldsSecond )
       
   515             {
       
   516             retCompare = ECAFirstIncludesSecond;
       
   517             }
       
   518         else
       
   519             {
       
   520             retCompare = ECADifferent;
       
   521             }
       
   522         }
       
   523     
       
   524     return retCompare;
       
   525     }
       
   526 
       
   527 // --------------------------------------------------------------------------
       
   528 // CPbk2MergeResolver::AddressField
       
   529 // --------------------------------------------------------------------------
       
   530 //
       
   531 TBool CPbk2MergeResolver::AddressField( const MVPbkStoreContactField& aField )
       
   532     {
       
   533     const MVPbkFieldType* fieldType = aField.BestMatchingFieldType();
       
   534     TBool address( EFalse );
       
   535     
       
   536     if ( fieldType )
       
   537         {
       
   538         TArray<TVPbkFieldVersitProperty> versitProp = fieldType->VersitProperties();
       
   539         
       
   540         for( TInt idx = 0; idx < versitProp.Count() && !address; idx++ )
       
   541             {
       
   542             TVPbkFieldVersitProperty versitPropFirst = versitProp[idx];
       
   543             if( versitPropFirst.Name() == EVPbkVersitNameADR ||
       
   544                 versitPropFirst.Name() == EVPbkVersitNameGEO )
       
   545                 {
       
   546                 address = ETrue;
       
   547                 }
       
   548             }
       
   549         }
       
   550     
       
   551     return address;
       
   552     }
       
   553 
       
   554 // --------------------------------------------------------------------------
       
   555 // CPbk2MergeResolver::FieldMultiplicity
       
   556 // --------------------------------------------------------------------------
       
   557 //
       
   558 TPbk2FieldMultiplicity CPbk2MergeResolver::FieldMultiplicity( const MVPbkStoreContactField& aField )
       
   559     {
       
   560     const CPbk2PresentationContactField& presentationField = 
       
   561             static_cast<const CPbk2PresentationContactField&>( aField );
       
   562     
       
   563     return presentationField.FieldProperty().Multiplicity();
       
   564     }
       
   565 
       
   566 // --------------------------------------------------------------------------
       
   567 // CPbk2MergeResolver::EqualsValues
       
   568 // --------------------------------------------------------------------------
       
   569 //
       
   570 TBool CPbk2MergeResolver::EqualsValues( 
       
   571         const MVPbkStoreContactField& aFieldFirst,
       
   572         const MVPbkStoreContactField& aFieldSecond )
       
   573     {
       
   574     const MVPbkContactFieldData& dataFirst = aFieldFirst.FieldData();
       
   575     TVPbkFieldStorageType storageTypeFirst = dataFirst.DataType();
       
   576     
       
   577     const MVPbkContactFieldData& dataSecond = aFieldSecond.FieldData();
       
   578     TVPbkFieldStorageType storageTypeSecond = dataSecond.DataType();
       
   579         
       
   580     if( storageTypeFirst != storageTypeSecond )
       
   581         {
       
   582         return EFalse;
       
   583         }
       
   584     
       
   585     TBool sameValues = EFalse;
       
   586     
       
   587     switch( storageTypeFirst )
       
   588         {
       
   589         case EVPbkFieldStorageTypeText:
       
   590             {
       
   591             if( MVPbkContactFieldTextData::Cast( dataFirst ).Text() == 
       
   592                 MVPbkContactFieldTextData::Cast( dataSecond ).Text() )
       
   593                 {
       
   594                 sameValues = ETrue;
       
   595                 }
       
   596             }
       
   597             break;
       
   598         case EVPbkFieldStorageTypeDateTime:
       
   599             {
       
   600             if( MVPbkContactFieldDateTimeData::Cast( dataFirst ).DateTime() == 
       
   601                 MVPbkContactFieldDateTimeData::Cast( dataSecond ).DateTime() )
       
   602                 {
       
   603                 sameValues = ETrue;
       
   604                 }
       
   605             }
       
   606             break;
       
   607         case EVPbkFieldStorageTypeUri:
       
   608             {
       
   609             if( MVPbkContactFieldUriData::Cast( dataFirst ).Text() == 
       
   610                 MVPbkContactFieldUriData::Cast( dataSecond ).Text() )
       
   611                 {
       
   612                 sameValues = ETrue;
       
   613                 }
       
   614             }
       
   615             break;
       
   616         case EVPbkFieldStorageTypeBinary:
       
   617             {
       
   618             const MVPbkContactFieldBinaryData& binaryFirst = 
       
   619                     MVPbkContactFieldBinaryData::Cast( dataFirst );
       
   620             const MVPbkContactFieldBinaryData& binarySecond = 
       
   621                     MVPbkContactFieldBinaryData::Cast( dataSecond );
       
   622             if( binaryFirst.BinaryData() == binarySecond.BinaryData() )
       
   623                 {
       
   624                 sameValues = ETrue;
       
   625                 }
       
   626             break;
       
   627             }
       
   628         // Ignore other field types
       
   629         }
       
   630     
       
   631     return sameValues;
       
   632     }
       
   633 
       
   634 // --------------------------------------------------------------------------
       
   635 // CPbk2MergeResolver::AreAddressesSubTypeSame
       
   636 // --------------------------------------------------------------------------
       
   637 //
       
   638 TBool CPbk2MergeResolver::AreAddressesSubTypeSame( 
       
   639         const MVPbkStoreContactField& aFieldFirst,
       
   640         const MVPbkStoreContactField& aFieldSecond )
       
   641     {
       
   642     TBool retCompare( EFalse );
       
   643     const MVPbkFieldType* fieldTypeFirst = aFieldFirst.BestMatchingFieldType();
       
   644     const MVPbkFieldType* fieldTypeSecond = aFieldSecond.BestMatchingFieldType();    
       
   645     
       
   646     if ( fieldTypeFirst && fieldTypeSecond )
       
   647         {
       
   648         TArray<TVPbkFieldVersitProperty> versitPropArrFirst = fieldTypeFirst->VersitProperties();
       
   649         TArray<TVPbkFieldVersitProperty> versitPropArrSecond = fieldTypeSecond->VersitProperties();
       
   650         
       
   651         TInt countFirst = versitPropArrFirst.Count();
       
   652         TInt countSecond = versitPropArrSecond.Count();    
       
   653         
       
   654         for( TInt idxFirst = 0; idxFirst < countFirst && !retCompare; idxFirst++ )
       
   655             {
       
   656             TVPbkFieldVersitProperty versitPropFirst = versitPropArrFirst[idxFirst];
       
   657             for( TInt idxSecond = 0; idxSecond < countSecond; idxSecond++ )
       
   658                 {
       
   659                 TVPbkFieldVersitProperty versitPropSecond = versitPropArrSecond[idxSecond];
       
   660                 if( versitPropFirst.SubField() == versitPropSecond.SubField() )
       
   661                     {
       
   662                     retCompare = ETrue;
       
   663                     break;
       
   664                     }
       
   665                 }
       
   666             }
       
   667         }
       
   668     
       
   669     return retCompare;
       
   670     }
       
   671 
       
   672 // --------------------------------------------------------------------------
       
   673 // CPbk2MergeResolver::AreFieldsTypeSame
       
   674 // --------------------------------------------------------------------------
       
   675 //
       
   676 TBool CPbk2MergeResolver::AreFieldsTypeSame( 
       
   677         const MVPbkStoreContactField& aFieldFirst,
       
   678         const MVPbkStoreContactField& aFieldSecond )
       
   679     {
       
   680     TBool isSame( EFalse );    
       
   681     const MVPbkFieldType* fieldTypeFirst = aFieldFirst.BestMatchingFieldType();  
       
   682     const MVPbkFieldType* fieldTypeSecond = aFieldSecond.BestMatchingFieldType();    
       
   683     
       
   684     if ( fieldTypeFirst && fieldTypeSecond )
       
   685         {
       
   686         isSame = fieldTypeFirst->IsSame( *fieldTypeSecond );
       
   687         }
       
   688     
       
   689     return isSame;
       
   690     }
       
   691 
       
   692 // --------------------------------------------------------------------------
       
   693 // CPbk2MergeResolver::AddConflictL
       
   694 // --------------------------------------------------------------------------
       
   695 //
       
   696 void CPbk2MergeResolver::AddConflictL( 
       
   697         const MVPbkStoreContactField& aFieldFirst,
       
   698         const MVPbkStoreContactField& aFieldSecond )
       
   699     {
       
   700     CPbk2MergeConflict* conflictNew = CPbk2MergeConflict::NewL();
       
   701     CleanupStack::PushL( conflictNew );
       
   702     conflictNew->AddFields( aFieldFirst, aFieldSecond );
       
   703 
       
   704     //For sync get possibly localised display fields 
       
   705     if( aFieldFirst.BestMatchingFieldType()->FieldTypeResId() == R_VPBK_FIELD_TYPE_SYNCCLASS )
       
   706         {
       
   707         HBufC* first = SyncDisplayFieldLC( aFieldFirst );
       
   708         if( first )
       
   709             {    
       
   710             HBufC* second = SyncDisplayFieldLC( aFieldSecond );
       
   711             if( second )
       
   712                 {
       
   713                 conflictNew->AddDisplayFields( first, second );
       
   714                 CleanupStack::Pop( 2, first );    
       
   715                 }
       
   716             else
       
   717                 {
       
   718                 CleanupStack::PopAndDestroy( first );
       
   719                 }
       
   720             }
       
   721         }
       
   722 
       
   723     iConflicts.AppendL( conflictNew );
       
   724     CleanupStack::Pop( conflictNew );
       
   725     
       
   726     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING( "CPbk2MergeResolver::AddConflictL - Conflict created" ) );
       
   727     }
       
   728 
       
   729 // --------------------------------------------------------------------------
       
   730 // CPbk2MergeResolver::SyncDisplayFieldLC
       
   731 // --------------------------------------------------------------------------
       
   732 //
       
   733 HBufC* CPbk2MergeResolver::SyncDisplayFieldLC(
       
   734         const MVPbkStoreContactField& aField )
       
   735     {
       
   736     HBufC* field = NULL;
       
   737     TVPbkFieldStorageType dataType = aField.FieldData().DataType();
       
   738 
       
   739     if ( dataType == EVPbkFieldStorageTypeText )
       
   740          {
       
   741          const MVPbkContactFieldTextData& textData =
       
   742                MVPbkContactFieldTextData::Cast( aField.FieldData() );
       
   743          field = LocaliseSyncDisplayFieldLC( textData.Text() );
       
   744          }
       
   745 
       
   746     return field;
       
   747     }
       
   748 
       
   749 // --------------------------------------------------------------------------
       
   750 // CPbk2MergeResolver::LocaliseSyncDisplayFieldL
       
   751 // --------------------------------------------------------------------------
       
   752 //
       
   753 HBufC* CPbk2MergeResolver::LocaliseSyncDisplayFieldLC( const TDesC& aContent )
       
   754     {
       
   755     HBufC* txt = NULL;
       
   756 
       
   757     if ( !aContent.CompareF( KVPbkContactSyncPublic ) )
       
   758         {
       
   759         txt = CCoeEnv::Static()->AllocReadResourceLC( R_QTN_CALE_CONFIDENT_PUBLIC );
       
   760         }
       
   761     else if ( !aContent.CompareF( KVPbkContactSyncNoSync ) )
       
   762         {
       
   763         txt = CCoeEnv::Static()->AllocReadResourceLC( R_QTN_CALE_CONFIDENT_NONE );
       
   764         }
       
   765     else
       
   766         {
       
   767         txt = CCoeEnv::Static()->AllocReadResourceLC( R_QTN_CALE_CONFIDENT_PRIVATE );
       
   768         }
       
   769 
       
   770     return txt;
       
   771     }
       
   772 
       
   773 // --------------------------------------------------------------------------
       
   774 // CPbk2MergeResolver::AddAddressConflictL
       
   775 // --------------------------------------------------------------------------
       
   776 //
       
   777 void CPbk2MergeResolver::AddAddressConflictL( TPbk2FieldGroupId aGroupId )
       
   778     {
       
   779     CPbk2MergeConflictAddress* conflictNew = CPbk2MergeConflictAddress::NewL();
       
   780     CleanupStack::PushL( conflictNew );
       
   781     conflictNew->AddAddress( *iFirstContact, *iSecondContact, aGroupId );
       
   782     iConflicts.AppendL( conflictNew );
       
   783     CleanupStack::Pop( conflictNew );
       
   784     
       
   785     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING( "CPbk2MergeResolver::AddAddressConflictL - Conflict created" ) );
       
   786     }
       
   787 
       
   788 // --------------------------------------------------------------------------
       
   789 // CPbk2MergeResolver::FindConflictByFieldL
       
   790 // --------------------------------------------------------------------------
       
   791 //
       
   792 TInt CPbk2MergeResolver::FindConflictByFieldL( const MVPbkStoreContactField& aField )
       
   793     {
       
   794     TInt countConflicts = iConflicts.Count();
       
   795     for( TInt idx = 0; idx < countConflicts; idx++ )
       
   796         {
       
   797         if( iConflicts[idx] && 
       
   798             ( iConflicts[idx]->GetConflictType() == EPbk2ConflictTypeNormal || 
       
   799               iConflicts[idx]->GetConflictType() == EPbk2ConflictTypeImage ) )
       
   800             {
       
   801             CPbk2MergeConflict* conflict = static_cast<CPbk2MergeConflict*>( iConflicts[idx] );
       
   802             if( conflict )
       
   803                 {
       
   804                 const MVPbkStoreContactField* fieldFirst = NULL;
       
   805                 const MVPbkStoreContactField* fieldSecond = NULL;
       
   806                 conflict->GetFieldsL( fieldFirst, fieldSecond );
       
   807                 if( fieldFirst == &aField || fieldSecond == &aField )
       
   808                     {
       
   809                     return idx;
       
   810                     }
       
   811                 }
       
   812             }
       
   813         }
       
   814     return KErrNotFound;
       
   815     }
       
   816 
       
   817 // --------------------------------------------------------------------------
       
   818 // CPbk2MergeResolver::CopyAddressToMergedL
       
   819 // --------------------------------------------------------------------------
       
   820 //
       
   821 void CPbk2MergeResolver::CopyAddressToMergedL( 
       
   822         const CPbk2PresentationContact& aContact, 
       
   823         TPbk2FieldGroupId aAddressGroup )
       
   824     {
       
   825     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   826         ("CPbk2MergeResolver::CopyAddressToMergedL - Add address(group id:%d) to merged. Fields:"), 
       
   827         aAddressGroup);
       
   828     CPbk2PresentationContactFieldCollection& fields = aContact.PresentationFields();
       
   829     for( TInt idx = 0; idx < fields.FieldCount(); idx++ )
       
   830         {
       
   831         CPbk2PresentationContactField& field = fields.At( idx );
       
   832         const MPbk2FieldProperty& property = field.FieldProperty();
       
   833         if( property.GroupId() == aAddressGroup )
       
   834             {
       
   835             DEBUG_PRINT_FIELD( field )
       
   836             iMerged.AppendL( &field );
       
   837             }
       
   838         }
       
   839     }
       
   840 
       
   841 #ifdef _DEBUG
       
   842 // --------------------------------------------------------------------------
       
   843 // CPbk2MergeResolver::PrintFieldL
       
   844 // --------------------------------------------------------------------------
       
   845 //
       
   846 void CPbk2MergeResolver::PrintFieldL( const MVPbkStoreContactField& aField )
       
   847     {
       
   848     const MVPbkContactFieldData& data = aField.FieldData();
       
   849     TVPbkFieldStorageType storageType = data.DataType();
       
   850     
       
   851     RBuf lebel;
       
   852     CleanupClosePushL( lebel );
       
   853     RBuf value;
       
   854     CleanupClosePushL( value );
       
   855     
       
   856     lebel.CreateL( aField.FieldLabel().Length() + 1 );
       
   857     lebel = aField.FieldLabel();
       
   858     lebel.SetLength( aField.FieldLabel().Length() + 1 );
       
   859     lebel[aField.FieldLabel().Length()] = KEndLine;
       
   860     
       
   861     switch( storageType )
       
   862         {
       
   863         case EVPbkFieldStorageTypeText:
       
   864             {
       
   865             value.CreateL( MVPbkContactFieldTextData::Cast( data ).Text().Length() + 1 );
       
   866             value = MVPbkContactFieldTextData::Cast( data ).Text();
       
   867             value.SetLength( MVPbkContactFieldTextData::Cast( data ).Text().Length() + 1 );
       
   868             value[MVPbkContactFieldTextData::Cast( data ).Text().Length()] = KEndLine;
       
   869             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   870                 ( "CPbk2MergeResolver: Field Info: %s:%s "), lebel.Ptr(), value.Ptr() );
       
   871             }
       
   872             break;
       
   873         case EVPbkFieldStorageTypeDateTime:
       
   874             {
       
   875             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   876                 ( "CPbk2MergeResolver: Field Info: %s"), lebel.Ptr() );
       
   877             }
       
   878             break;
       
   879         case EVPbkFieldStorageTypeUri:
       
   880             {
       
   881             value.CreateL( MVPbkContactFieldUriData::Cast( data ).Text().Length() + 1 );
       
   882             value = MVPbkContactFieldUriData::Cast( data ).Text();
       
   883             value.SetLength( MVPbkContactFieldUriData::Cast( data ).Text().Length() + 1 );
       
   884             value[MVPbkContactFieldUriData::Cast( data ).Text().Length()] = KEndLine;
       
   885             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   886                 ( "CPbk2MergeResolver: Field Info: %s:%s "), lebel.Ptr(), value.Ptr() );
       
   887             }
       
   888             break;
       
   889         }
       
   890     CleanupStack::PopAndDestroy( &value );
       
   891     CleanupStack::PopAndDestroy( &lebel );
       
   892     }
       
   893 #endif