// MIUTMSG.H
//
// Copyright (c) 1998 Symbian Ltd.  All rights reserved. 
//

#if !defined (__MIUTMSG_H__)
#define __MIUTMSG_H__

#if !defined(__MSVSTD_H__)
#include <msvstd.h>
#endif

#if !defined (__MSVAPI_H__)
#include <msvapi.h>			// TMsvEntry, CMsvOperation etc
#endif

#if !defined (__MENTACT_H__)
#include <mentact.h>		// CMsgActive
#endif

#if !defined (__MIUTHDR_H__)
#include "miuthdr.h"		// CImHeader, CImMimeHeader
#endif

#if !defined (__TXTRICH_H__)
#include <txtrich.h>		// CRichText etc
#endif

#if !defined (__MIUTDEF_H__)
#include "miutdef.h"		// CImHeader, CImMimeHeader
#endif

#if !defined (__MTMDEF_H__)
#include <mtmdef.h>
#endif

class TImAttachmentInfo
	{
public:
	TFileName iFullName;
	TInt32 iSize;
	TMsvId iId;
	};


// Mixin provides interface to Browser control to resolve MHTML URIs.

class MImURITranslator
	{
public:
	virtual void GetUniversalResourceIdentifierL(TMsvId aMessageId, TDes& rURI) = 0;
	};


class CImEmailMessage : public CMsgActive , public MImURITranslator
	{
public:
	enum TImEntryType
		{
		EBodyTextEntries=0,	
		EMailHeader,
		EMimeHeader,
		EMessageDigest
		//.. add new items here
		};
public:
	enum TImAttachmentType
		{
		EAllAttachments=0,	// all attachment files contained within this message (but not inside messages within this message)
		EVCards,
		EVCalendars,
		EVEntries,			// ie VCards & VCalendars
		EEncrypted,
		ESigned,
		ESecure				// ie Encrypted or signed (or both)
		//.. add new items here
		};
	enum TImBodyTextType
		{
		EBodyTextForThisMessage,
		EBodyTextForAllMessages
		};
public:
	IMPORT_C static CImEmailMessage* NewL(CMsvEntry& aServerEntry);
	IMPORT_C static CImEmailMessage* NewLC(CMsvEntry& aServerEntry);
	IMPORT_C ~CImEmailMessage();

	IMPORT_C void GetEntriesL(TRequestStatus& aStatus, TMsvId aMessageId, TImEntryType aEntryType);
	IMPORT_C void GetAttachmentsListL(TRequestStatus& aStatus, TMsvId aMessageId, TImAttachmentType aAttachmentType);

	IMPORT_C void GetFirstHTMLPageL(TRequestStatus& aStatus, TMsvId aMessageId);

	IMPORT_C const CMsvEntrySelection& Selection();	// return selection
	IMPORT_C const CArrayFixFlat<TImAttachmentInfo>& AttachmentSelection();	// return selection

	IMPORT_C void Progress(TInt& rSoFar, TInt& rTotal);

	IMPORT_C void GetBodyTextL(TRequestStatus& aStatus, TMsvId aMessageId, TImBodyTextType aParam, CRichText& rRichText,CParaFormatLayer& aParaLayer,CCharFormatLayer& aCharLayer);
	IMPORT_C void GetUniversalResourceIdentifierL(TMsvId aMessageId, TDes& rURI);	// Mixin function implementation

private:
	void DoRunL();
	void DoCancel();
	CImEmailMessage(CMsvEntry& aEntry);
	void ConstructL();
private:
	void Reset();
	void ChangeMessageContextL();
	void CompareChildrenAndPopulateSelL();
	void AttachmentInfoL();
	void AppendBodyTextEntriesL();
	void AssembleBodyTextL();
private:
	void AppendAttachmentL();
	TBool MessageDigestEntry();
	TBool HandleDifferentFolderTypesL();
private:
	enum TImEmailMessageStates
		{
		EIdle=0,
		EGetEmailMessageBodyText,
		EAssembleBodyText,
		EGetHTMLPage,
		EGetEmailDigestList,
		EGetEmailAttachmentsList,
		EFinished
		};
	CMsvEntry& iClientEntry;
	CMsvEntrySelection* iCompleteSel;
	CMsvEntrySelection* iResultSel;

	CRichText* iRichText;	// I don't own this object!
	TInt iRichTextIndex;
	CParaFormatLayer* iParaLayer;
	CCharFormatLayer* iCharLayer;

	CArrayFixFlat<TImAttachmentInfo>* iAttachmentList;
	TMsvId iMsgEntryId;			// the Id passed into Getxxx() function representing the 'parent' message ID
	TInt iState;
	TImAttachmentType iAttachmentType;
	TImEntryType iEntryType;
	TMsvEmailEntry iEntry;
	};



