messagingfw/sendas/test/sendastestmtm/src/csendastestservermtm.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 // csendastestservermtm.cpp
       
    15 //
       
    16 #include "csendastestservermtm.h"
       
    17 
       
    18 #include <mtclbase.h>
       
    19 #include <msventry.h>
       
    20 #include <msvids.h>
       
    21 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS  
       
    22 #include "msvconsts.h"
       
    23 #endif
       
    24 
       
    25 const TInt KSendAsTestServerSendDelay	= 1000000;
       
    26 
       
    27 EXPORT_C CSendAsTestServerMtm* CSendAsTestServerMtm::NewL(CRegisteredMtmDll& aRegisteredMtmDll, CMsvServerEntry* aServerEntry)
       
    28 	{
       
    29 	CSendAsTestServerMtm* self = new (ELeave) CSendAsTestServerMtm(aRegisteredMtmDll, aServerEntry);
       
    30 	CleanupStack::PushL(self);
       
    31 	self->ConstructL();
       
    32 	CleanupStack::Pop(self);
       
    33 	return self;
       
    34 	}
       
    35 
       
    36 CSendAsTestServerMtm::~CSendAsTestServerMtm()
       
    37 	{
       
    38 	Cancel();
       
    39 	
       
    40 	iTimer.Close();
       
    41 	delete iSelection;
       
    42 	}
       
    43 
       
    44 CSendAsTestServerMtm::CSendAsTestServerMtm(CRegisteredMtmDll& aRegisteredMtmDll, CMsvServerEntry* aServerEntry)
       
    45 : CBaseServerMtm(aRegisteredMtmDll, aServerEntry)
       
    46 	{
       
    47 	CActiveScheduler::Add(this);
       
    48 	}
       
    49 
       
    50 void CSendAsTestServerMtm::ConstructL()
       
    51 	{
       
    52 	User::LeaveIfError(iTimer.CreateLocal());
       
    53 	}
       
    54 	
       
    55 void CSendAsTestServerMtm::SendMessagesL(const CMsvEntrySelection& aSelection, TRequestStatus& aStatus)
       
    56 	{
       
    57 	aStatus = KRequestPending;
       
    58 	iRequestStatus = &aStatus;
       
    59 	
       
    60 	iProgress.iMessagesDone = 0;
       
    61 	iProgress.iMessageCount = aSelection.Count();
       
    62 
       
    63 	// Are there any messages to send?
       
    64 	if( iProgress.iMessageCount > 0 )
       
    65 		{
       
    66 		// Copy the selection of messages...
       
    67 		delete iSelection;
       
    68 		iSelection = NULL;
       
    69 		iSelection = aSelection.CopyL();
       
    70 		
       
    71 		iProgress.iState = TSendAsTestMtmProgress::ESendAsTestInitSend;
       
    72 		CompleteSelf();		
       
    73 		}
       
    74 	else
       
    75 		{
       
    76 		// Complete the request...
       
    77 		DoComplete(KErrNone);
       
    78 		}
       
    79 	}
       
    80 	
       
    81 void CSendAsTestServerMtm::DoInitialiseSendL()
       
    82 	{
       
    83 	// Set the message send state to waiting.
       
    84 	TInt count = iSelection->Count();
       
    85 	for( TInt i=0; i<count; ++i )
       
    86 		{
       
    87 		User::LeaveIfError(iServerEntry->SetEntry(iSelection->At(i)));
       
    88 		iEntry = iServerEntry->Entry();
       
    89 		iEntry.SetSendingState(KMsvSendStateWaiting);
       
    90 		User::LeaveIfError(iServerEntry->ChangeEntry(iEntry));
       
    91 		}	
       
    92 	iServerEntry->SetEntry(KMsvNullIndexEntryId);	// ignore error...
       
    93 	
       
    94 	// Set the current message to first message in selection and move onto the
       
    95 	// next one.
       
    96 	iCurrentMessage = iSelection->At(0);
       
    97 	++iProgress.iMessagesDone;
       
    98 	
       
    99 	iProgress.iState = TSendAsTestMtmProgress::ESendAsTestSendNext;
       
   100 	CompleteSelf();	
       
   101 	}
       
   102 	
       
   103 void CSendAsTestServerMtm::DoSendMessageL()
       
   104 	{
       
   105 	// Get the current message.
       
   106 	User::LeaveIfError(iServerEntry->SetEntry(iCurrentMessage));
       
   107 	iEntry = iServerEntry->Entry();
       
   108 
       
   109 	// Change the message send state to sending.
       
   110 	iEntry.SetSendingState(KMsvSendStateSending);
       
   111 	User::LeaveIfError(iServerEntry->ChangeEntry(iEntry));
       
   112 	
       
   113 	// Pretend to send - just wait a bit...
       
   114 	iProgress.iState = TSendAsTestMtmProgress::ESendAsTestSending;
       
   115 	iTimer.After(iStatus, KSendAsTestServerSendDelay);
       
   116 	SetActive();
       
   117 	}
       
   118 	
       
   119 void CSendAsTestServerMtm::DoMoveToSentFolderL()
       
   120 	{
       
   121 	__ASSERT_DEBUG( iCurrentMessage == iEntry.Id(), User::Invariant() );
       
   122 	__ASSERT_DEBUG( iEntry == iServerEntry->Entry(), User::Invariant() );
       
   123 	
       
   124 	// Update the sending state and move to the sent folder.
       
   125 	iEntry.SetSendingState(KMsvSendStateSent);
       
   126 	User::LeaveIfError(iServerEntry->ChangeEntry(iEntry));
       
   127 
       
   128 	User::LeaveIfError(iServerEntry->SetEntry(iEntry.Parent()));
       
   129 	User::LeaveIfError(iServerEntry->MoveEntryWithinService(iEntry.Id(),KMsvSentEntryId));
       
   130 	User::LeaveIfError(iServerEntry->SetEntry(KMsvNullIndexEntryId));
       
   131 	
       
   132 	iProgress.iState = TSendAsTestMtmProgress::ESendAsTestMoving;
       
   133 	CompleteSelf();
       
   134 	}
       
   135 	
       
   136 void CSendAsTestServerMtm::HandleSendErrorL(TInt aSendingState)
       
   137 	{
       
   138 	// Update the current message to reflect the error.
       
   139 	User::LeaveIfError(iServerEntry->SetEntry(iCurrentMessage));
       
   140 	iEntry = iServerEntry->Entry();
       
   141 	iEntry.iError = iProgress.iError;
       
   142 	iEntry.SetSendingState(aSendingState);
       
   143 	User::LeaveIfError(iServerEntry->ChangeEntry(iEntry));
       
   144 	
       
   145 	// Change the state of the remaining messages.
       
   146 	for(TInt i=iProgress.iMessagesDone; i<iProgress.iMessageCount; ++i )
       
   147 		{
       
   148 		User::LeaveIfError(iServerEntry->SetEntry(iSelection->At(i)));
       
   149 		iEntry = iServerEntry->Entry();
       
   150 		iEntry.iError = iProgress.iError;
       
   151 		iEntry.SetSendingState(aSendingState);
       
   152 		User::LeaveIfError(iServerEntry->ChangeEntry(iEntry));		
       
   153 		}
       
   154 	iServerEntry->SetEntry(KMsvNullIndexEntryId);	// ignore error...
       
   155 	}
       
   156 	
       
   157 void CSendAsTestServerMtm::CompleteSelf()
       
   158 	{
       
   159 	TRequestStatus* status=&iStatus;
       
   160 	User::RequestComplete(status, KErrNone);
       
   161 	SetActive();		
       
   162 	}
       
   163 	
       
   164 /*
       
   165  * Methods from CServerBaseMtm
       
   166  */
       
   167 
       
   168 void CSendAsTestServerMtm::CopyToLocalL(const CMsvEntrySelection& /*aSelection*/, TMsvId /*aDestination*/, TRequestStatus& aStatus)
       
   169 	{
       
   170 	TRequestStatus* status=&aStatus;
       
   171 	User::RequestComplete(status,KErrNotSupported);	
       
   172 	}
       
   173 
       
   174 void CSendAsTestServerMtm::CopyFromLocalL(const CMsvEntrySelection& /*aSelection*/, TMsvId /*aDestination*/, TRequestStatus& aStatus)
       
   175 	{
       
   176 	TRequestStatus* status=&aStatus;
       
   177 	User::RequestComplete(status,KErrNotSupported);	
       
   178 	}
       
   179 
       
   180 void CSendAsTestServerMtm::CopyWithinServiceL(const CMsvEntrySelection& /*aSelection*/, TMsvId /*aDestination*/, TRequestStatus& aStatus)
       
   181 	{
       
   182 	TRequestStatus* status=&aStatus;
       
   183 	User::RequestComplete(status,KErrNotSupported);	
       
   184 	}
       
   185 
       
   186 void CSendAsTestServerMtm::DeleteAllL(const CMsvEntrySelection& /*aSelection*/, TRequestStatus& aStatus)
       
   187 	{
       
   188 	TRequestStatus* status=&aStatus;
       
   189 	User::RequestComplete(status,KErrNotSupported);	
       
   190 	}
       
   191 
       
   192 void CSendAsTestServerMtm::CreateL(TMsvEntry /*aNewEntry*/, TRequestStatus& aStatus)
       
   193 	{
       
   194 	TRequestStatus* status=&aStatus;
       
   195 	User::RequestComplete(status,KErrNotSupported);	
       
   196 	}
       
   197 
       
   198 void CSendAsTestServerMtm::ChangeL(TMsvEntry /*aNewEntry*/, TRequestStatus& aStatus)
       
   199 	{
       
   200 	TRequestStatus* status=&aStatus;
       
   201 	User::RequestComplete(status,KErrNotSupported);	
       
   202 	}
       
   203 
       
   204 void CSendAsTestServerMtm::StartCommandL(CMsvEntrySelection& aSelection, TInt aCommand, const TDesC8& /*aParameter*/, TRequestStatus& aStatus)
       
   205 	{
       
   206 	__ASSERT_ALWAYS( iProgress.iState == TSendAsTestMtmProgress::ESendAsTestWaiting, User::Invariant() );
       
   207 
       
   208 	switch( aCommand )
       
   209 		{
       
   210 	case KMTMStandardFunctionsSendMessage:
       
   211 		{
       
   212 		// Send request - do the 'send'...
       
   213 		SendMessagesL(aSelection, aStatus);		
       
   214 		} break;
       
   215 	default:
       
   216 		TRequestStatus* status=&aStatus;
       
   217 		User::RequestComplete(status,KErrNotSupported);	
       
   218 		break;
       
   219 		};
       
   220 	}
       
   221 
       
   222 TBool CSendAsTestServerMtm::CommandExpected()
       
   223 	{
       
   224 	return EFalse;
       
   225 	}
       
   226 
       
   227 const TDesC8& CSendAsTestServerMtm::Progress()
       
   228 	{
       
   229 	iProgressBuf = TSendAsTestMtmProgressBuf(iProgress);
       
   230 	return iProgressBuf;
       
   231 	}
       
   232 
       
   233 void CSendAsTestServerMtm::MoveToLocalL(const CMsvEntrySelection& /*aSelection*/, TMsvId /*aDestination*/, TRequestStatus& aStatus)
       
   234 	{
       
   235 	TRequestStatus* status=&aStatus;
       
   236 	User::RequestComplete(status,KErrNotSupported);
       
   237 	}
       
   238 
       
   239 void CSendAsTestServerMtm::MoveFromLocalL(const CMsvEntrySelection& /*aSelection*/, TMsvId /*aDestination*/, TRequestStatus& aStatus)
       
   240 	{
       
   241 	TRequestStatus* status=&aStatus;
       
   242 	User::RequestComplete(status,KErrNotSupported);	
       
   243 	}
       
   244 
       
   245 void CSendAsTestServerMtm::MoveWithinServiceL(const CMsvEntrySelection& /*aSelection*/, TMsvId /*aDestination*/, TRequestStatus& aStatus)
       
   246 	{
       
   247 	TRequestStatus* status=&aStatus;
       
   248 	User::RequestComplete(status,KErrNotSupported);		
       
   249 	}
       
   250 
       
   251 void CSendAsTestServerMtm::DoRunL()
       
   252 	{
       
   253 	User::LeaveIfError(iStatus.Int());
       
   254 	
       
   255 	switch( iProgress.iState )
       
   256 		{
       
   257 	case TSendAsTestMtmProgress::ESendAsTestInitSend:
       
   258 		{
       
   259 		DoInitialiseSendL();
       
   260 		} break;
       
   261 	case TSendAsTestMtmProgress::ESendAsTestSendNext:
       
   262 		{
       
   263 		DoSendMessageL();
       
   264 		} break;
       
   265 	case TSendAsTestMtmProgress::ESendAsTestSending:
       
   266 		{
       
   267 		// Check the error value of the message entry - this has been set to
       
   268 		// indicate whether the send should have succeeded or not.
       
   269 		User::LeaveIfError(iEntry.iError);
       
   270 		
       
   271 		// The message was sent ok - move message to the sent folder.
       
   272 		DoMoveToSentFolderL();
       
   273 		} break;
       
   274 	case TSendAsTestMtmProgress::ESendAsTestMoving:
       
   275 		{
       
   276 		if( iProgress.iMessagesDone < iProgress.iMessageCount )
       
   277 			{
       
   278 			// There are still messages to send - move onto the next...
       
   279 			iCurrentMessage = iSelection->At(iProgress.iMessagesDone);
       
   280 
       
   281 			// Move onto the next message...
       
   282 			++iProgress.iMessagesDone;
       
   283 			
       
   284 			iProgress.iState = TSendAsTestMtmProgress::ESendAsTestSending;
       
   285 			CompleteSelf();
       
   286 			}
       
   287 		else
       
   288 			{
       
   289 			// All messages sent - complete request
       
   290 			DoComplete(KErrNone);
       
   291 			}
       
   292 		} break;
       
   293 	case TSendAsTestMtmProgress::ESendAsTestWaiting:
       
   294 	default:
       
   295 		User::Invariant();
       
   296 		break;
       
   297 		}	
       
   298 	}
       
   299 	
       
   300 void CSendAsTestServerMtm::DoComplete(TInt aError)
       
   301 	{
       
   302 	iProgress.iError = aError;
       
   303 	
       
   304 	if( iProgress.iError != KErrNone )
       
   305 		{
       
   306 		TRAPD(err, HandleSendErrorL(KMsvSendStateFailed));
       
   307 		}
       
   308 	else
       
   309 		{
       
   310 		// Make sure that the server MTM is not locking any entries.
       
   311 		iServerEntry->SetEntry(KMsvNullIndexEntryId);	// ignore error...	
       
   312 		}
       
   313 	
       
   314 	// Complete the request with the KErrNone - the progress has the error code.
       
   315 	User::RequestComplete(iRequestStatus, KErrNone);
       
   316 	}
       
   317 	
       
   318 /*
       
   319  * Methods from CActive
       
   320  */
       
   321 
       
   322 void CSendAsTestServerMtm::DoCancel()
       
   323 	{
       
   324 	switch( iProgress.iState )
       
   325 		{
       
   326 	case TSendAsTestMtmProgress::ESendAsTestSending:
       
   327 		{
       
   328 		iTimer.Cancel();
       
   329 		iProgress.iState = TSendAsTestMtmProgress::ESendAsTestWaiting;	
       
   330 		} break;
       
   331 	case TSendAsTestMtmProgress::ESendAsTestWaiting:
       
   332 	case TSendAsTestMtmProgress::ESendAsTestInitSend:
       
   333 	case TSendAsTestMtmProgress::ESendAsTestSendNext:
       
   334 	case TSendAsTestMtmProgress::ESendAsTestMoving:
       
   335 	default:
       
   336 		// Do nothing...
       
   337 		break;
       
   338 		}
       
   339 	// Suspend the messages that have not been sent.
       
   340 	iProgress.iError = KErrCancel;
       
   341 	TRAPD(err, HandleSendErrorL(KMsvSendStateSuspended));
       
   342 	
       
   343 	// Complete the request with the KErrNone - the progress has the error code.
       
   344 	User::RequestComplete(iRequestStatus, KErrNone);
       
   345 	}