brandingserver/bsserver/cbsstoragemanager.cpp
changeset 46 860cd8a5168c
parent 35 085f765766a0
equal deleted inserted replaced
35:085f765766a0 46:860cd8a5168c
     1 /*
       
     2 * Copyright (c) 2006-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Stores element data and writes it to stream
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32strm.h>
       
    20 #include <s32file.h>
       
    21 #include <f32file.h>
       
    22 
       
    23 #include <bautils.h>
       
    24 #include <utf.h>
       
    25 
       
    26 
       
    27 #include "bsimportconstants.h"
       
    28 #include "cbsstoragemanager.h"
       
    29 #include "cbsstorage.h"
       
    30 #ifdef __WINSCW__
       
    31 #include "cbsibywriter.h"
       
    32 #endif
       
    33 #include "cbsserver.h"
       
    34 #include "cbssession.h"
       
    35 #include "mbsupdater.h"
       
    36 //#include "importlogwriter.h"
       
    37 #include "debugtrace.h"
       
    38 
       
    39 
       
    40 // ======== MEMBER FUNCTIONS ========
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // CBSStorageManager::NewL
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CBSStorageManager* CBSStorageManager::NewL( CBSSession* aSession, const TDesC& aAppId )
       
    47     {
       
    48     CBSStorageManager* self = NewLC( aSession, aAppId );
       
    49     CleanupStack::Pop();
       
    50     return self;
       
    51     }
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // CBSStorageManager::NewLC
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CBSStorageManager* CBSStorageManager::NewLC( CBSSession* aSession, const TDesC& aAppId )
       
    58     {
       
    59     CBSStorageManager* self =
       
    60         new (ELeave) CBSStorageManager();
       
    61     CleanupStack::PushL( self );
       
    62     self->ConstructL( aSession, aAppId );
       
    63     return self;
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CBSStorageManager::ConstructL
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 void CBSStorageManager::ConstructL( CBSSession* aSession, const TDesC& aAppId )
       
    71     {
       
    72     TRACE( T_LIT( "CBSStorageManager::ConstructL begin aAppId[%S]"), &aAppId );
       
    73     User::LeaveIfError( iFs.Connect() );
       
    74  
       
    75 // RFs::ShareProtected() needs to be called for the session 
       
    76 // used to open the handle that is going to be shared.
       
    77 // ShareProtected() was called to a wrong Fs-session in 
       
    78 // CBSBrandHandler::GetFileL().
       
    79     User::LeaveIfError( iFs.ShareProtected() );
       
    80     
       
    81     iFs.CreatePrivatePath( EDriveC );
       
    82     
       
    83     iSession = aSession;
       
    84     
       
    85     
       
    86 #ifdef __WINSCW__        
       
    87     if( !iWriter )
       
    88     	{
       
    89     	iWriter = CBSIBYWriter::NewL();
       
    90     	}
       
    91     if( aAppId.Compare( KNullDesC ) )
       
    92     	{
       
    93     	HBufC* temp = HBufC::NewL( KBSIbyDirectory().Length() +
       
    94     							   aAppId.Length() +
       
    95     							   KBSIbyExtension().Length() );
       
    96 		TPtr ptr( temp->Des() );
       
    97 		ptr.Append( KBSIbyDirectory );
       
    98 		ptr.Append( aAppId );
       
    99 		ptr.Append( KBSIbyExtension );
       
   100 		delete iIbyFile;
       
   101 		iIbyFile = temp;
       
   102 	    iWriter->InitIbyFileL( iFs, *iIbyFile );
       
   103     	}
       
   104 #endif
       
   105     TRACE( T_LIT( "CBSStorageManager::ConstructL end") );
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // CBSStorageManager::CBSStorageManager
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 CBSStorageManager::CBSStorageManager() :
       
   113     iCurrent( KErrNotFound )
       
   114     {
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // CBSStorageManager::~CBSStorageManager
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 CBSStorageManager::~CBSStorageManager()
       
   122     {
       
   123     TRACE( T_LIT( "CBSStorageManager::~CBSStorageManager begin") );
       
   124     iStorages.ResetAndDestroy();
       
   125     iFs.Close();
       
   126 
       
   127 	delete iAppId;
       
   128 	delete iBrandId;
       
   129 	delete iLanguageId;
       
   130     delete iFilename;
       
   131     
       
   132     #ifdef __WINSCW__
       
   133     delete iWriter;
       
   134     #endif
       
   135     
       
   136     delete iIbyFile;
       
   137     TRACE( T_LIT( "CBSStorageManager::~CBSStorageManager end") );
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // CBSStorageManager::InitTransactionL
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 void CBSStorageManager::ConnectTransactionL(const TDesC& aAppId,
       
   145 				    					  const TDesC& aBrandId,
       
   146 				    					  TLanguage aLanguageId,
       
   147     					   				  TBool aAppending, /*= EFalse*/
       
   148     					   				  TInt aReserved /*=0*/)
       
   149 	{
       
   150 	TRACE( T_LIT( "CBSStorageManager::ConnectTransactionL begin AppId[%S],BrandId[%S]"),&aAppId, &aBrandId );
       
   151 	if( iActive )
       
   152 		{
       
   153 		TBool transactionOk = ETrue;
       
   154 		
       
   155 		transactionOk = iReserved == aReserved;
       
   156 
       
   157 		if( transactionOk )
       
   158 			{
       
   159 			transactionOk = iLanguageIdNum == aLanguageId;
       
   160 			}
       
   161 
       
   162 		if( transactionOk )
       
   163 			{
       
   164 			transactionOk != iAppId->Compare( aAppId );
       
   165 			}
       
   166 		
       
   167 		if( transactionOk )
       
   168 			{
       
   169 			transactionOk != iBrandId->Compare( aBrandId );
       
   170 			}
       
   171 			
       
   172 		if( !transactionOk )
       
   173 			{
       
   174 			User::Leave( KErrArgument );
       
   175 			}
       
   176 		// the same transaction as is ongoing
       
   177 		return;
       
   178 		}
       
   179 	iActive = ETrue;
       
   180 	
       
   181     HBufC* tmpAppId = aAppId.AllocL();
       
   182     delete iAppId;
       
   183     iAppId = tmpAppId;
       
   184     
       
   185     HBufC* tmpBrandId = aBrandId.AllocL();
       
   186     delete iBrandId;
       
   187     iBrandId = tmpBrandId;
       
   188     
       
   189     iLanguageIdNum = aLanguageId;
       
   190     
       
   191     TBuf<KLangBufLength> buffer;
       
   192     if( aLanguageId < 10 )
       
   193     	{
       
   194     	// we want two digit languageid
       
   195     	buffer.AppendNum( KLeadingZero );
       
   196     	}
       
   197     buffer.AppendNum( aLanguageId );
       
   198     HBufC* tmp = buffer.AllocL();
       
   199     delete iLanguageId;
       
   200     iLanguageId = tmp;
       
   201     
       
   202     iReserved = aReserved;
       
   203     
       
   204     HBufC* tempFile = ConstructFileNameL();
       
   205     delete iFilename;
       
   206     iFilename = tempFile;
       
   207     
       
   208     iAppending = aAppending;
       
   209 	TRACE( T_LIT( "CBSStorageManager::ConnectTransactionL end") );
       
   210 	}
       
   211 
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 // CBSStorageManager::CancelTransactionL
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 void CBSStorageManager::CancelTransactionL()
       
   218 	{
       
   219 	TRACE( T_LIT( "CBSStorageManager::CancelTransactionL begin") );
       
   220 	if( !iActive )
       
   221 		{
       
   222 		User::Leave( KErrNotFound );
       
   223 		}
       
   224 	// this closes the transaction
       
   225 	CleanupTransaction();
       
   226 	TRACE( T_LIT( "CBSStorageManager::CancelTransactionL end") );
       
   227 	}
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // CBSStorageManager::ConstructFileNameL()
       
   231 // ---------------------------------------------------------------------------
       
   232 //
       
   233 HBufC* CBSStorageManager::ConstructFileNameL( TInt aReserved /* = 0 */ )
       
   234     {
       
   235     HBufC* fileName = NULL;
       
   236     if( iAppId && iBrandId && iLanguageId )
       
   237         {
       
   238         fileName = ConstructFileNameL( *iAppId, *iBrandId, iLanguageIdNum, aReserved );
       
   239         }
       
   240     return fileName;
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 // CBSStorageManager::ConstructFileNameL()
       
   245 // ---------------------------------------------------------------------------
       
   246 //
       
   247 HBufC* CBSStorageManager::ConstructFileNameL( const TDesC& aAppId,
       
   248 										      const TDesC& aBrandId,
       
   249 										      TLanguage aLanguage,
       
   250 										      TInt aReserved /* = 0 */ )
       
   251     {
       
   252 	HBufC* fileName = NULL;
       
   253 
       
   254     TBuf<KLangBufLength> langBuf;
       
   255 	if (aLanguage >= 100)
       
   256 		User::LeaveIfError (KErrNotFound);
       
   257     if( aLanguage < 10 )
       
   258     	{
       
   259     	// we want two digit languageid
       
   260     	langBuf.AppendNum( KLeadingZero );
       
   261     	}
       
   262     langBuf.AppendNum( aLanguage );
       
   263     
       
   264     TInt length = aAppId.Length() + aBrandId.Length() +
       
   265                   KDefFileName().Length() + KDirSeparator().Length() * 2 +
       
   266                   langBuf.Length() + KDot().Length() * 3 + KMaxVersionLenght;
       
   267 	fileName = HBufC::NewLC( length );
       
   268     
       
   269     TPtr file( fileName->Des() );
       
   270 
       
   271     // [application_id]\[brand_id]\[def_filename][language_id]
       
   272     //
       
   273     
       
   274     file.Append( aAppId );
       
   275     file.Append( KDirSeparator );
       
   276     file.Append( aBrandId );
       
   277     file.Append( KDirSeparator );
       
   278     iDir.Set( file.Left( file.Length() ) );
       
   279     file.Append( KDefFileName );
       
   280     file.Append( langBuf );
       
   281     if( aReserved > 0 )
       
   282     	{
       
   283 	    TBuf<KMaxVersionLenght> versionBuffer;
       
   284 	    versionBuffer.AppendNum( aReserved );
       
   285 	    file.Append( KDot() );
       
   286     	file.Append( versionBuffer );
       
   287     	}
       
   288 
       
   289     if( fileName )
       
   290     	{
       
   291     	CleanupStack::Pop( fileName );
       
   292     	}
       
   293     return fileName;
       
   294     }
       
   295 
       
   296 // ---------------------------------------------------------------------------
       
   297 // CBSStorageManager::CreateStorageL
       
   298 // ---------------------------------------------------------------------------
       
   299 //
       
   300 void CBSStorageManager::CreateStorageL()
       
   301     {
       
   302     CBSStorage* tmp = CBSStorage::NewL();
       
   303     iStorages.Append( tmp );
       
   304     iCurrent = iStorages.Find( tmp );
       
   305     }
       
   306 
       
   307 // ---------------------------------------------------------------------------
       
   308 // CBSStorageManager::StorageCount
       
   309 // ---------------------------------------------------------------------------
       
   310 //
       
   311 TInt CBSStorageManager::StorageCount()
       
   312     {
       
   313     return iStorages.Count();
       
   314     }
       
   315 
       
   316 // ---------------------------------------------------------------------------
       
   317 // CBSStorageManager::Storage
       
   318 // ---------------------------------------------------------------------------
       
   319 //
       
   320 CBSStorage* CBSStorageManager::Storage( TInt aIndex )
       
   321     {
       
   322     return iStorages[ aIndex ];
       
   323     }
       
   324 
       
   325 // ---------------------------------------------------------------------------
       
   326 // CBSStorageManager::Storage
       
   327 // ---------------------------------------------------------------------------
       
   328 //
       
   329 CBSStorage* CBSStorageManager::Storage()
       
   330     {
       
   331     if( iStorages.Count() > 0 )
       
   332     	{
       
   333     	return iStorages[ iCurrent ];	
       
   334     	}
       
   335     return NULL;
       
   336     }
       
   337 
       
   338 // ---------------------------------------------------------------------------
       
   339 // CBSStorageManager::WriteStorageFilesL
       
   340 // ---------------------------------------------------------------------------
       
   341 //
       
   342 TInt CBSStorageManager::WriteStorageFilesL()
       
   343     {
       
   344     TRACE( T_LIT( "CBSStorageManager::WriteStorageFilesL begin") );
       
   345     if( !iActive )
       
   346     	{
       
   347     	User::Leave( KErrNotFound );
       
   348     	}
       
   349     TInt count = iStorages.Count();
       
   350     for( TInt i = 0; i < count; i++ )
       
   351         {
       
   352         // get the private path
       
   353         TBuf<KMaxPath> path;
       
   354         User::LeaveIfError( iFs.PrivatePath( path ) );
       
   355         
       
   356         // construct absolute filename
       
   357         HBufC* fullname = FullFileNameLC( *iFilename, ETrue );
       
   358 
       
   359         TParse filestorename;
       
   360         iFs.Parse( *fullname, filestorename );
       
   361 
       
   362 	    RFileWriteStream outstream;
       
   363 
       
   364 		TInt currentVersion = iReserved;
       
   365 		TInt newVersion = iReserved;
       
   366 		// check if the brand has any active clients
       
   367 		TBool brandActive = ETrue;
       
   368 		if( iSession )
       
   369 			{
       
   370 			CBSServer* server = iSession->Server();
       
   371 			if( server )
       
   372 				{
       
   373 				brandActive = server->MatchSessionL( *iAppId, *iBrandId,
       
   374 													 iLanguageIdNum, iSession, currentVersion );					
       
   375 				}
       
   376 			}
       
   377 
       
   378 		if( BaflUtils::FileExists( iFs, filestorename.FullName() ) )
       
   379 			{
       
   380 			currentVersion = ReadVersionL( filestorename.FullName() );
       
   381 			
       
   382 			if( iReserved == KBSAutomaticVersionUpdate && brandActive )
       
   383 				{
       
   384 
       
   385 				// the brand has active clients
       
   386 				// check if there is already a versioned file for this version
       
   387 				HBufC* fileName = FindAvailableVersionL( currentVersion + 1, newVersion );
       
   388 				CleanupStack::PushL( fileName);
       
   389 				
       
   390 				HBufC* newName = FullFileNameLC( *fileName, ETrue );
       
   391 
       
   392 		        iFs.Parse( *newName, filestorename );
       
   393 		        
       
   394 				if( BaflUtils::FileExists( iFs, filestorename.FullName() ) )
       
   395 					{
       
   396 					// the file already exists
       
   397 					User::Leave( KErrAlreadyExists );
       
   398 					}
       
   399 				CleanupStack::PopAndDestroy( 2, fileName ); // newName, fileName
       
   400 				}
       
   401 				
       
   402 			else if( ( iReserved == currentVersion ) )
       
   403 				{
       
   404 				// this version is already installed
       
   405 				User::Leave( KErrAlreadyExists );
       
   406 				}
       
   407 				
       
   408 			else
       
   409 				{
       
   410 				// check if the current version brand is active
       
   411 				brandActive = iSession->Server()->MatchSessionL( *iAppId, *iBrandId,
       
   412 													 iLanguageIdNum, iSession, currentVersion );
       
   413 				// new version
       
   414 				if( brandActive )
       
   415 					{
       
   416 					// brand is active
       
   417 					// check if there is already a versioned file for this version
       
   418 					HBufC* fileName = ConstructFileNameL( iReserved );
       
   419 					CleanupStack::PushL( fileName );
       
   420 					
       
   421 			        HBufC* newName = FullFileNameLC( *fileName, ETrue );
       
   422 			        
       
   423 			        iFs.Parse( *newName, filestorename );
       
   424 			        			        
       
   425 					if( BaflUtils::FileExists( iFs, filestorename.FullName() ) )
       
   426 						{
       
   427 						// the file already exists
       
   428 						User::Leave( KErrAlreadyExists );
       
   429 						}
       
   430 					CleanupStack::PopAndDestroy( 2, fileName ); // newName, fileName
       
   431 					}
       
   432 				}
       
   433 			}
       
   434 			
       
   435  
       
   436 		// write a new brand file
       
   437 		
       
   438 	    // Construct the output file & stream.
       
   439 	    iFs.MkDirAll( filestorename.DriveAndPath() );
       
   440 
       
   441         TInt err = outstream.Replace( iFs, filestorename.FullName(), EFileWrite );
       
   442 
       
   443 
       
   444         if( err )
       
   445             {
       
   446 			TRACE( T_LIT("ERROR: Cannot create branding file: '%S'"), fullname );
       
   447 
       
   448             User::Leave( err );
       
   449             }
       
   450 
       
   451         CleanupClosePushL( outstream );
       
   452 		
       
   453 		if( iReserved == KBSAutomaticVersionUpdate )
       
   454 			{
       
   455 			// set the next version
       
   456 			iStorages[i]->SetVersion( newVersion );
       
   457 			iReserved = newVersion;
       
   458 			}
       
   459 		
       
   460         // Write data to file
       
   461         TRAPD( extError, iStorages[i]->ExternalizeL( outstream ) );
       
   462 		if( extError && !iAppending )
       
   463 			{
       
   464 			// there was an error externalizing the data and we are installing
       
   465 			// delete the file, since it's not complete
       
   466 			iFs.Delete( filestorename.DriveAndPath() );
       
   467 			}
       
   468 		User::LeaveIfError( extError );
       
   469 
       
   470 
       
   471 #ifdef __WINSCW__
       
   472         // Add to IBY file
       
   473         iWriter->SetFileItemL( *fullname, *fullname );
       
   474 #endif
       
   475             
       
   476         CleanupStack::PopAndDestroy( 2 ); // fullname, outstream
       
   477 
       
   478 
       
   479 		// existing brand modified --> inform server 
       
   480 		if( iSession )
       
   481 			{
       
   482 			CBSServer* server = iSession->Server();
       
   483 			if( server )
       
   484 				{
       
   485 				server->BrandUpdatedL( *iAppId, *iBrandId,
       
   486 									   iLanguageIdNum, iSession, iReserved );					
       
   487 				}
       
   488 			}
       
   489         }
       
   490     
       
   491     // this closes the transaction
       
   492 	CleanupTransaction();
       
   493 	TRACE( T_LIT( "CBSStorageManager::WriteStorageFilesL end") );
       
   494 	return iReserved;
       
   495     }
       
   496 
       
   497 
       
   498 // ---------------------------------------------------------------------------
       
   499 // CBSStorageManager::ReadVersionL
       
   500 // ---------------------------------------------------------------------------
       
   501 //
       
   502 TInt CBSStorageManager::ReadVersionL( const TDesC& aFilename )
       
   503 	{
       
   504     RFile handle;
       
   505     User::LeaveIfError( handle.Open( iFs, aFilename, EFileRead ) );
       
   506     CleanupClosePushL( handle );
       
   507 	RFileReadStream stream;
       
   508 	stream.Attach( handle );
       
   509 	CleanupClosePushL( stream );
       
   510 	TInt version = stream.ReadInt16L();
       
   511 	CleanupStack::PopAndDestroy( 2 ); // stream, handle
       
   512 	return version;
       
   513 	}
       
   514 
       
   515 // ---------------------------------------------------------------------------
       
   516 // CBSStorageManager::FindAvailableVersionL
       
   517 // ---------------------------------------------------------------------------
       
   518 //	
       
   519 HBufC* CBSStorageManager::FindAvailableVersionL( TInt /*aCurrentVersion*/, TInt& aNewVersion )
       
   520 	{
       
   521 	HBufC* fileName =  NULL;
       
   522 	TInt newVersion = GetNewestVersionL( *iAppId, *iBrandId, iLanguageIdNum );
       
   523 	aNewVersion = newVersion++;
       
   524 	fileName = ConstructFileNameL( aNewVersion );
       
   525 	
       
   526 	return fileName;
       
   527 	}
       
   528 
       
   529 
       
   530 // ---------------------------------------------------------------------------
       
   531 // CBSStorageManager::CleanupTransaction
       
   532 // ---------------------------------------------------------------------------
       
   533 //
       
   534 void CBSStorageManager::CleanupTransaction()
       
   535 	{
       
   536 	iActive = EFalse;
       
   537 	delete iAppId;
       
   538 	iAppId = NULL;
       
   539 	delete iBrandId;
       
   540 	iBrandId = NULL;
       
   541 	delete iLanguageId;
       
   542 	iLanguageId = NULL;
       
   543     delete iFilename;
       
   544     iFilename = NULL;
       
   545 
       
   546     iStorages.ResetAndDestroy();
       
   547 	}
       
   548 
       
   549 
       
   550 TInt CBSStorageManager :: isActualBrandInstalledL ( const TDesC& aAppId, const TDesC& aBrandId, TLanguage aLanguage)
       
   551 {
       
   552 	TInt actualBrandInstalled = -1;
       
   553 	HBufC *actualBrandFileName = ConstructFileNameL(aAppId, aBrandId, aLanguage);
       
   554 	CleanupStack::PushL( actualBrandFileName );
       
   555 	HBufC* newName = FullFileNameLC( *actualBrandFileName , ETrue );
       
   556 	if (newName)
       
   557 		{
       
   558 		RFile handle;
       
   559 		TInt err = handle.Open( iFs, *newName, EFileShareAny );
       
   560 		if (KErrNone == err)
       
   561 			{
       
   562 			actualBrandInstalled = 1;
       
   563 			handle.Close ();
       
   564 			}
       
   565 		}
       
   566 	
       
   567 	CleanupStack::PopAndDestroy (2);
       
   568 	return actualBrandInstalled;
       
   569 }
       
   570 // ---------------------------------------------------------------------------
       
   571 // CBSStorageManager::BrandHandleL
       
   572 // ---------------------------------------------------------------------------
       
   573 //
       
   574 void CBSStorageManager::BrandHandleL( const TDesC& aAppId,
       
   575 									  const TDesC& aBrandId,
       
   576 									  TLanguage aLanguageId,
       
   577 									  RFile& aFile,
       
   578 									  TInt aReserved )
       
   579 	{
       
   580     TRACE( T_LIT( "CBSStorageManager::BrandHandleL begin AppId[%S],BrandId[%S]"),&aAppId, &aBrandId );
       
   581     TBuf<KLangBufLength> langBuf;
       
   582 	if (aLanguageId >= 100)
       
   583 		User::LeaveIfError( KErrNotFound );
       
   584     if( aLanguageId < 10 )
       
   585     	{
       
   586     	// we want two digit languageid
       
   587     	langBuf.AppendNum( KLeadingZero );
       
   588     	}
       
   589     langBuf.AppendNum( aLanguageId );
       
   590     
       
   591     TInt length = aAppId.Length() + aBrandId.Length() +
       
   592                   KDefFileName().Length() + KDirSeparator().Length() * 2 +
       
   593                   langBuf.Length();
       
   594                   
       
   595 	HBufC* fileName = HBufC::NewLC( length );
       
   596 	
       
   597     TPtr filePtr( fileName->Des() );
       
   598 
       
   599     // [application_id]\[brand_id]\[def_filename][language_id]
       
   600     //
       
   601     filePtr.Append( aAppId );
       
   602     filePtr.Append( KDirSeparator );
       
   603     filePtr.Append( aBrandId );
       
   604     filePtr.Append( KDirSeparator );
       
   605 
       
   606     TPtrC directory;
       
   607     directory.Set( filePtr.Left( filePtr.Length() ) );
       
   608     
       
   609     filePtr.Append( KDefFileName );
       
   610     filePtr.Append( langBuf );
       
   611 
       
   612 	HBufC* fullname  = FullFileNameLC( filePtr );
       
   613 
       
   614     RFile handle;
       
   615 
       
   616 	TBool versioned = EFalse;
       
   617 
       
   618 	TParse findname;
       
   619 	iFs.Parse( *fullname, findname );
       
   620 
       
   621 	TFileName fullPtr = fullname->Des();
       
   622 
       
   623 
       
   624 	// seems like nearestlanguagefile is only 
       
   625 	// searching from z-drive by default, unless 
       
   626 	// some other drive is explicitely defined
       
   627 	// for that reason we first try from
       
   628 	// default drive and then force c-drive if
       
   629 	// not found.
       
   630 	BaflUtils::NearestLanguageFile( iFs, fullPtr );
       
   631 
       
   632 	TInt currentVersion = 0;
       
   633 	TRAPD( err, currentVersion = ReadVersionL( fullPtr ) );
       
   634 	if( ( err == KErrNotFound ) || ( err == KErrPathNotFound ) )
       
   635 		{
       
   636 		// not found, we try forcing c-drive
       
   637 		HBufC* driveForced  = FullFileNameLC( filePtr, ETrue );
       
   638 		TFileName forcedPtr = driveForced->Des();
       
   639 		BaflUtils::NearestLanguageFile( iFs, forcedPtr );
       
   640 		TRAP( err, currentVersion = ReadVersionL( forcedPtr ) );
       
   641 		
       
   642 		if( err == KErrPathNotFound )
       
   643 			{
       
   644 			// switching the error code to be more clear
       
   645 			err = KErrNotFound;
       
   646 			}
       
   647 		User::LeaveIfError( err );
       
   648 		
       
   649 		// this file was found, we can use this
       
   650 		CleanupStack::Pop( driveForced );
       
   651 		CleanupStack::PopAndDestroy( fullname );
       
   652 		fullname = driveForced;
       
   653 		CleanupStack::PushL( fullname );
       
   654 		}
       
   655 	else
       
   656 		{
       
   657 		User::LeaveIfError( err );
       
   658 		}
       
   659 	err = KErrNone;
       
   660 	if( currentVersion != aReserved ) 
       
   661 		{
       
   662 		// not the base version, check if we have a versioned file for this
       
   663 		HBufC* versionedName = ConstructFileNameL( aAppId, aBrandId, 
       
   664 												   aLanguageId, aReserved );
       
   665 		CleanupStack::PushL( versionedName );
       
   666 		HBufC* fullVersionedName  = FullFileNameLC( *versionedName, ETrue );
       
   667 		
       
   668 		err = handle.Open( iFs, *fullVersionedName, EFileShareAny );
       
   669 		CleanupStack::Pop( fullVersionedName );
       
   670 		CleanupStack::PopAndDestroy( versionedName );
       
   671 		CleanupStack::PopAndDestroy( fullname );
       
   672 		fullname = fullVersionedName;
       
   673 		CleanupStack::PushL( fullname );
       
   674 		versioned = ETrue;
       
   675 		}
       
   676 	else
       
   677 		{
       
   678 		err = handle.Open( iFs, *fullname, EFileShareAny );
       
   679 		}
       
   680 
       
   681 	if( err == KErrBadName )
       
   682 		{
       
   683 		// change bad name to not found
       
   684 		err = KErrNotFound;
       
   685 		}
       
   686 	User::LeaveIfError( err );
       
   687     
       
   688     aFile = handle;
       
   689     
       
   690     if( iSession )
       
   691     	{
       
   692     	CBSServer* server = iSession->Server();
       
   693     	if( server )
       
   694     		{
       
   695     		server->RegisterFileForSessionL( iSession, *fullname, versioned );
       
   696     		}
       
   697     	}
       
   698     
       
   699     CleanupStack::PopAndDestroy( 2, fileName ); // fullname, fileName
       
   700 	TRACE( T_LIT( "CBSStorageManager::BrandHandleL end") );
       
   701 	}
       
   702 	
       
   703 // ---------------------------------------------------------------------------
       
   704 // CBSStorageManager::FileElementHandleL
       
   705 // ---------------------------------------------------------------------------
       
   706 //
       
   707 void CBSStorageManager::FileElementHandleL( const TDesC& aAppId,
       
   708 						     const TDesC& aBrandId,
       
   709 						     const TDesC& aFileName,
       
   710 						     const TDesC& aLanguageBuf,
       
   711 						     RFile& aFile )
       
   712 	{
       
   713     TRACE( T_LIT( "CBSStorageManager::FileElementHandleL begin AppId[%S],BrandId[%S],Filename[%S]"),&aAppId, &aBrandId, &aFileName );
       
   714 // Modified file name format: "r[n].filename.ext",
       
   715 // where "n" is the language code.
       
   716 // Format used to be "filename.ext.r[n]".
       
   717     HBufC* internalFn = HBufC::NewLC( aFileName.Length() 
       
   718         + KBSFileLangSuffix().Length()
       
   719         + aLanguageBuf.Length()
       
   720         + KDot().Length() );
       
   721     TPtr pInternalFn( internalFn->Des() );    
       
   722     pInternalFn.Append( KBSFileLangSuffix() );
       
   723     pInternalFn.Append( aLanguageBuf );	
       
   724     pInternalFn.Append( KDot() );
       
   725     pInternalFn.Append( aFileName );
       
   726     
       
   727     TInt length = aAppId.Length() 
       
   728         + aBrandId.Length() 
       
   729         + pInternalFn.Length()  
       
   730         + ( KDirSeparator().Length() * 3 )
       
   731         + KFileElementStore().Length();
       
   732                   
       
   733 	HBufC* fileName = HBufC::NewL( length ); // replaced NewLC with NewL
       
   734 	
       
   735     TPtr filePtr( fileName->Des() );
       
   736 
       
   737     // [application_id]\[brand_id]\files\[aFilename]
       
   738     //
       
   739     filePtr.Append( aAppId );
       
   740     filePtr.Append( KDirSeparator );
       
   741     filePtr.Append( aBrandId );
       
   742     filePtr.Append( KDirSeparator );
       
   743     filePtr.Append( KFileElementStore );
       
   744     filePtr.Append( KDirSeparator );
       
   745     filePtr.Append( pInternalFn ); 
       
   746 
       
   747     CleanupStack::PopAndDestroy( internalFn );
       
   748     internalFn = NULL;
       
   749     pInternalFn.Set( 0, 0, 0 );
       
   750     
       
   751     CleanupStack::PushL( fileName );
       
   752 	HBufC* fullname = FullFileNameLC( filePtr );
       
   753     
       
   754 // Make a TPtr instead of TFileName because after mod, "fullPtr"
       
   755 // does not need to be passed to BaflUtils::NearestLanguageFile()
       
   756 // old implementation:  TFileName fullPtr = fullname->Des();
       
   757     TPtr fullPtr( fullname->Des() );
       
   758     
       
   759 	
       
   760 //  A few thoughts about language suffix of files:
       
   761 // NOTE: NOT about brand file (e.g. "brandfile.bin.r99" ),
       
   762 // but about other files which belong to the branding package, 
       
   763 // since language suffix added by branding server to a
       
   764 // mif-file is not acceptable later when calling 
       
   765 // AknIconUtils::CreateIconLC() on the mif-file.
       
   766 // Skipped call to BaflUtils::NearestLanguageFile() because "XSP MOD" 
       
   767 // moves language code from suffix to prefix.
       
   768 // NearestLanguageFile()-call could be replaced by an op where 
       
   769 // manager seeks a file only from c and z, or from all drives,
       
   770 // but does so only for file matching to current or requested  
       
   771 // language. Meaning will not try to get the nearest language 
       
   772 // file but the matching one.
       
   773 // If nearest language file is actually needed, then 
       
   774 // manager could for example implement a seek-function similar to 
       
   775 // BaflUtils::NearestLanguageFile(), but with the difference that
       
   776 // it will inspect file prefix instead of suffix.
       
   777 
       
   778 
       
   779     RFile handle;
       
   780 // RFs::ShareProtected() needs to be called for the Fs-session 
       
   781 // used to open the handle that is about to be shared.
       
   782 // ShareProtected() was called to a wrong Fs session in 
       
   783 // CBSBrandHandler::GetFileL()!
       
   784 	TInt err = handle.Open( iFs, fullPtr, EFileShareReadersOnly );
       
   785 	if( err == KErrPathNotFound )
       
   786 		{
       
   787 		// not found, we try forcing c-drive
       
   788 		HBufC* driveForced  = FullFileNameLC( filePtr, ETrue );
       
   789 		
       
   790 		TFileName forcedPtr = driveForced->Des();
       
   791 
       
   792 		
       
   793 		err = handle.Open( iFs, forcedPtr, EFileShareReadersOnly );
       
   794 		User::LeaveIfError( err );
       
   795 		// this file was found, we can use this
       
   796 		CleanupStack::Pop( driveForced );
       
   797 		CleanupStack::PopAndDestroy( fullname );
       
   798 		fullname = driveForced;
       
   799 		CleanupStack::PushL( fullname );
       
   800 		}
       
   801 	else
       
   802 		{
       
   803 		User::LeaveIfError( err );
       
   804 		}
       
   805 
       
   806     CleanupStack::PopAndDestroy( 2, fileName ); // fullname, fileName
       
   807     aFile = handle;
       
   808 	TRACE( T_LIT( "CBSStorageManager::FileElementHandleL end") );
       
   809 	}
       
   810 	
       
   811 	
       
   812 // ---------------------------------------------------------------------------
       
   813 // CBSStorageManager::FullFileNameLC
       
   814 // ---------------------------------------------------------------------------
       
   815 //
       
   816 HBufC* CBSStorageManager::FullFileNameLC( const TDesC& aFilename, TBool aForceCDrive /*= EFalse*/ )
       
   817 	{
       
   818     // get the private path
       
   819     TBuf<KMaxPath> path;
       
   820     User::LeaveIfError( iFs.PrivatePath( path ) );
       
   821 
       
   822     // construct absolute filename
       
   823     HBufC* fullname = HBufC::NewLC( KBSCDrive().Length() + aFilename.Length() + path.Length() + 
       
   824     								KBSDataStore().Length() + KDirSeparator().Length() );
       
   825     TPtr fullPtr( fullname->Des() );
       
   826     fullPtr.Append( KBSCDrive );
       
   827     fullPtr.Append( path );
       
   828     fullPtr.Append( KBSDataStore );
       
   829     fullPtr.Append( KDirSeparator );
       
   830     fullPtr.Append( aFilename );
       
   831     
       
   832 	TParse filename;
       
   833 	iFs.Parse( fullPtr, filename );
       
   834 	
       
   835 	if( !aForceCDrive )
       
   836 		{
       
   837 		// C-drive not explicitely requested
       
   838 		if( !BaflUtils::FileExists( iFs, fullPtr ) )
       
   839 			{
       
   840 			// the file does not exist in C-drive, so 
       
   841 			// we have to return a path to Z-drive
       
   842 			fullPtr.Zero();
       
   843 		    fullPtr.Append( KBSZDrive );
       
   844 		    fullPtr.Append( path );
       
   845 		    fullPtr.Append( KBSDataStore );
       
   846 		    fullPtr.Append( KDirSeparator );
       
   847 		    fullPtr.Append( aFilename );
       
   848 			}
       
   849 		}
       
   850 	
       
   851     return fullname;
       
   852 	}
       
   853 	
       
   854 // ---------------------------------------------------------------------------
       
   855 // CBSStorageManager::CleanupFileL
       
   856 // ---------------------------------------------------------------------------
       
   857 //
       
   858 void CBSStorageManager::CleanupFileL( const TDesC& aFileName )
       
   859 	{
       
   860 	TRACE( T_LIT( "CBSStorageManager::CleanupFileL begin") );
       
   861 	// we have a versioned file we need to clean up
       
   862 	// get the file name without the version information
       
   863 	if( !IsBaseFileL( aFileName) )
       
   864 		{
       
   865 		// this is not a basefile
       
   866 		// we need to check if basefile should be replaced
       
   867 		// with this one
       
   868 		TPtrC baseFile = FilenameWithoutVersion( aFileName );
       
   869 		TInt version = ReadVersionL( aFileName );
       
   870 		TInt baseVersion = ReadVersionL( baseFile );
       
   871 		if( version > baseVersion )
       
   872 			{
       
   873 			// newer version --> replace basefile
       
   874 			TInt err = iFs.Replace( aFileName, baseFile);
       
   875 			User::LeaveIfError( err );
       
   876 			}
       
   877 		else
       
   878 			{
       
   879 			// same or older version --> delete
       
   880 			TInt err = iFs.Delete( aFileName );
       
   881 			User::LeaveIfError( err );
       
   882 			}
       
   883 		}
       
   884 	else
       
   885 		{
       
   886 		// this is a basefile
       
   887 		// we need to check if there are any newer versioned files
       
   888 		// that are not in use
       
   889 		
       
   890 		TParse filename;
       
   891 		iFs.Parse( aFileName, filename );
       
   892 		TPtrC driveAndPath = filename.DriveAndPath();
       
   893 		
       
   894 		HBufC* nameWithDrive = HBufC::NewLC( aFileName.Length() + KBSCDrive().Length() );
       
   895 		TPtr nameWithDrivePtr = nameWithDrive->Des();
       
   896 
       
   897 		nameWithDrivePtr.Append( aFileName );
       
   898 		
       
   899 		CDir* files = NULL;
       
   900 		CDir* directories = NULL;
       
   901 		
       
   902 		User::LeaveIfError( iFs.GetDir( driveAndPath, KEntryAttNormal, ESortByName, files, directories ) );
       
   903 
       
   904 		CleanupStack :: PushL (files);
       
   905 		CleanupStack :: PushL (directories);
       
   906 		
       
   907 		TInt count = files->Count();
       
   908 		for( TInt i = 0; i < count; i++ )
       
   909 			{
       
   910 			const TEntry file = (*files)[ i ];
       
   911 			TBufC<KMaxFileName> name = file.iName;
       
   912 			
       
   913 			HBufC* fullName = HBufC::NewLC( driveAndPath.Length() + name.Length() );
       
   914 			TPtr fullPtr = fullName->Des();
       
   915 			
       
   916 			fullPtr.Append( driveAndPath );
       
   917 			fullPtr.Append( name );
       
   918 			
       
   919 			TPtrC strippedName = FilenameWithoutVersion( *fullName );
       
   920 			
       
   921 			if( 0 == strippedName.Compare( nameWithDrivePtr ) )
       
   922 				{
       
   923 				// there is a versioned file, that might be newer
       
   924 				// than the basefile
       
   925 				TInt baseVersion = ReadVersionL( nameWithDrivePtr );
       
   926 				
       
   927 				TInt versionedVersion = ReadVersionL( fullPtr );
       
   928 				if( versionedVersion > baseVersion )
       
   929 					{
       
   930 					// versioned file newer than base file
       
   931 					// check if the versioned file is still in use
       
   932 					if( !iSession->Server()->FileStillInUse( iSession, fullPtr ) )
       
   933 						{
       
   934 						// the file is not in use, we can replace the basefile
       
   935 						// with the versioned file
       
   936 						TInt err = iFs.Replace( fullPtr, nameWithDrivePtr );
       
   937 						User::LeaveIfError( err );
       
   938 						}
       
   939 					}
       
   940 				}
       
   941 			CleanupStack::PopAndDestroy( fullName );
       
   942 			}
       
   943 		
       
   944 		CleanupStack :: PopAndDestroy (directories);
       
   945 		CleanupStack :: PopAndDestroy (files);
       
   946 		CleanupStack :: PopAndDestroy (nameWithDrive);
       
   947 		}
       
   948 	TRACE( T_LIT( "CBSStorageManager::CleanupFileL end") );
       
   949 	}
       
   950 
       
   951 // ---------------------------------------------------------------------------
       
   952 // CBSStorageManager::FilenameWithoutVersionL
       
   953 // ---------------------------------------------------------------------------
       
   954 //
       
   955 TPtrC CBSStorageManager::FilenameWithoutVersion( const TDesC& aFileName )
       
   956 	{
       
   957 	TParse filename;
       
   958 	iFs.Parse( aFileName, filename );
       
   959 	TPtrC extension = filename.Ext();
       
   960 	TPtrC strippedName = aFileName.Left( aFileName.Length() - ( extension.Length() ) );
       
   961 	return strippedName;
       
   962 	}
       
   963 
       
   964 
       
   965 // ---------------------------------------------------------------------------
       
   966 // CBSStorageManager::IsBaseFileL
       
   967 // ---------------------------------------------------------------------------
       
   968 //
       
   969 TBool CBSStorageManager::IsBaseFileL( const TDesC& aFileName )
       
   970 	{
       
   971 	TBool returnValue = ETrue;
       
   972 	TPtrC fileName = FilenameWithoutVersion( aFileName );
       
   973 	if( BaflUtils::FileExists( iFs, fileName ) )
       
   974 		{
       
   975 		// file with one extension stripped out exists
       
   976 		// --> the file was not a base file
       
   977 		returnValue = EFalse;
       
   978 		}
       
   979 	return returnValue;
       
   980 	}
       
   981 
       
   982 // ---------------------------------------------------------------------------
       
   983 // CBSStorageManager::InstallFile
       
   984 // ---------------------------------------------------------------------------
       
   985 //
       
   986 HBufC* CBSStorageManager::InstallFileLC( const TDesC& aFileName )
       
   987 	{
       
   988 	TRACE( T_LIT( "CBSStorageManager::InstallFileLC begin") );
       
   989 	if( !iAppId || !iBrandId )
       
   990 		{
       
   991 		User::Leave( KErrNotReady );
       
   992 		}
       
   993 	TParse filename;
       
   994 	iFs.Parse( aFileName, filename );
       
   995 	
       
   996     TInt length = iAppId->Length() + iBrandId->Length() +
       
   997                   aFileName.Length() + KDirSeparator().Length() * 3
       
   998                   + KFileElementStore().Length() + iLanguageId->Length()
       
   999                   + KDot().Length() + KBSFileLangSuffix().Length();
       
  1000                   
       
  1001 	HBufC* fileName = HBufC::NewLC( length );
       
  1002 	
       
  1003     TPtr filePtr( fileName->Des() );
       
  1004 
       
  1005     filePtr.Append( *iAppId );
       
  1006     filePtr.Append( KDirSeparator );
       
  1007     filePtr.Append( *iBrandId );
       
  1008     filePtr.Append( KDirSeparator );
       
  1009     filePtr.Append( KFileElementStore );
       
  1010     filePtr.Append( KDirSeparator );
       
  1011     filePtr.Append( KBSFileLangSuffix() );
       
  1012 	filePtr.Append( *iLanguageId );
       
  1013     filePtr.Append( KDot() ); 
       
  1014     filePtr.Append( filename.NameAndExt() ); 
       
  1015 	
       
  1016 	HBufC* fullname  = FullFileNameLC( filePtr, ETrue );
       
  1017     
       
  1018     TParse directory;
       
  1019     iFs.Parse( *fullname, directory );
       
  1020     TPtrC ptr = directory.DriveAndPath();
       
  1021     TInt dirError = iFs.MkDirAll( directory.DriveAndPath() );
       
  1022     if( dirError == KErrAlreadyExists )
       
  1023     	{
       
  1024     	// ignore already exists
       
  1025     	dirError = KErrNone;
       
  1026     	}
       
  1027     User::LeaveIfError( dirError );
       
  1028     
       
  1029     CFileMan* fileMan = CFileMan::NewL( iFs );
       
  1030     CleanupStack::PushL( fileMan );
       
  1031     
       
  1032     // copy the file, no overwriting
       
  1033 	TInt err = fileMan->Copy( aFileName, *fullname, 1 );
       
  1034 	User::LeaveIfError( err );
       
  1035 		
       
  1036 #ifdef __WINSCW__
       
  1037         // Add to IBY file
       
  1038         iWriter->SetFileItemL( *fullname, *fullname );
       
  1039 #endif
       
  1040 
       
  1041     CleanupStack::PopAndDestroy( 3, fileName ); // fileMan, fullname, fileName	
       
  1042 
       
  1043 
       
  1044 	HBufC* returnValue = filename.NameAndExt().AllocLC();
       
  1045 	TRACE( T_LIT( "CBSStorageManager::InstallFileLC end") );
       
  1046 	return returnValue;
       
  1047 	}
       
  1048 
       
  1049 
       
  1050 // ---------------------------------------------------------------------------
       
  1051 // CBSStorageManager::RemoveBrandL
       
  1052 // ---------------------------------------------------------------------------
       
  1053 //
       
  1054 void CBSStorageManager::RemoveBrandL(  const TDesC& aAppId,
       
  1055 									   const TDesC& aBrandId,
       
  1056 									   TLanguage aLanguageId,
       
  1057 									   TInt aReserved )
       
  1058 	{
       
  1059     TRACE( T_LIT( "CBSStorageManager::RemoveBrandL begin") );
       
  1060     HBufC* fileName = ConstructFileNameL( aAppId,
       
  1061 						       			  aBrandId,
       
  1062 						       			  aLanguageId,
       
  1063 						       			  aReserved );
       
  1064 	CleanupStack::PushL( fileName );
       
  1065 		
       
  1066     HBufC* fullname = FullFileNameLC( *fileName, ETrue );
       
  1067 
       
  1068 	TParse parseName;
       
  1069 	iFs.Parse( *fullname, parseName );
       
  1070 	
       
  1071 	TPtrC directory = parseName.DriveAndPath();
       
  1072 
       
  1073     CFileMan* fileMan = CFileMan::NewL( iFs );
       
  1074     CleanupStack::PushL( fileMan );
       
  1075     
       
  1076     // delete all files under this brand and language
       
  1077 	TInt err = fileMan->RmDir( directory );
       
  1078 	TRACE( T_LIT( "CBSStorageManager::RemoveBrandL dir='%S' err=%d "), &directory, err );
       
  1079 	
       
  1080 	//in case the removal of the entire brand failed because the brand was 
       
  1081 	//in use, we set this brand as discarded (since parts of the package 
       
  1082 	//might have been removed)
       
  1083     if ( KErrInUse  == err ) 
       
  1084         {
       
  1085         MarkBrandAsDiscardedL( aAppId, aBrandId );
       
  1086         }
       
  1087 	
       
  1088 	User::LeaveIfError( err );
       
  1089 
       
  1090 	CleanupStack::PopAndDestroy( 3, fileName );
       
  1091 	TRACE( T_LIT( "CBSStorageManager::RemoveBrandL end ") );
       
  1092 	}
       
  1093 
       
  1094 // ---------------------------------------------------------------------------
       
  1095 // CBSStorageManager::RemoveApplicationL
       
  1096 // ---------------------------------------------------------------------------
       
  1097 //
       
  1098 void CBSStorageManager::RemoveApplicationL( const TDesC& aAppId )
       
  1099 	{
       
  1100     TRACE( T_LIT( "CBSStorageManager::RemoveApplicationL begin") );
       
  1101     TInt length = aAppId.Length() + 
       
  1102                   KDefFileName().Length() + KDirSeparator().Length();
       
  1103                   
       
  1104 
       
  1105 	HBufC* fileName = HBufC::NewLC( length );
       
  1106     
       
  1107     TPtr file( fileName->Des() );
       
  1108 
       
  1109     file.Append( aAppId );
       
  1110     file.Append( KDirSeparator );
       
  1111     
       
  1112     HBufC* fullName = FullFileNameLC( *fileName, ETrue );
       
  1113     
       
  1114     CFileMan* fileMan = CFileMan::NewL( iFs );
       
  1115     CleanupStack::PushL( fileMan );
       
  1116     
       
  1117     // delete all files under this brand and language
       
  1118 	TInt err = fileMan->RmDir( *fullName );
       
  1119 	User::LeaveIfError( err ); 
       
  1120     
       
  1121     CleanupStack::PopAndDestroy( 3, fileName );
       
  1122     TRACE( T_LIT( "CBSStorageManager::RemoveApplicationL end") );
       
  1123 	}
       
  1124 
       
  1125 
       
  1126 // ---------------------------------------------------------------------------
       
  1127 // CBSStorageManager::NextAvailableVersionL
       
  1128 // ---------------------------------------------------------------------------
       
  1129 //
       
  1130 TInt CBSStorageManager::NextAvailableVersionL()
       
  1131 	{
       
  1132 	TRACE( T_LIT( "CBSStorageManager::NextAvailableVersionL begin") );
       
  1133 	TInt returnValue = 0;
       
  1134 	
       
  1135 	if( iAppId && iBrandId )
       
  1136 		{
       
  1137 		TInt newVersion = 0;
       
  1138 		TRAPD( err, newVersion = GetNewestVersionL( *iAppId, *iBrandId, iLanguageIdNum ) );
       
  1139 		if( err == KErrNotFound )
       
  1140 			{
       
  1141 			// ignore not found -> we default to 1
       
  1142 			err = KErrNone;
       
  1143 			returnValue = iReserved;
       
  1144 			}
       
  1145 		else
       
  1146 			{
       
  1147 			returnValue = newVersion + 1;
       
  1148 			}
       
  1149 		User::LeaveIfError( err );
       
  1150 		}
       
  1151     TRACE( T_LIT( "CBSStorageManager::NextAvailableVersionL end") );
       
  1152 	return returnValue;
       
  1153 	}
       
  1154 
       
  1155 
       
  1156 // ---------------------------------------------------------------------------
       
  1157 // CBSStorageManager::NextAvailableVersionL
       
  1158 // ---------------------------------------------------------------------------
       
  1159 //
       
  1160 void CBSStorageManager::SetVersion( TInt aReserved )
       
  1161 	{
       
  1162 	iReserved = aReserved;
       
  1163 	}
       
  1164 
       
  1165 
       
  1166 // ---------------------------------------------------------------------------
       
  1167 // CBSStorageManager::GetCurrentVersionL
       
  1168 // ---------------------------------------------------------------------------
       
  1169 //
       
  1170 TInt CBSStorageManager::GetNewestVersionL( const TDesC& aAppId,
       
  1171 										   const TDesC& aBrandId,
       
  1172 										   TLanguage aLanguageId,
       
  1173 										   TPtr* aDrive /*= KNullDesC()*/ )
       
  1174 	{
       
  1175     TRACE( T_LIT( "CBSStorageManager::GetNewestVersionL begin") );
       
  1176     HBufC* fileName = ConstructFileNameL( aAppId, aBrandId, aLanguageId );
       
  1177 	CleanupStack::PushL( fileName );
       
  1178 
       
  1179 
       
  1180 	// FullFileNameLC returns the file from C-drive if it exists there
       
  1181 	// and if not, then from ROM
       
  1182     HBufC* fullFileName = FullFileNameLC( *fileName );
       
  1183 
       
  1184 	TParse parse;
       
  1185 	iFs.Parse( *fullFileName, parse );
       
  1186 	TPtrC driveAndPath = parse.DriveAndPath();
       
  1187 	
       
  1188 	CDir* files = NULL;
       
  1189 	CDir* directories = NULL;
       
  1190 	
       
  1191 	TInt err = iFs.GetDir( driveAndPath, KEntryAttNormal, ESortByName, files, directories );
       
  1192 	if(KErrNone == err )
       
  1193 		{
       
  1194 		if(aDrive)
       
  1195 			{
       
  1196 			(*aDrive).Copy(KBSZDrive);	
       
  1197 			}
       
  1198 	    
       
  1199 		}
       
  1200 	if( err == KErrPathNotFound )
       
  1201 		{
       
  1202 		// not found from Z-drive, try from C-drive
       
  1203 		CleanupStack::PopAndDestroy( fullFileName );
       
  1204 		fullFileName = FullFileNameLC( *fileName, ETrue );
       
  1205 		driveAndPath.Set( parse.DriveAndPath() );
       
  1206 		err = iFs.GetDir( driveAndPath, KEntryAttNormal, ESortByName, files, directories );
       
  1207 		if(KErrNone == err )
       
  1208 			{
       
  1209 			if(aDrive)
       
  1210 				{
       
  1211 			    (*aDrive).Copy(KBSCDrive);
       
  1212 				}
       
  1213 			}
       
  1214 		if( err == KErrPathNotFound )
       
  1215 			{
       
  1216 			// switching error code to make it more clear
       
  1217 			err = KErrNotFound;
       
  1218 			}
       
  1219 		// as we are leaving here cleanup the stack
       
  1220 		CleanupStack::PopAndDestroy( 2, fileName ); // fullFileName, fileName
       
  1221 		User::LeaveIfError( err );
       
  1222 		}
       
  1223 	
       
  1224 	// get the base file version
       
  1225 	TInt returnValue = ReadVersionL( *fullFileName );
       
  1226 	
       
  1227 	TInt count = files->Count();
       
  1228 	for( TInt i = 0; i < count; i++ )
       
  1229 		{
       
  1230 		const TEntry file = (*files)[ i ];
       
  1231 		TBufC<KMaxFileName> name = file.iName;
       
  1232 		
       
  1233 		HBufC* fullName = HBufC::NewLC( driveAndPath.Length() + name.Length() );
       
  1234 		TPtr fullPtr = fullName->Des();
       
  1235 		
       
  1236 		fullPtr.Append( driveAndPath );
       
  1237 		fullPtr.Append( name );
       
  1238 		
       
  1239 		TPtrC strippedName = FilenameWithoutVersion( *fullName );
       
  1240 		
       
  1241 		if( 0 == strippedName.Compare( *fullFileName ) )
       
  1242 			{
       
  1243 			// there is a versioned file, that might be newer
       
  1244 			// than the basefile
       
  1245 			TInt versionedVersion = ReadVersionL( fullPtr );
       
  1246 			if( versionedVersion > returnValue )
       
  1247 				{
       
  1248 				// versioned file newer than base file
       
  1249 				returnValue = versionedVersion;
       
  1250 				}
       
  1251 			}
       
  1252 		CleanupStack::PopAndDestroy( fullName );
       
  1253 		}
       
  1254 
       
  1255 	CleanupStack::PopAndDestroy( 2, fileName ); // fullFileName, fileName
       
  1256     
       
  1257     TRACE( T_LIT( "CBSStorageManager::GetNewestVersionL end") );
       
  1258 	return returnValue;
       
  1259 	}
       
  1260 
       
  1261 
       
  1262 // ---------------------------------------------------------------------------
       
  1263 // CBSStorageManager::WriteIbyFiles
       
  1264 // ---------------------------------------------------------------------------
       
  1265 //
       
  1266 TInt CBSStorageManager::WriteIbyFiles()
       
  1267 	{
       
  1268 	TInt err = KErrNone;
       
  1269 #ifdef __WINSCW__	
       
  1270 	if( !iWriter )
       
  1271 		{
       
  1272 		return KErrNone;
       
  1273 		}
       
  1274 
       
  1275     TRAP( err, iWriter->WriteIBYFileL( iFs, *iIbyFile ) );
       
  1276     delete iIbyFile;
       
  1277     iIbyFile = NULL;
       
  1278     delete iWriter;
       
  1279     iWriter = NULL;
       
  1280 	
       
  1281 #endif	
       
  1282 	return err;
       
  1283 	}
       
  1284 
       
  1285 // ---------------------------------------------------------------------------
       
  1286 // CBSStorageManager::LockStorage
       
  1287 // ---------------------------------------------------------------------------
       
  1288 //
       
  1289 void CBSStorageManager::LockStorage()
       
  1290     {
       
  1291     TRACE( T_LIT( "CBSStorageManager::LockStorage begin") );
       
  1292     // This will disable all filehandles, so that clients can't use them
       
  1293     iFs.Close();
       
  1294     TRACE( T_LIT( "CBSStorageManager::LockStorage end") );
       
  1295     }
       
  1296     
       
  1297 // ---------------------------------------------------------------------------
       
  1298 // CBSStorageManager::ReleaseLockL
       
  1299 // ---------------------------------------------------------------------------
       
  1300 //
       
  1301 void CBSStorageManager::ReleaseLockL()
       
  1302     {
       
  1303     TRACE( T_LIT( "CBSStorageManager::ReleaseLockL begin") );
       
  1304     User::LeaveIfError( iFs.Connect() );
       
  1305     TRACE( T_LIT( "CBSStorageManager::ReleaseLockL end") );
       
  1306     }
       
  1307     
       
  1308 // ---------------------------------------------------------------------------
       
  1309 // CBSStorageManager::CheckBrandDiscarded
       
  1310 // ---------------------------------------------------------------------------
       
  1311 //
       
  1312 TBool CBSStorageManager::CheckBrandDiscarded(const TDesC& aAppId,
       
  1313 							const TDesC& aBrandId)
       
  1314 {
       
  1315 	TBool flag = EFalse ;
       
  1316 	
       
  1317 	HBufC *fileName = ConstructDiscardBrandFileNameL(aAppId, aBrandId) ;
       
  1318 	CleanupStack::PushL(fileName);
       
  1319 	HBufC *fullPath = FullDiscardBrandFileNameLC( *fileName ) ;
       
  1320 	
       
  1321 	// crate a flag file
       
  1322 	RFile file;
       
  1323 	TInt err = file.Open(iFs, *fullPath, EFileWrite);
       
  1324 	if(err == KErrNone)
       
  1325 	{
       
  1326 		// file exists.
       
  1327 		file.Close() ;
       
  1328 		flag = ETrue ;
       
  1329 	}
       
  1330 	
       
  1331 	CleanupStack::PopAndDestroy(fullPath) ;
       
  1332 	CleanupStack::PopAndDestroy(fileName) ;
       
  1333 	return flag ;
       
  1334 }
       
  1335 
       
  1336 // ---------------------------------------------------------------------------
       
  1337 // CBSStorageManager::ConstructDiscardBrandFileNameL
       
  1338 // ---------------------------------------------------------------------------
       
  1339 //
       
  1340 HBufC* CBSStorageManager::ConstructDiscardBrandFileNameL( const TDesC& aAppId,
       
  1341 										      const TDesC& aBrandId )
       
  1342     {
       
  1343 	HBufC* fileName = NULL;
       
  1344 
       
  1345     TInt length = aAppId.Length() + aBrandId.Length() +
       
  1346                   KDiscardBrandFileName().Length() + KDirSeparator().Length() * 2 ;
       
  1347 	fileName = HBufC::NewLC( length );
       
  1348     
       
  1349     TPtr file( fileName->Des() );
       
  1350 
       
  1351     // [application_id]\[brand_id]\discardedbrand.txt
       
  1352     file.Append( aAppId );
       
  1353     file.Append( KDirSeparator );
       
  1354     file.Append( aBrandId );
       
  1355     file.Append( KDirSeparator );
       
  1356     file.Append( KDiscardBrandFileName );
       
  1357 
       
  1358     if( fileName )
       
  1359     	{
       
  1360     	CleanupStack::Pop( fileName );
       
  1361     	}
       
  1362     	
       
  1363     return fileName;
       
  1364     }
       
  1365     
       
  1366 // ---------------------------------------------------------------------------
       
  1367 // CBSStorageManager::FullDiscardBrandFileNameLC
       
  1368 // ---------------------------------------------------------------------------
       
  1369 //
       
  1370 HBufC* CBSStorageManager::FullDiscardBrandFileNameLC( TDesC& aFilename )
       
  1371 	{
       
  1372     // get the private path
       
  1373     TBuf<KMaxPath> path;
       
  1374     RFs fs ;
       
  1375     User::LeaveIfError( fs.Connect() );
       
  1376     CleanupClosePushL(fs);
       
  1377     User::LeaveIfError( fs.PrivatePath( path ) );
       
  1378 
       
  1379     // construct absolute filename
       
  1380     HBufC* fullname = HBufC::NewLC( KBSCDrive().Length() + aFilename.Length() + path.Length() + 
       
  1381     								KBSDataStore().Length() + KDirSeparator().Length() );
       
  1382     TPtr fullPtr( fullname->Des() );
       
  1383     fullPtr.Append( KBSCDrive );
       
  1384     fullPtr.Append( path );
       
  1385     fullPtr.Append( KBSDataStore );
       
  1386     fullPtr.Append( KDirSeparator );
       
  1387     fullPtr.Append( aFilename );
       
  1388     
       
  1389 	CleanupStack::Pop(); // fullname
       
  1390 	CleanupStack::PopAndDestroy() ; //fs
       
  1391 	CleanupStack::PushL(fullname) ;
       
  1392 	
       
  1393     return fullname;
       
  1394 	}
       
  1395 
       
  1396 // ---------------------------------------------------------------------------
       
  1397 // CBSStorageManager::MarkBrandAsDiscardedL
       
  1398 // ---------------------------------------------------------------------------
       
  1399 //
       
  1400 void CBSStorageManager::MarkBrandAsDiscardedL( const TDesC& aAppId,
       
  1401 			  			                       const TDesC& aBrandId )
       
  1402     {
       
  1403 	TRACE( T_LIT( "CBSStorageManager::MarkBrandAsDiscardedL begin app='%S' brand='%S'"), &aAppId, &aBrandId );
       
  1404 	
       
  1405 	HBufC *fileName = ConstructDiscardBrandFileNameL( aAppId, aBrandId ) ;
       
  1406 	CleanupStack::PushL( fileName );
       
  1407 	HBufC *fullPath = FullDiscardBrandFileNameLC( *fileName ) ;
       
  1408     TRACE( T_LIT( "CBSStorageManager::MarkBrandAsDiscardedL discardedfile='%S'"), &fullPath );
       
  1409 	
       
  1410 	// create flag file
       
  1411 	RFile file;	
       
  1412 	TInt err = file.Replace( iFs, *fullPath, EFileWrite );
       
  1413 	TRACE( T_LIT( "CBSStorageManager::MarkBrandAsDiscardedL discarded file creation result='%d'"), err );
       
  1414 	file.Close();
       
  1415 	
       
  1416 	CleanupStack::PopAndDestroy( fullPath );
       
  1417 	fullPath = NULL;
       
  1418 	CleanupStack::PopAndDestroy( fileName );
       
  1419 	fileName = NULL;
       
  1420 	
       
  1421 	TRACE( T_LIT( "CBSStorageManager::MarkBrandAsDiscardedL begin") );	
       
  1422     }
       
  1423 
       
  1424 // End of file