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