buildverification/autosmoketest/contacts/src/TestContact.cpp
branchRCL_3
changeset 10 4ca382093dae
parent 5 6beaa9cf4752
child 11 493058e57c8c
equal deleted inserted replaced
5:6beaa9cf4752 10:4ca382093dae
     1 // Copyright (c) 2004-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 // This contains CTestContact
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <cntfldst.h>
       
    19 
       
    20 #include "TestContact.h"
       
    21 
       
    22 CTestContact* CTestContact::NewL(CContactItem& aSeedContact)
       
    23 	{
       
    24 	CTestContact* self=CTestContact::NewLC(aSeedContact);
       
    25 	CleanupStack::Pop();
       
    26 	return self;
       
    27 	}
       
    28 
       
    29 CTestContact* CTestContact::NewLC(CContactItem& aSeedContact)
       
    30 	{
       
    31 	CTestContact* self=new(ELeave) CTestContact();
       
    32 	CleanupStack::PushL(self);
       
    33 	self->ConstructL(aSeedContact);
       
    34 	return self;
       
    35 	}
       
    36 
       
    37 CTestContact* CTestContact::NewL(CContactItem* aSeedContact)
       
    38 	{
       
    39 	CTestContact* self=CTestContact::NewLC(aSeedContact);
       
    40 	CleanupStack::Pop();
       
    41 	return self;
       
    42 	}
       
    43 
       
    44 CTestContact* CTestContact::NewLC(CContactItem* aSeedContact)
       
    45 	{
       
    46 	CTestContact* self=new(ELeave) CTestContact();
       
    47 	CleanupStack::PushL(self);
       
    48 	self->ConstructL(aSeedContact);
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 CTestContact::CTestContact() : iDestroyOnExit(EFalse)
       
    53 	{
       
    54 	}
       
    55 
       
    56 void CTestContact::ConstructL(CContactItem& aSeedContact)
       
    57 	{
       
    58 	iDestroyOnExit=ETrue;
       
    59 	iContactItem=CContactCard::NewL(&aSeedContact);
       
    60 	}
       
    61 
       
    62 void CTestContact::ConstructL(CContactItem* aSeedContact)
       
    63 	{
       
    64 	iDestroyOnExit=EFalse;
       
    65 	iContactItem=aSeedContact;
       
    66 	}
       
    67 
       
    68 CTestContact::~CTestContact()
       
    69 	{
       
    70 	if (iDestroyOnExit)
       
    71 		delete iContactItem;
       
    72 	iContactItem=NULL;
       
    73 	}
       
    74 
       
    75 CContactItem& CTestContact::ContactItem()
       
    76 	{
       
    77 	return *iContactItem;
       
    78 	}
       
    79 
       
    80 const TPtrC CTestContact::FirstNameL()
       
    81 	{
       
    82 	return TextFieldL(KUidContactFieldGivenName);
       
    83 	}
       
    84 
       
    85 const TPtrC CTestContact::LastNameL()
       
    86 	{
       
    87 	return TextFieldL(KUidContactFieldFamilyName);
       
    88 	}
       
    89 
       
    90 const TPtrC CTestContact::CompanyNameL()
       
    91 	{
       
    92 	return TextFieldL(KUidContactFieldCompanyName);
       
    93 	}
       
    94 
       
    95 const TPtrC CTestContact::JobTitleL()
       
    96 	{
       
    97 	return TextFieldL(KUidContactFieldJobTitle);
       
    98 	}
       
    99 
       
   100 const TPtrC CTestContact::WorkPhoneL()
       
   101 	{
       
   102 	CContentType* contentType=CContentType::NewL();
       
   103 	CleanupStack::PushL(contentType);
       
   104 	contentType->AddFieldTypeL(KUidContactFieldPhoneNumber);
       
   105 	contentType->AddFieldTypeL(KUidContactFieldVCardMapWORK);
       
   106 	contentType->AddFieldTypeL(KUidContactFieldVCardMapVOICE);
       
   107 	const TPtrC text=TextFieldL(*contentType);
       
   108 	CleanupStack::PopAndDestroy(); // contentType.
       
   109 	return text;
       
   110 	}
       
   111 
       
   112 const TPtrC CTestContact::WorkMobileL()
       
   113 	{
       
   114 	CContentType* contentType=CContentType::NewL();
       
   115 	CleanupStack::PushL(contentType);
       
   116 	contentType->AddFieldTypeL(KUidContactFieldPhoneNumber);
       
   117 	contentType->AddFieldTypeL(KUidContactFieldVCardMapWORK);
       
   118 	contentType->AddFieldTypeL(KUidContactFieldVCardMapVOICE);
       
   119 	contentType->AddFieldTypeL(KUidContactFieldVCardMapCELL);
       
   120 	const TPtrC text=TextFieldL(*contentType);
       
   121 	CleanupStack::PopAndDestroy(); // contentType.
       
   122 	return text;
       
   123 	}
       
   124 
       
   125 const TPtrC CTestContact::HomePhoneL()
       
   126 	{
       
   127 	CContentType* contentType=CContentType::NewL();
       
   128 	CleanupStack::PushL(contentType);
       
   129 	contentType->AddFieldTypeL(KUidContactFieldPhoneNumber);
       
   130 	contentType->AddFieldTypeL(KUidContactFieldVCardMapHOME);
       
   131 	contentType->AddFieldTypeL(KUidContactFieldVCardMapVOICE);
       
   132 	const TPtrC text=TextFieldL(*contentType);
       
   133 	CleanupStack::PopAndDestroy(); // contentType.
       
   134 	return text;
       
   135 	}
       
   136 
       
   137 const TPtrC CTestContact::HomeMobileL()
       
   138 	{
       
   139 	CContentType* contentType=CContentType::NewL();
       
   140 	CleanupStack::PushL(contentType);
       
   141 	contentType->AddFieldTypeL(KUidContactFieldPhoneNumber);
       
   142 	contentType->AddFieldTypeL(KUidContactFieldVCardMapHOME);
       
   143 	contentType->AddFieldTypeL(KUidContactFieldVCardMapVOICE);
       
   144 	contentType->AddFieldTypeL(KUidContactFieldVCardMapCELL);
       
   145 	const TPtrC text=TextFieldL(*contentType);
       
   146 	CleanupStack::PopAndDestroy(); // contentType.
       
   147 	return text;
       
   148 	}
       
   149 
       
   150 const TPtrC CTestContact::FaxL()
       
   151 	{
       
   152 	return TextFieldL(KUidContactFieldFax);
       
   153 	}
       
   154 
       
   155 const TPtrC CTestContact::EmailAddressL()
       
   156 	{
       
   157 	return TextFieldL(KUidContactFieldEMail);
       
   158 	}
       
   159 
       
   160 const TPtrC CTestContact::InternetAddressL()
       
   161 	{
       
   162 	return TextFieldL(KUidContactFieldUrl);
       
   163 	}
       
   164 
       
   165 const TPtrC CTestContact::StreetL()
       
   166 	{
       
   167 	return TextFieldL(KUidContactFieldAddress);
       
   168 	}
       
   169 
       
   170 const TPtrC CTestContact::TownL()
       
   171 	{
       
   172 	return TextFieldL(KUidContactFieldLocality);
       
   173 	}
       
   174 
       
   175 const TPtrC CTestContact::CountyL()
       
   176 	{
       
   177 	return TextFieldL(KUidContactFieldRegion);
       
   178 	}
       
   179 
       
   180 const TPtrC CTestContact::PostCodeL()
       
   181 	{
       
   182 	return TextFieldL(KUidContactFieldPostcode);
       
   183 	}
       
   184 
       
   185 const TPtrC CTestContact::CountryL()
       
   186 	{
       
   187 	return TextFieldL(KUidContactFieldCountry);
       
   188 	}
       
   189 
       
   190 const TPtrC CTestContact::NotesL()
       
   191 	{
       
   192 	return TextFieldL(KUidContactFieldNote);
       
   193 	}
       
   194 
       
   195 const TPtrC CTestContact::NickNameL()
       
   196 	{
       
   197 	return TextFieldL(KUidContactFieldSecondName);
       
   198 	}
       
   199 
       
   200 const TPtrC CTestContact::PictureL()
       
   201 	{
       
   202 	return TextFieldL(KUidContactFieldPicture);
       
   203 	}
       
   204 
       
   205 void CTestContact::SetFirstNameL(const TDesC& aFirstName)
       
   206 	{
       
   207  	SetTextFieldL(KUidContactFieldGivenName, aFirstName, KUidContactFieldVCardMapUnusedN);
       
   208 	}
       
   209 
       
   210 void CTestContact::SetLastNameL(const TDesC& aLastName)
       
   211 	{
       
   212  	SetTextFieldL(KUidContactFieldFamilyName, aLastName, KUidContactFieldVCardMapUnusedN);
       
   213 	}
       
   214 
       
   215 void CTestContact::SetCompanyNameL(const TDesC& aCompanyName)
       
   216 	{
       
   217 	SetTextFieldL(KUidContactFieldCompanyName, aCompanyName, KUidContactFieldVCardMapORG);
       
   218 	}
       
   219 
       
   220 void CTestContact::SetJobTitleL(const TDesC& aJobTitle)
       
   221 	{
       
   222 	SetTextFieldL(KUidContactFieldJobTitle,aJobTitle);
       
   223 	}
       
   224 
       
   225 void CTestContact::SetWorkPhoneL(const TDesC& aWorkPhone)
       
   226 	{
       
   227 	CContentType* contentType=CContentType::NewL();
       
   228 	CleanupStack::PushL(contentType);
       
   229 	contentType->AddFieldTypeL(KUidContactFieldPhoneNumber);
       
   230 	contentType->AddFieldTypeL(KUidContactFieldVCardMapWORK);
       
   231 	contentType->AddFieldTypeL(KUidContactFieldVCardMapVOICE);
       
   232 	SetTextFieldL(*contentType,aWorkPhone);
       
   233 	CleanupStack::PopAndDestroy(); // contentType.
       
   234 	}
       
   235 
       
   236 void CTestContact::SetWorkMobileL(const TDesC& aWorkMobile)
       
   237 	{
       
   238 	CContentType* contentType=CContentType::NewL();
       
   239 	CleanupStack::PushL(contentType);
       
   240 	contentType->AddFieldTypeL(KUidContactFieldPhoneNumber);
       
   241 	contentType->AddFieldTypeL(KUidContactFieldVCardMapWORK);
       
   242 	contentType->AddFieldTypeL(KUidContactFieldVCardMapVOICE);
       
   243 	contentType->AddFieldTypeL(KUidContactFieldVCardMapCELL);
       
   244 	SetTextFieldL(*contentType,aWorkMobile);
       
   245 	CleanupStack::PopAndDestroy(); // contentType.
       
   246 	}
       
   247 
       
   248 void CTestContact::SetHomePhoneL(const TDesC& aHomePhone)
       
   249 	{
       
   250 	CContentType* contentType=CContentType::NewL();
       
   251 	CleanupStack::PushL(contentType);
       
   252 	contentType->AddFieldTypeL(KUidContactFieldPhoneNumber);
       
   253 	contentType->AddFieldTypeL(KUidContactFieldVCardMapHOME);
       
   254 	contentType->AddFieldTypeL(KUidContactFieldVCardMapVOICE);
       
   255 	SetTextFieldL(*contentType,aHomePhone);
       
   256 	CleanupStack::PopAndDestroy(); // contentType.
       
   257 	}
       
   258 
       
   259 void CTestContact::SetHomeMobileL(const TDesC& aHomeMobile)
       
   260 	{
       
   261 	CContentType* contentType=CContentType::NewL();
       
   262 	CleanupStack::PushL(contentType);
       
   263 	contentType->AddFieldTypeL(KUidContactFieldPhoneNumber);
       
   264 	contentType->AddFieldTypeL(KUidContactFieldVCardMapHOME);
       
   265 	contentType->AddFieldTypeL(KUidContactFieldVCardMapVOICE);
       
   266 	contentType->AddFieldTypeL(KUidContactFieldVCardMapCELL);
       
   267 	SetTextFieldL(*contentType,aHomeMobile);
       
   268 	CleanupStack::PopAndDestroy(); // contentType.
       
   269 	}
       
   270 
       
   271 void CTestContact::SetFaxL(const TDesC& aFax)
       
   272 	{
       
   273 	SetTextFieldL(KUidContactFieldFax,aFax);
       
   274 	}
       
   275 
       
   276 void CTestContact::SetHomeEmailAddressL(const TDesC& aEmailAddress)
       
   277 	{
       
   278 	CContentType* contentType=CContentType::NewL();
       
   279 	CleanupStack::PushL(contentType);
       
   280 	contentType->AddFieldTypeL(KUidContactFieldEMail);
       
   281 	contentType->AddFieldTypeL(KUidContactFieldVCardMapHOME);
       
   282 	SetTextFieldL(*contentType,aEmailAddress);
       
   283 	CleanupStack::PopAndDestroy(contentType);
       
   284 	}
       
   285 
       
   286 void CTestContact::SetWorkEmailAddressL(const TDesC& aEmailAddress)
       
   287 	{
       
   288 	CContentType* contentType=CContentType::NewL();
       
   289 	CleanupStack::PushL(contentType);
       
   290 	contentType->AddFieldTypeL(KUidContactFieldEMail);
       
   291 	contentType->AddFieldTypeL(KUidContactFieldVCardMapWORK);
       
   292 	SetTextFieldL(*contentType,aEmailAddress);
       
   293 	CleanupStack::PopAndDestroy(contentType);
       
   294 	}
       
   295 
       
   296 
       
   297 void CTestContact::SetInternetAddressL(const TDesC& aInternetAddress)
       
   298 	{
       
   299 	SetTextFieldL(KUidContactFieldUrl,aInternetAddress);
       
   300 	}
       
   301 
       
   302 void CTestContact::SetStreetL(const TDesC& aStreet)
       
   303 	{
       
   304 	SetTextFieldL(KUidContactFieldAddress,aStreet);
       
   305 	}
       
   306 
       
   307 void CTestContact::SetTownL(const TDesC& aTown)
       
   308 	{
       
   309 	SetTextFieldL(KUidContactFieldLocality,aTown);
       
   310 	}
       
   311 
       
   312 void CTestContact::SetCountyL(const TDesC& aCounty)
       
   313 	{
       
   314 	SetTextFieldL(KUidContactFieldRegion,aCounty);
       
   315 	}
       
   316 
       
   317 void CTestContact::SetPostCodeL(const TDesC& aPostCode)
       
   318 	{
       
   319 	SetTextFieldL(KUidContactFieldPostcode,aPostCode);
       
   320 	}
       
   321 
       
   322 void CTestContact::SetCountryL(const TDesC& aCountry)
       
   323 	{
       
   324 	SetTextFieldL(KUidContactFieldCountry,aCountry);
       
   325 	}
       
   326 
       
   327 void CTestContact::SetNotesL(const TDesC& aNotes)
       
   328 	{
       
   329 	SetTextFieldL(KUidContactFieldNote,aNotes);
       
   330 	}
       
   331 
       
   332 void CTestContact::SetNickNameL(const TDesC& aNickName)
       
   333 	{
       
   334 	CContentType* contentType=CContentType::NewL();
       
   335 	CleanupStack::PushL(contentType);
       
   336 	contentType->AddFieldTypeL(KUidContactFieldSecondName);
       
   337 	TRAPD(err, SetTextFieldL(*contentType,aNickName));
       
   338         if (!err) { }
       
   339 	CleanupStack::PopAndDestroy(); // contentType.
       
   340 	}
       
   341 
       
   342 void CTestContact::SetPictureL(const TDesC8& aPicture)
       
   343 	{
       
   344 	CContentType* contentType=CContentType::NewL();
       
   345 	CleanupStack::PushL(contentType);
       
   346 	contentType->AddFieldTypeL(KUidContactFieldPicture);
       
   347 	SetPictureFieldL(aPicture);
       
   348 	CleanupStack::PopAndDestroy(contentType);
       
   349 	}
       
   350 
       
   351 const TPtrC CTestContact::TextFieldL(TFieldType aFieldType) const
       
   352 	{
       
   353 	CContactItemField& field=FindFieldL(aFieldType);
       
   354 	ASSERT(field.StorageType()==KStorageTypeText);
       
   355 	return STATIC_CAST(CContactTextField*,(field.Storage()))->Text();
       
   356 	}
       
   357 
       
   358 const TPtrC CTestContact::TextFieldL(const CContentType& aTypesToMatch) const
       
   359 	{
       
   360 	CContactItemField& field=FindFieldL(aTypesToMatch);
       
   361 	ASSERT(field.StorageType()==KStorageTypeText);
       
   362 	return STATIC_CAST(CContactTextField*,field.Storage())->Text();
       
   363 	}
       
   364 
       
   365 void CTestContact::InsertTextFieldL(TFieldType aID, const TDesC& aStringOfStuff )
       
   366 	{
       
   367  	CContactItemField* itemFieldPtr = NULL;
       
   368  	CContactTextField* textStoragePtr = NULL;
       
   369 
       
   370  	itemFieldPtr = CContactItemField::NewLC( KStorageTypeText, aID );
       
   371  	textStoragePtr = itemFieldPtr->TextStorage(); // we don't have ownership here, hence no stack.
       
   372 
       
   373  	textStoragePtr->SetTextL( aStringOfStuff );
       
   374  	textStoragePtr = NULL; // we've finished with this ptr now.
       
   375 
       
   376  	iContactItem->AddFieldL( *itemFieldPtr ); // hand ownership over here
       
   377  	CleanupStack::Pop( itemFieldPtr ); // and pop,
       
   378 	}
       
   379 
       
   380 void CTestContact::SetTextFieldL(TFieldType aFieldType,const TDesC& aText)
       
   381 	{
       
   382  	TInt	fieldIndex=FindField(aFieldType);
       
   383 
       
   384  	if ( fieldIndex > KErrNotFound )
       
   385 		{
       
   386  		CContactItemField&	field=FindFieldL(aFieldType);
       
   387  		ASSERT(field.StorageType()==KStorageTypeText);
       
   388  		STATIC_CAST(CContactTextField*,field.Storage())->SetText(aText.AllocL());
       
   389 		}
       
   390  	else
       
   391 		{
       
   392  		InsertTextFieldL(aFieldType, aText);
       
   393 		}
       
   394 	}
       
   395 
       
   396 void CTestContact::SetTextFieldL(const CContentType& aTypesToMatch,const TDesC& aText) const
       
   397 	{
       
   398 	CContactItemField& field=FindFieldL(aTypesToMatch);
       
   399 	ASSERT(field.StorageType()==KStorageTypeText);
       
   400 	STATIC_CAST(CContactTextField*,field.Storage())->SetText(aText.AllocL());
       
   401 	}
       
   402 
       
   403 CContactItemField& CTestContact::FindFieldL(TFieldType aFieldType) const
       
   404 	{
       
   405 	CContactItemFieldSet& fields=iContactItem->CardFields();
       
   406 	TInt index=fields.Find(aFieldType);
       
   407 	User::LeaveIfError(index);
       
   408 	return fields[index];
       
   409 	}
       
   410 
       
   411 void CTestContact::SetTextFieldL(TFieldType aFieldType, const TDesC& aText, TUid aAddOnContentType)
       
   412 	{
       
   413 	CContactItemFieldSet& fields = iContactItem->CardFields();
       
   414 	CContactTextField* textField = NULL;
       
   415 	TInt fieldIndex = 0;
       
   416 
       
   417 	fieldIndex = FindField( aFieldType );
       
   418 
       
   419 	if (fieldIndex == KErrNotFound )
       
   420 		{
       
   421 		InsertTextFieldL( aFieldType, aText );
       
   422 		fieldIndex = FindField( aFieldType );
       
   423 		CContactItemField& field = fields[ fieldIndex ];
       
   424 		field.AddFieldTypeL( aAddOnContentType );
       
   425 		}
       
   426 		else if (fieldIndex >= KErrNone )
       
   427 		{
       
   428 		CContactItemField& field = fields[ fieldIndex ];
       
   429 		ASSERT(field.StorageType()==KStorageTypeText);
       
   430 		textField = static_cast<CContactTextField*>(field.Storage());
       
   431 		textField->SetText( aText.AllocL() );
       
   432 		field.AddFieldTypeL( aAddOnContentType );
       
   433 		}
       
   434 	}
       
   435 
       
   436 TInt CTestContact::FindField(TFieldType aFieldType) const
       
   437 	{
       
   438 	return iContactItem->CardFields().Find( aFieldType );
       
   439 	}
       
   440 
       
   441 CContactItemField& CTestContact::FindFieldL(const CContentType& aTypesToMatch) const
       
   442 	{
       
   443 	CContactItemFieldSet& fields=iContactItem->CardFields();
       
   444 	const TInt numTypesToMatch(aTypesToMatch.FieldTypeCount());
       
   445 	const TFieldType mainFieldType(aTypesToMatch.FieldType(0));
       
   446 	TInt index=-1;
       
   447 	FOREVER
       
   448 		{
       
   449 		index=fields.FindNext(mainFieldType,index+1);
       
   450 		User::LeaveIfError(index);
       
   451 
       
   452 		CContactItemField& thisPhoneField=fields[index];
       
   453 		const CContentType& thisPhoneFieldTypes=thisPhoneField.ContentType();
       
   454 		const TInt numFieldTypes(thisPhoneFieldTypes.FieldTypeCount());
       
   455 
       
   456 		if (numFieldTypes!=numTypesToMatch)
       
   457 			{
       
   458 			continue;
       
   459 			}
       
   460 
       
   461 		TInt numTypesLeftToMatch=numTypesToMatch;
       
   462 		for (TInt ii=0;ii<numFieldTypes;++ii)
       
   463 			{
       
   464 			TFieldType thisFieldType(thisPhoneFieldTypes.FieldType(ii));
       
   465 			for (TInt jj=0;jj<numTypesToMatch;++jj)
       
   466 				{
       
   467 				TFieldType thisFieldTypeToMatch(aTypesToMatch.FieldType(jj));
       
   468 				if (thisFieldType==thisFieldTypeToMatch)
       
   469 					{
       
   470 					if (--numTypesLeftToMatch<=0)
       
   471 						{
       
   472 						return thisPhoneField;
       
   473 						}
       
   474 
       
   475 					break;
       
   476 					}
       
   477 				}
       
   478 			}
       
   479 		}
       
   480 	}
       
   481 
       
   482 void CTestContact::SetPictureFieldL(const TDesC8& aPicture) const
       
   483 	{
       
   484 
       
   485 	CContactItemFieldSet& fieldSet=iContactItem->CardFields();
       
   486 	TInt pos;
       
   487 	pos=fieldSet.Find(KUidContactFieldPicture);
       
   488 	User::LeaveIfError(pos);
       
   489 	fieldSet[pos].StoreStorage()->SetThingL(aPicture);
       
   490 
       
   491 	}