messagingfw/msgtestfw/TestActions/Base/Attachments/src/CMtfTestActionAddFileAttachmentByHandleSync.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 // AddFileAttachmentByHandleSync
       
    17 // [Action Parameters]
       
    18 // Session        <input>: Reference to the session.
       
    19 // MsgId		  <input>: Value of the message Id.
       
    20 // FilePath       <input>: File path of the attachment file.
       
    21 // MimeType		  <input>: Mime-type of the attachment.
       
    22 // AttachmentId   <output>: Value of the created attachment id.
       
    23 // ErrorCode	  <output>: (Optional) Returned error code, if not requested then code will
       
    24 // leave if not KErrNone
       
    25 // [Action Description]
       
    26 // Creates a file handle from the specified file and synchronously adds the open
       
    27 // file handle as an attachment to the specified message entry.
       
    28 // [APIs Used]
       
    29 // CMsvSession::GetEntryL
       
    30 // CMsvStore::EditStoreL
       
    31 // CMsvStore::AttachmentManagerL
       
    32 // CMsvAttachment::NewL
       
    33 // CMsvAttachment::SetMimeType
       
    34 // MMsvAttachmentManager::AddAttachmentL
       
    35 // __ACTION_INFO_END__
       
    36 // 
       
    37 //
       
    38 
       
    39 
       
    40 #include "CMtfTestActionAddFileAttachmentByHandleSync.h"
       
    41 #include "CMtfAsyncWaiter.h"
       
    42 #include "CMtfTestCase.h"
       
    43 #include "CMtfTestActionParameters.h"
       
    44 #include "MtfTestActionUtilsUser.h"
       
    45 
       
    46 #include <miutset.h>
       
    47 #include <cmsvattachment.h>
       
    48 #include <mmsvattachmentmanager.h>
       
    49 
       
    50 CMtfTestAction* CMtfTestActionAddFileAttachmentByHandleSync::NewL(CMtfTestCase& aTestCase,CMtfTestActionParameters* aActionParameters)
       
    51 	{
       
    52 	CMtfTestActionAddFileAttachmentByHandleSync* self = new(ELeave) CMtfTestActionAddFileAttachmentByHandleSync(aTestCase);
       
    53 	CleanupStack::PushL(self);
       
    54 	self->ConstructL(aActionParameters);
       
    55 	CleanupStack::Pop();
       
    56 	return self;
       
    57 	}
       
    58 	
       
    59 
       
    60 CMtfTestActionAddFileAttachmentByHandleSync::CMtfTestActionAddFileAttachmentByHandleSync(CMtfTestCase& aTestCase)
       
    61 	: CMtfSynchronousTestAction(aTestCase)
       
    62 	{
       
    63 	}
       
    64 
       
    65 
       
    66 CMtfTestActionAddFileAttachmentByHandleSync::~CMtfTestActionAddFileAttachmentByHandleSync()
       
    67 	{
       
    68 	}
       
    69 
       
    70 void CMtfTestActionAddFileAttachmentByHandleSync::ExecuteActionL()
       
    71 	{
       
    72 	TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionAddFileAttachmentByHandleSync);
       
    73 	TRAPD(err, RunTestL());
       
    74 	
       
    75 	if( ActionParameters().Count() < 6 )
       
    76 		{
       
    77 		User::LeaveIfError(err);
       
    78 		}
       
    79 	else
       
    80 		{
       
    81 		StoreParameterL<TInt>(TestCase(),err,ActionParameters().Parameter(5));
       
    82 		}
       
    83 	TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionAddFileAttachmentByHandleSync);	
       
    84 	TestCase().ActionCompletedL(*this); 
       
    85 	}
       
    86 
       
    87 void CMtfTestActionAddFileAttachmentByHandleSync::RunTestL()
       
    88 	{
       
    89 	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
       
    90 	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
       
    91 	HBufC* paramFilePath   = ObtainParameterReferenceL<HBufC>(TestCase(),ActionParameters().Parameter(2));
       
    92 	HBufC8* paramMimeType = ObtainParameterReferenceL<HBufC8>(TestCase(),ActionParameters().Parameter(3));
       
    93 
       
    94 	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
       
    95 	CleanupStack::PushL(entry);
       
    96 	
       
    97 	CMsvStore* store = entry->EditStoreL();
       
    98 	CleanupStack::PushL(store);
       
    99 	
       
   100 	CMtfAsyncWaiter* waiter = CMtfAsyncWaiter::NewL();
       
   101 	CleanupStack::PushL(waiter);
       
   102 	
       
   103 	CMsvAttachment* attachment = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
       
   104 	CleanupStack::PushL(attachment);
       
   105 
       
   106 	attachment->SetMimeTypeL(*paramMimeType);
       
   107 	
       
   108 	MMsvAttachmentManager& manager = store->AttachmentManagerL();
       
   109 	
       
   110 	RFile fileHandle = OpenFileLC(paramSession->FileSession(), *paramFilePath);
       
   111 	
       
   112 	TFileName fileName;
       
   113 	User::LeaveIfError(fileHandle.Name(fileName));
       
   114 	attachment->SetAttachmentNameL(fileName);
       
   115 	TInt fileSize = 0;
       
   116 	User::LeaveIfError(fileHandle.Size(fileSize));
       
   117 	attachment->SetSize(fileSize);
       
   118 
       
   119 	manager.AddAttachmentL(fileHandle, attachment, waiter->iStatus);
       
   120 	CleanupStack::Pop(2, attachment); // fileHandle, attachment, ownership passed to manager
       
   121 	waiter->StartAndWait();
       
   122 	User::LeaveIfError(waiter->Result());
       
   123 	CleanupStack::PopAndDestroy(waiter);
       
   124 	
       
   125 	TMsvAttachmentId attachmentId = attachment->Id();
       
   126 	
       
   127 	store->CommitL();
       
   128 	
       
   129 	CleanupStack::PopAndDestroy(2, entry); // store, entry
       
   130 	
       
   131 	StoreParameterL<TMsvAttachmentId>(TestCase(),attachmentId,ActionParameters().Parameter(4));
       
   132 	}
       
   133 
       
   134 RFile CMtfTestActionAddFileAttachmentByHandleSync::OpenFileLC(RFs& aFs, const TDesC& aFilePath)
       
   135 	{
       
   136 	RFile fileHandle;
       
   137 	User::LeaveIfError(fileHandle.Open(aFs, aFilePath, EFileRead));
       
   138 	CleanupClosePushL(fileHandle);
       
   139 	return fileHandle;
       
   140 	}
       
   141 	
       
   142