// MSVSTORE.H
//
// Copyright (c) 1998 Symbian Ltd.  All rights reserved.
//

#if !defined(__MSVSTORE_H__)
#define __MSVSTORE_H__

#if !defined(__E32BASE_H__)
#include <e32base.h>
#endif

#if !defined(__S32FILE_H__)
#include <s32file.h>
#endif

#if !defined(__MSVSTD_H__)
#include "msvstd.h"
#endif

//**********************************
// CMsvDictionaryStore
//**********************************
//
// The message server version of CDictionaryFileStore
//

class CMsvDictionaryStore : public CDictionaryStore
	{
public:
	// exported for test purposes only
	IMPORT_C static CMsvDictionaryStore* OpenL(RFs& aFs,const TDesC& aName,TUid aUid3);
	CStreamStore& StreamStore();
private:
	CMsvDictionaryStore();
	void ConstructL(RFs& aFs,const TDesC& aName,TUid aUid3);
	void CreateStoreL(RFile& aFile,const TUidType& aType);
	};


//**********************************
// MMsvStoreObserver
//**********************************
//
// 
//

class MMsvStoreObserver
	{
public:
	enum TMsvStoreEvent {	EMsvReadStoreClosed,	// read only CMsvStore closed
							EMsvEditStoreClosed,		// edit only CMsvStore closed
							EMsvFileStoreDeleted,	// actual filestore deleted
							EMsvFileStoreCommited};	// actual filestore commited
public:
	virtual void HandleStoreEvent(TMsvStoreEvent aEvent, TMsvId aId)=0;
	};

//**********************************
// CMsvCompactorWait
//**********************************
//
// 
//

class CMsvCompactorWait : public CActive
	{
public:
	CMsvCompactorWait();
	~CMsvCompactorWait();
	inline void Start();
protected:
	void DoCancel();
	void RunL();
	};

//**********************************
// CMsvStoreCompactor
//**********************************
//
// Compact the store
//

class CMsvStoreCompactor : public CActive
	{
public:
	CMsvStoreCompactor(CMsvDictionaryStore& aStore, TInt aFileSize, TRequestStatus& aCompletionStatus);
	~CMsvStoreCompactor();
	//
	void StartL();
	TInt Progress();
	//
private:
	//
	void DoRunL();
	// from CActive
	void RunL();
	void DoCancel();
	//
private:
	enum {EStart=0, EReclaim, ECompact, ECompleted} iState;
	CMsvDictionaryStore& iStore;
	TInt iTotalSteps;
	TPckgBuf<TInt> iStepsLeft;
	RStoreReclaim iReclaimer;
	TRequestStatus* iCompletionStatus;
	TInt iFileSize;
	};


//**********************************
// CMsvStore
//**********************************
//
// The entry structure that the client uses
//

// Uid which are not neeed to be known outside CMsvStore
const TUid KMsvEntryRichTextBody={0x10000F70};

class CMsvStore : public CBase
	{
public:
	IMPORT_C ~CMsvStore();
	//
	IMPORT_C void StoreBodyTextL(const CRichText& aRichTextBody);
	IMPORT_C void RestoreBodyTextL(CRichText& aRichTextBody);
	IMPORT_C void DeleteBodyTextL();
	inline TBool HasBodyTextL() const;
	//
	IMPORT_C void DeleteL(); // deletes the message store
	IMPORT_C TInt SizeL(); // the size of the store
	//
	// The following give the Dictionary store interface
	inline TBool IsNullL() const;
	inline TBool IsPresentL(TUid aUid) const;
	IMPORT_C void Remove(TUid aUid);
	IMPORT_C void RemoveL(TUid aUid);
	IMPORT_C void Revert();
	IMPORT_C void RevertL();
	IMPORT_C TInt Commit(); 
	IMPORT_C void CommitL();
	//
	// should only be called after a commit
	IMPORT_C void CompactL();
	IMPORT_C void CompactL(TRequestStatus& aStatus);
	IMPORT_C TInt CompactionProgress();
	//
protected:
	IMPORT_C CMsvStore(MMsvStoreObserver& aObserver, RFs& aFs, TMsvId aId);
	IMPORT_C static CMsvStore* OpenForReadL(MMsvStoreObserver& aObserver, RFs& aFs, const TDesC& aFileName, TMsvId aId);
	IMPORT_C static CMsvStore* OpenForWriteL(MMsvStoreObserver& aObserver, RFs& aFs, const TDesC& aFileName, TMsvId aId);
private:
	void ConstructL(const TDesC& aFileName);
	inline void Lock();
	inline const CMsvDictionaryStore& Store() const;
	inline CMsvDictionaryStore& Store();
	//
private:
	enum {EMsvStoreUnlocked, EMsvStoreLocked} iLockStatus;
	RFs& iFs;
	MMsvStoreObserver& iObserver;
	HBufC* iFileName;
	const TMsvId iId;
	CMsvDictionaryStore* iStore;
	TBool iConstructed;
	CMsvStoreCompactor* iCompactor;
	CMsvCompactorWait* iWait;
	//
friend class CMsvEntry;
friend class CMsvServerEntry;
friend class RMsvReadStream;
friend class RMsvWriteStream;
	};


//**********************************
// RMsvReadStream
//**********************************
//
// The message server version of RDictionaryReadStream
//

class RMsvReadStream : public RDictionaryReadStream
	{
public:
	IMPORT_C void OpenL(const CMsvStore& aMsvStore, TUid aUid);
	IMPORT_C void OpenLC(const CMsvStore& aMsvStore,TUid aUid);
	};


//**********************************
// RMsvReadStream
//**********************************
//
// The message server version of RDictionaryWriteStream
//

class RMsvWriteStream : public RDictionaryWriteStream
	{
public:
	RMsvWriteStream() {}
	inline RMsvWriteStream(const MExternalizer<TStreamRef>& anExter);
	IMPORT_C void AssignL(CMsvStore& aMsvStore, TUid aUid);
	IMPORT_C void AssignLC(CMsvStore& aMsvStore,TUid aUid);
	};


#include "msvstore.inl"

#endif