email/imap4mtm/imapprotocolcontroller/src/cimapcompounddisconnect.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 "cimapmailstore.h" 
       
    17 #include "cimapcompounddisconnect.h"
       
    18 #include "cimapfolder.h"
       
    19 #include "cimapfolderinfo.h"
       
    20 #include "cimapsession.h"
       
    21 #include "cimapsessionmanager.h"
       
    22 #include "cimapcompoundsyncservice.h"
       
    23 #include "cimaplogger.h"
       
    24 #include "imappaniccodes.h"
       
    25 
       
    26 #include "mobilitytestmtmapi.h"
       
    27 
       
    28 CImapCompoundDisconnect::CImapCompoundDisconnect( CImapSyncManager& aSyncManager,
       
    29 						 				  		  CMsvServerEntry& aServerEntry,
       
    30 						 				  		  CImapSettings& aImapSettings,
       
    31 						 				  		  CImapSessionManager& aSessionManager,
       
    32 						 				  		  CImapMailStore& aImapMailStore,
       
    33 						 				  		  const RPointerArray<CImapSession>& aSessionList,
       
    34 						 				  		  CImapOfflineControl& aImapOfflineControl,
       
    35 						 				  		  TBool aDoDeletes  )
       
    36  : CImapCompoundBase(aSyncManager, aServerEntry, aImapSettings ),
       
    37    iSessionManager(aSessionManager), iSessionList(aSessionList), iImapMailStore(aImapMailStore),iImapOfflineControl(aImapOfflineControl), iDoDeletes(aDoDeletes)
       
    38 	{
       
    39 	}
       
    40 
       
    41 CImapCompoundDisconnect::~CImapCompoundDisconnect()
       
    42 	{
       
    43 	delete iCompoundSync;
       
    44 	}
       
    45 	
       
    46 CImapCompoundDisconnect* CImapCompoundDisconnect::NewL( CImapSyncManager& aSyncManager,
       
    47 						 				  				CMsvServerEntry& aServerEntry,
       
    48 						 				  				CImapSettings& aImapSettings, 
       
    49 						   								CImapSessionManager& aSessionManager,
       
    50 						   								CImapMailStore& aImapMailStore,
       
    51 						   								const RPointerArray<CImapSession>& aSessionList,
       
    52 						   								CImapOfflineControl& aImapOfflineControl,
       
    53 						   								TBool aDoDeletes  )
       
    54 	{
       
    55 	CImapCompoundDisconnect* self = new ( ELeave ) CImapCompoundDisconnect( aSyncManager,
       
    56 																			aServerEntry,
       
    57 																			aImapSettings,
       
    58 																			aSessionManager,
       
    59 																			aImapMailStore,
       
    60 																			aSessionList,
       
    61 																			aImapOfflineControl,
       
    62 																			aDoDeletes );
       
    63 	CleanupStack::PushL(self);
       
    64 	self->ConstructL();
       
    65 	CleanupStack::Pop(self);
       
    66 	return self;
       
    67 	}
       
    68 
       
    69 void CImapCompoundDisconnect::ConstructL()
       
    70 	{
       
    71 	CActiveScheduler::Add(this);
       
    72 	iCompoundSync = CImapCompoundSyncService::NewL(iSyncManager, 
       
    73 										    	   iServerEntry, 
       
    74 										    	   iImapSettings,
       
    75 										    	   iImapMailStore,
       
    76 										    	   iImapOfflineControl,
       
    77 										    	   ETrue);
       
    78 	}
       
    79 
       
    80 /**
       
    81 Starts tha compound object by setting the active object active
       
    82 @param aStatus
       
    83 @param aSessionManager Reference to the session manager that is used to disconnect
       
    84 @param aSessionList List of all connected sessions that need to be disconnected
       
    85 */
       
    86 void CImapCompoundDisconnect::StartOperation(TRequestStatus& aStatus, CImapSession& aSession)
       
    87 	{
       
    88 	iSession = &aSession;
       
    89 	__LOG_TEXT(iSession->LogId(), "CImapCompoundDisconnect::StartOperation()");
       
    90 	if(iImapSettings.DeleteEmailsWhenDisconnecting() && iDoDeletes)
       
    91 		iNextStep = EInboxLateDeletes;
       
    92 	else
       
    93 		iNextStep = EDisconnect;
       
    94 		
       
    95 	Queue(aStatus);
       
    96 	CompleteSelf();
       
    97 	}
       
    98 
       
    99 /**
       
   100 Handles the compound operation state machine
       
   101 
       
   102 @return ETrue if compound operation is completed, 
       
   103 		EFalse otherwise (will be called again, unless active)
       
   104 */
       
   105 TBool CImapCompoundDisconnect::DoRunLoopL()
       
   106 	{
       
   107 	SetCurrentStep();
       
   108 	switch (iCurrentStep)
       
   109 		{
       
   110 		case EInboxLateDeletes:  // asynchronous
       
   111 			{
       
   112 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapDisconnect1); // Sync'ing deletes
       
   113 			iCompoundSync->StartOperation(iStatus, *iSession);
       
   114 			iProgressState = TImap4GenericProgress::EDisconnecting;
       
   115 			iNextStep = EDisconnect;
       
   116 			SetActive();
       
   117 			break;
       
   118 			}
       
   119 		case EDisconnect:  // asynchronous
       
   120 			{
       
   121 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapDisconnect2); // disconnecting
       
   122 			iSessionManager.Disconnect( iStatus, iSessionList );
       
   123 			iProgressState = TImap4GenericProgress::EDisconnecting;
       
   124 			iNextStep = EFinished;
       
   125 			SetActive();
       
   126 			break;
       
   127 			}
       
   128 			
       
   129 		case EFinished:  // finished
       
   130 			{
       
   131 			__LOG_TEXT(iSession->LogId(), "CImapCompoundDisconnect::Completing OK");
       
   132 			iProgressState = TImap4GenericProgress::EDisconnected;
       
   133 			Complete(KErrNone);
       
   134 			return ETrue;
       
   135 			}
       
   136 			
       
   137 		default:
       
   138 			{
       
   139 			__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::EDisconnectCompoundUnexpectedState));
       
   140 			// unexpected state - complete the request
       
   141 			iProgressState = TImap4GenericProgress::EIdle;
       
   142 			return ETrue;
       
   143 			}
       
   144 		} // end switch (iCurrentStep)
       
   145 	return EFalse;
       
   146 	}
       
   147 
       
   148 /**
       
   149 May be called in case of a genuine cancel or a cancel for migrate.
       
   150 Following a genuine cancel, the compound operation will be deleted.
       
   151 Following a cancel for migrate, the compound operation will be resumed,
       
   152 so the iNextState is updated here to ensure the operation is
       
   153 correctly restarted.
       
   154 
       
   155 In either case, CMsgActive::DoCancel() is called to complete the
       
   156 user request with KErrCancel.
       
   157 
       
   158 Note that if the default CMsgActive::DoComplete() is overridden,
       
   159 then that must also be modified to handle either case described above.
       
   160 */
       
   161 void CImapCompoundDisconnect::DoCancel()
       
   162 	{
       
   163 	switch (iCurrentStep)
       
   164 		{
       
   165 		case EInboxLateDeletes:
       
   166 			{
       
   167 			iCompoundSync->Cancel();
       
   168 			break;
       
   169 			}
       
   170 		case EDisconnect:
       
   171 			{
       
   172 			iProgressState = TImap4GenericProgress::EDisconnecting;
       
   173 			iSessionManager.Cancel();
       
   174 			break;
       
   175 			}
       
   176 		case EFinished:
       
   177 			{
       
   178 			// self-completed or no outstanding request.
       
   179 			iProgressState = TImap4GenericProgress::EDisconnected;
       
   180 			break;
       
   181 			}
       
   182 		case ESuspendedForMigrate:
       
   183 		default :
       
   184 			{
       
   185 			__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::EDisconnectCompoundCancelUnexpectedState));
       
   186 			iProgressState = TImap4GenericProgress::EIdle;
       
   187 			break;
       
   188 			}
       
   189 		} // end switch (iCurrentStep)
       
   190 
       
   191 	// shouldn't be restarted after a migrate (in fact, should have 
       
   192 	// just been cancelled normally), but if it is, then just cut to
       
   193 	// the finish on resume.
       
   194 	iNextStep = EFinished;
       
   195 
       
   196 	if (!iCancelForMigrate)
       
   197 		{
       
   198 		// genuine cancel - update progress
       
   199 		iProgressErrorCode = KErrCancel;
       
   200 		}
       
   201 	CMsgActive::DoCancel();
       
   202 	}
       
   203 
       
   204 	
       
   205 void CImapCompoundDisconnect::Progress(TImap4CompoundProgress& aCompoundProgress)
       
   206 	{
       
   207 	aCompoundProgress.iGenericProgress.iOperation = TImap4GenericProgress::EDisconnect;
       
   208 	aCompoundProgress.iGenericProgress.iState = iProgressState;
       
   209 	
       
   210 	// Put error into progress buffer
       
   211 	if( aCompoundProgress.iGenericProgress.iErrorCode == KErrNone )
       
   212 		{
       
   213 		aCompoundProgress.iGenericProgress.iErrorCode = iProgressErrorCode;
       
   214 		}
       
   215 	}
       
   216 
       
   217 /**
       
   218 Handles NO/BAD responses according to current step.
       
   219 Negative server responses are considered fatal during disconnect.
       
   220 
       
   221 @return KErrGeneral
       
   222 */
       
   223 TInt CImapCompoundDisconnect::ProcessNegativeServerResponse()
       
   224 	{
       
   225 	iProgressErrorCode = iStatus.Int();
       
   226 	return KErrGeneral;
       
   227 	}
       
   228 
       
   229 /**
       
   230 Protocol Controller shouldn't attempt to resume a disconnect!
       
   231 So this completes with KErrDisconnected.
       
   232 */
       
   233 void CImapCompoundDisconnect::ResumeOperationL(TRequestStatus& aStatus, CImapSession& aSession)
       
   234 	{
       
   235 	iSession = &aSession;
       
   236 	__LOG_TEXT(iSession->LogId(), "CImapCompoundDisconnect::Resuming");
       
   237 	__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::EDisconnectCompoundCancelUnexpectedState));
       
   238 	iStopForMigrate = EFalse;
       
   239 
       
   240 	Queue(aStatus);
       
   241 	Complete(KErrDisconnected);
       
   242 	}