// APGDOOR.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <s32stor.h>
#include <fbs.h>

#include "apgdoor.h"
#include "apgicnfl.h"
#include "apgcli.h"
#include "apacln.h"
#include "apgstd.h"
#include "apgpriv.h"
#include "apfdef.h"

#include "..\apparc\trace.h"

const TUid KUidApaDoorStateStream={0x1000012a}; // build invariant
const TInt KHugeGranularity=4096; // 4k granularity for the door's host buffer
// default icon size only used if door was created by a model door, set to glass, but does not support glass
#define KDefaultIconSizeInTwips TSize(500,500)

///////////////////////////////////////////
// CApaDoor
///////////////////////////////////////////

EXPORT_C CApaDoor* CApaDoor::NewLC(CApaDocument& aDoc,const TSize& aDefaultIconSizeInTwips)
// Creates a door of given size around aDoc
// takes ownership of aDoc
//
	{
	CApaDoor* self=new CApaDoor(aDoc,aDefaultIconSizeInTwips);
	if (!self)
		{
		aDoc.Process()->DestroyDocument(&aDoc);
		User::LeaveNoMemory();
		}
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}


EXPORT_C CApaDoor* CApaDoor::NewL(CApaDocument& aDoc,const TSize& aDefaultIconSizeInTwips)
// Creates a door of given size around aDoc
// takes ownership of aDoc
//
	{
	CApaDoor* self = CApaDoor::NewLC(aDoc,aDefaultIconSizeInTwips);
	CleanupStack::Pop();
	return self;
	}


EXPORT_C CApaDoor* CApaDoor::NewL(const CStreamStore& aStore,TStreamId aStreamId,CApaProcess& aProcess)
// Creates a door and restores it
// Should only be called by the TApaPictureFactory
//
	{
	CApaDoor* self=new(ELeave) CApaDoor(aProcess);
	CleanupStack::PushL(self);
	self->RestoreL(aStore,aStreamId);
	CleanupStack::Pop();
	return self;
	}


CApaDoor::CApaDoor(CApaDocument& aDoc,const TSize& aDefaultIconSizeInTwips)
	: iApaProcess(aDoc.Process()),
	iApaDoc(&aDoc),
	iIconSizeInTwips(aDefaultIconSizeInTwips)
	{__DECLARE_NAME(_S("CApaDoor"));}


CApaDoor::CApaDoor(CApaProcess& aProcess)
	: iApaProcess(&aProcess)
	{__DECLARE_NAME(_S("CApaDoor"));}


EXPORT_C CApaDoor::~CApaDoor()
	{
	delete iPicture;
	if (iApaDoc)
		iApaProcess->DestroyDocument(iApaDoc); // removes it from the process and deletes it
	delete iAppCaption;
	delete iStore;
	delete iStoreHost;
	}


void CApaDoor::ConstructL()
	{
	__SHOW_TRACE(_L("Starting CApaDoor::ConstructL"));
	__APA_PROFILE_START(3);
	__ASSERT_ALWAYS(iApaDoc,Panic(EPanicNoDocumentOnConstruction));
	//
	// check that the doc supports embedding
	TApaAppCapabilityBuf buf;
	iApaDoc->Application()->Capability(buf);
	if (buf().iEmbeddability==TApaAppCapability::ENotEmbeddable)
		User::Leave(KErrNotSupported);
	//
	__SHOW_TRACE(_L("...doc is embeddable"));
	//
	// set up the icon
	SetFormatToIconL();
	//
	__PROFILE_END(3);
	}


