messagingfw/msgtestfw/TestActions/Email/Smtp/src/CMtfTestActionSmtpCreateAttachment.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2004-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 // SmtpCreateAttachment
       
    17 // [Action Parameters]
       
    18 // Session        <input>: Reference to the session.
       
    19 // MsgId		  <input>: Value of the message Id.
       
    20 // DataFilePath	  <input>: File path that contains the data to populate new attachment with.
       
    21 // AttachmentId   <output>: Value of the created attachment id.
       
    22 // ErrorCode	  <output>: (Optional) Returned error code, if not requested then code will
       
    23 // leave if not KErrNone
       
    24 // [Action Description]
       
    25 // Creates a new file attachment on the specified message entry directly in message store.
       
    26 // [APIs Used]
       
    27 // CMsvSession::GetEntryL
       
    28 // CImEmailMessage::AttachmentManagerL
       
    29 // CMsvAttachment::NewL
       
    30 // CMsvAttachment::SetMimeType
       
    31 // MMsvAttachmentManager::CreateAttachmentL
       
    32 // __ACTION_INFO_END__
       
    33 // 
       
    34 //
       
    35 
       
    36 
       
    37 #include "CMtfTestActionSmtpCreateAttachment.h"
       
    38 #include "CMtfAsyncWaiter.h"
       
    39 #include "CMtfTestCase.h"
       
    40 #include "CMtfTestActionParameters.h"
       
    41 #include "MtfTestActionUtilsUser.h"
       
    42 
       
    43 #include <miutset.h>
       
    44 #include <mmsvattachmentmanager.h>
       
    45 #include <cmsvattachment.h>
       
    46 #include <miutmsg.h>
       
    47 
       
    48 CMtfTestAction* CMtfTestActionSmtpCreateAttachment::NewL(CMtfTestCase& aTestCase,CMtfTestActionParameters* aActionParameters)
       
    49 	{
       
    50 	CMtfTestActionSmtpCreateAttachment* self = new(ELeave) CMtfTestActionSmtpCreateAttachment(aTestCase);
       
    51 	CleanupStack::PushL(self);
       
    52 	self->ConstructL(aActionParameters);
       
    53 	CleanupStack::Pop();
       
    54 	return self;
       
    55 	}
       
    56 	
       
    57 
       
    58 CMtfTestActionSmtpCreateAttachment::CMtfTestActionSmtpCreateAttachment(CMtfTestCase& aTestCase)
       
    59 	: CMtfSynchronousTestAction(aTestCase)
       
    60 	{
       
    61 	}
       
    62 
       
    63 
       
    64 CMtfTestActionSmtpCreateAttachment::~CMtfTestActionSmtpCreateAttachment()
       
    65 	{
       
    66 	}
       
    67 
       
    68 void CMtfTestActionSmtpCreateAttachment::ExecuteActionL()
       
    69 	{
       
    70 	TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionSmtpCreateAttachment);
       
    71 	TRAPD(err, RunTestL());
       
    72 	
       
    73 	if( ActionParameters().Count() < 5 )
       
    74 		{
       
    75 		User::LeaveIfError(err);
       
    76 		}
       
    77 	else
       
    78 		{
       
    79 		StoreParameterL<TInt>(TestCase(),err,ActionParameters().Parameter(4));
       
    80 		}
       
    81 		
       
    82 	TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionSmtpCreateAttachment);
       
    83 	TestCase().ActionCompletedL(*this); 
       
    84 	}
       
    85 	
       
    86 void CMtfTestActionSmtpCreateAttachment::RunTestL()
       
    87 	{
       
    88 	_LIT(KTxtFileName, "NewFile.txt");
       
    89 	_LIT8(KTxtFileMimeType, "*/*");
       
    90 	
       
    91 	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
       
    92 	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
       
    93 	HBufC* dataFilePath   = ObtainParameterReferenceL<HBufC>(TestCase(),ActionParameters().Parameter(2));
       
    94 
       
    95 	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
       
    96 	CleanupStack::PushL(entry);
       
    97 	
       
    98 	CImEmailMessage* emailMsg = CImEmailMessage::NewL(*entry);
       
    99 	CleanupStack::PushL(emailMsg);
       
   100 
       
   101 	CMtfAsyncWaiter* waiter = CMtfAsyncWaiter::NewL();
       
   102 	CleanupStack::PushL(waiter);
       
   103 	
       
   104 	CMsvAttachment* attachment = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
       
   105 	CleanupStack::PushL(attachment);
       
   106 		
       
   107 	attachment->SetMimeTypeL(KTxtFileMimeType());	
       
   108 	attachment->SetAttachmentNameL(KTxtFileName());
       
   109 	
       
   110 	MMsvAttachmentManager& manager = emailMsg->AttachmentManager();
       
   111 	
       
   112 	RFile newFile;
       
   113 	manager.CreateAttachmentL(KTxtFileName, newFile, attachment, waiter->iStatus);
       
   114 	CleanupStack::Pop(attachment); // ownership passed to manager
       
   115 	waiter->StartAndWait();
       
   116 	
       
   117 	attachment = NULL;
       
   118 	
       
   119 	CleanupClosePushL(newFile);
       
   120 	User::LeaveIfError(waiter->Result());
       
   121 	CleanupStack::Pop(&newFile);
       
   122 	
       
   123 	CleanupStack::PopAndDestroy(waiter);
       
   124 	
       
   125 	CleanupClosePushL(newFile);
       
   126 	
       
   127 	TInt attachmentCount = manager.AttachmentCount();
       
   128 	attachment = manager.GetAttachmentInfoL(attachmentCount - 1);
       
   129 	CleanupStack::PushL(attachment);
       
   130 	TMsvAttachmentId attachmentId = attachment->Id();
       
   131 	
       
   132 	PopulateFileL(newFile, *dataFilePath);
       
   133 	
       
   134 	CleanupStack::PopAndDestroy(attachment);
       
   135 	CleanupStack::PopAndDestroy(&newFile);
       
   136 	CleanupStack::PopAndDestroy(2, entry); // emailMsg, entry	
       
   137 		
       
   138 	StoreParameterL<TMsvAttachmentId>(TestCase(),attachmentId,ActionParameters().Parameter(3));
       
   139 	}
       
   140 
       
   141 void CMtfTestActionSmtpCreateAttachment::PopulateFileL(RFile& aFile, const TDesC& aDataFilePath)
       
   142 	{
       
   143 	RFs fs;
       
   144 	User::LeaveIfError(fs.Connect());
       
   145 	CleanupClosePushL(fs);
       
   146 	
       
   147 	RFile dataFile;
       
   148 	User::LeaveIfError(dataFile.Open(fs, aDataFilePath, EFileRead));
       
   149 	CleanupClosePushL(fs);
       
   150 	
       
   151 	TInt fileSize = 0;
       
   152 	User::LeaveIfError(dataFile.Size(fileSize));
       
   153 	
       
   154 	HBufC8* dataBuffer = HBufC8::NewLC(fileSize);
       
   155 	TPtr8 bufferPtr(dataBuffer->Des());
       
   156 	
       
   157 	User::LeaveIfError(dataFile.Read(bufferPtr));
       
   158 	
       
   159 	User::LeaveIfError(aFile.Write(bufferPtr));
       
   160 	
       
   161 	CleanupStack::PopAndDestroy(3, &fs); // dataBuffer, dataFile, fs
       
   162 	}