/**
  Function : doTestStepL
  Description : Fetches the srvice ID using the IMAP account name and sets
  				the full download limit fields of the setting object corresponding to it. 
  @return : TVerdict - Test step result
  @leave  :	KMsvNullIndexEntryId	If valid IMAP account name is not specified or
									If valid remote folder name is not specified
*/
TVerdict CT_MsgSetImap4FullDownloadLimits::doTestStepL()
	{
	INFO_PRINTF1(KInfoTestStepName);
	TPtrC imapAccountName;
	
	/* Reads IMAP Account Name from ini file */
	if(!GetStringFromConfig(ConfigSection(), KImapAccountName, imapAccountName))
		{
		ERR_PRINTF1(KAcntNameNtSpecified);
		SetTestStepResult(EFail);
		}		
	else
		{
		TPtrC destFolderName;

		/* Reads the destination Folder Name */
		if(!GetStringFromConfig(ConfigSection(), KFolderName, destFolderName))
			{
			ERR_PRINTF1(KDestFolderNtSpecified);
			SetTestStepResult(EFail);
			}	
		else
			{
			TInt maxEmailSize = KMaxTInt;
			/* Reads the size of the Email to be set,where default is KMaxTInt */
			GetIntFromConfig(ConfigSection(), KMaxEmailSize, maxEmailSize);
			
			TPtrC mailOption;
			/* Reads the mail option to be set from the ini file */
			if(!GetStringFromConfig(ConfigSection(), KImapMailOption, mailOption))
				{
				ERR_PRINTF1(KGetMailOptNtSpecified);
				SetTestStepResult(EFail);
				}
			else
				{
				TMsvId destFolderId = -1;
				/* Retrieves the IMAP service Id based on the IMAP account name*/
				TMsvId imapServiceId = CT_MsgUtilsCentralRepository::GetImapServiceIdL((TDes&) imapAccountName);
				if(imapServiceId == KMsvNullIndexEntryId)
					{
					ERR_PRINTF2(KAcntNtExist, &imapAccountName);
					SetTestStepResult(EFail);
					}
				else
					{
					/* 
					 * Retrieves the mail options and the remote destination folder Id
					 * based on the IMAP account name and the remote folder name given
					 */
					TImap4GetMailOptions imapMailOption = CT_MsgUtilsEnumConverter::ConvertDesToTImap4GetMailOptions(mailOption);
		
					destFolderId = CT_MsgUtils::GetRemoteFolderIdByNameL(iSharedDataIMAP.iSession,
												imapAccountName,destFolderName);
			
					if(destFolderId == KMsvNullIndexEntryId)
						{
						ERR_PRINTF1(_L("Invalid remote folder name specified"));
						SetTestStepResult(EFail);
						}
					else
						{																						
						INFO_PRINTF2(KFolderId,destFolderId);		
						
						/* Stores the fulll donwload limit values */
						TImImap4GetMailInfo  imap4GetMailInfo;
						imap4GetMailInfo.iMaxEmailSize      = maxEmailSize;	
						imap4GetMailInfo.iGetMailBodyParts  = imapMailOption;
						imap4GetMailInfo.iDestinationFolder = destFolderId;
						
						/* Loads the Imap account settings and save the download limits */
						CEmailAccounts* emailAccounts = CEmailAccounts::NewLC();
						CImImap4Settings* imapSettings =  new(ELeave) CImImap4Settings();
						CleanupStack::PushL(imapSettings);

						TImapAccount imapAccount;
						emailAccounts->GetImapAccountL(imapServiceId, imapAccount);
						emailAccounts->LoadImapSettingsL(imapAccount, *imapSettings);
						
						
						TBool deleteFlag;
						if(GetBoolFromConfig(ConfigSection(),KDeleteFlag,deleteFlag))	
							{
							imapSettings->SetDeleteEmailsWhenDisconnecting(deleteFlag);
							}
						TBool updateFlag;
						if(GetBoolFromConfig(ConfigSection(),KUpdateFlag,updateFlag))	
							{
							imapSettings->SetUpdatingSeenFlags(updateFlag);
							}
							
						TInt fetchSizeLimit = 20480;
						if(GetIntFromConfig(ConfigSection(),KFetchSizeLimit,fetchSizeLimit))
							{
							imapSettings->SetFetchSizeL(fetchSizeLimit);		
							}
							
						TInt inboxSyncLimit;
						if(GetIntFromConfig(ConfigSection(),KInboxSyncLimit,inboxSyncLimit))
							{
							imapSettings->SetInboxSynchronisationLimit(inboxSyncLimit);
							}
						
						TInt mailboxSyncLimit;
						if(GetIntFromConfig(ConfigSection(),KMailboxSyncLimit,mailboxSyncLimit))
							{
							imapSettings->SetMailboxSynchronisationLimit(mailboxSyncLimit);
							}
						
						TPtrC searchString;
						if(GetStringFromConfig(ConfigSection(),KSearchString,searchString))
							{
							HBufC8* string = HBufC8::NewLC(searchString.Length());
							string->Des().Copy(searchString);
							imapSettings->SetSearchStringL(string->Des());
							CleanupStack::PopAndDestroy(string);
							}
						
						TPtrC folderSubTypeName;
						if(GetStringFromConfig(ConfigSection(),KFolderSubType,folderSubTypeName))
							{
							TFolderSubscribeType folderSubType =  CT_MsgUtilsEnumConverter::ConvertDesToTFolderSubscribeType
												((TDesC&)folderSubTypeName);
							imapSettings->SetSubscribe(folderSubType);
							}
							
						TPtrC folderSyncTypeName;
						if(GetStringFromConfig(ConfigSection(),KFolderSyncType,folderSyncTypeName))
							{
							TFolderSyncType  folderSyncType = CT_MsgUtilsEnumConverter::ConvertDesToTFolderSyncType
												((TDesC&)folderSyncTypeName);
							imapSettings->SetSynchronise(folderSyncType);
							}
										
						/* Sets the download limts in the settings object created */
						imapSettings->SetMaxEmailSize( imap4GetMailInfo.iMaxEmailSize );
						imapSettings->SetGetMailOptions( imap4GetMailInfo.iGetMailBodyParts );
						
						/* Saves the settings */
						emailAccounts->SaveImapSettingsL(imapAccount, *imapSettings);
						
						CleanupStack::PopAndDestroy(2,emailAccounts);/* imapSettings, emailAccounts */
						
						INFO_PRINTF2(KInfoCompletionStatus, &KSetImap4FullDownloadLimits);
						}
					}
				}
			}
		}
	return TestStepResult();
	}