TStreamId CApaDoor::StoreL(CStreamStore& aTargetStore)const
// Encapsulates the storing of its components.
//
	{
	__SHOW_TRACE(_L("Starting CApaDoor::StoreL"));
	__APA_PROFILE_START(4);
	// create stream dictionary
	CStreamDictionary* streamDic = CStreamDictionary::NewLC();
	//
	// stream out door's state
	ExternalizeBaseStreamL(aTargetStore,*streamDic);
	ExternalizeStateStreamL(aTargetStore,*streamDic);
	//
	// store the doc if it exists, otherwise copy the persistent data directly
	TStreamId id;
	RStoreWriteStream stream;
	if (iApaDoc)
		{
		// create an embedded store in a new write stream
		id = stream.CreateL(aTargetStore);
		CEmbeddedStore* target=CEmbeddedStore::NewLC(stream); // takes ownership of stream
		streamDic->AssignL(KUidApaDoorDocStream,id);
		StoreDocL(*target);
		// close the new embedded store
		target->CommitL();
		CleanupStack::PopAndDestroy(); // target
		}
	else if (iStore)
		{
		RStoreWriteStream trg;
		id = trg.CreateLC(aTargetStore);
		CopyStoreL(*iStore,trg);
		CleanupStack::PopAndDestroy(); // trg
		streamDic->AssignL(KUidApaDoorDocStream,id);
		}
	else 
		Panic(EPanicNoDocOrStoreWhenStoring); // impossible situation
	//
	// store the stream dictionary and return its stream id
	id = stream.CreateLC(aTargetStore);
	stream<< *streamDic;
	stream.CommitL();
	CleanupStack::PopAndDestroy(); // stream
	//
	// tidy up
	CleanupStack::PopAndDestroy(); // streamDic
	__PROFILE_END(4);
	return id;
	}


void CApaDoor::CopyStoreL(const CEmbeddedStore& aSourceStore,RWriteStream& aTargetStream)
// static method
// copies an embedded store containing a doc to aTargetStream
//
	{
	__SHOW_TRACE(_L("Starting CApaDoor::CopyStoreL"));
	// read the contents of aSourceStore's rootstream (so I can write it out in a mo')
	CStreamDictionary* root=ReadStreamDictionaryLC(aSourceStore,aSourceStore.Root());
	//
	// copy the source store directly
	MStreamBuf* host=aSourceStore.Host();
	TStreamPos pos=aSourceStore.Position(aSourceStore.Root());
	host->SeekL(host->ERead,EStreamBeginning);
	RReadStream stream(host);
	aTargetStream.WriteL(stream,pos.Offset());
	//
	// write the root stream
	aTargetStream<< *root;
	aTargetStream.CommitL();
	CleanupStack::PopAndDestroy(); // root
	}


EXPORT_C void CApaDoor::RestoreL(const CStreamStore& aSourceStore,TStreamId aStreamId)
	{
	__SHOW_TRACE(_L("Starting CApaDoor::RestoreL"));
	__APA_PROFILE_START(5);
	__ASSERT_DEBUG(iApaProcess,Panic(EDPanicNoProcess));
	//
	if (iApaDoc)
		{
		iApaProcess->DestroyDocument(iApaDoc);
		iApaDoc = NULL;
		}
	delete iStore;
	delete iStoreHost;
	iStore=NULL;
	iStoreHost = NULL;
	//
	// internalize the streamDic from the headstream
	CStreamDictionary* streamDic=ReadStreamDictionaryLC(aSourceStore,aStreamId);
	//
	// internalize the door's state
	__APA_PROFILE_START(13);
	TSize currentSize=InternalizeBaseStreamL(aSourceStore,*streamDic);
	InternalizeStateStreamL(aSourceStore,*streamDic,currentSize);
	__APA_PROFILE_END(13);
	//
	// internalize the embedded store
	__APA_PROFILE_START(14);
	RStoreReadStream src;
	src.OpenL(aSourceStore,streamDic->At(KUidApaDoorDocStream));
	iStore = CEmbeddedStore::FromL(src);
	CleanupStack::PopAndDestroy(); // streamDic
	streamDic = NULL;
	__APA_PROFILE_END(14);
	//
	// internalize the doc's stream dict
	streamDic = ReadStreamDictionaryLC(*iStore,iStore->Root());
	//
	// set the door's format 
	if (iFormat==EIconic || (streamDic->At(KUidSecurityStream)!=KNullStreamId))
		// iconify automatically if a password is required for access
		SetFormatToIconL();
	else
		{
		TRAPD(ret, SetFormatToGlassL() );
		if (ret==KErrNone)
			iPicture->SetSizeInTwips(currentSize);
		else if (ret!=KErrNoMemory)
			// problem loading app/doc - just iconify it for now...
			SetFormatToIconL();
		else
			User::Leave(ret);
		}	
	CleanupStack::PopAndDestroy(); // streamDic
	__PROFILE_END(5);
	}


