// SMUTHDR.H
//
// Copyright (c) 1998 Symbian Ltd.  All rights reserved.
//



#if !defined (__SMUTHDR_H_)
#define __SMUTHDR_H_
#if !defined(__E32BASE_H__)
#include <e32base.h>
#endif
#if !defined(__F32FILE_H__)
#include <f32file.h>
#endif
class RReadStream;  //  Review, work around
class RWriteStream;
#if !defined(__MSVRCPT_H__)
#include <msvrcpt.h>
#endif
#if !defined(__SMUTMSG_H__)
#include "smutmsg.h"
#endif
#if !defined(__ETELBGSM_H__)
#include <etelbgsm.h>
#endif
#if !defined(__SMUTSET_H__)
#include "smutset.h"
#endif



class CPlainText;
class RMsvReadStream;
class RMsvWriteStream;
class CMsvStore;
class TMsvEntry;
class CSmsUserData;
class CSmsAddress;
class CGsmCompressionBase;
class CCnvCharacterSetConverter;


const TUid KUidMsgTypeSMS = {0x1000102C};
const TUid KUidMsvSMSHeaderStream={0x10001834};
const TUid KUidMsvSMSPDUHeaderStream={10001836};

const TInt KSmsDescriptionLength=32;


class CSmsRecipient : public CMsvRecipient
	{
public:
	IMPORT_C static CSmsRecipient* NewL();
	IMPORT_C ~CSmsRecipient();

	IMPORT_C void InternalizeL( RMsvReadStream& aStream);
	IMPORT_C void ExternalizeL( RMsvWriteStream& aStream) const;

	inline TPtrC Address() const;
	IMPORT_C void SetAddressL(const TDesC& aAddress);

	inline TPtrC Name() const;
	IMPORT_C void SetNameL(const TDesC& aName);

	IMPORT_C void SetNumMessagePDUsL(TInt aNum);
	IMPORT_C TInt NumMessagePDUs() const;
	IMPORT_C void AddSentMessagePDUReference(TUint aMsgRef);
	IMPORT_C TInt NumSentMessagePDUs() const;
	IMPORT_C TUint SentMessagePDUReference(TInt aIndex) const;

private:
	CSmsRecipient();
	void ConstructL();
	void SetBufferL(HBufC*& aBuffer,const TDesC& aString);

private:
	HBufC* iNumber;
	HBufC* iName;
	HBufC8* iSentMessagePDUReferenceArray;
	};

class CSmsHeader : public CBase
	{
public:
	IMPORT_C static CSmsHeader* NewL(const TSms& aSms,CPlainText& aText,TBool aServiceCenterAddressPresent=EFalse);
	IMPORT_C static CSmsHeader* NewL(CSmsMessage::TSmsMsgType aType,CPlainText& aText);
	IMPORT_C ~CSmsHeader();

	inline CSmsMessage::TSmsMsgType Type() const;

	IMPORT_C TPtrC ServiceCenterAddress() const;
	IMPORT_C void SetServiceCenterAddressL(const TDesC& aAddress);
	IMPORT_C void ParsedServiceCenterAddress(TGsmTelNumber& aParsedAddress) const;
	IMPORT_C void SetParsedServiceCenterAddressL(const TGsmTelNumber& aParsedAddress);

	inline void SetValidityPeriod(const TTimeIntervalMinutes& aTimeIntervalMinutes);
	inline const TTimeIntervalMinutes& ValidityPeriod() const;

	inline void SetTime(const TTime& aTime);
	inline const TTime& Time() const;

	inline TBool IsComplete() const;
	inline TBool IsDecoded() const;

	IMPORT_C void SetMessageSettingsL(const TSmsMessageSettings& aMessageSettings);
	IMPORT_C void MessageSettings(TSmsMessageSettings& aMessageSettings) const;

	IMPORT_C TInt MessageReference() const;
	IMPORT_C TInt NumMessagesL();

	inline const CArrayPtrFlat<CSmsRecipient>& Recipients() const;
	inline CArrayPtrFlat<CSmsRecipient>& Recipients();

	inline void SetFromAddressL(const TDesC& aAddress);
	inline TPtrC FromAddress() const;

	IMPORT_C TInt MaxTextLength() const; 

	IMPORT_C void EncodeMessagePDUsL(TInt aIndex,CArrayFix<TSms>& aSmsArray,TInt aReference=0,TBool aServiceCenterAddressPresent=ETrue);
	IMPORT_C void DecodeMessagePDUsL(const CArrayFix<TSms>& aSmsArray,TBool aServiceCenterAddressPresent=ETrue);

	IMPORT_C void InternalizeL( RMsvReadStream& aStream);
	IMPORT_C void ExternalizeL( RMsvWriteStream& aStream) const;

	IMPORT_C void RestoreL(CMsvStore& aStore);
	IMPORT_C void StoreL(CMsvStore& aStore) const;
	
	IMPORT_C void PopulateMsgEntryL(TMsvEntry& aMsvEntry);

	IMPORT_C static TBool CompressionSupported(RFs& aFs);

private:
	CSmsHeader(CPlainText& aText);
	void ConstructL(const TSms& aSms,TBool aServiceCenterAddressPresent);
	void ConstructL(CSmsMessage::TSmsMsgType aType);
	void ConstructConverterL();
	void ConstructL();
	void CreateRecipientFromSmsSubmitL();
	void CreateGsmCompressionL();
	void DoCreateGsmCompressionL();
	static TInt FindGsmCompressionDll(RFs& aFs,TDes& aFullName);
	CBufSeg* CompressTextL();
	void ConvertCharacters(TDes& aDes,TInt aFrom,TInt aTo) const;

private:
	CArrayPtrFlat<CSmsRecipient> iRecipients;
	CSmsMessage* iSmsMessage;
	CPlainText& iText;
	TBool iIsComplete;
	TBool iIsDecoded;
	TBuf<KSmsDescriptionLength> iDescription;    
	RFs iFs;
	RLibrary iGsmLibrary;
	CCnvCharacterSetConverter* iCharacterSetConverter;
	CGsmCompressionBase* iGsmCompression;   
	};

class TSmsPDUHeader
	{
public:
	IMPORT_C TSmsPDUHeader();

	IMPORT_C void InternalizeL( RMsvReadStream& aStream);
	IMPORT_C void ExternalizeL( RMsvWriteStream& aStream) const;

	IMPORT_C void RestoreL(CMsvStore& aStore);
	IMPORT_C void StoreL(CMsvStore& aStore) const;
	
	IMPORT_C void PopulateMsgEntry(TMsvEntry& aMsvEntry);
	IMPORT_C TInt Crc() const;

	inline TInt Error() const;
	inline void SetError(TInt aError);

	inline const RSmsMessaging::TMsgLocation& Location() const;
	inline void SetLocation(const RSmsMessaging::TMsgLocation& aLocation);

	inline const RSmsStorage::TSmsMsgStoreEntry& StoreEntry() const;
	inline RSmsStorage::TSmsMsgStoreEntry& StoreEntry();

private:
	RSmsMessaging::TMsgLocation iLocation;
	TInt iError;
	RSmsStorage::TSmsMsgStoreEntry iStoreEntry;
	TBuf<KSmsDescriptionLength> iDescription; 
	TBuf<KSmsDescriptionLength> iDetails;    
	};

#include "smuthdr.inl"

#endif // !defined __SMUTHDR_H_

