phonebookengines/contactsmodel/cntplsql/src/cpplcommaddrtable.cpp
branchRCL_3
changeset 9 0d28c1c5b6dd
parent 0 e686773b3f54
child 11 2828b4d142c0
equal deleted inserted replaced
8:5586b4d2ec3e 9:0d28c1c5b6dd
   692 @param aDatabase The database.
   692 @param aDatabase The database.
   693 
   693 
   694 @return Array of contact IDs which are candidate matches.
   694 @return Array of contact IDs which are candidate matches.
   695 */
   695 */
   696 CContactIdArray* CPplCommAddrTable::MatchPhoneNumberL(const TDesC& aNumber, const TInt aMatchLengthFromRight)
   696 CContactIdArray* CPplCommAddrTable::MatchPhoneNumberL(const TDesC& aNumber, const TInt aMatchLengthFromRight)
   697 	{
   697     {
   698 	CContactIdArray* phoneMatchArray = CContactIdArray::NewLC();
   698     CContactIdArray* phoneMatchArray = CContactIdArray::NewLC();
   699 
   699 
   700 	TInt numLowerDigits = aMatchLengthFromRight;
   700     TInt numLowerDigits = aMatchLengthFromRight;
   701 	TInt numUpperDigits = 0;
   701     TInt numUpperDigits = 0;
   702 
   702 
   703 	if(numLowerDigits > KLowerSevenDigits)
   703     if(numLowerDigits > KLowerSevenDigits)
   704 		{
   704         {
   705 		// New style matching.
   705         // New style matching.
   706 		numLowerDigits = KLowerSevenDigits;
   706         numLowerDigits = KLowerSevenDigits;
   707 		numUpperDigits = aMatchLengthFromRight - KLowerSevenDigits;
   707         numUpperDigits = aMatchLengthFromRight - KLowerSevenDigits;
   708 		}
   708         }
   709 
   709 
   710 	TMatch phoneDigits = CreatePaddedPhoneDigitsL(aNumber, numLowerDigits, numUpperDigits);
   710     TMatch phoneDigits = CreatePaddedPhoneDigitsL(aNumber, numLowerDigits, numUpperDigits);
   711 
   711 
   712 	if (phoneDigits.iNumLowerDigits + phoneDigits.iNumUpperDigits > 0)
   712     if (phoneDigits.iNumLowerDigits + phoneDigits.iNumUpperDigits > 0)
   713 		{
   713         {
   714 		// build statement
   714         // build statement
   715 		RSqlStatement stmnt;
   715         RSqlStatement stmnt;
   716 		CleanupClosePushL(stmnt);
   716         CleanupClosePushL(stmnt);
   717 		stmnt.PrepareL(iDatabase, iMatchSelectStmnt->SqlStringL() );
   717         stmnt.PrepareL(iDatabase, iMatchSelectStmnt->SqlStringL() );
   718 
   718 
   719 		const TInt KValueParamIndex(KFirstParam);			// first parameter in query...
   719         const TInt KValueParamIndex(KFirstParam);           // first parameter in query...
   720 		const TInt KTypeParamIndex(KValueParamIndex + 1);	// ...and the second.
   720         const TInt KTypeParamIndex(KValueParamIndex + 1);   // ...and the second.
   721 
   721 
   722     	User::LeaveIfError(stmnt.BindInt(KValueParamIndex, phoneDigits.iLowerSevenDigits ));
   722         User::LeaveIfError(stmnt.BindInt(KValueParamIndex, phoneDigits.iLowerSevenDigits ));
   723     	User::LeaveIfError(stmnt.BindInt(KTypeParamIndex, EPhoneNumber ));
   723         User::LeaveIfError(stmnt.BindInt(KTypeParamIndex, EPhoneNumber ));
   724 
   724 
   725 		// fetch the list of any matching contact ids
   725         // fetch the list of any matching contact ids
   726 		TInt err(KErrNone);
   726         TInt err(KErrNone);
   727 		const TInt KContactIdIdx(iMatchSelectStmnt->ParameterIndex(KCommAddrContactId() ) );
   727         const TInt KContactIdIdx(iMatchSelectStmnt->ParameterIndex(KCommAddrContactId() ) );
   728 		const TInt KExtraValueIdx(iMatchSelectStmnt->ParameterIndex(KCommAddrExtraValue() ) );
   728         const TInt KExtraValueIdx(iMatchSelectStmnt->ParameterIndex(KCommAddrExtraValue() ) );
   729 		while ((err = stmnt.Next() ) == KSqlAtRow)
   729         while ((err = stmnt.Next() ) == KSqlAtRow)
   730 			{
   730             {
   731 			if (aMatchLengthFromRight <= KLowerSevenDigits)
   731             if (aMatchLengthFromRight <= KLowerSevenDigits)
   732 				{
   732                 {
   733 				// Matching 7 or less digits...we've already matched.
   733                 // Matching 7 or less digits...we've already matched.
   734 				phoneMatchArray->AddL(stmnt.ColumnInt(KContactIdIdx) );
   734                 phoneMatchArray->AddL(stmnt.ColumnInt(KContactIdIdx) );
   735 				}
   735                 }
   736 			else
   736             else
   737 				{
   737                 {
   738 				// Check the upper digits...
   738                 // Check the upper digits...
   739 				TInt32 storedUpperDigits(0);
   739                 TInt32 storedUpperDigits(0);
   740 				TPtrC extValString = stmnt.ColumnTextL(KExtraValueIdx);
   740                 TPtrC extValString = stmnt.ColumnTextL(KExtraValueIdx);
   741 				User::LeaveIfError(TLex(extValString).Val(storedUpperDigits) );
   741                 User::LeaveIfError(TLex(extValString).Val(storedUpperDigits) );
   742 
   742 
   743 				const TInt KDigitsToRemove = KMaxPhoneMatchLength - KLowerSevenDigits - phoneDigits.iNumUpperDigits;
   743                 const TInt KDigitsToRemove = KMaxPhoneMatchLength - KLowerSevenDigits - phoneDigits.iNumUpperDigits;
   744 				for(TInt i = 0; i < KDigitsToRemove; ++i)
   744                 for(TInt i = 0; i < KDigitsToRemove; ++i)
   745 					{
   745                     {
   746 					// repeatedly divide by 10 to lop off the appropriate number of digits from the right
   746                     // repeatedly divide by 10 to lop off the appropriate number of digits from the right
   747 					storedUpperDigits /= 10;
   747                     storedUpperDigits /= 10;
   748 					}
   748                     }
   749 
   749 
   750 				storedUpperDigits = TMatch::PadOutPhoneMatchNumber(storedUpperDigits, KDigitsToRemove);
   750                 storedUpperDigits = TMatch::PadOutPhoneMatchNumber(storedUpperDigits, KDigitsToRemove);
   751 
   751 
   752 				if (phoneDigits.iUpperDigits == storedUpperDigits)
   752                 if (phoneDigits.iUpperDigits == storedUpperDigits)
   753 					{
   753                     {
   754 					phoneMatchArray->AddL(stmnt.ColumnInt(KContactIdIdx) );
   754                     phoneMatchArray->AddL(stmnt.ColumnInt(KContactIdIdx) );
   755 					}
   755                     }
   756 				}
   756                 }
   757 			}
   757             }
   758 
   758 
   759 		// leave if we didn't complete going through the results properly
   759         // leave if we didn't complete going through the results properly
   760 		if(err != KSqlAtEnd)
   760         if(err != KSqlAtEnd)
   761 			{
   761             {
   762 			User::Leave(err);
   762             User::Leave(err);
   763 			}
   763             }
   764 		CleanupStack::PopAndDestroy(&stmnt);
   764         CleanupStack::PopAndDestroy(&stmnt);
   765 		}
   765         }
   766 
   766 
   767 	CleanupStack::Pop(phoneMatchArray);
   767     CleanupStack::Pop(phoneMatchArray);
   768 	return phoneMatchArray;
   768     return phoneMatchArray;
   769 	}
   769     }
       
   770 
       
   771 /**
       
   772 Returns an array of contact item IDs for all the contact items which may contain
       
   773 the specified telephone number in a telephone, fax or SMS type field.
       
   774 
       
   775 This is improved version of MatchPhoneNumberL method.
       
   776 The number is compared starting from the right side of the number and 
       
   777 the method returns an array of candidate matches.  
       
   778 Punctuation (e.g. spaces) and other alphabetic characters are ignored
       
   779 when comparing. Leading zeros are removed. Digits are compared up to 
       
   780 the lenght of shorter number.
       
   781 
       
   782 @param aNumber Phone number string.
       
   783 @return Array of contact IDs which are candidate matches.
       
   784 */
       
   785 CContactIdArray* CPplCommAddrTable::BestMatchingPhoneNumberL(const TDesC& aNumber)
       
   786     {
       
   787     const TInt KUpperMaxLength = KMaxPhoneMatchLength - KLowerSevenDigits;
       
   788 
       
   789     CContactIdArray* phoneMatchArray = CContactIdArray::NewLC();
       
   790 
       
   791     TMatch phoneDigits = CreatePaddedPhoneDigitsL(aNumber, KLowerSevenDigits, KUpperMaxLength);
       
   792 
       
   793     if (phoneDigits.iNumLowerDigits + phoneDigits.iNumUpperDigits > 0)
       
   794         {
       
   795         // build statement
       
   796         RSqlStatement stmnt;
       
   797         CleanupClosePushL(stmnt);
       
   798         stmnt.PrepareL(iDatabase, iMatchSelectStmnt->SqlStringL());
       
   799 
       
   800         const TInt KValueParamIndex(KFirstParam); // first parameter in query...
       
   801         const TInt KTypeParamIndex(KValueParamIndex + 1); // ...and the second.
       
   802 
       
   803         User::LeaveIfError(stmnt.BindInt(KValueParamIndex,
       
   804                 phoneDigits.iLowerSevenDigits));
       
   805         User::LeaveIfError(stmnt.BindInt(KTypeParamIndex, EPhoneNumber));
       
   806 
       
   807         // fetch the list of any matching contact ids
       
   808         TInt err(KErrNone);
       
   809         const TInt KContactIdIdx(iMatchSelectStmnt->ParameterIndex( KCommAddrContactId()));
       
   810         const TInt KExtraValueIdx(iMatchSelectStmnt->ParameterIndex(KCommAddrExtraValue()));
       
   811         while ((err = stmnt.Next()) == KSqlAtRow)
       
   812             {
       
   813             // Check the upper digits...
       
   814             TInt32 number = phoneDigits.iUpperDigits;
       
   815             TPtrC extValString = stmnt.ColumnTextL(KExtraValueIdx);
       
   816             TInt32 storedUpperDigits;
       
   817             User::LeaveIfError(TLex(extValString).Val(storedUpperDigits));
       
   818             TInt32 stored = storedUpperDigits;
       
   819 
       
   820             while ( (number != 0) && (stored != 0) &&
       
   821                     (number % 10 == 0 || stored % 10 == 0) )
       
   822                 {
       
   823                 number /= 10;
       
   824                 stored /= 10;
       
   825                 }
       
   826 
       
   827             if ( (phoneDigits.iUpperDigits == 0) || (storedUpperDigits == 0) ||
       
   828                  (number == stored) )
       
   829                 {
       
   830                 phoneMatchArray->AddL(stmnt.ColumnInt(KContactIdIdx));
       
   831                 }
       
   832             }
       
   833 
       
   834         // leave if we didn't complete going through the results properly
       
   835         if (err != KSqlAtEnd)
       
   836             {
       
   837             User::Leave(err);
       
   838             }
       
   839         CleanupStack::PopAndDestroy(&stmnt);
       
   840         }
       
   841 
       
   842     CleanupStack::Pop(phoneMatchArray);
       
   843     return phoneMatchArray;
       
   844     }
   770 
   845 
   771 
   846 
   772 /**
   847 /**
   773 Searches the contacts database to find any contact items with an exact match on the email address supplied.
   848 Searches the contacts database to find any contact items with an exact match on the email address supplied.
   774 
   849 
   893 		}
   968 		}
   894 
   969 
   895 	return phoneNumber;
   970 	return phoneNumber;
   896 	}
   971 	}
   897 
   972 
   898 
       
   899 /**
   973 /**
   900 CPplCommAddrTable::TMatch constructor.
   974 CPplCommAddrTable::TMatch constructor.
   901 */
   975 */
   902 CPplCommAddrTable::TMatch::TMatch()
   976 CPplCommAddrTable::TMatch::TMatch()
   903 	:
   977 	: