mobilemessaging/smsmtm/clientmtm/src/smut.cpp
branchRCL_3
changeset 60 7fdbb852d323
parent 57 ebe688cedc25
equal deleted inserted replaced
57:ebe688cedc25 60:7fdbb852d323
   371 KErrArgument if aFromAddress is not a valid GSM number.
   371 KErrArgument if aFromAddress is not a valid GSM number.
   372 KErrNotFound if a contact could not be found.
   372 KErrNotFound if a contact could not be found.
   373 KErrAlreadyExists if more than one contact entry found.
   373 KErrAlreadyExists if more than one contact entry found.
   374 KErrNone if details is obtained successfully.
   374 KErrNone if details is obtained successfully.
   375 */
   375 */
   376 EXPORT_C TInt TSmsUtilities::GetDetails(RFs& /*aFs*/, const TDesC& aFromAddress, TDes& aDetails, TInt aMaxLength)
   376 EXPORT_C TInt TSmsUtilities::GetDetails(RFs& aFs, const TDesC& aFromAddress, TDes& aDetails, TInt aMaxLength)
   377 	{
   377 	{
   378 	__ASSERT_DEBUG( aMaxLength <= aDetails.MaxLength(), User::Invariant() );
   378 	__ASSERT_DEBUG( aMaxLength <= aDetails.MaxLength(), User::Invariant() );
   379 
   379 
   380 	if (aMaxLength > aDetails.MaxLength())
   380 	if (aMaxLength > aDetails.MaxLength())
   381 		{
   381 		{
   382 		aMaxLength = aDetails.MaxLength();
   382 		aMaxLength = aDetails.MaxLength();
   383 		}
   383 		}
   384 
   384 
   385 	if ( (aDetails.Length() == 0) )
   385 	TRAPD(err, DoGetDetailsL(aFs, aFromAddress, aDetails, aMaxLength));
       
   386 
       
   387 	if ( (err != KErrNone) || (aDetails.Length() == 0) )
   386 		{
   388 		{
   387 		if (aFromAddress.Length() <= aMaxLength)
   389 		if (aFromAddress.Length() <= aMaxLength)
   388 			{
   390 			{
   389 			aDetails = aFromAddress;
   391 			aDetails = aFromAddress;
   390 			aDetails.Trim();
   392 			aDetails.Trim();
   518 			aFirstId = id;
   520 			aFirstId = id;
   519 
   521 
   520 		if (aServiceIds)
   522 		if (aServiceIds)
   521 			aServiceIds->AppendL(id);
   523 			aServiceIds->AppendL(id);
   522 		}
   524 		}
       
   525 	}
       
   526 
       
   527 void TSmsUtilities::DoGetDetailsL(RFs& aFs, const TDesC& aFromAddress, TDes& aDetails, TInt aMaxLength)
       
   528 	{
       
   529 	__UHEAP_MARK;
       
   530 
       
   531 	// Check that aFromAddress is a valid GSM telephone number
       
   532 	if (!ValidGsmNumber(aFromAddress))
       
   533 		User::Leave(KErrArgument);
       
   534 
       
   535 	aDetails.Zero();
       
   536 
       
   537 	CContactDatabase* db = CContactDatabase::OpenL();
       
   538 	CleanupStack::PushL(db);
       
   539 
       
   540 	// Lookup the telephone number (aFromAddress) in the contact database
       
   541 	CContactIdArray* contactId = db->MatchPhoneNumberL(aFromAddress, KLowerSevenDigits);
       
   542 	CleanupStack::PushL(contactId);
       
   543 	
       
   544 	// Add the name if there is one and only one match in contacts. If there's more than
       
   545 	// one then wouldn't know which one to choose	
       
   546 	if (contactId->Count() <= 0)
       
   547 		{
       
   548 		//The telephone number (aFromAddress) was not found in the contact database.
       
   549 		User::Leave(KErrNotFound);
       
   550 		}
       
   551 	else if (contactId->Count() > 1)
       
   552 		{
       
   553 		//There's more than one telephone number match in contacts.	
       
   554 		User::Leave(KErrAlreadyExists);
       
   555 		}
       
   556 	
       
   557 	CContactItem* newContact = db->ReadMinimalContactL((*contactId)[0]);
       
   558 	CleanupStack::PushL(newContact);
       
   559 
       
   560 	CContactItemFieldSet& fieldSet = newContact->CardFields();
       
   561 
       
   562 	TInt count		= fieldSet.Count();
       
   563 
       
   564 	HBufC* family	= HBufC::NewLC(aMaxLength);
       
   565 	HBufC* given	= HBufC::NewLC(aMaxLength);
       
   566 	TPtr familyPtr(family->Des());
       
   567 	TPtr givenPtr(given->Des());
       
   568 
       
   569 	// Find the Given and First Name of the contact
       
   570 	// Order important
       
   571 	for (TInt curField = 0; curField < count && !(familyPtr.Length() && givenPtr.Length()); curField++)
       
   572 		{
       
   573 		CContactItemField& field = fieldSet[curField];
       
   574 
       
   575 		if (!familyPtr.Length())
       
   576 			GetName(field, KUidContactFieldFamilyName, familyPtr);
       
   577 
       
   578 		if (!givenPtr.Length())
       
   579 			GetName(field, KUidContactFieldGivenName, givenPtr);
       
   580 		}
       
   581 
       
   582 	familyPtr.Trim();
       
   583 	givenPtr.Trim();
       
   584 
       
   585 	TInt familyLen	= familyPtr.Length();
       
   586 	TInt givenLen	= givenPtr.Length();
       
   587 
       
   588 	if (!familyLen && !givenLen)
       
   589 		{
       
   590 		//Leave if no family nor given name found
       
   591 		User::Leave(KErrNotFound);
       
   592 		}
       
   593 	else if (givenLen == 0)
       
   594 		{
       
   595 		// The maximum length of familyPtr may be greater than
       
   596 		// aMaxLength, so need to check its length before copying.
       
   597 		if (familyPtr.Length() > aMaxLength)
       
   598   			{
       
   599   			familyPtr.Set(familyPtr.LeftTPtr(aMaxLength));
       
   600   			}
       
   601 
       
   602 		aDetails = familyPtr;
       
   603 		}
       
   604 	else if (familyLen == 0)
       
   605 		{
       
   606 		// The maximum length of givenPtr may be greater than
       
   607 		// aMaxLength, so need to check its length before copying.
       
   608 		if (givenPtr.Length() > aMaxLength)
       
   609   			{
       
   610   			givenPtr.Set(givenPtr.LeftTPtr(aMaxLength));
       
   611   			}
       
   612 
       
   613 		aDetails = givenPtr;
       
   614 		}
       
   615 	else
       
   616 		{
       
   617 		RResourceFile resFile = OpenResourceFileL(aFs);
       
   618 		CleanupClosePushL(resFile);
       
   619 		ReadResourceStringL(resFile, R_SENDER_NAME_FORMAT, aDetails);
       
   620 		CleanupStack::PopAndDestroy(&resFile);
       
   621 
       
   622 		TBuf<8> givenPlaceHolder = L_SMS_GIVEN_NAME;
       
   623 		TBuf<8> familyPlaceHolder = L_SMS_FAMILY_NAME;
       
   624 		TInt minLength = aDetails.Length() - givenPlaceHolder.Length() - familyPlaceHolder.Length();
       
   625 
       
   626 		if ((familyLen + givenLen + minLength) > aMaxLength)
       
   627 			{
       
   628 			// The maximum length of familyPtr may be greater than
       
   629 	   		// aMaxLength, so need to check its length before copying.
       
   630   			if (familyPtr.Length() > aMaxLength)
       
   631    				{
       
   632    				familyPtr.Set(familyPtr.LeftTPtr(aMaxLength));
       
   633    				}
       
   634 			aDetails = familyPtr;
       
   635 			}
       
   636 		else
       
   637 			{
       
   638 			Replace(givenPlaceHolder, givenPtr, aDetails);
       
   639 			Replace(familyPlaceHolder, familyPtr, aDetails);
       
   640 			}
       
   641 		}
       
   642 
       
   643 	//Remove leading and trailing spaces
       
   644 	aDetails.Trim();
       
   645 
       
   646 	CleanupStack::PopAndDestroy(5, db);
       
   647 
       
   648 	__UHEAP_MARKEND;
   523 	}
   649 	}
   524 
   650 
   525 TBool TSmsUtilities::ValidGsmNumber(const TDesC& aTelephone)
   651 TBool TSmsUtilities::ValidGsmNumber(const TDesC& aTelephone)
   526 	{
   652 	{
   527 	// Returns ETrue if
   653 	// Returns ETrue if
   575 
   701 
   576 		first++; //move to the next character
   702 		first++; //move to the next character
   577 		}
   703 		}
   578 
   704 
   579 	return validTel && validCharsFound >= KSmsValidGsmNumberMinLength;
   705 	return validTel && validCharsFound >= KSmsValidGsmNumberMinLength;
       
   706 	}
       
   707 
       
   708 void TSmsUtilities::GetName(CContactItemField& aField, TUid aFieldType, TDes& aName)
       
   709 	{
       
   710 	__UHEAP_MARK;
       
   711 	if (aField.ContentType().ContainsFieldType(aFieldType))
       
   712 		{
       
   713 		TPtrC name = aField.TextStorage()->Text();
       
   714 		aName = name.Left(Min(aName.MaxLength(), name.Length()));
       
   715 		}
       
   716 	__UHEAP_MARKEND;
   580 	}
   717 	}
   581 
   718 
   582 TBool TSmsUtilities::DoGetDescriptionL(const CSmsMessage& aMessage, TDes& aDescription, TInt aMaxLength)
   719 TBool TSmsUtilities::DoGetDescriptionL(const CSmsMessage& aMessage, TDes& aDescription, TInt aMaxLength)
   583 // this function returns EFalse if aMessage has no special message indication data and is not an SMS_STATUS_REPORT,
   720 // this function returns EFalse if aMessage has no special message indication data and is not an SMS_STATUS_REPORT,
   584 // i.e. more needs to be done to extract the description from the message
   721 // i.e. more needs to be done to extract the description from the message