messagingfw/msgtestfw/TestActions/Base/Attachments/src/CMtfTestActionCreateAttachment.cpp
changeset 62 db3f5fa34ec7
parent 0 8e480a14352b
equal deleted inserted replaced
60:9f5ae1728557 62:db3f5fa34ec7
       
     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 // CreateAttachment
       
    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 // CMsvStore::EditStoreL
       
    29 // CMsvStore::AttachmentManagerL
       
    30 // CMsvAttachment::NewL
       
    31 // CMsvAttachment::SetMimeType
       
    32 // MMsvAttachmentManager::CreateAttachmentL
       
    33 // __ACTION_INFO_END__
       
    34 // 
       
    35 //
       
    36 
       
    37 
       
    38 #include "CMtfTestActionCreateAttachment.h"
       
    39 #include "CMtfAsyncWaiter.h"
       
    40 #include "CMtfTestCase.h"
       
    41 #include "CMtfTestActionParameters.h"
       
    42 #include "MtfTestActionUtilsUser.h"
       
    43 
       
    44 #include <miutset.h>
       
    45 #include <mmsvattachmentmanager.h>
       
    46 #include <cmsvattachment.h>
       
    47 
       
    48 CMtfTestAction* CMtfTestActionCreateAttachment::NewL(CMtfTestCase& aTestCase,CMtfTestActionParameters* aActionParameters)
       
    49 	{
       
    50 	CMtfTestActionCreateAttachment* self = new(ELeave) CMtfTestActionCreateAttachment(aTestCase);
       
    51 	CleanupStack::PushL(self);
       
    52 	self->ConstructL(aActionParameters);
       
    53 	CleanupStack::Pop();
       
    54 	return self;
       
    55 	}
       
    56 	
       
    57 
       
    58 CMtfTestActionCreateAttachment::CMtfTestActionCreateAttachment(CMtfTestCase& aTestCase)
       
    59 	: CMtfSynchronousTestAction(aTestCase)
       
    60 	{
       
    61 	}
       
    62 
       
    63 
       
    64 CMtfTestActionCreateAttachment::~CMtfTestActionCreateAttachment()
       
    65 	{
       
    66 	}
       
    67 
       
    68 void CMtfTestActionCreateAttachment::ExecuteActionL()
       
    69 	{
       
    70 	TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionCreateAttachment);
       
    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 	TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionCreateAttachment);	
       
    82 	TestCase().ActionCompletedL(*this); 
       
    83 	}
       
    84 	
       
    85 void CMtfTestActionCreateAttachment::RunTestL()
       
    86 	{
       
    87 	_LIT(KTxtFileName, "NewFile.txt");
       
    88 	_LIT8(KTxtFileMimeType, "*/*");
       
    89 	
       
    90 	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
       
    91 	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
       
    92 	HBufC* dataFilePath   = ObtainParameterReferenceL<HBufC>(TestCase(),ActionParameters().Parameter(2));
       
    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(KTxtFileMimeType());
       
   107 	attachment->SetAttachmentNameL(KTxtFileName());
       
   108 	
       
   109 	
       
   110 	MMsvAttachmentManager& manager = store->AttachmentManagerL();
       
   111 	RFile newFile;
       
   112 	manager.CreateAttachmentL(KTxtFileName, newFile, attachment, waiter->iStatus);
       
   113 	CleanupStack::Pop(attachment); // ownership passed to manager
       
   114 	waiter->StartAndWait();
       
   115 	CleanupClosePushL(newFile);
       
   116 	User::LeaveIfError(waiter->Result());
       
   117 	
       
   118 	TMsvAttachmentId attachmentId = attachment->Id();
       
   119 	
       
   120 	TInt fileSize = PopulateFileL(newFile, *dataFilePath);
       
   121 	attachment->SetSize(fileSize);
       
   122 	
       
   123 	CleanupStack::PopAndDestroy(&newFile);
       
   124 	CleanupStack::PopAndDestroy(waiter);
       
   125 	
       
   126 	store->CommitL();
       
   127 	
       
   128 	CleanupStack::PopAndDestroy(2, entry); // store, entry
       
   129 	
       
   130 	StoreParameterL<TMsvAttachmentId>(TestCase(),attachmentId,ActionParameters().Parameter(3));
       
   131 	}
       
   132 
       
   133 TInt CMtfTestActionCreateAttachment::PopulateFileL(RFile& aFile, const TDesC& aDataFilePath)
       
   134 	{
       
   135 	RFs fs;
       
   136 	User::LeaveIfError(fs.Connect());
       
   137 	CleanupClosePushL(fs);
       
   138 	
       
   139 	RFile dataFile;
       
   140 	User::LeaveIfError(dataFile.Open(fs, aDataFilePath, EFileRead));
       
   141 	CleanupClosePushL(fs);
       
   142 	
       
   143 	TInt fileSize = 0;
       
   144 	User::LeaveIfError(dataFile.Size(fileSize));
       
   145 	
       
   146 	HBufC8* dataBuffer = HBufC8::NewLC(fileSize);
       
   147 	TPtr8 bufferPtr(dataBuffer->Des());
       
   148 	
       
   149 	User::LeaveIfError(dataFile.Read(bufferPtr));
       
   150 	
       
   151 	User::LeaveIfError(aFile.Write(bufferPtr));
       
   152 	
       
   153 	CleanupStack::PopAndDestroy(3, &fs); // dataBuffer, dataFile, fs
       
   154 	return fileSize;
       
   155 	}
       
   156