void CApaDoor::StoreDocL(CPersistentStore& aTargetStore)const
// stores the doc if it's in memory, otherwise panics!
// aStore should be protected before calling this method
//
	{
	__SHOW_TRACE(_L("Starting CApaDoor::StoreDocL"));
	__ASSERT_ALWAYS(iApaDoc,Panic(EPanicNoDocumentOnStore)); // the doc must be in memory to be stored
	//
	// create a stream dic
	CStreamDictionary* streamDic=CStreamDictionary::NewLC();
	// store the doc
	iApaDoc->StoreL(aTargetStore,*streamDic);
	// write store's root stream
	CApaProcess::WriteRootStreamL(aTargetStore,*streamDic,*iApaDoc->Application());
	// tidy up
	CleanupStack::PopAndDestroy(); // streamDic
	}


void CApaDoor::RestoreDocL(const CPersistentStore& aSourceStore)
// restores the document from the embedded store
// leaves with KErrNotFound if the app dll cant be located
//
	{
	__SHOW_TRACE(_L("Starting CApaDoor::RestoreDocL"));
	__ASSERT_ALWAYS(!iApaDoc,Panic(EPanicDocAlreadyExists));
	//
	// read the stream dic from the doc's root stream
	CStreamDictionary* streamDic=ReadStreamDictionaryLC(aSourceStore,aSourceStore.Root());
	//
	// read the app id from the store
	TApaAppIdentifier appId = CApaProcess::ReadAppIdentifierL(aSourceStore,*streamDic);
	//
	// if the app exists find it, load it and create a doc, else leave if the correct app cannot be found
	CApaDocument* doc = iApaProcess->AddNewDocumentL(appId.iFullName,appId.iAppUid); // creates an unrestored doc and adds it to the process list
	TApaDocCleanupItem cleanup(iApaProcess,doc);
	CleanupStack::PushL(cleanup);
	doc->RestoreL(aSourceStore,*streamDic); // restores the doc
	iApaDoc = doc;
	CleanupStack::Pop(); // doc
	CleanupStack::PopAndDestroy(); // streamDic
	}


CStreamDictionary* CApaDoor::ReadStreamDictionaryLC(const CStreamStore& aSourceStore,TStreamId aStreamId)
// static method
//
	{
	__APA_PROFILE_START(12);
	// read the stream dic from the doc's root stream
	CStreamDictionary* streamDic=CStreamDictionary::NewLC();
	RStoreReadStream stream;
	stream.OpenLC(aSourceStore,aStreamId);
	stream>> *streamDic;
	CleanupStack::PopAndDestroy(); // root
	__APA_PROFILE_END(12);
	return streamDic;
	}


void CApaDoor::ExternalizeL(RWriteStream& /*aStream*/)const
	{
	Panic(EPanicMethodNotSupported);
	}


void CApaDoor::ExternalizeStateStreamL(CStreamStore& aStore,CStreamDictionary& aStreamDict)const
	{
	__SHOW_TRACE(_L("Starting CApaDoor::ExternalizeStateStreamL"));
	__ASSERT_ALWAYS(iAppCaption,Panic(EPanicNoCaption));
	__ASSERT_ALWAYS(iPicture,Panic(EPanicNoPictureInDoor));
	RStoreWriteStream stream;
	TStreamId id=stream.CreateLC(aStore);
	//
	stream<< *iAppCaption;
	TSize size;
	if (iFormat==EIconic || iFormat==ETemporarilyIconic)
		GetSizeInTwips(size);
	else 
		size = iIconSizeInTwips;
	stream<< size;
	//
	stream.CommitL();
	CleanupStack::PopAndDestroy(); // stream
	aStreamDict.AssignL(KUidApaDoorStateStream,id);
	}


void CApaDoor::InternalizeStateStreamL(const CStreamStore& aStore,const CStreamDictionary& aStreamDict,TSize aDefaultIconSize)
	{
	__SHOW_TRACE(_L("Starting CApaDoor::InternalizeStateStreamL"));
	TStreamId id=aStreamDict.At(KUidApaDoorStateStream);
	if (id!=KNullStreamId)
		{
		RStoreReadStream stream;
		stream.OpenLC(aStore,id);
		TApaAppCaption caption;
		stream>> caption;
		delete iAppCaption;
		iAppCaption = NULL;
		iAppCaption = caption.AllocL();
		stream>> iIconSizeInTwips;
		CleanupStack::PopAndDestroy(); // stream
		}
	else
		{// use default settings
		delete iAppCaption;
		iAppCaption = NULL;
		iAppCaption = HBufC::NewL(0);
		if (iFormat==EIconic)
			iIconSizeInTwips = aDefaultIconSize;
		else
			iIconSizeInTwips = KDefaultIconSizeInTwips;
		}
	}


