email/email/smtp/src/T_RetrievePlainBodyTextAndCompare.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 // [TestStep Name]
       
    15 // RetrievePlainBodyTextAndCompare
       
    16 // [Parameters]
       
    17 // ServiceType			:	Specifies the service type(IMAP or POP) under which message exists.
       
    18 // ImapAccountName		:	If service type is IMAP4, specify the IMAP account name that holds the
       
    19 // message.
       
    20 // ParentFolderName	:	IMAP4 folder name that holds the message.
       
    21 // PopAccountName		:	If the the sevice type is POP3, specify POP account name.
       
    22 // SubjectOfExistingMsg:	Sbject line of the existing message to which forward message
       
    23 // would be creted.
       
    24 // EmailFileName		:	File name from which existing message has been created.
       
    25 // ChunkSize			:	Chunk size to be used for retriving body text of a message.
       
    26 // Searches for the message with the specified subject and retrives the body part of
       
    27 // the message and write it to the RetrivedBody.txt file.
       
    28 // [APIs Used]
       
    29 // CImEmailMessage::OpenPlainBodyTextForReadL
       
    30 // CImPlainBodyText::NextChunkL
       
    31 // NOTE : Comparison part has been removed as it would success only when 
       
    32 // message creation and fetching both uses same set of character set(8 bit or 16 bit).
       
    33 // However comaprison can be done manually using the text comparison tools(e.g, Beyond Compare)
       
    34 // 
       
    35 //
       
    36 
       
    37 
       
    38 
       
    39 /**
       
    40  @file
       
    41 */
       
    42 
       
    43 
       
    44 
       
    45 // User Includes
       
    46 #include "T_RetrievePlainBodyTextAndCompare.h"
       
    47 #include <t_utilscentralrepository.h>
       
    48 #include <t_utilsenumconverter.h>
       
    49 #include <t_utils.h>
       
    50 
       
    51 
       
    52 // Epoc Include
       
    53 #include <miutmsg.h>
       
    54 #include <cimplainbodytext.h>
       
    55 
       
    56 
       
    57 // Literals Used
       
    58 _LIT(KServiceType, "ServiceType");
       
    59 
       
    60 _LIT(KImapAccountName, "ImapAccountName");
       
    61 _LIT(KParentFolderName, "ParentFolderName");
       
    62 
       
    63 _LIT(KPopAccountName,"PopAccountName");
       
    64 
       
    65 _LIT(KSubjectOfExistingMsg,"SubjectOfExistingMsg");
       
    66 _LIT(KEmailFileName, "EmailFileName");
       
    67 _LIT(KChunkSize, "ChunkSize");
       
    68 
       
    69 _LIT(KRetrivedBody, "c:\\msgtest\\RetrivedBody.txt"); //File used to hold the retrive body part
       
    70 
       
    71 
       
    72 const TInt KArrayGranularity = 8;
       
    73 const TInt KZero={0};
       
    74 /**
       
    75 CT_RetrievePlainBodyTextAndCompare
       
    76 Constructor
       
    77 
       
    78 @param :
       
    79 @return : N/A
       
    80 
       
    81 
       
    82 */
       
    83 CT_RetrievePlainBodyTextAndCompare::CT_RetrievePlainBodyTextAndCompare(CT_MsgSharedDataSmtp& aSharedDataSMTP)
       
    84 : CT_MsgAsyncStepSMTP(aSharedDataSMTP)
       
    85 	{
       
    86 	SetTestStepName(KRetrievePlainBodyTextAndCompare);
       
    87 	}
       
    88 
       
    89 
       
    90 /**
       
    91 NewL()
       
    92 Static factory constructor. Uses two phase 
       
    93 construction.
       
    94 
       
    95 @param aSharedDataSMTP
       
    96 @leave KErrNoMemory
       
    97 @return
       
    98 A pointer to the newly created CT_RetrievePlainBodyTextAndCompare object.
       
    99 */
       
   100 CT_RetrievePlainBodyTextAndCompare* CT_RetrievePlainBodyTextAndCompare::NewL(CT_MsgSharedDataSmtp& aSharedDataSMTP)
       
   101 	{
       
   102 	CT_RetrievePlainBodyTextAndCompare* self = new(ELeave) CT_RetrievePlainBodyTextAndCompare(aSharedDataSMTP);
       
   103 	CleanupStack::PushL(self);
       
   104 	self->ConstructL(); 					 // Call CT_AsyncStep::ConstructL()
       
   105 	CleanupStack::Pop(self);
       
   106 	return self;
       
   107 	}
       
   108 
       
   109 
       
   110 /**
       
   111 ~CT_RetrievePlainBodyTextAndCompare()
       
   112 Destructor
       
   113 */
       
   114 CT_RetrievePlainBodyTextAndCompare::~CT_RetrievePlainBodyTextAndCompare()
       
   115 	{}
       
   116 
       
   117 
       
   118 /**
       
   119 ProgressL()
       
   120 
       
   121 @param aFinal
       
   122 */
       
   123 void CT_RetrievePlainBodyTextAndCompare::ProgressL(TBool aFinal)
       
   124 	{
       
   125 	if ( aFinal )
       
   126 		{
       
   127 		//	Display final progress iOperation
       
   128 		}
       
   129 	else
       
   130 		{
       
   131 		//	Display current progress iOperation
       
   132 		}
       
   133 	}
       
   134 
       
   135 
       
   136 /**
       
   137 CancelStep()
       
   138 */
       
   139 void CT_RetrievePlainBodyTextAndCompare::CancelStep()
       
   140 	{}
       
   141 
       
   142 	
       
   143 /**
       
   144 doTestStepL()
       
   145 Opens the body text part with read only mode. Reads the contents of body part chunkwise and write it
       
   146 to the file  RetrivedBody.txt. Then it comapares the contents of RetrivedBody.txt with the input file.
       
   147 @return
       
   148 Returns the test step result
       
   149 */
       
   150 TVerdict CT_RetrievePlainBodyTextAndCompare::doTestStepL()
       
   151 	{
       
   152 	INFO_PRINTF1( _L("Test Step : Edit Message Body Text") );	
       
   153 	TMsvId	entryToBesearched;
       
   154 	TPtrC serviceType;
       
   155 	if(!GetStringFromConfig( ConfigSection(), KServiceType, serviceType))
       
   156 		{
       
   157 		ERR_PRINTF1(_L("\"ServiceType\" for the message is not specified"));
       
   158 		SetTestStepResult(EFail);
       
   159 		}
       
   160 	else
       
   161 		{
       
   162 		TBuf<4> temp(serviceType);
       
   163 		temp.UpperCase();						// Makes case insensitive
       
   164 		// Perform the following if message has been downloaded using IMAP4 service
       
   165 		if(temp.CompareC(_L("IMAP")) == KZero)
       
   166 			{
       
   167 			// Read the IMAP account name from the ini file
       
   168 			TPtrC imapAccountName;
       
   169 			if(!GetStringFromConfig( ConfigSection(), KImapAccountName, imapAccountName))
       
   170 				{
       
   171 				ERR_PRINTF1(_L("ImapAccount Name is not specified"));
       
   172 				SetTestStepResult(EFail);
       
   173 				}
       
   174 			else
       
   175 				{
       
   176 				TPtrC	parentFolderName;
       
   177 				if(!GetStringFromConfig( ConfigSection(), KParentFolderName, parentFolderName))
       
   178 					{
       
   179 					ERR_PRINTF1(_L("Parent Folder is not specified"));
       
   180 					SetTestStepResult(EFail);
       
   181 					}
       
   182 				else
       
   183 					{
       
   184 					
       
   185 					// retrieves the folder Id based on the remote service folder name read from the ini file
       
   186 					entryToBesearched = CT_MsgUtils::GetRemoteFolderIdByNameL(iSharedDataSMTP.iSession, imapAccountName, parentFolderName);
       
   187 					if( entryToBesearched == KErrNotFound)
       
   188 						{
       
   189 						ERR_PRINTF1(_L("Invalid Remote folder name specified"));
       
   190 						SetTestStepResult(EFail);
       
   191 						}
       
   192 					else
       
   193 						{
       
   194 						INFO_PRINTF2(_L("The parent folder Id is %d"),entryToBesearched );
       
   195 						}
       
   196 					}
       
   197 				}
       
   198 			}
       
   199 		// Perform the following if message has been downloaded using POP3 service 	
       
   200 		else if(temp.CompareC(_L("POP")) == KZero)
       
   201 			{
       
   202 			TPtrC popAccountName;
       
   203 			if(!GetStringFromConfig( ConfigSection(), KPopAccountName, popAccountName))
       
   204 				{
       
   205 				ERR_PRINTF1(_L("PopAccount Name is not specified"));
       
   206 				SetTestStepResult(EFail);
       
   207 				}
       
   208 			else
       
   209 				{
       
   210 				// Retrieves the Pop service Id for the given Pop account
       
   211 				entryToBesearched = CT_MsgUtilsCentralRepository::GetPopServiceIdL((TDes&)popAccountName);
       
   212 				INFO_PRINTF2(_L("Pop service id is %d"), entryToBesearched);
       
   213 
       
   214 				// Fails the test step if an invalid POP account is specified 
       
   215 				if(entryToBesearched == KMsvNullIndexEntryId)
       
   216 					{
       
   217 					ERR_PRINTF1(_L("Invalid POP account name specified"));
       
   218 					SetTestStepResult(EFail);
       
   219 					}
       
   220 				}
       
   221 			}
       
   222 		else
       
   223 			{
       
   224 			ERR_PRINTF1(_L("\"ServiceType\" is not specified: It should be IMAP or POP"));
       
   225 			SetTestStepResult(EFail);
       
   226 			}
       
   227 		}
       
   228 
       
   229 	if(TestStepResult() == EPass)
       
   230 		{
       
   231 		TPtrC subjectOfExistingMsg;	
       
   232 		if( !GetStringFromConfig(ConfigSection(), KSubjectOfExistingMsg, subjectOfExistingMsg))
       
   233 			{
       
   234 			ERR_PRINTF1(_L("\"SubjectOfExistingMsg\" is not specified"));
       
   235 			SetTestStepResult(EFail);
       
   236 			}
       
   237 		else
       
   238 			{
       
   239 			// Retrieves the message Id based on the message subject from the given remote service folder
       
   240 			TMsvId messageId;
       
   241 			messageId = CT_MsgUtils::SearchMessageBySubjectL(iSharedDataSMTP.iSession, entryToBesearched, subjectOfExistingMsg, ETrue);
       
   242 			if( messageId == KMsvNullIndexEntryId)
       
   243 				{
       
   244 				ERR_PRINTF1(_L("The given message is not found"));
       
   245 				SetTestStepResult(EFail);
       
   246 				}
       
   247 			// Message found
       
   248 			else
       
   249 				{
       
   250 				TPtrC emailFileName;
       
   251 				if(!GetStringFromConfig( ConfigSection(), KEmailFileName, emailFileName))
       
   252 					{
       
   253 					ERR_PRINTF1(_L("Parent Folder is not specified"));
       
   254 					SetTestStepResult(EFail);
       
   255 					}
       
   256 				else
       
   257 					{
       
   258 					INFO_PRINTF2(_L("The Message Id is %d"),messageId );
       
   259 
       
   260 					// Setting the current context to the parent of the mesage
       
   261 					CMsvEntry*	messageEntry = CMsvEntry::NewL(*iSharedDataSMTP.iSession, messageId , TMsvSelectionOrdering());
       
   262 					CleanupStack::PushL(messageEntry);
       
   263 					messageEntry->SetEntryL(messageId);
       
   264 					CImEmailMessage *emailMessage = CImEmailMessage::NewLC(*messageEntry);
       
   265 					TInt chunkSize(1);
       
   266 					GetIntFromConfig(ConfigSection(), KChunkSize, chunkSize);
       
   267 					// Open the body text part for read only purpose
       
   268 					CImPlainBodyText* bodytext = emailMessage->OpenPlainBodyTextForReadL(CImEmailMessage::EThisMessageOnly, chunkSize);
       
   269 					CleanupStack::PushL(bodytext);
       
   270 			
       
   271 
       
   272 					HBufC*  fileData =  HBufC::NewLC(chunkSize);
       
   273 					TPtr readData = fileData->Des();
       
   274 					RFs	fs;
       
   275 					User::LeaveIfError(fs.Connect());
       
   276 					CleanupClosePushL(fs);
       
   277 
       
   278 					RFile	file;
       
   279 					User::LeaveIfError(file.Replace(fs,KRetrivedBody,EFileWrite));
       
   280 					CleanupClosePushL(file);
       
   281 					TInt count = 0;
       
   282 					// Retrieve and store the body text to a file
       
   283 					do
       
   284 						{
       
   285 						bodytext->NextChunkL(readData);
       
   286 						_LIT(KServerMessage, "*This message was transferred with a trial version of CommuniGate(tm) Pro*\r\n");
       
   287 						TInt position = readData.Find(KServerMessage);// readData.Find(KServerMessage);
       
   288 						if(position != KErrNotFound && count < 1)
       
   289 							{
       
   290 							count++;
       
   291 							readData.Delete(position, KServerMessage().Length());
       
   292 							TPtrC8 inFileBuf8((TUint8*)(readData.Ptr()), readData.Length()*2);
       
   293 							file.Write(inFileBuf8);
       
   294 							}
       
   295 						else
       
   296 							{
       
   297 							TPtrC8 inFileBuf8((TUint8*)(readData.Ptr()), readData.Length()*2);
       
   298 							file.Write(inFileBuf8);
       
   299 							}
       
   300 						}while(readData.Length()>0);
       
   301 					file.Close();
       
   302 					CleanupStack::PopAndDestroy(6, messageEntry); // file, fs, fileData, bodytext, emailMessage, messageEntry
       
   303 					}
       
   304 				}
       
   305 			}
       
   306 		}
       
   307 	return TestStepResult();	
       
   308 	}