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