void CApaDoor::DetachFromStoreL(TDetach aDegree)
	{
	__SHOW_TRACE(_L("Starting CApaDoor::DetachFromStoreL"));
	if (iApaDoc)
		{
		iApaDoc->DetachFromStoreL(aDegree);
		if (!iStoreHost)
			{
			delete iStore;
			iStore = NULL;
			}
		}
	else if (!iStoreHost)
		{
		if (aDegree==EDetachDraw)
			{
			delete iStore;
			iStore = NULL;
			// now all I can do is draw as I am, any attempt to change me will result in a panic
			}
		else
			{
			__ASSERT_ALWAYS(iStore,Panic(EPanicNoStoreOnDetach));
			// instantiate the mem buffer, and a stream to write to it
			CBufSeg* bufSeg = CBufSeg::NewL(KHugeGranularity);
			CleanupStack::PushL(bufSeg);
			HBufBuf* buf=HBufBuf::NewL(*bufSeg,0);
			RWriteStream writeStream(buf);
			writeStream.PushL();
			// write the store to the mem buffer
			CopyStoreL(*iStore,writeStream);
			CleanupStack::Pop(2); // bufSeg,writeStream
			//
			// set iStoreHost as host for the embedded store
			MStreamBuf* host=iStore->Host();
			__ASSERT_DEBUG(host!=NULL,Panic(EDPanicStoreDoesntHaveAHost));
			iStore->Detach();
			host->Release();
			iStore->Reattach(buf);
			iStoreHost = bufSeg;
			}
		}
	}

	
EXPORT_C CApaDocument* CApaDoor::DocumentL(TBool aCheckPassword)
// returns a pointer to the doc, restoring it if necessary
// leaves with KErrNotFound if the doc needs to be restored but the app dll cannot be found
//
	{
	__SHOW_TRACE(_L("Starting CApaDoor::DocumentL"));
	//
	if (!iApaDoc)
		{
		__ASSERT_ALWAYS(iStore,Panic(EPanicNoStoreOnRestore));
		RestoreDocL(*iStore);
		}
	else if (aCheckPassword)
		iApaDoc->ValidatePasswordL();
	//
	return iApaDoc;
	}


EXPORT_C void CApaDoor::SetFormatToTemporaryIconL(TBool aEnabled)
// if the door is currently iconic do nothing
// if the door is glass switch it's format to iconic, but ensure that when externalized the format will be persisted as glass
//
	{
	__SHOW_TRACE(_L("Starting CApaDoor::SetFormatToTemporaryIconL"));
	if (aEnabled && iFormat==EGlassDoor) 
		{
		TSize glassSize;
		GetSizeInTwips(glassSize);
		SetFormatToIconL();
		iFormat = ETemporarilyIconic;
		iIconSizeInTwips = glassSize; //abuse it!
		}
	else if (!aEnabled && iFormat==ETemporarilyIconic)
		SetFormatToGlassL();
	}


