telephonyserverplugins/ctsydispatchlayer/src/utility/cphonebookentry.cpp
changeset 0 3553901f7fa8
child 24 6638e7f4bd8f
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include <ctsy/ltsy/cphonebookentry.h>
       
    18 
       
    19 /**
       
    20  * Factory function
       
    21  * 
       
    22  * @return Pointer to a new CPhoneBookEntry object
       
    23  */
       
    24 EXPORT_C CPhoneBookEntry* CPhoneBookEntry::NewLC()
       
    25 	{
       
    26 	CPhoneBookEntry* self = new (ELeave) CPhoneBookEntry();
       
    27 	CleanupStack::PushL(self);
       
    28 	self->ConstructL();
       
    29 	return self;
       
    30 	}
       
    31 
       
    32 /**
       
    33  * Factory function
       
    34  * 
       
    35  * @return Pointer to a new CPhoneBookEntry object left on the cleanup stack
       
    36  */
       
    37 EXPORT_C CPhoneBookEntry* CPhoneBookEntry::NewL()
       
    38 	{
       
    39 	CPhoneBookEntry* self = CPhoneBookEntry::NewLC();
       
    40 	CleanupStack::Pop(self);
       
    41 	return self;
       
    42 	}
       
    43 
       
    44 /**
       
    45  * Second phase construction
       
    46  */
       
    47 void CPhoneBookEntry::ConstructL()
       
    48 	{
       
    49 	}
       
    50 
       
    51 /**
       
    52  * First phase construction
       
    53  */
       
    54 CPhoneBookEntry::CPhoneBookEntry() : 
       
    55 	iIndex(0),
       
    56 	iIndexSet(EFalse)
       
    57 	{
       
    58 	}
       
    59 
       
    60 /**
       
    61  * Destruction
       
    62  */
       
    63 
       
    64 CPhoneBookEntry::~CPhoneBookEntry()
       
    65 	{
       
    66 	Reset();
       
    67 	}
       
    68 
       
    69 /**
       
    70  * Resets the object to its initial state.
       
    71  */
       
    72 EXPORT_C void CPhoneBookEntry::Reset()
       
    73 	{
       
    74 	iIndex = 0;
       
    75 	iIndexSet = EFalse;
       
    76 	iFirstName.Close();
       
    77 	iDiallingNumber.Close();
       
    78 
       
    79 	TInt i = 0;
       
    80 	
       
    81 	const TInt KEmailAddressCount = iEmailAddresss.Count();
       
    82 	for(i = 0; i < KEmailAddressCount; ++i)
       
    83 		{
       
    84 		iEmailAddresss[i].Close();
       
    85 		}
       
    86 	iEmailAddresss.Close();
       
    87 	
       
    88 	const TInt KSecondNamesCount = iSecondNames.Count();
       
    89 	for(i = 0; i < KSecondNamesCount; ++i)
       
    90 		{
       
    91 		iSecondNames[i].Close();
       
    92 		}
       
    93 	iSecondNames.Close();
       
    94 	
       
    95 	const TInt KAdditionalNumbersCount = iAdditionalNumbers.Count();
       
    96 	for(i = 0; i < KAdditionalNumbersCount; ++i)
       
    97 		{
       
    98 		iAdditionalNumbers[i].Close();
       
    99 		}
       
   100 	iAdditionalNumbers.Close();
       
   101 	}
       
   102 
       
   103 /**
       
   104  * @return The Entry Index.
       
   105  */
       
   106 EXPORT_C TInt CPhoneBookEntry::GetIndex() const
       
   107 	{
       
   108 	return iIndex;
       
   109 	}
       
   110 
       
   111 /**
       
   112  * @return The entries firstname.
       
   113  */
       
   114 EXPORT_C const TDesC& CPhoneBookEntry::GetFirstName() const
       
   115 	{
       
   116 	return iFirstName;
       
   117 	}
       
   118 
       
   119 /**
       
   120  * @return The entries dialling number.
       
   121  */
       
   122 EXPORT_C const TDesC& CPhoneBookEntry::GetDiallingNumber() const
       
   123 	{
       
   124 	return iDiallingNumber;
       
   125 	}
       
   126 
       
   127 /**
       
   128  * @return An array of Email address's in the entry.
       
   129  */
       
   130 
       
   131 EXPORT_C const RArray<RBuf>& CPhoneBookEntry::GetEmailAddresss() const
       
   132 	{
       
   133 	return iEmailAddresss;
       
   134 	}
       
   135 
       
   136 /**
       
   137  * @return An array of second names in the entry.
       
   138  */
       
   139 EXPORT_C const RArray<RBuf>& CPhoneBookEntry::GetSecondNames() const
       
   140 	{
       
   141 	return iSecondNames;
       
   142 	}
       
   143 
       
   144 /**
       
   145  * @return An array of additional numbers.
       
   146  */
       
   147 EXPORT_C const RArray<RBuf>& CPhoneBookEntry::GetAdditionalNumbers() const
       
   148 	{
       
   149 	return iAdditionalNumbers;
       
   150 	}
       
   151 
       
   152 /**
       
   153  * @param aIndex The index to set.
       
   154  */
       
   155 EXPORT_C void CPhoneBookEntry::SetIndex(TInt aIndex)
       
   156 	{
       
   157 	iIndex = aIndex;
       
   158 	iIndexSet = ETrue;
       
   159 	}
       
   160 
       
   161 /**
       
   162  * @param aFirstName The firstname to be set.
       
   163  * @return System wide error code.
       
   164  */
       
   165 EXPORT_C TInt CPhoneBookEntry::SetFirstName(const TDesC& aFirstName)
       
   166 	{
       
   167 	TInt error = iFirstName.ReAlloc(aFirstName.Length());
       
   168 	
       
   169 	if(error == KErrNone)
       
   170 		{
       
   171 		iFirstName.Copy(aFirstName);
       
   172 		}
       
   173 	
       
   174 	return error;
       
   175 	}
       
   176 
       
   177 /**
       
   178  * @param aFirstName The dialling number to be set.
       
   179  * @return System wide error code.
       
   180  * 
       
   181  */
       
   182 EXPORT_C TInt CPhoneBookEntry::SetDiallingNumber(const TDesC& aDiallingNumber)
       
   183 	{
       
   184 	TInt error = iDiallingNumber.ReAlloc(aDiallingNumber.Length());
       
   185 	
       
   186 	if(error == KErrNone)
       
   187 		{
       
   188 		iDiallingNumber.Copy(aDiallingNumber);
       
   189 		}
       
   190 	
       
   191 	return error;
       
   192 	}
       
   193 
       
   194 /**
       
   195  * @param aEmailAddress An email address to be added to the entries list.
       
   196  * @return System wide error code.
       
   197  */
       
   198 EXPORT_C TInt CPhoneBookEntry::AddEmailAddress(const TDesC& aEmailAddress)
       
   199 	{
       
   200 	RBuf emailAddress;
       
   201 	TInt error = emailAddress.Create(aEmailAddress);
       
   202 	
       
   203 	if(error == KErrNone)
       
   204 		{
       
   205 		error = iEmailAddresss.Append(emailAddress);
       
   206 		}
       
   207 	
       
   208 	return error;
       
   209 	}
       
   210 
       
   211 /**
       
   212  * @param aSecondName An secondname to be added to the entries list.
       
   213  * @return System wide error code.
       
   214  * 
       
   215  */
       
   216 EXPORT_C TInt CPhoneBookEntry::AddSecondName(const TDesC& aSecondName)
       
   217 	{
       
   218 	RBuf secondName;
       
   219 	TInt error = secondName.Create(aSecondName);
       
   220 	
       
   221 	if(error == KErrNone)
       
   222 		{
       
   223 		error = iSecondNames.Append(secondName);
       
   224 		}
       
   225 	
       
   226 	return error;
       
   227 	}
       
   228 
       
   229 /**
       
   230  * @param aAdditionalNumber An additional number to be added to the entries list.
       
   231  * @return System wide error code.
       
   232  */
       
   233 EXPORT_C TInt CPhoneBookEntry::AddAdditionalNumber(const TDesC& aAdditionalNumber)
       
   234 	{
       
   235 	RBuf additionalNumber;
       
   236 	TInt error = additionalNumber.Create(aAdditionalNumber);
       
   237 	
       
   238 	if(error == KErrNone)
       
   239 		{
       
   240 		error = iAdditionalNumbers.Append(additionalNumber);
       
   241 		}
       
   242 	
       
   243 	return error;
       
   244 	}
       
   245 
       
   246 /**
       
   247  * @return If the two entries are equal, equivalence is defined is all entries are equal and in
       
   248  * the same order.
       
   249  */
       
   250 EXPORT_C TBool CPhoneBookEntry::operator== (const CPhoneBookEntry& aPhoneBookEntry) const
       
   251 	{
       
   252 	TBool ret =	(iIndex == aPhoneBookEntry.GetIndex()) &&
       
   253 				(iFirstName == aPhoneBookEntry.GetFirstName()) &&
       
   254 				(iDiallingNumber == aPhoneBookEntry.GetDiallingNumber()) &&
       
   255 				(iEmailAddresss.Count() == aPhoneBookEntry.GetEmailAddresss().Count()) &&
       
   256 				(iSecondNames.Count() == aPhoneBookEntry.GetSecondNames().Count()) &&
       
   257 				(iAdditionalNumbers.Count() == aPhoneBookEntry.GetAdditionalNumbers().Count());
       
   258 	
       
   259 	TInt i = 0;
       
   260 	for(i = 0; i < iEmailAddresss.Count() && ret; ++i)
       
   261 		{
       
   262 		ret = iEmailAddresss[i] == aPhoneBookEntry.GetEmailAddresss()[i];
       
   263 		}
       
   264 
       
   265 	for(i = 0; i < iSecondNames.Count() && ret; ++i)
       
   266 		{
       
   267 		ret = iSecondNames[i] == aPhoneBookEntry.GetSecondNames()[i];
       
   268 		}
       
   269 	
       
   270 	for(i = 0; i < iAdditionalNumbers.Count() && ret; ++i)
       
   271 		{
       
   272 		ret = iAdditionalNumbers[i] == aPhoneBookEntry.GetAdditionalNumbers()[i];
       
   273 		}
       
   274 	
       
   275 	return ret;
       
   276 	}
       
   277 
       
   278 /**
       
   279  * Writes the entry to a CPhoneBookBuffer in the TLV format.
       
   280  * 
       
   281  * @param aPhoneBookBuffer The buffer to write the entry to.
       
   282  * @return System wide error code.
       
   283  */
       
   284 EXPORT_C TInt CPhoneBookEntry::ExternalizeToTlvEntry(CPhoneBookBuffer& aPhoneBookBuffer) const
       
   285 	{
       
   286 	TInt error = KErrNone;
       
   287 	
       
   288 	if(error == KErrNone)
       
   289 		{
       
   290 		error = aPhoneBookBuffer.AddNewEntryTag();
       
   291 		}
       
   292 
       
   293 	if(iIndexSet && (error == KErrNone))
       
   294 		{
       
   295 		error = aPhoneBookBuffer.PutTagAndValue(RMobilePhoneBookStore::ETagPBAdnIndex, static_cast<TUint16>(iIndex));
       
   296 		}
       
   297 		
       
   298 	if(iFirstName.Length() > 0 && (error == KErrNone))
       
   299 		{
       
   300 		error = aPhoneBookBuffer.PutTagAndValue(RMobilePhoneBookStore::ETagPBText,iFirstName);
       
   301 		}
       
   302 		
       
   303 	if(iDiallingNumber.Length() > 0 && (error == KErrNone))
       
   304 		{
       
   305 		error = aPhoneBookBuffer.PutTagAndValue(RMobilePhoneBookStore::ETagPBNumber,iDiallingNumber);
       
   306 		}
       
   307 		
       
   308 	TInt i = 0;
       
   309 
       
   310 	for(i = 0; i < iEmailAddresss.Count() && (error == KErrNone); ++i)
       
   311 		{
       
   312 		if(iEmailAddresss[i].Length() > 0)
       
   313 			{
       
   314 			error = aPhoneBookBuffer.PutTagAndValue(RMobilePhoneBookStore::ETagPBEmailAddress,iEmailAddresss[i]);
       
   315 			}
       
   316 		}
       
   317 	
       
   318 	for(i = 0; i < iSecondNames.Count() && (error == KErrNone); ++i)
       
   319 		{
       
   320 		if(iSecondNames[i].Length() > 0)
       
   321 			{
       
   322 			error = aPhoneBookBuffer.PutTagAndValue(RMobilePhoneBookStore::ETagPBSecondName,iSecondNames[i]);
       
   323 			}
       
   324 		}
       
   325 		
       
   326 	for(i = 0; i < iAdditionalNumbers.Count() && (error == KErrNone); ++i)
       
   327 		{
       
   328 		if(iAdditionalNumbers[i].Length() > 0)
       
   329 			{
       
   330 			error = aPhoneBookBuffer.AddNewNumberTag();
       
   331 			if(error == KErrNone)
       
   332 				{
       
   333 				error = aPhoneBookBuffer.PutTagAndValue(RMobilePhoneBookStore::ETagPBNumber,iAdditionalNumbers[i]);
       
   334 				}
       
   335 			}
       
   336 		}
       
   337 	
       
   338 	return error;
       
   339 	}
       
   340 
       
   341 /**
       
   342  * Reads an entry from a CPhoneBookBuffer, this does not reset the object first.
       
   343  * 
       
   344  * @param aPhoneBookBuffer The buffer to read from.
       
   345  * @param aNewEntryTagRead If the TLV being read has a new entry tag or not.
       
   346  * 
       
   347  * @return System wide error code.
       
   348  * 
       
   349  */
       
   350 EXPORT_C TInt CPhoneBookEntry::InternalizeFromTlvEntry(CPhoneBookBuffer& aPhoneBookBuffer, TBool aNewEntryTagRead)
       
   351 	{
       
   352     TUint8 tag = 0;
       
   353     CPhoneBookBuffer::TPhBkTagType tagType = CPhoneBookBuffer::EPhBkTypeNoData;
       
   354 	TInt error = KErrNone;
       
   355 	TInt newEntryCount = aNewEntryTagRead ? 1 : 0;
       
   356 	TBool gettingAdditionalNumber = EFalse;
       
   357 	
       
   358 	while(error == KErrNone && aPhoneBookBuffer.RemainingReadLength() > 0 && newEntryCount < 2)
       
   359 		{
       
   360 		error = aPhoneBookBuffer.GetTagAndType(tag,tagType);
       
   361 
       
   362 		if(gettingAdditionalNumber)
       
   363 			{
       
   364 			if(tag != RMobilePhoneBookStore::ETagPBNumber)
       
   365 				{
       
   366 				error = KErrArgument;
       
   367 				}
       
   368 			}
       
   369 		
       
   370 		if(error == KErrNone)
       
   371 			{
       
   372 			switch(tag)
       
   373 				{
       
   374 				case RMobilePhoneBookStore::ETagPBNewEntry:
       
   375 					{
       
   376 					newEntryCount += 1;
       
   377 					break; //RMobilePhoneBookStore::ETagPBNewEntry
       
   378 					}
       
   379 
       
   380 				case RMobilePhoneBookStore::ETagPBAdnIndex:
       
   381 					{
       
   382 					iIndexSet = ETrue;
       
   383 					
       
   384 					switch(tagType)
       
   385 						{
       
   386 						case CPhoneBookBuffer::EPhBkTypeInt8:
       
   387 							{
       
   388 							TUint8 value = 0;
       
   389 							error = aPhoneBookBuffer.GetValue(value);
       
   390 							iIndex = (error == KErrNone ? value : 0);
       
   391 							break;
       
   392 							}
       
   393 						case CPhoneBookBuffer::EPhBkTypeInt16:
       
   394 							{
       
   395 							TUint16 value = 0;
       
   396 							error = aPhoneBookBuffer.GetValue(value);
       
   397 							iIndex = (error == KErrNone ? value : 0);
       
   398 							break;
       
   399 							}
       
   400 						case CPhoneBookBuffer::EPhBkTypeInt32:
       
   401 							{
       
   402 							TUint32 value = 0;
       
   403 							error = aPhoneBookBuffer.GetValue(value);
       
   404 							iIndex = (error == KErrNone ? value : 0);
       
   405 							break;
       
   406 							}
       
   407 						default:
       
   408 							{
       
   409 							error = KErrArgument;
       
   410 							break;
       
   411 							}
       
   412 						}
       
   413 					break; //RMobilePhoneBookStore::ETagPBAdnIndex
       
   414 					}
       
   415 
       
   416 				case RMobilePhoneBookStore::ETagPBText:
       
   417 					{
       
   418 					TPtrC16 text;
       
   419 					error = GetText(text,tagType,aPhoneBookBuffer);
       
   420 					if((error == KErrNone) && text.Length() > 0)
       
   421 						{
       
   422 						error = SetFirstName(text);
       
   423 						}
       
   424 					break; //RMobilePhoneBookStore::ETagPBText
       
   425 					}
       
   426 					
       
   427 				case RMobilePhoneBookStore::ETagPBNumber:
       
   428 					{
       
   429 					TPtrC16 number;
       
   430 					error = GetText(number,tagType,aPhoneBookBuffer);
       
   431 					if((error == KErrNone) && number.Length() > 0)
       
   432 						{
       
   433 						if(gettingAdditionalNumber)
       
   434 							{
       
   435 							error = AddAdditionalNumber(number);
       
   436 							gettingAdditionalNumber = EFalse;
       
   437 							}
       
   438 						else
       
   439 							{
       
   440 							error = SetDiallingNumber(number);
       
   441 							}
       
   442 						}
       
   443 					break; //RMobilePhoneBookStore::ETagPBNumber
       
   444 					}
       
   445 				
       
   446 				case RMobilePhoneBookStore::ETagPBEmailAddress:
       
   447 					{
       
   448 					TPtrC16 emailAddress;
       
   449 					error = GetText(emailAddress,tagType,aPhoneBookBuffer);
       
   450 					if((error == KErrNone) && emailAddress.Length() > 0)
       
   451 						{
       
   452 						error = AddEmailAddress(emailAddress);
       
   453 						}
       
   454 					break; //RMobilePhoneBookStore::ETagPBEmailAddress
       
   455 					}
       
   456 					
       
   457 				case RMobilePhoneBookStore::ETagPBSecondName:
       
   458 					{
       
   459 					TPtrC16 secondName;
       
   460 					error = GetText(secondName,tagType,aPhoneBookBuffer);
       
   461 					if((error == KErrNone) && secondName.Length() > 0)
       
   462 						{
       
   463 						error = AddSecondName(secondName);
       
   464 						}
       
   465 					break; //RMobilePhoneBookStore::ETagPBNumber
       
   466 					}
       
   467 					
       
   468 				case RMobilePhoneBookStore::ETagPBAnrStart:
       
   469 					{
       
   470 					gettingAdditionalNumber = ETrue;
       
   471 					break; //RMobilePhoneBookStore::ETagPBNumber
       
   472 					}
       
   473 					
       
   474 				default:
       
   475 					{
       
   476 					aPhoneBookBuffer.SkipValue(tagType);
       
   477 					break;
       
   478 					}
       
   479 				} //switch(tag)
       
   480 			}
       
   481 		} //while loop
       
   482 	
       
   483 	if((error == KErrNone) && (newEntryCount == 0))
       
   484 		{
       
   485 		//no error but no new entry found
       
   486 		error = KErrArgument;
       
   487 		}
       
   488 	
       
   489 	if(error != KErrNone)
       
   490 		{
       
   491 		//convert all error codes to KErrArgument
       
   492 		error = KErrArgument;
       
   493 		}
       
   494 	
       
   495 	return error;
       
   496 	}
       
   497 
       
   498 /**
       
   499  * Helper function
       
   500  * 
       
   501  * Gets a the next TPtrC16
       
   502  * @param aText A TPtrC to the next text entry
       
   503  * @param aPhoneBookBuffer The phonebook buffer to read from
       
   504  * 
       
   505  * @return System wide error code.
       
   506  */
       
   507 TInt CPhoneBookEntry::GetText(TPtrC16& aText, CPhoneBookBuffer::TPhBkTagType aTagType, CPhoneBookBuffer& aPhoneBookBuffer) const
       
   508 	{
       
   509 
       
   510 	TInt error = KErrNone;
       
   511 	
       
   512 	switch(aTagType)
       
   513 		{
       
   514 		case CPhoneBookBuffer::EPhBkTypeDes16:
       
   515 			{
       
   516 			error = aPhoneBookBuffer.GetValue(aText);
       
   517 			break;
       
   518 			}
       
   519 			
       
   520 		default:
       
   521 			{
       
   522 			error = KErrArgument;
       
   523 			break;
       
   524 			}
       
   525 		}
       
   526 	
       
   527 	return error;
       
   528 
       
   529 	}
       
   530 
       
   531 /**
       
   532  * Helper function
       
   533  * 
       
   534  * @param aSize The size to be updated.
       
   535  * @param aDes The descriptor being added.
       
   536  */
       
   537 void CPhoneBookEntry::DesSize(TInt& aSize, const TDesC& aDes) const
       
   538 	{
       
   539 	switch(aSize % 4)
       
   540 		{
       
   541 		case 0:
       
   542 			aSize += 1;
       
   543 			break;
       
   544 		case 1:
       
   545 			break;
       
   546 		case 2:
       
   547 			aSize += 3;
       
   548 			break;
       
   549 		case 3:
       
   550 			aSize += 2;
       
   551 			break;
       
   552 		default:
       
   553 			break;
       
   554 		}
       
   555 	aSize += 1 /*the tag*/ + 2 /*the length*/ +  aDes.Size();
       
   556 	}
       
   557 
       
   558 /**
       
   559  * @return The TLV length required for the CPhoneBookEntry's current state .
       
   560  */
       
   561 EXPORT_C TInt CPhoneBookEntry::TlvLength() const
       
   562 	{
       
   563 	TInt size = 0;
       
   564 		
       
   565 	size += 1; //new entry tag
       
   566 		
       
   567 	size += 1; //index tag
       
   568 	//tag TUint16
       
   569 	size += 2;
       
   570 	
       
   571 	if(iFirstName.Length() > 0)
       
   572 		{
       
   573 		DesSize(size,iFirstName);
       
   574 		}
       
   575 
       
   576 	if(iDiallingNumber.Length() > 0)
       
   577 		{
       
   578 		DesSize(size,iDiallingNumber);
       
   579 		}
       
   580 	
       
   581 	TInt i = 0;
       
   582 	for(i = 0; i < iEmailAddresss.Count(); ++i)
       
   583 		{
       
   584 		if(iEmailAddresss[i].Length() > 0)
       
   585 			{
       
   586 			DesSize(size,iEmailAddresss[i]);
       
   587 			}
       
   588 		}
       
   589 	
       
   590 	for(i = 0; i < iSecondNames.Count(); ++i)
       
   591 		{
       
   592 		if(iSecondNames[i].Length() > 0)
       
   593 			{
       
   594 			DesSize(size,iSecondNames[i]);
       
   595 			}
       
   596 		}
       
   597 	
       
   598 	for(i = 0; i < iAdditionalNumbers.Count(); ++i)
       
   599 		{
       
   600 		if(iAdditionalNumbers[i].Length() > 0)
       
   601 			{
       
   602 			size += 1; //new number tag
       
   603 			DesSize(size,iAdditionalNumbers[i]);
       
   604 			}
       
   605 		}
       
   606 	
       
   607 	size += 3; 
       
   608 	//add this as CPhoneBookBuffer::PutTagAndValue must allow for the worst case padding
       
   609 	//i.e. 3 "Also allow for the 1-byte tag and up to 3 zero padding bytes"
       
   610 		
       
   611 	return size;
       
   612 	}
       
   613