appfw/apparchitecture/tef/TSTAPP.CPP
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // tstapp.cpp
       
    15 //
       
    16 
       
    17 /**
       
    18  @file tstapp.cpp
       
    19  @test
       
    20  @internalComponent - Internal Symbian test code 
       
    21 */
       
    22 
       
    23 #include <e32uid.h>
       
    24 #include <s32std.h>
       
    25 #include <s32stor.h>
       
    26 #include <s32file.h>
       
    27 #include <apgicnfl.h>
       
    28 #include "apfdef.h"
       
    29 #include "tstapp.h"
       
    30 #include <apgcli.h>
       
    31 
       
    32 #include <eikstart.h>
       
    33 #include <ecom/ecom.h>
       
    34 #include <ecom/implementationproxy.h>
       
    35 
       
    36 _LIT(KTestAppCaption, "Test App");
       
    37 _LIT(KTempFilePath, "c:\\system\\temp\\");
       
    38 _LIT(KTestAppIniFilePath, "c:\\system\\data\\tstapp.ini");
       
    39 
       
    40 
       
    41 GLDEF_C TInt E32Dll(
       
    42 					)
       
    43 	{
       
    44 	return KErrNone;
       
    45 	}
       
    46 
       
    47 LOCAL_C CApaApplication* NewApplicationL()
       
    48 	{
       
    49 	CApaApplication* thing=NULL;
       
    50 	thing=CTestApp::NewL();
       
    51 	return thing;
       
    52 	//return new CExampleApplication;
       
    53 	}
       
    54 
       
    55 LOCAL_D const TImplementationProxy ImplementationTable[]=
       
    56 	{
       
    57 	IMPLEMENTATION_PROXY_ENTRY(KTestAppUidValue, NewApplicationL)
       
    58 	};
       
    59 
       
    60 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
       
    61 	{
       
    62 	aTableCount=sizeof(ImplementationTable)/sizeof(ImplementationTable[0]);
       
    63 	return ImplementationTable;
       
    64 	}
       
    65 
       
    66 
       
    67 //
       
    68 // CBasicAppUi
       
    69 //
       
    70 
       
    71 void CBasicAppUi::ConstructL()
       
    72 	{
       
    73 	BaseConstructL(ENoAppResourceFile|ENoScreenFurniture);
       
    74 	}
       
    75 	
       
    76 //
       
    77 // CTestApp
       
    78 //
       
    79 
       
    80 CTestApp* CTestApp::NewL()
       
    81 // The gated function
       
    82 //
       
    83 	{
       
    84 	CTestApp* self=new(ELeave) CTestApp();
       
    85 	CleanupStack::PushL(self);
       
    86 	self->ConstructL();
       
    87 	CleanupStack::Pop(self);
       
    88 	return self;
       
    89 	}
       
    90 
       
    91 
       
    92 CTestApp::CTestApp()
       
    93 	:iCaption(KTestAppCaption)
       
    94 	{}
       
    95 
       
    96 
       
    97 CTestApp::~CTestApp()
       
    98 	{
       
    99 	}
       
   100 
       
   101 void CTestApp::ConstructL()
       
   102 	{
       
   103 	User::LeaveIfError(iFs.Connect());
       
   104 	}
       
   105 
       
   106 	
       
   107 void CTestApp::PreDocConstructL()
       
   108 	{
       
   109 	}
       
   110 
       
   111 
       
   112 CEikDocument* CTestApp::CreateDocumentL()
       
   113 	{	
       
   114 	return CTestAppDoc::NewL(*this);
       
   115 	}
       
   116 
       
   117 TDesC& CTestApp::Caption()
       
   118 // return the app title in current system language
       
   119 //
       
   120 	{
       
   121 	return iCaption;
       
   122 	}
       
   123 
       
   124 
       
   125 TUid CTestApp::AppDllUid()const
       
   126 	{
       
   127 	return KUidTestApp;
       
   128 	}
       
   129 	
       
   130 CDictionaryStore* CTestApp::OpenIniFileLC(RFs& aFs) const
       
   131 // Opens the applications ini file if it exists, otherwise creates a new one
       
   132 // The ini file is located on KTestAppIniFilePath
       
   133 	{
       
   134 	aFs.MkDirAll(KTestAppIniFilePath); // ignore the error
       
   135 	//
       
   136 	// open the ini file if it exists, otherwise create a new one
       
   137 	return CDictionaryFileStore::OpenLC(aFs, KTestAppIniFilePath(), AppDllUid());
       
   138 	}
       
   139 
       
   140 
       
   141 void CTestApp::Capability(TDes8& aInfo)const
       
   142 	{
       
   143 	RApaLsSession apaLsSession;
       
   144 	TInt err = apaLsSession.Connect();
       
   145 	__ASSERT_ALWAYS(!err, User::Panic(_L("CTestApp::Capability"), err));	
       
   146 	apaLsSession.GetAppCapability(aInfo, CTestApp::AppDllUid());
       
   147 	apaLsSession.Close();
       
   148 	}
       
   149 
       
   150 
       
   151 //
       
   152 // CTestAppDoc
       
   153 //
       
   154 
       
   155 CTestAppDoc* CTestAppDoc::NewL(CEikApplication& aApp)
       
   156 	{
       
   157 	CTestAppDoc* self=new(ELeave) CTestAppDoc(aApp);
       
   158 	CleanupStack::PushL(self);
       
   159 	self->ConstructL();
       
   160 	CleanupStack::Pop();
       
   161 	return self;
       
   162 	}
       
   163 
       
   164 
       
   165 CTestAppDoc::CTestAppDoc(CEikApplication& aApp)
       
   166 	: CEikDocument(aApp),
       
   167 	iValue(0)
       
   168 	{}
       
   169 
       
   170 	
       
   171 void CTestAppDoc::ConstructL()
       
   172 	{
       
   173 	iEmbedList = new(ELeave) CArrayFixFlat<TPictureHeader>(1);
       
   174 	}
       
   175 
       
   176 CEikAppUi* CTestAppDoc::CreateAppUiL()
       
   177 	{
       
   178 	
       
   179 	return new(ELeave) CBasicAppUi();
       
   180 	}
       
   181 
       
   182 CTestAppDoc::~CTestAppDoc()
       
   183 	{
       
   184 	// delete all the embedded doc's first...
       
   185 	if (iEmbedList)
       
   186 		{
       
   187 		for (TInt i=iEmbedList->Count()-1 ; i>=0 ; i--)
       
   188 			delete EmbeddedDoor(i); // destroys the door (which in turn removes doc from the process and deletes it)
       
   189 		}
       
   190 	// then...
       
   191 	delete iEmbedList;
       
   192 	delete iStore;
       
   193 	}
       
   194 
       
   195 
       
   196 TBool CTestAppDoc::IsEmpty()const
       
   197 	{
       
   198 	// return ETrue if the document is empty
       
   199 	if (iValue==0)
       
   200 		return ETrue;
       
   201 	else 
       
   202 		return EFalse;
       
   203 	}
       
   204 
       
   205 
       
   206 TBool CTestAppDoc::HasChanged()const
       
   207 	{
       
   208 	return iHasChanged;
       
   209 	}
       
   210 
       
   211 /** builds a new embedded or main document without loading from a store (may create the content from 
       
   212 	eg code or a template file).
       
   213 */
       
   214 void CTestAppDoc::NewDocumentL()
       
   215 	{}
       
   216 
       
   217 
       
   218 /** Create a document file & store of the specified name, storing into the store.
       
   219     On system startup the name might not have a filename component in which case this function should parse the 
       
   220     passed name with a related default file name appropriate to this application and in an appropriate language
       
   221 */
       
   222 CFileStore* CTestAppDoc::CreateFileStoreLC(RFs& aFs,const TDesC& aFileName)
       
   223 	{
       
   224 	// set the default name just in case...
       
   225 	TFileName defaultName(_L("temp"));
       
   226 	TParse parser;
       
   227 	User::LeaveIfError(parser.Set(aFileName,&defaultName,NULL));
       
   228 	// create the file store
       
   229 	CFileStore* store;
       
   230 	store = CDirectFileStore::CreateLC(aFs,parser.FullName(),EFileWrite);
       
   231 	store->SetTypeL(TUidType(KDirectFileStoreLayoutUid,KUidAppDllDoc,KUidTestApp));
       
   232 	CStreamDictionary* streamDic=CStreamDictionary::NewL();
       
   233 	CleanupStack::PushL(streamDic);
       
   234 	StoreL(*store,*streamDic); 
       
   235 	Process()->WriteRootStreamL(*store,*streamDic,*Application());
       
   236 	store->CommitL(); // now the store will be fully initialised
       
   237 	CleanupStack::PopAndDestroy(); // streamDic
       
   238 	return store;
       
   239 	}
       
   240 
       
   241 
       
   242 void CTestAppDoc::EditL(MApaEmbeddedDocObserver* /*aContainer*/,TBool aReadOnly)
       
   243 	{
       
   244 	if (aReadOnly)
       
   245 		{
       
   246 		if (iContainer)
       
   247 			{
       
   248 			iContainer->NotifyExit(MApaEmbeddedDocObserver::ENoChanges);
       
   249 			iContainer = NULL; // iContainer must be nulled when editing is finished
       
   250 			}
       
   251 		}
       
   252 	else
       
   253 		{
       
   254 		iValue++;
       
   255 		iHasChanged = ETrue;
       
   256 		if (iContainer)
       
   257 			{
       
   258 			iContainer->NotifyExit(MApaEmbeddedDocObserver::EKeepChanges);
       
   259 			iContainer = NULL; // iContainer must be nulled when editing is finished
       
   260 			}
       
   261 		}
       
   262 	}
       
   263 
       
   264 void CTestAppDoc::NotifyExit(MApaEmbeddedDocObserver::TExitMode aMode)
       
   265 	{
       
   266 	switch (aMode)
       
   267 		{
       
   268 		case EKeepChanges:
       
   269 			iHasChanged = ETrue; // note that my contents have changed
       
   270 			break;
       
   271 		case ERevertToSaved:
       
   272 			// reload whole document (panic if I'm not the main doc)
       
   273 			break;
       
   274 		case ENoChanges:
       
   275 			// no changes
       
   276 			break;
       
   277 		case EEmpty:
       
   278 			// the embedded doc is empty
       
   279 			break;
       
   280 		}
       
   281 	}
       
   282 
       
   283 
       
   284 void CTestAppDoc::PrintL(const CStreamStore& /*aSourceStore*/)
       
   285 	{}
       
   286 
       
   287 
       
   288 void CTestAppDoc::SaveL()
       
   289 	{
       
   290 
       
   291 	CDirectFileStore* store;
       
   292 	TParse newFilePath;
       
   293 	// create temp file
       
   294 	User::LeaveIfError( newFilePath.Set(Process()->MainDocFileName(),NULL,NULL) ); // abuse new file path
       
   295 	TBuf<2> drive=newFilePath.Drive();
       
   296 	TFileName filePath(KTempFilePath);
       
   297 	User::LeaveIfError( newFilePath.Set(drive,&filePath,NULL) );
       
   298 	Process()->FsSession().MkDirAll(newFilePath.DriveAndPath());
       
   299 	TFileName tempName;
       
   300 	store = CDirectFileStore::TempLC(Process()->FsSession(),newFilePath.DriveAndPath(),tempName,EFileWrite);
       
   301 	store->SetTypeL(((CFileStore*)iStore)->Type());
       
   302 
       
   303 	// store main in temp
       
   304 	CStreamDictionary* streamDic=CStreamDictionary::NewL();
       
   305 	CleanupStack::PushL(streamDic);
       
   306 	StoreL(*store,*streamDic);
       
   307 	iStore->CommitL();  
       
   308 
       
   309 	// write root stream
       
   310 	Process()->WriteRootStreamL(*store,*streamDic,*Application());
       
   311 	CleanupStack::PopAndDestroy(); // streamDic
       
   312 	// close the new store
       
   313 	store->CommitL();
       
   314 	CleanupStack::PopAndDestroy(); // store
       
   315 	// close the old store
       
   316 	delete iStore;
       
   317 	iStore = NULL;
       
   318 	// replace the old file
       
   319 	User::LeaveIfError( Process()->FsSession().Replace(tempName,Process()->MainDocFileName()) );
       
   320 	// open new file
       
   321 	iStore = CDirectFileStore::OpenL(Process()->FsSession(),Process()->MainDocFileName(),EFileShareExclusive);
       
   322 	}
       
   323 
       
   324 
       
   325 void CTestAppDoc::StoreL(CStreamStore& aStore,CStreamDictionary& aStreamDic)const
       
   326 	{
       
   327 	//
       
   328 	// create map and store embedded doc's
       
   329 	CStoreMap* map=CStoreMap::NewLC(aStore);
       
   330 	StoreComponentsL(aStore,*map);
       
   331 	// store the headstream
       
   332 	RStoreWriteStream stream(*map);
       
   333 	TStreamId id=stream.CreateLC(aStore); 
       
   334 	ExternalizeL(stream);
       
   335 	stream.CommitL();
       
   336 	// assign the headstream in the dictionary
       
   337 	aStreamDic.AssignL(KUidTestAppHeadStream,id);
       
   338 	// tidy up
       
   339 	map->Reset();
       
   340 	CleanupStack::PopAndDestroy(2); // map,stream
       
   341 	}
       
   342 
       
   343 
       
   344 void CTestAppDoc::RestoreL(const CStreamStore& aStore,const CStreamDictionary& aStreamDic)
       
   345 	{
       
   346 	//
       
   347 	TStreamId headStreamId=aStreamDic.At(KUidTestAppHeadStream);
       
   348 	RStoreReadStream stream;
       
   349 	stream.OpenLC(aStore,headStreamId); 
       
   350 	InternalizeL(stream);
       
   351 	CleanupStack::PopAndDestroy(); // stream
       
   352 	// restore the embedded bits
       
   353 	RestoreComponentsL(aStore);
       
   354 	}
       
   355 
       
   356 
       
   357 void CTestAppDoc::DetachFromStoreL(CPicture::TDetach aDegree)
       
   358 	{
       
   359 	for (TInt i=0 ; i<iEmbedList->Count() ; i++)
       
   360 		EmbeddedDoor(i)->DetachFromStoreL(aDegree);
       
   361 	}
       
   362 
       
   363 
       
   364 void CTestAppDoc::StoreComponentsL(CStreamStore& aStore,CStoreMap& aMap)const
       
   365 	{
       
   366 	for (TInt i=0 ; i<iEmbedList->Count() ; i++)
       
   367 		{
       
   368 		TStreamId id=EmbeddedDoor(i)->StoreL(aStore);
       
   369 		aMap.BindL((*iEmbedList)[i].iPicture,id);
       
   370 		}
       
   371 	}
       
   372 
       
   373 
       
   374 void CTestAppDoc::RestoreComponentsL(const CStreamStore& aStore)
       
   375 	{
       
   376 	TApaPictureFactory factory(Process());
       
   377 	for (TInt i=0 ; i<iEmbedList->Count() ; i++)
       
   378 		{
       
   379 		TRAPD(ret, factory.NewPictureL((*iEmbedList)[i],aStore) );
       
   380 		if ((ret!=KErrNone)&&(ret!=KErrNotFound))
       
   381 			User::Leave(ret);
       
   382 		}
       
   383 	}
       
   384 
       
   385 
       
   386 void CTestAppDoc::ExternalizeL(RWriteStream& aStream)const
       
   387 	{
       
   388 	aStream.WriteInt32L(iValue);
       
   389 	// externalize num of embedded objects
       
   390 	TInt numObjects=iEmbedList->Count();
       
   391 	aStream.WriteInt32L(numObjects);
       
   392 	// externalize the doors
       
   393 	for (TInt i=0 ; i<numObjects ; i++)
       
   394 		aStream<< (*iEmbedList)[i].iPicture;
       
   395 	}
       
   396 
       
   397 
       
   398 void CTestAppDoc::InternalizeL(RReadStream& aStream)
       
   399 	{
       
   400 	// reset();
       
   401 	iValue = aStream.ReadInt32L();
       
   402 	TInt numObjects=aStream.ReadInt32L();
       
   403 	for (TInt i=0 ; i<numObjects ; i++)
       
   404 		{
       
   405 		TPictureHeader header;
       
   406 		header.iPictureType = KUidPictureTypeDoor;
       
   407 		aStream>> header.iPicture;
       
   408 		iEmbedList->AppendL(header);
       
   409 		}
       
   410 	}