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