// EIKDOC.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <eikdoc.h>
#include <eikapp.h>
#include <s32file.h>
#include <s32std.h>
#include <eikappui.h>
#include <eikproc.h>
#include <eikscncl.h>
#include <apgwgnam.h>
#include <apaid.h>

EXPORT_C CEikDocument::CEikDocument(CEikApplication& aApp)
	:CApaDocument(aApp,*aApp.Process())
	{
	}

EXPORT_C CEikDocument::~CEikDocument()
	{
	}

EXPORT_C void CEikDocument::NewDocumentL()
	{
	}

EXPORT_C CFileStore* CEikDocument::CreateFileStoreLC(RFs& aFs,const TDesC& aFileName)
	{
	CFileStore* store=CDirectFileStore::ReplaceLC(aFs,aFileName,EFileShareExclusive);
	TUidType fileUid(KDirectFileStoreLayoutUid,KUidAppDllDoc,Application()->AppDllUid());
	store->SetTypeL(fileUid);
	CStreamDictionary* streamDic=CStreamDictionary::NewL();
	CleanupStack::PushL(streamDic);
	StoreL(*store,*streamDic); // now the store will be fully initialised
	Process()->WriteRootStreamL(*store,*streamDic,*Application());
	store->CommitL();
	CleanupStack::PopAndDestroy(); // streamDic
	return store;
	}

EXPORT_C void CEikDocument::PrintL(const CStreamStore& /*aStore*/)
	{
	}

EXPORT_C void CEikDocument::StoreL(CStreamStore& /*aStore*/,CStreamDictionary& /*aStreamDic*/) const
	{
	}

EXPORT_C void CEikDocument::ExternalizeL(RWriteStream& /*aStream*/) const
	{
	}

EXPORT_C void CEikDocument::RestoreL(const CStreamStore& /*aStore*/,const CStreamDictionary& /*aStreamDic*/)
	{
	}

EXPORT_C void CEikDocument::SaveL()
	{
	iEikProcess->SaveToDirectFileStoreL(this);
	iChanged=EFalse;
	}

EXPORT_C TBool CEikDocument::IsEmpty() const
	{
	return(ETrue);
	}

EXPORT_C TBool CEikDocument::HasChanged() const
	{
	return(iChanged);
	}

EXPORT_C void CEikDocument::EditL(MApaEmbeddedDocObserver* aObserver,TBool aReadOnly)
	{
	TApaAppCapabilityBuf buf;
	Application()->Capability(buf);
	if (aObserver && buf().iEmbeddability==TApaAppCapability::ENotEmbeddable)
		User::Leave(KErrNotSupported);
	CEikScreenClearerWin::NewLC();
	TRAPD(err,PrepareToEditL(aObserver,aReadOnly));
	if (err)
		{
		delete iAppUi;
		iAppUi=NULL;
		User::Leave(err);
		}
	iAppUi->HandleModelChangeL();
	CleanupStack::PopAndDestroy(); // clearer window
	}

EXPORT_C void CEikDocument::SetEditStoreL(CStreamStore* aStore)
	{
	iEditStore=aStore;
	iAppUi->HandleModelChangeL();
	}

EXPORT_C void CEikDocument::PrepareToEditL(MApaEmbeddedDocObserver* aObserver,TBool aReadOnly)
	{
	iAppUi=CreateAppUiL();
	iAppUi->SetDocument(this);
	iAppUi->SetEmbeddedDocInfo(aObserver,aReadOnly);
	iAppUi->ConstructL();
	}

EXPORT_C void CEikDocument::PrepareToEditL()
	{
	PrepareToEditL(NULL,NULL);
	}

void CEikDocument::NullAppUi()
	{
	iAppUi=NULL;
	}

EXPORT_C void CEikDocument::SetChanged(TBool aHasChanged)
	{
	iChanged=aHasChanged;
	}

EXPORT_C const TApaAppCaption& CEikDocument::AppCaption() const
	{
	return(iEikApplication->AppCaption());
	}

EXPORT_C void CEikDocument::SetAppFileMode(TUint aMode)
	{
	iAppFileMode=aMode;
	}

EXPORT_C TUint CEikDocument::AppFileMode() const
	{
    return iAppFileMode;
	}

EXPORT_C void CEikDocument::UpdateTaskNameL(CApaWindowGroupName* aWgName)
	//
	// default window group name
	//
	{
    const TPtrC docName=iEikProcess->MainDocFileName();
	if (docName.Length())
		{
		TRAPD(ignored,iEikProcess->SetLastUsedFileL(CEikProcess::ELastOpenFile,docName));
		}
	const TPtrC name=AppCaption();
	aWgName->SetCaptionL(name);
	aWgName->SetAppUid(Application()->AppDllUid());
	aWgName->SetDocNameL(docName);
	}

EXPORT_C CFileStore* CEikDocument::OpenFileL(TBool aDoOpen,const TDesC& aFilename,RFs& aFs)
	{
	CFileStore* docStore=NULL;
	TUint appFileMode=EFileShareExclusive|EFileWrite;
	if (aDoOpen)
		{
		CStreamDictionary* streamDic=NULL;
		TRAPD(err,
		streamDic=CApaProcess::ReadRootStreamLC(aFs,docStore,aFilename,appFileMode);
		CleanupStack::Pop(););
		if (err==KErrNone)
			CleanupStack::PushL(streamDic);
		else
			{
			appFileMode=EFileRead|EFileShareReadersOnly;
			if (err==KErrAccessDenied || err==KErrInUse)
				streamDic=CApaProcess::ReadRootStreamLC(aFs,docStore,aFilename,appFileMode);
			else
				User::Leave(err);
			}
		CleanupStack::PushL(docStore);
		RestoreL(*docStore,*streamDic);
		CleanupStack::Pop();// docStore
		CleanupStack::PopAndDestroy(); // streamDic
		}
	else
		{
		NewDocumentL();
		aFs.MkDirAll(aFilename);
		docStore=CreateFileStoreLC(aFs,aFilename);
		docStore->CommitL();
		CleanupStack::Pop();  // docStore
		}
	iAppFileMode=appFileMode;
	return docStore;
	}

EXPORT_C void CEikDocument::Reserved_1()
	{}

EXPORT_C void CEikDocument::Reserved_2()
	{}
