phonebookui/Phonebook2/UIPolicy/src/CPbk2ContactDuplicatePolicy.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
child 68 9da50d567e3c
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2002-2007 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 contact duplicate policy.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2ContactDuplicatePolicy.h"
       
    20 
       
    21 // Phonebook2
       
    22 #include <CPbk2SortOrderManager.h>
       
    23 #include <Pbk2ContactNameFormatterFactory.h>
       
    24 #include <MPbk2ContactNameFormatter.h>
       
    25 #include <CPbk2DuplicateContactFinder.h>
       
    26 #include <MPbk2DuplicateContactObserver.h>
       
    27 #include <RPbk2LocalizedResourceFile.h>
       
    28 #include "Pbk2DataCaging.hrh"
       
    29 #include "Pbk2PresentationUtils.h"
       
    30 #include <Pbk2Presentation.rsg>
       
    31 #include <CVPbkFieldTypeSelector.h>
       
    32 #include <CVPbkFieldTypeIterator.h>
       
    33 #include <CVPbkFieldTypeRefsList.h>
       
    34 
       
    35 // Virtual Phonebook
       
    36 #include <CVPbkContactManager.h>
       
    37 #include <MVPbkContactOperationBase.h>
       
    38 #include <CVPbkContactLinkArray.h>
       
    39 #include <MVPbkStoreContact.h>
       
    40 
       
    41 // System includes
       
    42 #include <barsread.h>
       
    43 
       
    44 
       
    45 /// Unnamed namespace for local definitions
       
    46 namespace {
       
    47 
       
    48 NONSHARABLE_CLASS(CPbk2DuplicateOperation) :
       
    49             public CBase,
       
    50             public MVPbkContactOperationBase,
       
    51             private MPbk2DuplicateContactObserver
       
    52     {
       
    53     public: // Construction and destruction
       
    54 
       
    55         /**
       
    56          * Creates a new instance of this class.
       
    57          *
       
    58          * @param aFinder               Duplicate contact finder.
       
    59          * @param aOwnDuplicates        Array of duplicates.
       
    60          * @param aDuplicates           Array of duplicates.
       
    61          * @param aContact              Contact.
       
    62          * @param aTargetStore          Target store.
       
    63          * @param aObserver             Find observer.
       
    64          * @param aMaxDuplicatesToFind  Maximum amount to find.
       
    65          * @return  A new instance of this class.
       
    66          */
       
    67         static CPbk2DuplicateOperation* NewL(
       
    68                 CPbk2DuplicateContactFinder& aFinder,
       
    69                 RPointerArray<MVPbkStoreContact>& aOwnDuplicates,
       
    70                 RPointerArray<MVPbkStoreContact>& aDuplicates,
       
    71                 const MVPbkBaseContact& aContact,
       
    72                 MVPbkContactStore& aTargetStore,
       
    73                 MVPbkContactFindObserver& aObserver,
       
    74                 TInt aMaxDuplicatesToFind );
       
    75 
       
    76         /**
       
    77          * Destructor.
       
    78          */
       
    79         ~CPbk2DuplicateOperation();
       
    80 
       
    81     private: // From MPbk2DuplicateContactObserver
       
    82         void DuplicateFindComplete();
       
    83         void DuplicateFindFailed(
       
    84                 TInt aError );
       
    85 
       
    86     private: // Implementation
       
    87         CPbk2DuplicateOperation(
       
    88                 CPbk2DuplicateContactFinder& aFinder,
       
    89                 RPointerArray<MVPbkStoreContact>& aOwnDuplicates,
       
    90                 RPointerArray<MVPbkStoreContact>& aDuplicates,
       
    91                 MVPbkContactFindObserver& aObserver );
       
    92         void ConstructL(
       
    93                 const MVPbkBaseContact& aContact,
       
    94                 MVPbkContactStore& aTargetStore,
       
    95                 TInt aMaxDuplicatesToFind );
       
    96         void HandleFindCompleteL();
       
    97 
       
    98     private: // Data
       
    99         /// Ref: Array of duplicates
       
   100         RPointerArray<MVPbkStoreContact>& iDuplicates;
       
   101         /// Ref: Array of duplicates
       
   102         RPointerArray<MVPbkStoreContact>& iOwnDuplicates;
       
   103         /// Ref: Duplicate finder
       
   104         CPbk2DuplicateContactFinder& iFinder;
       
   105         /// Ref: Observer
       
   106         MVPbkContactFindObserver& iObserver;
       
   107     };
       
   108 
       
   109 // --------------------------------------------------------------------------
       
   110 // CPbk2DuplicateOperation::CPbk2DuplicateOperation
       
   111 // --------------------------------------------------------------------------
       
   112 //
       
   113 CPbk2DuplicateOperation::CPbk2DuplicateOperation(
       
   114         CPbk2DuplicateContactFinder& aFinder,
       
   115         RPointerArray<MVPbkStoreContact>& aOwnDuplicates,
       
   116         RPointerArray<MVPbkStoreContact>& aDuplicates,
       
   117         MVPbkContactFindObserver& aObserver ) :
       
   118             iDuplicates( aDuplicates ),
       
   119             iOwnDuplicates( aOwnDuplicates ),
       
   120             iFinder( aFinder ),
       
   121             iObserver( aObserver )
       
   122     {
       
   123     }
       
   124 
       
   125 // --------------------------------------------------------------------------
       
   126 // CPbk2DuplicateOperation::~CPbk2DuplicateOperation
       
   127 // --------------------------------------------------------------------------
       
   128 //
       
   129 CPbk2DuplicateOperation::~CPbk2DuplicateOperation()
       
   130     {
       
   131     }
       
   132 
       
   133 // --------------------------------------------------------------------------
       
   134 // CPbk2DuplicateOperation::NewL
       
   135 // --------------------------------------------------------------------------
       
   136 //
       
   137 CPbk2DuplicateOperation* CPbk2DuplicateOperation::NewL(
       
   138         CPbk2DuplicateContactFinder& aFinder,
       
   139         RPointerArray<MVPbkStoreContact>& aOwnDuplicates,
       
   140         RPointerArray<MVPbkStoreContact>& aDuplicates,
       
   141         const MVPbkBaseContact& aContact,
       
   142         MVPbkContactStore& aTargetStore,
       
   143         MVPbkContactFindObserver& aObserver,
       
   144         TInt aMaxDuplicatesToFind )
       
   145     {
       
   146     CPbk2DuplicateOperation* self = new( ELeave ) CPbk2DuplicateOperation(
       
   147         aFinder, aOwnDuplicates, aDuplicates, aObserver );
       
   148     CleanupStack::PushL( self );
       
   149     self->ConstructL( aContact, aTargetStore, aMaxDuplicatesToFind );
       
   150     CleanupStack::Pop( self );
       
   151     return self;
       
   152     }
       
   153 
       
   154 // --------------------------------------------------------------------------
       
   155 // CPbk2DuplicateOperation::ConstructL
       
   156 // --------------------------------------------------------------------------
       
   157 //
       
   158 void CPbk2DuplicateOperation::ConstructL
       
   159         ( const MVPbkBaseContact& aContact, MVPbkContactStore& aTargetStore,
       
   160           TInt aMaxDuplicatesToFind )
       
   161     {
       
   162     iFinder.StartL( aContact, aTargetStore, *this, aMaxDuplicatesToFind );
       
   163     }
       
   164 
       
   165 // --------------------------------------------------------------------------
       
   166 // CPbk2DuplicateOperation::DuplicateFindComplete
       
   167 // --------------------------------------------------------------------------
       
   168 //
       
   169 void CPbk2DuplicateOperation::DuplicateFindComplete()
       
   170     {
       
   171     TRAPD( res, HandleFindCompleteL() );
       
   172     if ( res != KErrNone )
       
   173         {
       
   174         DuplicateFindFailed( res );
       
   175         }
       
   176     }
       
   177 
       
   178 // --------------------------------------------------------------------------
       
   179 // CPbk2DuplicateOperation::DuplicateFindFailed
       
   180 // --------------------------------------------------------------------------
       
   181 //
       
   182 void CPbk2DuplicateOperation::DuplicateFindFailed( TInt aError )
       
   183     {
       
   184     iObserver.FindFailed( aError );
       
   185     }
       
   186 
       
   187 // --------------------------------------------------------------------------
       
   188 // CPbk2DuplicateOperation::HandleFindCompleteL
       
   189 // --------------------------------------------------------------------------
       
   190 //
       
   191 void CPbk2DuplicateOperation::HandleFindCompleteL()
       
   192     {
       
   193     CVPbkContactLinkArray* links = CVPbkContactLinkArray::NewLC();
       
   194     const TInt count = iOwnDuplicates.Count();
       
   195     for ( TInt i = count - 1; i >= 0; --i )
       
   196         {
       
   197         MVPbkStoreContact* contact = iOwnDuplicates[i];
       
   198 
       
   199         // Create links for results
       
   200         links->AppendL( iOwnDuplicates[i]->CreateLinkLC() );
       
   201         CleanupStack::Pop(); // link
       
   202 
       
   203         // Ownership of the contact changes from
       
   204         // iOwnDuplicates to iDuplicates
       
   205         iDuplicates.InsertL( contact, 0 );
       
   206         iOwnDuplicates.Remove(i);
       
   207         }
       
   208 
       
   209     CleanupStack::Pop( links );
       
   210     // Give ownership to client
       
   211     iObserver.FindCompleteL( links );
       
   212     }
       
   213 
       
   214 } /// namespace
       
   215 
       
   216 // --------------------------------------------------------------------------
       
   217 // CPbk2ContactDuplicatePolicy::CPbk2ContactDuplicatePolicy
       
   218 // --------------------------------------------------------------------------
       
   219 //
       
   220 CPbk2ContactDuplicatePolicy::CPbk2ContactDuplicatePolicy(
       
   221         CVPbkContactManager& aContactManager ) :
       
   222             iContactManager( aContactManager )
       
   223     {
       
   224     }
       
   225 
       
   226 // --------------------------------------------------------------------------
       
   227 // CPbk2ContactDuplicatePolicy::~CPbk2ContactDuplicatePolicy
       
   228 // --------------------------------------------------------------------------
       
   229 //
       
   230 CPbk2ContactDuplicatePolicy::~CPbk2ContactDuplicatePolicy()
       
   231     {
       
   232     iDuplicates.ResetAndDestroy();
       
   233     delete iDuplicateFinder;
       
   234     delete iNameFormatter;
       
   235     delete iSortOrderManager;
       
   236     delete iFieldTypeRefsList;
       
   237     }
       
   238 
       
   239 // --------------------------------------------------------------------------
       
   240 // CPbk2ContactDuplicatePolicy::NewL
       
   241 // --------------------------------------------------------------------------
       
   242 //
       
   243 CPbk2ContactDuplicatePolicy* CPbk2ContactDuplicatePolicy::NewL
       
   244         ( TParam* aParam )
       
   245     {
       
   246     CPbk2ContactDuplicatePolicy* self =
       
   247         new( ELeave ) CPbk2ContactDuplicatePolicy( aParam->iContactManager );
       
   248     CleanupStack::PushL( self );
       
   249     self->ConstructL( aParam->iFieldTypesForFind );
       
   250     CleanupStack::Pop( self );
       
   251     return self;
       
   252     }
       
   253 
       
   254 // --------------------------------------------------------------------------
       
   255 // CPbk2ContactDuplicatePolicy::ConstructL
       
   256 // --------------------------------------------------------------------------
       
   257 //
       
   258 void CPbk2ContactDuplicatePolicy::ConstructL
       
   259         ( const MVPbkFieldTypeList* aFieldTypeForFind )
       
   260     {
       
   261     iSortOrderManager = CPbk2SortOrderManager::NewL(
       
   262         iContactManager.FieldTypes() );
       
   263     iNameFormatter = Pbk2ContactNameFormatterFactory::CreateL(
       
   264         iContactManager.FieldTypes(), *iSortOrderManager,
       
   265         &iContactManager.FsSession() );
       
   266 
       
   267     const MVPbkFieldTypeList* fieldTypesForFind = aFieldTypeForFind;
       
   268     if ( !fieldTypesForFind )
       
   269         {
       
   270         iFieldTypeRefsList = CreateFieldTypesForFindL(iContactManager);
       
   271         fieldTypesForFind = iFieldTypeRefsList;
       
   272         }
       
   273     iDuplicateFinder = CPbk2DuplicateContactFinder::NewL( iContactManager,
       
   274         *iNameFormatter, *fieldTypesForFind, iDuplicates );
       
   275     }
       
   276 
       
   277 // --------------------------------------------------------------------------
       
   278 // CPbk2ContactDuplicatePolicy::FindDuplicatesL
       
   279 // --------------------------------------------------------------------------
       
   280 //
       
   281 MVPbkContactOperationBase* CPbk2ContactDuplicatePolicy::FindDuplicatesL
       
   282         ( const MVPbkBaseContact& aContact,
       
   283           MVPbkContactStore& aTargetStore,
       
   284           RPointerArray<MVPbkStoreContact>& aDuplicates,
       
   285           MVPbkContactFindObserver& aObserver,
       
   286           TInt aMaxDuplicatesToFind )
       
   287     {
       
   288     iDuplicates.ResetAndDestroy();
       
   289     return CPbk2DuplicateOperation::NewL( *iDuplicateFinder, iDuplicates,
       
   290         aDuplicates, aContact, aTargetStore, aObserver,
       
   291         aMaxDuplicatesToFind );
       
   292     }
       
   293 
       
   294 // --------------------------------------------------------------------------
       
   295 // CPbk2ContactDuplicatePolicy::CreateFieldTypesForFindL
       
   296 // --------------------------------------------------------------------------
       
   297 //
       
   298 MVPbkFieldTypeList* CPbk2ContactDuplicatePolicy::CreateFieldTypesForFindL
       
   299         (CVPbkContactManager& aContactManager) const
       
   300     {
       
   301     RFs fs = aContactManager.FsSession();
       
   302     RPbk2LocalizedResourceFile resFile(&fs);
       
   303     resFile.OpenLC(KPbk2RomFileDrive, 
       
   304         KDC_RESOURCE_FILES_DIR, 
       
   305         Pbk2PresentationUtils::PresentationResourceFile());
       
   306 
       
   307     // Create resource reader.
       
   308     TResourceReader resReader;
       
   309     resReader.SetBuffer(resFile.AllocReadLC(R_TITLE_FIELD_SELECTOR));
       
   310 
       
   311     // Create title field selector.
       
   312     CVPbkFieldTypeSelector* titleFieldSelector = CVPbkFieldTypeSelector::NewL
       
   313         (resReader, aContactManager.FieldTypes());
       
   314     CleanupStack::PushL(titleFieldSelector);
       
   315     
       
   316     // Create field type list for find.
       
   317     CVPbkFieldTypeRefsList* fieldTypeRefsList = CVPbkFieldTypeRefsList::NewL();
       
   318     CleanupStack::PushL(fieldTypeRefsList);
       
   319     const MVPbkFieldType* fieldType = NULL;
       
   320 
       
   321     // Create field type iterator.
       
   322     CVPbkFieldTypeIterator* fieldTypeIterator =
       
   323         CVPbkFieldTypeIterator::NewLC(*titleFieldSelector, 
       
   324             aContactManager.FieldTypes());
       
   325     while(fieldTypeIterator->HasNext())
       
   326         {
       
   327         fieldType = fieldTypeIterator->Next();
       
   328         if (fieldType)
       
   329             {
       
   330             // Filter the Versit type for find.
       
   331             if (EVPbkNonVersitTypeNone == fieldType->NonVersitType())
       
   332                 {
       
   333                 fieldTypeRefsList->AppendL(*fieldType);
       
   334                 }
       
   335             }
       
   336         }
       
   337 
       
   338     CleanupStack::PopAndDestroy();  // fieldTypeIterator
       
   339     CleanupStack::Pop(fieldTypeRefsList);
       
   340     CleanupStack::PopAndDestroy();  // titleFieldSelector
       
   341     CleanupStack::PopAndDestroy();  // resReader
       
   342     CleanupStack::PopAndDestroy();  // resFile
       
   343 
       
   344     return fieldTypeRefsList;
       
   345     }
       
   346 
       
   347 // End of File