phonebookengines/VirtualPhonebook/VPbkEng/src/CVPbkPhoneNumberMatchStrategy.cpp
branchRCL_3
changeset 45 34879f5cfc63
parent 35 4ae315f230bc
child 58 d4f567ce2e7c
equal deleted inserted replaced
39:a6539d1e8e43 45:34879f5cfc63
    35 #include <CVPbkFieldTypeSelector.h>
    35 #include <CVPbkFieldTypeSelector.h>
    36 #include <CVPbkFieldFilter.h>
    36 #include <CVPbkFieldFilter.h>
    37 #include <barsread.h>
    37 #include <barsread.h>
    38 #include <MVPbkContactFieldTextData.h>
    38 #include <MVPbkContactFieldTextData.h>
    39 #include <CVPbkContactStoreUriArray.h>
    39 #include <CVPbkContactStoreUriArray.h>
       
    40 #include <centralrepository.h>
       
    41 #include <VPbkStoreUriLiterals.h>
    40 
    42 
    41 #include "CVPbkPhoneNumberSequentialMatchStrategy.h"
    43 #include "CVPbkPhoneNumberSequentialMatchStrategy.h"
    42 #include "CVPbkPhoneNumberParallelMatchStrategy.h"
    44 #include "CVPbkPhoneNumberParallelMatchStrategy.h"
    43 
    45 
    44 // CONSTANTS
    46 // CONSTANTS
       
    47 // Unnamed namespace for local definitions
       
    48 namespace {
       
    49 // --------------------------------------------------------------------------
       
    50 // Phonebook Central Repository UIDs
       
    51 // Copied from sf\app\contacts\phonebookui\Phonebook2\inc\Phonebook2InternalCRKeys.h
       
    52 // --------------------------------------------------------------------------
       
    53 //
       
    54 const TUint32 KCRUidPhonebookStoreConfiguration             = 0x1020727f;
       
    55 const TUint32 KPhonebookCurrentConfigurationPartialKey      = 0x00000100;
       
    56 const TUint32 KPhonebookCurrentConfigurationMask            = 0xffffff00;
       
    57 
       
    58 const TInt KInitialStoreUriSize = 22; // length of KVPbkDefaultCntDbURI
    45 const TInt KMagicNumber = -1;
    59 const TInt KMagicNumber = -1;
       
    60 } // namespace
    46 
    61 
    47 NONSHARABLE_CLASS(CVPbkPhoneNumberMatchStrategyImpl) :
    62 NONSHARABLE_CLASS(CVPbkPhoneNumberMatchStrategyImpl) :
    48         public CActive,
    63         public CActive,
    49         public MVPbkContactFindObserver,
    64         public MVPbkContactFindObserver,
    50         public MVPbkSingleContactOperationObserver
    65         public MVPbkSingleContactOperationObserver
    59 
    74 
    60     public: // Interface
    75     public: // Interface
    61         void MatchL(const TDesC& aPhoneNumber);
    76         void MatchL(const TDesC& aPhoneNumber);
    62         TInt MaxMatchDigits() const;
    77         TInt MaxMatchDigits() const;
    63         TArray<MVPbkContactStore*> StoresToMatch() const;
    78         TArray<MVPbkContactStore*> StoresToMatch() const;
    64 
    79         TBool IsSimStore( const MVPbkContactStore& aStore );
       
    80         
    65     private: // From CActive
    81     private: // From CActive
    66         void RunL();
    82         void RunL();
    67         void DoCancel();
    83         void DoCancel();
    68         TInt RunError(TInt aError);
    84         TInt RunError(TInt aError);
    69 
    85 
   130         
   146         
   131         TBool ValidateBestMatchingRulesL( const TDesC& aNumber );
   147         TBool ValidateBestMatchingRulesL( const TDesC& aNumber );
   132         TBool CheckBestMatchingRules( const TDesC& aNumberA, TNumberType aNumberAType,
   148         TBool CheckBestMatchingRules( const TDesC& aNumberA, TNumberType aNumberAType,
   133                 const TDesC& aNumberB, TNumberType aNumberBType  );
   149                 const TDesC& aNumberB, TNumberType aNumberBType  );
   134         TInt FormatAndCheckNumberType( TDes& aNumber );
   150         TInt FormatAndCheckNumberType( TDes& aNumber );
       
   151         
       
   152         /**
       
   153          * Reads current store configuration from central repositiry
       
   154          * @return Array of stores or NULL if error during reading form cenrep.
       
   155         */
       
   156         CVPbkContactStoreUriArray* GetCurrentStoreConfigurationL();
       
   157         
       
   158         /**
       
   159          * If there is in the results at least one contact
       
   160          * from currently used stores in Phonebook2 it removes from results 
       
   161          * contacts from other stores
       
   162         */
       
   163         void RefineDuplicatedNumbersL();
   135         
   164         
   136     private: // Data
   165     private: // Data
   137         CVPbkPhoneNumberMatchStrategy& iParent;
   166         CVPbkPhoneNumberMatchStrategy& iParent;
   138         /// Ref: The contact manager instance to be used for searching.
   167         /// Ref: The contact manager instance to be used for searching.
   139         CVPbkContactManager* iContactManager;
   168         CVPbkContactManager* iContactManager;
   408                 results->AppendL( link );
   437                 results->AppendL( link );
   409                 CleanupStack::Pop( 2, results ); // results, link
   438                 CleanupStack::Pop( 2, results ); // results, link
   410                 delete iResults;
   439                 delete iResults;
   411                 iResults = results;
   440                 iResults = results;
   412                 }
   441                 }
       
   442 
       
   443             if ( iResults->Count() > 1 )
       
   444                 {
       
   445                 RefineDuplicatedNumbersL();
       
   446                 }
       
   447 
   413             CVPbkContactLinkArray* results = iResults;
   448             CVPbkContactLinkArray* results = iResults;
   414             iResults = NULL;            
   449             iResults = NULL;            
   415             iObserver->FindCompleteL( results );
   450             iObserver->FindCompleteL( results );
   416             break;
   451             break;
   417             }
   452             }
   943 	// Keep rules updated in the document Best_Number_Matching_Algorithm_Description.doc
   978 	// Keep rules updated in the document Best_Number_Matching_Algorithm_Description.doc
   944 
   979 
   945     return result;
   980     return result;
   946     }
   981     }
   947 
   982 
       
   983 CVPbkContactStoreUriArray* CVPbkPhoneNumberMatchStrategyImpl::GetCurrentStoreConfigurationL()
       
   984     {
       
   985     CRepository* repository = CRepository::NewL( TUid::Uid( KCRUidPhonebookStoreConfiguration ) );
       
   986     CleanupStack::PushL( repository );
       
   987     CVPbkContactStoreUriArray* result = CVPbkContactStoreUriArray::NewLC();
       
   988 
       
   989     RArray<TUint32> configurationKeys;
       
   990     CleanupClosePushL( configurationKeys );
       
   991 
       
   992     repository->FindL( KPhonebookCurrentConfigurationPartialKey,
       
   993             KPhonebookCurrentConfigurationMask, configurationKeys );
       
   994 
       
   995     HBufC* buffer = HBufC::NewLC( KInitialStoreUriSize );
       
   996     const TInt keyCount = configurationKeys.Count();
       
   997     TInt ret = KErrNone;
       
   998     for ( TInt i = 0; i < keyCount; ++i )
       
   999         {
       
  1000         TPtr ptr = buffer->Des();
       
  1001         ptr.Zero();
       
  1002         TInt actualSize = 0;
       
  1003         ret = repository->Get( configurationKeys[i], ptr, actualSize );
       
  1004         if ( ret == KErrOverflow )
       
  1005             {
       
  1006             CleanupStack::PopAndDestroy(); // buffer
       
  1007             buffer = HBufC::NewLC( actualSize );
       
  1008             ptr.Set( buffer->Des() );
       
  1009             ret = repository->Get( configurationKeys[i], ptr );
       
  1010             }
       
  1011         
       
  1012         if ( ret != KErrNone )
       
  1013             {
       
  1014             break;
       
  1015             }
       
  1016         
       
  1017         if( !result->IsIncluded( TVPbkContactStoreUriPtr( ptr ) ) )  // Only append if the uri is not yet included
       
  1018             {
       
  1019             result->AppendL( ptr );
       
  1020             }
       
  1021         }
       
  1022     CleanupStack::PopAndDestroy( buffer );
       
  1023     CleanupStack::PopAndDestroy( &configurationKeys );
       
  1024     CleanupStack::Pop( result );
       
  1025     CleanupStack::PopAndDestroy( repository );
       
  1026     
       
  1027     if ( ret != KErrNone )
       
  1028         {
       
  1029         delete result;
       
  1030         result = NULL;
       
  1031         }
       
  1032     return result;
       
  1033     }
       
  1034 
       
  1035 void CVPbkPhoneNumberMatchStrategyImpl::RefineDuplicatedNumbersL()
       
  1036     {
       
  1037     CVPbkContactStoreUriArray* stores = GetCurrentStoreConfigurationL();
       
  1038     if ( !stores )
       
  1039         {
       
  1040         return;
       
  1041         }
       
  1042     CleanupStack::PushL( stores );
       
  1043     
       
  1044     TInt storesCount = stores->Count();
       
  1045     if ( storesCount )
       
  1046         {
       
  1047         TInt linksCount = iResults->Count();
       
  1048         // check if there is in the results at least one contact
       
  1049         // from currently used stores
       
  1050         TBool isFromUsedStore = EFalse;
       
  1051         for ( TInt i = 0; i < linksCount; i++ )
       
  1052             {
       
  1053             const MVPbkContactStoreProperties& linkStoreProp = 
       
  1054                 iResults->At( i ).ContactStore().StoreProperties();
       
  1055 
       
  1056             for ( TInt j = 0; j < storesCount; j++ )
       
  1057                 {
       
  1058                 if ( !linkStoreProp.Uri().UriDes().Compare( ( *stores )[j].UriDes() ) )
       
  1059                     {
       
  1060                     isFromUsedStore = ETrue;
       
  1061                     break;
       
  1062                     }
       
  1063                 }
       
  1064             if ( isFromUsedStore )
       
  1065                 {
       
  1066                 break;
       
  1067                 }
       
  1068             }
       
  1069         // remove from results contacts from not used stores
       
  1070         if ( isFromUsedStore )
       
  1071             {
       
  1072             for ( TInt i = 0; i < linksCount; i++ )
       
  1073                 {
       
  1074                 TBool remove = ETrue;
       
  1075                 const MVPbkContactStoreProperties& linkStoreProp = 
       
  1076                     iResults->At( i ).ContactStore().StoreProperties();
       
  1077                 
       
  1078                 for ( TInt j = 0; j < storesCount; j++ )
       
  1079                     {
       
  1080                     if ( !linkStoreProp.Uri().UriDes().Compare( ( *stores )[j].UriDes() ) )
       
  1081                         {
       
  1082                         remove = EFalse;
       
  1083                         break;
       
  1084                         }
       
  1085                     }
       
  1086                 if ( remove )
       
  1087                     {
       
  1088                     iResults->Delete( i );
       
  1089                     linksCount--;
       
  1090                     i--;
       
  1091                     }
       
  1092                 }
       
  1093             }
       
  1094         }
       
  1095     CleanupStack::PopAndDestroy( stores );
       
  1096     }
       
  1097 
       
  1098 TBool CVPbkPhoneNumberMatchStrategyImpl::IsSimStore( const MVPbkContactStore& aStore )
       
  1099     {
       
  1100     TVPbkContactStoreUriPtr uriPtr = aStore.StoreProperties().Uri();
       
  1101     if ( !uriPtr.UriDes().Compare( KVPbkSimGlobalAdnURI )
       
  1102             || !uriPtr.UriDes().Compare( KVPbkSimGlobalFdnURI )
       
  1103             || !uriPtr.UriDes().Compare( KVPbkSimGlobalSdnURI )
       
  1104             || !uriPtr.UriDes().Compare( KVPbkSimGlobalOwnNumberURI ) )
       
  1105         {
       
  1106         return ETrue;
       
  1107         }
       
  1108     return EFalse;
       
  1109     }
       
  1110 
   948 CVPbkPhoneNumberMatchStrategy::CVPbkPhoneNumberMatchStrategy()
  1111 CVPbkPhoneNumberMatchStrategy::CVPbkPhoneNumberMatchStrategy()
   949     {
  1112     {
   950     }
  1113     }
   951 
  1114 
   952 EXPORT_C CVPbkPhoneNumberMatchStrategy* CVPbkPhoneNumberMatchStrategy::NewL(
  1115 EXPORT_C CVPbkPhoneNumberMatchStrategy* CVPbkPhoneNumberMatchStrategy::NewL(
   998 TArray<MVPbkContactStore*> CVPbkPhoneNumberMatchStrategy::StoresToMatch() const
  1161 TArray<MVPbkContactStore*> CVPbkPhoneNumberMatchStrategy::StoresToMatch() const
   999     {
  1162     {
  1000     return iImpl->StoresToMatch();
  1163     return iImpl->StoresToMatch();
  1001     }
  1164     }
  1002 
  1165 
       
  1166 TBool CVPbkPhoneNumberMatchStrategy::IsSimStore( const MVPbkContactStore& aStore )
       
  1167     {
       
  1168     return iImpl->IsSimStore( aStore );
       
  1169     }
  1003 // End of File
  1170 // End of File