email/imap4mtm/imapsession/inc/cimapsession.h
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 #ifndef __CIMAPSESSION_H__
       
    17 #define __CIMAPSESSION_H__
       
    18 
       
    19 #include <e32std.h>
       
    20  
       
    21 #include "cimaplistfolderinfo.h"
       
    22 #include "cimapfetchresponse.h"
       
    23 #include "cimapcommand.h"
       
    24 
       
    25 #include "msocketconnectobserver.h"
       
    26 #include "minputstreamobserver.h"
       
    27 #include "moutputstreamobserver.h"
       
    28 #include "cimapobservabletimer.h"
       
    29 
       
    30 #if (defined SYMBIAN_EMAIL_CAPABILITY_SUPPORT)
       
    31 	#include "cimapauthhelpers.h"
       
    32 #endif
       
    33 
       
    34 // Forward Declarations
       
    35 class CImSendMessage;
       
    36 class CImapServerGreetingInfo;
       
    37 class CImapCapabilityInfo;
       
    38 class CImapFolderInfo;
       
    39 class CFetchBodyInfo;
       
    40 class CImapFetchBodyResponse;
       
    41 class CImapSettings;
       
    42 class CImapMailStore;
       
    43 
       
    44 #if (defined SYMBIAN_EMAIL_CAPABILITY_SUPPORT)
       
    45 	class CImapAuthMechanismHelper;
       
    46 #endif
       
    47 /**
       
    48 Detects and records overflows during calls to RBuf8::AppendFormat().
       
    49 This class does nothing to resolve the overflow, but gives methods such as 
       
    50 CImapSession::AppendAndGrowFormatL() the opportunity to detect the success 
       
    51 of calling RBuf8::AppendFormat(), so that they can retry with a larger
       
    52 buffer if an overflow occured.
       
    53 @internalTechnology
       
    54 @prototype
       
    55 */
       
    56 class TImapOverflowDetector : public TDes8Overflow
       
    57 	{
       
    58 public:
       
    59 	TImapOverflowDetector();
       
    60 	TBool OverflowDetected();
       
    61 
       
    62 private:
       
    63 	// Implements TDes8Overflow
       
    64 	void Overflow(TDes8& aDes);
       
    65 
       
    66 private:
       
    67 	TBool iOverflowDetected;
       
    68 	};
       
    69 
       
    70 /**
       
    71 Represents a connected session with the IMAP server.
       
    72 This class provides a method for each command that can be sent to the IMAP server.
       
    73 Only one command may be run at a time.
       
    74 @internalTechnology
       
    75 @prototype
       
    76 */
       
    77 class CImapSession : public CBase
       
    78 				   , public MInputStreamObserver
       
    79 				   , public MOutputStreamObserver
       
    80 				   , public MImapTimerObserver
       
    81 	{
       
    82 public:
       
    83 	enum TImapServerState
       
    84 		{
       
    85 		EServerStateNone,
       
    86 		EServerStateNotAuthenticated,
       
    87 		EServerStateAuthenticated,
       
    88 		EServerStateSelected,
       
    89 		};
       
    90 		
       
    91 	IMPORT_C static CImapSession* NewL(CImapSettings& aImapSettings, CImapMailStore& aImapMailStore, MInputStream& aInputStream, MOutputStream& aOutputStream);
       
    92 	~CImapSession();
       
    93 
       
    94 	IMPORT_C TImapServerState ServerState();
       
    95 	IMPORT_C CImapFolderInfo* SelectedFolderInfo();
       
    96 	IMPORT_C const CImapServerGreetingInfo& ServerGreetingInfo();
       
    97 	IMPORT_C const CImapCapabilityInfo& CapabilityInfo();
       
    98 
       
    99 	// Support for IDLE mode (Authenticated or Selected state required for IDLE)
       
   100 	IMPORT_C TBool ImapIdleSupported() const;
       
   101 	
       
   102 	IMPORT_C void EnterIdleL(TRequestStatus& aStatus);
       
   103 	IMPORT_C void WaitForIdleEvent(TRequestStatus& aStatus);
       
   104 	IMPORT_C void DoneIdle(TRequestStatus& aStatus);
       
   105 	
       
   106 	// Support for reading unsolicited server events.
       
   107 	// For use when IDLE mode is not available.
       
   108 	IMPORT_C void WaitForServerEventL(TRequestStatus& aStatus);
       
   109 			
       
   110 	// Imap Commands
       
   111 	
       
   112 	//  > Establishing a Connection
       
   113 	IMPORT_C void ReadServerGreetingL(TRequestStatus& aStatus);
       
   114 	
       
   115 	//  > Any State Commands
       
   116 	IMPORT_C void CapabilityL(TRequestStatus& aStatus);
       
   117 	IMPORT_C void NoopL(TRequestStatus& aStatus);
       
   118 	IMPORT_C void LogoutL(TRequestStatus& aStatus);
       
   119 	
       
   120 	//  > Not-Authenticated State Commands
       
   121 	IMPORT_C void LoginL(TRequestStatus& aStatus, const TDesC8& aUserName, const TDesC8& aPassword);
       
   122 	IMPORT_C void StartTlsL(TRequestStatus& aStatus);
       
   123 	
       
   124 	//  > Authenticated State Commands
       
   125 	IMPORT_C void SelectL(TRequestStatus& aStatus, CImapFolderInfo* aFolderInfo, TBool aSelectBox = ETrue);
       
   126 	IMPORT_C void ExamineL(TRequestStatus& aStatus, CImapFolderInfo* aFolderInfo);
       
   127 	IMPORT_C void CreateL(TRequestStatus& aStatus, const TDesC& aMailboxName);
       
   128 	IMPORT_C void DeleteL(TRequestStatus& aStatus, const TDesC& aMailboxName);
       
   129 	IMPORT_C void RenameL(TRequestStatus& aStatus, const TDesC& aExistingMailboxName, const TDesC& aNewMailboxName);
       
   130 	IMPORT_C void SubscribeL(TRequestStatus& aStatus, const TDesC& aMailboxName);
       
   131 	IMPORT_C void UnsubscribeL(TRequestStatus& aStatus, const TDesC& aMailboxName);
       
   132 	IMPORT_C void ListL(TRequestStatus& aStatus, const TDesC& aReferenceName, const TDesC& aMailboxName, RArrayImapListFolderInfo& aFolderList);
       
   133 	IMPORT_C void LsubL(TRequestStatus& aStatus, const TDesC& aReferenceName, const TDesC& aMailboxName, RArrayImapListFolderInfo& aFolderList);
       
   134 	IMPORT_C void StatusL(TRequestStatus& aStatus, const TDesC& aMailboxName, const TDesC8& aDataItemNames, CImapFolderInfo& aFolderInfo);
       
   135 	IMPORT_C void AppendL(TRequestStatus& aStatus, CImSendMessage& aSource, const TDesC& aDestinationMailboxName);
       
   136 	
       
   137 	//  > Selected State Commands
       
   138 	IMPORT_C void CloseL(TRequestStatus& aStatus);
       
   139 	IMPORT_C void SearchL(TRequestStatus& aStatus, const TDesC8& aSearchCriteria, RArray<TUint>& aMatchingMessageIds);
       
   140 	IMPORT_C void FetchFlagsL(TRequestStatus& aStatus, const TDesC8& aSequenceSet, RArrayMessageFlagInfo& aOutFlagInfo);
       
   141 	IMPORT_C void FetchBodyL(TRequestStatus& aStatus, TUint aMessageUid, TBool aPeek, CFetchBodyInfo& aFetchBodyInfo, CImapFetchBodyResponse& aFetchBodyResponse);
       
   142 	IMPORT_C void FetchBodyStructureAndHeadersL(TRequestStatus& aStatus, TUint aMessageUid, const TDesC8& aFieldNames, CImapFetchResponse& aOutFetchResponse);
       
   143 	IMPORT_C void FetchBodyStructureAndHeadersL(TRequestStatus& aStatus, const TDesC8& aSequenceSet, const TDesC8& aFieldNames, MImapFetchStructureObserver& aObserver);
       
   144 	IMPORT_C void StoreL(TRequestStatus& aStatus, const TDesC8& aSequenceSet, const TDesC8& aMessageDataItem, const TDesC8& aValue, TBool aUseSilent, RArrayMessageFlagInfo& aOutMessageFlagInfo);
       
   145 	IMPORT_C void CopyL(TRequestStatus& aStatus, const TDesC8& aSequenceSet, const TDesC& aDestinationMailboxName);
       
   146 	IMPORT_C void ExpungeL(TRequestStatus& aStatus);
       
   147 
       
   148 	IMPORT_C static HBufC8* CreateSequenceSetLC(RArray<TUint>& aMessageUids);
       
   149 	
       
   150 	IMPORT_C void Cancel();
       
   151 	IMPORT_C void FlushCancelledCommand(TRequestStatus& aStatus);
       
   152 	
       
   153 	// Returns logger file identifier
       
   154 	IMPORT_C TInt LogId() const;
       
   155 	
       
   156 #if (defined SYMBIAN_EMAIL_CAPABILITY_SUPPORT)
       
   157 	IMPORT_C void AuthenticateL(TRequestStatus& aStatus,CImapAuthMechanismHelper::TImapAuthProfileFlag iCurrentAuthProfile);
       
   158 #endif
       
   159 	// Non-IMPORTED methods
       
   160 	void FetchBodyOperationComplete(TInt aErrorCode);
       
   161 	void AsyncSendFailed(TInt aErr);
       
   162 
       
   163 	MInputStream* InputStream();
       
   164 	MOutputStream* OutputStream();
       
   165 
       
   166 private:
       
   167 	/**
       
   168 	Specifies which operation to perform in DoAsyncCallBackL()
       
   169 	*/
       
   170 	enum TAsyncCallBackOpCode
       
   171 		{
       
   172 		EAsyncCallBackOpNone,
       
   173 		EAsyncCallBackOpProcessInputBuffer,
       
   174 		};
       
   175 	
       
   176 	/**
       
   177 	Specifies the high-level state of the session
       
   178 	*/	
       
   179 	enum TSessionState
       
   180 		{
       
   181 		/**
       
   182 		Session running as normal.
       
   183 		*/
       
   184 		ESessionNormal,
       
   185 		/**
       
   186 		Cancel has been called and the command needs to be flushed.
       
   187 		Session will return to a normal state after FlushCancelledCommand has completed successfully.
       
   188 		*/
       
   189 		ESessionFlushing,
       
   190 		/**
       
   191 		An unrecoverable error has occurred.  
       
   192 		When entering this state, the input and output streams will be closed automatically.
       
   193 		But the session object itself needs to be destroyed by its owner, and a new session started in its place.
       
   194 		*/
       
   195 		ESessionUnrecoverable,
       
   196 		};
       
   197 		
       
   198 	/**
       
   199 	Specifies any special processing that needs to be performed after a command completes
       
   200 	*/	
       
   201 	enum TPendingOperation
       
   202 		{
       
   203 		EOpNone,
       
   204 		EOpServerGreeting,
       
   205 		EOpSelect,
       
   206 		EOpLogin,
       
   207 		EOpLogout,
       
   208 		EOpClose,
       
   209 		EOpWaitForBodyOperationComplete,
       
   210 		EOpIdleEnter,
       
   211 		EOpIdleWait,
       
   212 #if (defined SYMBIAN_EMAIL_CAPABILITY_SUPPORT)
       
   213 		EOpAuthenticate,
       
   214 #endif
       
   215 		EOpIdleDone
       
   216 
       
   217 		};
       
   218 	
       
   219 	CImapSession(CImapSettings& aImapSettings, CImapMailStore& aImapMailStore, MInputStream& aInputStream, MOutputStream& aOutputStream);
       
   220 	void ConstructL();
       
   221 
       
   222 	void StartCommandL(TRequestStatus& aStatus, CImapCommand* aCommand);
       
   223 	void RequestReadData();
       
   224 
       
   225 	void ProcessInputBufferAsync();	
       
   226 	void ProcessInputBufferL();
       
   227 	TBool TryFindLiteralBlockL(TPtrC8& aDataToDeliver);
       
   228 	TBool TryFindLineL(TPtrC8& aDataToDeliver);
       
   229 
       
   230 	// Implement MInputStreamObserver
       
   231 	void ReceivedDataIndL(const TDesC8& aBuffer);
       
   232 	void SecureServerCnf();
       
   233 	void InputStreamCloseInd(TInt aError);
       
   234 	
       
   235 	// Implement MOutputStreamObserver
       
   236 	void SendDataCnf();
       
   237 	void OutputStreamCloseInd(TInt aError);
       
   238 
       
   239 	void CommandComplete();
       
   240 	TBool CompleteIfStreamsClosed(TRequestStatus& aStatus);
       
   241 	void CompleteAndDestroyCommand(TInt aCompletionCode, TBool aCloseStreams);
       
   242 	void DoCancel();
       
   243 	
       
   244 	TBool DoPendingOperationForOk();
       
   245 	void DoPendingOperationForFail();
       
   246 	void DoPendingOperationForCancel();
       
   247 	TBool DoPendingOperationForIntermediateResponse();
       
   248 
       
   249 	// Implement MImapTimerObserver
       
   250 	void OnTimerL(const CImapObservableTimer& aSourceTimer);
       
   251 
       
   252 	// Managing iReport
       
   253 	void Queue(TRequestStatus& aStatus);
       
   254 	void Complete(TInt aStatus);
       
   255 	void DoComplete(TInt& aStatus);
       
   256 	
       
   257 	// Async callback
       
   258 	void RequestAsyncCallBack(TAsyncCallBackOpCode aAsyncCallBackOpCode);
       
   259 	
       
   260 	static TInt AsyncCallBack(TAny* aSelf);
       
   261 	void AsyncCallBack();
       
   262 	void DoAsyncCallBackL();
       
   263 	
       
   264 	// State Setters (includes logging)
       
   265 	void SetSessionState(TSessionState aSessionState);
       
   266 	void SetPendingOperation(TPendingOperation aPendingOperation);
       
   267 	void SetServerState(TImapServerState aServerState);
       
   268 	
       
   269 	static void AppendAndGrowFormatL(RBuf8& aBuffer, TRefByValue<const TDesC8> aFormat, TImapOverflowDetector* aOverflowHandler, ...);
       
   270 	
       
   271 private:
       
   272 	CImapSettings& iImapSettings;
       
   273 	CImapMailStore& iImapMailStore;
       
   274 
       
   275 	MInputStream* iInputStream;
       
   276 	MOutputStream* iOutputStream;
       
   277 	
       
   278 	TPtrC8 iInputBuffer;
       
   279 	CBufFlat* iInputCache;
       
   280 	
       
   281 	TImapServerState iServerState;
       
   282 	
       
   283 	TPendingOperation iPendingOperation;
       
   284 	
       
   285 	TAsyncCallBackOpCode iAsyncCallBackOpCode;
       
   286 	CAsyncCallBack* iAsyncCallBack;
       
   287 	
       
   288 	// When flushing a cancelled command, 
       
   289 	// timeout if the expected data is not received from the server in time.
       
   290 	CImapObservableTimer* iFlushCommandTimeout;
       
   291 	
       
   292 	TSessionState iSessionState;
       
   293 	
       
   294 	/**
       
   295 	Records information about the currently selected folder.
       
   296 	This object is passed into CImapSession by SelectL() and ExamineL().
       
   297 	CImapSession takes ownership of the object, and destroying the current object.
       
   298 	CImapSession ensures that iSelectedFolderInfo is NULL whenever it is NOT in EServerStateSelected state.
       
   299 	*/
       
   300 	CImapFolderInfo* iSelectedFolderInfo;
       
   301 	
       
   302 	CImapCommand* iCurrentCommand; // Is NULL when no command is in progress.  OWNED.
       
   303 	TInt iCommandLiteralRequestSize;
       
   304 	
       
   305 	TInt iNextTagId;
       
   306 	
       
   307 	// Capabilities
       
   308 	CImapServerGreetingInfo* iServerGreetingInfo;
       
   309 	CImapCapabilityInfo* iCapabilityInfo;
       
   310 	
       
   311 	TRequestStatus* iReport;
       
   312 
       
   313 	TInt iLogId;
       
   314 #if (defined SYMBIAN_EMAIL_CAPABILITY_SUPPORT)
       
   315 	CImapAuthMechanismHelper::TImapAuthProfileFlag iCurrentAuthProfile;
       
   316 #endif
       
   317 	};
       
   318 								 
       
   319 #endif // __CIMAPSESSION_H__