email/email/smtp/src/T_SmtpCreateForwardMessage.cpp
branchSymbian2
changeset 1 8758140453c0
child 6 c108117318cb
equal deleted inserted replaced
0:e8c1ea2c6496 1:8758140453c0
       
     1 // Copyright (c) 2007-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 the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // @file 
       
    15 // [TestStep Name]
       
    16 // SmtpCreateForwadMessage
       
    17 // [Parameters]
       
    18 // ServiceType			:	Specifies the service type(IMAP or POP) under which message exists.
       
    19 // FolderName			:	Destination local folder where message is to be created.
       
    20 // ImapAccountName		:	If service type is IMAP4, specify the IMAP account name that holds the
       
    21 // message.
       
    22 // ParentFolderName	:	IMAP4 folder name that holds the message.
       
    23 // PopAccountName		:	If the the sevice type is POP3, specify POP account name.
       
    24 // CommandName			:	SMTP client MTM command used to create the forward message.
       
    25 // FolderName			:	Destination local folder in which message would be storted.
       
    26 // SubjectOfExistingMsg:	Sbject line of the existing message to which forward message
       
    27 // would be creted.
       
    28 // SubjectOfForwardMsg	:	Specifies the subject line to be set for the froward message.
       
    29 // This is an optional parameter and if not specified the subject line
       
    30 // would take the default format(Fwd: <SubjectOfExistingMsg>).
       
    31 // Creates a forward message to an existing message using the SMTP client MTM command
       
    32 // and modifies the message header information.
       
    33 // [APIs Used]
       
    34 // CImEmailMessage::OpenPlainBodyTextForReadL
       
    35 // CImPlainBodyText::NextChunkL
       
    36 // 
       
    37 //
       
    38 
       
    39 
       
    40 
       
    41 
       
    42 // User includes 
       
    43 #include "T_SmtpCreateForwardMessage.h"
       
    44 #include <t_utilscentralrepository.h>
       
    45 #include <t_utilsenumconverter.h>
       
    46 #include <t_utils.h>
       
    47 
       
    48 
       
    49 // Epoc includes 
       
    50 #include <miutmsg.h>
       
    51 
       
    52 
       
    53 // Literals Used 
       
    54 _LIT(KServiceType, "ServiceType");
       
    55 
       
    56 _LIT(KImapAccountName,"ImapAccountName");
       
    57 _LIT(KParentFolderName,"ParentFolderName");
       
    58 
       
    59 _LIT(KPopAccountName,"PopAccountName");
       
    60 
       
    61 _LIT(KCommandName,"CommandName");
       
    62 _LIT(KFolderName, "FolderName");
       
    63 _LIT(KSubjectOfExistingMsg,"SubjectOfExistingMsg");
       
    64 
       
    65 _LIT(KSubjectOfForwardMsg, "SubjectOfForwardMsg");
       
    66 _LIT(KRecipientsAddress, "RecipientsAddress");
       
    67 
       
    68 const TInt KZero={0};
       
    69 /**
       
    70   Function 		: CT_SmtpCreateForwadMessage
       
    71   Description 	: Constructor
       
    72   @param 		: aSharedDataSMTP		Reference to CT_MsgSharedDataSmtp
       
    73   @return 		: N/A
       
    74 */
       
    75 CT_SmtpCreateForwadMessage::CT_SmtpCreateForwadMessage(CT_MsgSharedDataSmtp& aSharedDataSMTP)
       
    76 :	CT_MsgAsyncStepSMTP(aSharedDataSMTP)
       
    77 ,	iOperation(NULL)
       
    78 	{
       
    79 	SetTestStepName(KSmtpCreateForwadMessage);
       
    80 	}
       
    81 
       
    82 
       
    83 /**
       
    84   Function 		: NewL
       
    85   Description 	: Creates an object of CT_SmtpCreateForwadMessage 				 
       
    86   @return 		: N/A
       
    87 */
       
    88 CT_SmtpCreateForwadMessage* CT_SmtpCreateForwadMessage::NewL(CT_MsgSharedDataSmtp& aSharedDataSMTP)
       
    89 	{
       
    90 	CT_SmtpCreateForwadMessage* self = new(ELeave) CT_SmtpCreateForwadMessage(aSharedDataSMTP);
       
    91 	CleanupStack::PushL(self);
       
    92 	self->ConstructL(); 					 
       
    93 	CleanupStack::Pop(self);
       
    94 	return self;
       
    95 	}	
       
    96 
       
    97 	
       
    98 /**
       
    99   Function 		: ~CT_SmtpCreateForwadMessage
       
   100   Description 	: Destructor
       
   101   @return 		: N/A
       
   102 */
       
   103 CT_SmtpCreateForwadMessage::~CT_SmtpCreateForwadMessage()
       
   104 	{
       
   105 	delete iRecipientAddress;
       
   106 	delete iOperation;
       
   107 	iOperation = NULL;
       
   108 	}
       
   109 
       
   110 /**
       
   111   Function 		: ProgressL
       
   112   Description 	: Displays the status of the asynchronous operation
       
   113   @return 		: N/A
       
   114 */
       
   115 void CT_SmtpCreateForwadMessage::ProgressL(TBool /*bFinal*/)
       
   116 	{}
       
   117 
       
   118 
       
   119 /**
       
   120   Function 		: CancelStep
       
   121   Description 	: Cancels the asynchronous operation
       
   122   @return 		: N/A
       
   123 */
       
   124 void CT_SmtpCreateForwadMessage::CancelStep()
       
   125 	{
       
   126 	iOperation->Cancel();
       
   127 	}
       
   128 
       
   129 
       
   130 /**
       
   131   Function 		: doTestStepL
       
   132   Description 	: Creates a forward message for an existing message. Existing message
       
   133   				  may be under some remot folder under IMAP service or under POP service.
       
   134   @return 		: TVerdict - Test step result
       
   135 */
       
   136 TVerdict CT_SmtpCreateForwadMessage::doTestStepL()
       
   137 	{
       
   138 	INFO_PRINTF1(_L("TestStep: SmtpCreateFwdMsgForPlainBodyTextMsg"));
       
   139 	TMsvId	entryToBesearched = KMsvNullIndexEntryId;
       
   140 	TPtrC serviceType;
       
   141 	if(!GetStringFromConfig( ConfigSection(), KServiceType, serviceType))
       
   142 		{
       
   143 		ERR_PRINTF1(_L("\"ServiceType\" for the message is not specified"));
       
   144 		SetTestStepResult(EFail);
       
   145 		}
       
   146 	else
       
   147 		{
       
   148 		TBuf<4> temp(serviceType);
       
   149 		temp.UpperCase();						// Makes case insensitive
       
   150 		// Perform the following if message has been downloaded using IMAP4 service
       
   151 		if(temp.CompareC(_L("IMAP")) == KZero)
       
   152 			{
       
   153 			// Read the IMAP account name from the ini file
       
   154 			TPtrC imapAccountName;
       
   155 			if(!GetStringFromConfig( ConfigSection(), KImapAccountName, imapAccountName))
       
   156 				{
       
   157 				ERR_PRINTF1(_L("ImapAccount Name is not specified"));
       
   158 				SetTestStepResult(EFail);
       
   159 				}
       
   160 			else
       
   161 				{
       
   162 				TPtrC	parentFolderName;
       
   163 				if(!GetStringFromConfig( ConfigSection(), KParentFolderName, parentFolderName))
       
   164 					{
       
   165 					ERR_PRINTF1(_L("Parent Folder is not specified"));
       
   166 					SetTestStepResult(EFail);
       
   167 					}
       
   168 				else
       
   169 					{
       
   170 					
       
   171 					// retrieves the folder Id based on the remote service folder name read from the ini file
       
   172 					entryToBesearched = CT_MsgUtils::GetRemoteFolderIdByNameL(iSharedDataSMTP.iSession, imapAccountName, parentFolderName);
       
   173 					if( entryToBesearched == KErrNotFound)
       
   174 						{
       
   175 						ERR_PRINTF1(_L("Invalid Remote folder name specified"));
       
   176 						SetTestStepResult(EFail);
       
   177 						}
       
   178 					else
       
   179 						{
       
   180 						INFO_PRINTF2(_L("The parent folder Id is %d"),entryToBesearched );
       
   181 						}
       
   182 					}
       
   183 				}
       
   184 			}
       
   185 		// Perform the following if message has been downloaded using POP3 service 	
       
   186 		else if(temp.CompareC(_L("POP")) == KZero)
       
   187 			{
       
   188 			TPtrC popAccountName;
       
   189 			if(!GetStringFromConfig( ConfigSection(), KPopAccountName, popAccountName))
       
   190 				{
       
   191 				ERR_PRINTF1(_L("PopAccount Name is not specified"));
       
   192 				SetTestStepResult(EFail);
       
   193 				}
       
   194 			else
       
   195 				{
       
   196 				// Retrieves the Pop service Id for the given Pop account
       
   197 				entryToBesearched = CT_MsgUtilsCentralRepository::GetPopServiceIdL((TDes&)popAccountName);
       
   198 				INFO_PRINTF2(_L("Pop service id is %d"), entryToBesearched);
       
   199 
       
   200 				// Fails the test step if an invalid POP account is specified 
       
   201 				if(entryToBesearched == KMsvNullIndexEntryId)
       
   202 					{
       
   203 					ERR_PRINTF1(_L("Invalid POP account name specified"));
       
   204 					SetTestStepResult(EFail);
       
   205 					}
       
   206 				}
       
   207 			}
       
   208 		else
       
   209 			{
       
   210 			ERR_PRINTF1(_L("Sevice Type is not specified: It should be IMAP or POP"));
       
   211 			SetTestStepResult(EFail);
       
   212 			}
       
   213 		}
       
   214 		
       
   215 	if(TestStepResult() == EPass)
       
   216 		{
       
   217 		TPtrC commandName;
       
   218 		if(!GetStringFromConfig( ConfigSection(), KCommandName, commandName))
       
   219 			{
       
   220 			ERR_PRINTF1(_L("Command name is not specified"));
       
   221 			SetTestStepResult(EFail);
       
   222 			}
       
   223 		else
       
   224 			{
       
   225 			// Read the name of the target folder from the ini file
       
   226 			TPtrC folderName;
       
   227 			if(!GetStringFromConfig( ConfigSection(), KFolderName, folderName))
       
   228 				{
       
   229 				ERR_PRINTF1(_L("Folder name is not specified"));
       
   230 				SetTestStepResult(EFail);
       
   231 				}
       
   232 			else
       
   233 				{
       
   234 				TPtrC subjectOfExistingMsg;	
       
   235 				if( !GetStringFromConfig(ConfigSection(), KSubjectOfExistingMsg, subjectOfExistingMsg))
       
   236 					{
       
   237 					ERR_PRINTF1(_L("\"SubjectOfExistingMsg\" is not specified"));
       
   238 					SetTestStepResult(EFail);
       
   239 					}
       
   240 				else
       
   241 					{
       
   242 					// Retrieves the message Id based on the message subject from the given remote service folder
       
   243 					TMsvId messageId;
       
   244 					messageId = CT_MsgUtils::SearchMessageBySubjectL(iSharedDataSMTP.iSession, entryToBesearched, subjectOfExistingMsg, ETrue);
       
   245 					if( messageId == KMsvNullIndexEntryId)
       
   246 						{
       
   247 						ERR_PRINTF1(_L("The given message is not found"));
       
   248 						SetTestStepResult(EFail);
       
   249 						}
       
   250 					else
       
   251 						{
       
   252 						TMsvId folderId;
       
   253 						folderId = CT_MsgUtilsEnumConverter::FindFolderIdByName(folderName);
       
   254 						
       
   255 						// Interpret the SMTP client MTM command to be executed 
       
   256 						TSmtpCmds commandId = CT_MsgUtilsEnumConverter::ConvertDesToTSmtpCmdsL((TDesC&) commandName);									
       
   257 
       
   258 						CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
       
   259 						CleanupStack::PushL(selection);
       
   260 									
       
   261 						selection->AppendL(folderId);
       
   262 						selection->AppendL(messageId);
       
   263 
       
   264 						TImCreateMessageOptions createMessageOptions;
       
   265 						createMessageOptions.iMsvPartList = KMsvMessagePartBody;
       
   266 						createMessageOptions.iMessageType = KUidMsgTypeSMTP;
       
   267 						createMessageOptions.iMsvEmailTypeList = 0;
       
   268 						TPckgBuf<TImCreateMessageOptions> paramPack(createMessageOptions);
       
   269 						
       
   270 						CT_MsgActive&	active=Active();
       
   271 						delete iOperation;
       
   272 						iOperation = NULL;
       
   273 						TRAPD(err2,iOperation = iSharedDataSMTP.iMtm->InvokeAsyncFunctionL(commandId,*selection,paramPack,active.iStatus));
       
   274 						active.Activate();
       
   275 						CActiveScheduler::Start();
       
   276 						SetHeaderPartL();
       
   277 
       
   278 						User::LeaveIfError(active.Result());
       
   279 						INFO_PRINTF2(_L("err2 : %d"), err2);
       
   280 						CleanupStack::PopAndDestroy(selection); // selection
       
   281 						}
       
   282 					}
       
   283 				}
       
   284 			}
       
   285 		}
       
   286 	return TestStepResult();
       
   287 	}
       
   288 
       
   289 
       
   290 /**
       
   291   Function 		: SetHeaderPartL
       
   292   Description 	: Sets the message header fields. If the field values are not provided
       
   293   				  as input, it takes the default values.
       
   294   @return 		: N/A  				  
       
   295 */
       
   296 void CT_SmtpCreateForwadMessage::SetHeaderPartL()
       
   297 	{
       
   298 	//Get the message ID from the CMsvOperation object
       
   299 	TMsvId temp;	
       
   300 	TPckgC<TMsvId> paramPack(temp);
       
   301 	const TDesC8& progBuf = iOperation->ProgressL();	
       
   302 	paramPack.Set(progBuf);
       
   303 	TMsvId newMessageId = paramPack();	
       
   304 	if (newMessageId!=NULL)
       
   305 		{
       
   306 		CMsvEntry *entry = CMsvEntry::NewL(*(iSharedDataSMTP.iSession), newMessageId, TMsvSelectionOrdering(KMsvNoGrouping,EMsvSortByNone,ETrue));
       
   307 		CleanupStack::PushL(entry);
       
   308 		entry->SetEntryL(newMessageId);			// 
       
   309 		CMsvStore* store = entry->EditStoreL();
       
   310 		CleanupStack::PushL(store);
       
   311 		
       
   312 		CImHeader* header = CImHeader::NewLC(); 
       
   313 		header->RestoreL(*store); // Retrieves the email message header part from the message store
       
   314 		TPtrC subjectOfForwardMsg;
       
   315 		if(GetStringFromConfig(ConfigSection(), KSubjectOfForwardMsg, subjectOfForwardMsg))
       
   316 			{
       
   317 			header->SetSubjectL(subjectOfForwardMsg);
       
   318 			}
       
   319 		TPtrC recipientsAddress;	
       
   320 		if(GetStringFromConfig(ConfigSection(), KRecipientsAddress, recipientsAddress))
       
   321 			{
       
   322 			header->ToRecipients().AppendL(recipientsAddress);
       
   323 			}
       
   324 		else
       
   325 			{
       
   326 			GetDefaultAddressL();
       
   327 			header->ToRecipients().AppendL(*iRecipientAddress);
       
   328 			}
       
   329 		header->StoreL(*store);
       
   330 		store->CommitL();				// Saves the modifications to message store
       
   331 		TMsvEntry indexEntry = entry->Entry();
       
   332 		indexEntry.iDescription.Set(header->Subject());
       
   333 		INFO_PRINTF2(_L("Subject field of Forward message : %S"), &(indexEntry.iDescription));
       
   334 		indexEntry.SetVisible(ETrue);
       
   335 		entry->ChangeL(indexEntry);			// Modify the corresponding index entry permanently
       
   336 
       
   337 		CleanupStack::PopAndDestroy(3,entry);	//	header, store, 	entry
       
   338 		}
       
   339 	}
       
   340 
       
   341 
       
   342 /**
       
   343   Function 		: GetDefaultAddressL
       
   344   Description 	: Retrieve the default email address from the default SMTP service.
       
   345   @return 		: N/A
       
   346 */
       
   347 void CT_SmtpCreateForwadMessage::GetDefaultAddressL()
       
   348 	{
       
   349 	CImSmtpSettings* settings = new(ELeave) CImSmtpSettings();
       
   350 	CleanupStack::PushL(settings);
       
   351 
       
   352 	CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   353 	TSmtpAccount smtpAccount;
       
   354 	smtpAccount.iSmtpService = KMsvNullIndexEntryId;
       
   355 	accounts->DefaultSmtpAccountL(smtpAccount);
       
   356 	accounts->LoadSmtpSettingsL(smtpAccount, *settings);
       
   357 	CleanupStack::PopAndDestroy(accounts);					// accounts
       
   358 	iRecipientAddress = settings->EmailAddress().AllocL();
       
   359 	INFO_PRINTF2(_L("Default Email Address is %s"),iRecipientAddress->Ptr());
       
   360 	CleanupStack::PopAndDestroy(settings);					// settings
       
   361 	}