EXPORT_C void CApaDoor::SetFormatToIconL()
// if the app's icon file cannot be located the default icon file is used instead
// the method only leaves if construction of the default icon fails
//
	{
	__SHOW_TRACE(_L("Starting CApaDoor::SetFormatToIconL"));
	__APA_PROFILE_START(6);
	if (iFormat==ETemporarilyIconic && iPicture)
		{
		GetSizeInTwips(iIconSizeInTwips);
		iFormat = EIconic;
		}
	else if (iFormat!=EIconic || !iPicture)
		{
		TUid appUid;
		TParse parser;
		TInt ret=KErrNone;
		if (iApaDoc)
			{
			appUid = iApaDoc->Application()->AppDllUid();
			TFileName fullName=iApaDoc->Application()->AppFullName();
			User::LeaveIfError(parser.Set(KAppInfoFileExtension,&fullName,NULL) );
			}
		else
			{
			__ASSERT_DEBUG(iStore,Panic(EDPanicNoStoreOnIconify));
			__APA_PROFILE_START(15);
			// get the app id
			CStreamDictionary* streamDic=ReadStreamDictionaryLC(*iStore,iStore->Root());
			TApaAppIdentifier appId=CApaProcess::ReadAppIdentifierL(*iStore,*streamDic);
			CleanupStack::PopAndDestroy(); // streamDic
			appUid = appId.iAppUid;
			__APA_PROFILE_END(15);
			//
			// find the AIF
			__APA_PROFILE_START(8);
			RApaLsSession ls;
			TInt ret=ls.Connect();
			if (ret==KErrNone)
				{
				TApaAppInfo info;
				ret=ls.GetAppInfo(info,appUid);
				if (ret==KErrNone)
					ret=parser.Set(KAppInfoFileExtension,&info.iFullName,NULL);
				}
			else
				{
				TFileName fullName=iApaProcess->AppFinder()->FindAppL(parser.FullName(),appUid);
				TRAP(ret,parser.Set(KAppInfoFileExtension,&fullName,NULL));
				}
			ls.Close();
			__APA_PROFILE_END(8);
			}
		//
		CApaAppInfoFileReader* aif=NULL;
		TRAP(ret,aif = CApaAppInfoFileReader::NewL(parser.FullName(),appUid));
		CPicture* icon=NULL;
		if (ret==KErrNone)
			{
			__APA_PROFILE_START(9);
			CleanupStack::PushL(aif);
			//
			// get the app name (in current system language) for future reference, if we dont have it already
			if (!iAppCaption)
				iAppCaption = aif->CaptionL(User::Language()).AllocL();
			//
			// create a new icon
			TRAP(ret, icon=CApaIconPicture::NewL(iIconSizeInTwips,*aif,parser.FullName(),appUid) );
			CleanupStack::PopAndDestroy(); // aif
			__APA_PROFILE_END(9);
			}
		else
			{
			__APA_PROFILE_START(11);
			TRAP(ret, icon=CApaIconPicture::NewL(iIconSizeInTwips,parser.FullName(),appUid) );
			__APA_PROFILE_END(11);
			}
		if (!iAppCaption)
			iAppCaption = HBufC::NewL(0);
		//
		if (ret!=KErrNone)
			icon = GetDefaultIconL(iIconSizeInTwips);
		delete iPicture;
		iPicture = icon;
		iFormat = EIconic;
		}
	__PROFILE_END(6);
	}


EXPORT_C void CApaDoor::SetFormatToGlassL()
// leaves with KErrNotSupported and rolls back the format if the new format is not supported
// leaves with KErrNotFound if the app dll cannot be located
//
	{
	__SHOW_TRACE(_L("Starting CApaDoor::SetFormatToGlassL"));
	__APA_PROFILE_START(7);
	if (iFormat!=EGlassDoor || !iPicture)
		{
		if (!iApaDoc)
			{
			__ASSERT_DEBUG(iStore,Panic(EDPanicNoStoreOnGlassing));
			RestoreDocL(*iStore);
			}
		if (iApaDoc->Capability().CanDrawGlass())
			{
			CPicture* glass = iApaDoc->GlassPictureL();
			__ASSERT_ALWAYS(glass,Panic(EPanicNoGlassPicture));
			if (iPicture)
				iPicture->GetSizeInTwips(iIconSizeInTwips); // store the current icon size
			delete iPicture;
			iPicture = glass;
			iFormat = EGlassDoor;
			}
		else
			User::Leave(KErrNotSupported); // glass pic's not supported
		}
	__PROFILE_END(7);
	}


CPicture* CApaDoor::GetDefaultIconL(const TSize& aIconSizeInTwips)
// returns KErrNotSupported and leaves the format unchanged if the new format is not supported
//
	{
	__SHOW_TRACE(_L("Starting CApaDoor::GetDefaultIconL"));
	__APA_PROFILE_START(10);
	// test for the existence of the default icon file
	TEntry entry;
	TPtrC defaultIconFileName=iApaProcess->AppFinder()->DefaultAppInfoFileName();
	if (iApaProcess->FsSession().Entry(defaultIconFileName,entry)!=KErrNone)
		Panic(EPanicDefaultIcnFileDoesNotExist);
	// create a new icon
	CPicture* icon=CApaIconPicture::NewL(aIconSizeInTwips,defaultIconFileName);
	__APA_PROFILE_END(10);
	return icon;
	}


EXPORT_C TUid CApaDoor::AppUidL()const
	{
	if (iApaDoc)
		return iApaDoc->Application()->AppDllUid();
	//
	__ASSERT_ALWAYS(iStore,Panic(EPanicNoStoreOnAppUid));
	//
	// read uid from store's headstream
	CStreamDictionary* streamDic = ReadStreamDictionaryLC(*iStore,iStore->Root());
	TApaAppIdentifier appId = CApaProcess::ReadAppIdentifierL(*iStore,*streamDic);
	CleanupStack::PopAndDestroy(); // streamDic
	return appId.iAppUid;
	}
	

