brandingserver/bsserver/cbssession.cpp
changeset 51 61fad867f68e
equal deleted inserted replaced
-1:000000000000 51:61fad867f68e
       
     1 /*
       
     2 * Copyright (c) 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: CBSSession.cpp
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 #include <apgcli.h>
       
    22 
       
    23 #include "cbssession.h"
       
    24 #include "cbsserver.h"
       
    25 #include "debugtrace.h"
       
    26 #include "cbsbrandhandler.h"
       
    27 #include "cbsstoragemanager.h"
       
    28 #include "cbsstorage.h"
       
    29 #include "cbsbitmap.h"
       
    30 //#include "cbselement.h"
       
    31 #include "rbsobjowningptrarray.h"
       
    32 #include "bselementfactory.h"
       
    33 #include "bsimportconstants.h"
       
    34 #include <badesca.h>
       
    35 #include <s32buf.h>
       
    36 #include <s32mem.h>
       
    37 #include <utf.h>
       
    38 #include <e32property.h>
       
    39 
       
    40 // ==============================================================
       
    41 // ======================== SESSION =============================
       
    42 // ==============================================================
       
    43 
       
    44 // Two-phased constructor.
       
    45 CBSSession* CBSSession::NewL()
       
    46     {
       
    47     TRACE( T_LIT("CBSSession::NewL begin") );
       
    48     CBSSession* self = new( ELeave ) CBSSession();
       
    49     CleanupStack::PushL( self );
       
    50     self->ConstructL();
       
    51     CleanupStack::Pop( self );
       
    52     TRACE( T_LIT("CBSSession::NewL end") );
       
    53     return self;
       
    54     }
       
    55 
       
    56 // Symbian OS default constructor can leave.
       
    57 void CBSSession::ConstructL()
       
    58 	{
       
    59 	}
       
    60 
       
    61 
       
    62 // destructor
       
    63 CBSSession::~CBSSession()
       
    64     {
       
    65     TRACE( T_LIT("CBSSession[%d]::~CBSSession()"), this );
       
    66 
       
    67     if( Server() )
       
    68         {
       
    69         Server()->SessionDied( this );
       
    70         }
       
    71 	delete iText;
       
    72 	delete iBuffer;
       
    73 	delete iBranding;
       
    74 	delete iSeveralData;
       
    75 
       
    76 	delete iApplicationId;
       
    77 	delete iBrandId;
       
    78 	delete iDefaultBrandId;
       
    79 	
       
    80 #ifdef __WINSCW__
       
    81 	if( iStorageManager )
       
    82 		{
       
    83 		iStorageManager->WriteIbyFiles();
       
    84 		}
       
    85 #endif
       
    86 	delete iStorageManager;
       
    87     delete iSessionInfo;
       
    88 
       
    89     }
       
    90 
       
    91 
       
    92 // C++ default constructor can NOT contain any code, that
       
    93 // might leave.
       
    94 //
       
    95 CBSSession::CBSSession()
       
    96     {
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------
       
   100 // CBSSession::CreateL
       
   101 //
       
   102 // (other items were commented in a header).
       
   103 // ---------------------------------------------------------
       
   104 //
       
   105 void CBSSession::CreateL()
       
   106     {
       
   107     TRACE( T_LIT("CBSSession[%d]::CreateL()"), this );
       
   108     Server()->SessionCreatedL( this );
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------
       
   112 // CBSSession::ServiceL
       
   113 //
       
   114 // (other items were commented in a header).
       
   115 // ---------------------------------------------------------
       
   116 //
       
   117 void CBSSession::ServiceL( const RMessage2 &aMessage )
       
   118     {
       
   119 
       
   120     /*if( !iSessionInfo )
       
   121         {
       
   122         TRAPD( err, ExtractInfoL( aMessage ) );
       
   123         TRACE( T_LIT("CBSSession::ServiceL() ExtractInfoL returned with [%d]"), err );
       
   124         }
       
   125 	*/
       
   126     if( DispatchMessageL( aMessage ) )
       
   127         {
       
   128         if( !iMessageCompleted )
       
   129         	{
       
   130         	aMessage.Complete( KErrNone );
       
   131         	}
       
   132         }
       
   133     }
       
   134 
       
   135 // ---------------------------------------------------------
       
   136 // CBSSession::ServiceError
       
   137 //
       
   138 // (other items were commented in a header).
       
   139 // ---------------------------------------------------------
       
   140 //
       
   141 void CBSSession::ServiceError( const RMessage2& aMessage,
       
   142                                       TInt aError )
       
   143     {
       
   144     aMessage.Complete( aError );
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------
       
   148 // CBSSession::HandleBackupStateL
       
   149 //
       
   150 // (other items were commented in a header).
       
   151 // ---------------------------------------------------------
       
   152 //
       
   153 void CBSSession::HandleBackupStateL( TBackupState aState )
       
   154     {
       
   155 	iIsBackup = ETrue ;	
       
   156     switch( aState )
       
   157         {
       
   158         case EBackupNotActive: // backup is complete
       
   159             {
       
   160             // release session lock
       
   161             iBackupActive = EFalse;
       
   162             if( iStorageManager )
       
   163                 {
       
   164                 iStorageManager->ReleaseLockL();
       
   165                 }
       
   166                 
       
   167 			// Sending backup active state change event after unlocking it
       
   168 			if( iObserverActive )
       
   169 				{
       
   170 				iObserverMessage.Complete( KErrNone );
       
   171 				iObserverActive = EFalse;
       
   172 				}
       
   173 
       
   174             break;
       
   175             }
       
   176         case EBackupActive: // backup activated
       
   177             {
       
   178 
       
   179             // Lock session. Branding data is not available until
       
   180             // backup is completed.
       
   181             iBackupActive = ETrue;
       
   182 
       
   183 			// Sending backup active state change event before locking it
       
   184 			if( iObserverActive )
       
   185 				{
       
   186 				iObserverMessage.Complete( KErrNone );
       
   187 				iObserverActive = EFalse;
       
   188 				}
       
   189 
       
   190             if( iStorageManager )
       
   191                 {
       
   192                 iStorageManager->LockStorage();
       
   193                 }
       
   194             break;
       
   195             }
       
   196         default:
       
   197             {
       
   198             // unknown state
       
   199             }
       
   200         }
       
   201 
       
   202         
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------
       
   206 // CBSSession::DispatchMessageL
       
   207 //
       
   208 // (other items were commented in a header).
       
   209 // ---------------------------------------------------------
       
   210 //
       
   211 TBool CBSSession::DispatchMessageL( const RMessage2& aMessage )
       
   212     {
       
   213     TRACE( T_LIT("CBrandingSession[%d]::DispatchMessageL() %d"),
       
   214                   this, aMessage.Function() );
       
   215 	iMessageCompleted = EFalse;
       
   216     TBool msgNeedsToBeCompleted = ETrue;
       
   217     
       
   218     if( iBackupActive )
       
   219         {
       
   220         // if backup is active, we don't take requests.
       
   221         // We could take some requests which do not require disk
       
   222         // operations. This is not a good solution and a better one should be
       
   223         // investigated. I'm running out of time.
       
   224         TInt msg = aMessage.Function() ; 
       
   225         if( msg == EBSObserveGetChange ||
       
   226         	msg == EBSObserveGetBackupState ||
       
   227         	msg == EBSObserveBrand)
       
   228         	{
       
   229         	//allow to do the operation	
       
   230         	}
       
   231         else
       
   232     	    {
       
   233 	       	User::Leave( KErrNotReady );	
       
   234         	}
       
   235         
       
   236         }
       
   237     
       
   238     switch( aMessage.Function() )
       
   239         {
       
   240         case EBSInitInstall:
       
   241         	{
       
   242         	InitUpdateL( aMessage, EBSTxInstall );
       
   243         	break;
       
   244         	}
       
   245         case EBSInitUninstall:
       
   246         	{
       
   247         	InitUpdateL( aMessage, EBSTxUninstall );
       
   248         	break;
       
   249         	}
       
   250         case EBSInitAppend:
       
   251         	{
       
   252         	InitUpdateL( aMessage, EBSTxAppend );
       
   253         	break;
       
   254         	}
       
   255         case EBSInitReplace:
       
   256         	{
       
   257         	InitUpdateL( aMessage, EBSTxReplace );
       
   258         	break;
       
   259         	}
       
   260         case EBSInitAccess:
       
   261         	{
       
   262         	InitAccessL( aMessage );
       
   263         	break;
       
   264         	}
       
   265         case EBSPrepareText:
       
   266             {
       
   267 			GetTextL( aMessage );
       
   268             break;
       
   269             }
       
   270         case EBSGetText:
       
   271             {
       
   272             if( iText )
       
   273             	{
       
   274             	aMessage.WriteL( 2, *iText );	
       
   275             	}
       
   276 			else
       
   277 				{
       
   278 				aMessage.WriteL( 2, KNullDesC() );
       
   279 				}
       
   280             break;
       
   281             }
       
   282 
       
   283 		case EBSIsBrandUpdateRequired:
       
   284 			isBrandUpdateRequiredL (aMessage);
       
   285 			break;
       
   286 
       
   287         case EBSGetInt:
       
   288             {
       
   289 			GetIntL( aMessage );
       
   290             break;
       
   291             }
       
   292 
       
   293         case EBSPrepareBuffer:
       
   294             {
       
   295 			GetBufferL( aMessage );
       
   296             break;
       
   297             }
       
   298             
       
   299         case EBSGetBuffer:
       
   300             {
       
   301             if( iBuffer )
       
   302             	{
       
   303             	aMessage.WriteL( 2, *iBuffer );	
       
   304             	}
       
   305 			else
       
   306 				{
       
   307 				aMessage.WriteL( 2, KNullDesC8() );
       
   308 				}
       
   309             break;
       
   310             }
       
   311             
       
   312         case EBSGetFile:
       
   313             {
       
   314  			GetFileL( aMessage );
       
   315             break;
       
   316             }
       
   317 
       
   318         case EBSPrepareSeveral:
       
   319         	{
       
   320 			PrepareSeveralL( aMessage );
       
   321         	break;
       
   322         	}
       
   323 
       
   324         case EBSGetSeveral:
       
   325         	{
       
   326 			GetSeveralL( aMessage );
       
   327         	break;
       
   328         	}
       
   329 
       
   330         case EBSPrepareStructure:
       
   331         	{
       
   332 			PrepareStructureL( aMessage );
       
   333         	break;
       
   334         	}
       
   335 
       
   336         case EBSGetStructure:
       
   337     		{
       
   338 			GetStructureL( aMessage );
       
   339     		break;
       
   340     		}
       
   341 
       
   342         case EBSStartTransaction:
       
   343         	{
       
   344 			StartTransactionL( aMessage );
       
   345         	break;
       
   346         	}
       
   347         	
       
   348         case EBSStopTransaction:
       
   349         	{
       
   350         	StopTransactionL( aMessage );
       
   351         	break;
       
   352         	}
       
   353         case EBSCancelTransaction:
       
   354         	{
       
   355         	CancelTransactionL( aMessage );
       
   356         	break;
       
   357         	}
       
   358 
       
   359         case EBSInstall:
       
   360         	{
       
   361         	InstallL( aMessage );
       
   362         	break;
       
   363         	}
       
   364 
       
   365         case EBSReplace:
       
   366         	{
       
   367         	ReplaceL( aMessage );
       
   368         	break;
       
   369         	}
       
   370 
       
   371         case EBSAppend:
       
   372         	{
       
   373         	AppendL( aMessage );
       
   374         	break;
       
   375         	}
       
   376 
       
   377         case EBSRemoveBrand:
       
   378         	{
       
   379         	RemoveBrandL( aMessage );
       
   380         	break;
       
   381         	}
       
   382         case EBSRemoveApplication:
       
   383         	{
       
   384         	RemoveApplicationL( aMessage );
       
   385         	break;
       
   386         	}
       
   387         	
       
   388         case EBSObserveBrand:
       
   389         	{
       
   390         	if( iObserverActive )
       
   391         		{
       
   392         		// complete the old observer request with KErrCancel
       
   393         		iObserverMessage.Complete( KErrCancel );
       
   394         		}
       
   395         	iObserverMessage = aMessage;
       
   396         	iObserverActive = ETrue;
       
   397         	msgNeedsToBeCompleted = EFalse;
       
   398         	break;
       
   399         	}
       
   400 
       
   401         case EBSObserveGetNewVersion:
       
   402         	{
       
   403         	TInt newVersion = GetNewVersionL();
       
   404 			TPckgC<TInt> pack( newVersion );
       
   405 			aMessage.WriteL( 0, pack );
       
   406         	break;
       
   407         	}
       
   408         	
       
   409 		// Get what change happen        	
       
   410         case EBSObserveGetChange:
       
   411         	{
       
   412 			TPckgC<TInt> pack( iIsBackup );
       
   413 			aMessage.WriteL( 0, pack );
       
   414         	break ;
       
   415         	}
       
   416         	
       
   417         // Get the backup state
       
   418         case EBSObserveGetBackupState:
       
   419         	{
       
   420 			TPckgC<TInt> pack( iBackupActive );
       
   421 			aMessage.WriteL( 0, pack );
       
   422         	break ;	
       
   423         	}
       
   424         	
       
   425         default:
       
   426             {
       
   427             User::Leave( KErrNotSupported );
       
   428             break;
       
   429             }
       
   430         }
       
   431 
       
   432     return msgNeedsToBeCompleted;
       
   433     }
       
   434 
       
   435 
       
   436 // ---------------------------------------------------------
       
   437 // CBSSession::InitUpdateL
       
   438 //
       
   439 // (other items were commented in a header).
       
   440 // ---------------------------------------------------------
       
   441 //
       
   442 void CBSSession::InitUpdateL( const RMessage2 &aMessage, TTransactionType aType)
       
   443 	{
       
   444     TRACE( T_LIT("CBSSession::InitUpdateL: begin TTransactionType[%d] "),aType );
       
   445 	
       
   446 	// get the application id
       
   447 	TInt bufferSize( aMessage.GetDesLength( 0 ) );
       
   448 
       
   449     HBufC8* appId = HBufC8::NewLC( bufferSize );
       
   450 	TPtr8 appPtr = appId->Des();
       
   451 	aMessage.ReadL( 0, appPtr );
       
   452 
       
   453 	HBufC* tmpAppId = CnvUtfConverter::ConvertToUnicodeFromUtf8L( *appId );
       
   454 	CleanupStack::PopAndDestroy( appId );
       
   455 	
       
   456 	delete iApplicationId;
       
   457 	iApplicationId = tmpAppId;
       
   458 
       
   459 	// get the brand id
       
   460 	bufferSize = aMessage.GetDesLength( 1 );
       
   461     HBufC8* streamBuf = HBufC8::NewLC( bufferSize );
       
   462 	TPtr8 streamPtr = streamBuf->Des();
       
   463 	aMessage.ReadL( 1, streamPtr );
       
   464 
       
   465 	RDesReadStream stream;
       
   466 	CleanupClosePushL( stream );
       
   467 	stream.Open( streamPtr );
       
   468 	
       
   469 	TInt descriptorLength = stream.ReadInt16L();
       
   470 	HBufC8* brandId = HBufC8::NewLC( descriptorLength );
       
   471 	TPtr8 brandPtr = brandId->Des();
       
   472 	stream.ReadL( brandPtr, descriptorLength );
       
   473 
       
   474 	HBufC* tmpBrandId = CnvUtfConverter::ConvertToUnicodeFromUtf8L( *brandId );
       
   475 	CleanupStack::PopAndDestroy( brandId );
       
   476 	delete iBrandId;
       
   477 	iBrandId = tmpBrandId;
       
   478 	
       
   479 	descriptorLength = stream.ReadInt16L();
       
   480 	HBufC8* defaultBrandId = HBufC8::NewLC( descriptorLength );
       
   481 	TPtr8 defaultPtr = defaultBrandId->Des();
       
   482 	stream.ReadL( defaultPtr, descriptorLength );
       
   483 	
       
   484 	HBufC* tmpDefaultBrandId = CnvUtfConverter::ConvertToUnicodeFromUtf8L( *defaultBrandId );
       
   485 	CleanupStack::PopAndDestroy( defaultBrandId );
       
   486 	delete iDefaultBrandId;
       
   487 	iDefaultBrandId = tmpDefaultBrandId;
       
   488 	
       
   489 	CleanupStack::PopAndDestroy( 2, streamBuf ); // stream, streamBuf
       
   490 
       
   491 
       
   492 	// get the language id
       
   493 	iLanguageId = (TLanguage)aMessage.Int2();
       
   494 
       
   495 	// get the version
       
   496 	iReserved = aMessage.Int3();
       
   497 
       
   498 	// Check if brand is discarded and client is trying to access/append/replace on that
       
   499 	// give error message saying brand not found so that client will not use that brand
       
   500 	if(aType == EBSTxAccess || aType == EBSTxAppend || aType == EBSTxReplace )
       
   501 	{
       
   502 		if(!iStorageManager)
       
   503 		{
       
   504 			iStorageManager = CBSStorageManager::NewL( this, *iApplicationId );	
       
   505 		}
       
   506 		
       
   507 		TBool brandDiscarded = EFalse ;
       
   508 		brandDiscarded = iStorageManager->CheckBrandDiscarded(*iApplicationId, *iBrandId) ;
       
   509 		
       
   510 		if(brandDiscarded)
       
   511 		{
       
   512 			TRACE( T_LIT("CBSSession::InitUpdateL: Brand discarded!->LeaveWith KErrNotFound") );
       
   513 			///Server()->DisplaySessionInfoL( this, KErrNotFound );
       
   514 			//User::Leave( KErrNotFound );	
       
   515 			iBrandId->Des().Copy( *iDefaultBrandId );
       
   516 		}
       
   517 	}
       
   518 
       
   519 	if( aType == EBSTxAppend )
       
   520 		{
       
   521 		PrepareAppendL();
       
   522 		}
       
   523 	else if( aType == EBSTxReplace )
       
   524 		{
       
   525 		PrepareReplaceL();
       
   526 		}
       
   527 	else if(aType == EBSTxUninstall)
       
   528 		{
       
   529 		if( !iStorageManager )
       
   530 			{
       
   531 			iStorageManager = CBSStorageManager::NewL( this, *iApplicationId );
       
   532 			}
       
   533 		// if any client is accessing same brand leave with KErrInUse.
       
   534 		// check if the brand has any active clients
       
   535 		TBool brandActive = EFalse;
       
   536 		CBSServer* server = Server();
       
   537 		if( server )
       
   538 			{
       
   539 			brandActive = server->MatchSessionUninstallL( *iApplicationId, *iBrandId,
       
   540 												 			this);					
       
   541 			}
       
   542 			
       
   543 		if(brandActive)
       
   544 			{
       
   545 			TRACE( T_LIT("CBSSession::InitUpdateL: UnInstallation aborted -> LeaveWith KErrInUse") );
       
   546 			//Server()->DisplaySessionInfoL( this,KErrInUse);
       
   547 			//when changing this errorcode: please read the note in 
       
   548 			//UninstallL() ( installer.cpp )
       
   549 			User::Leave( KErrInUse );
       
   550 			}
       
   551 		}
       
   552 	else if( aType == EBSTxInstall )
       
   553 		{
       
   554 		if( !iStorageManager )
       
   555 			{
       
   556 			iStorageManager = CBSStorageManager::NewL( this, *iApplicationId );
       
   557 			}
       
   558 			
       
   559 		HBufC* drive = HBufC::NewLC(3);
       
   560 		TRAPD( err, iStorageManager->GetNewestVersionL( *iApplicationId,
       
   561 												   	      *iBrandId,
       
   562 										   		    	  iLanguageId ) );
       
   563 		if( ( err == KErrNotFound ) || ( err == KErrPathNotFound ) )
       
   564 			{
       
   565 			CleanupStack::PopAndDestroy(drive);
       
   566 			// this brand is not yet there, which is the correct situation here
       
   567 			// for starting install. We can just ignore this error
       
   568 			}
       
   569 			
       
   570 		else if( !err )
       
   571 			{
       
   572 			// there was no error, therefore this brand already exists in the ROM
       
   573 			// (Z drive).
       
   574 			// we leave with KErrAlreadyExists
       
   575 			if( 0 == (drive->Des().Compare(KBSZDrive)) )
       
   576 				{
       
   577 				CleanupStack::PopAndDestroy(drive);		
       
   578 			    User::Leave( KErrAlreadyExists );
       
   579 				}
       
   580 			else
       
   581 				{
       
   582 				//PopAndDestroy drive as it is not needed any longer.
       
   583 				CleanupStack::PopAndDestroy(drive);
       
   584 
       
   585 				// if any client is accessing same brand leave with KErrInUse.
       
   586 				// check if the brand has any active clients
       
   587 				TBool brandActive = EFalse;
       
   588 				CBSServer* server = Server();
       
   589 				if( server )
       
   590 					{
       
   591 					brandActive = server->MatchSessionL( *iApplicationId, *iBrandId,
       
   592 													 iLanguageId, this, iReserved );					
       
   593 					}
       
   594 
       
   595 				if(brandActive == EFalse)
       
   596 					{	
       
   597 					TRACE( T_LIT("CBSSession::InitUpdateL: Brand not used -> remove") );
       
   598 					iStorageManager->RemoveBrandL( *iApplicationId, *iBrandId,
       
   599 													iLanguageId, iReserved );
       
   600 					}
       
   601 				else
       
   602 					{
       
   603 					TRACE( T_LIT("CBSSession::InitUpdateL: Installation aborted->LeaveWith KErrInUse") );
       
   604 					//Server()->DisplaySessionInfoL( this,KErrInUse);
       
   605 					//when changing this errorcode: please read the note in 
       
   606 					//UninstallL() ( installer.cpp )
       
   607 					User::Leave( KErrInUse );
       
   608 					}
       
   609 				}
       
   610 			}
       
   611 		else
       
   612 			{
       
   613 			CleanupStack::PopAndDestroy(drive);	
       
   614 			// some other error
       
   615 			TRACE( T_LIT("CBSSession::InitUpdateL: ERROR aType[%d] LeaveWith[%d]"),aType,err );
       
   616 			User::Leave( err );
       
   617 			}
       
   618 		}
       
   619 		
       
   620 	
       
   621 	iInitialized = ETrue;
       
   622 
       
   623     TRACE( T_LIT("Server initialized") );
       
   624 	}
       
   625 
       
   626 
       
   627 
       
   628 // ---------------------------------------------------------
       
   629 // CBSSession::PrepareAppendL
       
   630 //
       
   631 // (other items were commented in a header).
       
   632 // ---------------------------------------------------------
       
   633 //
       
   634 void CBSSession::PrepareAppendL()
       
   635 	{
       
   636 	if( !iStorageManager )
       
   637 		{
       
   638 		iStorageManager = CBSStorageManager::NewL( this, *iApplicationId );
       
   639 		}
       
   640 		
       
   641 	RFile brandHandle;
       
   642 	iStorageManager->BrandHandleL( *iApplicationId,
       
   643 								   *iBrandId, iLanguageId, brandHandle, iReserved );
       
   644 	CleanupClosePushL( brandHandle );
       
   645 	RFileReadStream oldStream;
       
   646 	oldStream.Attach( brandHandle );
       
   647 	CleanupClosePushL( oldStream );
       
   648 	TInt version = oldStream.ReadInt16L();
       
   649 
       
   650 	TInt count = oldStream.ReadInt16L();
       
   651 	
       
   652 	if( !iStorageManager->Storage() )
       
   653 		{
       
   654 		iStorageManager->CreateStorageL();
       
   655 		}
       
   656 	for( TInt i = 0; i < count; i++ )
       
   657 		{
       
   658 		MBSElement* element = InternalizeElementL( oldStream, ETrue );
       
   659 		CleanupDeletePushL( element );
       
   660 		// transfers ownership
       
   661 		iStorageManager->Storage()->AppendElementsL( element );
       
   662 		CleanupStack::Pop(); // element
       
   663 		}
       
   664 
       
   665 	iStorageManager->Storage()->SetVersion( iReserved );
       
   666 	iStorageManager->Storage()->SetStorageIdL( *iBrandId );
       
   667 	iStorageManager->Storage()->SetApplicationIdL( *iApplicationId );
       
   668 	iStorageManager->Storage()->SetLanguageL( iLanguageId );
       
   669 
       
   670 	CleanupStack::PopAndDestroy( 2 ); // oldStream, brandHandle
       
   671 	iAppending = ETrue;
       
   672 	}
       
   673 // ---------------------------------------------------------
       
   674 // CBSSession::PrepareReplaceL
       
   675 //
       
   676 // (other items were commented in a header).
       
   677 // ---------------------------------------------------------
       
   678 //
       
   679 void CBSSession::PrepareReplaceL()
       
   680 	{
       
   681 	PrepareAppendL();
       
   682 	}
       
   683 	
       
   684 // ---------------------------------------------------------
       
   685 // CBSSession::InitAccessL
       
   686 //
       
   687 // (other items were commented in a header).
       
   688 // ---------------------------------------------------------
       
   689 //
       
   690 void CBSSession::InitAccessL( const RMessage2 &aMessage )
       
   691 	{
       
   692 	InitUpdateL( aMessage, EBSTxAccess );
       
   693 	
       
   694 	iBranding = CBSBrandHandler::NewL( *iApplicationId, *iBrandId, *iDefaultBrandId,
       
   695 										iLanguageId, this, iReserved );
       
   696 
       
   697 
       
   698 	iAccessInit = ETrue;
       
   699     TRACE( T_LIT("Access initialized") );
       
   700 	}
       
   701 
       
   702 
       
   703 // ---------------------------------------------------------
       
   704 // CBSSession::GetTextL
       
   705 //
       
   706 // (other items were commented in a header).
       
   707 // ---------------------------------------------------------
       
   708 //
       
   709 void CBSSession::GetTextL( const RMessage2 &aMessage )
       
   710 	{
       
   711 	if( !iAccessInit )
       
   712 		{
       
   713 		User::Leave( KErrNotReady );
       
   714 		}
       
   715 	TInt idSize( aMessage.GetDesLength( 0 ) );
       
   716 
       
   717     HBufC8* idBuffer = HBufC8::NewLC( idSize );
       
   718 	TPtr8 ptrId = idBuffer->Des();
       
   719 	aMessage.ReadL( 0, ptrId );
       
   720 
       
   721     HBufC* temp = iBranding->GetTextL( ptrId );
       
   722     delete iText;
       
   723     iText = temp;
       
   724 
       
   725     CleanupStack::PopAndDestroy( idBuffer );
       
   726     TPckgC<TInt> pack( iText->Size() );
       
   727     aMessage.WriteL( 1, pack );
       
   728     TRACE( T_LIT("aMessage.WriteL( 1, %d );"), iText->Size() );
       
   729 	}
       
   730 
       
   731 void CBSSession :: isBrandUpdateRequiredL (const RMessage2 &aMessage)
       
   732 {
       
   733 	if( !iAccessInit )
       
   734 		{
       
   735 		User::Leave( KErrNotReady );
       
   736 		}
       
   737 	TInt updateRequired = iBranding->isBrandUpdateRequiredL ();
       
   738 //	iValue = updateRequired;
       
   739 	TPckgC <TInt> pack (updateRequired);
       
   740 	aMessage.WriteL( 0, pack );
       
   741 }
       
   742 // ---------------------------------------------------------
       
   743 // CBSSession::GetIntL
       
   744 //
       
   745 // (other items were commented in a header).
       
   746 // ---------------------------------------------------------
       
   747 //
       
   748 void CBSSession::GetIntL( const RMessage2 &aMessage )
       
   749 	{
       
   750 	if( !iInitialized )
       
   751 		{
       
   752 		User::Leave( KErrNotReady );
       
   753 		}
       
   754 	TInt idSize( aMessage.GetDesLength( 0 ) );
       
   755 
       
   756 	HBufC8* idBuffer = HBufC8::NewLC( idSize );
       
   757 	TPtr8 ptrId = idBuffer->Des();
       
   758 	aMessage.ReadL( 0, ptrId );
       
   759 
       
   760 	iValue = iBranding->GetIntL( ptrId );
       
   761 	TPckgC<TInt> pack( iValue );
       
   762 	aMessage.WriteL( 1, pack );
       
   763 	CleanupStack::PopAndDestroy( idBuffer );
       
   764 	}
       
   765 
       
   766 // ---------------------------------------------------------
       
   767 // CBSSession::GetBufferL
       
   768 //
       
   769 // (other items were commented in a header).
       
   770 // ---------------------------------------------------------
       
   771 //
       
   772 void CBSSession::GetBufferL( const RMessage2 &aMessage )
       
   773 	{
       
   774 	if( !iAccessInit )
       
   775 		{
       
   776 		User::Leave( KErrNotReady );
       
   777 		}
       
   778 	TInt idSize( aMessage.GetDesLength( 0 ) );
       
   779 
       
   780     HBufC8* idBuffer = HBufC8::NewLC( idSize );
       
   781 	TPtr8 ptrId = idBuffer->Des();
       
   782 	aMessage.ReadL( 0, ptrId );
       
   783 
       
   784     HBufC8* temp = iBranding->GetBufferL( ptrId );
       
   785     delete iBuffer;
       
   786     iBuffer = temp;
       
   787 
       
   788     CleanupStack::PopAndDestroy( idBuffer );
       
   789     TPckgC<TInt> pack( iBuffer->Size() );
       
   790     aMessage.WriteL( 1, pack );
       
   791     TRACE( T_LIT("aMessage.WriteL( 1, %d );"), iBuffer->Size() );
       
   792 	}
       
   793 
       
   794 
       
   795 // ---------------------------------------------------------
       
   796 // CBSSession::GetFileL
       
   797 //
       
   798 // (other items were commented in a header).
       
   799 // ---------------------------------------------------------
       
   800 //
       
   801 void CBSSession::GetFileL( const RMessage2 &aMessage )
       
   802 	{
       
   803     TRACE( T_LIT("CBSSession::GetFileL begin") );
       
   804     if( !iInitialized )
       
   805 		{
       
   806 		User::Leave( KErrNotReady );
       
   807 		}
       
   808 	TInt idSize( aMessage.GetDesLength( 2 ) );
       
   809 	HBufC8* idBuffer = HBufC8::NewLC( idSize );
       
   810 	TPtr8 ptrId = idBuffer->Des();
       
   811 	aMessage.ReadL( 2, ptrId );
       
   812 
       
   813 	RFile file;
       
   814     iBranding->GetFileL( ptrId, file );
       
   815 
       
   816 	CleanupStack::PopAndDestroy( idBuffer );
       
   817 
       
   818 	User::LeaveIfError( file.TransferToClient( aMessage, 0 ) );
       
   819 
       
   820 	iMessageCompleted = ETrue;
       
   821 
       
   822 	file.Close();
       
   823 	TRACE( T_LIT("CBSSession::GetFileL end") );
       
   824 	}
       
   825 
       
   826 
       
   827 // ---------------------------------------------------------
       
   828 // CBSSession::InternalizeElementIdsL
       
   829 //
       
   830 // (other items were commented in a header).
       
   831 // ---------------------------------------------------------
       
   832 //
       
   833 void CBSSession::InternalizeElementIdsL( RReadStream& aStream, RBSObjOwningPtrArray<HBufC8>& aArray )
       
   834 	{
       
   835 	TInt count = aStream.ReadInt16L();
       
   836 	
       
   837 	for(TInt i = 0; i < count; i++ )
       
   838 		{
       
   839 		TInt length = aStream.ReadInt16L();
       
   840 		HBufC8* id = HBufC8::NewLC( length );
       
   841 		TPtr8 ptrId = id->Des();
       
   842 		aStream.ReadL( ptrId, length );
       
   843 		aArray.AppendL( id );
       
   844 		CleanupStack::Pop( id );
       
   845 		}
       
   846 	}
       
   847 
       
   848 // ---------------------------------------------------------
       
   849 // CBSSession::PrepareTextL
       
   850 //
       
   851 // (other items were commented in a header).
       
   852 // ---------------------------------------------------------
       
   853 //
       
   854 void CBSSession::PrepareSeveralL( const RMessage2 &aMessage )
       
   855 	{
       
   856 	if( !iInitialized )
       
   857 		{
       
   858 		User::Leave( KErrNotReady );
       
   859 		}
       
   860 		
       
   861 	TInt msgSize( aMessage.GetDesLength( 1 ) );
       
   862 
       
   863     HBufC8* idBuffer = HBufC8::NewLC( msgSize );
       
   864 	TPtr8 ptrId = idBuffer->Des();
       
   865 	aMessage.ReadL( 1, ptrId );
       
   866 
       
   867 	RDesReadStream idStream;
       
   868 	CleanupClosePushL( idStream );
       
   869 	idStream.Open( ptrId );
       
   870 
       
   871 	RBSObjOwningPtrArray<HBufC8> idArray;
       
   872 	CleanupClosePushL( idArray );
       
   873 	
       
   874 	InternalizeElementIdsL( idStream, idArray );
       
   875 
       
   876 
       
   877 	MBSElement* texts = iBranding->GetSeveralL( idArray );
       
   878 
       
   879 	CleanupStack::PopAndDestroy(); // idArray
       
   880 	CleanupStack::PopAndDestroy(); // idStream
       
   881 	CleanupStack::PopAndDestroy();  // idBuffer
       
   882 
       
   883 	CleanupDeletePushL( texts );
       
   884 
       
   885 	// FIXME magic number
       
   886 	CBufFlat* data = CBufFlat::NewL( 2000 );
       
   887 	CleanupStack::PushL( data );
       
   888 
       
   889 	RBufWriteStream writeStream;
       
   890 	CleanupClosePushL( writeStream );
       
   891 	writeStream.Open( *data );
       
   892 
       
   893 	texts->ExternalizeL( writeStream );	
       
   894 
       
   895 	CleanupStack::PopAndDestroy(); // writeStream
       
   896 	
       
   897 	delete iSeveralData;
       
   898 	iSeveralData = data;
       
   899 	CleanupStack::Pop( data );
       
   900 
       
   901 	CleanupStack::PopAndDestroy(); // texts
       
   902 
       
   903 	delete iBuffer;
       
   904 	iBuffer = NULL;
       
   905 	iBuffer = iSeveralData->Ptr(0).AllocL();
       
   906 
       
   907 	delete iSeveralData;
       
   908 	iSeveralData = NULL;
       
   909 	TPckgC<TInt> pack( iBuffer->Size() );
       
   910 	TRACE( T_LIT("DispatchMessageL - EPlatSecPrepareSeveral writing size %d"), iBuffer->Size() );
       
   911     aMessage.WriteL( 0, pack );
       
   912 
       
   913 	}
       
   914 
       
   915 // ---------------------------------------------------------
       
   916 // CBSSession::GetSeveralTextL
       
   917 //
       
   918 // (other items were commented in a header).
       
   919 // ---------------------------------------------------------
       
   920 //
       
   921 void CBSSession::GetSeveralL( const RMessage2 &aMessage )
       
   922 	{
       
   923 	if( !iInitialized )
       
   924 		{
       
   925 		User::Leave( KErrNotReady );
       
   926 		}
       
   927 	aMessage.WriteL( 0, *iBuffer );
       
   928 
       
   929 	delete iBuffer;
       
   930 	iBuffer = NULL;
       
   931 	}
       
   932 
       
   933 
       
   934 // ---------------------------------------------------------
       
   935 // CBSSession::PrepareStructureL
       
   936 //
       
   937 // (other items were commented in a header).
       
   938 // ---------------------------------------------------------
       
   939 //
       
   940 void CBSSession::PrepareStructureL( const RMessage2 &aMessage )
       
   941 	{
       
   942 	if( !iInitialized )
       
   943 		{
       
   944 		User::Leave( KErrNotReady );
       
   945 		}
       
   946 	TInt idSize( aMessage.GetDesLength( 1 ) );
       
   947 
       
   948 	RBSObjOwningPtrArray<MBSElement> valueArray;
       
   949 	CleanupClosePushL( valueArray );
       
   950 
       
   951     HBufC8* idBuffer = HBufC8::NewLC( idSize );
       
   952 	TPtr8 ptrId = idBuffer->Des();
       
   953 	aMessage.ReadL( 1, ptrId );
       
   954 
       
   955 
       
   956 	MBSElement* extElement = iBranding->GetStructureL( ptrId );
       
   957 	CleanupDeletePushL( extElement );
       
   958 	// FIXME magic number
       
   959 	CBufFlat* data = CBufFlat::NewL( 2000 );
       
   960 	CleanupStack::PushL( data );
       
   961 
       
   962 	RBufWriteStream writeStream;
       
   963 	CleanupClosePushL( writeStream );
       
   964 	writeStream.Open( *data );
       
   965 
       
   966 	extElement->ExternalizeL( writeStream );
       
   967 
       
   968 	CleanupStack::PopAndDestroy(); // writeStream
       
   969 
       
   970 	delete iSeveralData;
       
   971 	iSeveralData = data;
       
   972 	CleanupStack::Pop( data );
       
   973 
       
   974 	CleanupStack::PopAndDestroy( extElement );
       
   975 
       
   976 	delete iBuffer;
       
   977 	iBuffer = NULL;
       
   978 	iBuffer = iSeveralData->Ptr(0).AllocL();
       
   979 
       
   980 	delete iSeveralData;
       
   981 	iSeveralData = NULL;
       
   982 	TPckgC<TInt> pack( iBuffer->Size() );
       
   983 	TRACE( T_LIT("DispatchMessageL - EPlatSecPrepareSeveral writing size %d"), iBuffer->Size() );
       
   984     aMessage.WriteL( 0, pack );
       
   985 
       
   986 
       
   987 	CleanupStack::PopAndDestroy(idBuffer);
       
   988 
       
   989 	CleanupStack::PopAndDestroy(); // valueArray
       
   990 
       
   991 	}
       
   992 
       
   993 // ---------------------------------------------------------
       
   994 // CBSSession::GetStructureL
       
   995 //
       
   996 // (other items were commented in a header).
       
   997 // ---------------------------------------------------------
       
   998 //
       
   999 void CBSSession::GetStructureL( const RMessage2 &aMessage )
       
  1000 	{
       
  1001 	if( !iInitialized )
       
  1002 		{
       
  1003 		User::Leave( KErrNotReady );
       
  1004 		}
       
  1005 	aMessage.WriteL( 0, *iBuffer );
       
  1006 
       
  1007 	delete iBuffer;
       
  1008 	iBuffer = NULL;
       
  1009 	}
       
  1010 
       
  1011 
       
  1012 
       
  1013 // ---------------------------------------------------------
       
  1014 // CBSSession::InstallL
       
  1015 //
       
  1016 // (other items were commented in a header).
       
  1017 // ---------------------------------------------------------
       
  1018 //
       
  1019 void CBSSession::InstallL( const RMessage2 &aMessage )
       
  1020 	{
       
  1021 	TRACE( T_LIT( "CBSSession::InstallL begin") );
       
  1022 	if( !iInitialized || !iStorageManager )
       
  1023 		{
       
  1024 		User::Leave( KErrNotReady );
       
  1025 		}
       
  1026 
       
  1027 	if( !iStorageManager->Storage() )
       
  1028 		{
       
  1029 		iStorageManager->CreateStorageL();
       
  1030 		}
       
  1031 	
       
  1032 	iStorageManager->ConnectTransactionL( *iApplicationId,
       
  1033 										  *iBrandId, iLanguageId, EFalse, iReserved );
       
  1034 
       
  1035 	TInt msgSize( aMessage.GetDesLength( 0 ) );
       
  1036 
       
  1037     HBufC8* buffer = HBufC8::NewLC( msgSize );
       
  1038 	TPtr8 ptr = buffer->Des();
       
  1039 	aMessage.ReadL( 0, ptr );
       
  1040 
       
  1041 	RDesReadStream stream;
       
  1042 	CleanupClosePushL( stream );
       
  1043 	stream.Open( ptr );
       
  1044 
       
  1045 	MBSElement* element = InternalizeElementL( stream );
       
  1046 	CleanupDeletePushL( element );
       
  1047 
       
  1048 	iStorageManager->Storage()->SetVersion( iReserved );
       
  1049 	iStorageManager->Storage()->SetStorageIdL( *iBrandId );
       
  1050 	iStorageManager->Storage()->SetApplicationIdL( *iApplicationId );
       
  1051 	iStorageManager->Storage()->SetLanguageL( iLanguageId );
       
  1052 	// transfers ownership
       
  1053 	iStorageManager->Storage()->AppendElementsL( element );
       
  1054 
       
  1055 	CleanupStack::Pop(); // element 
       
  1056 	CleanupStack::PopAndDestroy( 2, buffer );
       
  1057 	iWriteNeeded = ETrue;
       
  1058 	TRACE( T_LIT( "CBSSession::InstallL end") );
       
  1059 	}
       
  1060 
       
  1061 // ---------------------------------------------------------
       
  1062 // CBSSession::ReplaceL
       
  1063 //
       
  1064 // (other items were commented in a header).
       
  1065 // ---------------------------------------------------------
       
  1066 //
       
  1067 void CBSSession::ReplaceL( const RMessage2 &aMessage )
       
  1068 	{
       
  1069 	TRACE( T_LIT( "CBSSession::ReplaceL begin") );
       
  1070 	if( !iInitialized || !iStorageManager )
       
  1071 		{
       
  1072 		User::Leave( KErrNotReady );
       
  1073 		}
       
  1074 		
       
  1075 	iStorageManager->ConnectTransactionL( *iApplicationId,
       
  1076 										  *iBrandId, iLanguageId, EFalse, iReserved );
       
  1077 
       
  1078 	TInt msgSize( aMessage.GetDesLength( 0 ) );
       
  1079 
       
  1080     HBufC8* buffer = HBufC8::NewLC( msgSize );
       
  1081 	TPtr8 ptr = buffer->Des();
       
  1082 	aMessage.ReadL( 0, ptr );
       
  1083 
       
  1084 	RDesReadStream stream;
       
  1085 	CleanupClosePushL( stream );
       
  1086 	stream.Open( ptr );
       
  1087 
       
  1088 	MBSElement* element = InternalizeElementL( stream );
       
  1089 	CleanupDeletePushL( element );
       
  1090 
       
  1091 	// transfers ownership
       
  1092 	iStorageManager->Storage()->ReplaceElementL( element );
       
  1093 
       
  1094 	CleanupStack::Pop(); // element 
       
  1095 	CleanupStack::PopAndDestroy( 2, buffer );
       
  1096 	iWriteNeeded = ETrue;
       
  1097 	TRACE( T_LIT( "CBSSession::ReplaceL end") );
       
  1098 	}
       
  1099 
       
  1100 
       
  1101 // ---------------------------------------------------------
       
  1102 // CBSSession::AppendL
       
  1103 //
       
  1104 // (other items were commented in a header).
       
  1105 // ---------------------------------------------------------
       
  1106 //
       
  1107 void CBSSession::AppendL( const RMessage2 &aMessage )
       
  1108 	{
       
  1109 	TRACE( T_LIT( "CBSSession::AppendL begin") );
       
  1110 	if( !iInitialized || !iStorageManager )
       
  1111 		{
       
  1112 		User::Leave( KErrNotReady );
       
  1113 		}
       
  1114 	
       
  1115 	iStorageManager->ConnectTransactionL( *iApplicationId,
       
  1116 										  *iBrandId, iLanguageId, EFalse, iReserved );
       
  1117 
       
  1118 	TInt msgSize( aMessage.GetDesLength( 0 ) );
       
  1119 
       
  1120     HBufC8* buffer = HBufC8::NewLC( msgSize );
       
  1121 	TPtr8 ptr = buffer->Des();
       
  1122 	aMessage.ReadL( 0, ptr );
       
  1123 
       
  1124 	RDesReadStream stream;
       
  1125 	CleanupClosePushL( stream );
       
  1126 	stream.Open( ptr );
       
  1127 
       
  1128 	MBSElement* element = InternalizeElementL( stream );
       
  1129 	CleanupDeletePushL( element );
       
  1130 
       
  1131 	iStorageManager->Storage()->SetVersion( iReserved );
       
  1132 	iStorageManager->Storage()->SetStorageIdL( *iBrandId );
       
  1133 	iStorageManager->Storage()->SetApplicationIdL( *iApplicationId );
       
  1134 	iStorageManager->Storage()->SetLanguageL( iLanguageId );
       
  1135 	// transfers ownership
       
  1136 	iStorageManager->Storage()->AppendElementsL( element );
       
  1137 
       
  1138 	CleanupStack::Pop(); // element 
       
  1139 	CleanupStack::PopAndDestroy( 2, buffer );
       
  1140 	iWriteNeeded = ETrue;
       
  1141     TRACE( T_LIT( "CBSSession::AppendL end") );
       
  1142 	}
       
  1143 
       
  1144 // ---------------------------------------------------------
       
  1145 // CBSSession::StartTransactionL
       
  1146 //
       
  1147 // (other items were commented in a header).
       
  1148 // ---------------------------------------------------------
       
  1149 //
       
  1150 void CBSSession::StartTransactionL( const RMessage2& /*aMessage*/ )
       
  1151 	{
       
  1152 	if( !iStorageManager )
       
  1153 		{
       
  1154 		iStorageManager = CBSStorageManager::NewL( this, *iApplicationId );
       
  1155 		}
       
  1156 	// if we are appending or replacing, we don't need to 
       
  1157 	// create storage
       
  1158 	if( !iAppending )
       
  1159 		{
       
  1160 		iStorageManager->CreateStorageL();	
       
  1161 		}
       
  1162 	}
       
  1163 
       
  1164 
       
  1165 // ---------------------------------------------------------
       
  1166 // CBSSession::CancelTransactionL
       
  1167 //
       
  1168 // (other items were commented in a header).
       
  1169 // ---------------------------------------------------------
       
  1170 //
       
  1171 void CBSSession::CancelTransactionL( const RMessage2 &aMessage )
       
  1172 	{
       
  1173 	// get the application id
       
  1174 	TInt bufferSize( aMessage.GetDesLength( 0 ) );
       
  1175 
       
  1176     HBufC8* appId = HBufC8::NewLC( bufferSize );
       
  1177 	TPtr8 appPtr = appId->Des();
       
  1178 	aMessage.ReadL( 0, appPtr );
       
  1179 
       
  1180 
       
  1181 	// get the brand id
       
  1182 	bufferSize = aMessage.GetDesLength( 1 );
       
  1183     HBufC8* brandId = HBufC8::NewLC( bufferSize );
       
  1184 	TPtr8 brandPtr = brandId->Des();
       
  1185 	aMessage.ReadL( 1, brandPtr );
       
  1186 
       
  1187 	// get the language id
       
  1188 	TLanguage language = (TLanguage)aMessage.Int2();
       
  1189 
       
  1190 	// get the version
       
  1191 	TInt version = aMessage.Int3();
       
  1192 	
       
  1193 	
       
  1194 	HBufC* tmpBrandId = CnvUtfConverter::ConvertToUnicodeFromUtf8L( *brandId );
       
  1195 	CleanupStack::PopAndDestroy( brandId );
       
  1196 	CleanupStack::PushL( tmpBrandId );
       
  1197 	HBufC* tmpAppId = CnvUtfConverter::ConvertToUnicodeFromUtf8L( *appId );
       
  1198 	CleanupStack::PushL( tmpAppId );
       
  1199 	
       
  1200 	// check if the transaction is correct	
       
  1201 	iStorageManager->ConnectTransactionL( *tmpAppId,
       
  1202 										  *tmpBrandId, iLanguageId,
       
  1203 										  EFalse, iReserved );
       
  1204 
       
  1205 	CleanupStack::PopAndDestroy( 2, tmpBrandId ); // tmpBrandId, tmpAppId
       
  1206 	CleanupStack::PopAndDestroy( appId );
       
  1207 	// cancel the transaction
       
  1208 	iStorageManager->CancelTransactionL();
       
  1209 	iWriteNeeded = EFalse;
       
  1210 	}
       
  1211 
       
  1212 // ---------------------------------------------------------
       
  1213 // CBSSession::StopTransactionL
       
  1214 //
       
  1215 // (other items were commented in a header).
       
  1216 // ---------------------------------------------------------
       
  1217 //
       
  1218 void CBSSession::StopTransactionL( const RMessage2 &aMessage )
       
  1219 	{
       
  1220 	if( !iStorageManager )
       
  1221 		{
       
  1222 		// transaction not found
       
  1223 		User::Leave( KErrNotFound );
       
  1224 		}
       
  1225 	if( iAppending )
       
  1226 		{
       
  1227 		// find next available version number
       
  1228 		TInt nextVersion = iStorageManager->NextAvailableVersionL();
       
  1229 		iStorageManager->SetVersion( nextVersion );
       
  1230 		iStorageManager->Storage()->SetVersion( nextVersion );
       
  1231 		}
       
  1232 		
       
  1233 	if( iWriteNeeded )
       
  1234 		{
       
  1235 		iReserved = iStorageManager->WriteStorageFilesL();	
       
  1236 		iWriteNeeded = EFalse;
       
  1237 		}
       
  1238 	
       
  1239 	delete iStorageManager;
       
  1240 	iStorageManager = NULL;
       
  1241 	
       
  1242 	TPckgC<TInt> pack( iReserved );
       
  1243 	aMessage.WriteL( 0, pack );
       
  1244 	
       
  1245 	}
       
  1246 
       
  1247 
       
  1248 // ---------------------------------------------------------
       
  1249 // CBSSession::InternalizeElementL
       
  1250 //
       
  1251 // (other items were commented in a header).
       
  1252 // ---------------------------------------------------------
       
  1253 //
       
  1254 MBSElement* CBSSession::InternalizeElementL( RReadStream& aStream, TBool aAppending /*= EFalse*/ )
       
  1255 	{
       
  1256 	MBSElement* returnValue = NULL;
       
  1257 
       
  1258 	TBSElementType type = (TBSElementType)aStream.ReadInt16L();
       
  1259 	TInt idSize = aStream.ReadInt16L();
       
  1260 	HBufC8* elementId = HBufC8::NewLC( idSize );
       
  1261 	TPtr8 elementIdPtr = elementId->Des();
       
  1262 	aStream.ReadL( elementIdPtr, idSize );
       
  1263 
       
  1264 
       
  1265 	switch( type )
       
  1266 		{
       
  1267 		case EBSInt:
       
  1268 			{
       
  1269 			TInt intData = aStream.ReadInt16L();
       
  1270 			returnValue = BSElementFactory::CreateBSElementL( *elementId,
       
  1271 															   EBSInt,
       
  1272 															   intData );
       
  1273 			break;
       
  1274 			}
       
  1275 		case EBSText:
       
  1276 			{
       
  1277 			TInt textSize = aStream.ReadInt16L();
       
  1278 			HBufC* textData = HBufC::NewLC( textSize );
       
  1279 
       
  1280 			TPtr textPtr = textData->Des();
       
  1281 			aStream.ReadL( textPtr, textSize );
       
  1282 
       
  1283 			returnValue = BSElementFactory::CreateBSElementL( *elementId,
       
  1284 															   type,
       
  1285 															   *textData );
       
  1286 
       
  1287 			CleanupStack::Pop( textData );
       
  1288 			break;
       
  1289 			}
       
  1290 
       
  1291 		case EBSFile: 
       
  1292 			{
       
  1293 			TInt nameSize = aStream.ReadInt16L();
       
  1294 			HBufC* fileName = HBufC::NewLC( nameSize );
       
  1295 
       
  1296 			TPtr filePtr = fileName->Des();
       
  1297 			aStream.ReadL( filePtr, nameSize );
       
  1298 			
       
  1299 			if( !aAppending )
       
  1300 				{
       
  1301 				// we are installing, so the filename is pointing to a file
       
  1302 				// that has to be installed
       
  1303 				// Install file and get the filename without path
       
  1304 				HBufC* strippedName = iStorageManager->InstallFileLC( *fileName );
       
  1305 				returnValue = BSElementFactory::CreateBSElementL( *elementId,
       
  1306 																   type,
       
  1307 																   *strippedName );
       
  1308 				CleanupStack::Pop( strippedName );
       
  1309 				CleanupStack::PopAndDestroy( fileName );
       
  1310 				}
       
  1311 			else
       
  1312 				{
       
  1313 				// we are appending, so this is really the filename
       
  1314 				returnValue = BSElementFactory::CreateBSElementL( *elementId,
       
  1315 																   type,
       
  1316 																   *fileName );
       
  1317 				CleanupStack::Pop( fileName );
       
  1318 				}
       
  1319 			break;
       
  1320 			}
       
  1321 			
       
  1322 		case EBSList:
       
  1323 			{
       
  1324 			RBSObjOwningPtrArray<MBSElement> listData;
       
  1325 			CleanupClosePushL( listData );
       
  1326 			TInt listCount = aStream.ReadInt16L();
       
  1327 			for( TInt i = 0; i < listCount; i++ )
       
  1328 				{
       
  1329 				MBSElement* listElement = InternalizeElementL( aStream );
       
  1330 				CleanupDeletePushL( listElement );
       
  1331 				listData.AppendL( listElement );
       
  1332 				CleanupStack::Pop(); // listElement
       
  1333 				}
       
  1334 
       
  1335 			returnValue = BSElementFactory::CreateBSElementL( *elementId,
       
  1336 															   EBSList,
       
  1337 															   listData );
       
  1338 			CleanupStack::Pop(); //  listData
       
  1339 			break;
       
  1340 			}
       
  1341 		case EBSBuffer:
       
  1342 			{
       
  1343 			TInt bufferSize = aStream.ReadInt16L();
       
  1344 			HBufC8* buffer = HBufC8::NewLC( bufferSize );
       
  1345 
       
  1346 			TPtr8 buffPtr = buffer->Des();
       
  1347 			aStream.ReadL( buffPtr, bufferSize );
       
  1348 
       
  1349 			returnValue = BSElementFactory::CreateBSElementL( *elementId,
       
  1350 															   EBSBuffer,
       
  1351 															   *buffer );
       
  1352 
       
  1353 			CleanupStack::Pop( buffer );
       
  1354 			break;
       
  1355 			}
       
  1356 		case EBSBitmap:
       
  1357 			{
       
  1358 			TInt length = aStream.ReadInt16L();
       
  1359 			HBufC8* fileId = HBufC8::NewLC( length );
       
  1360 			
       
  1361 			TPtr8 fileIdPtr = fileId->Des();
       
  1362 			aStream.ReadL( fileIdPtr, length );
       
  1363 			
       
  1364 			TInt bitmapId = aStream.ReadInt16L();
       
  1365 			TInt maskId = aStream.ReadInt16L();
       
  1366 			TInt skinId = aStream.ReadInt16L();
       
  1367 			TInt skinMaskId = aStream.ReadInt16L();
       
  1368 
       
  1369 
       
  1370 			CBSBitmap* bitmap = CBSBitmap::NewLC( bitmapId,
       
  1371 												  maskId,
       
  1372 												  skinId,
       
  1373 												  skinMaskId,
       
  1374 												  fileIdPtr );
       
  1375 													  
       
  1376 			returnValue = BSElementFactory::CreateBSElementL( *elementId, 
       
  1377 															  EBSBitmap,
       
  1378 															  bitmap );
       
  1379 			CleanupStack::Pop( bitmap ); 
       
  1380 			CleanupStack::PopAndDestroy( fileId );
       
  1381 			break;
       
  1382 			}
       
  1383 
       
  1384 		default:
       
  1385 			{
       
  1386 			User::Leave( KErrArgument );
       
  1387 			break;
       
  1388 			}
       
  1389 		}
       
  1390 
       
  1391 	CleanupStack::PopAndDestroy( elementId );
       
  1392 
       
  1393 	return returnValue;
       
  1394 	}
       
  1395 
       
  1396 
       
  1397 // ---------------------------------------------------------
       
  1398 // CBSSession::MatchSessionL
       
  1399 //
       
  1400 // (other items were commented in a header).
       
  1401 // ---------------------------------------------------------
       
  1402 //
       
  1403 TBool CBSSession::MatchSessionL( const TDesC& aApplicationId,
       
  1404 								 const TDesC& aBrandId,
       
  1405 								 TLanguage aLanguageId,
       
  1406 							 	 TInt aReserved )
       
  1407 	{
       
  1408 	TBool returnValue = EFalse;
       
  1409 	if( iApplicationId && iBrandId )
       
  1410 		{
       
  1411 		if( ( 0 == iApplicationId->Compare( aApplicationId ) ) &&
       
  1412 		   ( 0 == iBrandId->Compare( aBrandId ) ) &&
       
  1413 		   ( iLanguageId == aLanguageId ) && 
       
  1414 		   ( iReserved == aReserved ))
       
  1415 			{
       
  1416 			returnValue = ETrue;
       
  1417 			}
       
  1418 		}
       
  1419 	
       
  1420 	return returnValue;
       
  1421 	}
       
  1422 
       
  1423 // ---------------------------------------------------------
       
  1424 // CBSSession::MatchSessionL
       
  1425 //
       
  1426 // (other items were commented in a header).
       
  1427 // ---------------------------------------------------------
       
  1428 //
       
  1429 TBool CBSSession::MatchSessionUninstallL( const TDesC& aApplicationId,
       
  1430 								 const TDesC& aBrandId)
       
  1431 	{
       
  1432 	TBool returnValue = EFalse;
       
  1433 	if( iApplicationId && iBrandId )
       
  1434 		{
       
  1435 		if( ( 0 == iApplicationId->Compare( aApplicationId ) ) &&
       
  1436 		   ( 0 == iBrandId->Compare( aBrandId ) ) )
       
  1437 			{
       
  1438 			returnValue = ETrue;
       
  1439 			}
       
  1440 		}
       
  1441 	
       
  1442 	return returnValue;
       
  1443 	}
       
  1444 // ---------------------------------------------------------
       
  1445 // CBSSession::BrandUpdatedL
       
  1446 //
       
  1447 // (other items were commented in a header).
       
  1448 // ---------------------------------------------------------
       
  1449 //
       
  1450 void CBSSession::BrandUpdatedL( const TDesC& aApplicationId,
       
  1451 								 const TDesC& aBrandId,
       
  1452 								 TLanguage aLanguageId,
       
  1453 							 	 TInt /*aReserved*/ )
       
  1454 	{
       
  1455 	TRACE( T_LIT( "CBSSession::BrandUpdatedL begin aAppId[%S] aBrandId[%S]"), &aApplicationId, &aBrandId );
       
  1456 	iIsBackup = EFalse ; 
       
  1457 	if( iApplicationId && iBrandId )
       
  1458 		{
       
  1459 		if( ( 0 == iApplicationId->Compare( aApplicationId ) ) &&
       
  1460 		   ( 0 == iBrandId->Compare( aBrandId ) ) &&
       
  1461 		   ( iLanguageId == aLanguageId ) )
       
  1462 			{
       
  1463 			// this event is for us 
       
  1464 			// complete the observer request from client
       
  1465 			if( iObserverActive )
       
  1466 				{
       
  1467 				iObserverMessage.Complete( KErrNone );
       
  1468 				iObserverActive = EFalse;
       
  1469 				}
       
  1470 			}
       
  1471 		}
       
  1472 	TRACE( T_LIT( "CBSSession::BrandUpdatedL end") );
       
  1473 	}
       
  1474 
       
  1475 
       
  1476 // ---------------------------------------------------------
       
  1477 // CBSSession::RemoveBrandL
       
  1478 //
       
  1479 // (other items were commented in a header).
       
  1480 // ---------------------------------------------------------
       
  1481 //
       
  1482 void CBSSession::RemoveBrandL( const RMessage2& /*aMessage*/ )
       
  1483 	{
       
  1484 	TRACE( T_LIT( "CBSSession::RemoveBrandL begin") );
       
  1485 	if( !iInitialized || !iStorageManager )
       
  1486 		{
       
  1487 		User::Leave( KErrNotReady );
       
  1488 		}
       
  1489 
       
  1490 	iStorageManager->RemoveBrandL( *iApplicationId, *iBrandId,
       
  1491 								   iLanguageId, iReserved );
       
  1492 	
       
  1493 	
       
  1494 	
       
  1495 	TRACE( T_LIT( "CBSSession::RemoveBrandL end") );
       
  1496 	}
       
  1497 
       
  1498 // ---------------------------------------------------------
       
  1499 // CBSSession::RemoveApplicationL
       
  1500 //
       
  1501 // (other items were commented in a header).
       
  1502 // ---------------------------------------------------------
       
  1503 //
       
  1504 void CBSSession::RemoveApplicationL( const RMessage2& /*aMessage*/ )
       
  1505 	{
       
  1506 	TRACE( T_LIT( "CBSSession::RemoveApplicationL begin") );
       
  1507 	if( !iInitialized || !iStorageManager )
       
  1508 		{
       
  1509 		User::Leave( KErrNotReady );
       
  1510 		}
       
  1511 	iStorageManager->RemoveApplicationL( *iApplicationId );
       
  1512 	TRACE( T_LIT( "CBSSession::RemoveApplicationL end") );
       
  1513 	}
       
  1514 	
       
  1515 // ---------------------------------------------------------
       
  1516 // CBSSession::GetNewVersionL
       
  1517 //
       
  1518 // (other items were commented in a header).
       
  1519 // ---------------------------------------------------------
       
  1520 //	
       
  1521 TInt CBSSession::GetNewVersionL()
       
  1522 	{
       
  1523 	if( !iStorageManager )
       
  1524 		{
       
  1525 		iStorageManager = CBSStorageManager::NewL( this, KNullDesC );
       
  1526 		}
       
  1527 
       
  1528 	return iStorageManager->GetNewestVersionL( *iApplicationId, *iBrandId, iLanguageId );
       
  1529 	}
       
  1530 	
       
  1531 
       
  1532 // CBSSessionInfo
       
  1533 CBSSession::CBSSessionInfo* CBSSession::CBSSessionInfo::NewL( const TDesC& aFileName, 
       
  1534 	    		                                              const TDesC& aCaption,
       
  1535 	    		                                              TThreadId aThreadId, 
       
  1536 	    		                                              TProcessId aProcessId )
       
  1537 	{
       
  1538 	CBSSessionInfo* self = new( ELeave ) CBSSessionInfo( aThreadId, aProcessId );
       
  1539 	CleanupStack::PushL( self );
       
  1540 	self->ConstructL( aFileName, aCaption );
       
  1541 	CleanupStack::Pop( self );
       
  1542 	return self;
       
  1543 	}
       
  1544 void CBSSession::CBSSessionInfo::ConstructL( const TDesC& aFileName, const TDesC& aCaption )
       
  1545 	{	
       
  1546 	iFileName = aFileName.AllocL();
       
  1547 	iCaption = aCaption.AllocL();
       
  1548 	}
       
  1549 CBSSession::CBSSessionInfo::CBSSessionInfo( TThreadId aThreadId, TProcessId aProcessId  )
       
  1550 : iThreadId( aThreadId ),
       
  1551   iProcessId( aProcessId )
       
  1552 	{
       
  1553 	}
       
  1554 CBSSession::CBSSessionInfo::~CBSSessionInfo()
       
  1555 	{
       
  1556 	delete iFileName;
       
  1557 	delete iCaption;
       
  1558 	}
       
  1559 
       
  1560 const TDesC& CBSSession::CBSSessionInfo::FileName()
       
  1561 	{
       
  1562 	return *iFileName;
       
  1563 	}
       
  1564 const TDesC& CBSSession::CBSSessionInfo::Caption()
       
  1565 	{
       
  1566 	return *iCaption;
       
  1567 	}
       
  1568 
       
  1569 TThreadId CBSSession::CBSSessionInfo::ThreadId()
       
  1570 	{
       
  1571 	return iThreadId;
       
  1572 	}
       
  1573 TProcessId CBSSession::CBSSessionInfo::ProcessId()
       
  1574 	{
       
  1575 	return iProcessId;
       
  1576 	}
       
  1577 
       
  1578 
       
  1579 // ---------------------------------------------------------------------------
       
  1580 // CBSSession::ExtractInfoL
       
  1581 // Extracts some information from the specified RMessage2 and saves it.
       
  1582 // ---------------------------------------------------------------------------
       
  1583 //
       
  1584 void CBSSession::ExtractInfoL( const RMessage2& aMessage )
       
  1585     {
       
  1586 	TRACE( T_LIT( "CBSSession::ExtractInfoL begin") );
       
  1587 	//Collect all necessary data
       
  1588 	RThread thread;
       
  1589 	TInt getProcessErr,getClientThreadErr;
       
  1590 	getClientThreadErr = aMessage.Client( thread );
       
  1591 	CleanupClosePushL( thread );
       
  1592     
       
  1593 	if ( KErrNone == getClientThreadErr )
       
  1594 	    {
       
  1595 	    TRACE( T_LIT( "CBSSession::ExtractInfoL Client retrieved OK") );
       
  1596 	    TThreadId threadId = thread.Id();
       
  1597 	    RProcess process;
       
  1598 	    getProcessErr = thread.Process( process );
       
  1599 	    CleanupClosePushL( process );
       
  1600 	    if ( getProcessErr == KErrNone )
       
  1601 	        {
       
  1602 	        TRACE( T_LIT( "CBSSession::ExtractInfoL Processfilename retrieved OK") );
       
  1603 	        TFileName fileName = process.FileName();	        
       
  1604 	        TParsePtrC parser( fileName );	        
       
  1605 	        TPtrC processFileNameAndExt( parser.NameAndExt() );
       
  1606 	        
       
  1607 	        RApaLsSession session;
       
  1608 	        TInt connectErr = session.Connect();
       
  1609 	        User :: LeaveIfError (connectErr);
       
  1610 	        session.GetAllApps();	        
       
  1611 	        CleanupClosePushL( session );
       
  1612 	        
       
  1613 	        TApaAppInfo info;	        
       
  1614 	        TPtrC captionPtr( KNullDesC );
       
  1615 	        TPtrC printCaptionPtr( KNullDesC );
       
  1616 	        TPtrC fullName( KNullDesC );
       
  1617 	        while ( KErrNone == session.GetNextApp( info ) )
       
  1618 	            {
       
  1619 	            fullName.Set( info.iFullName );
       
  1620 	            printCaptionPtr.Set( info.iCaption );	            
       
  1621 	            if( KErrNotFound != fullName.Find( processFileNameAndExt ) )
       
  1622 	                {	                
       
  1623 	                captionPtr.Set( info.iCaption );
       
  1624 	                TRACE( T_LIT( "CBSSession::ExtractInfoL caption saved: %S"),&captionPtr );
       
  1625 	                break;
       
  1626 	                }	            
       
  1627 	            }	        
       
  1628 	        
       
  1629 	        CBSSessionInfo* temp = 
       
  1630 	            CBSSessionInfo::NewL( processFileNameAndExt,
       
  1631 	                                  captionPtr,
       
  1632 	                                  threadId, 
       
  1633 	                                  process.Id() );
       
  1634 	        TRACE( T_LIT( "CBSSession::ExtractInfoL SessionInfo object creation OK") );
       
  1635 	        delete iSessionInfo;
       
  1636 	        iSessionInfo = NULL;
       
  1637 	        iSessionInfo = temp;	        
       
  1638 	        CleanupStack::PopAndDestroy( &session );
       
  1639 	        }
       
  1640 	    CleanupStack::PopAndDestroy( &process );
       
  1641 	    }    
       
  1642     CleanupStack::PopAndDestroy( &thread );    
       
  1643     TRACE( T_LIT( "CBSSession::ExtractInfoL end") );
       
  1644     }
       
  1645 
       
  1646 
       
  1647 // ---------------------------------------------------------------------------
       
  1648 // CBSSession::FileName
       
  1649 // ---------------------------------------------------------------------------
       
  1650 //
       
  1651 const TDesC& CBSSession::FileName()
       
  1652 	{
       
  1653 	TRACE( T_LIT( "CBSSession::FileName begin") );
       
  1654 	if ( iSessionInfo )
       
  1655 	    {
       
  1656 	    TRACE( T_LIT( "CBSSession::FileName SessionInfo exists") );
       
  1657 	    return iSessionInfo->FileName();
       
  1658 	    }
       
  1659 	else
       
  1660 	    {
       
  1661 	    TRACE( T_LIT( "CBSSession::FileName SessionInfo does not exist") );
       
  1662 	    return KNullDesC;
       
  1663 	    }
       
  1664 	}
       
  1665 
       
  1666 // ---------------------------------------------------------------------------
       
  1667 // CBSSession::Caption
       
  1668 // ---------------------------------------------------------------------------
       
  1669 //	
       
  1670 const TDesC& CBSSession::Caption()
       
  1671 	{
       
  1672 	TRACE( T_LIT( "CBSSession::Caption begin") );
       
  1673 	if ( iSessionInfo )
       
  1674 	    {
       
  1675 	    TRACE( T_LIT( "CBSSession::Caption SessionInfo exists") );
       
  1676 	    return iSessionInfo->Caption();
       
  1677 	    }
       
  1678 	else
       
  1679 	    {
       
  1680 	    TRACE( T_LIT( "CBSSession::Caption SessionInfo does not exist") );
       
  1681 	    return KNullDesC;
       
  1682 	    }
       
  1683 	}	
       
  1684 
       
  1685 
       
  1686 // ---------------------------------------------------------------------------
       
  1687 // CBSSession::ThreadId
       
  1688 // ---------------------------------------------------------------------------
       
  1689 //		
       
  1690 TInt CBSSession::ThreadId( TThreadId& aThreadId )
       
  1691 	{
       
  1692 	TRACE( T_LIT( "CBSSession::ThreadId begin") );
       
  1693 	TInt err( KErrNotFound );
       
  1694 	if ( iSessionInfo )
       
  1695 	    {	    
       
  1696 	    TRACE( T_LIT( "CBSSession::ThreadId SessionInfo exists") );
       
  1697 	    aThreadId = iSessionInfo->ThreadId();
       
  1698 	    err = KErrNone;
       
  1699 	    }
       
  1700 	return err;
       
  1701 	}
       
  1702 
       
  1703 
       
  1704 // ---------------------------------------------------------------------------
       
  1705 // CBSSession::ProcessId
       
  1706 // ---------------------------------------------------------------------------
       
  1707 //	
       
  1708 TInt CBSSession::ProcessId( TProcessId& aProcessId )
       
  1709 	{
       
  1710 	TRACE( T_LIT( "CBSSession::ProcessId begin") );
       
  1711 	TInt err( KErrNotFound );
       
  1712 	if ( iSessionInfo )
       
  1713 	    {	    
       
  1714 	    TRACE( T_LIT( "CBSSession::ProcessId SessionInfo exists") );
       
  1715 	    aProcessId = iSessionInfo->ProcessId();
       
  1716 	    err = KErrNone;
       
  1717 	    }
       
  1718 	return err;
       
  1719 	}
       
  1720 
       
  1721 
       
  1722 // ---------------------------------------------------------------------------
       
  1723 // CBSSession::InfoAvailable
       
  1724 // ---------------------------------------------------------------------------
       
  1725 //	
       
  1726 TBool CBSSession::InfoAvailable()
       
  1727     {
       
  1728     return iSessionInfo ? ETrue : EFalse;
       
  1729     }
       
  1730 
       
  1731 // END OF FILE
       
  1732 
       
  1733