messagingfw/msgtestfw/TestActions/Email/Smtp/src/CMtfTestActionCreateSmtpMessageFromEmailFile.cpp
changeset 0 8e480a14352b
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 // __ACTION_INFO_BEGIN__ 
       
    15 // [Action Name]
       
    16 // CreateSmtpMessageFromEmailFile
       
    17 // [Action Parameters]
       
    18 // CMsvSession			Session		<input>: Reference to the session.
       
    19 // TMsvId				ServiceId   <input>: Value of the SMTP service id.
       
    20 // TMsvId				FolderId    <input>: Value of the local folder id where the message will be created.
       
    21 // TInt		 (ConfigIndexEmailFile) <input>: Value of config index for email file. Default is 0. 
       
    22 // TMsvID	MessageId   <output-initiation>: Value of the created SMTP message id.
       
    23 // TInt					OverrideAddress <input>: Override the To: address in the email to the current SMTP settings
       
    24 // [Action Description]
       
    25 // Creates a SMTP message on one of the local folders.
       
    26 // [APIs Used]
       
    27 // CMsvEntry::SetEntryL
       
    28 // CMsvEntry::Entry
       
    29 // TMsvId::Id
       
    30 // __ACTION_INFO_END__
       
    31 // 
       
    32 //
       
    33 
       
    34 /**
       
    35  @file
       
    36  @internalTechnology 
       
    37 */
       
    38 
       
    39 // EPOC include
       
    40 #include <msvapi.h>
       
    41 #include <miutset.h>
       
    42 #include <smtpset.h>
       
    43 #include <miuthdr.h>
       
    44 #ifdef __MESSAGING_API_V2__
       
    45 #include  <cemailaccounts.h>
       
    46 #endif // __MESSAGING_API_V2__
       
    47 	
       
    48 
       
    49 // User include
       
    50 #include "CMtfTestActionCreateSmtpMessageFromEmailFile.h"
       
    51 #include "CMtfTestCase.h"
       
    52 #include "CMtfTestActionParameters.h"
       
    53 #include "ImCltCvRecv.h"
       
    54 
       
    55 
       
    56 // Maximum lenght of a line read from Email file
       
    57 const TInt	 KMaxLenghtOfLine = 1024;
       
    58 
       
    59 
       
    60 /**
       
    61   NewL()
       
    62   Constructs a CMtfTestActionCreateSmtpMessageFromEmailFile object.
       
    63   Uses two phase construction and leaves nothing on the CleanupStack.   
       
    64   @internalTechnology
       
    65   @param  aTestCase         Test Case to which this Test Action belongs
       
    66   @param  aActionParameters Action parameters, must not be NULL
       
    67   @return Created object of type CMtfTestActionCreateSmtpMessageFromEmailFile
       
    68   @pre    None
       
    69   @post   CMtfTestActionCreateSmtpMessageFromEmailFile object is created
       
    70 */
       
    71 CMtfTestAction* CMtfTestActionCreateSmtpMessageFromEmailFile::NewL(CMtfTestCase& aTestCase,CMtfTestActionParameters* aActionParameters)
       
    72 	{
       
    73 	CMtfTestActionCreateSmtpMessageFromEmailFile* self = new (ELeave) CMtfTestActionCreateSmtpMessageFromEmailFile(aTestCase);
       
    74 	CleanupStack::PushL(self);
       
    75 	self->ConstructL(aActionParameters);
       
    76 	CleanupStack::Pop(self);
       
    77 	return self;
       
    78 	}
       
    79 	
       
    80 
       
    81 /**
       
    82   CMtfTestActionCreateSmtpMessageFromEmailFile constructor
       
    83   Calls the base class' constructor
       
    84   @internalTechnology  
       
    85   @param  aTestCase  Test Case to which this Test Action belongs
       
    86   @pre    None
       
    87   @post   None
       
    88 */ 	
       
    89 CMtfTestActionCreateSmtpMessageFromEmailFile::CMtfTestActionCreateSmtpMessageFromEmailFile(CMtfTestCase& aTestCase)
       
    90 	: CMtfSynchronousTestAction(aTestCase)
       
    91 	{
       
    92 	}
       
    93 
       
    94 /** Destructor */
       
    95 CMtfTestActionCreateSmtpMessageFromEmailFile::~CMtfTestActionCreateSmtpMessageFromEmailFile()
       
    96 	{
       
    97 	}
       
    98 
       
    99 /**
       
   100   ExecuteActionL
       
   101   Parse the Email file and creates an email in the specified local folder.  Creates
       
   102   simple emails, emails with HTML, attachments, multipart and embedded emails.
       
   103   @internalTechnology 
       
   104   @pre    None
       
   105   @post   None
       
   106   @leave  System wide errors
       
   107 */
       
   108 void CMtfTestActionCreateSmtpMessageFromEmailFile::ExecuteActionL()
       
   109 	{
       
   110 	TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionCreateSmtpMessageFromEmailFile);
       
   111 	CMsvSession* paramSession	= ObtainParameterReferenceL<CMsvSession>(TestCase(),
       
   112 									ActionParameters().Parameter(0));
       
   113 								 
       
   114 	TMsvId paramServiceId 		= ObtainValueParameterL<TMsvId>(TestCase(),
       
   115 									ActionParameters().Parameter(1));
       
   116 								 
       
   117 	TMsvId paramFolderId 		= ObtainValueParameterL<TMsvId>(TestCase(),
       
   118 								    ActionParameters().Parameter(2));
       
   119 								 
       
   120 	TInt paramConfigIndexMailFile = ObtainValueParameterL<TInt>(TestCase(),
       
   121 								 ActionParameters().Parameter(3),0);
       
   122 	
       
   123 	TInt overrideAddress=0;
       
   124 	if(ActionParameters().Count()>5)
       
   125 		{
       
   126 		overrideAddress=ObtainValueParameterL<TInt>(TestCase(),
       
   127 								 ActionParameters().Parameter(5));		
       
   128 		}
       
   129 
       
   130 	TPtrC fileName = TestCase().GetConfigurationFileL(CMtfConfigurationType::EMtfEmailFile,
       
   131 														paramConfigIndexMailFile);
       
   132 
       
   133 	/************************************************************************************
       
   134 	Get the name of the Email file save at the Configuration file index, open the  file, 
       
   135 	read one line of text from the file, parse the line and set the fields of the email 
       
   136 	message based on the contents of the parsed line.
       
   137 	************************************************************************************/
       
   138 	
       
   139 	// Set the context ot the folder in which emails have to be created
       
   140 	CMsvEntry* entry = CMsvEntry::NewL(*paramSession,paramFolderId,TMsvSelectionOrdering());
       
   141 	CleanupStack::PushL(entry);
       
   142 	entry->SetEntryL(paramFolderId);
       
   143 
       
   144 	RFs fs;
       
   145 	fs.Connect();
       
   146 
       
   147 	CImCltRecvConvert* recvConvert = CImCltRecvConvert::NewLC(fs,entry,KUidMsgTypeSMTP,
       
   148 															   paramServiceId);
       
   149 	recvConvert->SetMsvId(paramFolderId);
       
   150 
       
   151 	// Open the Email file
       
   152 	RFile file;
       
   153 	User::LeaveIfError(file.Open(fs,fileName,EFileRead));
       
   154 
       
   155 	TestCase().INFO_PRINTF2(_L("Email Data file: %S"), &fileName);
       
   156 	
       
   157 	TBuf8<KMaxLenghtOfLine> line;
       
   158 	TBuf8<1> aChar;				// To read one character from the file
       
   159 	TBool finished = FALSE;
       
   160 
       
   161 	recvConvert->ResetL();
       
   162 
       
   163 	// Parse each line from the 
       
   164 
       
   165 	do {
       
   166 		line.FillZ();
       
   167 		line.SetLength(0);
       
   168 		// Read one line from email file
       
   169 		do {
       
   170 			file.Read(aChar, 1);
       
   171 			if(aChar.Length())
       
   172 				{
       
   173 				line.Append(aChar);
       
   174 				}
       
   175 			else
       
   176 				{
       
   177 				finished = TRUE;
       
   178 				}
       
   179 			} while(aChar.Length() && aChar[0] != 0x0A);
       
   180 			
       
   181 			if(!line.Length())
       
   182 				{
       
   183 				break;
       
   184 				}
       
   185 			// Parse the line for the fields of the email and store them
       
   186 			recvConvert->ParseNextFieldL(line);
       
   187 		} while(!finished);
       
   188 
       
   189 
       
   190 	/****************************************************************************
       
   191 	 Complete the message creation by setting all the required fields and 
       
   192 	 storing the email on to the message store.
       
   193 	 The created email's ID is returned by MessageCompelteL function
       
   194 	****************************************************************************/
       
   195 	TMsvId paramMessageId;
       
   196 	paramMessageId = recvConvert->MessageCompleteL();
       
   197 
       
   198 	if(paramMessageId == 0)
       
   199 		{
       
   200 		User::Leave(KErrUnknown);
       
   201 		}
       
   202 
       
   203 	file.Close();
       
   204 	CleanupStack::PopAndDestroy(recvConvert);
       
   205 	
       
   206 	if(overrideAddress)
       
   207 		{
       
   208 #ifndef __MESSAGING_API_V2__		
       
   209 		entry->SetEntryL(paramServiceId);
       
   210 		CMsvStore* store=entry->ReadStoreL();
       
   211 		CleanupStack::PushL(store);
       
   212 #endif		
       
   213 		CImSmtpSettings* settings= new(ELeave) CImSmtpSettings();
       
   214 		CleanupStack::PushL(settings);
       
   215 #ifdef __MESSAGING_API_V2__
       
   216  		CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   217  		TSmtpAccount smtpAccount;
       
   218  		accounts->GetSmtpAccountL(paramServiceId, smtpAccount);
       
   219 		accounts->LoadSmtpSettingsL(smtpAccount, *settings);
       
   220 		CleanupStack::PopAndDestroy();
       
   221 #else		
       
   222 		settings->RestoreL(*store);
       
   223 		CleanupStack::PopAndDestroy();
       
   224 #endif		
       
   225 		HBufC* emailAddress=settings->EmailAddress().AllocL();
       
   226 		CleanupStack::PopAndDestroy();
       
   227 		CleanupStack::PushL(emailAddress);
       
   228 		entry->SetEntryL(paramMessageId);
       
   229 #ifdef __MESSAGING_API_V2__
       
   230 		CMsvStore* store=entry->EditStoreL();
       
   231 #else
       
   232 		
       
   233 		store=entry->EditStoreL();
       
   234 #endif
       
   235 		CleanupStack::PushL(store);
       
   236 		
       
   237 		CImHeader* header= CImHeader::NewLC();
       
   238 		header->RestoreL(*store);
       
   239 		
       
   240 		header->ToRecipients().Reset();
       
   241 		header->ToRecipients().AppendL(*emailAddress);
       
   242 		
       
   243 		header->StoreL(*store);
       
   244 		store->CommitL();
       
   245 		
       
   246 		CleanupStack::PopAndDestroy(3,emailAddress);
       
   247 		}
       
   248 	
       
   249 	CleanupStack::PopAndDestroy(entry);
       
   250 
       
   251 	TestCase().INFO_PRINTF2(_L("Created a email from file %S"),&fileName);
       
   252 
       
   253 	StoreParameterL<TMsvId>(TestCase(),paramMessageId,ActionParameters().Parameter(4));
       
   254 	fs.Close();
       
   255 	TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionCreateSmtpMessageFromEmailFile);
       
   256 	TestCase().ActionCompletedL(*this);
       
   257 	}
       
   258