messagingfw/biomsgfw/BITSSRC/BioTestUtils.cpp
changeset 0 8e480a14352b
child 58 6c34d0baa0b1
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2003-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 // BioTestUtils.h
       
    15 //
       
    16 
       
    17 #include "biotestutils.h"
       
    18 
       
    19 #include <e32test.h>
       
    20 #include <f32fsys.h>
       
    21 #include <s32file.h>
       
    22 #include <e32uid.h>
       
    23 #include <txtrich.h>
       
    24 #include <txtfmlyr.h>
       
    25 #include <msvids.h>
       
    26 #include <msvuids.h>
       
    27 #include <msvruids.h>
       
    28 #include <e32std.h>
       
    29 #include <e32math.h>
       
    30 #include <mtclbase.h>
       
    31 #include <mtclreg.h>
       
    32 #include <msvapi.h>
       
    33 #include <mtmuids.h>
       
    34 
       
    35 #include <smuthdr.h>
       
    36 #include <biouids.h>
       
    37 #include <biodb.h>
       
    38 #include <biouids.h>
       
    39 #include <regpsdll.h>
       
    40 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS 
       
    41 #include <tmsvsmsentry.h>
       
    42 #include <bifchangeobserver.h>
       
    43 #include <biomessageuids.h>
       
    44 #include "tmsvbioinfo.h"
       
    45 #endif
       
    46 
       
    47 #define KCharSpace          ' '
       
    48 
       
    49 EXPORT_C CBioTestUtils::~CBioTestUtils()
       
    50 	{
       
    51 	Reset();
       
    52 	delete iBioDb;
       
    53 	}
       
    54 
       
    55 EXPORT_C CBioTestUtils* CBioTestUtils::NewL(RTest& aRTest)
       
    56 	{
       
    57 	CBioTestUtils* self = new (ELeave) CBioTestUtils(aRTest);
       
    58 	CleanupStack::PushL(self);
       
    59 	self->ConstructL();
       
    60 	CleanupStack::Pop();
       
    61 	return self;
       
    62 	}
       
    63 
       
    64 EXPORT_C CBioTestUtils* CBioTestUtils::NewLC(RTest& aRTest)
       
    65 	{
       
    66 	CBioTestUtils* self = new(ELeave) CBioTestUtils(aRTest);
       
    67 	CleanupStack::PushL(self);
       
    68 	self->ConstructL();
       
    69 	return self;
       
    70 	}
       
    71 
       
    72 EXPORT_C CBioTestUtils* CBioTestUtils::NewL(RTest& aRTest, TInt aCreationFlags)
       
    73 	{
       
    74 	CBioTestUtils* self = NewLC(aRTest, aCreationFlags);
       
    75 	CleanupStack::Pop();
       
    76 	return self;
       
    77 	}
       
    78 
       
    79 EXPORT_C CBioTestUtils* CBioTestUtils::NewLC(RTest& aRTest, TInt aCreationFlags)
       
    80 	{
       
    81 	CBioTestUtils* self = new(ELeave) CBioTestUtils(aRTest);
       
    82 	CleanupStack::PushL(self);
       
    83 	self->ConstructL(aCreationFlags);
       
    84 	return self;
       
    85 	}
       
    86 
       
    87 void CBioTestUtils::Reset()
       
    88 	{
       
    89 	if(iTestParsedFieldArray)
       
    90 		{
       
    91 		iTestParsedFieldArray->ResetAndDestroy();
       
    92 		delete iTestParsedFieldArray;
       
    93 		iTestParsedFieldArray=NULL;
       
    94 		}
       
    95 	if(iMessageBody)
       
    96 		{
       
    97 		delete iMessageBody;
       
    98 		iMessageBody = NULL;
       
    99 		}
       
   100 	if(iDir)
       
   101 		{
       
   102 		delete iDir;
       
   103 		iDir = NULL;
       
   104 		}
       
   105 	if(iRegisteredParserDll)
       
   106 		{
       
   107 		delete iRegisteredParserDll;
       
   108 		iRegisteredParserDll = NULL;
       
   109 		}
       
   110 	if (iBioClientMtm)
       
   111 		{
       
   112 		delete iBioClientMtm;
       
   113 		iBioClientMtm=NULL;
       
   114 		}
       
   115 	if (iBioServerMtm)
       
   116 		{
       
   117 		delete iBioServerMtm;
       
   118 		iBioServerMtm = NULL;
       
   119 		iServerEntry = NULL;
       
   120 		}
       
   121 
       
   122 	iNumFiles = 0;
       
   123 	iFilesProcessed = 0;
       
   124 	iMessagesCreated = 0;
       
   125 
       
   126 	CMsvTestUtils::Reset();
       
   127 	}
       
   128 
       
   129 //
       
   130 // Register a Sms Mtm server DLL
       
   131 //
       
   132 EXPORT_C void CBioTestUtils::CreateBioServerMtmRegL()
       
   133 	{
       
   134 	CreateServerMtmRegL(KUidMsgTypeSmartMessage, _L("BIO MTM"),KBioServerMtmFileName(), KBioClientMtmFileName(), TMsvTestDllInfo(KBioUiMtmFileName,2), TMsvTestDllInfo(KBioUiMtmFileName,1), KUidMsgTypeSmartMessage, KDataComponentFileName);
       
   135 	}
       
   136 
       
   137 CBioTestUtils::CBioTestUtils(RTest& aRTest)
       
   138 : CMsvTestUtils(aRTest)
       
   139 	{
       
   140 	iBioServiceId = KMsvNullIndexEntryId;
       
   141 	}
       
   142 
       
   143 void CBioTestUtils::ConstructL()
       
   144 	{
       
   145 	TInt flags=NULL; // Settings flags for the base ConstructL() function
       
   146 
       
   147 	//	Loading the DLLs
       
   148 	iRTest.Next(_L("Create Data Component FileStores"));
       
   149 
       
   150 	CMsvTestUtils::ConstructL(flags);
       
   151 
       
   152 	iBioDb = CBIODatabase::NewL(iFs);
       
   153 
       
   154 	FileSession().SetSessionPath(_L("c:\\"));
       
   155 	}
       
   156 
       
   157 // Overloaded constructor - allows developer to decide what components to create
       
   158 void CBioTestUtils::ConstructL(TInt aCreationFlags)
       
   159 	{
       
   160 	iRTest.Next(_L("Initialising Data Components"));
       
   161 	// 
       
   162 	CMsvTestUtils::ConstructL(aCreationFlags);
       
   163 
       
   164 	iBioDb = CBIODatabase::NewL(iFs);
       
   165 	FileSession().SetSessionPath(_L("c:\\"));
       
   166 	}
       
   167 
       
   168 
       
   169 void CBioTestUtils::Panic(TBioTestUtilsPanic aPanic)
       
   170 	{
       
   171 	Panic((TInt) aPanic);
       
   172 	}
       
   173 
       
   174 void CBioTestUtils::Panic(TInt aPanic)
       
   175 	{
       
   176 	User::Panic(BIOTEST_PANIC, aPanic);
       
   177 	}
       
   178 
       
   179 EXPORT_C TMsvId CBioTestUtils::CreateBIOEntryL(TDesC& aText, TBIOMessageType aMessageType )
       
   180 	{
       
   181 	// Ensure that we have a valid service ID to work with:
       
   182 	if (iBioServiceId==0)
       
   183 		iBioServiceId = SetBIOServiceIdL();
       
   184 
       
   185 	// First take the descriptor reference and create a new descriptor to manipulate.
       
   186 	// Then convert '\n' to the paragraph delimiters used in proper SMS.
       
   187 	HBufC* localBuffer = aText.AllocL();
       
   188 	CleanupStack::PushL(localBuffer);
       
   189 
       
   190 	TPtr messDes = localBuffer->Des();
       
   191 
       
   192 	if (aMessageType != EBiovCardMessage && aMessageType != EBiovCalenderMessage)
       
   193 		{
       
   194 		// convert \r\n to \n since this is what is expected from SMS when not vCard data
       
   195 		for (TInt i=0; i<messDes.Length(); i++)
       
   196 			{
       
   197 			if (messDes[i]==(TText)'\r' && i<messDes.Length()-1 && messDes[i+1]==(TText)'\n')
       
   198 				messDes.Delete(i,1); 
       
   199 			} 
       
   200 		}
       
   201 
       
   202 	//	Create and fill a CRichText object for the jobbie:
       
   203 	CParaFormatLayer* paraFormatLayer = CParaFormatLayer::NewL();
       
   204 	CleanupStack::PushL(paraFormatLayer);
       
   205 	CCharFormatLayer* charFormatLayer = CCharFormatLayer::NewL();
       
   206 	CleanupStack::PushL(charFormatLayer);
       
   207 	CRichText* richText = CRichText::NewL(paraFormatLayer,charFormatLayer);
       
   208 	CleanupStack::PushL(richText);
       
   209 
       
   210 	TInt pos = richText->DocumentLength();	// Ought to be 0.
       
   211 	richText->InsertL(pos, messDes);
       
   212 	
       
   213 	TMsvEntry newBioEntry;
       
   214 	newBioEntry.SetNew(ETrue);
       
   215 	newBioEntry.SetComplete(EFalse);
       
   216 	newBioEntry.SetUnread(ETrue);
       
   217 	newBioEntry.SetVisible(ETrue);
       
   218 	newBioEntry.SetReadOnly(EFalse);
       
   219 	newBioEntry.SetFailed(EFalse);
       
   220 	newBioEntry.SetOperation(EFalse);
       
   221 	newBioEntry.SetMultipleRecipients(EFalse);
       
   222 	newBioEntry.SetAttachment(EFalse);
       
   223 	newBioEntry.iMtm= KUidBIOMessageTypeMtm;
       
   224 	newBioEntry.iType=KUidMsvMessageEntry;
       
   225 	newBioEntry.iServiceId=iBioServiceId;
       
   226 	TTime now;
       
   227 	now.UniversalTime();
       
   228 	newBioEntry.iDate = now;
       
   229 
       
   230 	newBioEntry.iDescription.Set(richText->Read(0, richText->DocumentLength()));
       
   231 	TBufC<KTelephoneNumberMaxLength> telNumber;
       
   232 	HBufC* tempNumber = CreateDummyPhoneNumberL();
       
   233 	telNumber = tempNumber->Des();
       
   234 	newBioEntry.iDetails.Set(telNumber);
       
   235 	// Finished with telNumber, so free up the memory:
       
   236 	delete tempNumber; 
       
   237 
       
   238 	// Change the iServiceId, iMtm, iBioType  for message:
       
   239 	SetForMtmTypeL(newBioEntry, aMessageType);
       
   240 	// Update the size to that of the attachment length  - correct? 
       
   241 	// Otherwise just get 68
       
   242 	newBioEntry.iSize = richText->DocumentLength();// msgSize;
       
   243 	iClientServer == EServerSide ? CreateBioEntryServerSideL(newBioEntry,*richText) : CreateBioEntryClientSideL(newBioEntry,*richText);
       
   244 
       
   245 	CleanupStack::PopAndDestroy(4); // richText, charFormatLayer, paraFormatLayer, localBuffer
       
   246 	return newBioEntry.Id(); 
       
   247 	}
       
   248 
       
   249 void CBioTestUtils::SetForMtmTypeL(TMsvEntry& aEntry, TBIOMessageType aType)
       
   250 	{
       
   251 	TInt index;
       
   252 	// Should have a match for the Message Type as the message is created with the SMS id's.
       
   253 	switch(aType)
       
   254 		{
       
   255 		case ENoMessage:
       
   256 			// Just to keep compiler happy!
       
   257 			break;
       
   258 		case EBioIapSettingsMessage:
       
   259 			aEntry.iBioType  = KUidBIOInternetAccessPointMsg;
       
   260 			break;
       
   261 		case EBioEnpMessage:
       
   262 			aEntry.iBioType =KUidBIOEmailNotificationMsg;
       
   263 			break;
       
   264 		case EBioRingTonesMessage:
       
   265 			aEntry.iBioType =KUidBIORingingTonesMsg;
       
   266 			break;
       
   267 		case EBioOpLogoMessage:
       
   268 			aEntry.iBioType =KUidBIOOperatorLogoMsg;
       
   269 			break;
       
   270 		case EBioCompBusCardMessage:
       
   271 			aEntry.iBioType =KUidBIOBusinessCardMsg;
       
   272 			break;
       
   273 		case EBiovCardMessage:
       
   274 			aEntry.iBioType =KUidBIOVCardMsg;
       
   275 			break;
       
   276 		case EBiovCalenderMessage:
       
   277 			aEntry.iBioType =KUidBIOVCalenderMsg;
       
   278 			break;
       
   279 		case EBioWAPSettingsMessage:
       
   280 			aEntry.iBioType =KUidBIOWAPAccessPointMsg;
       
   281 			break;
       
   282 		case EBioWPRVSettingsMessage:
       
   283 			aEntry.iBioType =KUidBIOWPRVMsg;
       
   284 			break;
       
   285 		}
       
   286 	// Set other TMsvEntry fields if we have a BIO message type
       
   287 	if (aType != ENoMessage)
       
   288 		{
       
   289 		aEntry.SetMtmData2(0);
       
   290 		aEntry.SetMtmData3(0);
       
   291 		}
       
   292 
       
   293 	// set description from BIF file
       
   294 	iBioDb->GetBioIndexWithMsgIDL(TUid::Uid(aEntry.iBioType),index);
       
   295 	if(index==KErrNotFound)
       
   296 		User::Leave(KErrNotFound);
       
   297 	aEntry.iDescription.Set(iBioDb->BifReader(index).Description());
       
   298 	}
       
   299 
       
   300 EXPORT_C void CBioTestUtils::LogCurrentMessageTypeL(TMsvId aMsvId)
       
   301 	{
       
   302 	// assume we're already pointing at a BIO message
       
   303 	TMsvEntry entry;
       
   304 	TMsvId oldId;
       
   305 	if(iClientServer == EServerSide)
       
   306 		{
       
   307 		oldId = iServerEntry->Entry().Id();
       
   308 		User::LeaveIfError(iServerEntry->SetEntry(aMsvId));
       
   309 		entry = iServerEntry->Entry();
       
   310 		}
       
   311 	else
       
   312 		{
       
   313 		oldId = iMsvEntry->Entry().Id();
       
   314 		iMsvEntry->SetEntryL(aMsvId);
       
   315 		entry = iMsvEntry->Entry();
       
   316 		}
       
   317 
       
   318 	TInt biotype = entry.iBioType;
       
   319 	if(biotype == KUidBIOInternetAccessPointMsg)
       
   320 		{
       
   321 		switch(entry.MtmData1())
       
   322 			{
       
   323 		case 1:
       
   324 			WriteComment(_L("IACP basic IAP message"));
       
   325 			break;
       
   326 		case 2:
       
   327 			WriteComment(_L("IACP basic mail message"));
       
   328 			break;
       
   329 		case 3:
       
   330 			WriteComment(_L("IACP basic IAP & mail message"));
       
   331 			break;
       
   332 		case 7:
       
   333 			WriteComment(_L("IACP Internet script message"));
       
   334 			break;
       
   335 		case 8:
       
   336 			WriteComment(_L("IACP SMS settings message"));
       
   337 			break;
       
   338 		case 15:
       
   339 			WriteComment(_L("Telephone settings message"));
       
   340 			break;
       
   341 		default:
       
   342 			WriteComment(_L("Unsupported IACP type"));
       
   343 			break;
       
   344 			}
       
   345 		}
       
   346 	else if(biotype == KUidBIOEmailNotificationMsg)
       
   347 			WriteComment(_L("Email notification message"));
       
   348 	else if(biotype == KUidBIORingingTonesMsg)
       
   349 			WriteComment(_L("Ringing tones message"));
       
   350 	else if(biotype == KUidBIOOperatorLogoMsg)
       
   351 			WriteComment(_L("Operator logo message"));
       
   352 	else if(biotype == KUidBIOBusinessCardMsg)
       
   353 			WriteComment(_L("Compact Business Card message"));
       
   354 	else if(biotype == KUidBIOVCardMsg)
       
   355 			WriteComment(_L("vCard message"));
       
   356 	else if(biotype == KUidBIOVCalenderMsg)
       
   357 			WriteComment(_L("vCalendar"));
       
   358 	else if(biotype == KUidBIOWAPAccessPointMsg)
       
   359 			WriteComment(_L("WAP settings message"));
       
   360 	else
       
   361 		WriteComment(_L("Unknown type"));
       
   362 
       
   363 	if(iClientServer == EServerSide)
       
   364 		{
       
   365 		User::LeaveIfError(iServerEntry->SetEntry(oldId));
       
   366 		}
       
   367 	else
       
   368 		{
       
   369 		iMsvEntry->SetEntryL(oldId);
       
   370 		}
       
   371 	}
       
   372 
       
   373 EXPORT_C TMsvId CBioTestUtils::CreateBIOEntryFromFileL(const TDesC& aFilename, TBIOMessageType aMessageType)
       
   374 	{
       
   375 	//First use the filename to get the message body, 
       
   376 	// then convert '\n' to the paragraph delimiters used in proper SMS	
       
   377 	HBufC* tempBuffer;
       
   378 	TMsvId newEntryId;
       
   379 	tempBuffer = ReadFromFileL(aFilename);
       
   380 	CleanupStack::PushL(tempBuffer);
       
   381 
       
   382 	newEntryId = CreateBIOEntryL(*tempBuffer, aMessageType );
       
   383 
       
   384 	// Finished with our local descriptors - free up some memory
       
   385 	CleanupStack::PopAndDestroy();  
       
   386 	return newEntryId;
       
   387 	}
       
   388 
       
   389 
       
   390 EXPORT_C void CBioTestUtils::EmptyInboxMessagesL()
       
   391 	{
       
   392 	// Function gets the Inbox Entry, and then all the children of this entry.
       
   393 	// Then we iterate through the selection deleting each entry.
       
   394 	CMsvEntrySelection* inboxMessageSel;
       
   395 	if(iClientServer == EServerSide)
       
   396 		{
       
   397 		inboxMessageSel = new (ELeave) CMsvEntrySelection;
       
   398 		CleanupStack::PushL(inboxMessageSel);
       
   399 		// Create a local Server entry, and get the children of the Global Inbox:
       
   400 		User::LeaveIfError(iServerEntry->SetEntry(KMsvGlobalInBoxIndexEntryId));	
       
   401 		iServerEntry->GetChildren(*inboxMessageSel ); // children of parent, ie global inbox
       
   402 		if (inboxMessageSel->Count() != 0)
       
   403 			{
       
   404 			iServerEntry->DeleteEntries(*inboxMessageSel);
       
   405     		}
       
   406 		//	Point back at something safe before we return:
       
   407 		User::LeaveIfError(iServerEntry->SetEntry(KMsvRootIndexEntryId));
       
   408 		}
       
   409 	else
       
   410 		{
       
   411 		iMsvEntry->SetEntryL(KMsvGlobalInBoxIndexEntryId);
       
   412 		inboxMessageSel = iMsvEntry->ChildrenL();
       
   413 		CleanupStack::PushL(inboxMessageSel);
       
   414 		if (inboxMessageSel->Count() != 0)
       
   415 			{
       
   416 			TInt count;
       
   417 			TMsvId currentMsgId;
       
   418 			for (count = 0; count < inboxMessageSel->Count(); count++)
       
   419 				{
       
   420 				currentMsgId= inboxMessageSel->At(count);
       
   421 				// Delete this entry
       
   422 				DeleteLocalEntrySynchronouslyL(*iMsvEntry, currentMsgId);
       
   423 				}
       
   424     		}
       
   425 		iMsvEntry->SetEntryL(KMsvRootIndexEntryId);
       
   426 		}
       
   427 	CleanupStack::PopAndDestroy();	// inboxMessageSel
       
   428 	}
       
   429 
       
   430 void CBioTestUtils::DeleteLocalEntrySynchronouslyL(CMsvEntry& aParentEntry, TMsvId anId)
       
   431 	{ 
       
   432 	//  Creates an entry synchronously by handling the asynchrous parts locally
       
   433  
       
   434 	const TMsvSelectionOrdering& originalSort=aParentEntry.SortType();
       
   435 	TMsvSelectionOrdering sort(originalSort);
       
   436 	sort.SetShowInvisibleEntries(ETrue);
       
   437 	aParentEntry.SetSortTypeL(sort);
       
   438 	CMsvOperationWait* myDog=CMsvOperationWait::NewLC();
       
   439 	CMsvOperation* op=aParentEntry.DeleteL(anId, myDog->iStatus);
       
   440 	myDog->Start();
       
   441 	CActiveScheduler::Start();
       
   442 	const TInt err=op->iStatus.Int();
       
   443 	TMsvLocalOperationProgress progress=McliUtils::GetLocalProgressL(*op);
       
   444 	delete op;
       
   445 	User::LeaveIfError(err);
       
   446 	User::LeaveIfError(progress.iError);
       
   447 	CleanupStack::PopAndDestroy();
       
   448 	aParentEntry.SetSortTypeL(originalSort);
       
   449 	}
       
   450 //*************************************************************************
       
   451 //
       
   452 //		Getting BIO service ID
       
   453 //
       
   454 //*************************************************************************
       
   455 
       
   456 // Get,or create, the default BIO service:
       
   457 EXPORT_C TMsvId CBioTestUtils::SetBIOServiceIdL()
       
   458 	{
       
   459 	// If Bio service Id is none zero its already been set - just return the value:
       
   460 	if (iBioServiceId)
       
   461 		return iBioServiceId;
       
   462 
       
   463 	if(iClientServer == EServerSide)
       
   464 		SetBioServiceServerSideL();
       
   465 	else
       
   466 		SetBioServiceClientSideL();
       
   467 	return iBioServiceId;
       
   468 	}
       
   469 
       
   470 void CBioTestUtils::SetBioServiceServerSideL()
       
   471 	{
       
   472 	// Set up the RootEntry sort order first:
       
   473 	TMsvSelectionOrdering oldSortType = iServerEntry->Sort();
       
   474 	TMsvSelectionOrdering sortType(KMsvGroupByType, EMsvSortByNone, ETrue);	
       
   475 	iServerEntry->SetSort(sortType);
       
   476 
       
   477 	// Search the root folder for any entries which are BIO Message services.
       
   478 	// If none are found then create one.
       
   479 	TMsvId localServiceId=KMsvNullIndexEntryId;
       
   480 	localServiceId = DefaultServiceForMTML(KUidBIOMessageTypeMtm, ETrue);
       
   481 
       
   482 	// Check that the Service id is valid, i.e. set iCurrentEntry context to service entry:
       
   483 	TRAPD( error, iServerEntry->SetEntry(localServiceId) );
       
   484 	if ( (localServiceId == KMsvUnknownServiceIndexEntryId)  || (error == KErrNotFound) )
       
   485 		{
       
   486 		// Id isn't a valid BIO service, so try and get the first BIO service:
       
   487 		CMsvEntrySelection* sel = GetListOfAccountsWithMTMServerL(KUidBIOMessageTypeMtm);
       
   488 		CleanupStack::PushL(sel);
       
   489 		if (sel->Count())
       
   490 			iBioServiceId=(*sel)[0];	// Select the first available service.
       
   491 		CleanupStack::PopAndDestroy();	// sel
       
   492 		if (localServiceId == KMsvUnknownServiceIndexEntryId || localServiceId == KMsvNullIndexEntryId)
       
   493 			// Haven't found any valid BIO service, so try and create a BIO service:
       
   494 			localServiceId=CreateDefaultBIOServiceL();
       
   495 		}
       
   496 		else
       
   497 			User::LeaveIfError(error);  // An error other than no service occurred.
       
   498 
       
   499 	__ASSERT_DEBUG( localServiceId != KMsvNullIndexEntryId, User::Panic(_L("BioGen"), KErrNotFound));
       
   500 
       
   501 	//	Point back at something safe:
       
   502 	User::LeaveIfError(iServerEntry->SetEntry(KMsvRootIndexEntryId));
       
   503 	iServerEntry->SetSort(oldSortType);
       
   504 
       
   505 	iBioServiceId = localServiceId;
       
   506 	}
       
   507 
       
   508 void CBioTestUtils::SetBioServiceClientSideL()
       
   509 	{
       
   510 	iMsvEntry->SetSortTypeL(TMsvSelectionOrdering(KMsvGroupByType, EMsvSortByNone, ETrue));
       
   511 #if defined (_DEBUG)
       
   512 	if (iMsvEntry->EntryId() != KMsvRootIndexEntryId)  //iMsvEntry should be set to root
       
   513 		User::Leave(KErrNotFound);	//replaces a panic - ENotAServiceEntry
       
   514 #endif
       
   515 
       
   516 	TMsvId localServiceId=KMsvNullIndexEntryId;
       
   517 	localServiceId=DefaultServiceForMTML(KUidBIOMessageTypeMtm, ETrue);
       
   518 
       
   519 	//Check if there is a valid entry for this ID
       
   520 	TRAPD(ret, /*const TMsvEntry& ent=*/iMsvEntry->ChildDataL(localServiceId));
       
   521 
       
   522 	if (localServiceId == KMsvUnknownServiceIndexEntryId || ret==KErrNotFound)
       
   523 		{
       
   524 		// Our Id isn't a valid BIO service, possibly because there isn't a default 
       
   525 		// BIO service.  So try and get the first BIO service.
       
   526 		CMsvEntrySelection* sel=GetListOfAccountsWithMTMClientL(KUidBIOMessageTypeMtm);
       
   527 		if (sel->Count())
       
   528 			localServiceId=(*sel)[0]; // select the first available service
       
   529 		delete sel;
       
   530 		if (localServiceId == KMsvUnknownServiceIndexEntryId || localServiceId == KMsvNullIndexEntryId)
       
   531 			// No SMS service present, so let's create one 
       
   532 			CreateServicesL();
       
   533 		}
       
   534 	else
       
   535 		User::LeaveIfError(ret);  // an error other than no service occurred
       
   536 
       
   537 #if defined (_DEBUG)
       
   538 	if (localServiceId == KMsvNullIndexEntryId)
       
   539 		User::Leave(KErrNotFound);	//replaces a panic - ENotAServiceEntry
       
   540 #endif
       
   541 	iBioServiceId = localServiceId;
       
   542 	}
       
   543 
       
   544 TMsvId CBioTestUtils::CreateDefaultBIOServiceL() 
       
   545 	{
       
   546 	//	Haven't found an entry so create a BIO Message service:
       
   547 	TMsvEntry bioServiceEntry;
       
   548 	bioServiceEntry.iMtm = KUidBIOMessageTypeMtm;
       
   549 	bioServiceEntry.iType = KUidMsvServiceEntry;
       
   550 	bioServiceEntry.SetVisible(EFalse);
       
   551 	bioServiceEntry.iDate.UniversalTime();
       
   552 	bioServiceEntry.iDescription.Set(_L("BIO Message Service "));	// Is there such a thing?
       
   553 	bioServiceEntry.iDetails.Set(_L("BIO Message Service"));
       
   554 
       
   555 //	no need to set default service for v2, CBaseMtm 'knows' that for services 
       
   556 //	where there is a single service entry, it has to be the default
       
   557 	TMsvId newBIOServiceId;
       
   558 	if(iClientServer == EServerSide)
       
   559 		{
       
   560 		User::LeaveIfError(iServerEntry->SetEntry(KMsvRootIndexEntryId));
       
   561 		User::LeaveIfError(iServerEntry->CreateEntry(bioServiceEntry)); // Needs to be a child of the root!
       
   562 		newBIOServiceId =bioServiceEntry.Id();
       
   563 
       
   564 		}
       
   565 	else
       
   566 		{
       
   567 		iMsvEntry->SetEntryL(KMsvRootIndexEntryId);
       
   568 		iMsvEntry->CreateL(bioServiceEntry); // Needs to be a child of the root!
       
   569 		newBIOServiceId =bioServiceEntry.Id();
       
   570 
       
   571 		}
       
   572 
       
   573 	// No need to set Server Entry - still pointing at the root.
       
   574 	return newBIOServiceId;
       
   575 	}
       
   576 
       
   577 
       
   578 TMsvId CBioTestUtils::DefaultServiceForMTML(TUid aMtmUid, TBool aFindFirstServiceIfNoDefault)
       
   579 	{
       
   580 	TMsvId serviceId=KMsvUnknownServiceIndexEntryId;
       
   581 	if(iClientServer == EServerSide)
       
   582 		serviceId = DefaultServiceServerL(aMtmUid,aFindFirstServiceIfNoDefault);
       
   583 	else
       
   584 		serviceId = DefaultServiceClientL(aMtmUid,aFindFirstServiceIfNoDefault);
       
   585 	return serviceId;
       
   586 	}
       
   587 
       
   588 TMsvId CBioTestUtils::DefaultServiceServerL(TUid aMtmUid, TBool /*aFindFirstServiceIfNoDefault*/)
       
   589 	{
       
   590 	TMsvId serviceId=KMsvUnknownServiceIndexEntryId;
       
   591 	User::LeaveIfError(iServerEntry->SetEntry(KMsvRootIndexEntryId));
       
   592 
       
   593 		// Find first available service with the desired iMtm value:
       
   594 		CMsvEntrySelection* sel=new (ELeave) CMsvEntrySelection;
       
   595 		CleanupStack::PushL(sel);
       
   596 
       
   597 		iServerEntry->GetChildren(*sel);
       
   598 		
       
   599 		for (TInt count = 0; serviceId != 0 && count < sel->Count(); count++)
       
   600 			{
       
   601 			User::LeaveIfError(iServerEntry->SetEntry(sel->At(count)));
       
   602 			if (iServerEntry->Entry().iMtm == aMtmUid)
       
   603 				serviceId = sel->At(count);
       
   604 			}
       
   605 		CleanupStack::PopAndDestroy(); //  sel
       
   606 
       
   607 	//	Point back at something safe:
       
   608 	User::LeaveIfError(iServerEntry->SetEntry(KMsvRootIndexEntryId));
       
   609 
       
   610 	return serviceId;
       
   611 	}
       
   612 
       
   613 TMsvId CBioTestUtils::DefaultServiceClientL(TUid /*aMtmUid*/, TBool /*aFindFirstServiceIfNoDefault*/)
       
   614 	{
       
   615 	if (!iBioClientMtm)
       
   616 		{
       
   617 		InstantiateClientMtmsL();
       
   618 		}
       
   619 	return iBioClientMtm->DefaultServiceL();
       
   620 	}
       
   621 
       
   622 CMsvEntrySelection* CBioTestUtils::GetListOfAccountsWithMTMServerL(TUid aMtmUid)
       
   623 	{
       
   624 	// Gets a list of entries with the passed in MTM.
       
   625 	CMsvEntrySelection*	 returnSelection =new(ELeave) CMsvEntrySelection();
       
   626 	CleanupStack::PushL(returnSelection);
       
   627 
       
   628 	CMsvEntrySelection*	 rootKin = new (ELeave) CMsvEntrySelection();
       
   629 	CleanupStack::PushL(rootKin);
       
   630 
       
   631 	CMsvEntrySelection*	 entryKin = new (ELeave) CMsvEntrySelection();
       
   632 	CleanupStack::PushL(entryKin);
       
   633 	
       
   634 	TMsvSelectionOrdering  oldSortType = iServerEntry->Sort();
       
   635 	TMsvSelectionOrdering	sortType(KMsvGroupByType|KMsvGroupByStandardFolders, EMsvSortByDetailsReverse, ETrue);
       
   636 
       
   637 	// iCurrentEntry should be pointing towards the root, but make sure:
       
   638 	User::LeaveIfError(iServerEntry->SetEntry(KMsvRootIndexEntryIdValue));
       
   639 	iServerEntry->SetSort(sortType);
       
   640 	iServerEntry->GetChildren(*rootKin);  // Get the child entries of the root.
       
   641 
       
   642 	TBool visible=EFalse;
       
   643 	TInt err;
       
   644 	if ( rootKin->Count() !=0)
       
   645 		{
       
   646 		TMsvEntry tentry;
       
   647 		for (TInt cc=rootKin->Count(); --cc>=0; ) // N.B. decrements prior to entering loop.
       
   648 			{
       
   649 			// Set the context of our Server Entry to the next child entry of the root:
       
   650 			User::LeaveIfError(iServerEntry->SetEntry(rootKin->At(cc)) );
       
   651 			// Get the entry and check its mtm type:
       
   652 			tentry =iServerEntry->Entry();   
       
   653 			if (tentry.iMtm==aMtmUid)
       
   654 				{
       
   655 				// Is it a proper service entry?
       
   656 				if (tentry.iType.iUid==KUidMsvServiceEntryValue && tentry.Id()!=KMsvLocalServiceIndexEntryIdValue)
       
   657 					{
       
   658 					// If it's visible or it has no entry in the index - add to our return array:
       
   659 					if (tentry.Visible() || tentry.iRelatedId==KMsvNullIndexEntryId)
       
   660 						returnSelection->AppendL(tentry.Id());
       
   661 					else if (tentry.iRelatedId!=tentry.Id())
       
   662 						{
       
   663 						// Check if there are any related entries:
       
   664 						TRAP(err, iServerEntry->GetChildren(*entryKin) );
       
   665 						TInt count = entryKin->Count();
       
   666 						while (count--)
       
   667 							{
       
   668 							User::LeaveIfError(iServerEntry->SetEntry( entryKin->At(count) ) );
       
   669 							if (iServerEntry->Entry().Id() == tentry.iRelatedId)
       
   670 								break;
       
   671 							}
       
   672 						visible=iServerEntry->Entry().Visible();
       
   673 						if (err==KErrNone && !visible && returnSelection->Find(tentry.iRelatedId)!=KErrNone)
       
   674 							returnSelection->AppendL(tentry.Id());
       
   675 						}
       
   676 					}
       
   677 				}
       
   678 			}
       
   679 		}		
       
   680 
       
   681 	//	Point back at something safe:
       
   682 	User::LeaveIfError(iServerEntry->SetEntry(KMsvRootIndexEntryId));
       
   683 	iServerEntry->SetSort(oldSortType);
       
   684 	CleanupStack::PopAndDestroy(2); // entryKin, rootKin, 	
       
   685 	CleanupStack::Pop(); // returnSelection 
       
   686 
       
   687 	return returnSelection;
       
   688 	}
       
   689 
       
   690 CMsvEntrySelection* CBioTestUtils::GetListOfAccountsWithMTMClientL(TUid aMtmUid)
       
   691 	{
       
   692 	// Gets a list of entries with the passed in MTM
       
   693 	CMsvEntrySelection*	 returnSelection =new(ELeave) CMsvEntrySelection();
       
   694 	CleanupStack::PushL(returnSelection);
       
   695 
       
   696 	CMsvEntrySelection*	 rootKin;
       
   697 	CMsvEntrySelection*	 entryKin;
       
   698 
       
   699 	TMsvSelectionOrdering  oldSortType = iMsvEntry->SortType();
       
   700 	TMsvSelectionOrdering	sortType(KMsvGroupByType|KMsvGroupByStandardFolders, EMsvSortByDetailsReverse, ETrue);
       
   701 
       
   702 	// iCurrentEntry should be pointing towards the root, but make sure
       
   703 	iMsvEntry->SetEntryL(KMsvRootIndexEntryIdValue);
       
   704 	iMsvEntry->SetSortTypeL(sortType);
       
   705 	rootKin=iMsvEntry->ChildrenL();  // Get the child entries of the root
       
   706 	CleanupStack::PushL(rootKin);
       
   707 
       
   708 	TBool visible=EFalse;
       
   709 
       
   710 	if ( rootKin->Count() !=0)
       
   711 		{
       
   712 		TMsvEntry tentry;
       
   713 		for (TInt cc=rootKin->Count(); --cc>=0; ) // NB decrements prior to entering loop
       
   714 			{
       
   715 			// Set the context of our Server Entry to the next child entry of the root
       
   716 			iMsvEntry->SetEntryL(rootKin->At(cc)) ;
       
   717 			// get the entry and check it's mtm type
       
   718 			tentry =iMsvEntry->Entry();   
       
   719 			if (tentry.iMtm==aMtmUid)
       
   720 				{
       
   721 				// Is it a proper service entry
       
   722 				if (tentry.iType.iUid==KUidMsvServiceEntryValue && tentry.Id()!=KMsvLocalServiceIndexEntryIdValue)
       
   723 					{
       
   724 					// if it's visible or it has no entry in the index - add to our return array
       
   725 					if (tentry.Visible() || tentry.iRelatedId==KMsvNullIndexEntryId)
       
   726 						returnSelection->AppendL(tentry.Id());
       
   727 					else if (tentry.iRelatedId!=tentry.Id())
       
   728 						{
       
   729 						// Check if there are any related entries
       
   730 						entryKin=iMsvEntry->ChildrenL() ;
       
   731 						CleanupStack::PushL(entryKin);
       
   732 
       
   733 						TInt count = entryKin->Count();
       
   734 						while (count--)
       
   735 							{
       
   736 							iMsvEntry->SetEntryL( entryKin->At(count) );
       
   737 							if (iMsvEntry->Entry().Id() == tentry.iRelatedId)
       
   738 								break;
       
   739 							}
       
   740 						visible=iMsvEntry->Entry().Visible();
       
   741 						if (!visible && returnSelection->Find(tentry.iRelatedId)!=KErrNone)
       
   742 							returnSelection->AppendL(tentry.Id());
       
   743 						CleanupStack::PopAndDestroy(); // entryKin 	
       
   744 						}
       
   745 					}
       
   746 				}
       
   747 			}
       
   748 		}		
       
   749 
       
   750 	//	Point back at something safe.....
       
   751 	iMsvEntry->SetEntryL(KMsvRootIndexEntryId);
       
   752 	iMsvEntry->SetSortTypeL(oldSortType);
       
   753 	CleanupStack::PopAndDestroy(); // entryKin, rootKin, 	
       
   754 	CleanupStack::Pop(); //returnSelection 
       
   755 
       
   756 	return returnSelection;
       
   757 	}
       
   758 
       
   759 
       
   760 //*****************************************************************************
       
   761 //
       
   762 // File Read and Message Creation functions
       
   763 //
       
   764 //*****************************************************************************
       
   765 EXPORT_C void CBioTestUtils::SetSessionPath(const TDesC& aSessionPath)
       
   766 	{
       
   767     iFs.SetSessionPath(aSessionPath);
       
   768 	}
       
   769 
       
   770 HBufC* CBioTestUtils::ReadFromFileL(const TDesC& aFile)
       
   771 	{
       
   772 	// Reads in data from a file and loads it into a buffer which is later used
       
   773 	// to create an SMS message in the  Inbox.  This function differs from the others
       
   774 	// in that it also gets any From: field by locating the token in the file. 
       
   775 	// NOTE: There is an inherent assumption that the From: token and the field will
       
   776 	// be on a single line in the text file (i.e. less than 1024 characters.
       
   777 	RFile           file;
       
   778 	TBuf8<1024>     lineBuffer;   // Buffer for Read function.
       
   779 	TInt			err=KErrNone;
       
   780 
       
   781 	err = file.Open(iFs,aFile,EFileStreamText|EFileRead|EFileShareAny);
       
   782 
       
   783     if(err != KErrNone)  // Didn't find the file, so leave - should only get valid filenames!
       
   784         {
       
   785         User::Leave(KErrNotFound);
       
   786 		}
       
   787 
       
   788 	// In real life this is set by the Socket Port Observer, or the SMS server.
       
   789 	// So,in this test code we must do it by creating a random  telephone number.
       
   790 	//	iMessageDetails = CreateDummyPhoneNumberL();
       
   791 
       
   792     HBufC*	bioBuf=HBufC::NewLC(65535); // Create a new descriptor on the heap.
       
   793 	HBufC*	copyBuffer=HBufC::NewLC(1024);
       
   794 
       
   795     do // Read in the text from file, and also check if there is a name field:
       
   796 		{
       
   797 		err = file.Read(lineBuffer);// Read upto 256 chars, '\n' and all...
       
   798 		//err = ReadLineL(file,lineBuffer);
       
   799 		if (err==KErrNone)				// Made a valid read,
       
   800 			if (lineBuffer.Length()==0) // but read 0 chars
       
   801 				err=KErrEof;			// so set err value to end processing
       
   802 
       
   803 		if(err == KErrNone)
       
   804 			{
       
   805 			copyBuffer->Des().Copy(lineBuffer);// Copy, and overwrite existing text
       
   806 			if( (bioBuf->Length() + copyBuffer->Length()) > bioBuf->Des().MaxLength() )
       
   807 					{
       
   808 					bioBuf = bioBuf->ReAllocL(bioBuf->Length() + copyBuffer->Length());
       
   809 					}
       
   810 			bioBuf->Des().Append(*copyBuffer);
       
   811             //bioBuf->Des().Append(_L("\n"));
       
   812 			}
       
   813 		}
       
   814     while(err != KErrEof);
       
   815 
       
   816 	CleanupStack::PopAndDestroy(); // Destroy the copyBuffer.
       
   817 	CleanupStack::Pop();// Remove the bioBuf.
       
   818     
       
   819 	file.Close();
       
   820 	return bioBuf;  // Transfer ownership and responsibility of the buffer.
       
   821 	}
       
   822 
       
   823 
       
   824 
       
   825 //*****************************************************************************
       
   826 //
       
   827 // Creating a dummy phone number for use in messages
       
   828 //
       
   829 //*****************************************************************************
       
   830 
       
   831 
       
   832 
       
   833 HBufC* CBioTestUtils::CreateDummyPhoneNumberL()
       
   834 	{
       
   835 	HBufC* number= HBufC::NewL(32); // Note: function doesn't own this, up to the caller to delete
       
   836 	TInt64 phoneNo;
       
   837 	TInt64 seed;
       
   838 	TTime tempTime;
       
   839 	tempTime.UniversalTime();
       
   840 	seed=tempTime.Int64();
       
   841 	phoneNo= Math::Rand(seed);
       
   842 	if (phoneNo <0)
       
   843 		phoneNo = -phoneNo;
       
   844 	number->Des().Num( phoneNo); 
       
   845 	number->Des().Insert(0,_L("+44"));// Add a leading '+' to make if an international numbers
       
   846 	return number;
       
   847 	}
       
   848 
       
   849 
       
   850 void CBioTestUtils::CreateBioEntryServerSideL(TMsvEntry& aEntry, CRichText& aBody)
       
   851 	{
       
   852 	//	Get the global inbox:
       
   853 	User::LeaveIfError(iServerEntry->SetEntry(KMsvGlobalInBoxIndexEntryId));
       
   854 	// Entry Context already set, create our entry:
       
   855 	User::LeaveIfError(iServerEntry->CreateEntry(aEntry)); 
       
   856 	// Set our context to the the new entry so we can create a store:
       
   857 	User::LeaveIfError(iServerEntry->SetEntry(aEntry.Id()) );
       
   858 	// Save all the changes:
       
   859 	CMsvStore* store;
       
   860 	store=iServerEntry->EditStoreL();
       
   861 	CleanupStack::PushL(store);
       
   862 
       
   863 	if (store->HasBodyTextL())
       
   864 		{
       
   865 		store->DeleteBodyTextL();
       
   866 		}
       
   867 	store->StoreBodyTextL(aBody);
       
   868 	store->CommitL();
       
   869 	CleanupStack::PopAndDestroy(); // Close and destroy the store.
       
   870 	// Set context to the new entry to allow updating of  the entry:
       
   871 	aEntry.SetComplete(ETrue);
       
   872 	iServerEntry->ChangeEntry(aEntry);
       
   873 	User::LeaveIfError(iServerEntry->SetEntry(KMsvRootIndexEntryId));
       
   874 	}
       
   875 
       
   876 void CBioTestUtils::CreateBioEntryClientSideL(TMsvEntry& aEntry, CRichText& aBody)
       
   877 	{
       
   878 		//	Get the global inbox.	
       
   879 	iMsvEntry->SetEntryL(KMsvGlobalInBoxIndexEntryId);
       
   880 	iMsvEntry->CreateL(aEntry); 
       
   881 	iMsvEntry->SetEntryL(aEntry.Id());
       
   882 
       
   883 	// Save all the changes
       
   884 	CMsvStore* store;
       
   885 	store=iMsvEntry->EditStoreL();
       
   886 	CleanupStack::PushL(store);
       
   887 
       
   888 	if (store->HasBodyTextL())
       
   889 		{
       
   890 		store->DeleteBodyTextL();
       
   891 		}
       
   892 	store->StoreBodyTextL(aBody);
       
   893 	store->CommitL();
       
   894 
       
   895 	CleanupStack::PopAndDestroy(); //store - close the store
       
   896 
       
   897 	aEntry.SetComplete(ETrue);
       
   898 	// Update the entry
       
   899 	iMsvEntry->ChangeL(aEntry);
       
   900 	iMsvEntry->SetEntryL(KMsvRootIndexEntryId);
       
   901 	}
       
   902 
       
   903 void CBioTestUtils::InstantiateClientMtmsL()
       
   904 	{
       
   905 	// client side
       
   906 	__ASSERT_ALWAYS(iMsvSession, User::Panic(BIOTEST_PANIC, KErrBIONotOnClientSide));
       
   907 	if(iClientMtmRegistry)
       
   908 		delete iClientMtmRegistry;
       
   909 	iClientMtmRegistry = CClientMtmRegistry::NewL(*iMsvSession);
       
   910 	iBioClientMtm = (CBIOClientMtm*) iClientMtmRegistry->NewMtmL(KUidMsgTypeSmartMessage);
       
   911 	}
       
   912 
       
   913 void CBioTestUtils::InstantiateServerMtmsL()
       
   914 	{
       
   915 	iBioServerMtm = (CBIOServerMtm*) InstantiateServerMtmL(KUidBIOMessageTypeMtm, iBioServiceId);
       
   916 	const CMsvEntrySelection* selection = new(ELeave)CMsvEntrySelection();
       
   917 	TRequestStatus st;
       
   918 	TMsvId s =0;
       
   919 	TRAP_IGNORE(iBioServerMtm->CopyFromLocalL(*selection,s, st));
       
   920 	TRAP_IGNORE(iBioServerMtm->CopyToLocalL(*selection,s, st));
       
   921 	TRAP_IGNORE(iBioServerMtm->CopyWithinServiceL(*selection,s, st));
       
   922 	TRAP_IGNORE(iBioServerMtm->MoveFromLocalL(*selection,s, st));
       
   923 	TRAP_IGNORE(iBioServerMtm->MoveToLocalL(*selection,s, st));
       
   924 	TRAP_IGNORE(iBioServerMtm->MoveWithinServiceL(*selection,s, st));
       
   925 	TRAP_IGNORE(iBioServerMtm->DeleteAllL(*selection,st));
       
   926 	
       
   927 	TInt intw =0;
       
   928 		
       
   929 	TRAP_IGNORE(iBioServerMtm->Cancel());
       
   930 	iBioServerMtm->CommandExpected();
       
   931 	TMsvEntry entry;
       
   932 	
       
   933 	TRAP_IGNORE(iBioServerMtm->ChangeL(entry,st));
       
   934 	TRAP_IGNORE(iBioServerMtm->CreateL(entry,st));
       
   935 	delete selection;
       
   936 	}
       
   937 
       
   938 void CBioTestUtils::DeleteServicesL()
       
   939 	{
       
   940 	DeleteServiceL(KUidBIOMessageTypeMtm);
       
   941 	}
       
   942 
       
   943 void CBioTestUtils::CreateServicesL()
       
   944 	{
       
   945 	iBioServiceId = CreateServiceL(KUidMsgTypeSmartMessage);
       
   946 	}
       
   947 
       
   948 void CBioTestUtils::FindExistingServicesL()
       
   949 	{
       
   950 	iBioServiceId = 0;
       
   951 	TRAPD(err,ServiceIdL(KUidMsgTypeSmartMessage, iBioServiceId));
       
   952 	if (err)
       
   953 		Printf(_L("No existing SMS services found!\n"));
       
   954 	}
       
   955 
       
   956 void CBioTestUtils::InstallMtmGroupsL()
       
   957 	{
       
   958 	InstallMtmGroupL(KDataComponentFileName);
       
   959 	}
       
   960 
       
   961 void CBioTestUtils::CreateServerMtmRegsL()
       
   962 	{
       
   963 	CreateBioServerMtmRegL();
       
   964 	}
       
   965 
       
   966 //
       
   967 // stuff to generate test BIO messages
       
   968 //
       
   969 
       
   970 EXPORT_C CMsvEntrySelection* CBioTestUtils::GenerateMessagesL()
       
   971 	{
       
   972 	return GenerateMessagesL(KBIOTxtFilePath);
       
   973 	}
       
   974 
       
   975 EXPORT_C CMsvEntrySelection*  CBioTestUtils::GenerateMessagesL(const TDesC& aFileDirectory, TBool aLogCreationDetails)
       
   976 	{
       
   977 	TBufC<KMaxFileName> currentFile;
       
   978 	TMsvId messageId;
       
   979 	TBIOMessageType currentMsgType;
       
   980 
       
   981 	CMsvEntrySelection* selection = new(ELeave)CMsvEntrySelection();
       
   982 	CleanupStack::PushL(selection);
       
   983 
       
   984 	TInt err = iFs.GetDir(aFileDirectory, KEntryAttMatchMask, ESortByName, iDir);
       
   985 	if (err == KErrPathNotFound)
       
   986 		{
       
   987 		TInt makeDirErr = iFs.MkDirAll(aFileDirectory);	
       
   988 		makeDirErr==KErrNone ? User::Leave(KErrNotFound): User::Leave(makeDirErr);
       
   989 		}
       
   990 	else if (err!=KErrNone)
       
   991 		{
       
   992 		User::Leave(err);
       
   993 		}
       
   994 
       
   995 	// Set the session path for the RFs
       
   996 	SetSessionPath(aFileDirectory);
       
   997 	if (iDir) 
       
   998 		delete iDir;
       
   999 	User::LeaveIfError(iFs.GetDir(_L("*.txt"), KEntryAttNormal, ESortByName, iDir));
       
  1000     TInt count = iDir->Count();
       
  1001     if(count == 0)
       
  1002         {
       
  1003         User::Leave(KErrNotFound);   // No files to process
       
  1004         }
       
  1005 
       
  1006 	TBuf<60> outputBuf;
       
  1007 
       
  1008 	for(TInt loop = 0; loop < count; loop++)
       
  1009 		{
       
  1010 		currentFile=( (*iDir)[iFilesProcessed].iName );
       
  1011 		// Not processed all the messages - so keep the current state
       
  1012 		iFilesProcessed++;   // Here because need to update the counter promptly
       
  1013 		currentMsgType = SetMessageType(currentFile);
       
  1014 		if (currentMsgType!=ENoMessage) // skip any dodgy filenames
       
  1015 			{
       
  1016 			messageId = CreateBIOEntryFromFileL( currentFile,currentMsgType);
       
  1017 			selection->AppendL(messageId);
       
  1018 			TPtrC tempPtr = (currentFile.Des() );
       
  1019 			if (aLogCreationDetails)
       
  1020 				{
       
  1021 				outputBuf.Format(_L("TMsvId  %d   -    %S"), messageId, &tempPtr);
       
  1022 				WriteComment(outputBuf);
       
  1023 				}
       
  1024 			}
       
  1025 		}
       
  1026 	CleanupStack::Pop(); // selection
       
  1027 	return selection;
       
  1028     }
       
  1029 
       
  1030 EXPORT_C TBIOMessageType CBioTestUtils::SetMessageType(const TDesC& aFileName)
       
  1031 	{
       
  1032 
       
  1033 	// Check each file prefix in turn with the filename passed as a parameter. 
       
  1034 	// If cannot find the correct type set to ENoMessage to indicate an error. 
       
  1035 	// GenerateNextMessage will then skip the file!
       
  1036 
       
  1037 	if (aFileName.MatchF(KBIOIapPrefix)==0)  // File starts with "iap"
       
  1038 		{
       
  1039 		return EBioIapSettingsMessage;
       
  1040 		}
       
  1041 	if(aFileName.MatchF(KBIOEnpPrefix)==0) // File name starts "enp"
       
  1042 		{
       
  1043 		return EBioEnpMessage;
       
  1044 		}
       
  1045 	if (aFileName.MatchF(KBIORingTonePrefix)==0)//Filename begins "rtone"
       
  1046 		{
       
  1047 		return EBioRingTonesMessage;
       
  1048 		}
       
  1049 	if (aFileName.MatchF(KBIOOpLogoPrefix)==0)	// Filename begins "oplogo"
       
  1050 		{
       
  1051 		return EBioOpLogoMessage;
       
  1052 		}
       
  1053 	if (aFileName.MatchF(KBIOcBusinessCardPrefix)==0)// Filename begins "cbc"
       
  1054 		{
       
  1055 		return EBioCompBusCardMessage;
       
  1056 		}
       
  1057 	if (aFileName.MatchF(KBIOvCardPrefix)==0)
       
  1058 		{
       
  1059 		return EBiovCardMessage;
       
  1060 		}
       
  1061 	if (aFileName.MatchF(KBIOvCalenderPrefix)==0)
       
  1062 		{
       
  1063 		return EBiovCalenderMessage;
       
  1064 		}
       
  1065 	if (aFileName.MatchF(KBIOWappPrefix)==0)
       
  1066 		{
       
  1067 		return EBioWAPSettingsMessage;
       
  1068 		}
       
  1069 	// if we've reached this point it's an unknown filename 
       
  1070 	return ENoMessage;
       
  1071 	}
       
  1072 
       
  1073 EXPORT_C void CBioTestUtils::DoAppendVariantName(TDes& aFileName)
       
  1074 	{
       
  1075 #if defined(__WINS__) && defined(__WINSCW__)
       
  1076 	aFileName.Append(_L(".WINSCW."));
       
  1077 #elif(__WINS__)
       
  1078 	aFileName.Append(_L(".WINS."));
       
  1079 #endif
       
  1080 #if (defined(__THUMB__) || defined(__MARM_THUMB__))
       
  1081 	aFileName.Append(_L(".THUMB."));
       
  1082 #endif
       
  1083 #if (defined(__ARMI__) || defined(__MARM_ARMI__))
       
  1084 	aFileName.Append(_L(".ARMI."));
       
  1085 #endif
       
  1086 #if (defined(__ARM4__) || defined(__MISA__) || defined(__MARM_ARM4__))
       
  1087 	aFileName.Append(_L(".ARM4."));
       
  1088 #endif
       
  1089 
       
  1090 #if defined(_DEBUG)
       
  1091 	aFileName.Append(_L("DEB."));
       
  1092 #else
       
  1093 	aFileName.Append(_L("REL."));
       
  1094 #endif
       
  1095 
       
  1096 	aFileName.Append(_L("LOG"));
       
  1097 	}
       
  1098 
       
  1099 //
       
  1100 // Create a return the appropriate parser for the biomsg with id 
       
  1101 //
       
  1102 EXPORT_C CBaseScriptParser2* CBioTestUtils::CreateParserL(TMsvId aBioMsgId)
       
  1103 	{
       
  1104 	TMsvEntry myBioEntry;
       
  1105 
       
  1106 	if(iClientServer==EClientSide)
       
  1107 		{
       
  1108 		iMsvEntry->SetEntryL(aBioMsgId);
       
  1109 		myBioEntry = iMsvEntry->Entry();
       
  1110 		}
       
  1111 	else
       
  1112 		{
       
  1113 		User::Leave(KErrNotSupported);
       
  1114 		}
       
  1115 
       
  1116 	// check its the right type
       
  1117 	if(myBioEntry.iMtm.iUid != KUidMsgTypeSmartMessage.iUid)
       
  1118 		User::Leave(KBspInvalidMessage);
       
  1119 
       
  1120 	return CreateParserTypeL(myBioEntry.iBioType);
       
  1121 	}
       
  1122 
       
  1123 EXPORT_C CBaseScriptParser2* CBioTestUtils::CreateParserTypeL(TInt32 aBioMsgType)
       
  1124 	{
       
  1125 	// get the bio type and create the parser
       
  1126 	TUid messageUid = TUid::Uid(aBioMsgType);
       
  1127 
       
  1128 	// look up identification for parser
       
  1129 	TFileName parserDllName(iBioDb->GetBioParserNameL(messageUid));
       
  1130 
       
  1131 	if (iRegisteredParserDll)  // Already have an old RegParserDll, delete it to stop memory leak.
       
  1132 		{
       
  1133 		delete iRegisteredParserDll;
       
  1134 		iRegisteredParserDll = NULL;
       
  1135 		}
       
  1136 
       
  1137     iRegisteredParserDll = CRegisteredParserDll::NewL(parserDllName); // Create a new RegParserDll
       
  1138 
       
  1139     RLibrary parserlibrary;
       
  1140     User::LeaveIfError(iRegisteredParserDll->GetLibrary(iFs, parserlibrary));
       
  1141 
       
  1142     typedef CBaseScriptParser2* (*NewParserL)(CRegisteredParserDll& aRegisteredParserDll, CMsvEntry& aEntry, RFs& aFs);
       
  1143 
       
  1144     TInt entrypointordinalnumber=1; // The one and only entry point
       
  1145     TLibraryFunction libFunc=parserlibrary.Lookup(entrypointordinalnumber);
       
  1146     if (libFunc==NULL)
       
  1147         User::Leave(KErrBadLibraryEntryPoint);
       
  1148     NewParserL pFunc=(NewParserL) libFunc;
       
  1149     TInt refcount=iRegisteredParserDll->DllRefCount();
       
  1150     CBaseScriptParser2* parser=NULL;
       
  1151     TRAPD(ret,parser=((*pFunc)(*iRegisteredParserDll, *iMsvEntry, iFs)));
       
  1152     if ((ret!=KErrNone) && (iRegisteredParserDll->DllRefCount()==refcount))
       
  1153         iRegisteredParserDll->ReleaseLibrary();
       
  1154 
       
  1155 	User::LeaveIfError(ret);
       
  1156     return parser;
       
  1157 	}
       
  1158 
       
  1159 EXPORT_C TPtrC CBioTestUtils::MessageBodyL(TMsvId aBioMsgId)
       
  1160 	{
       
  1161 	CMsvStore* store = NULL;
       
  1162 	if(iClientServer==EServerSide)
       
  1163 		{
       
  1164 		User::LeaveIfError(iServerEntry->SetEntry(aBioMsgId));
       
  1165 		if(iServerEntry->Entry().iMtm.iUid != KUidMsgTypeSmartMessage.iUid)
       
  1166 			User::Leave(KBspInvalidMessage);
       
  1167 		store =  iServerEntry->ReadStoreL();
       
  1168 		}
       
  1169 	else
       
  1170 		{
       
  1171 		iMsvEntry->SetEntryL(aBioMsgId);
       
  1172 		if(iMsvEntry->Entry().iMtm.iUid != KUidMsgTypeSmartMessage.iUid)
       
  1173 			User::Leave(KBspInvalidMessage);
       
  1174 		store = iMsvEntry->ReadStoreL();
       
  1175 		}
       
  1176     //  Extract the body text or leave false if not
       
  1177 	CleanupStack::PushL(store);
       
  1178 	CParaFormatLayer* paraFormatLayer = CParaFormatLayer::NewL();
       
  1179     CleanupStack::PushL(paraFormatLayer);
       
  1180     CCharFormatLayer* charFormatLayer = CCharFormatLayer::NewL();
       
  1181     CleanupStack::PushL(charFormatLayer);
       
  1182     CRichText* richText = CRichText::NewL(paraFormatLayer, charFormatLayer);
       
  1183     CleanupStack::PushL(richText);
       
  1184 
       
  1185 	store->HasBodyTextL() ? store->RestoreBodyTextL(*richText) : User::Leave( KErrNotFound);
       
  1186 	
       
  1187 	TInt messageLength = richText->DocumentLength();
       
  1188 
       
  1189 	delete iMessageBody;
       
  1190 	iMessageBody = HBufC::NewL(messageLength);
       
  1191 	TPtr messDes = iMessageBody->Des();
       
  1192 
       
  1193 	TInt length = messDes.Length();
       
  1194 	while(length < messageLength)
       
  1195 		{
       
  1196 		TPtrC desc = richText->Read(length, messageLength-length);
       
  1197 		messDes.Append(desc);
       
  1198 		length+=desc.Length();
       
  1199 		}
       
  1200 
       
  1201 	CleanupStack::PopAndDestroy(4);	// store, text, charFormatLayer, paraFormatLayer 
       
  1202 	return messDes;
       
  1203 	}
       
  1204 
       
  1205 EXPORT_C void CBioTestUtils::LogExtractedFieldsL(TMsvId aMessage)
       
  1206 	{
       
  1207 	// make sure we've got the right entry
       
  1208 	CMsvStore* store = NULL;
       
  1209 	if(iClientServer==EServerSide)
       
  1210 		{
       
  1211 		User::LeaveIfError(iServerEntry->SetEntry(aMessage));
       
  1212 		if(iServerEntry->Entry().iMtm.iUid != KUidMsgTypeSmartMessage.iUid)
       
  1213 			User::Leave(KBspInvalidMessage);
       
  1214 		store =  iServerEntry->ReadStoreL();
       
  1215 		}
       
  1216 	else
       
  1217 		{
       
  1218 		iMsvEntry->SetEntryL(aMessage);
       
  1219 		if(iMsvEntry->Entry().iMtm.iUid != KUidMsgTypeSmartMessage.iUid)
       
  1220 			User::Leave(KBspInvalidMessage);
       
  1221 		store = iMsvEntry->ReadStoreL();
       
  1222 		}
       
  1223     //  Extract the body text or leave false if not
       
  1224 	CleanupStack::PushL(store);
       
  1225 
       
  1226 	// try to get our array of parsed fields
       
  1227 	RMsvReadStream in;
       
  1228 	in.OpenLC( *store, KUidMsvBIODataStream );
       
  1229 	InternalizeL(in);
       
  1230 	CleanupStack::PopAndDestroy();
       
  1231 
       
  1232 	HBufC* fieldBuf  = NULL;
       
  1233 	TInt length = 0;
       
  1234 	WriteComment(_L("Extracted fields"));
       
  1235 	for(TInt loop = 0; loop < iTestParsedFieldArray->Count(); loop++)
       
  1236 		{
       
  1237 		CParsedField& field = *(*iTestParsedFieldArray)[loop];
       
  1238 		length = (field.FieldName().Length() + field.FieldValue().Length() + 1);
       
  1239 
       
  1240 		delete fieldBuf;
       
  1241 		fieldBuf = HBufC::NewL(length);
       
  1242 		TPtr des = fieldBuf->Des();
       
  1243 		des.Append(field.FieldName());
       
  1244 		des.Append(KCharSpace);
       
  1245 		des.Append(field.FieldValue());
       
  1246 		
       
  1247 		//FieldValue may be bigger than '0x100' i.e logging script
       
  1248 		//this may exeede max buffer, so we need to break it into chuncks
       
  1249 		//of 64 chars and log the chuncks in sequence.
       
  1250 		TInt desLength = des.Length();
       
  1251 		TInt BufSize=64;
       
  1252 		if (desLength > BufSize)
       
  1253 			{
       
  1254 			TBuf<64> tempBuf;
       
  1255 			TInt count=0;
       
  1256 			while(count <= desLength)
       
  1257 				{
       
  1258 				if(desLength-count > BufSize )
       
  1259 					tempBuf= des.Mid(count,BufSize);
       
  1260 				else
       
  1261 					tempBuf= des.Mid(count,desLength-count);
       
  1262 
       
  1263 				WriteComment(tempBuf);
       
  1264 				count+=BufSize;
       
  1265 				}
       
  1266 			}
       
  1267 		else
       
  1268 			WriteComment(des);
       
  1269 
       
  1270 		}
       
  1271 	delete fieldBuf;
       
  1272 	CleanupStack::PopAndDestroy();
       
  1273 	}
       
  1274 
       
  1275 
       
  1276 void CBioTestUtils::InternalizeL(RMsvReadStream& aReadStream)
       
  1277 	{
       
  1278 	if(iTestParsedFieldArray)
       
  1279 		{
       
  1280 		iTestParsedFieldArray->ResetAndDestroy();
       
  1281 		delete iTestParsedFieldArray;
       
  1282 		iTestParsedFieldArray=NULL;
       
  1283 		}
       
  1284 
       
  1285 	iTestParsedFieldArray = new(ELeave) CArrayPtrSeg<CParsedField>(16);
       
  1286 
       
  1287 	CParsedField* parsedField = NULL;
       
  1288 	TInt count = aReadStream.ReadUint8L();
       
  1289 	for (TInt i=0; i < count; i++)
       
  1290 		{
       
  1291 		parsedField = new (ELeave) CParsedField();
       
  1292 		TRAPD(err, parsedField->InternalizeL(aReadStream))
       
  1293 		if(err)
       
  1294 			{
       
  1295 			delete parsedField; //deletes the last allocated object, privious ones will be deleted by iParsedFieldArray->ResetAndDestroy()
       
  1296 			User::Leave(err);
       
  1297 			}
       
  1298 		iTestParsedFieldArray->AppendL(parsedField);
       
  1299 		}
       
  1300 	}
       
  1301 
       
  1302 EXPORT_C CArrayPtrSeg<CParsedField>& CBioTestUtils::ParsedFieldArray()
       
  1303 	{
       
  1304 	return *iTestParsedFieldArray;
       
  1305 	}