void CApaDoor::Draw(CGraphicsContext& aGc,const TPoint& aTopLeft,const TRect& aClipRect,
						MGraphicsDeviceMap* aMap)const
// depending upon format, draws either an icon or a glass door
//
	{
	__ASSERT_DEBUG(iPicture,Panic(EDPanicNoPictureOnDrawing));
	//
	iPicture->Draw(aGc,aTopLeft,aClipRect,aMap);
	}


void CApaDoor::GetOriginalSizeInTwips(TSize& aSize)const
	{
	iPicture->GetOriginalSizeInTwips(aSize);
	}


void CApaDoor::SetScaleFactor(TInt aScaleFactorWidth,TInt aScaleFactorHeight)
	{
	iPicture->SetScaleFactor(aScaleFactorWidth,aScaleFactorHeight);
	}


TInt CApaDoor::ScaleFactorWidth()const
	{
	return iPicture->ScaleFactorWidth();
	}


TInt CApaDoor::ScaleFactorHeight()const
	{
	return iPicture->ScaleFactorHeight();
	}


void CApaDoor::SetCropInTwips(const TMargins& aMargins)
	{
	iPicture->SetCropInTwips(aMargins);
	}


void CApaDoor::GetCropInTwips(TMargins& aMargins)const
	{
	iPicture->GetCropInTwips(aMargins);
	}


TPictureCapability CApaDoor::Capability()const
	{
	return iPicture->Capability();
	}

TSize CApaDoor::GlassDoorSize()const
	{
	TSize size;
	if (iFormat==EGlassDoor)
		GetSizeInTwips(size);
	else if (iFormat==ETemporarilyIconic)
		{
		if (!iApaDoc)
			size = iIconSizeInTwips;
		else
			{
			// there's a doc, so get a glass door from it just in case it's size has changed since I changed format
			CPicture* glass = iApaDoc->GlassPictureL();
			__ASSERT_ALWAYS(glass,Panic(EPanicNoGlassPicture));
			glass->GetSizeInTwips(size);
			delete glass;
			}
		}
	else
		Panic(EIllegalCallToGlassDoorSize);
	return size;
	}


void CApaDoor::SetIconSizeInTwips(TSize aSize)
// for use of factory
	{
	if (iFormat==EGlassDoor)
		iIconSizeInTwips = aSize;
	else
		SetSizeInTwips(aSize);
	}


///////////////////////////////////
// TApaPictureFactory
///////////////////////////////////

#define KDoNotApplyIconSize TSize(-1,-1)

EXPORT_C TApaPictureFactory::TApaPictureFactory(CApaProcess* aAppProcess)
	:iApaProcess(aAppProcess),
	iIconSize(KDoNotApplyIconSize)
	{}


EXPORT_C void TApaPictureFactory::NewPictureL(TPictureHeader& aPictureHeader,const CStreamStore& aPictureStore)const
// called (by the containing doc) to restore an app door from its header
//
	{
	__SHOW_TRACE(_L("Starting TApaPictureFactory::NewPictureL"));
	if (aPictureHeader.iPictureType!=KUidPictureTypeDoor)
		User::Leave(KErrNotSupported); // wrong type
	if (!aPictureHeader.iPicture.IsId())
		User::Leave(KErrBadHandle); // not an id - can't restore
	//
	// create and restore the door
	TStreamId id = aPictureHeader.iPicture.AsId();
	CApaDoor* door=CApaDoor::NewL(aPictureStore,id,*CONST_CAST(CApaProcess*,iApaProcess));
	aPictureHeader.iPicture = door;
	//
	// set the icon size if requested
	if (iIconSize!=KDoNotApplyIconSize)
		door->SetIconSizeInTwips(iIconSize);
	}


////////////////////////////////////
// HBufBuf
////////////////////////////////////

HBufBuf* HBufBuf::NewL(CBufBase& aBuf,TInt aPos,TInt aMode)
//
// Create a pre-set buffer stream buffer.
//
	{
	HBufBuf* buf=new(ELeave) HBufBuf;
	buf->Set(aBuf,aPos,aMode);
	return buf;
	}

void HBufBuf::DoRelease()
//
// Finished with this stream buffer.
//
	{
	delete this;
	}

