phoneengine/PhoneCntFinder/ContactService/src/CPhCntMatcherImpl.cpp
changeset 0 5f000ab63145
child 22 94dc1107e8b2
equal deleted inserted replaced
-1:000000000000 0:5f000ab63145
       
     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 
       
    19 
       
    20 #include <CVPbkPhoneNumberMatchStrategy.h>
       
    21 #include <MVPbkContactLinkArray.h>
       
    22 #include <CPbk2StoreConfiguration.h>
       
    23 #include <MVPbkContactStoreProperties.h>
       
    24 #include <MVPbkContactStore.h>
       
    25 #include <CVPbkContactStoreUriArray.h>
       
    26 
       
    27 
       
    28 #include "CPhCntMatcherImpl.h"
       
    29 #include "cphcntmatchcontact.h"
       
    30 #include "cphcntfetchcontact.h"
       
    31 #include "cphcntfoundcontacts.h"
       
    32 #include "CPhCntContact.h"
       
    33 #include "CPhCntContactManager.h"
       
    34 #include "CPhCntContactStores.h"
       
    35 #include "MPhoneCntPbkOwner.h"
       
    36 #include "cphcntvpbkcontactid.h"
       
    37 #include "cphcntcontactmatchstrategy.h"
       
    38 #include <talogger.h>
       
    39 
       
    40 
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // Static constructor
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CPhCntMatcherImpl* CPhCntMatcherImpl::NewL(
       
    47     const MPhoneCntPbkOwner& aOwner )
       
    48     {
       
    49     CPhCntMatcherImpl* self = new( ELeave )CPhCntMatcherImpl( aOwner );
       
    50     CleanupStack::PushL( self );
       
    51     self->ConstructL();
       
    52     CleanupStack::Pop( self );
       
    53     return self;
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // Destructor
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 CPhCntMatcherImpl::~CPhCntMatcherImpl()
       
    61     {
       
    62     delete iCSMatchStrategy;
       
    63     delete iFoundContacts;
       
    64     delete iContactStores;
       
    65     delete iMatchContact;
       
    66     delete iFetchContact;
       
    67     }
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // From CPhCntMatcher
       
    71 // Tries to find a contact which has aTelNumber.
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 TInt CPhCntMatcherImpl::MatchNumber(
       
    75     MPhCntMatch*& aMatch,
       
    76     const TDesC& aTelNumber )
       
    77     {
       
    78     TEFLOGSTRING( KTAOBJECT, "CNT CPhCntMatcherImpl::MatchNumber" );
       
    79     TInt err = CreateMatcher();
       
    80     if ( !err )
       
    81     	{
       
    82 	    // Check if we already have the contact.
       
    83 	    CPhCntContact* contact = iFoundContacts->FindContact( aTelNumber );
       
    84 
       
    85 	    err = KErrNone;
       
    86 	    if( !contact )
       
    87 	        {
       
    88 	        // Get contact from contact stores
       
    89 	        TRAPD( traperr, err = GetContactL( aMatch, aTelNumber ));
       
    90 	        if ( traperr )
       
    91 	            {
       
    92 	            err = traperr;
       
    93 	            }
       
    94 	        }
       
    95 	    else
       
    96 	        {
       
    97 	        aMatch = contact;
       
    98 	        }
       
    99     	}
       
   100     TEFLOGSTRING2( KTAOBJECT, "CNT CPhCntMatcherImpl::MatchNumber %d " , err);
       
   101     return err;
       
   102     }
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // From CPhCntMatcher
       
   106 // Tries to find a contact which has aTelNumber.
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 TInt CPhCntMatcherImpl::MatchNumber(
       
   110     MPhCntMatch*& aMatch,
       
   111     const TDesC& aTelNumber,
       
   112     const CPhCntContactId& aContactId )
       
   113     {
       
   114     TInt err = CreateMatcher();
       
   115     if ( !err )
       
   116 	    {
       
   117 	    if( aContactId.IsValid() )
       
   118 	        {
       
   119 	        // Do we have existing contact for the link and number.
       
   120 	        const CPhCntVPbkContactId& contactId =
       
   121 	            static_cast<const CPhCntVPbkContactId&>( aContactId );
       
   122 	        const MVPbkContactLink& link = contactId.ContactLink();
       
   123 	        aMatch =
       
   124 	            iFoundContacts->FindContact( aTelNumber, link );
       
   125 	        if( !aMatch )
       
   126 	            {
       
   127 	            // Get the contact.
       
   128 	            CPhCntContact* match = NULL;
       
   129 	            err = FetchContact( match, link, aTelNumber );
       
   130 	            if( !err )
       
   131 	                {
       
   132 	                aMatch = match;
       
   133 	                }
       
   134 	            }
       
   135 	        }
       
   136 	    else
       
   137 	        {
       
   138 	        err = MatchNumber( aMatch, aTelNumber );
       
   139 	        }
       
   140 	    }
       
   141     return err;
       
   142     }
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // From CPhCntMatcher
       
   146 // Empty implementation. Implemented in CPhCntMatcherVoIPImpl.
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 TInt CPhCntMatcherImpl::MatchVoipNumber(
       
   150     MPhCntMatch*& /*aMatch*/,
       
   151     const TDesC& /*aMatchString*/,
       
   152     TBool /*aAllowUserNameMatch*/,
       
   153     MDesCArray* /*aContactStoreUris*/,
       
   154     TInt /*aCharsForMatching*/ )
       
   155     {
       
   156     return KErrNotFound;
       
   157     }
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 // From CPhCntMatcher
       
   161 // Empty implementation. Implemented in CPhCntMatcherVoIPImpl.
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 TInt CPhCntMatcherImpl::MatchVoipNumber(
       
   165     MPhCntMatch*& /*aMatch*/,
       
   166     const CPhCntContactId& /*aContactId*/ )
       
   167     {
       
   168     return KErrNotFound;
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // From CPhCntMatcher
       
   173 // Empty implementation. Implemented in CPhCntMatcherVoIPImpl.
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 TBool CPhCntMatcherImpl::HasCSNumbers(
       
   177     const CPhCntContactId& /*aContactId*/ )
       
   178     {
       
   179     return EFalse;
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // Gets contact from contact stores
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 TInt CPhCntMatcherImpl::GetContactL(
       
   187     MPhCntMatch*& aMatch,
       
   188     const TDesC& aTelNumber )
       
   189     {
       
   190     TEFLOGSTRING( KTAOBJECT, "CNT CPhCntMatcherImpl::GetContactL" );
       
   191     // Try to find matching contact.
       
   192     const MVPbkContactLinkArray* linkArray = NULL;
       
   193     delete iCSMatchStrategy;
       
   194     iCSMatchStrategy = 0;
       
   195     iCSMatchStrategy = iContactManager.CreateContactMatchStrategyL( *iMatchContact,
       
   196         EFalse ); // don't remove duplicates
       
   197     TInt err( iMatchContact->MatchContact( linkArray, aTelNumber,
       
   198         *iCSMatchStrategy ) );
       
   199 
       
   200     CPhCntContact* match( NULL );
       
   201     TInt index( KErrNotFound );
       
   202 
       
   203     // Apply exact match on additional stores first.
       
   204     // If match is found, don't care about other stores as
       
   205     // these come first.
       
   206     if(  !err && ( linkArray->Count() > 1 ) )
       
   207         {
       
   208         index = MatchFromAdditionalStore( *linkArray );
       
   209 
       
   210         // Single match on additonal stores
       
   211         if ( index != KErrNotFound && index != KErrAlreadyExists )
       
   212             {
       
   213             FetchContact( match, linkArray->At( index ), aTelNumber );
       
   214             aMatch = match;
       
   215             return err;
       
   216             }
       
   217         else if ( index == KErrAlreadyExists ) //Several matches on additional stores
       
   218             {
       
   219             aMatch = match;
       
   220             return KErrNotFound;
       
   221             }
       
   222         }
       
   223 
       
   224     if(  !err && ( linkArray->Count() > 1 ) )
       
   225         {
       
   226         delete iCSMatchStrategy;
       
   227         iCSMatchStrategy = 0;
       
   228         iCSMatchStrategy = iContactManager.CreateContactMatchStrategyL(
       
   229             *iMatchContact, ETrue ); //remove duplicates
       
   230         err = iMatchContact->MatchContact( linkArray, aTelNumber,
       
   231             *iCSMatchStrategy );
       
   232         }
       
   233     if ( !err )
       
   234         {
       
   235         // If only one contact, no additional checks needed
       
   236         if ( linkArray->Count() == 1 )
       
   237             {
       
   238             index = 0;
       
   239             }
       
   240         }
       
   241 
       
   242 
       
   243     // Finally fetch contact details
       
   244     if ( index != KErrNotFound )
       
   245         {
       
   246         FetchContact( match, linkArray->At( index ), aTelNumber );
       
   247         }
       
   248 
       
   249     aMatch = match;
       
   250     return err;
       
   251     }
       
   252 
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // Constructor
       
   256 // ---------------------------------------------------------------------------
       
   257 //
       
   258 CPhCntMatcherImpl::CPhCntMatcherImpl( const MPhoneCntPbkOwner& aOwner ) :
       
   259     iContactManager( *aOwner.ContactManager() ),
       
   260     iPbkOwner( aOwner )
       
   261     {
       
   262     }
       
   263 
       
   264 // ---------------------------------------------------------------------------
       
   265 // Delayed on-demand based construction
       
   266 // ---------------------------------------------------------------------------
       
   267 //
       
   268 void CPhCntMatcherImpl::DoCreateMatcherL()
       
   269 	{
       
   270 	TEFLOGSTRING( KTAOBJECT, "CNT CPhCntMatcherImpl::DoCreateMatcherL" );
       
   271 	if ( !iContactStores )
       
   272 		{
       
   273 		iFoundContacts = CPhCntFoundContacts::NewL();
       
   274 	    iContactStores = CPhCntContactStores::NewL( iContactManager );
       
   275 	    iMatchContact = CPhCntMatchContact::NewL();
       
   276 	    iFetchContact = CPhCntFetchContact::NewL( *iContactStores );
       
   277 		}
       
   278 	}
       
   279 
       
   280 // ---------------------------------------------------------------------------
       
   281 // Delayed on-demand based construction
       
   282 // ---------------------------------------------------------------------------
       
   283 //
       
   284 TInt CPhCntMatcherImpl::CreateMatcher()
       
   285 	{
       
   286 	TRAPD( err, DoCreateMatcherL() );
       
   287 	return err;
       
   288 	}
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // Second phase constructor
       
   292 // ---------------------------------------------------------------------------
       
   293 //
       
   294 void CPhCntMatcherImpl::ConstructL()
       
   295     {
       
   296     }
       
   297 
       
   298 // ---------------------------------------------------------------------------
       
   299 // Fetches contact
       
   300 // ---------------------------------------------------------------------------
       
   301 //
       
   302 TInt CPhCntMatcherImpl::FetchContact(
       
   303     CPhCntContact*& aMatch,
       
   304     const MVPbkContactLink& aContactLink,
       
   305     const TDesC& aTelNumber )
       
   306     {
       
   307     TEFLOGSTRING( KTAOBJECT, "CNT CPhCntMatcherImpl::FetchContact" );
       
   308     CPhCntContact* contact = NULL;
       
   309     TInt err = iFetchContact->FetchContact( aContactLink,  contact );
       
   310     if( !err )
       
   311         {
       
   312         TRAP( err, iFoundContacts->AddL( contact, aTelNumber ) );
       
   313         if( err )
       
   314             {
       
   315             delete contact;
       
   316             }
       
   317         else
       
   318             {
       
   319             aMatch = contact;
       
   320             }
       
   321         }
       
   322     return err;
       
   323     }
       
   324 
       
   325 // --------------------------------------------------------------------------
       
   326 // CPhCntMatcherImpl::MatchFromAdditionalStore
       
   327 // --------------------------------------------------------------------------
       
   328 //
       
   329 TInt CPhCntMatcherImpl::MatchFromAdditionalStore(
       
   330     const MVPbkContactLinkArray& linkArray ) const
       
   331     {
       
   332     TEFLOGSTRING( KTAOBJECT, "CNT CPhCntMatcherImpl::MatchFromAdditionalStore" );
       
   333     TInt ret( KErrNotFound );
       
   334     TInt found(0); // count of found contacts from additional stores.
       
   335 
       
   336     for ( TInt i = 0; i < linkArray.Count(); i++ )
       
   337         {
       
   338         TVPbkContactStoreUriPtr uri =
       
   339             linkArray.At( i ).ContactStore().StoreProperties().Uri();
       
   340 
       
   341         // Compare if contact is from additional store.
       
   342         if ( iContactManager.AdditionalContactStoreUris().IsIncluded( uri ) )
       
   343             {
       
   344             // Contact found from additional store.
       
   345             found++;
       
   346             ret = i;
       
   347             }
       
   348         }
       
   349 
       
   350     if ( found > 1)
       
   351         {
       
   352         // Multiple matches from additional stores -> no match.
       
   353         ret = KErrAlreadyExists;
       
   354         }
       
   355 
       
   356     return ret;
       
   357     }
       
   358