phoneengine/PhoneCntFinder/ContactService/src/CPhCntMatcherImpl.cpp
branchRCL_3
changeset 81 c26cc2a7c548
parent 62 5266b1f337bd
equal deleted inserted replaced
73:e30d4a1b8bad 81:c26cc2a7c548
    13 *
    13 *
    14 * Description:  Matcher implementation
    14 * Description:  Matcher implementation
    15 *
    15 *
    16 */
    16 */
    17 
    17 
       
    18 
       
    19 
    18 #include <CVPbkPhoneNumberMatchStrategy.h>
    20 #include <CVPbkPhoneNumberMatchStrategy.h>
    19 #include <MVPbkContactLinkArray.h>
    21 #include <MVPbkContactLinkArray.h>
    20 #include <CPbk2StoreConfiguration.h>
    22 #include <CPbk2StoreConfiguration.h>
    21 #include <MVPbkContactStoreProperties.h>
    23 #include <MVPbkContactStoreProperties.h>
    22 #include <MVPbkContactStore.h>
    24 #include <MVPbkContactStore.h>
    23 #include <CVPbkContactStoreUriArray.h>
    25 #include <CVPbkContactStoreUriArray.h>
    24 #include <talogger.h>
    26 
    25 #include <telephonyvariant.hrh>
       
    26 #include <telinternalcrkeys.h>
       
    27 #include <centralrepository.h>
       
    28 
    27 
    29 #include "CPhCntMatcherImpl.h"
    28 #include "CPhCntMatcherImpl.h"
    30 #include "cphcntmatchcontact.h"
    29 #include "cphcntmatchcontact.h"
    31 #include "cphcntfetchcontact.h"
    30 #include "cphcntfetchcontact.h"
    32 #include "cphcntfoundcontacts.h"
    31 #include "cphcntfoundcontacts.h"
    34 #include "CPhCntContactManager.h"
    33 #include "CPhCntContactManager.h"
    35 #include "CPhCntContactStores.h"
    34 #include "CPhCntContactStores.h"
    36 #include "MPhoneCntPbkOwner.h"
    35 #include "MPhoneCntPbkOwner.h"
    37 #include "cphcntvpbkcontactid.h"
    36 #include "cphcntvpbkcontactid.h"
    38 #include "cphcntcontactmatchstrategy.h"
    37 #include "cphcntcontactmatchstrategy.h"
    39 
    38 #include <talogger.h>
    40 // Local functions
    39 
    41 /**
       
    42  * Gets contact selection strategy from localvariation flag.
       
    43  */
       
    44 TPhCntContactSelectionStrategy::TAllowSeveralMatches 
       
    45     GetContactSelectionStrategyL()
       
    46     {
       
    47     CRepository* repository = CRepository::NewLC( KCRUidTelVariation );
       
    48     TInt variationFlag;
       
    49     User::LeaveIfError( repository->Get( KTelVariationFlags, variationFlag ) );
       
    50     CleanupStack::PopAndDestroy( repository );
       
    51     
       
    52     if( variationFlag & KTelephonyLVFlagShowContactWhenSeveralMatches ) 
       
    53         {
       
    54         return TPhCntContactSelectionStrategy::EAllowSeveralMatches;
       
    55         }
       
    56     else
       
    57         {
       
    58         return TPhCntContactSelectionStrategy::EAllowSingleMatch;
       
    59         }
       
    60     }
       
    61 
    40 
    62 
    41 
    63 // ---------------------------------------------------------------------------
    42 // ---------------------------------------------------------------------------
    64 // Static constructor
    43 // Static constructor
    65 // ---------------------------------------------------------------------------
    44 // ---------------------------------------------------------------------------
   210     const TDesC& aTelNumber )
   189     const TDesC& aTelNumber )
   211     {
   190     {
   212     TEFLOGSTRING( KTAOBJECT, "CNT CPhCntMatcherImpl::GetContactL" );
   191     TEFLOGSTRING( KTAOBJECT, "CNT CPhCntMatcherImpl::GetContactL" );
   213     // Try to find matching contact.
   192     // Try to find matching contact.
   214     const MVPbkContactLinkArray* linkArray = NULL;
   193     const MVPbkContactLinkArray* linkArray = NULL;
   215     TInt err = MatchContactL( linkArray, aTelNumber, 
   194     delete iCSMatchStrategy;
   216         MPhCntContactManager::EDontRemoveDuplicates );
   195     iCSMatchStrategy = 0;
       
   196     iCSMatchStrategy = iContactManager.CreateContactMatchStrategyL( *iMatchContact,
       
   197             MPhCntContactManager::EDontRemoveDuplicates ); 
       
   198     TInt err( iMatchContact->MatchContact( linkArray, aTelNumber,
       
   199         *iCSMatchStrategy ) );
   217 
   200 
   218     CPhCntContact* match( NULL );
   201     CPhCntContact* match( NULL );
   219     TInt index( KErrNotFound );
   202     TInt index( KErrNotFound );
   220 
   203 
   221     // Apply exact match on additional stores first.
   204     // Apply exact match on additional stores first.
   222     // If match is found, don't care about other stores as
   205     // If match is found, don't care about other stores as
   223     // these come first.
   206     // these come first.
   224     if( !err )
   207     if(  !err && ( linkArray->Count() > 1 ) )
   225         {        
   208         {
   226         const CVPbkContactStoreUriArray& additionalStores = iContactManager.AdditionalContactStoreUris();
   209         index = MatchFromAdditionalStore( *linkArray );
   227         index = iContactSelectionStrategy.ApplyAdditonalStoreStrategy( *linkArray, additionalStores );  
   210 
   228         
   211         // Single match on additonal stores
   229         const TBool manyContactsFound = index == KManyContacts;
   212         if ( index != KErrNotFound && index != KErrAlreadyExists )
   230         const TBool singleContactFound = 
       
   231             index != KNoContact && index != KManyContacts;
       
   232         
       
   233         if ( singleContactFound )
       
   234             {
   213             {
   235             FetchContact( match, linkArray->At( index ), aTelNumber );
   214             FetchContact( match, linkArray->At( index ), aTelNumber );
   236             aMatch = match;
   215             aMatch = match;
   237             return err;
   216             return err;
   238             }
   217             }
   239         else if ( manyContactsFound ) 
   218         else if ( index == KErrAlreadyExists ) //Several matches on additional stores
   240             {
   219             {
   241             aMatch = match;
   220             aMatch = match;
   242             return KErrNotFound;
   221             return KErrNotFound;
   243             }
   222             }
   244         }
   223         }
   245 
   224 
   246     if(  !err && ( linkArray->Count() > 1 ) )
   225     if(  !err && ( linkArray->Count() > 1 ) )
   247         {
   226         {
   248         err = MatchContactL( linkArray, aTelNumber,
   227         delete iCSMatchStrategy;
   249             MPhCntContactManager::ERemoveDuplicates );
   228         iCSMatchStrategy = 0;
       
   229         iCSMatchStrategy = iContactManager.CreateContactMatchStrategyL(
       
   230             *iMatchContact, MPhCntContactManager::ERemoveDuplicates ); 
       
   231         err = iMatchContact->MatchContact( linkArray, aTelNumber,
       
   232             *iCSMatchStrategy );
   250         }
   233         }
   251     if ( !err )
   234     if ( !err )
   252         {
   235         {
   253         index = iContactSelectionStrategy.ApplyStrategy( *linkArray );        
   236         // If only one contact, no additional checks needed
   254         }
   237         if ( linkArray->Count() == 1 )
       
   238             {
       
   239             index = 0;
       
   240             }
       
   241         }
       
   242 
   255 
   243 
   256     // Finally fetch contact details
   244     // Finally fetch contact details
   257     if ( index != KNoContact )
   245     if ( index != KErrNotFound )
   258         {
   246         {
   259         FetchContact( match, linkArray->At( index ), aTelNumber );
   247         FetchContact( match, linkArray->At( index ), aTelNumber );
   260         aMatch = match;
   248         }
   261         }
   249 
   262     else
   250     aMatch = match;
   263         {
       
   264         err = KErrNotFound;
       
   265         }
       
   266     
       
   267     TEFLOGSTRING2( KTAMESIN,"CNT CPhCntMatcherImpl::GetContactL,err: %d", err );    
       
   268     return err;
       
   269     }
       
   270 
       
   271 // ---------------------------------------------------------------------------
       
   272 // Matches contact
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 TInt CPhCntMatcherImpl::MatchContactL( 
       
   276     const MVPbkContactLinkArray*& aContactLinkArray, 
       
   277     const TDesC& aTelNumber,
       
   278     MPhCntContactManager::TDuplicateRemovalStrategy aRemoveDuplicatesStrategy )
       
   279     {
       
   280     delete iCSMatchStrategy;
       
   281     iCSMatchStrategy = NULL;
       
   282     iCSMatchStrategy = iContactManager.CreateContactMatchStrategyL( 
       
   283         *iMatchContact,
       
   284         aRemoveDuplicatesStrategy );
       
   285     
       
   286     const TInt err( iMatchContact->MatchContact( aContactLinkArray, aTelNumber,
       
   287         *iCSMatchStrategy ) );
       
   288     return err;
   251     return err;
   289     }
   252     }
   290 
   253 
   291 
   254 
   292 // ---------------------------------------------------------------------------
   255 // ---------------------------------------------------------------------------
   329 // Second phase constructor
   292 // Second phase constructor
   330 // ---------------------------------------------------------------------------
   293 // ---------------------------------------------------------------------------
   331 //
   294 //
   332 void CPhCntMatcherImpl::ConstructL()
   295 void CPhCntMatcherImpl::ConstructL()
   333     {
   296     {
   334     const TPhCntContactSelectionStrategy::TAllowSeveralMatches strategy = 
       
   335         GetContactSelectionStrategyL();
       
   336     iContactSelectionStrategy.SetContactSelectionStrategy( strategy );    
       
   337     }
   297     }
   338 
   298 
   339 // ---------------------------------------------------------------------------
   299 // ---------------------------------------------------------------------------
   340 // Fetches contact
   300 // Fetches contact
   341 // ---------------------------------------------------------------------------
   301 // ---------------------------------------------------------------------------
   361             }
   321             }
   362         }
   322         }
   363     return err;
   323     return err;
   364     }
   324     }
   365 
   325 
       
   326 // --------------------------------------------------------------------------
       
   327 // CPhCntMatcherImpl::MatchFromAdditionalStore
       
   328 // --------------------------------------------------------------------------
       
   329 //
       
   330 TInt CPhCntMatcherImpl::MatchFromAdditionalStore(
       
   331     const MVPbkContactLinkArray& linkArray ) const
       
   332     {
       
   333     TEFLOGSTRING( KTAOBJECT, "CNT CPhCntMatcherImpl::MatchFromAdditionalStore" );
       
   334     TInt ret( KErrNotFound );
       
   335     TInt found(0); // count of found contacts from additional stores.
       
   336 
       
   337     for ( TInt i = 0; i < linkArray.Count(); i++ )
       
   338         {
       
   339         TVPbkContactStoreUriPtr uri =
       
   340             linkArray.At( i ).ContactStore().StoreProperties().Uri();
       
   341 
       
   342         // Compare if contact is from additional store.
       
   343         if ( iContactManager.AdditionalContactStoreUris().IsIncluded( uri ) )
       
   344             {
       
   345             // Contact found from additional store.
       
   346             found++;
       
   347             ret = i;
       
   348             }
       
   349         }
       
   350 
       
   351     if ( found > 1)
       
   352         {
       
   353         // Multiple matches from additional stores -> no match.
       
   354         ret = KErrAlreadyExists;
       
   355         }
       
   356 
       
   357     return ret;
       
   358     }
       
   359