email/imap4mtm/imapprotocolcontroller/src/cimapcompoundsynctree.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 "cimapcompoundsynctree.h"
       
    17 
       
    18 #include "cimapfolder.h"
       
    19 #include "cimapsession.h"
       
    20 #include "cimaplogger.h"
       
    21 #include "imappaniccodes.h"
       
    22 
       
    23 #include "mobilitytestmtmapi.h"
       
    24 
       
    25 CImapCompoundSyncTree::CImapCompoundSyncTree(CImapSyncManager& aSyncManager, CMsvServerEntry& aServerEntry, CImapSettings& aImapSettings)
       
    26  : CImapCompoundBase(aSyncManager, aServerEntry, aImapSettings)
       
    27 	{
       
    28 	// Set default progress values here so that any progress request before
       
    29 	// the operation is started returns valid information.
       
    30 	iProgressState = TImap4GenericProgress::ESyncing;	
       
    31 	iSyncProgressState = TImap4SyncProgress::ESyncFolderTree;
       
    32 	}
       
    33 
       
    34 CImapCompoundSyncTree::~CImapCompoundSyncTree()
       
    35 	{
       
    36 	}
       
    37 	
       
    38 CImapCompoundSyncTree* CImapCompoundSyncTree::NewL(CImapSyncManager& aSyncManager, CMsvServerEntry& aServerEntry, CImapSettings& aImapSettings)
       
    39 	{
       
    40 	CImapCompoundSyncTree* self = new (ELeave) CImapCompoundSyncTree(aSyncManager, aServerEntry, aImapSettings);
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL();
       
    43 	CleanupStack::Pop(self);
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 void CImapCompoundSyncTree::ConstructL()
       
    48 	{
       
    49 	CActiveScheduler::Add(this);
       
    50 	}
       
    51 
       
    52 void CImapCompoundSyncTree::StartOperation(TRequestStatus& aStatus, CImapSession& aSession)
       
    53 	{
       
    54 	iSession = &aSession;
       
    55 	__LOG_TEXT(iSession->LogId(), "CImapCompoundSyncTree::StartOperation()");
       
    56 	iNextStep = ESyncTree;
       
    57 	Queue(aStatus);
       
    58 	CompleteSelf();
       
    59 	}
       
    60 	
       
    61 /**
       
    62 Handles the compound operation state machine
       
    63 
       
    64 @return ETrue if compound operation is completed, 
       
    65 		EFalse otherwise (will be called again, unless active)
       
    66 */
       
    67 TBool CImapCompoundSyncTree::DoRunLoopL()
       
    68 	{
       
    69 	SetCurrentStep();
       
    70 	switch (iCurrentStep)
       
    71 		{
       
    72 		case ESyncTree:
       
    73 			{
       
    74 			MOBILITY_TEST_MTM_STATE(iImapSettings.ServiceId(), KMobilityTestMtmStateImapSyncFolderTree);
       
    75 			
       
    76 			// SyncManager does the tree synchronisation.
       
    77 			iSyncManager.SynchroniseFolderTreeL(iStatus, *iSession);
       
    78 			iNextStep = EFinished;
       
    79 			SetActive();
       
    80 			break;
       
    81 			}
       
    82 			
       
    83 		case EFinished:
       
    84 			{
       
    85 			__LOG_TEXT(iSession->LogId(), "CImapCompoundSyncTree::Completing OK");
       
    86 			iProgressState = TImap4GenericProgress::EIdle;
       
    87 			iSyncProgressState = TImap4SyncProgress::EIdle;
       
    88 			Complete(KErrNone);
       
    89 			return ETrue;
       
    90 			}
       
    91 			
       
    92 		default:
       
    93 			{
       
    94 			__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::ESyncTreeCompoundUnexpectedState));
       
    95 			// unexpected state - complete the request
       
    96 			iProgressState = TImap4GenericProgress::EIdle;
       
    97 			iSyncProgressState = TImap4SyncProgress::EIdle;
       
    98 			return ETrue;
       
    99 			}
       
   100 		} // end of switch (iCurrentStep)
       
   101 
       
   102 	return EFalse;
       
   103 	}
       
   104 	
       
   105 
       
   106 /**
       
   107 May be called in case of a genuine cancel or a cancel for migrate.
       
   108 Following a genuine cancel, the compound operation will be deleted.
       
   109 Following a cancel for migrate, the compound operation will be resumed,
       
   110 so the iNextState is updated here to ensure the operation is
       
   111 correctly restarted.
       
   112 
       
   113 In either case, CMsgActive::DoCancel() is called to complete the
       
   114 user request with KErrCancel.
       
   115 
       
   116 Note that if the default CMsgActive::DoComplete() is overridden,
       
   117 then that must also be modified to handle either case described above.
       
   118 */
       
   119 void CImapCompoundSyncTree::DoCancel()
       
   120 	{
       
   121 	switch (iCurrentStep)
       
   122 		{
       
   123 		case ESyncTree:
       
   124 			{
       
   125 			// outstanding request on syncmanager
       
   126 			iSyncManager.Cancel();
       
   127 			iNextStep=ESyncTree;
       
   128 			break;
       
   129 			}
       
   130 		case EFinished:
       
   131 			{
       
   132 			// self-completed or no outstanding request.
       
   133 			break;
       
   134 			}
       
   135 		case ESuspendedForMigrate:
       
   136 		default:
       
   137 			{
       
   138 			__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::ESyncTreeCompoundCancelUnexpectedState));
       
   139 			break;		
       
   140 			}
       
   141 		} // end of switch (iCurrentStep)
       
   142 
       
   143 	// always re-start after migration		
       
   144 	iNextStep=ESelectSourceMailboxRW;
       
   145 
       
   146 	if (!iCancelForMigrate)
       
   147 		{
       
   148 		// genuine cancel - update progress
       
   149 		iProgressErrorCode = KErrCancel;
       
   150 		}
       
   151 	CMsgActive::DoCancel();	
       
   152 	}
       
   153 	
       
   154 void CImapCompoundSyncTree::Progress(TImap4CompoundProgress& aCompoundProgress)
       
   155 	{
       
   156 	aCompoundProgress.iGenericProgress.iOperation = TImap4GenericProgress::ESync;
       
   157 	aCompoundProgress.iGenericProgress.iState = iProgressState;
       
   158 
       
   159 	iSyncManager.Progress(aCompoundProgress.iSyncProgress);
       
   160 	aCompoundProgress.iSyncProgress.iState = iSyncProgressState;
       
   161 
       
   162 	// Put error into progress buffer
       
   163 	if( aCompoundProgress.iGenericProgress.iErrorCode == KErrNone )
       
   164 		{
       
   165 		aCompoundProgress.iGenericProgress.iErrorCode = iProgressErrorCode;
       
   166 		}
       
   167 	}
       
   168 
       
   169 /**
       
   170 Handles NO/BAD responses according to current step.
       
   171 Negative server responses are not fatal - the error is saved in
       
   172 the message currently being operated on and the state machine pushed
       
   173 on to process the next message in the requested selection.
       
   174 
       
   175 @return KErrNone if the error has been handled
       
   176 		Completion error code otherwise.
       
   177 */
       
   178 TInt CImapCompoundSyncTree::ProcessNegativeServerResponse()
       
   179 	{
       
   180 	TInt err = iStatus.Int();
       
   181 	switch (iCurrentStep)
       
   182 		{
       
   183 		case ESyncTree:
       
   184 			{
       
   185 			// Just store the error code in the progress response.
       
   186 			break;
       
   187 			}
       
   188 		case ESuspendedForMigrate:
       
   189 		case EFinished:
       
   190 		default:
       
   191 			{
       
   192 			// positive error code not expected,
       
   193 			// self-completed states or no outstanding request.
       
   194 			TImapServerPanic::ImapPanic(TImapServerPanic::ESyncTreeCompoundUnexpectedState);
       
   195 			break;
       
   196 			}
       
   197 		} // end of switch (iCurrentStep)
       
   198 	iProgressErrorCode = err;
       
   199 	return KErrNone;
       
   200 	}
       
   201 
       
   202 
       
   203 /**
       
   204 Resume the operation following a bearer migration.
       
   205 */
       
   206 void CImapCompoundSyncTree::ResumeOperationL(TRequestStatus& aStatus, CImapSession& aSession)
       
   207 	{
       
   208 	iSession = &aSession;
       
   209 	__LOG_TEXT(iSession->LogId(), "CImapCompoundSyncTree::Resuming");
       
   210 	__ASSERT_DEBUG(iCurrentStep==ESuspendedForMigrate, TImapServerPanic::ImapPanic(TImapServerPanic::ESyncTreeCompoundUnexpectedState));
       
   211 	iStopForMigrate = EFalse;
       
   212 	iNextStep = ESyncTree;
       
   213 	Queue(aStatus);
       
   214 	CompleteSelf();
       
   215 	}