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