email/imap4mtm/imapprotocolcontroller/src/cimapupdateflagoperation.cpp
changeset 80 8b14b30db193
equal deleted inserted replaced
79:2981cb3aa489 80:8b14b30db193
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cimapupdateflagoperation.h"
       
    20 #include "cimapfolder.h"
       
    21 #include "cimapsyncmanager.h"
       
    22 #include "cimapsessionconsts.h"
       
    23 #include "cimaplogger.h"
       
    24 #include "imappaniccodes.h"
       
    25 
       
    26 _LIT8(KImapAddFlags, "+FLAGS");
       
    27 _LIT8(KStoreFlagsClearCommand, "-FLAGS");
       
    28 _LIT8(KImapFlagSeen, "(\\Seen)");
       
    29 
       
    30 
       
    31 CImapUpdateFlagOperation::CImapUpdateFlagOperation(CImapSyncManager& aSyncManager,
       
    32 						 				 CMsvServerEntry& aServerEntry,
       
    33 						 				 CImapSettings& aImapSettings) :
       
    34 	CImapCompoundBase(aSyncManager, aServerEntry, aImapSettings)
       
    35 	
       
    36 	{
       
    37 	
       
    38 	}
       
    39 
       
    40 CImapUpdateFlagOperation::~CImapUpdateFlagOperation()
       
    41 	{
       
    42 	
       
    43 
       
    44 	iMessageUids.Reset();
       
    45 	iFlagInfoResponseArray.Reset();
       
    46 	}
       
    47 	
       
    48 CImapUpdateFlagOperation* CImapUpdateFlagOperation::NewL(CImapSyncManager& aSyncManager,
       
    49 						 				  	   CMsvServerEntry& aServerEntry,
       
    50 						 				  	   CImapSettings& aImapSettings)
       
    51 	{
       
    52 	CImapUpdateFlagOperation* self = new (ELeave) CImapUpdateFlagOperation(aSyncManager,
       
    53 																 aServerEntry,
       
    54 																 aImapSettings
       
    55 																 );
       
    56 	CleanupStack::PushL(self);
       
    57 	self->ConstructL();
       
    58 	CleanupStack::Pop(self);
       
    59 	return self;
       
    60 	}
       
    61 
       
    62 void CImapUpdateFlagOperation::ConstructL()
       
    63 	{
       
    64 	
       
    65 	// Add to the active scheduler
       
    66 	CActiveScheduler::Add(this);
       
    67 	}
       
    68 
       
    69 // NewL for Selection
       
    70 CImapUpdateFlagOperation* CImapUpdateFlagOperation::NewL(CImapSyncManager& aSyncManager,
       
    71                                                          CMsvServerEntry& aServerEntry,
       
    72                                                          CImapSettings& aImapSettings, 
       
    73                                                          const CMsvEntrySelection& aSourceSel , TBool aFlagChanged)
       
    74 
       
    75     {
       
    76     
       
    77     CImapUpdateFlagOperation* self = new (ELeave) CImapUpdateFlagOperation(aSyncManager,
       
    78                                                                      aServerEntry,
       
    79                                                                      aImapSettings);
       
    80     CleanupStack::PushL(self);
       
    81     self->ConstructL(aSourceSel,aFlagChanged);
       
    82     CleanupStack::Pop(self);
       
    83     return self;
       
    84     }
       
    85 //// ConstructL for Selection   
       
    86 void CImapUpdateFlagOperation::ConstructL(const CMsvEntrySelection& aSourceSel, TBool aFlagChanged)
       
    87     {
       
    88   
       
    89     iSourceSel=new (ELeave) CMsvEntrySelection;
       
    90     CheckSelectionL(aSourceSel, iSourceSel, ETrue, EFalse, EFalse, ETrue);
       
    91 
       
    92     // Initialise the progress counters 
       
    93     iProgressMsgsToDo=iSourceSel->Count();
       
    94     iProgressMsgsDone=0;
       
    95     iMarkFlag = aFlagChanged;
       
    96     iEntrySelection = ETrue;
       
    97     // Add to the active scheduler
       
    98     CActiveScheduler::Add(this);
       
    99     }
       
   100 
       
   101 
       
   102 
       
   103 void CImapUpdateFlagOperation::StartOperation(TRequestStatus& aStatus, CImapSession& aSession)
       
   104 //void CImapUpdateFlagOperation::StartOperation(TRequestStatus& aStatus)
       
   105 	{
       
   106 	iSession = &aSession;
       
   107 	__LOG_TEXT(iSession->LogId(), "CImapUpdateFlagOperation::StartOperation()");
       
   108 	iNextStep = EUpdateFlag;
       
   109 	Queue(aStatus);
       
   110 	CompleteSelf();
       
   111 	}
       
   112 
       
   113 /**
       
   114 Handles the compound operation state machine
       
   115 
       
   116 @return ETrue if compound operation is completed, 
       
   117 		EFalse otherwise.
       
   118 */	
       
   119 TBool CImapUpdateFlagOperation::DoRunLoopL()
       
   120 	{
       
   121 	SetCurrentStep();
       
   122 	switch (iCurrentStep)
       
   123 		{
       
   124 	
       
   125 	case EUpdateFlag:
       
   126 		{
       
   127 		HBufC8* uidSeq = NULL;
       
   128         if(!iEntrySelection) //Entry iMarkFlag is Flase
       
   129             {
       
   130             TMsvEmailEntry entry = iServerEntry.Entry();
       
   131             TUint id = entry.UID();
       
   132             iMessageUids.AppendL(id);
       
   133             if(entry.Unread())
       
   134                 {
       
   135                 entry.SetSeenIMAP4Flag(EFalse);
       
   136                 }
       
   137             else
       
   138                 {
       
   139                 entry.SetSeenIMAP4Flag(ETrue);	
       
   140                 }
       
   141                     
       
   142                     
       
   143             User::LeaveIfError(iServerEntry.ChangeEntry(entry));
       
   144     
       
   145             uidSeq = CImapSession::CreateSequenceSetLC(iMessageUids);
       
   146     
       
   147             // finished with the UID array. Clear it.
       
   148             iMessageUids.Reset();
       
   149             if(entry.SeenIMAP4Flag())
       
   150                 {
       
   151                 // issue the store command
       
   152                 iSession->StoreL(iStatus, uidSeq->Des(), KImapAddFlags, KImapFlagSeen, ETrue, iFlagInfoResponseArray);
       
   153                 
       
   154                 }
       
   155             else
       
   156                 {
       
   157                 // issue the store command
       
   158                 iSession->StoreL(iStatus, uidSeq->Des(), KStoreFlagsClearCommand, KImapFlagSeen, ETrue, iFlagInfoResponseArray);
       
   159                 }
       
   160             }
       
   161         else  //Selection iMarkFlag is Trure
       
   162             {
       
   163             TMsvEntry *aNewEntry1;
       
   164 			
       
   165 			TInt  count = iSourceSel->Count();
       
   166 			while (count--)
       
   167 				{
       
   168 				iServerEntry.GetEntryFromId(iSourceSel->At(count), aNewEntry1);
       
   169 				TMsvEmailEntry entry(*aNewEntry1);
       
   170 				iMessageUids.AppendL(entry.UID());
       
   171 					
       
   172 				if(iMarkFlag) // True  means  Unread
       
   173 					 {
       
   174 					 entry.SetSeenIMAP4Flag(EFalse);
       
   175 					 }
       
   176 				 else
       
   177 					 {
       
   178 					 entry.SetSeenIMAP4Flag(ETrue);  
       
   179 					 }
       
   180 				SetEntryL((*iSourceSel)[count]);
       
   181 				User::LeaveIfError(iServerEntry.ChangeEntry(entry));
       
   182 				}// end of while
       
   183 
       
   184 			uidSeq = CImapSession::CreateSequenceSetLC(iMessageUids);
       
   185 	
       
   186 			// finished with the UID array. Clear it.
       
   187 			iMessageUids.Reset();
       
   188 			if(!iMarkFlag) //read
       
   189 				{
       
   190 				// issue the store command
       
   191 				iSession->StoreL(iStatus, uidSeq->Des(), KImapAddFlags, KImapFlagSeen, ETrue, iFlagInfoResponseArray);
       
   192 				}
       
   193 			else   //Unread
       
   194 				{
       
   195 				// issue the store command
       
   196 				iSession->StoreL(iStatus, uidSeq->Des(), KStoreFlagsClearCommand, KImapFlagSeen, ETrue, iFlagInfoResponseArray);
       
   197 				}
       
   198              } //End of else (Selection)
       
   199 	
       
   200 		iProgressState = TImap4GenericProgress::EBusy;
       
   201 		CleanupStack::PopAndDestroy(uidSeq);
       
   202 		iNextStep = EFinished;
       
   203 		break;
       
   204 		
       
   205 		}
       
   206 	
       
   207 	
       
   208 	case EFinished:
       
   209 		{
       
   210 		__LOG_TEXT(iSession->LogId(), "CImapUpdateFlagOperation::Completing OK");
       
   211 		iProgressState = TImap4GenericProgress::EIdle;
       
   212 		Complete(KErrNone);
       
   213 		return ETrue;
       
   214 		}
       
   215 		
       
   216 	default:
       
   217 		{
       
   218 		__ASSERT_DEBUG(EFalse, 
       
   219 					   TImapServerPanic::ImapPanic(TImapServerPanic::ECreateCompoundUnexpectedState));
       
   220 		// unexpected state - complete the request
       
   221 		iProgressState = TImap4GenericProgress::EIdle;
       
   222 		return ETrue;
       
   223 		}
       
   224 		}
       
   225 
       
   226 	if (!IsActive())
       
   227 		{
       
   228 		SetActive();
       
   229 		}
       
   230 	return EFalse;
       
   231 	}
       
   232 
       
   233 void CImapUpdateFlagOperation::DoCancel()
       
   234 	{
       
   235 	iProgressErrorCode = KErrCancel;
       
   236 	
       
   237 	switch (iCurrentStep)
       
   238 		{
       
   239 	case EUpdateFlag:
       
   240 		{
       
   241 		// outstanding request is on session
       
   242 		iSession->Cancel();
       
   243 		break;
       
   244 		}
       
   245 	case EFinished:
       
   246 		{
       
   247 		// self-completed or no outstanding request.
       
   248 		break;
       
   249 		}
       
   250 
       
   251 	default:
       
   252 		{
       
   253 		__ASSERT_DEBUG(EFalse,
       
   254 		               TImapServerPanic::ImapPanic(TImapServerPanic::ECreateCompoundCancelUnexpectedState));
       
   255 		break;
       
   256 		}
       
   257 		} // end switch (iCurrentStep)
       
   258 	
       
   259 	CMsgActive::DoCancel();
       
   260 	}
       
   261 
       
   262 
       
   263 
       
   264 
       
   265 void CImapUpdateFlagOperation::Progress(TImap4CompoundProgress& aCompoundProgress)
       
   266 	{
       
   267 	// Create does not set iOperation, it just sets iState to EBusy 
       
   268 	// when doing the creation
       
   269 	aCompoundProgress.iGenericProgress.iState = iProgressState;
       
   270 
       
   271 	
       
   272 	// Put error into progress buffer
       
   273 	if( aCompoundProgress.iGenericProgress.iErrorCode == KErrNone )
       
   274 		{
       
   275 		aCompoundProgress.iGenericProgress.iErrorCode = iProgressErrorCode;
       
   276 		}
       
   277 	}
       
   278 
       
   279 
       
   280 /**
       
   281 Handles NO/BAD responses according to current step.
       
   282 Negative server responses are not fatal - the error is reported in the 
       
   283 progress report, and the operation completes.
       
   284 
       
   285 @return KErrNone if the error has been handled
       
   286 		Completion error code otherwise.
       
   287 */
       
   288 TInt CImapUpdateFlagOperation::ProcessNegativeServerResponse()
       
   289 	{
       
   290 	TInt err = iStatus.Int();
       
   291 	switch (iCurrentStep)
       
   292 		{
       
   293 	case EUpdateFlag:
       
   294 		iNextStep = EFinished;
       
   295 		break;
       
   296 	case EFinished:
       
   297 	default:
       
   298 		{
       
   299 		// positive error code not expected,
       
   300 		// self-completed states or no outstanding request.
       
   301 		TImapServerPanic::ImapPanic(TImapServerPanic::ECreateCompoundUnexpectedState);
       
   302 		break;
       
   303 		}
       
   304 		
       
   305 		} // end switch (iCurrentStep)
       
   306 	iProgressErrorCode = err;
       
   307 	return KErrNone;
       
   308 	}
       
   309 
       
   310 /**
       
   311 Resume the operation following a bearer migration.
       
   312 */
       
   313 void CImapUpdateFlagOperation::ResumeOperationL(TRequestStatus& aStatus, CImapSession& aSession)
       
   314 	{
       
   315 	iSession = &aSession;
       
   316 	__LOG_TEXT(iSession->LogId(), "CImapUpdateFlagOperation::Resuming");
       
   317 	__ASSERT_DEBUG(iCurrentStep==ESuspendedForMigrate, TImapServerPanic::ImapPanic(TImapServerPanic::EUpdateCompoundUnexpectedState));
       
   318 	iStopForMigrate = EFalse;
       
   319 
       
   320 	switch (iNextStep)
       
   321 		{
       
   322 		case EUpdateFlag:
       
   323 			{
       
   324 			// just return to the main state machine
       
   325 			CompleteSelf();
       
   326 			break;
       
   327 			}
       
   328 		case EFinished:
       
   329 		default:
       
   330 			{
       
   331 			__ASSERT_DEBUG(EFalse, TImapServerPanic::ImapPanic(TImapServerPanic::EUpdateCompoundUnexpectedState));
       
   332 			// abandon the compound operation
       
   333 			iNextStep=EFinished;
       
   334 			CompleteSelf();
       
   335 			break;
       
   336 			}
       
   337 		}
       
   338 	Queue(aStatus);
       
   339 	CompleteSelf();
       
   340 	}
       
   341