email/imap4mtm/imapsession/src/cimapidle.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 
       
    17 #include "cimapidle.h"
       
    18 #include "moutputstream.h"
       
    19 #include "cimapsessionconsts.h"
       
    20 #include "imappaniccodes.h"
       
    21 #include "cimaplogger.h"
       
    22 
       
    23 // IMAP IDLE command
       
    24 _LIT8(KTxtIdleFormat, "%d IDLE\r\n");
       
    25 const TInt KIdleFormatParamChars = -2; // 2 chars in "%d"
       
    26 
       
    27 _LIT8(KTxtDoneCrlf, "DONE\r\n");
       
    28 
       
    29 CImapIdle* CImapIdle::NewL(CImapFolderInfo* aSelectedFolderData, TInt aLogId)
       
    30 	{
       
    31 	CImapIdle* self = new (ELeave) CImapIdle(aSelectedFolderData, aLogId);
       
    32 	CleanupStack::PushL(self);
       
    33 	self->ConstructL();
       
    34 	CleanupStack::Pop();
       
    35 	return self;		
       
    36 	}	
       
    37 
       
    38 CImapIdle::CImapIdle(CImapFolderInfo* aSelectedFolderData, TInt aLogId) 
       
    39 	: CImapCommandEx(aSelectedFolderData, aLogId)
       
    40 	, iIdleState(iIdleState)
       
    41 	{
       
    42 	}
       
    43 	
       
    44 void CImapIdle::ConstructL()
       
    45 	{
       
    46 	}
       
    47 	
       
    48 CImapIdle::~CImapIdle()
       
    49 	{
       
    50 	}
       
    51 
       
    52 /** 
       
    53 Formats and sends the IMAP IDLE command.
       
    54 @param aTagId Command sequence id which will be send along with the IMAP command.
       
    55 @param aStream A wrapper for the outbound stream of a connected socket, using which 
       
    56 the command will be send to the server
       
    57 */
       
    58 void CImapIdle::SendMessageL(TInt aTagId, MOutputStream& aStream)
       
    59 	{
       
    60 	iTagId = aTagId;
       
    61 	TInt bufferLength = KTxtIdleFormat().Length() - KIdleFormatParamChars + TagLength(aTagId);
       
    62 	
       
    63 	__ASSERT_DEBUG(iOutputBuffer==NULL, TImapServerPanic::ImapPanic(TImapServerPanic::ECommandOutputBufferNotNull));
       
    64 	iOutputBuffer = HBufC8::NewL(bufferLength);
       
    65 	iOutputBuffer->Des().Format(KTxtIdleFormat, iTagId);
       
    66 	
       
    67 	//send the command to the server
       
    68 	aStream.SendDataReq(*iOutputBuffer);
       
    69 	}
       
    70 
       
    71 /**
       
    72 If waiting for a continuation, iIdleState is updated to waiting for untagged responses.
       
    73 This state change will be detected by the session.
       
    74 When not waiting for a continuation, then the method will leave with KErrImapCorrupt.
       
    75 */
       
    76 void CImapIdle::ParseContinuationResponseL()
       
    77 	{
       
    78 	if (iIdleState == EIdleWaitContinue)
       
    79 		{
       
    80 		iIdleState = EIdleWaitResponse;
       
    81 		}
       
    82 	else
       
    83 		{
       
    84 		CorruptDataL();
       
    85 		}			
       
    86 	}
       
    87 
       
    88 /**
       
    89 Overrides the default version of cancel 
       
    90 because we don't want to "flush" on cancel while in the EIdleWaitResponse state.
       
    91 After CImapSession::Cancel() is called during EIdleWaitResponse, 
       
    92 the user should call DoneL() next, not FlushCancelledCommand()
       
    93 */
       
    94 void CImapIdle::Cancel()
       
    95 	{
       
    96 	__LOG_TEXT(iLogId, "CImapIdle::Cancel()"); // Overrides CImapCommand::Cancel()
       
    97 	
       
    98 	if (iIdleState != EIdleWaitResponse)
       
    99 		{
       
   100 		// When in EIdleWaitDone, a cancel would require a flush as normal, as we are now expecting a tagged OK.
       
   101 		
       
   102 		// When in EIdleWaitContinue, setting flush to true on cancel will cause a corrupt data error when the 
       
   103 		// continuation character is received, and the session will have to be shut down.
       
   104 		// This is reasonable as the command would otherwise have to send data to the server, which is not
       
   105 		// what we want to do when flushing a cancel.
       
   106 			
       
   107 		EnterFlushingState();
       
   108 		}	
       
   109 	}
       
   110 	
       
   111 /**
       
   112 Sends the "DONE" command to the server, which should reply with 
       
   113 zero or more untagged status responses, followed by the tagged OK response.
       
   114 */
       
   115 void CImapIdle::SendDone(MOutputStream& aStream)
       
   116 	{
       
   117 	__ASSERT_DEBUG(iIdleState == EIdleWaitResponse, TImapServerPanic::ImapPanic(TImapServerPanic::EIdleStateInvalid));
       
   118 	
       
   119 	//send the command to the server
       
   120 	iIdleState = EIdleWaitDone;
       
   121 	// DONE command is sent with a 10 seconds tineout.
       
   122 	aStream.SendDataReq(KTxtDoneCrlf, 10);
       
   123 	}
       
   124 
       
   125 /**
       
   126 @return The current state of the idle command.
       
   127 */
       
   128 CImapIdle::TIdleState CImapIdle::IdleState()
       
   129 	{
       
   130 	return iIdleState;
       
   131 	}