messagingappbase/obexmtms/TObexMTM/SRC/SendMessageState.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2002-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 // $Workfile: SendMessageState.cpp $
       
    15 // $Author: Stevep $
       
    16 // $Revision: 8 $
       
    17 // $Date: 27/03/02 10:49 $
       
    18 // 
       
    19 //
       
    20 
       
    21 #include "sendmessagestate.h"
       
    22 
       
    23 //TODO:- check that all these headers are required
       
    24 
       
    25 // System includes
       
    26 #include <btmsgtypeuid.h>   // KUidMsgTypeBt
       
    27 #include <btmtmcmds.h>      // TBtMtmAsyncCmds
       
    28 #include <irmsgtypeuid.h>   // KUidMsgTypeIr
       
    29 #include <irmtmcmds.h>      // TIrMtmAsyncCmds
       
    30 #include <msvuids.h>        // KUidMsvMessageEntry
       
    31 #include <obexclientmtm.h>
       
    32 #include <btcmtm.h>
       
    33 #include <ircmtm.h>
       
    34 
       
    35 #include <mmsvattachmentmanager.h>
       
    36 #include <cmsvattachment.h>
       
    37 
       
    38 #include "cobextestfilenameandheaders.h"
       
    39 #include "casyncwaiter.h"
       
    40 
       
    41 // User includes
       
    42 #include "obexharness.h"
       
    43 #include "obextestutils.h"
       
    44 #include "obextestheaderlist.h"
       
    45 
       
    46 // Stub support.
       
    47 _LIT(KStubConfigFolderName, "c:\\msgtest\\obexsmoke\\stub\\");
       
    48 _LIT(KStubConfigFileName, "c:\\msgtest\\obexsmoke\\stub\\conf.txt");
       
    49 
       
    50 // To test with the old "CObexClientMtm::STimeouts" structure, updated with
       
    51 // new "CIrClientMtm::STimeouts" structure by DEF36727, remove comment below.
       
    52 //#define VERIFY_DEF36727_USING_OLD_STURCT
       
    53 
       
    54 //
       
    55 // CSendMessageState
       
    56 //
       
    57 
       
    58 CSendMessageState::CSendMessageState(const TDesC& aStubDesiredResult, CObexClientTest& aClientTest)
       
    59 	:	CActiveTestState(EPriorityNormal),
       
    60 		iClientTest(aClientTest),
       
    61 		iStubDesiredResult(aStubDesiredResult)
       
    62 	{
       
    63 	iTimer = CPeriodic::NewL(EPriorityNormal);
       
    64 	CActiveScheduler::Add(this);
       
    65 	}
       
    66 
       
    67 void CSendMessageState::StartL(TRequestStatus& aStatus)
       
    68 	{
       
    69 	// If the "stub" folder is present, externalize desired result and
       
    70 	// verify headers.
       
    71 
       
    72 	RFs rfs;
       
    73 	CleanupClosePushL(rfs);		// PUSH
       
    74 	User::LeaveIfError(rfs.Connect());
       
    75 
       
    76 	RDir dir;
       
    77 	CleanupClosePushL(dir);
       
    78 
       
    79 	TInt fileErr = dir.Open(rfs, KStubConfigFolderName, KEntryAttDir);
       
    80 	if (fileErr == KErrNone)
       
    81 		{
       
    82 		CObexTestHeaderList* verifyHeaderList = iClientTest.VerifyHeaders();
       
    83 
       
    84 		verifyHeaderList->AutoExternaliseL(KStubConfigFileName, iStubDesiredResult);
       
    85 
       
    86 		// Clear the verify headers, ready for the next send.
       
    87 		iClientTest.ClearVerifyHeadersL();
       
    88 		}
       
    89 
       
    90 	CleanupStack::PopAndDestroy(2);
       
    91 
       
    92 
       
    93 	
       
    94 	// Add any attachments.
       
    95 	iReportStatus = &aStatus;
       
    96     CObexTestUtils& testUtils = iClientTest.ObexTestUtils();
       
    97 	CObexClientMtm&	obexMtm = (CObexClientMtm&)testUtils.GetCurrentObexClientMtm();
       
    98 	
       
    99 	// Create a "selection" object from the currently selected entry.
       
   100 	iSelection = new (ELeave) CMsvEntrySelection;
       
   101     iSelection->AppendL(testUtils.Entry().Id());
       
   102 
       
   103 	// Add each attachment to the message, if any.
       
   104 	RPointerArray<CObexTestFilenameAndHeaders>& attachments = iClientTest.Attachments();
       
   105 
       
   106 	if (attachments.Count() > 0)
       
   107 		{
       
   108 		CMsvEntry& entry = obexMtm.Entry();
       
   109 		CMsvStore* store = entry.EditStoreL();
       
   110 		CleanupStack::PushL(store);
       
   111 		MMsvAttachmentManager& manager = store->AttachmentManagerL();
       
   112 		TInt count = manager.AttachmentCount();
       
   113 		for( TInt ii=0; ii<count; ++ii )
       
   114 			{
       
   115 			CAsyncWaiter* waiter = CAsyncWaiter::NewL();
       
   116 			CleanupStack::PushL(waiter);
       
   117 			
       
   118 			CMsvAttachment* attachment = CMsvAttachment::NewL(CMsvAttachment::EMsvLinkedFile);
       
   119 			CleanupStack::PushL(attachment);
       
   120 			// Add the headers if any have been set
       
   121 			CObexHeaderList* headerList = attachments[ii]->iHeaderList;
       
   122 			if( headerList != NULL )
       
   123 				{
       
   124 				headerList->ExportToAttachmentL(*attachment);
       
   125 				}
       
   126 			
       
   127 			TParse fileNameParser;
       
   128 			User::LeaveIfError(fileNameParser.Set(attachments[ii]->iFileName, NULL, NULL));
       
   129 			attachment->SetAttachmentNameL(fileNameParser.NameAndExt());
       
   130 			manager.AddLinkedAttachmentL(attachments[ii]->iFileName, attachment, waiter->iStatus);
       
   131 			CleanupStack::Pop(attachment);
       
   132 			waiter->StartAndWait();
       
   133 			User::LeaveIfError(waiter->Result());
       
   134 			CleanupStack::PopAndDestroy(waiter);
       
   135 			}
       
   136 		store->CommitL();
       
   137 		CleanupStack::PopAndDestroy(store);
       
   138 		}
       
   139 	iClientTest.ClearAttachmentsL();
       
   140 
       
   141 
       
   142 	// Now carry out the send.
       
   143 	if(testUtils.CurrentObexMtm() == KUidMsgTypeBt)
       
   144 		{
       
   145 		CBtClientMtm::SBtcCmdSendParams sendParams;
       
   146 			// if not, we set the password length to zero
       
   147 			iConnectPassword.SetLength(0);
       
   148 
       
   149 		// initialise T-Class to something sensible.
       
   150 		sendParams.iTimeouts.iPutTimeout = 300000000;
       
   151 		sendParams.iTimeouts.iConnectTimeout = 300000000;
       
   152 		sendParams.iRemoteObexPort = 0;
       
   153 		sendParams.iConnectPassword = &iConnectPassword;
       
   154 
       
   155 		TPckgBuf<CBtClientMtm::SBtcCmdSendParams> sendParamsBuf(sendParams);
       
   156 
       
   157 		// Invoke the "send" command on the Obex Client MTM,
       
   158 		// asking it to notify us when it completes (which will be when the
       
   159 		// message has actually been sent).
       
   160 		
       
   161 		// Note that the current obex mtm needs to have been set
       
   162     	testUtils.InstantiateBTClientMtmL();
       
   163 		testUtils.GetCurrentObexClientMtm().SwitchCurrentEntryL(testUtils.Entry().Id());
       
   164 		testUtils.GetCurrentObexClientMtm().LoadMessageL();
       
   165 		iClientTest.SetCurrentOperation(testUtils.GetCurrentObexClientMtm().InvokeAsyncFunctionL(EBtMtmCmdSend, *iSelection, sendParamsBuf, iStatus));
       
   166 		}
       
   167 	else
       
   168 		{
       
   169 #ifdef VERIFY_DEF36727_USING_OLD_STURCT
       
   170 		CObexClientMtm::STimeouts sendParams;
       
   171 #else
       
   172 		CIrClientMtm::STimeouts sendParams;
       
   173 #endif
       
   174 
       
   175 		// initialise T-Class to something sensible.
       
   176 		sendParams.iPutTimeout = 300000000;
       
   177 		sendParams.iConnectTimeout = 300000000;
       
   178 
       
   179 #ifdef VERIFY_DEF36727_USING_OLD_STURCT		
       
   180 		TPckgBuf<CObexClientMtm::STimeouts> sendParamsBuf(sendParams);
       
   181 #else
       
   182 		TPckgBuf<CIrClientMtm::STimeouts> sendParamsBuf(sendParams);
       
   183 #endif
       
   184 
       
   185 		// Invoke the "send" command on the Obex Client MTM,
       
   186 		// asking it to notify us when it completes (which will be when the
       
   187 		// message has actually been sent).
       
   188 		
       
   189 		// Note that the current obex mtm needs to have been set
       
   190     
       
   191 		testUtils.InstantiateIRClientMtmL();
       
   192 		testUtils.GetCurrentObexClientMtm().SwitchCurrentEntryL(testUtils.Entry().Id());
       
   193 		testUtils.GetCurrentObexClientMtm().LoadMessageL();
       
   194 		iClientTest.SetCurrentOperation(testUtils.GetCurrentObexClientMtm().InvokeAsyncFunctionL(CIrClientMtm::EIrcCmdSend, *iSelection, sendParamsBuf, iStatus));
       
   195 		}
       
   196 
       
   197 	// If we want to cancel this request after a certain time, set up
       
   198 	// the timer which will notify us when the time is up.
       
   199 	if (iClientTest.CancelGranularity() > 0)
       
   200 		{
       
   201 		iCancel = ETrue;
       
   202 		// NB: the timer interval is specified in microseconds
       
   203 		iTimer->Start(iClientTest.CancelGranularity() * 1000,
       
   204 			iClientTest.CancelGranularity() * 1000,
       
   205 			TCallBack(TimerCallback, this));
       
   206 		}
       
   207 	// If we're checking the progress every n milliseconds, set up the
       
   208 	// timer to check this.
       
   209 	else if (iClientTest.ProgressGranularity() > 0)
       
   210 		{
       
   211 		iCheckProgress = ETrue;
       
   212 		// NB: the timer interval is specified in microseconds
       
   213 		iTimer->Start(1, iClientTest.ProgressGranularity() * 1000,
       
   214 			TCallBack(TimerCallback, this));
       
   215 		}
       
   216 
       
   217 	// tell the caller that we're waiting for the request to complete
       
   218 	aStatus = KRequestPending;
       
   219 	SetActive();
       
   220 	}
       
   221 
       
   222 TInt CSendMessageState::TimerCallback(TAny* aPtr)
       
   223 	{
       
   224 	CSendMessageState* self = static_cast<CSendMessageState*>(aPtr);
       
   225 
       
   226 	// if we want to cancel the timer, then do so
       
   227 	if (self->iCancel)
       
   228 		{
       
   229 		// stop the timer
       
   230 		self->iTimer->Cancel();
       
   231 		delete self->iTimer;
       
   232 		self->iTimer = NULL;
       
   233 		
       
   234 		// cancel and delete the operation
       
   235 		self->iClientTest.CurrentOperation().Cancel();
       
   236 		self->iClientTest.SetCurrentOperation(NULL);
       
   237 
       
   238 		self->iCancelled = ETrue;
       
   239 
       
   240 		// tell the caller that we're complete - if the send isn't
       
   241 		// already complete
       
   242 		if (!self->iSendComplete)
       
   243 			User::RequestComplete(self->iReportStatus, KErrNone);
       
   244 		}
       
   245 	else if (self->iCheckProgress)
       
   246 		{
       
   247 		TPckgBuf<TObexMtmProgress> package;
       
   248 // Assume that a callback function shouldn't leave
       
   249 		TRAPD(err, package.Copy(self->iClientTest.CurrentOperation().ProgressL()));
       
   250 
       
   251         if(err == KErrNone)
       
   252             {
       
   253 		    TBuf<256> progressBuf;
       
   254 			_LIT(KFormatString, "Sent %d/%d messages (%d/%d bytes): error code = %d");
       
   255 		    progressBuf.Format(KFormatString,
       
   256 			    package().iEntriesDone, 
       
   257                 package().iTotalEntryCount,
       
   258 			    package().iCurrentBytesTrans, 
       
   259                 package().iCurrentEntrySize,
       
   260 			    package().iError);
       
   261 		    TRAP(err, self->iClientTest.LogCommentL(progressBuf));
       
   262             }
       
   263 
       
   264 		if (self->iSendComplete)
       
   265 			{
       
   266 			self->iTimer->Cancel();
       
   267 			delete self->iTimer;
       
   268 			self->iTimer = NULL;
       
   269 			self->iClientTest.SetCurrentOperation(NULL);
       
   270 			return EFalse;
       
   271 			}
       
   272 		return ETrue;
       
   273 		}
       
   274 	return EFalse;
       
   275 	}
       
   276 
       
   277 void CSendMessageState::RunL()
       
   278 	{
       
   279 	// set the send to be complete
       
   280 	iSendComplete = ETrue;
       
   281 	
       
   282 	if (iCancelled)
       
   283 		{
       
   284 		// select the parent entry (usually the Outbox) for convenience
       
   285 		iClientTest.ObexTestUtils().SetEntryL(
       
   286 			iClientTest.ObexTestUtils().Entry().Parent());
       
   287 
       
   288 		// exit now, before progress is checked
       
   289 		return;
       
   290 		}
       
   291 
       
   292 	// Check the progress so that we can extract the error code, to see
       
   293 	// whether the send operation succeeded or failed.
       
   294 	TPckgBuf<TObexMtmProgress> package;
       
   295 	package.Copy(iClientTest.CurrentOperation().ProgressL());
       
   296 	if (package().iError != KErrNone)
       
   297 		{
       
   298         TInt err = iClientTest.IgnoreSendErrors() ? KErrNone : package().iError;
       
   299 		User::RequestComplete(iReportStatus, err);
       
   300 		}
       
   301 	else
       
   302 		{
       
   303 		User::RequestComplete(iReportStatus, iStatus.Int());
       
   304 		}
       
   305 
       
   306 	// NB: we can't delete the message operation if we need to
       
   307 	// check for progress etc., because it will be used by a timer event
       
   308 	// after this point.
       
   309 	if (!iCheckProgress && !iCancel)
       
   310 		iClientTest.SetCurrentOperation(NULL);
       
   311 
       
   312 	// select the parent entry (usually the Outbox) for convenience
       
   313 	iClientTest.ObexTestUtils().SetEntryL(
       
   314 		iClientTest.ObexTestUtils().Entry().Parent());
       
   315 	}
       
   316 
       
   317 void CSendMessageState::DoCancel()
       
   318 	{
       
   319 	iClientTest.CurrentOperation().Cancel();
       
   320 	// delete the operation
       
   321 	iClientTest.SetCurrentOperation(NULL);
       
   322 	}
       
   323 
       
   324 CSendMessageState::~CSendMessageState()
       
   325 	{
       
   326 	delete iTimer;
       
   327 	if (iSelection)
       
   328 		iSelection->Reset();
       
   329 	delete iSelection;
       
   330 	}
       
   331 
       
   332 //
       
   333 //
       
   334