// AGSFMGR.H
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//


#include <e32base.h>
#include <s32file.h>


#if !defined(__AGSFMGR_H__)
#define __AGSFMGR_H__

#include <agmmodel.h>
#include "agsactiv.h"
#include "agnserv.h"

class CAgnServerActive;
class CAgnServer;

const TInt KAgnCompactionThresholdInBytes=10240;


class CAgnServFile : public CBase
	{
public:
	enum ELockStatus{EOk, ELocked, EClosed};
	enum EActiveStep{ENone, EBuildIndex, ECompact};

	CAgnServFile(RFs& aFs);
	static CAgnServFile* NewL(RFs& aFs);

	TFileName FileName();
	void OpenAgendaL(TFileName aFilename);
	TBool CloseAgenda();
	CFileStore* Store();
	void AddReference();
	CAgnEntryModel* Model();
	TStreamId DictionaryLookup(TUid aUid);
	void CloseFile();
	TBool IsLocked();
	void UnlockFile(TBool aCommit);
	void LockFile();
	TBool DoBuildIndexStepL();	
	TBool DoCompactorStepL();
	TBool DoStepL();			// called by active object
	TInt PercentageLoaded();
	TBool IsReadOnly();
	void StartCompactorL();
	void StopCompactorL();
	void AddStreamToDictionaryL(TUid aStreamUid, TStreamId aStreamId);
	TBool ModelIndexes();
	TBool UidIndexes();
	void SetServer(CAgnServer* aServer);
	static TInt CompactStep(TAny* aPtr);
	void CompactFile();
public:
	TStreamId iModelStreamId;
	enum {EProcessing, ESuccess, EFail}		iCompactionStatus;
	TInt	iActiveStep;		// active object step
private:
	void RestoreStreamLayoutL(CStreamStore& aStore, CStreamDictionary& aDictionary);
	void BuildIndexL(CAgnEntryModel* aModel);
	void PanicClient(TInt aError);
	TInt DoCompact();
public:
#ifdef LOGGING
	CAgnServer*	iAgnServer;
#endif
private:
	TFileName iFileName;
	CFileStore* iStore;
	TInt iRefCount;
	CAgnEntryModel* iModel;
	CAgnServer* iServer;
	CStreamDictionary* iDictionary; 
	TInt iLock;
	RFs& iFs;
	TBool iReadOnly;
	CAgnServerActive*	iActive;		// active object to build indexes
	CIdle*	iCompact;
	TInt iPercentageLoaded;				// % loaded, or 0 when complete
	TBool iHaveIndexes;
	//Compaction
	enum {ENothing, ECompacting, EReclaiming}	iCompactionStage;
	TInt				iNextCompactEffort;
	RStoreReclaim		iCompactor;
	};


/// CAgnServFileMgr

class CAgnServFileMgr : public CBase
	{
public:
	CAgnServFileMgr(RFs& aFs);
	~CAgnServFileMgr();
	void ConstructL();
	static CAgnServFileMgr* NewL(RFs& aFs);
	CAgnServFile* OpenAgendaL(TFileName aFilename);
	void CloseAgendaL(CAgnServFile* aStore);
public:
#ifdef LOGGING
	CAgnServer*	iAgnServer;
#endif
private:
	CArrayFixFlat<CAgnServFile*>* iFileList;
	RFs& iFs;
	};


#endif
