// IMCVSEND.H
//
// Copyright (c) 1998 Symbian Ltd.  All rights reserved. 
//

#if !defined(__IMCVSEND_H__)
#define __IMCVSEND_H__

#if !defined (__IMUTDLL_H__)
#include "imutdll.h"
#endif

#if !defined (__S32MEM_H__)
#include <s32mem.h>
#endif

#if !defined (__IMCVTEXT_H__)
#include "imcvtext.h"
#endif

#if !defined (__IMCVFILT_H__)
#include "imcvfilt.h"
#endif


#if !defined (__MIUTATCH_H__)
#include <miutatch.h>
#endif

#if !defined(__BADESCA_H__)
#include <BADESCA.H>
#endif

#if !defined (__MENTACT_H__)
#include <mentact.h>
#endif

#if !defined(__MSVUIDS_H__)
#include <MSVUIDS.H>
#endif


class CRichText;
class CImHeader;
class TMimeSend;
class TImConvert;

class CImSendConvert : public CBase
	{
public:
	IMPORT_C static CImSendConvert* NewL( RFs &anFs,CMsvServerEntry* aServerEntry);
	IMPORT_C static CImSendConvert* NewLC( RFs &anFs,CMsvServerEntry* aServerEntry);

	IMPORT_C ~CImSendConvert( );

	// indicate which objects to use to define output
	IMPORT_C void InitialiseL(TMsvId aId, TImSendEncoding aAlgorithm,TTime& aTimeDate, const TDesC& aDomainName);

	IMPORT_C void SetMessageIdL();

	inline CImHeader* Header() const; 
	IMPORT_C TInt HeaderSize();
	inline TInt BodySize();
	IMPORT_C TInt NumberOfAttachments();
	IMPORT_C TInt AttachmentsSize();
	IMPORT_C TInt PadNextLineL( TDes8& rOutputLine, TInt& rPaddingCount );
private:
	CImSendConvert( RFs& anFs, CMsvServerEntry* aServerEntry);
	void ConstructL();


	inline TInt FromL( TDes8& rOutputLine );
	inline TInt ReplyToL( TDes8& rOutputLine );
	inline TInt ToL( TDes8& rOutputLine );
	inline TInt CcL( TDes8& rOutputLine );
	inline TInt BccL( TDes8& rOutputLine );
	inline TInt SubjectL( TDes8& rOutputLine );
	inline TInt MessageIdL( TDes8& rOutputLine );
	inline TInt XMailerL( TDes8& rOutputLine );
	inline TInt ReturnReceiptsL(TDes8& rOutputLine);
	
	inline TInt MimeHeaderNextLine( TDes8& rOutputLine, TInt& rPaddingCount );
	inline TInt BodyHeaderNextLine( TDes8& rOutputLine, TInt& rPaddingCount );
	inline TInt BodyNextLine( TDes8& rOutputLine, TInt& rPaddingCount );	
	inline TInt AttachmentNextLine( TDes8& rOutputLine, TInt& rPaddingCount );	
	
	inline TBool IsLineCRLF( TDesC8& aOutputLine );
	TInt DoRecipientsL( TDes8& rOutputLine, const TPtrC8& aPrompt, CDesCArray& aList );
	void PrepareBufferL( const TPtrC8& aPrompt, const TDesC& aData, TBool aEncode = EFalse );
	TInt SendOutput( TDes8& rOutputLine );
	TInt PriorityL(const TPtrC8& aPrompt, TDes8& rOutputLine);
	TMsvId ScanTextAndAttachmentsL();
	TBool ValidCharacter(TUint aCharacter) ;

	void SetHeader();
	void SetBodyL();
	void SetAlgorithmL( TImSendEncoding aAlgorithm );
	void Reset();
	TBool ValidEmailAddress(const TPtrC8& aData, const TInt Pos, TInt& rStart, TInt& rEnd);

private:
	enum TSendState		// Defines the state order of the CImSendConvert object... !! DO NOT CHANGE THE ORDER !!
		{
		EFrom=0,		// 0
		EReplyTo,
		ETo,
		ECc,
		EBcc,
		ESubject,
		EDate,
		EReturnReceiptTo,
		EPriority,
		EImportance,
		EMessageID,
		EXMailer,		
		EMimeHeader,	
		EMimeBodyHeader,
		EBody,			 
		EAttachments,
		EEndOfMessage	
		};

	TInt iState;
	CImHeader* iOutputHeader;
	CRichText* iSourceText;
	CParaFormatLayer* iParaLayer;
	CCharFormatLayer* iCharLayer;
	TImSendEncoding iAlgorithm;
	HBufC8* iOutputBuffer;
	HBufC* iDomainName;
	const TUint8* ipOutputBuffer;
	// we need to store the length of the CRichText contents because the CBufSeg doesn't know how big the data actually is.
	TInt iBodySize;
	TInt iTextPos;

	// resource strings
	HBufC* iProductName;

	CImConvertCharconv* iCharConv;
	CCnvCharacterSetConverter* iCharacterConverter;
	CImConvertHeader* iHeaderConverter;

	// a couple of helper objects 
	CImMimeSend* iMimeSend;
	CImBodyConversion* iBodyConversion;
	CImAttachmentConversion* iAttachmentConversion;

	RFs& iFs;
	CArrayFix<SAttachmentInfo>* iAttachmentList;
	
	TBool	iLogFileExists;
	RFile	iLogFile;
	TTime	iTimeDate;
	TImRfc822DateField iRfc822Date;
	TMsvPriority iPriority;
	CMsvServerEntry* iServerEntry;
	HBufC8* iCharset; // to convert to before sending
	};



class CImCalculateMsgSize : public CMsgActive
	{
public:
	IMPORT_C static CImCalculateMsgSize* NewL(RFs &anFs,CMsvServerEntry& aServerEntry);
	IMPORT_C ~CImCalculateMsgSize();
	IMPORT_C void StartL(TRequestStatus& aStatus,TMsvId aMsvId, 
			TImSendEncoding aAlgorithm, TTime& aTimeDate, 
			const TDesC& aDomainName);

	inline TInt MessageSize() const;	// total size of Email - once active object has completed
	inline void Progress(TInt& aBytes, TInt& aTotal);	// current info on size of Email

private:
	void DoRunL();
	void DoCancel();
private:
	CImCalculateMsgSize(RFs &anFs,CMsvServerEntry& aServerEntry);
	void ConstructL();
	void Reset();
private:
	RFs& iFs;
	CMsvServerEntry& iServerEntry;
	TMsvId iMsvId;

	TInt iSize;
	TInt iTotal;

	CImSendConvert* iSendConvert;
	TBuf8<KImMailMaxBufferSize> iMailString;
	TBool iFinished;
#if defined (_DEBUG)
	RFile iFile;
	TBool iFileOpen;
#endif
	};
#include "IMCVSEND.INL"

#endif