/**
ReadImapSettingsFromConfigurationFileL()
Modifies the IMAP related default setting and IAP objects.

@param  aSettingsFile
Configuration/setting file path. 

@param  aImapSet
CImImap4Settings object.

@param  aImapIapPref
CImIAPPreferences object.
*/
EXPORT_C void CT_MsgUtilsReadEmailSettingsFromConfigFile::ReadImapSettingsFromConfigurationFileL(
															TDesC& aSettingsFile, CImImap4Settings& aImapSet, CImIAPPreferences& aImapIapPref,const TDesC& /* aPtrEmailAdd */)

	{
	// Parses the setting file
	CT_MsgUtilsConfigFileParserUtility* scriptFileParser = CT_MsgUtilsConfigFileParserUtility::NewL(aSettingsFile);
	CleanupStack::PushL(scriptFileParser);	
	
	TPtrC8 string8Ptr; 
	TPtrC stringPtr = KNone(); 
	TInt intTmp;
		
	if(scriptFileParser->GetFieldAsString(KCfgImapServerAddress, stringPtr) == KErrNone)
		{
		aImapSet.SetServerAddressL(stringPtr);
		}

	CT_MsgUtilsConfigFileMachineName* machineNameFile = CT_MsgUtilsConfigFileMachineName::NewLC(stringPtr);
	TPtrC8 machineName(machineNameFile->MachineName());
	
	if(scriptFileParser->GetFieldAsString8(KCfgImapLoginName, string8Ptr) == KErrNone)
		{
		aImapSet.SetLoginNameL(string8Ptr);
		}
	else
		{
		aImapSet.SetLoginNameL(machineName);
		}

	if(scriptFileParser->GetFieldAsString8(KCfgImapPassword, string8Ptr) == KErrNone)
		{
		aImapSet.SetPasswordL(string8Ptr);
		}
	else
		{
		aImapSet.SetPasswordL(machineName);
		}
	
	if(scriptFileParser->GetFieldAsInteger(KCfgImapPort, intTmp) == KErrNone)
		{
		aImapSet.SetPort(intTmp);
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapSecureSockets, intTmp) == KErrNone)
		{		
		aImapSet.SetSecureSockets( (TBool) intTmp );
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapSSLWrapper, intTmp ) == KErrNone)
		{		
		aImapSet.SetSSLWrapper( (TBool) intTmp );
		}

	if(scriptFileParser->GetFieldAsString8(KCfgImapFolderPath, string8Ptr) == KErrNone)
		{
		aImapSet.SetFolderPathL(string8Ptr);
		}

	if(scriptFileParser->GetFieldAsString8(KCfgImapPathSeperator, string8Ptr) == KErrNone)
		{
		if ( string8Ptr.Length() == 1  )
			{
			aImapSet.SetPathSeparator( string8Ptr[0] );			
			}
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapMaxEmailSize, intTmp) == KErrNone)
		{
		aImapSet.SetMaxEmailSize(intTmp);
		}
	
	if(scriptFileParser->GetFieldAsString(KCfgImapGetMailOptions, stringPtr) == KErrNone)
		{
		TImap4GetMailOptions setGetMailOptions = CT_MsgUtilsEnumConverter::ConvertDesToTImap4GetMailOptions(stringPtr);
		aImapSet.SetGetMailOptions(setGetMailOptions);
		}
	
	if(scriptFileParser->GetFieldAsInteger(KCfgImapInboxSynchronisationLimit, intTmp) == KErrNone)
		{
		aImapSet.SetInboxSynchronisationLimit(intTmp);
		}
		
	if(scriptFileParser->GetFieldAsInteger(KCfgImapMailboxSynchronisationLimit, intTmp) == KErrNone)
		{
		aImapSet.SetMailboxSynchronisationLimit(intTmp);
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapAutoSendOnConnect, intTmp) == KErrNone)
		{		
		aImapSet.SetAutoSendOnConnect((TBool) intTmp);
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapDeleteEmailsWhenDisconnecting, intTmp) == KErrNone)
		{		
		aImapSet.SetDeleteEmailsWhenDisconnecting((TBool) intTmp);
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapAcknowledgeReceipts, intTmp) == KErrNone)
		{		
		aImapSet.SetAcknowledgeReceipts((TBool) intTmp);
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapDisconnectedUserMode, intTmp) == KErrNone)
		{		
		aImapSet.SetDisconnectedUserMode((TBool) intTmp);
		}

	if(scriptFileParser->GetFieldAsString(KCfgImapSynchronise, stringPtr) == KErrNone)
		{
		TFolderSyncType setFolderSyncType = CT_MsgUtilsEnumConverter::ConvertDesToTFolderSyncType(stringPtr);
		aImapSet.SetSynchronise(setFolderSyncType);
		}

	if(scriptFileParser->GetFieldAsString(KCfgImapSubscribe, stringPtr) == KErrNone)
		{
		TFolderSubscribeType setFolderSubscribeType = CT_MsgUtilsEnumConverter::ConvertDesToTFolderSubscribeType(stringPtr);
		aImapSet.SetSubscribe(setFolderSubscribeType);
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapUpdatingSeenFlags, intTmp) == KErrNone)
		{		
		aImapSet.SetUpdatingSeenFlags((TBool) intTmp);
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapSyncRate, intTmp) == KErrNone)
		{
		aImapSet.SetSyncRateL(intTmp);
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapFetchSize, intTmp) == KErrNone)
		{
		aImapSet.SetFetchSizeL(intTmp);
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapIdle, intTmp) == KErrNone)
		{		
		aImapSet.SetImapIdle( (TBool) intTmp );
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapIdleTimeout, intTmp) == KErrNone)
		{
		aImapSet.SetImapIdleTimeoutL(intTmp);
		}

	if(scriptFileParser->GetFieldAsString(KCfgImapPartialMailOptions, stringPtr) == KErrNone)
		{
		TImImap4PartialMailOptions setImap4PartialMailOptions = CT_MsgUtilsEnumConverter::ConvertDesToTImImap4PartialMailOptions(stringPtr);
		aImapSet.SetPartialMailOptionsL(setImap4PartialMailOptions);
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapBodyTextSizeLimit, intTmp) == KErrNone)
		{
		aImapSet.SetBodyTextSizeLimitL(intTmp);
		}

	if(scriptFileParser->GetFieldAsInteger(KCfgImapAttachmentSizeLimit, intTmp) == KErrNone)
		{
		aImapSet.SetAttachmentSizeLimitL(intTmp);
		}

	if(scriptFileParser->GetFieldAsString(KCfgImapIapList, stringPtr) == KErrNone)
		{
		ReadImapIAPAndAddL(scriptFileParser, (TDesC&)KCfgImapIapList, aImapIapPref);
		}
	
	if(scriptFileParser->GetFieldAsInteger(KUseExpunge, intTmp) == KErrNone)
		{		
		aImapSet.SetUseExpunge( (TBool) intTmp);
		}
	
	if(scriptFileParser->GetFieldAsInteger(KCfgImapSNAPId, intTmp) == KErrNone)
		{		
		aImapIapPref.SetSNAPL(intTmp);
		}
		
	if(scriptFileParser->GetFieldAsInteger(KCfgImapBearerMobility, intTmp) == KErrNone)
		{		
		aImapSet.SetBearerMobility((TBool) intTmp);
		}
	
	if(scriptFileParser->GetFieldAsInteger(KCfgImapDownloadRules, intTmp) == KErrNone)
		{		
		aImapSet.SetUseSyncDownloadRules((TBool) intTmp);
		}	
	
	CleanupStack::PopAndDestroy(2,scriptFileParser);
	}
