email/imap4mtm/imapprotocolcontroller/src/cimapcompounddeletefolder.cpp
changeset 31 ebfee66fde93
parent 0 72b543305e3a
equal deleted inserted replaced
30:6a20128ce557 31:ebfee66fde93
       
     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 "cimapcompounddeletefolder.h"
       
    17 #include "cimapsyncmanager.h"
       
    18 #include "cimapfolder.h"
       
    19 #include "cimapsession.h"
       
    20 #include "cimapfolderinfo.h"
       
    21 #include "cimapsessionconsts.h"
       
    22 #include "cimaplogger.h"
       
    23 #include "imappaniccodes.h"
       
    24 
       
    25 #include "mobilitytestmtmapi.h"
       
    26 
       
    27 _LIT8(KAllUids, "1:*");
       
    28 _LIT8(KMessageDataItem, "+FLAGS");
       
    29 _LIT8(KDeleteValue,	 "\\Deleted");
       
    30 			
       
    31 CImapCompoundDeleteFolder::CImapCompoundDeleteFolder(CImapSyncManager& aSyncManager, CMsvServerEntry& aServerEntry, CImapSettings& aImapSettings)
       
    32  :	CImapCompoundBase(aSyncManager, aServerEntry, aImapSettings)
       
    33 	{
       
    34 	}
       
    35 
       
    36 CImapCompoundDeleteFolder::~CImapCompoundDeleteFolder()
       
    37 	{
       
    38 	delete iDeleteSel;
       
    39 	iOutMessageFlagInfo.Close();
       
    40 	}
       
    41 	
       
    42 CImapCompoundDeleteFolder* CImapCompoundDeleteFolder::NewL(CImapSyncManager& aSyncManager, CMsvServerEntry& aServerEntry, CImapSettings& aImapSettings, const CMsvEntrySelection& aSourceSel)
       
    43 	{
       
    44 	CImapCompoundDeleteFolder* self = new (ELeave) CImapCompoundDeleteFolder(aSyncManager, aServerEntry, aImapSettings);
       
    45 	CleanupStack::PushL(self);
       
    46 	self->ConstructL(aSourceSel);
       
    47 	CleanupStack::Pop(self);
       
    48 	return self;
       
    49 	}
       
    50 
       
    51 void CImapCompoundDeleteFolder::ConstructL(const CMsvEntrySelection& aSourceSel)
       
    52 	{
       
    53 	iDeleteSel=new (ELeave) CMsvEntrySelection;
       
    54 
       
    55 	// Check the source selection for acceptable types (folders)
       
    56 	// Messages         EFalse
       
    57 	// Handle Parts     EFalse
       
    58 	// Handle Folders   ETrue
       
    59 	// Check source     ETrue
       
    60 	// Makes a local copy of the source selection in iDeleteSel
       
    61 	CheckSelectionL(aSourceSel, iDeleteSel, EFalse, EFalse, ETrue, ETrue);
       
    62 
       
    63 	// Initialise the progress counters	
       
    64 	iFoldersToDelete=iDeleteSel->Count();
       
    65 	iFoldersDone = 0;
       
    66 	
       
    67 	// Add to the active scheduler
       
    68 	CActiveScheduler::Add(this);
       
    69 	}
       
    70 	
       
    71 
       
    72 void CImapCompoundDeleteFolder::StartOperation(TRequestStatus& aStatus, CImapSession& aSession)
       
    73 	{
       
    74 	iSession = &aSession;
       
    75 	__LOG_TEXT(iSession->LogId(), "CImapCompoundDeleteFolder::StartOperation()");
       
    76 	iNextStep = ESelectSourceMailboxRW;
       
    77 	Queue(aStatus);
       
    78 	CompleteSelf();
       
    79 	}
       
    80 
       
    81 
       
    82 
       
    83 /**
       
    84 Handles the compound operation state machine
       
    85 
       
    86 @return ETrue if compound operation is completed, 
       
    87 		EFalse otherwise (will be called again, unless active)
       
    88 */
       
    89 TBool CImapCompoundDeleteFolder::DoRunLoopL()
       
    90 	{
       
    91 	SetCurrentStep();
       
    92 	switch (iCurrentStep)
       
    93 		{
       
    94 		case ESelectSourceMailboxRW:  // asynchronous
       
    95 			{
       
    96 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapDeleteFolder1); // selecting folder
       
    97 			// anything to do?
       
    98 			if (iDeleteSel->Count() <= 0)
       
    99 				{
       
   100 				iNextStep = EFinished;
       
   101 				return EFalse;
       
   102 				}
       
   103 				
       
   104 			// suspend operation if stop for migrate has been requested
       
   105 			if (iStopForMigrate)
       
   106 				{
       
   107 				__LOG_TEXT(iSession->LogId(), "CImapCompoundDeleteFolder::Stopped for migrate");
       
   108 				iCurrentStep = ESuspendedForMigrate;
       
   109 				iNextStep = ESelectSourceMailboxRW;
       
   110 				Complete(KErrNone);
       
   111 				return ETrue;
       
   112 				}
       
   113 			
       
   114 			// Note that SelectSourceMailboxL() calls SetActive();	
       
   115 			SelectSourceMailboxL();
       
   116 			break;
       
   117 			}
       
   118 
       
   119 		case EDeleteAllMessages:  // asynchronous
       
   120 			{
       
   121 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapDeleteFolder2); // deleting all messages
       
   122 			// mark all messages in the selected folder /deleted
       
   123 			DeleteAllMessagesL(); // note this method calls SetActive();
       
   124 			break;
       
   125 			}
       
   126 			
       
   127 		case ECloseFolder:  // asynchronous
       
   128 			{
       
   129 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapDeleteFolder3); // closing the folder
       
   130 			// closing the folder causes messages 
       
   131 			// marked /Deleted to be expunged
       
   132 			iSession->CloseL(iStatus);
       
   133 			iNextStep=EDeleteFolder;
       
   134 			SetActive();
       
   135 			break;
       
   136 			}
       
   137 			
       
   138 		case EDeleteFolder:  // asynchronous
       
   139 			{
       
   140 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapDeleteFolder4); // issued delete command
       
   141 			iSession->DeleteL(iStatus, iFolder->FullFolderPathL());
       
   142 			iNextStep=EDeleteLocalFolder;
       
   143 			SetActive();
       
   144 			break;
       
   145 			}
       
   146 		
       
   147 		case EDeleteLocalFolder:  // synchronous
       
   148 			{
       
   149 			DeleteLocalFolderL();
       
   150 			// No async request issued, loop.
       
   151 			return EFalse;
       
   152 			}
       
   153 			
       
   154 		case EFinished:  // finished
       
   155 			{
       
   156 			__LOG_TEXT(iSession->LogId(), "CImapCompoundDeleteFolder::Completing OK");
       
   157 			iProgressState = TImap4GenericProgress::EIdle;
       
   158 			Complete(KErrNone);
       
   159 			return ETrue;
       
   160 			}
       
   161 			
       
   162 		default:
       
   163 			{
       
   164 			__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::EDeleteFolderCompoundUnexpectedState));
       
   165 			// unexpected state - complete the request
       
   166 			iProgressState = TImap4GenericProgress::EIdle;
       
   167 			return ETrue;
       
   168 			}
       
   169 		} // end switch (iCurrentStep)
       
   170 	return EFalse;
       
   171 	}
       
   172 
       
   173 void CImapCompoundDeleteFolder::SelectSourceMailboxL()
       
   174 	{
       
   175 	iFolder = NULL;
       
   176 	iRemoteFolderNotDeleted=EFalse;
       
   177 	
       
   178 	// Get a temporary folder object for the delete operation, as this
       
   179 	// can be performed on non-subscribed folders for which the sync manager
       
   180 	// does not own a folder object. Owned by this class.
       
   181 	iFolder = iSyncManager.GetFolderL((*iDeleteSel)[0]);
       
   182 	if (iFolder==NULL)
       
   183 		{
       
   184 		IncrementProgress();
       
   185 		// Try with the next folder in the array, unless this was the last.
       
   186 		if (iDeleteSel->Count()==0)
       
   187 			{
       
   188 			iNextStep=EFinished;
       
   189 			}
       
   190 		CompleteSelf();
       
   191 		}
       
   192 	else
       
   193 		{
       
   194 		SetEntryL(iFolder->MailboxId());
       
   195 		TMsvEmailEntry entry = iServerEntry.Entry();
       
   196 		if (entry.Mailbox())
       
   197 			{
       
   198 			iFolder->SelectL(iStatus, *iSession);
       
   199 			iProgressState = TImap4GenericProgress::ESelecting;
       
   200 			iNextStep=EDeleteAllMessages;
       
   201 			SetActive();
       
   202 			}
       
   203 		else
       
   204 			{
       
   205 			// do not attempt to select a folder marked \noselect
       
   206 			// go straight to the delete folder step.
       
   207 			iNextStep=EDeleteLocalFolder;
       
   208 			CompleteSelf();
       
   209 			}
       
   210 		}
       
   211 	}
       
   212 	
       
   213 	
       
   214 void CImapCompoundDeleteFolder::DeleteAllMessagesL()
       
   215 	{
       
   216 	// following steps are delete operations...
       
   217 	iProgressState = TImap4GenericProgress::EDeleting;
       
   218 
       
   219 	// Are there any messages in the remote folder to delete?
       
   220 	if (iSession->SelectedFolderInfo()->Exists()==0)
       
   221 		{
       
   222 		// No: Skip this step then
       
   223 		CompleteSelf();
       
   224 		}
       
   225 	else
       
   226 		{
       
   227 		// DeleteAllMessages is a special case: we want to delete everything,
       
   228 		// regardless of wether it's in the mirror or not. So, we set deleted
       
   229 		// flags on everything then close the folder to expunge the messages.
       
   230 
       
   231 		// Mark all messages in the folder for delete
       
   232 		iSession->StoreL(iStatus, KAllUids, KMessageDataItem, KDeleteValue, ETrue, iOutMessageFlagInfo);
       
   233 		SetActive();
       
   234 		}
       
   235 	iNextStep=ECloseFolder;
       
   236 	}
       
   237 
       
   238 
       
   239 void CImapCompoundDeleteFolder::DeleteLocalFolderL()
       
   240 	{
       
   241 	// sync manager deletes the local folder, any children entries
       
   242 	iSyncManager.DeleteFolderContentsL((*iDeleteSel)[0]);
       
   243 
       
   244 	if (!iRemoteFolderNotDeleted)
       
   245 		{
       
   246 		// update the sync manager's view of folders.
       
   247 		iSyncManager.RemoveFolderL((*iDeleteSel)[0]);
       
   248 		}
       
   249 	
       
   250 	// finished with this folder now, 
       
   251 	// increment the progress
       
   252 	IncrementProgress();
       
   253 	iNextStep=ESelectSourceMailboxRW;
       
   254 	}
       
   255 
       
   256 /**
       
   257 May be called in case of a genuine cancel or a cancel for migrate.
       
   258 Following a genuine cancel, the compound operation will be deleted.
       
   259 Following a cancel for migrate, the compound operation will be resumed,
       
   260 so the iNextState is updated here to ensure the operation is
       
   261 correctly restarted.
       
   262 
       
   263 In either case, CMsgActive::DoCancel() is called to complete the
       
   264 user request with KErrCancel.
       
   265 
       
   266 Note that if the default CMsgActive::DoComplete() is overridden,
       
   267 then that must also be modified to handle either case described above.
       
   268 */	
       
   269 void CImapCompoundDeleteFolder::DoCancel()
       
   270 	{
       
   271 	switch (iCurrentStep)
       
   272 		{
       
   273 		case ESelectSourceMailboxRW:
       
   274 		case EDeleteAllMessages:
       
   275 		case ECloseFolder:
       
   276 			{
       
   277 			// outstanding request on the session.
       
   278 			// Note that although select operations are performed on a folder they
       
   279 			// actually pass our iStatus to the session, so that is why we cancel
       
   280 			// the session for them.
       
   281 			// either the SELECT, STORE or the CLOSE session command is 
       
   282 			// to be cancelled... restart from the beginning to make sure 
       
   283 			// the folder has been emptied and the delete happens properly.
       
   284 			iSession->Cancel();
       
   285 			iNextStep = ESelectSourceMailboxRW;
       
   286 			break;
       
   287 			}
       
   288 		case EDeleteFolder:
       
   289 			{
       
   290 			// Folder has been emptied. After migrate, just delete it.
       
   291 			iSession->Cancel();
       
   292 			iNextStep=EDeleteFolder;
       
   293 			break;
       
   294 			}
       
   295 		case EDeleteLocalFolder:
       
   296 		case EFinished:
       
   297 			{
       
   298 			// self-completed or no outstanding request.
       
   299 			break;
       
   300 			}
       
   301 		case ESuspendedForMigrate:
       
   302 		default:
       
   303 			{
       
   304 			__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::EDeleteFolderCompoundCancelUnexpectedState));
       
   305 			// attempt to resume the operation from the start.
       
   306 			iNextStep = ESelectSourceMailboxRW;
       
   307 			break;
       
   308 			}
       
   309 		} // end switch (iCurrentStep)
       
   310 
       
   311 	if (!iCancelForMigrate)
       
   312 		{
       
   313 		// genuine cancel - update progress
       
   314 		iProgressErrorCode = KErrCancel;
       
   315 		}
       
   316 	CMsgActive::DoCancel();
       
   317 	}
       
   318 	
       
   319 void CImapCompoundDeleteFolder::Progress(TImap4CompoundProgress& aCompoundProgress)
       
   320 	{
       
   321 	aCompoundProgress.iGenericProgress.iOperation = TImap4GenericProgress::EDelete;
       
   322 	aCompoundProgress.iGenericProgress.iState = iProgressState;
       
   323 	aCompoundProgress.iGenericProgress.iMsgsToDo = iFoldersToDelete;
       
   324 	aCompoundProgress.iGenericProgress.iMsgsDone = iFoldersDone;
       
   325 
       
   326 	// Put error into progress buffer
       
   327 	if( aCompoundProgress.iGenericProgress.iErrorCode == KErrNone )
       
   328 		{
       
   329 		aCompoundProgress.iGenericProgress.iErrorCode = iProgressErrorCode;
       
   330 		}	
       
   331 	}
       
   332 
       
   333 /**
       
   334 Handles NO/BAD responses according to current step.
       
   335 Negative server responses are not fatal - the error is saved in
       
   336 the message currently being operated on and the state machine pushed
       
   337 on to process the next message in the requested selection.
       
   338 
       
   339 @return KErrNone if the error has been handled
       
   340 		Completion error code otherwise.
       
   341 */
       
   342 TInt CImapCompoundDeleteFolder::ProcessNegativeServerResponse()
       
   343 	{
       
   344 	TInt err = iStatus.Int();
       
   345 	switch(iCurrentStep)
       
   346 		{
       
   347 		case ESelectSourceMailboxRW:
       
   348 			{
       
   349 			if (err == KErrImapNo)
       
   350 				{
       
   351 				err = KErrNotFound;
       
   352 				}
       
   353 			} // fall through
       
   354 		case EDeleteFolder:
       
   355 			{
       
   356 			// The folder is not selectable for some reason, or
       
   357 			// failed to delete the folder on the remote server,
       
   358 			// setting this flag prevents the local folder being
       
   359 			// deleted, but the contents will still be deleted.
       
   360 			iProgressErrorCode = KErrImapCantDeleteFolder;
       
   361 			iRemoteFolderNotDeleted=ETrue;
       
   362 			iNextStep=EDeleteLocalFolder;
       
   363 			break;
       
   364 			}
       
   365 		case EDeleteAllMessages:
       
   366 		case ECloseFolder:
       
   367 			{
       
   368 			// Error indicates a problem with the issued STORE or CLOSE command.
       
   369 			// Attempt to continue regardless.
       
   370 			break;
       
   371 			}
       
   372 		case EDeleteLocalFolder:
       
   373 		case EFinished:
       
   374 		case ESuspendedForMigrate:
       
   375 		default:
       
   376 			{
       
   377 			// positive completion code not expected,
       
   378 			// self-completed states or no outstanding request.
       
   379 			TImapServerPanic::ImapPanic(TImapServerPanic::EDeleteFolderCompoundUnexpectedState);
       
   380 			break;
       
   381 			}
       
   382 		}
       
   383 	return KErrNone;
       
   384 	}
       
   385 
       
   386 /**
       
   387 Increments the progress count and removes the deleted folder
       
   388 from the source array
       
   389 */
       
   390 void CImapCompoundDeleteFolder::IncrementProgress()
       
   391 	{
       
   392 	++iFoldersDone;
       
   393 	iDeleteSel->Delete(0,1);
       
   394 	}
       
   395 
       
   396 /**
       
   397 Resumes the operation following a migration.
       
   398 */
       
   399 void CImapCompoundDeleteFolder::ResumeOperationL(TRequestStatus& aStatus, CImapSession& aSession)
       
   400 	{
       
   401 	iSession = &aSession;
       
   402 	__LOG_TEXT(iSession->LogId(), "CImapCompoundDeleteFolder::Resuming");
       
   403 	__ASSERT_DEBUG(iCurrentStep==ESuspendedForMigrate, TImapServerPanic::ImapPanic(TImapServerPanic::EDeleteCompoundUnexpectedState));
       
   404 	iStopForMigrate = EFalse;
       
   405 	
       
   406 	// Switch on next step - some "next steps" require a SELEECT first...
       
   407 	switch (iNextStep)
       
   408 		{
       
   409 		case ESelectSourceMailboxRW:
       
   410 		case EDeleteFolder:
       
   411 			{
       
   412 			// just return to the main state machine
       
   413 			CompleteSelf();
       
   414 			break;
       
   415 			}
       
   416 
       
   417 		case EDeleteAllMessages:
       
   418 		case ECloseFolder:
       
   419 		case EDeleteLocalFolder:
       
   420 		case EFinished:
       
   421 			// not expected
       
   422 		default:
       
   423 			{
       
   424 			__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::EDeleteCompoundUnexpectedState));
       
   425 			// abandon the compound operation
       
   426 			iNextStep=EFinished;
       
   427 			CompleteSelf();
       
   428 			break;
       
   429 			}
       
   430 		}
       
   431 	Queue(aStatus);
       
   432 	}
       
   433