email/imap4mtm/imapprotocolcontroller/src/cimapcompoundcopyfromlocal.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2006-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 //
       
    15 
       
    16 #include "cimapcompoundcopyfromlocal.h"
       
    17 
       
    18 #include <imapset.h>
       
    19 #include <imcvsend.h>
       
    20 
       
    21 #include "cimapfolder.h"
       
    22 #include "cimapsession.h"
       
    23 #include "cimapsessionconsts.h"
       
    24 #include "cimaplogger.h"
       
    25 #include "imappaniccodes.h"
       
    26 
       
    27 #include "mobilitytestmtmapi.h"
       
    28 
       
    29 CImapCompoundCopyFromLocal::CImapCompoundCopyFromLocal( CImapSyncManager& aSyncManager,
       
    30 						 				  				CMsvServerEntry& aServerEntry,
       
    31 						 				  				CImapSettings& aImapSettings,
       
    32 						 				  				TBool aIsMove, 
       
    33 						 				  				const TMsvId aDestination) :
       
    34 	CImapCompoundBase(aSyncManager, aServerEntry, aImapSettings),
       
    35 	iIsMove(aIsMove), iDestinationFolderId(aDestination)
       
    36 	{
       
    37 	
       
    38 	}
       
    39 
       
    40 CImapCompoundCopyFromLocal::~CImapCompoundCopyFromLocal()
       
    41 	{
       
    42 	delete iSourceSel;
       
    43 	delete iSendMessage;
       
    44 	}
       
    45 	
       
    46 CImapCompoundCopyFromLocal* CImapCompoundCopyFromLocal::NewL( CImapSyncManager& aSyncManager,
       
    47 						 				  					  CMsvServerEntry& aServerEntry,
       
    48 						 				  					  CImapSettings& aImapSettings,
       
    49 						 				  					  TBool aIsMove, 
       
    50 													  		  const CMsvEntrySelection& aSourceSel, 
       
    51 													  		  const TMsvId aDestination)
       
    52 	{
       
    53 	CImapCompoundCopyFromLocal* self = new (ELeave) CImapCompoundCopyFromLocal(aSyncManager,
       
    54 																			   aServerEntry,
       
    55 																			   aImapSettings,
       
    56 																			   aIsMove,
       
    57 																			   aDestination);
       
    58 	CleanupStack::PushL(self);
       
    59 	self->ConstructL(aSourceSel);
       
    60 	CleanupStack::Pop(self);
       
    61 	return self;
       
    62 	
       
    63 	}
       
    64 
       
    65 void CImapCompoundCopyFromLocal::ConstructL(const CMsvEntrySelection& aSourceSel)
       
    66 	{
       
    67 	// Save parameters
       
    68 	iSourceSel = aSourceSel.CopyL();
       
    69 	iSendMessage = CImSendMessage::NewL(iServerEntry);
       
    70 	
       
    71 	iMessageSelection = iSelectionStillToCopy = iSelectionStillToDelete = aSourceSel.Count();
       
    72 	iTotalSize = CalculateDownloadSizeL(*iSourceSel);
       
    73 	
       
    74 	// Add to the active scheduler
       
    75 	CActiveScheduler::Add(this);
       
    76 	}
       
    77 
       
    78 void CImapCompoundCopyFromLocal::StartOperation(TRequestStatus& aStatus, CImapSession& aSession)
       
    79 	{
       
    80 	iSession = &aSession;
       
    81 	__LOG_TEXT(iSession->LogId(), "CImapCompoundCopyFromLocal::StartOperation()");
       
    82 	iNextStep = ECheckDestinationMailbox;
       
    83 	Queue(aStatus);
       
    84 	CompleteSelf();
       
    85 	}
       
    86 
       
    87 /**
       
    88 Handles the compound operation state machine
       
    89 
       
    90 Sequence of steps:
       
    91 	For all messages
       
    92 		ESetSource
       
    93 		EAppendMessage
       
    94 		EDeleteLocalMessage
       
    95 	ESelectDestinationMailboxRO
       
    96 	ENewSyncFolder
       
    97 	EFinished
       
    98 	
       
    99 @return ETrue if compound operation is completed, 
       
   100 		EFalse otherwise.
       
   101 */	
       
   102 TBool CImapCompoundCopyFromLocal::DoRunLoopL()
       
   103 	{
       
   104 	SetCurrentStep();
       
   105 	switch (iCurrentStep)
       
   106 		{
       
   107 		case ECheckDestinationMailbox: // synchonous
       
   108 			{
       
   109 			iNextStep = ESetSource;		// default next step to ESetSource
       
   110 			iDestinationFolder = iSyncManager.GetFolderL(iDestinationFolderId);
       
   111 			if (iDestinationFolder==NULL)
       
   112 				{
       
   113 				// note the error code for the progress report and complete
       
   114 				iProgressErrorCode = KErrNotFound;
       
   115 				iNextStep = EFinished;
       
   116 				}
       
   117 			// go to the next step
       
   118 			return EFalse;
       
   119 			}
       
   120 
       
   121 		case ESetSource:  // synchonous
       
   122 			{
       
   123 			if (iSelectionStillToCopy > 0)
       
   124 				{
       
   125 				// suspend operation if stop for migrate has been requested
       
   126 				if (iStopForMigrate)
       
   127 					{
       
   128 					__LOG_TEXT(iSession->LogId(), "CImapCompoundCopyFromLocal::Stopped for migrate");
       
   129 					iCurrentStep = ESuspendedForMigrate;
       
   130 					iNextStep = ESetSource;
       
   131 					Complete(KErrNone);
       
   132 					return ETrue;
       
   133 					}
       
   134 
       
   135 				// decrement iSelectionStillToCopy - it becomes the index in the list 
       
   136 				// of the current message to operate on.
       
   137 				--iSelectionStillToCopy;
       
   138 					
       
   139 				iCurrentMsgId = iSourceSel->At(iSelectionStillToCopy);
       
   140 				iNextStep = EAppendMessage;
       
   141 				}
       
   142 			else
       
   143 				{
       
   144 				// All messages copied...
       
   145 				iNextStep = ESelectDestinationMailboxRO;
       
   146 				}
       
   147 			// immediately proceed to the next step
       
   148 			return EFalse;
       
   149 			}
       
   150 			
       
   151 		case EAppendMessage:  // asynchonous
       
   152 			{
       
   153 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapCopyFromLocal1);
       
   154 			SetEntryL(iCurrentMsgId);
       
   155 			TTime messageDate = iServerEntry.Entry().iDate;
       
   156 
       
   157 			iSendMessage->InitialiseL(iCurrentMsgId, 
       
   158 									   ESendAsMimeEmail, 
       
   159 									   messageDate, 
       
   160 									   iImapSettings.ServerAddress(), 
       
   161 									   KCharacterSetIdentifierUtf8,
       
   162 									   ESendNoCopy);
       
   163 
       
   164 			iSession->AppendL(iStatus, *iSendMessage, iDestinationFolder->FullFolderPathL());
       
   165 			iNextStep = (iIsMove) ? EDeleteLocalMessage : ESetSource;
       
   166 			SetActive();
       
   167 			break;
       
   168 			}
       
   169 			
       
   170 		case EDeleteLocalMessage:  // synchonous
       
   171 			{
       
   172 			SetEntryL(iServerEntry.Entry().Parent());
       
   173 			User::LeaveIfError(iServerEntry.DeleteEntry(iCurrentMsgId));
       
   174 
       
   175 			--iSelectionStillToDelete;
       
   176 
       
   177 			iNextStep = ESetSource;			
       
   178 			// immediately proceed to the next step
       
   179 			return EFalse;
       
   180 			}
       
   181 			
       
   182 		case ESelectDestinationMailboxRO: // asynchonous
       
   183 			{
       
   184 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapCopyFromLocal2); // SELECT destination folder
       
   185 			iDestinationFolder->ExamineL(iStatus, *iSession);
       
   186 			iNextStep = ENewSyncFolder;
       
   187 			SetActive();
       
   188 			break;
       
   189 			}
       
   190 			
       
   191 		case ENewSyncFolder: // asynchonous
       
   192 			{
       
   193 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapCopyFromLocal3); // synchronising destination folder
       
   194 			// if the current folder hasn't actually changed then don't
       
   195 			// bother with the Sync
       
   196 			if (!iDestinationFolder->Changed(*iSession))
       
   197 				{
       
   198 				iNextStep = EFinished;
       
   199 				return EFalse;
       
   200 				}
       
   201 			
       
   202 			iSyncProgressState=TImap4SyncProgress::ESyncOther;
       
   203 			iDestinationFolder->SynchroniseL(iStatus, *iSession, ETrue, 0);
       
   204 			iNextStep = EFinished;
       
   205 			SetActive();
       
   206 			break;
       
   207 			}
       
   208 			
       
   209 		case EFinished: // finished
       
   210 			{
       
   211 			__LOG_TEXT(iSession->LogId(), "CImapCompoundCopyFromLocal::Completing OK");
       
   212 			iProgressState = TImap4GenericProgress::EIdle;
       
   213 			iSyncProgressState = TImap4SyncProgress::EIdle;
       
   214 			Complete(KErrNone);
       
   215 			return ETrue;
       
   216 			}
       
   217 			
       
   218 		default:
       
   219 			{
       
   220 			__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::ECopyFromLocalCompoundUnexpectedState));
       
   221 			// unexpected state - complete the request
       
   222 			iProgressState = TImap4GenericProgress::EIdle;
       
   223 			return ETrue;
       
   224 			}
       
   225 		}
       
   226 	return EFalse;
       
   227 	}
       
   228 
       
   229 /**
       
   230 May be called in case of a genuine cancel or a cancel for migrate.
       
   231 Following a genuine cancel, the compound operation will be deleted.
       
   232 Following a cancel for migrate, the compound operation will be resumed,
       
   233 so the iNextState is updated here to ensure the operation is
       
   234 correctly restarted.
       
   235 
       
   236 In either case, CMsgActive::DoCancel() is called to complete the
       
   237 user request with KErrCancel.
       
   238 
       
   239 Note that if the default CMsgActive::DoComplete() is overridden,
       
   240 then that must also be modified to handle either case described above.
       
   241 */
       
   242 void CImapCompoundCopyFromLocal::DoCancel()
       
   243 	{
       
   244 	switch (iCurrentStep)
       
   245 		{
       
   246 		case ESelectDestinationMailboxRO:
       
   247 			{
       
   248 			// outstanding request is on the imap session.
       
   249 			// Note that although select operations are performed on a folder they
       
   250 			// actually pass our iStatus to the session, so that is why we cancel
       
   251 			// the session for them.
       
   252 			iSession->Cancel();
       
   253 			iNextStep = ESelectDestinationMailboxRO;
       
   254 			break;
       
   255 			}
       
   256 		case EAppendMessage:
       
   257 			{
       
   258 			iSession->Cancel();
       
   259 			iNextStep = EAppendMessage;
       
   260 			break;
       
   261 			}
       
   262 		case ENewSyncFolder:
       
   263 			{
       
   264 			iDestinationFolder->Cancel();
       
   265 			// on resume, will need to reselect the folder first
       
   266 			iNextStep = ENewSyncFolder;
       
   267 			break;
       
   268 			}
       
   269 		case ECheckDestinationMailbox:
       
   270 		case ESetSource:
       
   271 		case EDeleteLocalMessage :
       
   272 		case EFinished:
       
   273 			{
       
   274 			// self-completed or no outstanding request.
       
   275 			iNextStep = ESelectDestinationMailboxRO;
       
   276 			break;
       
   277 			}
       
   278 		case ESuspendedForMigrate:
       
   279 		default:
       
   280 			{
       
   281 			__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::ECopyFromLocalCompoundCancelUnexpectedState));
       
   282 			iNextStep = ESelectDestinationMailboxRO;
       
   283 			break;
       
   284 			}
       
   285 		} // end of switch (iCurrentStep)
       
   286 
       
   287 	if (!iCancelForMigrate)
       
   288 		{
       
   289 		// genuine cancel - update progress
       
   290 		iProgressErrorCode = KErrCancel;
       
   291 		}
       
   292 	CMsgActive::DoCancel();
       
   293 	}
       
   294 	
       
   295 void CImapCompoundCopyFromLocal::Progress(TImap4CompoundProgress& aCompoundProgress)
       
   296 	{
       
   297 	if (iIsMove)
       
   298 		{
       
   299 		aCompoundProgress.iGenericProgress.iOperation = TImap4GenericProgress::EMoveFromLocal;
       
   300 		}
       
   301 	else
       
   302 		{
       
   303 		aCompoundProgress.iGenericProgress.iOperation = TImap4GenericProgress::ECopyFromLocal;
       
   304 		}
       
   305 
       
   306 	// Progress through the received selection
       
   307 	aCompoundProgress.iGenericProgress.iTotalSize = iTotalSize;
       
   308 	aCompoundProgress.iGenericProgress.iMsgsToDo  = iMessageSelection;
       
   309 	aCompoundProgress.iGenericProgress.iMsgsDone  = iMessageSelection - iSelectionStillToCopy;
       
   310 	
       
   311 	aCompoundProgress.iSyncProgress.iState=iSyncProgressState;
       
   312 	
       
   313 	//if currently syncing then get the latest progress
       
   314 	if(iSyncProgressState!=TImap4SyncProgress::EIdle)
       
   315 		{
       
   316 		iDestinationFolder->Progress(aCompoundProgress.iSyncProgress);
       
   317 		}
       
   318 	
       
   319 	// Put error into progress buffer
       
   320 	if( aCompoundProgress.iGenericProgress.iErrorCode == KErrNone )
       
   321 		{
       
   322 		aCompoundProgress.iGenericProgress.iErrorCode = iProgressErrorCode;
       
   323 		}
       
   324 	}
       
   325 
       
   326 /**
       
   327 Handles NO/BAD responses according to current step.
       
   328 Negative server responses are not fatal - the error is saved in
       
   329 the message currently being operated on and the state machine pushed
       
   330 on to process the next message in the requested selection.
       
   331 
       
   332 @return KErrNone if the error has been handled
       
   333 		Completion error code otherwise.
       
   334 */
       
   335 TInt CImapCompoundCopyFromLocal::ProcessNegativeServerResponse()
       
   336 	{
       
   337 	TInt err = iStatus.Int();
       
   338 	switch (iCurrentStep)
       
   339 		{
       
   340 		case ESelectDestinationMailboxRO:
       
   341 		case EAppendMessage:
       
   342 			{
       
   343 			if (err == KErrImapNo)
       
   344 				{
       
   345 				err = KErrNotFound;
       
   346 				}
       
   347 			} // fall through
       
   348 		case ESetSource:
       
   349 		case EDeleteLocalMessage:
       
   350 		case ENewSyncFolder:
       
   351 			{
       
   352 			// save the error with the associated message
       
   353 			TRAP_IGNORE(MessageErrorL(iCurrentMsgId, err));
       
   354 			// Skip to the next message or finish
       
   355 			iNextStep = (iSelectionStillToCopy>0)?ESetSource:EFinished;
       
   356 			break;
       
   357 			}
       
   358 		case ESuspendedForMigrate:
       
   359 		case EFinished:
       
   360 		default:
       
   361 			{
       
   362 			// positive error code not expected,
       
   363 			// self-completed states or no outstanding request.
       
   364 			TImapServerPanic::ImapPanic(TImapServerPanic::ECopyFromLocalCompoundUnexpectedState);
       
   365 			break;
       
   366 			}
       
   367 		} // end of switch (iCurrentStep)
       
   368 	iProgressErrorCode = err;
       
   369 	return KErrNone;
       
   370 	}
       
   371 	
       
   372 /**
       
   373 Called to resume the compound operation following a bearer migration.
       
   374 */
       
   375 void CImapCompoundCopyFromLocal::ResumeOperationL(TRequestStatus& aStatus, CImapSession& aSession)
       
   376 	{
       
   377 	iSession = &aSession; 
       
   378 	__LOG_TEXT(iSession->LogId(), "CImapCompoundCopyFromLocal::Resuming");
       
   379 	__ASSERT_DEBUG(iCurrentStep==ESuspendedForMigrate, TImapServerPanic::ImapPanic(TImapServerPanic::ECopyFromLocalCompoundUnexpectedState));
       
   380 	iStopForMigrate = EFalse;
       
   381 	
       
   382 	// Switch on next step - some "next steps" require a SELECT first...
       
   383 	switch (iNextStep)
       
   384 		{
       
   385 		case ESelectDestinationMailboxRO:
       
   386 		case EAppendMessage:
       
   387 		case ESetSource:	// if stopped for migrate.
       
   388 			{
       
   389 			// just return to the main state machine
       
   390 			CompleteSelf();
       
   391 			break;
       
   392 			}
       
   393 		case ENewSyncFolder:
       
   394 			{
       
   395 			// re-issue the EXAMINE before kicking off the folder sync
       
   396 			iDestinationFolder->ExamineL(iStatus, *iSession);
       
   397 			iCurrentStep=ESelectDestinationMailboxRO;
       
   398 			SetActive();
       
   399 			break;
       
   400 			}
       
   401 		case ECheckDestinationMailbox:
       
   402 		case EDeleteLocalMessage:
       
   403 		case EFinished:
       
   404 			// not expected
       
   405 		default:
       
   406 			{
       
   407 			__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::ECopyFromLocalCompoundCancelUnexpectedState));
       
   408 			// abandon the compound operation
       
   409 			iNextStep=EFinished;
       
   410 			CompleteSelf();
       
   411 			break;
       
   412 			}
       
   413 		} // end switch (iNextStep)
       
   414 
       
   415 	Queue(aStatus);
       
   416 	}