void CMtfTestActionSetImap4ServiceParameters::ExecuteActionL()
	{
	TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionSetImap4ServiceParameters);
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId paramServiceId = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	TInt paramConfigIndex = ObtainValueParameterL<TInt>(TestCase(),ActionParameters().Parameter(2),0);

	CImImap4Settings* settings = new(ELeave) CImImap4Settings();
	CleanupStack::PushL(settings);
	TPtrC fileName = TestCase().GetConfigurationFileL(CMtfConfigurationType::EMtfImap4Settings,paramConfigIndex);
	iParser = CMtfTestActionUtilsConfigFileParser::NewL(fileName);
	
	settings->Reset();

	TPtrC serverName;
	User::LeaveIfError(iParser->GetFieldAsString(KServerName,serverName));
	settings->SetServerAddressL(serverName);

	CMtfTestActionUtilsConfigFileMachineName* machineNameFile = NULL;

	TPtrC emailAddressExtension;
	if (iParser->GetFieldAsString(KEmailAddressExtension, emailAddressExtension) == KErrNone)
		{
		machineNameFile = CMtfTestActionUtilsConfigFileMachineName::NewLC(emailAddressExtension);
		}
	else
		{
		machineNameFile = CMtfTestActionUtilsConfigFileMachineName::NewLC(serverName);
		}

	TPtrC8 machineName(machineNameFile->MachineName());

	TPtrC8 loginName;
	if(iParser->GetFieldAsString8(KLoginName,loginName)==KErrNone)
		{
		settings->SetLoginNameL(loginName);	
		}
	else
		{
		settings->SetLoginNameL(machineName);	
		}

	TPtrC8 password;
	if(iParser->GetFieldAsString8(KPassword,password)==KErrNone)
		{
		settings->SetPasswordL(password);	
		}
	else
		{
		settings->SetPasswordL(machineName);	
		}

	TPtrC8 folderPath;
	TInt err = iParser->GetFieldAsString8(KFolderPath,folderPath);
	if (err == KErrNone)
		settings->SetFolderPathL(folderPath);
	
	TPtrC8 imapSearchString;
	err = iParser->GetFieldAsString8(KImapSearchString,imapSearchString);
	if (err == KErrNone)
		settings->SetSearchStringL(imapSearchString);

	TInt emailDeleteFlag;
	err = iParser->GetFieldAsInteger(KEmailDeleteFlag,emailDeleteFlag);
	if (err == KErrNone)
		settings->SetDeleteEmailsWhenDisconnecting(emailDeleteFlag);

	TInt autoSendFlag;
	err = iParser->GetFieldAsInteger(KAutoSendFlag,autoSendFlag);
	if (err == KErrNone)
		settings->SetAutoSendOnConnect(autoSendFlag);	
	
 	CEmailAccounts* accounts = CEmailAccounts::NewLC();
 	TImapAccount imapAccount;
 	accounts->GetImapAccountL(paramServiceId, imapAccount);
 	accounts->SaveImapSettingsL(imapAccount, *settings);
 	//Pop and destroy settings, machineNameFile and accounts
	CleanupStack::PopAndDestroy(3, settings);    
	
	TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionSetImap4ServiceParameters);	
	
	TestCase().ActionCompletedL(*this);
	}
