phoneengine/PhoneCntFinder/ContactService/src/CPhCntMatcherImpl.cpp
branchRCL_3
changeset 62 5266b1f337bd
child 81 c26cc2a7c548
equal deleted inserted replaced
61:41a7f70b3818 62:5266b1f337bd
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Matcher implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include <CVPbkPhoneNumberMatchStrategy.h>
       
    19 #include <MVPbkContactLinkArray.h>
       
    20 #include <CPbk2StoreConfiguration.h>
       
    21 #include <MVPbkContactStoreProperties.h>
       
    22 #include <MVPbkContactStore.h>
       
    23 #include <CVPbkContactStoreUriArray.h>
       
    24 #include <talogger.h>
       
    25 #include <telephonyvariant.hrh>
       
    26 #include <telinternalcrkeys.h>
       
    27 #include <centralrepository.h>
       
    28 
       
    29 #include "CPhCntMatcherImpl.h"
       
    30 #include "cphcntmatchcontact.h"
       
    31 #include "cphcntfetchcontact.h"
       
    32 #include "cphcntfoundcontacts.h"
       
    33 #include "CPhCntContact.h"
       
    34 #include "CPhCntContactManager.h"
       
    35 #include "CPhCntContactStores.h"
       
    36 #include "MPhoneCntPbkOwner.h"
       
    37 #include "cphcntvpbkcontactid.h"
       
    38 #include "cphcntcontactmatchstrategy.h"
       
    39 
       
    40 // Local functions
       
    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 
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // Static constructor
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CPhCntMatcherImpl* CPhCntMatcherImpl::NewL(
       
    68     const MPhoneCntPbkOwner& aOwner )
       
    69     {
       
    70     CPhCntMatcherImpl* self = new( ELeave )CPhCntMatcherImpl( aOwner );
       
    71     CleanupStack::PushL( self );
       
    72     self->ConstructL();
       
    73     CleanupStack::Pop( self );
       
    74     return self;
       
    75     }
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // Destructor
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 CPhCntMatcherImpl::~CPhCntMatcherImpl()
       
    82     {
       
    83     delete iCSMatchStrategy;
       
    84     delete iFoundContacts;
       
    85     delete iContactStores;
       
    86     delete iMatchContact;
       
    87     delete iFetchContact;
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // From CPhCntMatcher
       
    92 // Tries to find a contact which has aTelNumber.
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 TInt CPhCntMatcherImpl::MatchNumber(
       
    96     MPhCntMatch*& aMatch,
       
    97     const TDesC& aTelNumber )
       
    98     {
       
    99     TEFLOGSTRING( KTAOBJECT, "CNT CPhCntMatcherImpl::MatchNumber" );
       
   100     TInt err = CreateMatcher();
       
   101     if ( !err )
       
   102         {
       
   103         // Check if we already have the contact.
       
   104         CPhCntContact* contact = iFoundContacts->FindContact( aTelNumber );
       
   105 
       
   106         err = KErrNone;
       
   107         if( !contact )
       
   108             {
       
   109             // Get contact from contact stores
       
   110             TRAPD( traperr, err = GetContactL( aMatch, aTelNumber ));
       
   111             if ( traperr )
       
   112                 {
       
   113                 err = traperr;
       
   114                 }
       
   115             }
       
   116         else
       
   117             {
       
   118             aMatch = contact;
       
   119             }
       
   120         }
       
   121     TEFLOGSTRING2( KTAOBJECT, "CNT CPhCntMatcherImpl::MatchNumber %d " , err);
       
   122     return err;
       
   123     }
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 // From CPhCntMatcher
       
   127 // Tries to find a contact which has aTelNumber.
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 TInt CPhCntMatcherImpl::MatchNumber(
       
   131     MPhCntMatch*& aMatch,
       
   132     const TDesC& aTelNumber,
       
   133     const CPhCntContactId& aContactId )
       
   134     {
       
   135     TInt err = CreateMatcher();
       
   136     if ( !err )
       
   137         {
       
   138         if( aContactId.IsValid() )
       
   139             {
       
   140             // Do we have existing contact for the link and number.
       
   141             const CPhCntVPbkContactId& contactId =
       
   142                 static_cast<const CPhCntVPbkContactId&>( aContactId );
       
   143             const MVPbkContactLink& link = contactId.ContactLink();
       
   144             aMatch =
       
   145                 iFoundContacts->FindContact( aTelNumber, link );
       
   146             if( !aMatch )
       
   147                 {
       
   148                 // Get the contact.
       
   149                 CPhCntContact* match = NULL;
       
   150                 err = FetchContact( match, link, aTelNumber );
       
   151                 if( !err )
       
   152                     {
       
   153                     aMatch = match;
       
   154                     }
       
   155                 }
       
   156             }
       
   157         else
       
   158             {
       
   159             err = MatchNumber( aMatch, aTelNumber );
       
   160             }
       
   161         }
       
   162     return err;
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // From CPhCntMatcher
       
   167 // Empty implementation. Implemented in CPhCntMatcherVoIPImpl.
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 TInt CPhCntMatcherImpl::MatchVoipNumber(
       
   171     MPhCntMatch*& /*aMatch*/,
       
   172     const TDesC& /*aMatchString*/,
       
   173     TBool /*aAllowUserNameMatch*/,
       
   174     MDesCArray* /*aContactStoreUris*/,
       
   175     TInt /*aCharsForMatching*/ )
       
   176     {
       
   177     return KErrNotFound;
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // From CPhCntMatcher
       
   182 // Empty implementation. Implemented in CPhCntMatcherVoIPImpl.
       
   183 // ---------------------------------------------------------------------------
       
   184 //
       
   185 TInt CPhCntMatcherImpl::MatchVoipNumber(
       
   186     MPhCntMatch*& /*aMatch*/,
       
   187     const TDesC& /*aMatchString*/,
       
   188     const CPhCntContactId& /*aContactId*/ )
       
   189     {
       
   190     return KErrNotFound;
       
   191     }
       
   192 
       
   193 // ---------------------------------------------------------------------------
       
   194 // From CPhCntMatcher
       
   195 // Empty implementation. Implemented in CPhCntMatcherVoIPImpl.
       
   196 // ---------------------------------------------------------------------------
       
   197 //
       
   198 TBool CPhCntMatcherImpl::HasCSNumbers(
       
   199     const CPhCntContactId& /*aContactId*/ )
       
   200     {
       
   201     return EFalse;
       
   202     }
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // Gets contact from contact stores
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 TInt CPhCntMatcherImpl::GetContactL(
       
   209     MPhCntMatch*& aMatch,
       
   210     const TDesC& aTelNumber )
       
   211     {
       
   212     TEFLOGSTRING( KTAOBJECT, "CNT CPhCntMatcherImpl::GetContactL" );
       
   213     // Try to find matching contact.
       
   214     const MVPbkContactLinkArray* linkArray = NULL;
       
   215     TInt err = MatchContactL( linkArray, aTelNumber, 
       
   216         MPhCntContactManager::EDontRemoveDuplicates );
       
   217 
       
   218     CPhCntContact* match( NULL );
       
   219     TInt index( KErrNotFound );
       
   220 
       
   221     // Apply exact match on additional stores first.
       
   222     // If match is found, don't care about other stores as
       
   223     // these come first.
       
   224     if( !err )
       
   225         {        
       
   226         const CVPbkContactStoreUriArray& additionalStores = iContactManager.AdditionalContactStoreUris();
       
   227         index = iContactSelectionStrategy.ApplyAdditonalStoreStrategy( *linkArray, additionalStores );  
       
   228         
       
   229         const TBool manyContactsFound = index == KManyContacts;
       
   230         const TBool singleContactFound = 
       
   231             index != KNoContact && index != KManyContacts;
       
   232         
       
   233         if ( singleContactFound )
       
   234             {
       
   235             FetchContact( match, linkArray->At( index ), aTelNumber );
       
   236             aMatch = match;
       
   237             return err;
       
   238             }
       
   239         else if ( manyContactsFound ) 
       
   240             {
       
   241             aMatch = match;
       
   242             return KErrNotFound;
       
   243             }
       
   244         }
       
   245 
       
   246     if(  !err && ( linkArray->Count() > 1 ) )
       
   247         {
       
   248         err = MatchContactL( linkArray, aTelNumber,
       
   249             MPhCntContactManager::ERemoveDuplicates );
       
   250         }
       
   251     if ( !err )
       
   252         {
       
   253         index = iContactSelectionStrategy.ApplyStrategy( *linkArray );        
       
   254         }
       
   255 
       
   256     // Finally fetch contact details
       
   257     if ( index != KNoContact )
       
   258         {
       
   259         FetchContact( match, linkArray->At( index ), aTelNumber );
       
   260         aMatch = match;
       
   261         }
       
   262     else
       
   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;
       
   289     }
       
   290 
       
   291 
       
   292 // ---------------------------------------------------------------------------
       
   293 // Constructor
       
   294 // ---------------------------------------------------------------------------
       
   295 //
       
   296 CPhCntMatcherImpl::CPhCntMatcherImpl( const MPhoneCntPbkOwner& aOwner ) :
       
   297     iContactManager( *aOwner.ContactManager() ),
       
   298     iPbkOwner( aOwner )
       
   299     {
       
   300     }
       
   301 
       
   302 // ---------------------------------------------------------------------------
       
   303 // Delayed on-demand based construction
       
   304 // ---------------------------------------------------------------------------
       
   305 //
       
   306 void CPhCntMatcherImpl::DoCreateMatcherL()
       
   307     {
       
   308     TEFLOGSTRING( KTAOBJECT, "CNT CPhCntMatcherImpl::DoCreateMatcherL" );
       
   309     if ( !iContactStores )
       
   310         {
       
   311         iFoundContacts = CPhCntFoundContacts::NewL();
       
   312         iContactStores = CPhCntContactStores::NewL( iContactManager );
       
   313         iMatchContact = CPhCntMatchContact::NewL();
       
   314         iFetchContact = CPhCntFetchContact::NewL( *iContactStores );
       
   315         }
       
   316     }
       
   317 
       
   318 // ---------------------------------------------------------------------------
       
   319 // Delayed on-demand based construction
       
   320 // ---------------------------------------------------------------------------
       
   321 //
       
   322 TInt CPhCntMatcherImpl::CreateMatcher()
       
   323     {
       
   324     TRAPD( err, DoCreateMatcherL() );
       
   325     return err;
       
   326     }
       
   327 
       
   328 // ---------------------------------------------------------------------------
       
   329 // Second phase constructor
       
   330 // ---------------------------------------------------------------------------
       
   331 //
       
   332 void CPhCntMatcherImpl::ConstructL()
       
   333     {
       
   334     const TPhCntContactSelectionStrategy::TAllowSeveralMatches strategy = 
       
   335         GetContactSelectionStrategyL();
       
   336     iContactSelectionStrategy.SetContactSelectionStrategy( strategy );    
       
   337     }
       
   338 
       
   339 // ---------------------------------------------------------------------------
       
   340 // Fetches contact
       
   341 // ---------------------------------------------------------------------------
       
   342 //
       
   343 TInt CPhCntMatcherImpl::FetchContact(
       
   344     CPhCntContact*& aMatch,
       
   345     const MVPbkContactLink& aContactLink,
       
   346     const TDesC& aTelNumber )
       
   347     {
       
   348     TEFLOGSTRING( KTAOBJECT, "CNT CPhCntMatcherImpl::FetchContact" );
       
   349     CPhCntContact* contact = NULL;
       
   350     TInt err = iFetchContact->FetchContact( aContactLink,  contact );
       
   351     if( !err )
       
   352         {
       
   353         TRAP( err, iFoundContacts->AddL( contact, aTelNumber ) );
       
   354         if( err )
       
   355             {
       
   356             delete contact;
       
   357             }
       
   358         else
       
   359             {
       
   360             aMatch = contact;
       
   361             }
       
   362         }
       
   363     return err;
       
   364     }
       
   365