email/imap4mtm/imapprotocolcontroller/src/cimapcompounddelete.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 "cimapcompounddelete.h"
       
    17 #include "cimapsession.h"
       
    18 #include "cimapfolder.h"
       
    19 #include "cimapsessionconsts.h"
       
    20 #include "cimaplogger.h"
       
    21 #include "imappaniccodes.h"
       
    22 
       
    23 #include "mobilitytestmtmapi.h"
       
    24 
       
    25 _LIT8(KImapAddFlags, "+FLAGS");
       
    26 _LIT8(KImapFlagDeleted, "(\\Deleted)");
       
    27 
       
    28 CImapCompoundDelete::CImapCompoundDelete(CImapSyncManager& aSyncManager,
       
    29 						 				 CMsvServerEntry& aServerEntry,
       
    30 						 				 CImapSettings& aImapSettings) :
       
    31 	CImapCompoundBase(aSyncManager, aServerEntry, aImapSettings)
       
    32 	{
       
    33 	
       
    34 	}
       
    35 
       
    36 CImapCompoundDelete::~CImapCompoundDelete()
       
    37 	{
       
    38 	delete iSourceSel;
       
    39 	iMessageUids.Reset();
       
    40 	iFlagInfoResponseArray.Reset();
       
    41 	}
       
    42 	
       
    43 CImapCompoundDelete* CImapCompoundDelete::NewL(CImapSyncManager& aSyncManager,
       
    44 						 				  	   CMsvServerEntry& aServerEntry,
       
    45 						 				  	   CImapSettings& aImapSettings, 
       
    46 						 				  	   const CMsvEntrySelection& aSourceSel)
       
    47 	{
       
    48 	CImapCompoundDelete* self = new (ELeave) CImapCompoundDelete(aSyncManager,
       
    49 																 aServerEntry,
       
    50 																 aImapSettings );
       
    51 	CleanupStack::PushL(self);
       
    52 	self->ConstructL(aSourceSel);
       
    53 	CleanupStack::Pop(self);
       
    54 	return self;
       
    55 	}
       
    56 	
       
    57 void CImapCompoundDelete::ConstructL(const CMsvEntrySelection& aSourceSel)
       
    58 	{
       
    59 	iSourceSel=new (ELeave) CMsvEntrySelection;
       
    60 	
       
    61 
       
    62 	// Check the source selection contains only messages
       
    63 	// Messages         True
       
    64 	// Handle Parts     False
       
    65 	// Handle Folders   False
       
    66 	// Check source     True
       
    67 	// Makes a local copy of the source selection in iSourceSel
       
    68 	CheckSelectionL(aSourceSel, iSourceSel, ETrue, EFalse, EFalse, ETrue);
       
    69 
       
    70 	// Initialise the progress counters	
       
    71 	iProgressMsgsToDo=iSourceSel->Count();
       
    72 	iProgressMsgsDone=0;
       
    73 
       
    74 	// Add to the active scheduler
       
    75 	CActiveScheduler::Add(this);
       
    76 	}
       
    77 
       
    78 void CImapCompoundDelete::StartOperation(TRequestStatus& aStatus, CImapSession& aSession)
       
    79 	{
       
    80 	iSession = &aSession;
       
    81 	__LOG_TEXT(iSession->LogId(), "CImapCompoundDelete::StartOperation()");
       
    82 	iNextStep = ESelectSourceMailboxRW;
       
    83 	Queue(aStatus);
       
    84 	CompleteSelf();
       
    85 	}
       
    86 
       
    87 /**
       
    88 Handles the compound operation state machine
       
    89 
       
    90 @return ETrue if compound operation is completed, 
       
    91 		EFalse otherwise (will be called again, unless active)
       
    92 */
       
    93 TBool CImapCompoundDelete::DoRunLoopL()
       
    94 	{
       
    95 	SetCurrentStep();
       
    96 	switch (iCurrentStep)
       
    97 		{
       
    98 		case ESelectSourceMailboxRW: // asynchronous
       
    99 			{
       
   100 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapDelete1); // select source folder
       
   101 			// Anything to do?
       
   102 			if (iSourceSel->Count()==0)
       
   103 				{
       
   104 				iNextStep = EFinished;
       
   105 				return EFalse;
       
   106 				}
       
   107 			
       
   108 			// suspend operation if stop for migrate has been requested
       
   109 			if (iStopForMigrate)
       
   110 				{
       
   111 				__LOG_TEXT(iSession->LogId(), "CImapCompoundDelete::Stopped for migrate");
       
   112 				iCurrentStep = ESuspendedForMigrate;
       
   113 				iNextStep = ESelectSourceMailboxRW;
       
   114 				Complete(KErrNone);
       
   115 				return ETrue;
       
   116 				}
       
   117 			
       
   118 			// Find the parent folder for the first entry in the array
       
   119 			iParentFolderId = FindFolderL((*iSourceSel)[0]);
       
   120 			iParentFolder = iSyncManager.GetFolderL(iParentFolderId);
       
   121 			if (iParentFolder==NULL)
       
   122 				{
       
   123 				// Try again with the next message
       
   124 				// Delete the first entry in the source selection
       
   125 				iSourceSel->Delete(0, 1);
       
   126 				// increment progress counter
       
   127 				++iProgressMsgsDone;
       
   128 				return EFalse;
       
   129 				}
       
   130 			
       
   131 			// reset the message UID list
       
   132 			iMessageUids.Reset();
       
   133 
       
   134 			__LOG_FORMAT((iSession->LogId(), "Deleting message %x in folder %x", (*iSourceSel)[0], iParentFolderId));
       
   135 
       
   136 			iParentFolder->SelectL(iStatus, *iSession);
       
   137 			iProgressState = TImap4GenericProgress::ESelecting;
       
   138 			iNextStep = EDeleteMessage;
       
   139 			SetActive();
       
   140 			break;
       
   141 			}
       
   142 
       
   143 		case EDeleteMessage: // asynchronous
       
   144 			{
       
   145 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapDelete2); // issue the STORE /deleted command
       
   146 			// build an array of the UIDs to delete,
       
   147 			// mark local IMAP \deleted flag.
       
   148 			TInt  i = 0;
       
   149 			TInt  count = iSourceSel->Count();
       
   150 			TBool sameParent = ETrue;
       
   151 			while (i<count && sameParent)
       
   152 				{
       
   153 				SetEntryL((*iSourceSel)[i]);
       
   154 				TMsvEmailEntry entry = iServerEntry.Entry();
       
   155 				if (entry.Parent()==iParentFolderId)
       
   156 					{
       
   157 					TUint id = entry.UID();
       
   158 					iMessageUids.AppendL(id);
       
   159 
       
   160 					// Set deleted flag on local entry
       
   161 					entry.SetDeletedIMAP4Flag(ETrue);
       
   162 					User::LeaveIfError(iServerEntry.ChangeEntry(entry));
       
   163 					++iSelectionToDelete;
       
   164 					}
       
   165 				else
       
   166 					{
       
   167 					sameParent=EFalse;
       
   168 					}
       
   169 				++i;
       
   170 				}
       
   171 
       
   172 			HBufC8* uidSeq = CImapSession::CreateSequenceSetLC(iMessageUids);
       
   173 
       
   174 			// issue the store command
       
   175 			iSession->StoreL(iStatus, uidSeq->Des(), KImapAddFlags, KImapFlagDeleted, ETrue, iFlagInfoResponseArray);
       
   176 			iProgressState = TImap4GenericProgress::EDeleting;
       
   177 			CleanupStack::PopAndDestroy(uidSeq);
       
   178 			if (iImapSettings.UseExpunge())
       
   179 				{
       
   180 				iNextStep=EExpunge;
       
   181 				}
       
   182 			else
       
   183 				{
       
   184 				iNextStep=ECloseFolder;
       
   185 				}
       
   186 			SetActive();
       
   187 			break;
       
   188 			}
       
   189 
       
   190 		case EExpunge: // asynchronous
       
   191 			{
       
   192 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapDelete3); // issue the EXPUNGE command
       
   193 			// Messages have been marked for delete on the remote server
       
   194 			// issue the expunge command to remove them completely
       
   195 			iSession->ExpungeL(iStatus);
       
   196 			// no change in progress state
       
   197 			iNextStep = EDeleteLocalMessage;
       
   198 			SetActive();
       
   199 			break;
       
   200 			}
       
   201 
       
   202 		case ECloseFolder:
       
   203 			{
       
   204 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapDelete4); // CLOSE FOLDER
       
   205 			//Permanently removes all messages that have the deleted flag set
       
   206 			//from the currently selected mailbox
       
   207 			iSession->CloseL(iStatus);
       
   208 			iNextStep=ESelectFolderAfterClose;
       
   209 			SetActive();
       
   210 			break;
       
   211 			}
       
   212 
       
   213 		case ESelectFolderAfterClose:
       
   214 			{
       
   215 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapDelete5); // SELECT FOLDER
       
   216 			//Selecting a mailbox so that messages in the mailbox can be accessed. 
       
   217 			iParentFolder->SelectL(iStatus, *iSession);
       
   218 			iNextStep=EDeleteLocalMessage;
       
   219 			SetActive();
       
   220 			break;
       
   221 			}
       
   222 
       
   223 		case EDeleteLocalMessage: // synchronous
       
   224 			{
       
   225 			DeleteLocalMessagesL();
       
   226 			// test for remaining messages to delete performed in state ESelectSourceMailboxRW;
       
   227 			iNextStep=ESelectSourceMailboxRW;
       
   228 			// no async request made.
       
   229 			return EFalse;
       
   230 			}
       
   231 				
       
   232 		case EFinished:	// finished
       
   233 			{
       
   234 			__LOG_TEXT(iSession->LogId(), "CImapCompoundDelete::Completing OK");
       
   235 			iProgressState = TImap4GenericProgress::EIdle;
       
   236 			Complete(KErrNone);
       
   237 			return ETrue;
       
   238 			}
       
   239 			
       
   240 		default:
       
   241 			{
       
   242 			__ASSERT_DEBUG(EFalse, 
       
   243 				TImapServerPanic::ImapPanic(TImapServerPanic::EDeleteCompoundUnexpectedState));
       
   244 			// unexpected state - complete the request
       
   245 			iProgressState = TImap4GenericProgress::EIdle;
       
   246 			return ETrue;
       
   247 			}
       
   248 		}
       
   249 	return EFalse;
       
   250 	}
       
   251 
       
   252 /**
       
   253 May be called in case of a genuine cancel or a cancel for migrate.
       
   254 Following a genuine cancel, the compound operation will be deleted.
       
   255 Following a cancel for migrate, the compound operation will be resumed,
       
   256 so the iNextState is updated here to ensure the operation is
       
   257 correctly restarted.
       
   258 
       
   259 In either case, CMsgActive::DoCancel() is called to complete the
       
   260 user request with KErrCancel.
       
   261 
       
   262 Note that if the default CMsgActive::DoComplete() is overridden,
       
   263 then that must also be modified to handle either case described above.
       
   264 */	
       
   265 void CImapCompoundDelete::DoCancel()
       
   266 	{
       
   267 	switch (iCurrentStep)
       
   268 		{
       
   269 		case ESelectSourceMailboxRW:
       
   270 			{
       
   271 			// There is an outstanding select command issued. Cancel it.
       
   272 			// Need to re-start with next step = ESelectSourceMailboxRW
       
   273 			iSession->Cancel();
       
   274 			iNextStep = ESelectSourceMailboxRW;
       
   275 			break;
       
   276 			}
       
   277 		case EDeleteMessage: 
       
   278 			{
       
   279 			// in DoRunL an array of messages in this folder has been created
       
   280 			// so, it is possible that that requested delete has not actually
       
   281 			// been received by the remote server. When the operation re-starts
       
   282 			// it will need to re-select the parent folder and then re-issue the
       
   283 			// STORE \deleted command.
       
   284 			
       
   285 			// the entries have already been marked locally.
       
   286 			// we still have the iUID array - iMessageUids.
       
   287 		
       
   288 			// Cancel the session command
       
   289 			iSession->Cancel();
       
   290 			iSelectionToDelete = 0; // reset the count of messages to delete locally
       
   291 			iNextStep = EDeleteMessage; // restart function
       
   292 			break;
       
   293 			}
       
   294 		case EExpunge:
       
   295 			{
       
   296 			// Messages have been marked for delete on the remote server
       
   297 			// and the expunge command issued - cancel it
       
   298 
       
   299 			// Cancel the session command
       
   300 			iSession->Cancel();
       
   301 
       
   302 			// need to re-issue the expunge command after migrate 
       
   303 			// (and re-select of correct folder)
       
   304 			iNextStep = EExpunge;
       
   305 			break;
       
   306 			}
       
   307 		case ECloseFolder:
       
   308 			{
       
   309 			iSession->Cancel();
       
   310 			iNextStep = ECloseFolder;
       
   311 			break;
       
   312 			}
       
   313 		case ESelectFolderAfterClose:
       
   314 			{
       
   315 			iSession->Cancel();
       
   316 			iNextStep = ESelectFolderAfterClose;
       
   317 			break;
       
   318 			}
       
   319 		case EDeleteLocalMessage:
       
   320 		case EFinished:
       
   321 			{
       
   322 			// self-completed or no outstanding request.
       
   323 			break;
       
   324 			}
       
   325 		case ESuspendedForMigrate:
       
   326 		default:
       
   327 			{
       
   328 			__ASSERT_DEBUG(EFalse, 
       
   329 				TImapServerPanic::ImapPanic(TImapServerPanic::EDeleteCompoundCancelUnexpectedState));
       
   330 			break;
       
   331 			}
       
   332 		} // end switch (iCurrentStep)	
       
   333 	
       
   334 	if (!iCancelForMigrate)
       
   335 		{
       
   336 		// genuine cancel - update progress
       
   337 		iProgressErrorCode = KErrCancel;
       
   338 		}
       
   339 	CMsgActive::DoCancel();
       
   340 	}
       
   341 
       
   342 	
       
   343 void CImapCompoundDelete::Progress(TImap4CompoundProgress& aCompoundProgress)
       
   344 	{
       
   345 	aCompoundProgress.iGenericProgress.iOperation = TImap4GenericProgress::EDelete;
       
   346 	aCompoundProgress.iGenericProgress.iState = iProgressState;
       
   347 	aCompoundProgress.iGenericProgress.iMsgsToDo = iProgressMsgsToDo;
       
   348 	aCompoundProgress.iGenericProgress.iMsgsDone = iProgressMsgsDone;
       
   349 	
       
   350 	// Put error into progress buffer
       
   351 	if( aCompoundProgress.iGenericProgress.iErrorCode == KErrNone )
       
   352 		{
       
   353 		aCompoundProgress.iGenericProgress.iErrorCode = iProgressErrorCode;
       
   354 		}
       
   355 		
       
   356 	}
       
   357 
       
   358 
       
   359 /**
       
   360 Deletes messages locally
       
   361 Deletes messages in iSourceSel contains only messages
       
   362 belonging to a single parent folder.
       
   363 */
       
   364 void CImapCompoundDelete::DeleteLocalMessagesL()
       
   365 	{
       
   366 	// Set the server entry context to the parent.
       
   367 	SetEntryL((*iSourceSel)[0]);
       
   368 	SetEntryL(iServerEntry.Entry().Parent());
       
   369 	
       
   370 	// delete local entries that have been expunged remotely
       
   371 	TInt count = 0;
       
   372 	while(count<iSelectionToDelete)
       
   373 		{
       
   374 		// Do not leave when entry is in use 
       
   375 		TInt err = iServerEntry.DeleteEntry((*iSourceSel)[count]);
       
   376 		if(err!=KErrNone)
       
   377 			{
       
   378 			if(err!=KErrInUse)
       
   379 				{
       
   380 				User::LeaveIfError(err);
       
   381 				}
       
   382 			}
       
   383 		++count;
       
   384 		} // end while()
       
   385 		
       
   386 	// remove deleted items from the selection 
       
   387 	iSourceSel->Delete(0,count);
       
   388 	iSelectionToDelete=0;
       
   389 
       
   390 	// increment progress counter
       
   391 	iProgressMsgsDone+=count;
       
   392 	}
       
   393 
       
   394 
       
   395 /**
       
   396 Handles NO/BAD responses according to current step.
       
   397 Negative server responses are not fatal - the error is saved in
       
   398 the message currently being operated on and the state machine pushed
       
   399 on to process the next message in the requested selection.
       
   400 
       
   401 @return KErrNone if the error has been handled
       
   402 		Completion error code otherwise.
       
   403 */
       
   404 TInt CImapCompoundDelete::ProcessNegativeServerResponse()
       
   405 	{
       
   406 	TInt err = iStatus.Int();
       
   407 	switch (iCurrentStep)
       
   408 		{
       
   409 		case ESelectSourceMailboxRW:
       
   410 		case ESelectFolderAfterClose:
       
   411 			{
       
   412 			// only knowingly operating on a single message at this point.
       
   413 			if (err == KErrImapNo)
       
   414 				{
       
   415 				err = KErrNotFound;
       
   416 				}
       
   417 			// save the error with the associated message
       
   418 			TRAP_IGNORE(MessageErrorL((*iSourceSel)[0], err));
       
   419 			++iProgressMsgsDone;
       
   420 			// Skip to the next message, or finish
       
   421 			iNextStep = (iProgressMsgsDone<iProgressMsgsToDo)?ESelectSourceMailboxRW:EFinished;
       
   422 			break;
       
   423 			}
       
   424 		case EDeleteMessage:
       
   425 		case EExpunge:
       
   426 		case ECloseFolder:
       
   427 			{
       
   428 			// failed to mark one or more messages for delete.
       
   429 			// ignore the error - no point in storing it in a 
       
   430 			// message that is just about to be deleted.
       
   431 			break;
       
   432 			}
       
   433 		case EDeleteLocalMessage:
       
   434 		case EFinished:
       
   435 		case ESuspendedForMigrate:
       
   436 		default:
       
   437 			{
       
   438 			// positive error code not expected,
       
   439 			// self-completed states or no outstanding request.
       
   440 			TImapServerPanic::ImapPanic(TImapServerPanic::EDeleteCompoundUnexpectedState);
       
   441 			break;
       
   442 			}
       
   443 		} // end switch (iCurrentStep)
       
   444 	iProgressErrorCode = err;
       
   445 	return KErrNone;
       
   446 	}
       
   447 
       
   448 /**
       
   449 Resumes the operation following a migration.
       
   450 */
       
   451 void CImapCompoundDelete::ResumeOperationL(TRequestStatus& aStatus, CImapSession& aSession)
       
   452 	{
       
   453 	iSession = &aSession;
       
   454 	__LOG_TEXT(iSession->LogId(), "CImapCompoundDelete::Resuming");
       
   455 	__ASSERT_DEBUG(iCurrentStep==ESuspendedForMigrate, TImapServerPanic::ImapPanic(TImapServerPanic::EDeleteCompoundUnexpectedState));
       
   456 	iStopForMigrate = EFalse;
       
   457 	
       
   458 	// Switch on next step - some "next steps" require a SELEECT first...
       
   459 	switch (iNextStep)
       
   460 		{
       
   461 		case ESelectSourceMailboxRW:
       
   462 		case ESelectFolderAfterClose:
       
   463 			{
       
   464 			// just return to the main state machine
       
   465 			CompleteSelf();
       
   466 			break;
       
   467 			}
       
   468 		case EDeleteMessage: 
       
   469 		case EExpunge:
       
   470 		case ECloseFolder:
       
   471 			{
       
   472 			// re-issue the SELECT before kicking off the DELETE or EXPUNGE command:
       
   473 			iParentFolder->SelectL(iStatus, *iSession);
       
   474 			iCurrentStep = ESelectSourceMailboxRW;
       
   475 			SetActive();
       
   476 			break;
       
   477 			}
       
   478 		case EDeleteLocalMessage:
       
   479 		case EFinished:
       
   480 			// not expected
       
   481 		default:
       
   482 			{
       
   483 			__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::EDeleteCompoundUnexpectedState));
       
   484 			// abandon the compound operation
       
   485 			iNextStep=EFinished;
       
   486 			CompleteSelf();
       
   487 			break;
       
   488 			}
       
   489 		} // end switch (iNextStep)
       
   490 	Queue(aStatus);
       
   491 	}