LOCAL_C void InitL()
	{
	// Load the serial comms device drivers.  If this is not done,
	// connecting via NT-RAS returns KErrNotFound (-1).
	TInt driverErr;
	driverErr=User::LoadPhysicalDevice(PDD_NAME);
	if (driverErr!=KErrNone && driverErr!=KErrAlreadyExists)
		User::Leave(driverErr);
	driverErr=User::LoadLogicalDevice(LDD_NAME);
	if (driverErr!=KErrNone && driverErr!=KErrAlreadyExists)
		User::Leave(driverErr);

	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
	CActiveScheduler::Install(scheduler);
	CleanupStack::PushL(scheduler);
	testUtils = CEmailTestUtils::NewLC(test);
	testUtils->CreateAllTestDirectories();
	testUtils->FileSession().SetSessionPath(_L("C:\\"));
	testUtils->CleanMessageFolderL();
	testUtils->GoServerSideL();
    testUtils->ClearEmailAccountsL();
	test.Next(_L("Create Data Component FileStores"));
	testUtils->CreateSmtpServerMtmRegL();
	//	Loading the DLLs
	testUtils->CreateRegistryObjectAndControlL();
	testUtils->InstallSmtpMtmGroupL();
	testUtils->GoClientSideL();
	// Create Services
	smtpService = testUtils->CreateSmtpServiceL();
	imap4Service = testUtils->CreateImapServiceL();
	CEmailAccounts* accounts = CEmailAccounts::NewLC();
	TSmtpAccount smtpAccount;
	accounts->GetSmtpAccountL(smtpService, smtpAccount);
	accounts->SetDefaultSmtpAccountL(smtpAccount);
    testUtils->WriteComment(_L("Create  Imap   Services           ")); 
	// Delete any *.scr files in c:\logs\email directory.
	testUtils->DeleteScriptFilesFromLogsDirL();

	testUtils->iMsvEntry->SetEntryL(imap4Service);
	//overwrite the settings with test code one.  Don't want the default settings.
	CImImap4Settings* settings = new(ELeave) CImImap4Settings();
	CleanupStack::PushL(settings);
	settings->Reset();
	TInt serverLength = testUtils->MachineName().Length();
	HBufC* serverad = HBufC::NewL(serverLength + 7 ); // 7 is the length of KServerAddress
	CleanupStack::PushL(serverad);
	serverad->Des().Copy(testUtils->MachineName());
	serverad->Des().Append(KServer);
	settings->SetServerAddressL(*serverad);
	CleanupStack::PopAndDestroy(serverad); 
	settings->SetDeleteEmailsWhenDisconnecting(ETrue);
	settings->SetAcknowledgeReceipts(ETrue);
	settings->SetAutoSendOnConnect(ETrue);
	settings->SetLoginNameL(testUtils->MachineName());
	settings->SetPasswordL(testUtils->MachineName());
   	settings->SetPort(143);
	settings->SetFolderPathL(KFolderPath);
	accounts->GetImapAccountL(imap4Service, imapAccount);
	accounts->SaveImapSettingsL(imapAccount, *settings);
	CleanupStack::PopAndDestroy(2,accounts); //accounts,settings
	test.Printf(_L("Instantiating IMAP4 Client MTM"));
	testUtils->InstantiateImapClientMtmL();
	}