class CImEmailOperation : public CMsvOperation
	{
public:
	IMPORT_C static CImEmailOperation* CreateNewL(TRequestStatus& aObserverRequestStatus, CMsvSession& aMsvSession, TMsvId aDestination, TMsvPartList aPartList, const TMsvEmailTypeList& aMsvEmailTypeList, TUid aMsgType);
	IMPORT_C static CImEmailOperation* CreateReplyL(TRequestStatus& aObserverRequestStatus, CMsvSession& aMsvSession, TMsvId aMessageId, TMsvId aDestination, TMsvPartList aPartList, const TDesC& aFormatString, const TMsvEmailTypeList& aMsvEmailTypeList, TUid aMsgType);
	IMPORT_C static CImEmailOperation* CreateReplyL(TRequestStatus& aObserverRequestStatus, CMsvSession& aMsvSession, TMsvId aMessageId, TMsvId aDestination, TMsvPartList aPartList, const TMsvEmailTypeList& aMsvEmailTypeList, TUid aMsgType);
	IMPORT_C static CImEmailOperation* CreateForwardL(TRequestStatus& aObserverRequestStatus, CMsvSession& aMsvSession, TMsvId aMessageId, TMsvId aDestination, TMsvPartList aPartList, const TDesC& aFormatString, const TMsvEmailTypeList& aMsvEmailTypeList, TUid aMsgType);
	IMPORT_C static CImEmailOperation* CreateForwardL(TRequestStatus& aObserverRequestStatus, CMsvSession& aMsvSession, TMsvId aMessageId, TMsvId aDestination, TMsvPartList aPartList, const TMsvEmailTypeList& aMsvEmailTypeList, TUid aMsgType);
	IMPORT_C static CImEmailOperation* CreateReceiptL(TRequestStatus& aObserverRequestStatus, CMsvSession& aMsvSession, TMsvId aMessageId, TMsvId aDestination, TMsvPartList aPartList, const TDesC& aFormatString, const TMsvEmailTypeList& aMsvEmailTypeList, TUid aMsgType);
	IMPORT_C static CImEmailOperation* CreateReceiptL(TRequestStatus& aObserverRequestStatus, CMsvSession& aMsvSession, TMsvId aMessageId, TMsvId aDestination, TMsvPartList aPartList, const TMsvEmailTypeList& aMsvEmailTypeList, TUid aMsgType);
	IMPORT_C void DoCancel();
	IMPORT_C void RunL();
	IMPORT_C const TDesC8& ProgressL();
	IMPORT_C ~CImEmailOperation();
private:
	enum TImEmailOperation 
		{
		ENew,
		EReply,
		EForward,
		EReceipt,
		EDeletingBadEmail
		};
private:
	CImEmailOperation(TRequestStatus& aObserverRequestStatus, CMsvSession& aMsvSession, TMsvId aDestination, TMsvPartList aPartList, const TMsvEmailTypeList& aMsvEmailTypeList, TUid aMsgType);
	void ConstructL(TMsvId aMessageId, const TDesC& aFormatString, TImEmailOperation aOperation);
	void ConstructL(TMsvId aMessageId, TImEmailOperation aOperation);
	void ConstructL(TImEmailOperation aOperation);
	void SelectNextState();					// selects next state to go to
	void ChangeStateL();					// initiates the next state operation
	void CreateNewHeaderL();
	void GetBodyTextL();
	void GetAttachmentListL();
	void CreateEntryDetails(TMsvEmailEntry& aMsvEmailEntry);
	void CreateNewMessageL();
	void StoreBodyAndHeaderL();
	void CreateAttachmentEntry();
	void StoreAttachmentL();
	void CompleteEmailOperationL();
	void RequestComplete(TInt aError);
	void OpenAndReadResourceFileL();
	void SetUserEmailAddressFromDefaultSmtpServiceL();
	void SetUserEmailAddressL();
	void SetUserEmailAddressFromSmtpServiceL();
	void ErrorRecovery(TInt error);
	
private:
	enum TImEmailOperationState
		{
		ECreateNewHeader,
		EGetBodyText,
		EGetAttachmentList,
		ECreateNewMessageEntry,
		EStoreBodyAndHeader,
		ECreateAttachmentEntry,
		EStoreAttachment,
		ECompleteEmailOperation,
		EFinished
		};
	
	CMsvEntry* iMsvEntry;
	CMsvOperation* iMsvOperation;
	
	CImHeader* iNewHeader;
	CParaFormatLayer* iParaLayer;
	CCharFormatLayer* iCharLayer;
	CRichText* iRichText;
	CImEmailMessage* iEmailMessage;
	HBufC* iFormatString;
	CFileMan* iFileMan;
	
	TInt iState;
	TImEmailOperation iOperation;
	TMsvId iDestinationId;
	TMsvId iOrigMessageId;
	TMsvId iNewMessageId;
	TUid iMsgType;
	TMsvPartList iPartList;
	TMsvEmailTypeList iMsvEmailTypeList;
	TInt iAttachmentCount;
	TInt iTotalAttachments;
	TPckgBuf<TMsvId> iDataMember;
	RFs iFs;
	HBufC* iBodyHeaderFormatString;
	HBufC* iBodyHeader;
	HBufC* iUserEmailAddress;
	};

#endif