phoneengine/PhoneCntFinder/ContactService/src/cphcntmatchervoipimpl.cpp
branchRCL_3
changeset 57 94dc1107e8b2
parent 19 544e34b3255a
equal deleted inserted replaced
44:3c221667e687 57:94dc1107e8b2
    76     {    
    76     {    
    77     TPhCntSipURI sipUri( aMatchString, aCharsForMatching );
    77     TPhCntSipURI sipUri( aMatchString, aCharsForMatching );
    78     TInt err = CreateMatcher();    
    78     TInt err = CreateMatcher();    
    79 
    79 
    80     if ( err == KErrNone )
    80     if ( err == KErrNone )
    81     {
    81         {
    82     CPhCntContact* existingContact = 
    82         CPhCntContact* existingContact = 
    83         iFoundContacts->FindContact( sipUri.SipURI() );
    83             iFoundContacts->FindContact( sipUri.SipURI() );
    84     if( !existingContact )
    84         if( !existingContact )
    85         {
    85             {
    86         // Check for service specific contact stores and
    86             // Check for service specific contact stores and
    87         // open stores if not open
    87             // open stores if not open
    88         if ( aContactStoreUris )
    88             if ( aContactStoreUris )
    89             {
    89                 {
    90             TInt storeCount = aContactStoreUris->MdcaCount();
    90                 TInt storeCount = aContactStoreUris->MdcaCount();
    91             for( TInt i = 0; i < storeCount ; i++ )
    91                 for( TInt i = 0; i < storeCount ; i++ )
    92                 {
    92                     {
    93                 TPtrC storeUri = aContactStoreUris->MdcaPoint( i );
    93                     TPtrC storeUri = aContactStoreUris->MdcaPoint( i );
    94                
    94                    
    95                 err = iContactStoreLoader->LoadContactStoreWithUri( storeUri );              
    95                     err = iContactStoreLoader->LoadContactStoreWithUri( storeUri );              
    96                 }  
    96                     }  
    97             }
    97                 }
    98 
    98     
    99         const MVPbkContactLinkArray* linkArray = NULL;
    99             const MVPbkContactLinkArray* linkArray = NULL;
   100         
   100             
   101         // Find possible contacts.
   101             // Find possible contacts.
   102         if( aAllowUserNameMatch && !aCharsForMatching )
   102             if( aAllowUserNameMatch && !aCharsForMatching )
   103             {
   103                 {
   104             err = iMatchContact->MatchContact(
   104                 err = iMatchContact->MatchContact(
   105                 linkArray, sipUri.UserNamePart(), *iVoipMatchStrategy );
   105                     linkArray, sipUri.UserNamePart(), *iVoipMatchStrategy );
   106             }
   106                 }
   107         else if ( aCharsForMatching )
   107             else if ( aCharsForMatching )
   108             {
   108                 {
   109             err = iMatchContact->MatchContact(
   109                 err = iMatchContact->MatchContact(
   110                 linkArray, sipUri.FixedUserNamePart(), *iVoipMatchStrategy );            
   110                     linkArray, sipUri.FixedUserNamePart(), *iVoipMatchStrategy );            
       
   111                 }
       
   112             else
       
   113                 {
       
   114                 err = iMatchContact->MatchContact( 
       
   115                     linkArray, sipUri.SipURI() , *iVoipMatchStrategy );    
       
   116                 }
       
   117             
       
   118             MPhCntMatch* match = NULL;
       
   119             if( !err )
       
   120                 {
       
   121                 // Fetch all the matched contacts, because iVoipMatchStrategy
       
   122                 // could give us a match that is matched to wrong contact field.
       
   123                 const TInt matchedContacts( linkArray->Count() );
       
   124     
       
   125                 // Find real matches.
       
   126                 TPhCntVoipMatchArray voipMatches;
       
   127                 for( TInt i = 0; i < matchedContacts; i++ )
       
   128                     {
       
   129                     // Get contacts from phone book.
       
   130                     CPhCntContact* match = NULL;
       
   131                     err = FetchContact( match, linkArray->At( i ), sipUri.SipURI() );
       
   132                     if( !err )
       
   133                         {
       
   134                         TRAP_IGNORE( voipMatches.AppendL( match ) );
       
   135                         }
       
   136                     else
       
   137                         {
       
   138                         // Error in fetching contacts 
       
   139                         break;
       
   140                         }
       
   141                     }
       
   142                     
       
   143                 if( aAllowUserNameMatch || aCharsForMatching )   
       
   144                     {
       
   145                     match = voipMatches.FindFullOrUsernameMatch( sipUri, aCharsForMatching );
       
   146                     }
       
   147                 else
       
   148                     {
       
   149                     // Take the first match, that is voip contact. Gives NULL if match not found.
       
   150                     match = voipMatches.FindFullMatch( sipUri );
       
   151                     }
       
   152                 
       
   153                 // Release extra matches
       
   154                 voipMatches.ReleaseMatches();
       
   155                 }
       
   156             
       
   157             // If no error and match still null pointer, then no contacts were found.
       
   158             if( !err && !match )        
       
   159                 {
       
   160                 err = KErrNotFound;
       
   161                 }
       
   162             else
       
   163                 {
       
   164                 aMatch = match;
       
   165                 }
   111             }
   166             }
   112         else
   167         else
   113             {
   168             {
   114             err = iMatchContact->MatchContact( 
   169             aMatch = existingContact;
   115                 linkArray, sipUri.SipURI() , *iVoipMatchStrategy );    
   170             }
   116             }
   171         }
   117         
       
   118             
       
   119         MPhCntMatch* match = NULL;
       
   120         if( !err )
       
   121             {
       
   122             // Fetch all the matched contacts, because iVoipMatchStrategy
       
   123             // could give us a match that is matched to wrong contact field.
       
   124             const TInt matchedContacts( linkArray->Count() );
       
   125 
       
   126             // Find real matches.
       
   127             TPhCntVoipMatchArray voipMatches;
       
   128             for( TInt i = 0; i < matchedContacts; i++ )
       
   129                 {
       
   130                 // Get contacts from phone book.
       
   131                 CPhCntContact* match = NULL;
       
   132                 err = FetchContact( match, linkArray->At( i ), sipUri.SipURI() );
       
   133                 if( !err )
       
   134                     {
       
   135                     TRAP_IGNORE( voipMatches.AppendL( match ) );
       
   136                     }
       
   137                 else
       
   138                     {
       
   139                     // Error in fetching contacts 
       
   140                     break;
       
   141                     }
       
   142                 }
       
   143                 
       
   144             if( aAllowUserNameMatch || aCharsForMatching )   
       
   145                 {
       
   146                 match = voipMatches.FindFullOrUsernameMatch( sipUri, aCharsForMatching );
       
   147                 }
       
   148             else
       
   149                 {
       
   150                 // Take the first match, that is voip contact. Gives NULL if match not found.
       
   151                 match = voipMatches.FindFullMatch( sipUri );
       
   152                 }
       
   153             
       
   154             // Release extra matches
       
   155             voipMatches.ReleaseMatches();
       
   156             }
       
   157         
       
   158         // If no error and match still null pointer, then no contacts were found.
       
   159         if( !err && !match )        
       
   160             {
       
   161             err = KErrNotFound;
       
   162             }
       
   163         else
       
   164             {
       
   165             aMatch = match;
       
   166             }
       
   167         }
       
   168     else
       
   169         {
       
   170         aMatch = existingContact;
       
   171         }
       
   172     }
       
   173 
   172 
   174     return err;
   173     return err;
   175     }
   174     }
   176     
   175 
   177 // ---------------------------------------------------------------------------
   176 // ---------------------------------------------------------------------------
   178 // From class CPhCntMatcher
   177 // From class CPhCntMatcher
   179 // Matches voip contacts
   178 // Matches voip contacts
   180 // ---------------------------------------------------------------------------
   179 // ---------------------------------------------------------------------------
   181 TInt CPhCntMatcherVoIPImpl::MatchVoipNumber(
   180 TInt CPhCntMatcherVoIPImpl::MatchVoipNumber(
   182             MPhCntMatch*& aMatch,
   181     MPhCntMatch*& aMatch,
   183             const CPhCntContactId& aContactId )
   182     const TDesC& aMatchString,
       
   183     const CPhCntContactId& aContactId )
   184     {
   184     {
   185     const CPhCntVPbkContactId& contactId = 
   185     const CPhCntVPbkContactId& contactId = 
   186             static_cast<const CPhCntVPbkContactId&>( aContactId );
   186             static_cast<const CPhCntVPbkContactId&>( aContactId );
   187             
   187 
   188     const MVPbkContactLink& contactLink = contactId.ContactLink();
   188     const MVPbkContactLink& contactLink = contactId.ContactLink();
   189     
   189     
   190     
   190     TInt err = CreateMatcher();
   191     TInt err = CreateMatcher();    
       
   192 
   191 
   193     const TVPbkContactStoreUriPtr uri 
   192     const TVPbkContactStoreUriPtr uri 
   194         = contactLink.ContactStore().StoreProperties().Uri(); 
   193         = contactLink.ContactStore().StoreProperties().Uri(); 
   195 
   194 
   196     // Open contact store if not opened earlier
   195     // Open contact store if not opened earlier
   197     iContactStoreLoader->LoadContactStoreWithUri( uri.UriDes() );
   196     iContactStoreLoader->LoadContactStoreWithUri( uri.UriDes() );
   198     
   197     
   199     if ( err == KErrNone )
   198     if ( err == KErrNone )
   200     {
   199         {
   201     CPhCntContact* existingContact = 
   200         CPhCntContact* existingContact = 
   202         iFoundContacts->FindContact( contactLink );
   201             iFoundContacts->FindContact( contactLink );
   203      
   202         
   204     
   203         if ( !existingContact )
   205     if( !existingContact )
   204             {
   206         {
   205             CPhCntContact* contact = NULL;
   207         CPhCntContact* contact = NULL;
   206             err = iFetchContact->FetchContact( contactLink,  contact );
   208         err = iFetchContact->FetchContact( contactLink,  contact );
   207     
   209 
   208             if ( !err )
   210         if( !err )
   209                 {
   211             {
   210                 SetMatchedVoIPNumberIfExists( *contact, aMatchString );
   212             const RArray<TPhCntNumber>& allNumbers = 
   211 
   213                 contact->AllNumbers();
   212                 TPhCntSipURI sipUri( contact->Number() );
   214             const TInt count( allNumbers.Count() );
   213                 TRAP( err, iFoundContacts->AddL( contact, sipUri.SipURI() ) );
   215             for( TInt i = 0; i < count; i++ )
   214                 if ( err )
   216                 {
   215                     {
   217                 // Take first voip number and set it as contacts number.
   216                     delete contact;
   218                 TPhCntNumber number = allNumbers[i];
   217                     }
   219                 if( number.Type() == MPhCntMatch::EVoipNumber ||
   218                 else
   220                     number.Type() == MPhCntMatch::EMobileNumber ||
   219                     {
   221                     number.Type() == MPhCntMatch::EStandardNumber ||
   220                     aMatch = contact;
   222                     number.Type() == CPhCntContact::EPagerNumber ||
   221                     }
   223                     number.Type() == CPhCntContact::EVideoNumber ||
   222                 }
   224                     number.Type() == CPhCntContact::EAssistantNumber ||
   223             }
   225                     number.Type() == CPhCntContact::EFaxNumber ||
   224         else
   226                     number.Type() == CPhCntContact::ECarNumber ) 
   225             {
   227                     {
   226             aMatch = existingContact;
   228                     contact->SetMatchedVoipNumber( TPhCntSipURI( number.Number() ) );
   227             }
   229                     break;
   228         }
   230                     }
       
   231                 }
       
   232             TPhCntSipURI sipUri( contact->Number() );
       
   233             TRAP( err, iFoundContacts->AddL( contact, sipUri.SipURI() ) );
       
   234             if( err )
       
   235                 {
       
   236                 delete contact;
       
   237                 }
       
   238             else
       
   239                 {
       
   240                 aMatch = contact;
       
   241                 }
       
   242          
       
   243             }
       
   244         }
       
   245     else
       
   246         {
       
   247         aMatch = existingContact;
       
   248         }
       
   249     }
       
   250     return err;
   229     return err;
   251     }
   230     }
   252 
   231 
       
   232 
       
   233 // ---------------------------------------------------------------------------
       
   234 // Sets matched VoIP number if it was found.
       
   235 // ---------------------------------------------------------------------------
       
   236 //    
       
   237 void CPhCntMatcherVoIPImpl::SetMatchedVoIPNumberIfExists( 
       
   238     CPhCntContact& aContact,
       
   239     const TDesC& aMatchString )
       
   240     {
       
   241     const RArray<TPhCntNumber>& allNumbers = aContact.AllNumbers();
       
   242     const TInt numberCount( allNumbers.Count() );
       
   243     TPhCntSipURI sipUri( aMatchString );
       
   244     
       
   245     for ( TInt i = 0; i < numberCount; i++ ) 
       
   246         {
       
   247         TPhCntNumber number = allNumbers[i];
       
   248         if ( number.Type() == CPhCntContact::EVoipNumber ||
       
   249              number.Type() == CPhCntContact::EMobileNumber || 
       
   250              number.Type() == CPhCntContact::EStandardNumber ||
       
   251              number.Type() == CPhCntContact::EPagerNumber ||
       
   252              number.Type() == CPhCntContact::EVideoNumber ||
       
   253              number.Type() == CPhCntContact::EAssistantNumber ||
       
   254              number.Type() == CPhCntContact::EFaxNumber ||
       
   255              number.Type() == CPhCntContact::ECarNumber ) 
       
   256             {
       
   257             // If uris are the same then we have a full match.
       
   258             // (usernamepart requires case sensitive match, domain is not sensitive)
       
   259             TPhCntSipURI matchURI( number.Number() );
       
   260             if ( matchURI.SipURI().CompareF( sipUri.SipURI() ) == KErrNone &&
       
   261                  matchURI.UserNamePart().Compare( sipUri.UserNamePart() ) == KErrNone )
       
   262                 {
       
   263                 aContact.SetMatchedVoipNumber( matchURI );
       
   264                 break;
       
   265                 }
       
   266             }
       
   267         }
       
   268     }
   253 
   269 
   254 // ---------------------------------------------------------------------------
   270 // ---------------------------------------------------------------------------
   255 // From class CPhCntMatcher
   271 // From class CPhCntMatcher
   256 // Determines if contact has CS numbers.
   272 // Determines if contact has CS numbers.
   257 // ---------------------------------------------------------------------------
   273 // ---------------------------------------------------------------------------
   263     const CPhCntVPbkContactId& contactId = 
   279     const CPhCntVPbkContactId& contactId = 
   264             static_cast<const CPhCntVPbkContactId&>( aContactId );
   280             static_cast<const CPhCntVPbkContactId&>( aContactId );
   265     
   281     
   266     if ( CreateMatcher() == KErrNone )
   282     if ( CreateMatcher() == KErrNone )
   267         {
   283         {
   268             
   284         // Check if we have contact already.
   269     // Check if we have contact already.
   285         CPhCntContact* contact = 
   270     CPhCntContact* contact = 
   286             iFoundContacts->FindContact( contactId.ContactLink() );
   271         iFoundContacts->FindContact( contactId.ContactLink() );
   287             
   272         
   288         if ( contact ) 
   273     if( contact ) 
   289             {
   274         {
   290             hasCSNumbers = HasCSNumbers( contact );
   275         hasCSNumbers = HasCSNumbers( contact );
   291             contact->Release();
   276         contact->Release();
   292             }
   277         }
   293         else
   278     else
   294             {
   279         {
   295             // Fetch the contact from Virtual phonebook.
   280         // Fetch the contact from Virtual phonebook.
   296             const TInt err = 
   281         const TInt err = 
   297                 iFetchContact->FetchContact( contactId.ContactLink(), contact );
   282             iFetchContact->FetchContact( contactId.ContactLink(), contact );
   298             hasCSNumbers = HasCSNumbers( contact );
   283         hasCSNumbers = HasCSNumbers( contact );
   299             
   284         
   300             // We can delete the contact, because it is not added to 
   285         // We can delete the contact, because it is not added to 
   301             // iFoundContacts.
   286         // iFoundContacts.
   302             delete contact;
   287         delete contact;
   303             }
   288         }
       
   289     
       
   290         }
   304         }
   291 
   305 
   292     return hasCSNumbers;
   306     return hasCSNumbers;
   293     }
   307     }
   294 
   308 
   321 TBool CPhCntMatcherVoIPImpl::HasCSNumbers( 
   335 TBool CPhCntMatcherVoIPImpl::HasCSNumbers( 
   322     const CPhCntContact* const aContact )
   336     const CPhCntContact* const aContact )
   323     {
   337     {
   324     
   338     
   325     TBool hasCSNumbers = EFalse;
   339     TBool hasCSNumbers = EFalse;
   326     if( aContact )
   340     if ( aContact )
   327         {
   341         {
   328         const RArray<TPhCntNumber>& allNumbers = aContact->AllNumbers();
   342         const RArray<TPhCntNumber>& allNumbers = aContact->AllNumbers();
   329         const TInt count( allNumbers.Count() );
   343         const TInt count( allNumbers.Count() );
   330         for( TInt i = 0; i < count; i++ )
   344         for( TInt i = 0; i < count; i++ )
   331             {
   345             {
   332             const MPhCntMatch::TNumberType type = allNumbers[i].Type();
   346             const MPhCntMatch::TNumberType type = allNumbers[i].Type();
   333             if( type != MPhCntMatch::ENone &&
   347             if ( type != MPhCntMatch::ENone &&
   334                 type != MPhCntMatch::EFaxNumber &&
   348                  type != MPhCntMatch::EFaxNumber &&
   335                 type != MPhCntMatch::EPagerNumber &&
   349                  type != MPhCntMatch::EPagerNumber &&
   336                 type != MPhCntMatch::EVoipNumber 
   350                  type != MPhCntMatch::EVoipNumber 
   337                 )
   351                 )
   338                 {
   352                 {
   339                 hasCSNumbers = ETrue;
   353                 hasCSNumbers = ETrue;
   340                 }
   354                 }
   341             }
   355             }
   342         }
   356         }
   343     return hasCSNumbers;
   357     return hasCSNumbers;
   344     }
   358     }
   345 
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // Creates the contact matcher.
       
   362 // ---------------------------------------------------------------------------
       
   363 //  
   346 TInt CPhCntMatcherVoIPImpl::CreateMatcher()
   364 TInt CPhCntMatcherVoIPImpl::CreateMatcher()
   347     {
   365     {
   348     TInt err = CPhCntMatcherImpl::CreateMatcher();
   366     TInt err = CPhCntMatcherImpl::CreateMatcher();
   349     if (!err && !iVoipMatchStrategy )
   367     if ( !err && !iVoipMatchStrategy )
   350         {
   368         {
   351         TRAP( err, iVoipMatchStrategy = 
   369         TRAP( err, iVoipMatchStrategy = 
   352             CPhCntVoipContactMatchStrategy::NewL( iContactManager, 
   370             CPhCntVoipContactMatchStrategy::NewL( iContactManager, 
   353             *iMatchContact ) );
   371             *iMatchContact ) );
   354         }
   372         }