homescreenpluginsrv/hspsmanager/src/hspsthemeserver.cpp
changeset 0 79c6a41cd166
child 3 ff572005ac23
equal deleted inserted replaced
-1:000000000000 0:79c6a41cd166
       
     1 /*
       
     2 * Copyright (c) 2008 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:  HSPS Theme Server. For more information, see the header.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #define __INCLUDE_CAPABILITY_NAMES__
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <centralrepository.h>
       
    23 #include <f32file.h>
       
    24 #include <bautils.h>
       
    25 #include <sbdefs.h>
       
    26 #include <sysversioninfo.h>
       
    27 #include <sysutil.h>
       
    28 
       
    29 #include "hsps_builds_cfg.hrh"
       
    30 #include "hspsthemeserver.h"
       
    31 #include "hspsthemeserversession.h"
       
    32 #include "hspssecurityenforcer.h"
       
    33 #include "hspsserverutil.h"
       
    34 #include "hspsdefinitionrepository.h"
       
    35 #include "hspsdefinitionengineinterface.h"
       
    36 #include "hspsconfiguration.h"
       
    37 #ifdef _hsps_PERFORMANCE_TEST_
       
    38 #include "hspstimemon.h"
       
    39 #endif //_hsps_PERFORMANCE_TEST_
       
    40 
       
    41 #ifndef __DISABLE_SISX_INSTALLATION_
       
    42 #include "hspsrominstaller.h"
       
    43 #include "hspsautoinstaller.h"
       
    44 #include "hspsinstaller.h"
       
    45 #include "hspsinstallationhandler.h"
       
    46 #endif //__DISABLE_SISX_INSTALLATION_
       
    47 
       
    48 #ifdef HSPS_LOG_ACTIVE
       
    49 #include <hspsodtdump.h>
       
    50 #include <hspslogbusfile.h>
       
    51 #endif
       
    52 
       
    53 // CONSTANTS
       
    54 
       
    55 // Directory for the SISX installation files
       
    56 _LIT( KImportDirectoryC, "c:\\private\\200159c0\\import\\" );
       
    57 
       
    58 // Directory for the ROM based installation files
       
    59 
       
    60 // Directories for backup folders
       
    61 _LIT( KBackupThemesDirectoryC, "c:\\private\\200159c0\\backup\\themes\\" );
       
    62 
       
    63 // Name of the installation file
       
    64 _LIT( KManifestFile, "manifest.dat" );
       
    65 
       
    66 // Name of the configuration file
       
    67 _LIT( KConfFile, "*.o0000" );
       
    68 
       
    69 // File prefixes for the *.DAT imports
       
    70 _LIT( KPrefixFileApp, "app_");
       
    71 _LIT( KPrefixFilePlugin, "plugin_");
       
    72 
       
    73 _LIT( KUpgradePlugin, "upgrade_plugin" );
       
    74 _LIT( KUpgrade, "upgrade" );
       
    75 _LIT( KDoubleBackSlash, "\\" );
       
    76 _LIT( KUpgradePluginWild, "upgrade_plugin_*.dat" );
       
    77 // Just in case emulator cannot read system version we use this to
       
    78 // prevent ROM installation on every boot up
       
    79 _LIT( KDummyVersionInfo, "dummyversioninfo" );
       
    80 
       
    81 const TUint KAppId_hspsAS =  270486738; //  0x101F4CD2 from S60 3.1 Xuikon AppShell in 3.1 product.
       
    82 const TUint KAppId_hspsAI =  271012080 ; // 0x102750F0 support for Xuikon-based ActiveIdle 
       
    83 const TUint KAppId_LE =    270551469 ; // 0x102049AD LayoutTest
       
    84 
       
    85 const TInt KAppIdAny = -1; // All applications
       
    86 
       
    87 // ======== LOCAL FUNCTIONS ========
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // ResetAndDestroyHBufCArray 
       
    91 // cleanup item for HBufC dynamic arrays
       
    92 // -----------------------------------------------------------------------------
       
    93 static void ResetAndDestroyHBufC8Array( TAny* aArray )
       
    94     {
       
    95     CArrayPtrSeg<HBufC8>* array = reinterpret_cast< CArrayPtrSeg<HBufC8>* >( aArray );
       
    96     array->ResetAndDestroy();
       
    97     delete array;
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // ResetAndDestroyChspsODTArray 
       
   102 // cleanup item for ChspsODT dynamic arrays
       
   103 // -----------------------------------------------------------------------------
       
   104 static void ResetAndDestroyChspsODTArray( TAny* aArray )
       
   105     {
       
   106     CArrayPtrSeg<ChspsODT>* array = reinterpret_cast< CArrayPtrSeg<ChspsODT>* >( aArray );
       
   107     array->ResetAndDestroy();
       
   108     delete array;
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // Callback function for removing repository lock if error occurs while repository is locked
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 static void UnlockRepository( TAny* aObject )
       
   116     {
       
   117     ChspsDefinitionRepository* DefRep = reinterpret_cast<ChspsDefinitionRepository*>( aObject );
       
   118         
       
   119     if (DefRep->Locked())
       
   120         {
       
   121         DefRep->Unlock();
       
   122         }
       
   123     }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // GetFWVersion
       
   127 // Gets firmware version of phone and sets it to buffer given as parameter
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 static void GetFWVersion( TDes& aVersion )
       
   131     {
       
   132     TInt err = SysVersionInfo::GetVersionInfo( SysVersionInfo::EFWVersion, aVersion );
       
   133     if ( err != KErrNone )
       
   134         {
       
   135         // emulator might not have fwid*.txt files so we need to read sw version
       
   136         // by using sysutil.
       
   137         aVersion.Zero();
       
   138         err = SysUtil::GetSWVersion( aVersion );
       
   139         if ( err != KErrNone )
       
   140             {
       
   141             // for some reason emulator cannot read version info
       
   142             aVersion.Copy( KDummyVersionInfo );
       
   143             }
       
   144         }
       
   145     }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // RunServerL()
       
   149 // Server startup code
       
   150 // Perform all server initialisation, in particular creation of the
       
   151 // scheduler and server and then run the scheduler
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 static void RunServerL()
       
   155     {
       
   156 #ifdef _hsps_PERFORMANCE_TEST_
       
   157     ChspsTimeMon::PrintUserMem( _L("hspsThemeServer::RunServerL(): - initialising..") );
       
   158 #endif//_hsps_PERFORMANCE_TEST_
       
   159     // naming the server thread after the server helps to debug panics
       
   160     User::LeaveIfError(User::RenameThread(KhspsThemeServerName));
       
   161     // create and install the active scheduler we need
       
   162     CActiveScheduler* s=new(ELeave) CActiveScheduler;
       
   163     CleanupStack::PushL(s);
       
   164     CActiveScheduler::Install(s);
       
   165     // create the server (leave it on the cleanup stack)
       
   166     ChspsThemeServer::NewLC();
       
   167     // Initialisation complete, now signal the client
       
   168     RProcess::Rendezvous(KErrNone);
       
   169     CActiveScheduler::Start();
       
   170     // Cleanup the server and scheduler
       
   171     CleanupStack::PopAndDestroy( 2, s );
       
   172 #ifdef _hsps_PERFORMANCE_TEST_
       
   173     ChspsTimeMon::PrintUserMem( _L("hspsThemeServer::RunServerL(): - server fully running") );
       
   174 #endif//_hsps_PERFORMANCE_TEST_
       
   175     }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // E32Main
       
   179 // Server process entry-point
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 TInt E32Main()
       
   183     {
       
   184     __UHEAP_MARK;
       
   185     CTrapCleanup* cleanup=CTrapCleanup::New();
       
   186     TInt r=KErrNoMemory;
       
   187     if (cleanup)
       
   188         {
       
   189         TRAP(r,RunServerL());
       
   190         delete cleanup;
       
   191         }
       
   192     __UHEAP_MARKEND;
       
   193     return r;
       
   194     }
       
   195     
       
   196 // ======== MEMBER FUNCTIONS ========
       
   197 
       
   198 #ifdef _hsps_SERVER_SHUTDOWN_ENABLED_
       
   199 // -----------------------------------------------------------------------------
       
   200 // CShutdown::NewL
       
   201 // (other items were commented in a header).
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 CShutdown* CShutdown::NewL( MShutdownObserver& aObserver )
       
   205     {
       
   206     CShutdown* object = new (ELeave) CShutdown( aObserver );
       
   207     CleanupStack::PushL( object );
       
   208     object->ConstructL();
       
   209     CleanupStack::Pop( object );
       
   210     return object;    
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CShutdown::CShutdown
       
   215 // (other items were commented in a header).
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 CShutdown::CShutdown( MShutdownObserver& aObserver ) : CTimer(-1), iObserver( aObserver )
       
   219     {
       
   220     CActiveScheduler::Add(this);
       
   221     }
       
   222   
       
   223 // -----------------------------------------------------------------------------
       
   224 // CShutdown::ConstructL
       
   225 // (other items were commented in a header).
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 void CShutdown::ConstructL()
       
   229     {
       
   230     CTimer::ConstructL();
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CShutdown::Start
       
   235 // (other items were commented in a header).
       
   236 // -----------------------------------------------------------------------------
       
   237 //
       
   238 void CShutdown::Start()
       
   239     {
       
   240     After( KhspsShutdownDelay );
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // CShutdown::RunL
       
   245 // (other items were commented in a header).
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 void CShutdown::RunL()
       
   249     {
       
   250     // Notify observer.
       
   251     iObserver.ShutdownInitiating();
       
   252     
       
   253     // Stop active scheduler.
       
   254     CActiveScheduler::Stop();
       
   255     }
       
   256 #endif // _hsps_SERVER_SHUTDOWN_ENABLED_
       
   257 
       
   258 // -----------------------------------------------------------------------------
       
   259 // ChspsThemeServer::ChspsThemeServer
       
   260 // (other items were commented in a header).
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 inline ChspsThemeServer::ChspsThemeServer():CPolicyServer(0,hspsThemePolicy,ESharableSessions)
       
   264     {
       
   265     }
       
   266   
       
   267 // -----------------------------------------------------------------------------
       
   268 // ChspsThemeServer::NewLC
       
   269 // (other items were commented in a header).
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 CServer2* ChspsThemeServer::NewLC()
       
   273     {
       
   274 #ifdef _hsps_PERFORMANCE_TEST_ 
       
   275     ChspsTimeMon::PrintUserMem( _L("SERVER: - ready to install server.") );
       
   276 #endif    
       
   277     ChspsThemeServer* self=new(ELeave) ChspsThemeServer;
       
   278     CleanupStack::PushL(self);
       
   279     self->ConstructL();
       
   280 
       
   281 #ifdef _hsps_PERFORMANCE_TEST_ 
       
   282     ChspsTimeMon::PrintUserMem( _L("SERVER: - server installed.") );
       
   283 #endif        
       
   284     return self;
       
   285     }
       
   286 
       
   287 // -----------------------------------------------------------------------------
       
   288 // ChspsThemeServer::ConstructL
       
   289 // 2nd phase construction - ensure the timer and server objects are running
       
   290 // (other items were commented in a header).
       
   291 // -----------------------------------------------------------------------------
       
   292 //
       
   293 void ChspsThemeServer::ConstructL()
       
   294     {
       
   295     User::LeaveIfError( iFsSession.Connect() );
       
   296     
       
   297 #ifdef HSPS_LOG_ACTIVE    
       
   298     iLogBus = ChspsLogBusFile::NewL( ChspsLogBusFile::CreateLogFilename( _L("themeserver") ) );
       
   299     iLogBus->LogText( _L( "--------------------------------------------------------" ) );
       
   300     iLogBus->LogText( _L( "hspsThemeServer: constructing server" ) );
       
   301 #endif
       
   302            
       
   303     // Setup a runtime array of headers (=header cache)
       
   304     iHeaderListCache = new ( ELeave ) CArrayPtrSeg< ChspsODT >( KHeaderListGranularity );
       
   305         
       
   306     iDefinitionRepository = ChspsDefinitionRepository::NewL();
       
   307 #ifdef HSPS_LOG_ACTIVE    
       
   308     iDefinitionRepository->SetLogBus( iLogBus );
       
   309 #endif
       
   310     // Setup a Plug-in Repository listener
       
   311     iCentralRepository = CRepository::NewL( KhspsThemeStatusRepositoryUid ); 
       
   312     
       
   313     // Get active device language
       
   314     iDeviceLanguage = GetDeviceLanguage();
       
   315             
       
   316     // Setup a search mask for finding headers from the cache
       
   317     iCacheMask = ChspsODT::NewL();
       
   318 
       
   319     // Setup a listener for changes in the Central Repository
       
   320     iCenRepListener = ChspsCenRepListener::NewL( *this, KhspsThemeStatusRepositoryUid );        
       
   321     
       
   322 	// Access lists for client processes
       
   323     iSecurityEnforcer = ChspsSecurityEnforcer::NewL( *iDefinitionRepository, *iCentralRepository );
       
   324 #ifdef HSPS_LOG_ACTIVE    
       
   325      iSecurityEnforcer->SetLogBus( iLogBus );
       
   326 #endif     
       
   327     
       
   328     // Find header files from the Plug-in Repository
       
   329 	UpdateHeaderListCacheL();  
       
   330     
       
   331     // Adds the server with the specified name to the active scheduler, and issues the 
       
   332     // first request for messages, and leaves if the operation fails
       
   333     StartL( KhspsThemeServerName );
       
   334     
       
   335     // Initialize remove index
       
   336     iThemeIndexToRemove = KErrNotFound;
       
   337                            
       
   338     // Listen to changes in any key
       
   339     iCenRepListener->SetupAll();     
       
   340         
       
   341     // Start observing the notifications
       
   342     iDefinitionRepository->RegisterObserverL( *this );
       
   343            
       
   344 	// Resolution & orientation change listener
       
   345 #if defined(WINSCW) || defined(__WINS__)
       
   346     iFamilyListener = ChspsFamilyListener::NewL( *this );
       
   347 #endif // defined(WINSCW)    
       
   348     
       
   349     // Auto-localize ODTs in the Definition Repository when the device language has changed
       
   350     HandleLanguageChangeL();            
       
   351     
       
   352     // Installs manifest files from the ROM drive when server is starting for the first time
       
   353     HandleRomInstallationsL();
       
   354         
       
   355 #ifndef __DISABLE_SISX_INSTALLATION_
       
   356     // Start observing of import folder to enable autoinstallations
       
   357     EnableAutoInstallationL();
       
   358 #endif //__DISABLE_SISX_INSTALLATION_    
       
   359 
       
   360     // Update configurations
       
   361     // When server is started all configurations with "WaitForConfirmation" state are updated
       
   362     // to "NotConfirmed" state to allow client to validate configurations
       
   363     User::LeaveIfError( iCentralRepository->Set( KCenrepKeyCheckConf, 0 ) );
       
   364     CheckConfigurationL( KAppIdAny );
       
   365     // Activate configuration checking
       
   366     // Configurations are checked every time when a new client session is created
       
   367     User::LeaveIfError( iCentralRepository->Set( KCenrepKeyCheckConf, 1 ) );
       
   368 
       
   369     // Backup event listener
       
   370     iBRObserver = CHSPSBRObserver::NewL( *this );
       
   371     iBRObserver->Start();
       
   372 
       
   373     // Initialize backup state
       
   374     iBRState = EhspsBRNormal;
       
   375 
       
   376 #ifdef _hsps_SERVER_SHUTDOWN_ENABLED_    
       
   377     iShutdown = CShutdown::NewL( *this );
       
   378 #endif // _hsps_SERVER_SHUTDOWN_ENABLED_
       
   379     
       
   380 #ifdef HSPS_LOG_ACTIVE	
       
   381     iLogBus->LogText( _L( "hspsThemeServer: server fully constructed" ) );
       
   382     iLogBus->LogText( _L( "--------------------------------------------------------" ) );
       
   383 #endif    
       
   384     }
       
   385 
       
   386 // Destructor
       
   387 ChspsThemeServer::~ChspsThemeServer()
       
   388     {
       
   389 #ifdef _hsps_SERVER_SHUTDOWN_ENABLED_
       
   390     delete iShutdown;
       
   391     iShutdown = NULL;
       
   392 #else
       
   393     // Server must call cleanup if shutdown object does not exist (and call it).
       
   394     Cleanup();
       
   395 #endif // _hsps_SERVER_SHUTDOWN_ENABLED_        
       
   396     
       
   397 #ifdef HSPS_LOG_ACTIVE    
       
   398     // Delete log bus as absolute last!
       
   399     // Some members might log destructor activity to this object.
       
   400     delete iLogBus;
       
   401     iLogBus = NULL;
       
   402 #endif    
       
   403     }
       
   404 
       
   405 #ifdef _hsps_SERVER_SHUTDOWN_ENABLED_
       
   406 // -----------------------------------------------------------------------------
       
   407 // ChspsThemeServer::ShutdownInitiating
       
   408 // From MShutdownObserver.
       
   409 // -----------------------------------------------------------------------------
       
   410 //
       
   411 void ChspsThemeServer::ShutdownInitiating()
       
   412     {
       
   413     Cleanup();
       
   414     }
       
   415 #endif // #ifdef _hsps_SERVER_SHUTDOWN_ENABLED_
       
   416 
       
   417 // -----------------------------------------------------------------------------
       
   418 // ChspsThemeServer::Cleanup
       
   419 // (other items were commented in a header).
       
   420 // -----------------------------------------------------------------------------
       
   421 //
       
   422 void ChspsThemeServer::Cleanup()
       
   423     {
       
   424 #ifdef HSPS_LOG_ACTIVE      
       
   425     iLogBus->LogText( _L( "hspsThemeServer: server is shutting down" ) );
       
   426 #endif    
       
   427             
       
   428     if( iDefinitionRepository )
       
   429         {
       
   430         iDefinitionRepository->UnregisterObserver( *this );
       
   431         }
       
   432 
       
   433 #ifndef __DISABLE_SISX_INSTALLATION_
       
   434     DisableAutoInstallation();
       
   435 #endif //__DISABLE_SISX_INSTALLATION_
       
   436 
       
   437 #if defined(WINSCW) || defined(__WINS__)    
       
   438     delete iFamilyListener;
       
   439     iFamilyListener = NULL;
       
   440 #endif // defined(WINSCW)    
       
   441     
       
   442     delete iCenRepListener;
       
   443     iCenRepListener = NULL;
       
   444     
       
   445     delete iRomInstaller;
       
   446     iRomInstaller = NULL;
       
   447     
       
   448     delete iAutoInstaller;    
       
   449     iAutoInstaller = NULL;
       
   450     
       
   451     if( iHeaderListCache )
       
   452         {
       
   453         iHeaderListCache->ResetAndDestroy();
       
   454         delete iHeaderListCache;  
       
   455         iHeaderListCache = NULL;
       
   456         }
       
   457              
       
   458     delete iDefinitionRepository;
       
   459     iDefinitionRepository = NULL;
       
   460     
       
   461     delete iCentralRepository;
       
   462     iCentralRepository = NULL;
       
   463     
       
   464     delete iSecurityEnforcer;
       
   465     iSecurityEnforcer = NULL;
       
   466     
       
   467     delete iCacheMask;
       
   468     delete iBRObserver;
       
   469     delete iActiveBackupClient;
       
   470     delete iBRHandler;
       
   471 
       
   472     iCacheMask = NULL;
       
   473     
       
   474     iFsSession.Close();    
       
   475     }
       
   476 
       
   477 // -----------------------------------------------------------------------------
       
   478 // ChspsThemeServer::NewSessionL
       
   479 // Create a new client session. This should really check the version number.
       
   480 // (other items were commented in a header).
       
   481 // -----------------------------------------------------------------------------
       
   482 //
       
   483 CSession2* ChspsThemeServer::NewSessionL(const TVersion&,const RMessage2& aMessage ) const
       
   484     {
       
   485     return new (ELeave) ChspsThemeServerSession( aMessage.SecureId().iId );
       
   486     }
       
   487 
       
   488 // -----------------------------------------------------------------------------
       
   489 // ChspsThemeServer::AddSession
       
   490 // A new session is being created
       
   491 // (other items were commented in a header).
       
   492 // -----------------------------------------------------------------------------
       
   493 //
       
   494 void ChspsThemeServer::AddSession()
       
   495     {
       
   496     iSessionCount++;
       
   497 
       
   498 #ifdef _hsps_SERVER_SHUTDOWN_ENABLED_    
       
   499     if( iShutdown->IsActive() )
       
   500         {
       
   501         iShutdown->Cancel();
       
   502         }        
       
   503 #endif // _hsps_SERVER_SHUTDOWN_ENABLED_
       
   504 
       
   505 #ifdef HSPS_LOG_ACTIVE    
       
   506     iLogBus->LogText( _L( "ChspsThemeServer::AddSession(): - now %d concurrent sessions." ),
       
   507             iSessionCount );
       
   508 #endif    
       
   509     }
       
   510 
       
   511 
       
   512 // -----------------------------------------------------------------------------
       
   513 // ChspsThemeServer::DropSession
       
   514 // A session is being destroyed
       
   515 // (other items were commented in a header).
       
   516 // -----------------------------------------------------------------------------
       
   517 //
       
   518 void ChspsThemeServer::DropSession()
       
   519     {
       
   520     iSessionCount--;
       
   521     
       
   522 #ifdef _hsps_SERVER_SHUTDOWN_ENABLED_    
       
   523     if( iSessionCount == 0 )
       
   524         {
       
   525         iShutdown->Cancel();
       
   526         iShutdown->Start();
       
   527         }
       
   528 #endif // _hsps_SERVER_SHUTDOWN_ENABLED_
       
   529 
       
   530 #ifdef HSPS_LOG_ACTIVE    
       
   531     iLogBus->LogText( _L( "ChspsThemeServer::DropSession(): - %d concurrent sessions left." ),
       
   532             iSessionCount );
       
   533 #endif    
       
   534     }
       
   535   
       
   536 // -----------------------------------------------------------------------------
       
   537 // ChspsThemeServer::IncreaseRequestClientCount
       
   538 // (other items were commented in a header).
       
   539 // -----------------------------------------------------------------------------
       
   540 //
       
   541 void ChspsThemeServer::IncreaseRequestClientCount()
       
   542     {
       
   543 #ifdef HSPS_LOG_ACTIVE    
       
   544     iLogBus->LogText( _L( "ChspsThemeServer: ChspsThemeServer::IncreaseRequestClientCount()" ) );
       
   545 #endif
       
   546     
       
   547     iRequestClientCount++;
       
   548     } 
       
   549   
       
   550 // -----------------------------------------------------------------------------
       
   551 // ChspsThemeServer::DecreaseRequestClientCount
       
   552 // (other items were commented in a header).
       
   553 // -----------------------------------------------------------------------------
       
   554 //
       
   555 void ChspsThemeServer::DecreaseRequestClientCount()
       
   556     {
       
   557 #ifdef HSPS_LOG_ACTIVE    
       
   558     iLogBus->LogText( _L( "ChspsThemeServer: ChspsThemeServer::DecreaseRequestClientCount()" ) );
       
   559 #endif    
       
   560 
       
   561     if ( iRequestClientCount )
       
   562         {
       
   563         iRequestClientCount--;
       
   564         }
       
   565     } 
       
   566     
       
   567     
       
   568 // -----------------------------------------------------------------------------
       
   569 // ChspsThemeServer::HandleDefinitionRespositoryEvent()
       
   570 // Handles events coming from hspsDefinitionRepository.
       
   571 // (other items were commented in a header).
       
   572 // -----------------------------------------------------------------------------
       
   573 //
       
   574 TBool ChspsThemeServer::HandleDefinitionRespositoryEvent( ThspsRepositoryInfo aRepositoryInfo )
       
   575     {
       
   576     TInt mask = aRepositoryInfo.iEventType;
       
   577     TBool ret = EFalse;
       
   578 
       
   579 #ifdef HSPS_LOG_ACTIVE
       
   580     iLogBus->LogText( _L( "--------------------------------------------------------" ) );
       
   581 #endif        
       
   582             
       
   583     // If header cache should be updated from files in the Plug-in Repository
       
   584     if (mask & EhspsCacheUpdate)
       
   585         {
       
   586 #ifdef HSPS_LOG_ACTIVE        
       
   587         iLogBus->LogText( _L( "ChspsThemeServer::HandleDefinitionRespositoryEvent(): - EhspsCacheUpdate occurred." ) );
       
   588 #endif        
       
   589         // Update the cache from contents of the themes folder in drive C (slowish operation!) 
       
   590         TRAP_IGNORE( UpdateHeaderListCacheL() );  
       
   591         }
       
   592         
       
   593     // If a plug-in configuration was uninstalled from the Plug-in Repository
       
   594     if (mask & EhspsClean)
       
   595         {
       
   596 #ifdef HSPS_LOG_ACTIVE        
       
   597         iLogBus->LogText( _L( "ChspsThemeServer::HandleDefinitionRespositoryEvent(): - EhspsClean occurred." ) );
       
   598 #endif                  
       
   599         if(aRepositoryInfo.iLastNotification)
       
   600             {
       
   601             // Remove the provided ODT from the header list cache 
       
   602                     UpdateHeaderListCache(
       
   603                         EhspsCacheRemoveHeader,
       
   604                         aRepositoryInfo.iPluginIfUid,
       
   605                         aRepositoryInfo.iPluginProviderUid,
       
   606                         aRepositoryInfo.iPluginUid
       
   607                         );
       
   608             }
       
   609           
       
   610         }
       
   611     
       
   612     // If a plug-in configuration was installed into the Plug-in Repository
       
   613     if (mask & EhspsODTAdded)
       
   614         {
       
   615 #ifdef HSPS_LOG_ACTIVE        
       
   616         iLogBus->LogText( _L( "ChspsThemeServer::HandleDefinitionRespositoryEvent(): - EhspsODTAdded occurred." ) );
       
   617 #endif  
       
   618         if(aRepositoryInfo.iLastNotification)
       
   619             {
       
   620             // Add the provided ODT to the header list cache
       
   621                     UpdateHeaderListCache(
       
   622                         EhspsCacheAddHeader,
       
   623                         aRepositoryInfo.iPluginIfUid,
       
   624                         aRepositoryInfo.iPluginProviderUid,
       
   625                         aRepositoryInfo.iPluginUid
       
   626                         );
       
   627             }
       
   628                                  
       
   629         }
       
   630 
       
   631     // If a plug-in configuration was upgraded into the Plug-in Repository
       
   632     if( mask & EhspsODTUpdated )
       
   633         {
       
   634 #ifdef HSPS_LOG_ACTIVE        
       
   635         iLogBus->LogText( _L( "ChspsThemeServer::HandleDefinitionRespositoryEvent(): - EhspsODTUpdate occurred." ) );
       
   636 #endif  
       
   637         if( aRepositoryInfo.iLastNotification )
       
   638             {
       
   639             // Add the provided ODT to the header list cache
       
   640             UpdateHeaderListCache(
       
   641                 EhspsCacheAddHeader,
       
   642                 aRepositoryInfo.iPluginIfUid,
       
   643                 aRepositoryInfo.iPluginProviderUid,
       
   644                 aRepositoryInfo.iPluginUid
       
   645                 );
       
   646             }                                 
       
   647         }    
       
   648     
       
   649     // If settings of an application configuration were modified
       
   650     if (mask & EhspsSettingsChanged)
       
   651         {
       
   652 #ifdef HSPS_LOG_ACTIVE        
       
   653         iLogBus->LogText( _L( "ChspsThemeServer::HandleDefinitionRespositoryEvent(): - EhspsSettingsChanged occurred." ) );
       
   654 #endif
       
   655         }
       
   656 
       
   657     // If a plug-in configuration was added into or moved within an application configuration
       
   658     if (mask & EhspsODTModified)
       
   659         {
       
   660 #ifdef HSPS_LOG_ACTIVE        
       
   661         iLogBus->LogText( _L( "ChspsThemeServer::HandleDefinitionRespositoryEvent(): - EhspsODTModified occured." ) );
       
   662 #endif
       
   663         }
       
   664     
       
   665     // If a plug-in configuration was removed from an application configuration
       
   666     if (mask & EhspsODTRemoved)
       
   667         {
       
   668 #ifdef HSPS_LOG_ACTIVE        
       
   669         iLogBus->LogText( _L( "ChspsThemeServer::HandleDefinitionRespositoryEvent(): - EhspsODTRemoved occurred." ) );
       
   670 #endif        
       
   671         }
       
   672 
       
   673 #ifdef HSPS_LOG_ACTIVE
       
   674     iLogBus->LogText( _L( "--------------------------------------------------------" ) );
       
   675 #endif
       
   676     
       
   677     return ret;
       
   678     }
       
   679     
       
   680 // -----------------------------------------------------------------------------
       
   681 // ChspsThemeServer::DefinitionRepository
       
   682 // (other items were commented in a header).
       
   683 // -----------------------------------------------------------------------------
       
   684 //
       
   685 ChspsDefinitionRepository& ChspsThemeServer::DefinitionRepository()
       
   686     {
       
   687     return *iDefinitionRepository;
       
   688     } 
       
   689   
       
   690 // ChspsThemeServer::SecurityEnforcer
       
   691 // (other items were commented in a header).
       
   692 // -----------------------------------------------------------------------------
       
   693 //
       
   694 ChspsSecurityEnforcer& ChspsThemeServer::SecurityEnforcer()
       
   695     {
       
   696     return *iSecurityEnforcer;
       
   697     } 
       
   698   
       
   699   
       
   700 // ChspsThemeServer::CentralRepository
       
   701 // (other items were commented in a header).
       
   702 // -----------------------------------------------------------------------------
       
   703 //
       
   704 CRepository& ChspsThemeServer::CentralRepository()
       
   705     {
       
   706     return *iCentralRepository;
       
   707     }
       
   708   
       
   709 // ChspsThemeServer::DeviceLaguage
       
   710 // (other items were commented in a header).
       
   711 // -----------------------------------------------------------------------------
       
   712 //
       
   713 TLanguage ChspsThemeServer::DeviceLanguage()
       
   714     {
       
   715     iDeviceLanguage = GetDeviceLanguage();
       
   716     return iDeviceLanguage;
       
   717     }   
       
   718   
       
   719 // -----------------------------------------------------------------------------
       
   720 // ChspsThemeServer::UpdateHeaderListCache
       
   721 // (other items were commented in a header).
       
   722 // -----------------------------------------------------------------------------
       
   723 //
       
   724 CArrayPtrSeg<ChspsODT>& ChspsThemeServer::HeaderListCache()
       
   725     {
       
   726     return *iHeaderListCache;
       
   727     }   
       
   728 
       
   729 // -----------------------------------------------------------------------------
       
   730 // ChspsThemeServer::UninstallImport
       
   731 // -----------------------------------------------------------------------------
       
   732 //
       
   733 void ChspsThemeServer::UninstallImport( 
       
   734 		const CDir* aFilelist )
       
   735 	{
       
   736 	TInt count( 0 );
       
   737 	if ( aFilelist )
       
   738 		{
       
   739 		count = aFilelist->Count();
       
   740 		}
       
   741 		
       
   742 	// Loop installed configurations
       
   743     for( TInt i = 0; i < iInstalledSisxThemes.Count(); i++ )
       
   744         {
       
   745         // Suppose this is the one which was removed
       
   746         iThemeIndexToRemove = i;
       
   747         
       
   748         // Loop files under the imports folder 
       
   749         for( TInt j = 0; j < count; j++ )
       
   750             {
       
   751             TPtrC filename( ( *aFilelist )[j].iName );
       
   752             
       
   753             // If theme path exists
       
   754             if( filename == *( iInstalledSisxThemes[i] ) )
       
   755                 {
       
   756                 // Break inner loop
       
   757                 iThemeIndexToRemove = KErrNotFound; 
       
   758                 break;                       
       
   759                 }
       
   760             }
       
   761         
       
   762         // iThemeIndexToRemove should now point the configuration which was removed
       
   763         RunAutoInstaller( EAutoInstallerUninstall );                                                   
       
   764         }  	
       
   765 	}
       
   766 
       
   767 // -----------------------------------------------------------------------------
       
   768 // ChspsThemeServer::GetManifestFromImportLC
       
   769 // -----------------------------------------------------------------------------
       
   770 //
       
   771 HBufC* ChspsThemeServer::GetManifestFromImportLC(
       
   772 		const TDesC& aImportFile,
       
   773 		const TDesC& aPath )		
       
   774 	{	
       
   775 	HBufC* manifestBuf = 0;
       
   776 	
       
   777 	const TChar KCharUnderscore('_');
       
   778 
       
   779     // Strip file prefix 
       
   780     TPtrC fixedName;        	                                
       
   781     TInt offset = aImportFile.Locate( KCharUnderscore );
       
   782     if( offset )
       
   783         {
       
   784         TPtrC filePrefix( aImportFile.Left(offset+1) );
       
   785         
       
   786         // If a valid prefix
       
   787         if ( filePrefix.CompareF( KPrefixFileApp) || filePrefix.CompareF( KPrefixFilePlugin ) )
       
   788         	{
       
   789         	fixedName.Set( aImportFile.Mid( filePrefix.Length() ) );
       
   790         	}
       
   791         }    	
       
   792     
       
   793     // If stripping was successfull
       
   794     if ( fixedName.Length() )
       
   795     	{
       
   796 	    // Remove file extension
       
   797 	    TParsePtrC parsePtr( fixedName );            		   
       
   798 	    TPtrC fileName( parsePtr.Name() );
       
   799 	    
       
   800 	    // Setup path for the manifest file
       
   801 	    manifestBuf = HBufC::NewLC( KMaxFileName );
       
   802 	    TPtr manifestPtr( manifestBuf->Des() );		    
       
   803 	    manifestPtr.Append( aPath );
       
   804 	    manifestPtr.Append( fileName );
       
   805 	                                                                                
       
   806 	    // Replace underscores with backslashes
       
   807 	    TInt pos( manifestPtr.Locate( KCharUnderscore ) );                   	            		    
       
   808 	    while( pos != KErrNotFound )
       
   809 	        {
       
   810 	        manifestPtr.Replace( pos, 1, KDoubleBackSlash );
       
   811 	        pos = manifestPtr.Locate( KCharUnderscore );                
       
   812 	        }		           
       
   813 	    manifestPtr.Append( KDoubleBackSlash );            
       
   814 	
       
   815 	    // Find a manifest.dat file from the generated path
       
   816 	    CDir* manifestDir( NULL );		                      
       
   817 	    TFindFile fileFinder( iFsSession );
       
   818 	    fileFinder.FindWildByDir( KManifestFile, manifestPtr, manifestDir );    
       
   819 	    
       
   820 	    // Check whether the manifest was found
       
   821 	    if( !manifestDir || manifestDir->Count() > 1 )
       
   822 	        {
       
   823 	        // Failed
       
   824 	        CleanupStack::PopAndDestroy( 1, manifestBuf );
       
   825 	        manifestBuf = NULL;
       
   826 	        }                                            
       
   827 	    else
       
   828 	        {    
       
   829 	        manifestPtr.Append( KManifestFile );
       
   830 	        }	    
       
   831 	    
       
   832 	    delete manifestDir;
       
   833 	    manifestDir = NULL;
       
   834     	}
       
   835     
       
   836     return manifestBuf;
       
   837 	}
       
   838 
       
   839 // -----------------------------------------------------------------------------
       
   840 // ChspsThemeServer::CheckConfiguration
       
   841 // -----------------------------------------------------------------------------
       
   842 //
       
   843 void ChspsThemeServer::CheckConfigurationL(
       
   844     const TInt aAppUid )        
       
   845     {
       
   846     
       
   847     if ( iHeaderListCache )
       
   848         {
       
   849         TInt count = iHeaderListCache->Count();
       
   850         if ( iDefinitionRepository->Locked() )
       
   851             {
       
   852             User::Leave( KErrInUse );
       
   853             }
       
   854         iDefinitionRepository->Lock();
       
   855 
       
   856         // Check if configuration checking is activated
       
   857         TPtrC8 confState;
       
   858         TPtrC8 nextConfState;
       
   859         TInt value( 0 );
       
   860         User::LeaveIfError( iCentralRepository->Get( KCenrepKeyCheckConf, value ) );
       
   861         if ( value == 1 )
       
   862             {
       
   863             // Configuration checking activated
       
   864             // All configurations with "WaitForConfiguration" state will be updated to
       
   865             // "Error" state
       
   866             confState.Set( KConfStateWaitForConfirmation );
       
   867             nextConfState.Set( KConfStateError );
       
   868             }
       
   869         else
       
   870             {
       
   871             // Configuration checking not activated
       
   872             // All configurations with "WaitForConfiguration" state will be updated to
       
   873             // "NotConfirmed" state
       
   874             confState.Set( KConfStateWaitForConfirmation );
       
   875             nextConfState.Set( KConfStateNotConfirmed );
       
   876             }
       
   877         
       
   878         for ( TInt i = 0; i < count; i++ )
       
   879             {
       
   880             ChspsODT* header = ( *iHeaderListCache )[ i ];
       
   881             if ( header->ConfigurationType() == EhspsAppConfiguration &&
       
   882                  ( header->RootUid() == aAppUid ||
       
   883                    aAppUid == KAppIdAny ) )
       
   884                 {
       
   885                 // Get ODT from definition repository
       
   886                 
       
   887                 ChspsODT* odt = ChspsODT::NewL();
       
   888                 CleanupStack::PushL( odt );
       
   889                 odt->SetRootUid( header->RootUid() );
       
   890                 odt->SetThemeUid( header->ThemeUid() );        
       
   891                 odt->SetConfigurationType( header->ConfigurationType() );
       
   892                 odt->SetRootUid( header->RootUid() );
       
   893                 odt->SetProviderUid( header->ProviderUid() );
       
   894                 odt->SetThemeUid( header->ThemeUid() );
       
   895                 odt->SetProviderNameL( header->ProviderName() );
       
   896                 odt->SetThemeFullNameL( header->ThemeFullName() );
       
   897                 odt->SetThemeShortNameL( header->ThemeShortName() );
       
   898                 odt->SetThemeVersionL( header->ThemeVersion() );            
       
   899                 odt->SetFlags( header->Flags() );                                                                       
       
   900                 odt->SetMultiInstance( header->MultiInstance() );
       
   901 
       
   902                 User::LeaveIfError( iDefinitionRepository->GetOdtL( *odt ) );
       
   903  
       
   904                 // Update configuration states
       
   905                 TBool odtUpdated( EFalse );
       
   906                 hspsServerUtil::UpdateConfigurationStateL(
       
   907                     *odt,
       
   908                     confState,
       
   909                     nextConfState,
       
   910                     odtUpdated );
       
   911                 if ( odtUpdated )
       
   912                     {
       
   913                     // Update modified ODT to definition repository
       
   914                     User::LeaveIfError( iDefinitionRepository->SetOdtL( *odt ) );
       
   915                     }
       
   916                 
       
   917                 CleanupStack::PopAndDestroy( odt );
       
   918                 }
       
   919             }
       
   920         iDefinitionRepository->Unlock();
       
   921         }
       
   922     }
       
   923 
       
   924 // -----------------------------------------------------------------------------
       
   925 // ChspsThemeServer::InstallImportsL
       
   926 // -----------------------------------------------------------------------------
       
   927 //
       
   928 void ChspsThemeServer::InstallImportsL( 
       
   929 		const CDir& aFilelist )
       
   930 	{		                        
       
   931     const TInt count( aFilelist.Count() );
       
   932 		
       
   933     // Go through the filtered file list
       
   934     for( TInt importIndex = 0; importIndex < count; importIndex++ )
       
   935         {        
       
   936         TBool found( EFalse );
       
   937         TPtrC importName( aFilelist[importIndex].iName );
       
   938         	
       
   939     	// Is the file already pending an installation?
       
   940         for( TInt j = 0; !found && j < iInstallableSisxThemes.Count(); j++ )
       
   941             {                
       
   942             if( importName == *( iInstallableSisxThemes[j] ) )
       
   943                 {
       
   944                 found = ETrue;
       
   945                 }                    
       
   946             }  
       
   947         
       
   948         // Is the file already installed?
       
   949         for( TInt j = 0; !found && j < iInstalledSisxThemes.Count(); j++ )
       
   950             {
       
   951             if( importName == *( iInstalledSisxThemes[j] ) )
       
   952                 {
       
   953                 found = ETrue;
       
   954                 }
       
   955             }
       
   956         
       
   957         if ( !found )
       
   958         	{
       
   959         	// Get manifest from the import's name in C drive 
       
   960         	HBufC* manifestBuf = GetManifestFromImportLC( 
       
   961         	        importName, 
       
   962         	        KImportDirectoryC );        	
       
   963         	if( manifestBuf )
       
   964         		{        		
       
   965         		// Append the manifest 
       
   966         		iManifestFiles.AppendL( manifestBuf );
       
   967 	            CleanupStack::Pop( manifestBuf );          
       
   968 	            
       
   969 	        	// Append the import
       
   970 		        iInstallableSisxThemes.AppendL( importName.AllocL() );
       
   971         		}
       
   972         	}
       
   973         }
       
   974     
       
   975     // Now that the manifest list has been parsed, initiate installation of 
       
   976     // the new manifest files
       
   977     if( iAutoInstallerState != EAutoInstallerStateInstall )
       
   978         {                        
       
   979         RunAutoInstaller( EAutoInstallerStateInstall );                                      
       
   980         }           
       
   981 	}
       
   982 
       
   983 // -----------------------------------------------------------------------------
       
   984 // ChspsThemeServer::HandleConfigurationImportsL
       
   985 // -----------------------------------------------------------------------------
       
   986 //
       
   987 void ChspsThemeServer::HandleConfigurationImportsL(
       
   988 		const TDesC& aFilePrefix )
       
   989 	{
       
   990 	// Set mask for finding files under imports folder
       
   991     TPtrC filter( KFilterAllPluginImportsV1 ); 
       
   992     if ( aFilePrefix.CompareF( KPrefixFileApp ) == 0 )
       
   993     	{
       
   994     	filter.Set( KFilterAllAppImportsV1 );
       
   995     	}
       
   996 
       
   997     // Get a listing of plugin_*.dat files from the imports folder in C drive
       
   998 	CDir* importsDir( NULL );  	        
       
   999     TFindFile fileFinder( iFsSession );
       
  1000     fileFinder.FindWildByDir( filter, KImportDirectoryC, importsDir );    
       
  1001     CleanupStack::PushL( importsDir );
       
  1002     
       
  1003     // pathFileList can be NULL when no path_*.dat files are available  in the import folder. 
       
  1004     // This can mean that currently the last sisx package is under unistallation, and because 
       
  1005     // of that, we need to check that case with pathFileCount = 0
       
  1006     TInt importsCount( 0 );    
       
  1007     if( importsDir )
       
  1008         {
       
  1009         importsCount = importsDir->Count();
       
  1010         }
       
  1011     
       
  1012     // Find out number of installed themes with the provided file prefix
       
  1013     TInt installedCount = 0;    
       
  1014     const TInt prefixLength = aFilePrefix.Length();
       
  1015     for( TInt i=0; i < iInstalledSisxThemes.Count(); i++  )
       
  1016     	{  
       
  1017     	TPtrC ptr( iInstalledSisxThemes[ i ]->Des() );    	
       
  1018     	if ( ptr.Length() > prefixLength )
       
  1019     		{
       
  1020     		if ( ptr.Left( prefixLength ).CompareF( aFilePrefix ) == 0 )
       
  1021     			{
       
  1022     			installedCount++;
       
  1023     			}
       
  1024     		}
       
  1025     	}
       
  1026     
       
  1027     if( importsCount < installedCount )        
       
  1028         {
       
  1029         // there were less *.dat files than in the last evaluation
       
  1030         UninstallImport( importsDir );         
       
  1031         }                                
       
  1032     else if( importsCount > installedCount && importsDir )
       
  1033         {
       
  1034         // If a *.dat file was added since the last evaluation        
       
  1035         InstallImportsL( *importsDir );
       
  1036         }
       
  1037 
       
  1038     CleanupStack::PopAndDestroy( importsDir );                  	
       
  1039 	}
       
  1040 
       
  1041 // -----------------------------------------------------------------------------
       
  1042 // ChspsThemeServer::HandleFileSystemChangeL
       
  1043 // -----------------------------------------------------------------------------
       
  1044 //
       
  1045 void ChspsThemeServer::HandleFileSystemChangeL()
       
  1046     {    
       
  1047     // Install unprocessed plug-in configurations from the import folders,  
       
  1048     // these might be referenced by one or more application configurations - thus,
       
  1049     // this step should be done first!
       
  1050     HandleConfigurationImportsL( KPrefixFilePlugin );
       
  1051     
       
  1052     // Install remaining application configurations from the import folders
       
  1053     HandleConfigurationImportsL( KPrefixFileApp );
       
  1054     
       
  1055     // File system change can also mean that a theme is being updated           
       
  1056     HandleThemeUpgradeL();    
       
  1057     }
       
  1058 
       
  1059 // -----------------------------------------------------------------------------
       
  1060 // ChspsThemeServer::HandleFileSystemChange
       
  1061 // -----------------------------------------------------------------------------
       
  1062 //
       
  1063 void ChspsThemeServer::HandleFileSystemChange()
       
  1064     {    
       
  1065     // Fancy way of checking whether SISX installations are enabled :)    
       
  1066 #ifndef __DISABLE_SISX_INSTALLATION_
       
  1067     
       
  1068     // Register again ...
       
  1069     iFileChangeListener->Setup();
       
  1070     
       
  1071     // ... and then handle the change
       
  1072     TRAP_IGNORE( HandleFileSystemChangeL() );
       
  1073     
       
  1074 #endif // __DISABLE_SISX_INSTALLATION_    
       
  1075      }
       
  1076 
       
  1077 // -----------------------------------------------------------------------------
       
  1078 // ChspsThemeServer::CheckActiveTheme
       
  1079 // CheckActiveTheme the specified theme.
       
  1080 // (other items were commented in a header).
       
  1081 // -----------------------------------------------------------------------------
       
  1082 void ChspsThemeServer::CheckActiveThemesL()    
       
  1083    {        
       
  1084 #ifndef __DISABLE_SISX_INSTALLATION_        
       
  1085     TInt themeUid;
       
  1086     RArray<TUint32> res;
       
  1087     CleanupClosePushL( res );
       
  1088     TInt errorCode = KErrNone;
       
  1089     iCentralRepository->FindL( 0x00000000,0xFFFFFFFF, res );
       
  1090     const TInt keyCount = res.Count();
       
  1091     for (TInt i = 0; i < keyCount; i++ )   	    
       
  1092    	    {   	    
       
  1093    	    errorCode = iCentralRepository->Get( res[i], themeUid );
       
  1094    	    if ( !errorCode )
       
  1095    	        {   	        
       
  1096    	        HBufC* matchthemeUid = HBufC::NewLC( KMaxFileName );
       
  1097    	        TPtr matchthemeUidPtr = matchthemeUid->Des();         
       
  1098             matchthemeUidPtr.Append( KPrefixFileApp );     
       
  1099             matchthemeUidPtr.Append( _L("*_") );
       
  1100             matchthemeUidPtr.AppendNumUC( themeUid, EHex );
       
  1101             matchthemeUidPtr.Append( _L( "_*" ) );                
       
  1102             
       
  1103             HBufC* pathFileName = HBufC::NewL ( KMaxFileName );
       
  1104             TPtr pathFileNamePtr = pathFileName->Des();    
       
  1105             CDir* fileList;  
       
  1106             TFindFile fileFinder( iFsSession );                    
       
  1107             // Find path info file for matching unique themeuid
       
  1108             fileFinder.FindWildByDir( *matchthemeUid, KImportDirectoryC, fileList );       
       
  1109             if( fileList )
       
  1110                 {
       
  1111                 TBool isItOpen = EFalse;                            
       
  1112                 pathFileNamePtr.Copy( KImportDirectoryC );    
       
  1113                 pathFileNamePtr.Append( (*fileList)[0].iName );                
       
  1114                 if ( res[i] == KAppId_hspsAS )
       
  1115                     {
       
  1116                     errorCode = iFsSession.IsFileOpen( pathFileNamePtr, isItOpen );    
       
  1117                     if ( !isItOpen )
       
  1118                         {
       
  1119                         errorCode = iFileAS.Open(iFsSession, pathFileNamePtr, EFileRead );
       
  1120                         }           
       
  1121                     }
       
  1122                 else if ( res[i] == KAppId_hspsAI )
       
  1123                     {
       
  1124                     errorCode = iFsSession.IsFileOpen( pathFileNamePtr, isItOpen );
       
  1125                     if ( !isItOpen )
       
  1126                         {
       
  1127                         errorCode = iFileAI.Open(iFsSession, pathFileNamePtr, EFileRead );
       
  1128                         }           
       
  1129                     }
       
  1130                 else if ( res[i] == KAppId_LE )
       
  1131                     {
       
  1132                     errorCode = iFsSession.IsFileOpen( pathFileNamePtr, isItOpen );
       
  1133                     if ( !isItOpen )
       
  1134                         {
       
  1135                         errorCode = iFileLT.Open(iFsSession, pathFileNamePtr, EFileRead );
       
  1136                         }           
       
  1137                     }                                                
       
  1138                 }
       
  1139             delete fileList;
       
  1140             delete pathFileName;
       
  1141             CleanupStack::PopAndDestroy( matchthemeUid ); 
       
  1142    	        }
       
  1143    	    }
       
  1144    	res.Close();    
       
  1145    	CleanupStack::PopAndDestroy(1, &res);
       
  1146 #endif //__DISABLE_SISX_INSTALLATION_   	      
       
  1147     }
       
  1148     
       
  1149 // -----------------------------------------------------------------------------
       
  1150 // ChspsThemeServer::UnlockSisxImportL()
       
  1151 // -----------------------------------------------------------------------------
       
  1152 //
       
  1153 void ChspsThemeServer::UnlockSisxImportL( 
       
  1154         const TUint32 aAppUid )
       
  1155     {            
       
  1156 __UHEAP_MARK;    
       
  1157     // Setup a filter for finding an SISX import with the provided application uid
       
  1158     HBufC* filterBuf = HBufC::NewLC( KMaxFileName );
       
  1159     TPtr filterPtr = filterBuf->Des();        
       
  1160     filterPtr.Copy( KPrefixFileApp );        
       
  1161     filterPtr.AppendNumUC( aAppUid, EHex );        
       
  1162     _LIT(KFilter, "_*_*_1.0.dat");
       
  1163     filterPtr.Append( KFilter );    
       
  1164          
       
  1165     // Find imports with the generated filter (ROM based imports should not be found)
       
  1166     CDir* importsDir( NULL );  
       
  1167     TFindFile fileFinder( iFsSession );                    
       
  1168     fileFinder.FindWildByDir( filterPtr, KImportDirectoryC, importsDir );   
       
  1169     if( importsDir )
       
  1170         {        
       
  1171         HBufC* importBuf = HBufC::NewLC ( KMaxFileName );
       
  1172         TPtr importPtr = importBuf->Des();        
       
  1173                             
       
  1174         // CleanupStack is not required since there are no leaving function calls below
       
  1175         
       
  1176         // Loop application specific imports
       
  1177         TInt errorCode = KErrNone;
       
  1178         TBool isOpen = EFalse;
       
  1179         for ( TInt i = 0; i< importsDir->Count(); i++ )
       
  1180             {
       
  1181             importPtr.Copy( KImportDirectoryC );                       
       
  1182             importPtr.Append( (*importsDir)[i].iName );
       
  1183             errorCode = iFsSession.IsFileOpen( importPtr, isOpen );
       
  1184             if ( errorCode == KErrNone )
       
  1185                 {
       
  1186                 // Only a single file should be open
       
  1187                 if( isOpen ) 
       
  1188                     {               
       
  1189                     // Close application specific file handles
       
  1190                     if ( aAppUid == KAppId_hspsAS )
       
  1191                         {                                         
       
  1192                         iFileAS.Close();
       
  1193                         }
       
  1194                     else if ( aAppUid == KAppId_hspsAI )
       
  1195                         {                                                        
       
  1196                         iFileAI.Close(); 
       
  1197                         }
       
  1198                     else if ( aAppUid == KAppId_LE )
       
  1199                         {                            
       
  1200                         iFileLT.Close();                                           
       
  1201                         }                           
       
  1202                     }
       
  1203                 }
       
  1204             }
       
  1205         
       
  1206         CleanupStack::PopAndDestroy( importBuf );
       
  1207         delete importsDir;
       
  1208         }        
       
  1209             
       
  1210     CleanupStack::PopAndDestroy( filterBuf );
       
  1211 __UHEAP_MARKEND;    
       
  1212     }
       
  1213 
       
  1214 // -----------------------------------------------------------------------------
       
  1215 // ChspsThemeServer::LockSisxImportL()
       
  1216 // -----------------------------------------------------------------------------
       
  1217 //
       
  1218 void ChspsThemeServer::LockSisxImportL( 
       
  1219         const TUint32 aAppUid,
       
  1220         const TInt aConfUid )
       
  1221     {
       
  1222 __UHEAP_MARK;                
       
  1223     // Setup a filter for finding specific application configuration
       
  1224     HBufC* filterBuf = HBufC::NewLC( KMaxFileName );
       
  1225     TPtr filterPtr = filterBuf->Des();                         
       
  1226     filterPtr.Copy( KPrefixFileApp );
       
  1227     filterPtr.Append( _L("*_*_") );
       
  1228     filterPtr.AppendNumUC( aConfUid, EHex );
       
  1229     filterPtr.Append( _L( "_1.0.dat" ) );
       
  1230     
       
  1231     // Find imports with the generated filter (ROM based imports should not be found)
       
  1232     CDir* importsDir( NULL );  
       
  1233     TFindFile fileFinder( iFsSession );
       
  1234     fileFinder.FindWildByDir( filterPtr, KImportDirectoryC, importsDir );           
       
  1235     if ( importsDir )        
       
  1236        {
       
  1237        CleanupStack::PushL( importsDir );
       
  1238        
       
  1239        // themeuid is unique so count should be 1           
       
  1240        if ( importsDir->Count() == 1 )
       
  1241            {
       
  1242            HBufC* importBuf = HBufC::NewLC ( KMaxFileName );
       
  1243            TPtr importPtr = importBuf->Des();               
       
  1244            importPtr.Copy( KImportDirectoryC );
       
  1245            importPtr.Append( (*importsDir)[0].iName );           
       
  1246        
       
  1247            TBool isOpen = EFalse;
       
  1248            TInt errorCode = iFsSession.IsFileOpen( importPtr, isOpen );
       
  1249            if ( errorCode == KErrNone && !isOpen )
       
  1250                {
       
  1251                // Lock the application specific SISX imports
       
  1252                if ( aAppUid == KAppId_hspsAS )
       
  1253                    {
       
  1254                    errorCode = iFileAS.Open( iFsSession, importPtr, EFileRead );                   
       
  1255                    }
       
  1256                else if ( aAppUid == KAppId_hspsAI )
       
  1257                    {
       
  1258                    errorCode = iFileAI.Open( iFsSession, importPtr, EFileRead );                    
       
  1259                    }
       
  1260                else if ( aAppUid == KAppId_LE )
       
  1261                    {
       
  1262                    errorCode = iFileLT.Open( iFsSession, importPtr, EFileRead );                    
       
  1263                    }   
       
  1264                }
       
  1265            CleanupStack::PopAndDestroy( importBuf );
       
  1266            }
       
  1267        
       
  1268        CleanupStack::PopAndDestroy( importsDir );           
       
  1269        }
       
  1270     
       
  1271     CleanupStack::PopAndDestroy( filterBuf );                
       
  1272 __UHEAP_MARKEND;    
       
  1273     }
       
  1274 
       
  1275 
       
  1276 // -----------------------------------------------------------------------------
       
  1277 // ChspsThemeServer::HandleCenRepChangeL()
       
  1278 // Handles events coming from CentralRepository.
       
  1279 // -----------------------------------------------------------------------------
       
  1280 //
       
  1281 void ChspsThemeServer::HandleCenRepChangeL( const TUint32 aKey )
       
  1282     {    
       
  1283     switch ( aKey )
       
  1284         {        
       
  1285         case KCenrepKeyLang:
       
  1286         case KCenrepKeyPostRFS:
       
  1287         case KCenrepKeyCheckConf:
       
  1288             {
       
  1289             // No processing required
       
  1290             }
       
  1291             break;
       
  1292         default:
       
  1293             {
       
  1294                         
       
  1295             // If SISX installations were enabled
       
  1296 #ifndef __DISABLE_SISX_INSTALLATION_                            
       
  1297             // Get active application configuration for the provided application uid        
       
  1298             TInt confUid(0);
       
  1299             TInt errorCode = iCentralRepository->Get( aKey, confUid );    
       
  1300             if ( !errorCode && confUid > 0 )
       
  1301                 {
       
  1302                 // Remove file lock from active SISX import
       
  1303                 UnlockSisxImportL( aKey );    
       
  1304                 // Lock the new SISX import (prevents uninstallation of the SISX 
       
  1305                 // while it's being used as an active application configuration)
       
  1306                 LockSisxImportL( aKey, confUid );
       
  1307                 }
       
  1308 #endif //__DISABLE_SISX_INSTALLATION_
       
  1309             
       
  1310             }
       
  1311         }
       
  1312     
       
  1313     // Set up the listener to listen to changes in Central Repository for all settings 
       
  1314     iCenRepListener->SetupAll();
       
  1315     }
       
  1316     
       
  1317 // -----------------------------------------------------------------------------
       
  1318 // ChspsThemeServer::HandleAutoInstallerEvent
       
  1319 // (other items were commented in a header).
       
  1320 // -----------------------------------------------------------------------------
       
  1321 //
       
  1322 void ChspsThemeServer::HandleAutoInstallerEvent( ThspsServiceCompletedMessage aMsg )
       
  1323     {    
       
  1324 #ifndef __DISABLE_SISX_INSTALLATION_    
       
  1325     switch ( aMsg )
       
  1326         {
       
  1327         case EhspsInstallPhaseSuccess:
       
  1328             // Phase was success, installation will continue
       
  1329             break;        
       
  1330         case EhspsInstallThemeSuccess:            
       
  1331             {
       
  1332             // Mark current theme as installed
       
  1333         	TBool isInstalled = EFalse;
       
  1334         	
       
  1335         	//check if theme is already listed as installed
       
  1336         	//there is a possibility that the install is an upgrade and the theme would be alredy listed 
       
  1337         	//in the iInstalledSisxThemes
       
  1338         	if( iInstalledSisxThemes.Count() > iCurrentManifestFile )
       
  1339         	    {
       
  1340         	    for (TInt i = 0; i < iInstalledSisxThemes.Count(); i++)
       
  1341         	        {
       
  1342         	        if ( *iInstalledSisxThemes[i] == *iInstallableSisxThemes[ iCurrentManifestFile ] )
       
  1343         	            {
       
  1344         	            isInstalled = ETrue;
       
  1345         	            break;
       
  1346         	            }
       
  1347         	        }
       
  1348         		}
       
  1349         	
       
  1350         	//if theme is not installed yet than add it to iInstalledSisxThemes
       
  1351         	if ( !isInstalled ) 
       
  1352         		{
       
  1353         		iInstalledSisxThemes.Append( iInstallableSisxThemes[ iCurrentManifestFile ] ); 
       
  1354         		}              
       
  1355         	
       
  1356         	iCurrentManifestFile++;
       
  1357             
       
  1358             // Check if more installable files available
       
  1359             delete iAutoInstaller;
       
  1360             iAutoInstaller = NULL;
       
  1361                                                                                                             
       
  1362             RunAutoInstaller( EAutoInstallerStateInstall );                       
       
  1363             }
       
  1364             break;             
       
  1365         case EhspsRemoveThemeSuccess:            
       
  1366             {
       
  1367             delete iInstalledSisxThemes[ iThemeIndexToRemove ];
       
  1368             iInstalledSisxThemes[ iThemeIndexToRemove ] = NULL;
       
  1369             iInstalledSisxThemes.Remove( iThemeIndexToRemove );
       
  1370             }
       
  1371             /* flow through */
       
  1372         case EhspsRemoveThemeFailed:               
       
  1373             /* flow through */            
       
  1374         default:                        
       
  1375             StopAutoInstaller();            
       
  1376             break;
       
  1377         }        
       
  1378 #endif //__DISABLE_SISX_INSTALLATION_
       
  1379     }
       
  1380 
       
  1381 // -----------------------------------------------------------------------------
       
  1382 // ChspsThemeServer::RunAutoInstaller
       
  1383 // (other items were commented in a header).
       
  1384 // -----------------------------------------------------------------------------
       
  1385 //
       
  1386 TBool ChspsThemeServer::RunAutoInstaller( TAutoInstallerStates aState )
       
  1387     {
       
  1388 #ifndef __DISABLE_SISX_INSTALLATION_                       
       
  1389     TBool started( EFalse );
       
  1390     
       
  1391     if( !iAutoInstaller )
       
  1392         {
       
  1393         TRAP_IGNORE( iAutoInstaller = ChspsAutoInstaller::NewL( *this ) );
       
  1394 #ifdef HSPS_LOG_ACTIVE        
       
  1395         if ( iAutoInstaller )
       
  1396             {           
       
  1397             iAutoInstaller->SetLogBus( iLogBus );
       
  1398             }
       
  1399 #endif
       
  1400         }
       
  1401     
       
  1402     iAutoInstallerState = aState;
       
  1403     
       
  1404     switch( aState )
       
  1405         {
       
  1406         case EAutoInstallerStateInstall:       
       
  1407             {
       
  1408             if( iCurrentManifestFile < iManifestFiles.Count() )
       
  1409                 {
       
  1410                 TInt ret = KErrNone;
       
  1411 
       
  1412                 TRAPD( err, ret = iAutoInstaller->InstallThemeL( *iManifestFiles[ iCurrentManifestFile ] ) ); 
       
  1413                 if( err == KErrNone && ret == KErrNone )
       
  1414                     {                                       
       
  1415                     started = ETrue;
       
  1416                     }    
       
  1417                 }
       
  1418             }
       
  1419             break;
       
  1420         case EAutoInstallerUninstall:
       
  1421             {
       
  1422             if( iThemeIndexToRemove != KErrNotFound && iThemeIndexToRemove < iInstalledSisxThemes.Count() )
       
  1423                 {                                   
       
  1424                 TRAPD( err, iAutoInstaller->UnInstallThemeL( *iInstalledSisxThemes[ iThemeIndexToRemove ] ) )
       
  1425 
       
  1426                 if( !err )                    
       
  1427                     {
       
  1428                     started = ETrue;
       
  1429                     }
       
  1430                 }
       
  1431             }
       
  1432             break;
       
  1433         default:    
       
  1434             {
       
  1435             iAutoInstallerState = EAutoInstallerStateIdle;
       
  1436             }
       
  1437             break;                        
       
  1438         }
       
  1439         
       
  1440     if( !started )
       
  1441         {
       
  1442         StopAutoInstaller();
       
  1443         }
       
  1444     
       
  1445     return started;
       
  1446 #else
       
  1447     return EFalse;    
       
  1448 #endif // __DISABLE_SISX_INSTALLATION_    
       
  1449     }
       
  1450 
       
  1451 // -----------------------------------------------------------------------------
       
  1452 // ChspsThemeServer::StopAutoInstaller
       
  1453 // (other items were commented in a header).
       
  1454 // -----------------------------------------------------------------------------
       
  1455 //
       
  1456 void ChspsThemeServer::StopAutoInstaller()
       
  1457     {        
       
  1458 #ifndef __DISABLE_SISX_INSTALLATION_    
       
  1459     switch( iAutoInstallerState )
       
  1460         {
       
  1461         case EAutoInstallerStateInstall:
       
  1462             {
       
  1463             iCurrentManifestFile = 0;
       
  1464             iInstallableSisxThemes.Reset();
       
  1465             iManifestFiles.ResetAndDestroy();
       
  1466             }
       
  1467             break;
       
  1468         case EAutoInstallerUninstall:
       
  1469             {
       
  1470             iThemeIndexToRemove = KErrNotFound;            
       
  1471             }
       
  1472             break;
       
  1473         default:
       
  1474             break;            
       
  1475         }
       
  1476         
       
  1477     iAutoInstallerState = EAutoInstallerStateIdle;          
       
  1478     
       
  1479     delete iAutoInstaller;
       
  1480     iAutoInstaller = NULL;
       
  1481 #endif // __DISABLE_SISX_INSTALLATION_
       
  1482     }
       
  1483 
       
  1484 // -----------------------------------------------------------------------------
       
  1485 // ChspsThemeServer::CheckInstalledPluginUsageL
       
  1486 // -----------------------------------------------------------------------------
       
  1487 //
       
  1488 TInt ChspsThemeServer::CheckInstalledPluginUsageL( const TInt aPluginUid )        
       
  1489     { 
       
  1490     TInt sum = 0;
       
  1491     if (  iHeaderListCache )
       
  1492         {
       
  1493         TInt count = iHeaderListCache->Count();
       
  1494         if ( iDefinitionRepository->Locked() )
       
  1495             {
       
  1496             User::Leave( KErrInUse );
       
  1497             }
       
  1498         iDefinitionRepository->Lock();
       
  1499         for ( TInt i = 0; i < count; i++ )
       
  1500             {
       
  1501             ChspsODT* header = ( *iHeaderListCache )[ i ];
       
  1502             if ( header->ConfigurationType() == EhspsAppConfiguration )
       
  1503                 {
       
  1504                 
       
  1505                 ChspsODT* odt = ChspsODT::NewL();
       
  1506                 CleanupStack::PushL( odt );
       
  1507                 odt->SetRootUid( header->RootUid() );
       
  1508                 odt->SetThemeUid( header->ThemeUid() );        
       
  1509                 odt->SetConfigurationType( header->ConfigurationType() );
       
  1510                 odt->SetRootUid( header->RootUid() );
       
  1511                 odt->SetProviderUid( header->ProviderUid() );
       
  1512                 odt->SetThemeUid( header->ThemeUid() );
       
  1513                 odt->SetProviderNameL( header->ProviderName() );
       
  1514                 odt->SetThemeFullNameL( header->ThemeFullName() );
       
  1515                 odt->SetThemeShortNameL( header->ThemeShortName() );
       
  1516                 odt->SetThemeVersionL( header->ThemeVersion() );            
       
  1517                 odt->SetFlags( header->Flags() );                                                                       
       
  1518                 odt->SetMultiInstance( header->MultiInstance() );
       
  1519                 User::LeaveIfError( iDefinitionRepository->GetOdtL( *odt ) );
       
  1520                 TInt count = 0;
       
  1521                 hspsServerUtil::PluginInstanceCountL( *odt, aPluginUid, count );
       
  1522                 sum = sum + count;
       
  1523                 CleanupStack::PopAndDestroy( odt );
       
  1524                 }
       
  1525             }
       
  1526         iDefinitionRepository->Unlock();
       
  1527         
       
  1528         }
       
  1529     return sum;
       
  1530     }
       
  1531 
       
  1532 // -----------------------------------------------------------------------------
       
  1533 // ChspsThemeServer::PluginInHeaderCache
       
  1534 // -----------------------------------------------------------------------------
       
  1535 //
       
  1536 TBool ChspsThemeServer::PluginInHeaderCache( const TUid aPluginUid )        
       
  1537     { 
       
  1538     TBool found = EFalse;
       
  1539     
       
  1540     if ( iHeaderListCache )
       
  1541         {        
       
  1542         const TInt count = iHeaderListCache->Count();
       
  1543         for ( TInt i = 0; i < count; i++ )
       
  1544             {
       
  1545             ChspsODT* header = (*iHeaderListCache)[i];
       
  1546             if( header->ThemeUid() == aPluginUid.iUid )
       
  1547                 {
       
  1548                 found = ETrue;
       
  1549                 break;
       
  1550                 }
       
  1551             }                
       
  1552         }
       
  1553     
       
  1554     return found;
       
  1555     }
       
  1556 
       
  1557 // -----------------------------------------------------------------------------
       
  1558 // ChspsThemeServer::UpdateHeaderListCacheL
       
  1559 // -----------------------------------------------------------------------------
       
  1560 //
       
  1561 void ChspsThemeServer::UpdateHeaderListCacheL()
       
  1562     {
       
  1563     if ( iHeaderListPending )
       
  1564         {
       
  1565 #ifdef HSPS_LOG_ACTIVE        
       
  1566         iLogBus->LogText( _L( "ChspsThemeServer::UpdateHeaderListCacheL(): already updating the cache" ) );
       
  1567 #endif                
       
  1568         }
       
  1569     else
       
  1570         {
       
  1571         iHeaderListPending = ETrue;
       
  1572         
       
  1573 #ifdef _hsps_PERFORMANCE_TEST_
       
  1574         TTime start_time;        
       
  1575         start_time = ChspsTimeMon::StartTiming(_L("timer started")); 
       
  1576 #endif    
       
  1577         
       
  1578         // Get headers from the Plug-in Repository
       
  1579         CArrayPtrSeg<ChspsODT>* headerList =  new (ELeave)CArrayPtrSeg<ChspsODT>(KHeaderListGranularity);
       
  1580         CleanupStack::PushL( headerList );
       
  1581                 
       
  1582         // Reset search mask
       
  1583         iCacheMask->SetRootUid( 0 );
       
  1584         iCacheMask->SetProviderUid( 0 );
       
  1585         iCacheMask->SetThemeUid( 0 );
       
  1586         
       
  1587         TInt err = DoUpdateHeaderListCache( *headerList );
       
  1588         User::LeaveIfError( err );
       
  1589                                 
       
  1590         if ( headerList->Count() > 0 )
       
  1591             {
       
  1592             // Destroys all objects whose pointers form the elements of the array, 
       
  1593             // before resetting the array.
       
  1594             iHeaderListCache->ResetAndDestroy();
       
  1595                         
       
  1596             TInt count = headerList->Count() - 1;
       
  1597             for (TInt i = count; i >= 0; --i)
       
  1598                 {
       
  1599                 // Update the cache array (ownership is transferred)
       
  1600                 TRAP( err, iHeaderListCache->InsertL(0, headerList->At(i)) );
       
  1601                                                    
       
  1602                 // If allocation failed
       
  1603                 if ( err )
       
  1604                     {
       
  1605                     break;
       
  1606                     }
       
  1607                 
       
  1608                 // Complete the ownership transfer
       
  1609                 headerList->Delete(i);                        
       
  1610                 }
       
  1611             }
       
  1612 
       
  1613         // Destroys all objects whose pointers form the elements of the array, 
       
  1614         // before resetting the array.
       
  1615         headerList->ResetAndDestroy();
       
  1616         CleanupStack::PopAndDestroy( headerList );
       
  1617 
       
  1618         // calculating service time
       
  1619 #ifdef _hsps_PERFORMANCE_TEST_        
       
  1620         ChspsTimeMon::StopTiming( start_time, _L( "ChspsThemeServer::UpdateHeaderListCache(): - timer stopped" ) );
       
  1621 #endif       
       
  1622                         
       
  1623         iHeaderListPending = EFalse;
       
  1624         }      
       
  1625     }
       
  1626 
       
  1627 // -----------------------------------------------------------------------------
       
  1628 // ChspsThemeServer::UpdateHeaderListCache
       
  1629 // -----------------------------------------------------------------------------
       
  1630 //
       
  1631 TInt ChspsThemeServer::UpdateHeaderListCache(    
       
  1632         ThspsCacheOperations aOperation,
       
  1633         const TInt aRootUid,
       
  1634         const TInt aProviderUid,
       
  1635         const TInt aConfigurationUid )
       
  1636     {
       
  1637     TInt err = KErrNone;
       
  1638     
       
  1639     // Input validation
       
  1640     if ( aRootUid < 1 || aProviderUid < 1 || aConfigurationUid < 1 )
       
  1641         {
       
  1642         err = KErrArgument;
       
  1643         }    
       
  1644             
       
  1645     if ( !err )
       
  1646         {
       
  1647         if ( aOperation == EhspsCacheAddHeader )
       
  1648             {
       
  1649             // Add or replace a header in the header list cache
       
  1650             TRAP( 
       
  1651                 err, 
       
  1652                 HeaderCacheAddL(
       
  1653                     aRootUid, 
       
  1654                     aProviderUid, 
       
  1655                     aConfigurationUid ) 
       
  1656                 );
       
  1657             }
       
  1658         else
       
  1659             {
       
  1660             // Remove a header from the header list cache
       
  1661             err = HeaderCacheRemove( 
       
  1662                     aRootUid, 
       
  1663                     aProviderUid, 
       
  1664                     aConfigurationUid 
       
  1665                     );            
       
  1666             }   
       
  1667         }
       
  1668     
       
  1669     if ( err )
       
  1670         {
       
  1671 #ifdef HSPS_LOG_ACTIVE                     
       
  1672         iLogBus->LogText( 
       
  1673             _L( "ChspsThemeServer::UpdateHeaderListCache(): - Failed to update %d header in the cache!" ), 
       
  1674             aConfigurationUid 
       
  1675             );
       
  1676 #endif            
       
  1677         }
       
  1678     
       
  1679     return err;
       
  1680     }
       
  1681 
       
  1682 // -----------------------------------------------------------------------------
       
  1683 // ChspsThemeServer::DoUpdateHeaderListCache
       
  1684 // -----------------------------------------------------------------------------
       
  1685 //
       
  1686 TInt ChspsThemeServer::DoUpdateHeaderListCache(
       
  1687         CArrayPtrSeg<ChspsODT>& aHeaderList )       
       
  1688     {              
       
  1689     // Get headers matching the search mask
       
  1690     TRAPD( err, GetHeaderListL( aHeaderList, *iCacheMask ) );
       
  1691     if ( err )
       
  1692     	{
       
  1693 #ifdef HSPS_LOG_ACTIVE                     
       
  1694         iLogBus->LogText( 
       
  1695             _L( "ChspsThemeServer::DoUpdateHeaderListCache(): - failed %d"),
       
  1696             err
       
  1697             );
       
  1698 #endif
       
  1699     	}
       
  1700     
       
  1701     return err;         
       
  1702     }
       
  1703 
       
  1704 // -----------------------------------------------------------------------------
       
  1705 // ChspsThemeServer::HeaderCacheAddL
       
  1706 // -----------------------------------------------------------------------------
       
  1707 //
       
  1708 void ChspsThemeServer::HeaderCacheAddL(
       
  1709         const TInt aRootUid,
       
  1710         const TInt aProviderUid,
       
  1711         const TInt aConfigurationUid )
       
  1712     {        
       
  1713     // Setup a search mask    
       
  1714     iCacheMask->SetRootUid( aRootUid );
       
  1715     iCacheMask->SetProviderUid( aProviderUid );
       
  1716     iCacheMask->SetThemeUid( aConfigurationUid );
       
  1717     
       
  1718     // Setup a list for headers matching the search mask
       
  1719     CArrayPtrSeg<ChspsODT>* newHeadersList =  new (ELeave)CArrayPtrSeg<ChspsODT>(KHeaderListGranularity);
       
  1720     CleanupStack::PushL( newHeadersList );
       
  1721             
       
  1722     // Find the headers
       
  1723     User::LeaveIfError( DoUpdateHeaderListCache( *newHeadersList ) );
       
  1724     
       
  1725     // There should be only one match 
       
  1726     if ( newHeadersList->Count() != 1 )
       
  1727         {
       
  1728 #ifdef HSPS_LOG_ACTIVE        
       
  1729         iLogBus->LogText( 
       
  1730             _L( "ChspsThemeServer::HeaderCacheAddL(): - same %S header was found several times!" ), 
       
  1731             &(newHeadersList->At(0)->ThemeShortName()) 
       
  1732             );        
       
  1733 #endif        
       
  1734         User::Leave( KErrGeneral );
       
  1735         }    
       
  1736             
       
  1737     // Set new default position 
       
  1738     TInt newIndex = 0;
       
  1739     
       
  1740     // Check if old version of the header instance exists in the cache
       
  1741     TBool replaced = EFalse;
       
  1742     ChspsODT* header = NULL;
       
  1743     for( TInt index=0; !replaced && index < iHeaderListCache->Count(); index++ )
       
  1744         {
       
  1745         header = iHeaderListCache->At( index );
       
  1746         if ( header )
       
  1747             {
       
  1748             if ( header->RootUid() == aRootUid 
       
  1749                     && header->ProviderUid() == aProviderUid 
       
  1750                     && header->ThemeUid() == aConfigurationUid )
       
  1751                 {
       
  1752                 // Remove the old header instance
       
  1753                 iHeaderListCache->Delete( index );
       
  1754                 delete header;
       
  1755                 header = NULL;
       
  1756                 replaced = ETrue;
       
  1757                 newIndex = index;
       
  1758                 }
       
  1759             }
       
  1760         }
       
  1761     
       
  1762     // Append the new version into the specified position (ownership is transferred)    
       
  1763     iHeaderListCache->InsertL(newIndex, newHeadersList->At(0) );
       
  1764 
       
  1765 #ifdef HSPS_LOG_ACTIVE        
       
  1766     iLogBus->LogText( _L( "ChspsThemeServer::HeaderCacheAddL(): - header '%S' was added/updated to cache!" ), &(newHeadersList->At(0)->ThemeShortName()) );        
       
  1767 #endif    
       
  1768     
       
  1769     // Complete the ownership transfer 
       
  1770     newHeadersList->Delete(0);      
       
  1771         
       
  1772     // Cleanup
       
  1773     newHeadersList->ResetAndDestroy();
       
  1774     CleanupStack::PopAndDestroy( newHeadersList );        
       
  1775     }
       
  1776 
       
  1777 // -----------------------------------------------------------------------------
       
  1778 // ChspsThemeServer::HeaderCacheRemove
       
  1779 // -----------------------------------------------------------------------------
       
  1780 //
       
  1781 TInt ChspsThemeServer::HeaderCacheRemove(            
       
  1782         const TInt aRootUid,
       
  1783         const TInt aProviderUid,
       
  1784         const TInt aConfigurationUid )
       
  1785     {
       
  1786     TInt err = KErrNone;
       
  1787     
       
  1788     // Find the header to be removed from the cache        
       
  1789     ChspsODT* header = NULL;
       
  1790     for( TInt index=0; index < iHeaderListCache->Count(); index++ )
       
  1791         {
       
  1792         header = iHeaderListCache->At( index );
       
  1793         if ( header )
       
  1794             {
       
  1795             if ( header->RootUid() == aRootUid 
       
  1796                     && header->ProviderUid() == aProviderUid 
       
  1797                     && header->ThemeUid() == aConfigurationUid )
       
  1798                 {
       
  1799 #ifdef HSPS_LOG_ACTIVE        
       
  1800                 iLogBus->LogText( _L( "ChspsThemeServer::HeaderCacheRemove(): - header '%S' was removed from cache!" ), &(header->ThemeShortName()) );        
       
  1801 #endif                               
       
  1802                 iHeaderListCache->Delete( index );
       
  1803                 delete header;
       
  1804                 header = NULL;
       
  1805                 break;
       
  1806                 }
       
  1807             }
       
  1808         }
       
  1809     
       
  1810     return err;   
       
  1811     }
       
  1812 
       
  1813 // -----------------------------------------------------------------------------
       
  1814 // ChspsThemeServer::CompareL
       
  1815 // (other items were commented in a header).
       
  1816 // -----------------------------------------------------------------------------
       
  1817 //
       
  1818 TBool ChspsThemeServer::ComparePaths(const ChspsODT& aOldHeader, const ChspsODT& aNewHeader)
       
  1819   {
       
  1820   TBool ret;
       
  1821   if  (
       
  1822         (
       
  1823         aOldHeader.RootUid() == aNewHeader.RootUid()
       
  1824         )
       
  1825     &&
       
  1826         (
       
  1827         aOldHeader.ProviderUid() == aNewHeader.ProviderUid()
       
  1828         )
       
  1829     &&
       
  1830         (
       
  1831         aOldHeader.ThemeUid() == aNewHeader.ThemeUid()
       
  1832         )       
       
  1833     &&
       
  1834         (
       
  1835         !aOldHeader.ThemeVersion().Compare( aNewHeader.ThemeVersion())
       
  1836         )
       
  1837     )
       
  1838 	  {
       
  1839 	  ret = ETrue;
       
  1840 	  }
       
  1841   else
       
  1842 	  {
       
  1843 	  ret = EFalse;
       
  1844 	  }
       
  1845   return ret; 
       
  1846   }
       
  1847   
       
  1848 // -----------------------------------------------------------------------------
       
  1849 // ChspsThemeServer::GetDeviceLanguage()
       
  1850 // (other items were commented in a header).
       
  1851 // -----------------------------------------------------------------------------
       
  1852 //
       
  1853 TLanguage ChspsThemeServer::GetDeviceLanguage()
       
  1854     {
       
  1855 #ifdef HSPS_LOG_ACTIVE    
       
  1856     iLogBus->LogText( _L( "ChspsThemeServer::GetDeviceLanguage(): %d returned" ), User::Language() );
       
  1857 #endif                    
       
  1858 
       
  1859     return User::Language();
       
  1860     } 
       
  1861 	
       
  1862 // -----------------------------------------------------------------------------
       
  1863 // ChspsThemeServer::GetHeaderListL
       
  1864 // (other items were commented in a header).
       
  1865 // -----------------------------------------------------------------------------
       
  1866 //
       
  1867 void ChspsThemeServer::GetHeaderListL(CArrayPtrSeg<ChspsODT>& aHeaderList, const ChspsODT& aSearchMask )    
       
  1868     {
       
  1869     CArrayPtrSeg<ChspsODT>* licenceeDefaultList = new (ELeave)CArrayPtrSeg<ChspsODT>( KHeaderListGranularity );
       
  1870     CleanupStack::PushL( TCleanupItem( ResetAndDestroyChspsODTArray, licenceeDefaultList ) );
       
  1871     
       
  1872     CArrayPtrSeg<HBufC8>* headerDataList = new (ELeave)CArrayPtrSeg<HBufC8>( KHeaderListGranularity );
       
  1873     CleanupStack::PushL( TCleanupItem( ResetAndDestroyHBufC8Array, headerDataList ) );
       
  1874     iDefinitionRepository->GetThemeListAsStreamL( *headerDataList, aSearchMask );
       
  1875         
       
  1876     // Convert data streams into header instances and populate the lincencee and header lists    
       
  1877     for ( TInt i=0; i < headerDataList->Count(); i++ )
       
  1878         {
       
  1879         HBufC8* headerData = headerDataList->At(i);
       
  1880         if ( headerData )
       
  1881             {
       
  1882             ChspsODT* header = ChspsODT::UnMarshalHeaderLC( *headerData );                        
       
  1883             if ( header->Flags() & EhspsThemeStatusLicenceeDefault )
       
  1884                 {                
       
  1885                 licenceeDefaultList->AppendL( header );
       
  1886                 }
       
  1887             else
       
  1888                 {
       
  1889                 // Get header's file path
       
  1890                 HBufC* fileBuf = HBufC::NewLC ( KMaxFileName );
       
  1891                 TPtr filePtr = fileBuf->Des();    
       
  1892                 iDefinitionRepository->GetResourcePathL( *header, EResourceODT, filePtr );
       
  1893                 aHeaderList.AppendL( header );                 
       
  1894                 CleanupStack::PopAndDestroy( fileBuf ); 
       
  1895                 }        
       
  1896             CleanupStack::Pop( header );  
       
  1897             }
       
  1898         }
       
  1899     
       
  1900     // Cleanup the data streams
       
  1901     CleanupStack::PopAndDestroy( headerDataList );            
       
  1902         
       
  1903 
       
  1904     // remove duplicate headers from the licencee list  
       
  1905     if( aHeaderList.Count() )
       
  1906         {
       
  1907         // go through the theme list again and put back the themes without update
       
  1908         const TInt headersCount = aHeaderList.Count();        
       
  1909         for( TInt i = 0; i < licenceeDefaultList->Count(); i++ )
       
  1910             {
       
  1911             ChspsODT* licenceeHeader = licenceeDefaultList->At( i );
       
  1912             
       
  1913             TBool found( EFalse );            
       
  1914             for( TInt j = 0; !found && j < headersCount; j++ )
       
  1915                 {
       
  1916                 ChspsODT* header = aHeaderList.At( j );
       
  1917                 
       
  1918                 // compare two themes to find out are they they the same
       
  1919                 if( ComparePaths( *header, *licenceeHeader ) )
       
  1920                     {
       
  1921                     // there was an update, this theme does not need licencee default
       
  1922                     licenceeDefaultList->Delete( i );
       
  1923                     delete licenceeHeader;
       
  1924                     // next i
       
  1925                     i--;
       
  1926                     found = ETrue;
       
  1927                     }
       
  1928                 }
       
  1929             }       
       
  1930         }
       
  1931 
       
  1932     // append headers from the licencee list into the headers list
       
  1933     for ( TInt i = 0; i < licenceeDefaultList->Count(); i++)
       
  1934         {
       
  1935         ChspsODT* licenceeHeader = licenceeDefaultList->At(i);
       
  1936         
       
  1937         HBufC8* data = licenceeHeader->MarshalHeaderL();
       
  1938         CleanupStack::PushL( data );
       
  1939         
       
  1940         ChspsODT* odt = ChspsODT::UnMarshalHeaderLC( data->Des() );
       
  1941         aHeaderList.AppendL( odt );
       
  1942         CleanupStack::Pop( odt );
       
  1943         
       
  1944         CleanupStack::PopAndDestroy( data );
       
  1945         }
       
  1946         
       
  1947     CleanupStack::PopAndDestroy( licenceeDefaultList );
       
  1948             
       
  1949     // Modify headers flags     
       
  1950     for ( TInt i = 0; i < aHeaderList.Count(); i++ )
       
  1951         {        
       
  1952         ChspsODT* header = aHeaderList.At(i);
       
  1953         TInt appUid = header->RootUid();
       
  1954         if ( header->ConfigurationType() == EhspsAppConfiguration )
       
  1955             {        
       
  1956             // Check whether the application has an active configuration
       
  1957             TInt confUid = 0;            
       
  1958             if ( iCentralRepository->Get( appUid, confUid ) == KErrNone && confUid > 0 )
       
  1959                 {
       
  1960                 // If active application configuration is the same as the header in question 
       
  1961                 if ( header->ThemeUid() == confUid )
       
  1962                     {
       
  1963                     // Update header's flags
       
  1964                     TUint flags = header->Flags();
       
  1965                     header->SetFlags( flags | EhspsThemeStatusActive );
       
  1966                     }
       
  1967                 }
       
  1968             }
       
  1969         }
       
  1970  
       
  1971     }
       
  1972     	
       
  1973 	
       
  1974 // -----------------------------------------------------------------------------
       
  1975 // ChspsThemeServer::CustomSecurityCheckL
       
  1976 // (other items were commented in a header).
       
  1977 // -----------------------------------------------------------------------------
       
  1978 //
       
  1979 CPolicyServer::TCustomResult ChspsThemeServer::CustomSecurityCheckL(const RMessage2& aMsg, 
       
  1980     TInt& /*aAction*/, TSecurityInfo& /*aMissing*/ )
       
  1981     {
       
  1982     CPolicyServer::TCustomResult ret;
       
  1983     if ( iSecurityEnforcer->CheckAccessRightsL( aMsg ) )
       
  1984         {
       
  1985         ret = EPass;
       
  1986         }
       
  1987     else
       
  1988         {
       
  1989         ret = EFail;
       
  1990         }
       
  1991     return ret;
       
  1992     }
       
  1993 
       
  1994 // -----------------------------------------------------------------------------
       
  1995 // ChspsThemeServerSession::HandleThemeUpgradeL
       
  1996 // (other items were commented in a header).
       
  1997 // -----------------------------------------------------------------------------
       
  1998 //
       
  1999 void ChspsThemeServer::HandleThemeUpgradeL()
       
  2000 	{
       
  2001 	CDir* upgradePathFileList( NULL );
       
  2002     TFindFile upgradeFileFinder( iFsSession );
       
  2003     
       
  2004     // If a upgrade_path_*.dat file appears in the import directory this means that a theme is being upgraded
       
  2005     upgradeFileFinder.FindWildByDir( KUpgradePluginWild , 
       
  2006     		KImportDirectoryC, upgradePathFileList );  
       
  2007     if( !upgradePathFileList )
       
  2008     	{
       
  2009     	return;
       
  2010     	}
       
  2011 	// only one upgrade_path file can be present because it is deleted after upgrade
       
  2012 	if ( upgradePathFileList->Count() == 1 )
       
  2013 		{
       
  2014 		CleanupStack::PushL( upgradePathFileList );
       
  2015 		TPtrC upgradePathFile( ( *upgradePathFileList )[0].iName ); 
       
  2016 
       
  2017 		//path_*.dat theme file
       
  2018 		HBufC* fileNamePathOnly = HBufC::NewLC( KMaxFileName );
       
  2019 		TPtr fileNamePathOnlyPtr( fileNamePathOnly->Des() );
       
  2020 		TParsePtrC parsePtrTemp( upgradePathFile.Right( 
       
  2021 			upgradePathFile.Length() - KUpgrade().Length() - 1 ) );  
       
  2022 		fileNamePathOnlyPtr.Append( parsePtrTemp.FullName() ); 
       
  2023 		
       
  2024 		TBool isInstalled = EFalse;
       
  2025     	//check if theme is installed
       
  2026     	for (TInt i = 0; i < iInstalledSisxThemes.Count(); i++)
       
  2027     		{
       
  2028     		if ( *iInstalledSisxThemes[i] == *fileNamePathOnly )
       
  2029     			{
       
  2030     			isInstalled = ETrue;
       
  2031     			break;
       
  2032     			}
       
  2033     		}
       
  2034     	
       
  2035     	if ( !isInstalled )
       
  2036 			{
       
  2037 			// base theme package is not an installed theme
       
  2038 #ifdef HSPS_LOG_ACTIVE			
       
  2039 	        iLogBus->LogText( _L( "hspsThemeServer: ChspsThemeServer::HandleThemeUpgradeL(): Base theme package not installed" ) );
       
  2040 #endif			        
       
  2041 			}
       
  2042     	else
       
  2043 			{
       
  2044 			//upgrade_path_*.dat file path portion
       
  2045     		TParsePtrC parsePtr( upgradePathFile.Right( 
       
  2046 				upgradePathFile.Length() - KUpgradePlugin().Length() - 1 ) );
       
  2047     		TPtrC fileNameUpgradePath( parsePtr.Name() );
       
  2048     		
       
  2049             //path to manifest
       
  2050     		HBufC* manifestFile = HBufC::NewLC( KMaxFileName );
       
  2051             TPtr manifestFilePtr( manifestFile->Des() );
       
  2052             manifestFilePtr.Append( KImportDirectoryC );
       
  2053             manifestFilePtr.Append( fileNameUpgradePath );
       
  2054                                                                                                                                
       
  2055             TInt pos( manifestFilePtr.Locate( TChar( '_' ) ) );
       
  2056             while( pos != KErrNotFound )
       
  2057                 {
       
  2058                 manifestFilePtr.Replace( pos, 1, KDoubleBackSlash );
       
  2059                 pos = manifestFilePtr.Locate( TChar( '_' ) );                
       
  2060                 }
       
  2061                    
       
  2062             manifestFilePtr.Append( KDoubleBackSlash );            
       
  2063             manifestFilePtr.Append( KManifestFile );           
       
  2064             
       
  2065             //store manifest file for autoinstaller
       
  2066             iManifestFiles.AppendL( manifestFile );
       
  2067 
       
  2068             //Store current path_*.dat file
       
  2069             iInstallableSisxThemes.AppendL( ( *fileNamePathOnly ).AllocL() );
       
  2070             iCurrentManifestFile = 0;
       
  2071     		
       
  2072     		if( !RunAutoInstaller( EAutoInstallerStateInstall ) )
       
  2073 	            {
       
  2074 	            // All installation files are not present
       
  2075 	            // here should be some kind of error handling
       
  2076 	            }  	
       
  2077 
       
  2078     		CleanupStack::Pop( manifestFile ); //manifestFile
       
  2079 			}
       
  2080 		
       
  2081 		HBufC * upgradePathFileToDelete = HBufC::NewLC( KMaxFileName );
       
  2082 		TPtr upgradePathFileToDeletePtr( upgradePathFileToDelete->Des() );
       
  2083 		upgradePathFileToDeletePtr.Append( KImportDirectoryC );
       
  2084 		upgradePathFileToDeletePtr.Append( upgradePathFile );
       
  2085 		
       
  2086 		//delete the upgrade_path_*.dat auxiliary file
       
  2087 		TInt err = iFsSession.Delete( *upgradePathFileToDelete );
       
  2088 		if ( err != KErrNone )
       
  2089 			{
       
  2090 #ifdef HSPS_LOG_ACTIVE
       
  2091 			//file not deleted error handling			
       
  2092 			iLogBus->LogText( _L( "hspsThemeServer: ChspsThemeServer::HandleThemeUpgradeL(): %d ,upgrade_path_*.dat file not deleted" ), err );
       
  2093 #endif			                
       
  2094 			}
       
  2095 		
       
  2096 		CleanupStack::PopAndDestroy( upgradePathFileToDelete );
       
  2097 		CleanupStack::PopAndDestroy( fileNamePathOnly );
       
  2098 		CleanupStack::PopAndDestroy( upgradePathFileList );	
       
  2099 		}
       
  2100 	else 
       
  2101 		{
       
  2102 #ifdef HSPS_LOG_ACTIVE		
       
  2103 		//more than one upgrade_path is present. in import directory error handling
       
  2104         iLogBus->LogText( _L( "hspsThemeServer: ChspsThemeServer::HandleThemeUpgradeL(): more than one upgrade_path_*.dat file" ) );
       
  2105 #endif		        
       
  2106 		}
       
  2107 	}
       
  2108 
       
  2109 // -----------------------------------------------------------------------------
       
  2110 // Checks whether the device language has been changed and updates 
       
  2111 // configurations accordingly
       
  2112 // -----------------------------------------------------------------------------
       
  2113 //
       
  2114 void ChspsThemeServer::HandleLanguageChangeL()
       
  2115 	{
       
  2116 #ifdef HSPS_LOG_ACTIVE          
       
  2117         iLogBus->LogText( _L( "ChspsThemeServer::HandleLanguageChangeL()") );
       
  2118 #endif
       
  2119         
       
  2120 	// Set key for fetching previously used language from the cenrep
       
  2121 	const TInt KCenrepLangKey = 100000001;
       
  2122 	
       
  2123 	// The key should already exist
       
  2124 	const TUint32 fullMask = 0xFFFFFFFF;
       
  2125 	RArray<TUint32> res;	
       
  2126 	CleanupClosePushL( res );
       
  2127 	
       
  2128 	iCentralRepository->FindL( KCenrepLangKey, fullMask, res );
       
  2129    	if ( res.Count() == 0 )
       
  2130    		{
       
  2131 #ifdef HSPS_LOG_ACTIVE   		
       
  2132    		iLogBus->LogText( _L( "ChspsThemeServer::HandleLanguageChangeL(): - Couldn't find %d key from the HSPS cenrep!" ), KCenrepLangKey );
       
  2133 #endif   		             
       
  2134 
       
  2135    		User::Leave( KErrNotFound );
       
  2136    		}	
       
  2137 	
       
  2138    	// Check the cenrep key's value
       
  2139 	TInt prevLanguage = ELangNone;
       
  2140     iCentralRepository->Get( KCenrepLangKey, prevLanguage );
       
  2141     
       
  2142     // If the language has been changed
       
  2143     if( iDeviceLanguage != prevLanguage )
       
  2144     	{
       
  2145     	// Update all the ODTs so that the localized configurations can be fetched    	
       
  2146     	LocalizeConfigurationsL();
       
  2147     		
       
  2148     	// Finally, update the cenrep with the new language
       
  2149     	TInt errorCode = iCentralRepository->Set( KCenrepLangKey, iDeviceLanguage );
       
  2150     	if ( errorCode  )
       
  2151     		{
       
  2152 #ifdef HSPS_LOG_ACTIVE    		
       
  2153 	        iLogBus->LogText( _L( "ChspsThemeServer::HandleLanguageChangeL(): - Failed to update the cenrep!" ) );
       
  2154 #endif    		        
       
  2155 
       
  2156     		User::Leave( KErrWrite );
       
  2157     		}
       
  2158     	else
       
  2159     		{
       
  2160 #ifdef HSPS_LOG_ACTIVE    		
       
  2161 	        iLogBus->LogText( _L( "ChspsThemeServer::HandleLanguageChangeL(): - themes were localized successfully" ) );
       
  2162 #endif    		        
       
  2163     		}
       
  2164     	}
       
  2165     
       
  2166     res.Close();  
       
  2167     CleanupStack::PopAndDestroy(1, &res);    
       
  2168 	}
       
  2169 
       
  2170 
       
  2171 // -----------------------------------------------------------------------------
       
  2172 // ChspsThemeServer::SetHeadersLanguages()
       
  2173 // -----------------------------------------------------------------------------
       
  2174 //
       
  2175 void ChspsThemeServer::LocalizeConfigurationsL()
       
  2176 	{
       
  2177 #ifdef HSPS_LOG_ACTIVE              
       
  2178     iLogBus->LogText( _L( "ChspsThemeServer::LocalizeConfigurationsL()" ) );
       
  2179 #endif	
       
  2180 	
       
  2181 	// Check header count 	
       
  2182 	TInt headerCount = HeaderListCache().Count();	
       
  2183 	if( headerCount )
       
  2184 		{				
       
  2185 		_LIT8(KhspsDefinitionEngine, "hspsdefinitionengine");		
       
  2186 		ChspsDefinitionEngineInterface* defEngine = ChspsDefinitionEngineInterface::NewL(KhspsDefinitionEngine);
       
  2187 		CleanupStack::PushL( defEngine );
       
  2188 			
       
  2189 		// Loop found headers
       
  2190 		ChspsODT* header = NULL;
       
  2191 		const TInt KInterfaceUid = 0; 
       
  2192 		for( TInt headerIndex=0; headerIndex < headerCount; headerIndex++ )
       
  2193 			{			
       
  2194 			// Get pointer to the header (contains minimal information from an ODT)
       
  2195 			header = iHeaderListCache->At( headerIndex );
       
  2196 									
       
  2197 			// Get the ODT based on header UIDs
       
  2198 			ChspsODT* odt = ChspsODT::NewL();
       
  2199 			CleanupStack::PushL( odt );		
       
  2200 			GetConfigurationL(
       
  2201 				KInterfaceUid,
       
  2202 				header->ThemeUid(),
       
  2203 				*odt );		
       
  2204 			if ( !odt || !odt->ThemeUid() )
       
  2205 	        	{
       
  2206 #ifdef HSPS_LOG_ACTIVE	        	
       
  2207        	        iLogBus->LogText( _L( "ChspsThemeServer::LocalizeConfigurationsL(): - Failed to retrieve header with an configuration uid %d" ),
       
  2208        	                header->ThemeUid() );
       
  2209 #endif	        	                
       
  2210 	        	                
       
  2211 	        	User::Leave( KErrNotFound );
       
  2212 	        	}
       
  2213 			
       
  2214 			// Try to localize the ODT according to the new device language
       
  2215 			TBool localized = LocalizeL(
       
  2216 				*defEngine,
       
  2217 				*odt );
       
  2218 			if ( localized )
       
  2219 				{
       
  2220 				// Set header's name from the ODT
       
  2221 				header->SetThemeFullNameL( odt->ThemeFullName() );				
       
  2222 				}
       
  2223 			
       
  2224 			CleanupStack::PopAndDestroy( odt );			
       
  2225 			} // for loop		
       
  2226 		
       
  2227 		CleanupStack::PopAndDestroy( defEngine );
       
  2228 		
       
  2229 		} // count
       
  2230 	}
       
  2231 
       
  2232 // -----------------------------------------------------------------------------
       
  2233 // ChspsThemeServer::LocalizeL()
       
  2234 // -----------------------------------------------------------------------------
       
  2235 //
       
  2236 TBool ChspsThemeServer::LocalizeL( 
       
  2237 		ChspsDefinitionEngineInterface& aEngine,
       
  2238 		ChspsODT& aOdt )
       
  2239 	{		  			
       
  2240 	TBool localized = ETrue;
       
  2241 	
       
  2242 	TLanguage requestedLanguage = DeviceLanguage();
       
  2243 	if ( requestedLanguage == ELangNone )
       
  2244 	    {
       
  2245 	    requestedLanguage = ELangTest;
       
  2246 	    }		
       
  2247 	
       
  2248     if ( aOdt.OdtLanguage() != requestedLanguage )
       
  2249         {
       
  2250         localized = EFalse;
       
  2251         TBool domChanged = EFalse;
       
  2252         	
       
  2253     	// Localize XML configuration with the requested language
       
  2254     	if ( aOdt.ConfigurationType() == EhspsAppConfiguration )
       
  2255     	    {	        
       
  2256     	    // Apply plugin and application specific DTD files 
       
  2257     	    domChanged = LocalizeApplicationConfigurationL( 
       
  2258     	            aEngine, 
       
  2259     	            aOdt,
       
  2260     	            requestedLanguage );
       
  2261     	    }
       
  2262     	else
       
  2263     	    {	    
       
  2264     	    // Apply plugin specific DTD files only
       
  2265     	    domChanged = LocalizePluginConfigurationL( 
       
  2266                     aEngine, 
       
  2267                     aOdt,
       
  2268                     requestedLanguage,
       
  2269                     0 );    	
       
  2270     	    }
       
  2271     	
       
  2272     	if ( domChanged )
       
  2273     		{
       
  2274     		// Update ODT with the new localized name from the XML configuration
       
  2275     		TPtrC8 name( hspsServerUtil::FindConfigurationAttrL( aOdt, KConfigurationAttrName ) );
       
  2276     		if ( name.Length() )
       
  2277     			{					
       
  2278     			HBufC* buf = HBufC::NewLC( name.Length() );
       
  2279     			buf->Des().Copy( name );
       
  2280     			aOdt.SetThemeFullNameL( buf->Des() );
       
  2281     			CleanupStack::PopAndDestroy( buf );
       
  2282     
       
  2283     			// The ODT instance has now been localized - either with the requested language or a test language
       
  2284     			aOdt.SetOdtLanguage( requestedLanguage );    			    			
       
  2285     			}
       
  2286     		
       
  2287     		// Update ODT with the new localized description from the XML configuration
       
  2288     		TPtrC8 desc( hspsServerUtil::FindConfigurationAttrL( aOdt, KConfigurationAttrDesc ) );
       
  2289             if ( desc.Length() )
       
  2290                 {
       
  2291                 HBufC* buf = HBufC::NewLC( desc.Length() );
       
  2292                 buf->Des().Copy( desc );
       
  2293                 aOdt.SetDescriptionL( buf->Des() );
       
  2294                 CleanupStack::PopAndDestroy( buf );                
       
  2295                 }
       
  2296             
       
  2297             // Save the changes
       
  2298             iDefinitionRepository->SetOdtL( aOdt );                     
       
  2299             localized = ETrue;
       
  2300     		}    	    	
       
  2301     	
       
  2302     	if ( !localized )
       
  2303     		{
       
  2304 #ifdef HSPS_LOG_ACTIVE		
       
  2305             iLogBus->LogText( 
       
  2306                     _L( "ChspsThemeServer::LocalizeL(): - failed to localize header %S!" ),
       
  2307                     &aOdt.ThemeShortName() 
       
  2308                     );
       
  2309 #endif		        
       
  2310     		}
       
  2311     
       
  2312 #ifdef HSPS_LOG_ACTIVE          
       
  2313         iLogBus->LogText( _L( "ChspsThemeServer::LocalizeL() - DOM dump after localization:" ) );                                       
       
  2314         // Print DOM debug
       
  2315         ChspsOdtDump::Dump( aOdt, *iLogBus );
       
  2316 #endif          
       
  2317         }
       
  2318     
       
  2319 	return localized;
       
  2320 	}
       
  2321 
       
  2322 // -----------------------------------------------------------------------------
       
  2323 // ChspsThemeServer::LocalizeApplicationConfigurationL()
       
  2324 // -----------------------------------------------------------------------------
       
  2325 //
       
  2326 TBool ChspsThemeServer::LocalizeApplicationConfigurationL(
       
  2327         ChspsDefinitionEngineInterface& aEngine,
       
  2328         ChspsODT& aOdt,
       
  2329         const TLanguage aRequestedLanguage )
       
  2330     {    
       
  2331 #ifdef HSPS_LOG_ACTIVE      
       
  2332     iLogBus->LogText( 
       
  2333         _L( "ChspsThemeServer::LocalizeApplicationConfigurationL(): - Localizing %S application configuration..." ), 
       
  2334         &aOdt.ThemeShortName() 
       
  2335         );
       
  2336 #endif    
       
  2337     
       
  2338     // Localize the application configuration itself
       
  2339     TInt resourceFilter = aOdt.ThemeUid(); 
       
  2340     TBool domLocalized = LocalizePluginConfigurationL(
       
  2341             aEngine,
       
  2342             aOdt,
       
  2343             aRequestedLanguage,
       
  2344             resourceFilter );    
       
  2345     
       
  2346     // Next, localize all plugins instances in the DOM
       
  2347     if ( domLocalized )
       
  2348         {
       
  2349         // Find all unique plugin uids that are used in the application configuration 
       
  2350         RArray<TInt> pluginArray;
       
  2351         CleanupClosePushL( pluginArray );            
       
  2352         hspsServerUtil::FindUniquePluginsL( aOdt, pluginArray );
       
  2353         
       
  2354         // Loop the plugins
       
  2355         const TInt pluginsCount = pluginArray.Count();
       
  2356         for( TInt pluginIndex=0; domLocalized && pluginIndex < pluginsCount; pluginIndex++ )
       
  2357             {
       
  2358             // Apply plugin specific DTD files only
       
  2359             TBool pluginLocalized = LocalizePluginConfigurationL(
       
  2360                     aEngine,
       
  2361                     aOdt,
       
  2362                     aRequestedLanguage,
       
  2363                     pluginArray[pluginIndex] );                    
       
  2364             }
       
  2365         
       
  2366         pluginArray.Reset();
       
  2367         CleanupStack::PopAndDestroy( &pluginArray );
       
  2368         }
       
  2369     
       
  2370     return domLocalized;
       
  2371     }
       
  2372 
       
  2373 // -----------------------------------------------------------------------------
       
  2374 // ChspsThemeServer::LocalizePluginConfigurationL()
       
  2375 // -----------------------------------------------------------------------------
       
  2376 //
       
  2377 TBool ChspsThemeServer::LocalizePluginConfigurationL(
       
  2378         ChspsDefinitionEngineInterface& aEngine,
       
  2379         ChspsODT& aOdt,
       
  2380         const TLanguage aRequestedLanguage,
       
  2381         TInt aPluginUid )
       
  2382     {               
       
  2383     // If the plugin has already been localized as requested
       
  2384     if ( aOdt.OdtLanguage() == aRequestedLanguage )
       
  2385         {
       
  2386         return ETrue;
       
  2387         }
       
  2388     
       
  2389     TBool domLocalized = EFalse;
       
  2390     const TInt resourceCount = aOdt.ResourceCount();
       
  2391     
       
  2392     // Localize XML document         
       
  2393     TFileName pluginFolder;
       
  2394     for( TInt resourceIndex=0; !domLocalized && resourceIndex < resourceCount; resourceIndex++ )
       
  2395         {                       
       
  2396         ChspsResource& r = aOdt.ResourceL( resourceIndex );        
       
  2397         if ( r.ResourceType() == EResourceDTD )
       
  2398             {
       
  2399             TBool isPluginSpecific = ETrue;
       
  2400             
       
  2401             // If plugin uid restriction was set (localizing an application configuration)
       
  2402             if ( aPluginUid > 0 )
       
  2403                 {       
       
  2404                 // Check whether the resource belongs to the plugin 
       
  2405                 pluginFolder.Format( _L("\\%d\\"), aPluginUid );
       
  2406                 isPluginSpecific = ( r.FileName().Find(pluginFolder) > 0 );                    
       
  2407                 }            
       
  2408             if ( isPluginSpecific && r.Language() == aRequestedLanguage )             
       
  2409                 {                               
       
  2410                 // Parses the given DTD file and replaces all entity referencies in the DOM tree            
       
  2411                 aEngine.ApplyLanguageL( 
       
  2412                     iFsSession, 
       
  2413                     r.FileName(), 
       
  2414                     aOdt );                                                             
       
  2415                 domLocalized = ETrue;
       
  2416 #ifdef HSPS_LOG_ACTIVE
       
  2417                 if ( aPluginUid > 0 )
       
  2418                     {
       
  2419                     iLogBus->LogText( 
       
  2420                         _L( "ChspsThemeServer::LocalizePluginConfigurationL(): - %S/%X instances localized to %d language!" ), 
       
  2421                         &aOdt.ThemeShortName(),
       
  2422                         aPluginUid,
       
  2423                         aRequestedLanguage );                   
       
  2424                     }
       
  2425                 else
       
  2426                     {
       
  2427                     iLogBus->LogText( 
       
  2428                         _L( "ChspsThemeServer::LocalizePluginConfigurationL(): - %S localized to %d language!" ), 
       
  2429                         &aOdt.ThemeShortName(),                        
       
  2430                         aRequestedLanguage );
       
  2431                     }                
       
  2432 #endif                
       
  2433                 }                               
       
  2434             }
       
  2435         }   
       
  2436 
       
  2437     // If the requested locale couldn't be found   
       
  2438     if ( !domLocalized )
       
  2439         {                                                
       
  2440         // Localize XML document with the test locale (mandatory language, used by HSPS developers)
       
  2441         for( TInt resourceIndex=0; !domLocalized && resourceIndex < resourceCount; resourceIndex++ )
       
  2442             {
       
  2443             ChspsResource& r = aOdt.ResourceL( resourceIndex );            
       
  2444             if ( r.ResourceType() == EResourceDTD )
       
  2445                 {
       
  2446                 TBool isPluginSpecific = ETrue;
       
  2447                 
       
  2448                 // If plugin uid restriction was set (localizing an application configuration)
       
  2449                 if ( aPluginUid > 0 )
       
  2450                     {       
       
  2451                     // Check whether the resource belongs to the plugin 
       
  2452                     pluginFolder.Format( _L("\\%d\\"), aPluginUid );
       
  2453                     isPluginSpecific = ( r.FileName().Find(pluginFolder) > 0 );                    
       
  2454                     }           
       
  2455                 if ( isPluginSpecific && r.Language() == ELangTest )
       
  2456                     {                                
       
  2457                     // Parses the given DTD file and replaces all entity referencies in the DOM tree            
       
  2458                     aEngine.ApplyLanguageL( 
       
  2459                         iFsSession, 
       
  2460                         r.FileName(), 
       
  2461                         aOdt );
       
  2462                     domLocalized = ETrue;
       
  2463 #ifdef HSPS_LOG_ACTIVE      
       
  2464                     if ( aPluginUid > 0 )
       
  2465                         {
       
  2466                         iLogBus->LogText( 
       
  2467                             _L( "ChspsThemeServer::LocalizePluginConfigurationL(): - %S/%X instances localized to %d language!" ), 
       
  2468                             &aOdt.ThemeShortName(),
       
  2469                             aPluginUid,
       
  2470                             ELangTest );                   
       
  2471                         }
       
  2472                     else
       
  2473                         {
       
  2474                         iLogBus->LogText( 
       
  2475                             _L( "ChspsThemeServer::LocalizePluginConfigurationL(): - %S localized to %d language!" ), 
       
  2476                             &aOdt.ThemeShortName(),                        
       
  2477                             ELangTest );
       
  2478                         }
       
  2479 #endif                                                
       
  2480                     }            
       
  2481                 }
       
  2482             }           
       
  2483         }    
       
  2484                 
       
  2485     return domLocalized;
       
  2486     }
       
  2487 
       
  2488 // -----------------------------------------------------------------------------
       
  2489 // ChspsThemeServer::GetConfigurationL()
       
  2490 // -----------------------------------------------------------------------------
       
  2491 //
       
  2492 TInt ChspsThemeServer::GetConfigurationL(
       
  2493 		const TInt aInterfaceUid,
       
  2494 		const TInt aConfigurationUid,
       
  2495 		ChspsODT& aRetrievedODT )
       
  2496 	{			
       
  2497 	// Input validation
       
  2498 	if ( aInterfaceUid < 0 || aConfigurationUid < 1)
       
  2499 		{
       
  2500 #ifdef HSPS_LOG_ACTIVE		
       
  2501 		iLogBus->LogText( _L( "ChspsThemeServer::GetConfigurationL(): - Invalid arguments" ) );
       
  2502 #endif
       
  2503 		
       
  2504         return KErrArgument;        		
       
  2505 		}	
       
  2506 			
       
  2507 	TInt headerCount = HeaderListCache().Count();
       
  2508     if ( headerCount < 1 )
       
  2509         {
       
  2510         UpdateHeaderListCacheL();        
       
  2511         headerCount = HeaderListCache().Count();
       
  2512         if ( headerCount < 1 )
       
  2513             {
       
  2514             // configurations were not found or repository is locked        
       
  2515 #ifdef HSPS_LOG_ACTIVE        
       
  2516             iLogBus->LogText( _L( "ChspsThemeServer::GetConfigurationL(): - Cache is empty" ) );
       
  2517 #endif
       
  2518             return KErrAbort;        	
       
  2519             }
       
  2520         }
       
  2521 
       
  2522 #ifdef HSPS_LOG_ACTIVE        
       
  2523     iLogBus->LogText( _L( "ChspsThemeServer::GetConfigurationL(): - header count in cache: %d" ), iHeaderListCache->Count() );
       
  2524 #endif    
       
  2525     
       
  2526 	// Loop headers in the cache
       
  2527     TBool found = EFalse;
       
  2528     ChspsODT* h = NULL;
       
  2529     for ( TInt i=0; !found && i<iHeaderListCache->Count(); i++ )
       
  2530         {
       
  2531         h = iHeaderListCache->At(i);
       
  2532         TBool rootUidMatched = ETrue;
       
  2533         TBool themeUidMatched = ETrue;
       
  2534         if ( aInterfaceUid )
       
  2535         	{
       
  2536         	rootUidMatched = ( h->RootUid() == aInterfaceUid );        	
       
  2537         	}       
       
  2538     	themeUidMatched = ( h->ThemeUid() == aConfigurationUid );
       
  2539     
       
  2540         found = ( rootUidMatched && themeUidMatched );
       
  2541 //#ifdef HSPS_LOG_ACTIVE        
       
  2542 //        iLogBus->LogText( _L( "ChspsThemeServer::GetConfigurationL(): - header '%S' matched: %d" ), &(h->ThemeShortName()), found );        
       
  2543 //#endif        
       
  2544         } 	        	       	              
       
  2545     if ( !found )
       
  2546         {
       
  2547 #ifdef HSPS_LOG_ACTIVE           
       
  2548         iLogBus->LogText( _L( "ChspsThemeServer::GetConfigurationL(): - theme %D was not found!" ), aConfigurationUid );
       
  2549 #endif
       
  2550         return KErrNotFound;        
       
  2551         }
       
  2552 
       
  2553     // Copy properties which are needed by the definition repository to form up ODT paths            
       
  2554     aRetrievedODT.SetRootUid( h->RootUid() );
       
  2555     aRetrievedODT.SetThemeUid( h->ThemeUid() );        
       
  2556     aRetrievedODT.SetConfigurationType(h->ConfigurationType());
       
  2557     aRetrievedODT.SetRootUid(h->RootUid());
       
  2558     aRetrievedODT.SetProviderUid(h->ProviderUid());
       
  2559     aRetrievedODT.SetThemeUid(h->ThemeUid());
       
  2560     aRetrievedODT.SetProviderNameL(h->ProviderName());
       
  2561     aRetrievedODT.SetThemeFullNameL(h->ThemeFullName());
       
  2562     aRetrievedODT.SetThemeShortNameL(h->ThemeShortName());
       
  2563     aRetrievedODT.SetThemeVersionL(h->ThemeVersion());            
       
  2564     aRetrievedODT.SetFlags(h->Flags());                                                                	      
       
  2565     aRetrievedODT.SetMultiInstance( h->MultiInstance() );
       
  2566                 
       
  2567     // Get a complete ODT instance from the definition repository
       
  2568     if ( iDefinitionRepository->GetOdtL( aRetrievedODT ) )    
       
  2569         {        
       
  2570 #ifdef HSPS_LOG_ACTIVE        
       
  2571         iLogBus->LogText( _L( "ChspsThemeServer::GetConfigurationL(): - theme cannot be accessed." ) );
       
  2572 #endif                
       
  2573         return KErrAccessDenied;
       
  2574         }    
       
  2575     
       
  2576     return KErrNone;
       
  2577 	}
       
  2578 
       
  2579 // -----------------------------------------------------------------------------
       
  2580 // ChspsThemeServer::HandleRomInstallationsL()
       
  2581 // -----------------------------------------------------------------------------
       
  2582 //
       
  2583 void ChspsThemeServer::HandleRomInstallationsL()
       
  2584     {    
       
  2585 #ifdef HSPS_LOG_ACTIVE        
       
  2586         iLogBus->LogText( _L( "ChspsThemeServer::HandleRomInstallationsL()" ) );
       
  2587 #endif    
       
  2588     
       
  2589     const TInt key = KCenrepKeyPostRFS;
       
  2590     const TUint32 fullMask = 0xFFFFFFFF;
       
  2591     RArray<TUint32> res;
       
  2592     CleanupClosePushL( res );
       
  2593     iCentralRepository->FindL( key, fullMask, res );
       
  2594     if ( res.Count() == 0 )
       
  2595         {
       
  2596         // The key should already exist!
       
  2597 #ifdef HSPS_LOG_ACTIVE        
       
  2598         iLogBus->LogText( _L( "ChspsThemeServer::HandleRomInstallationsL(): - Couldn't find %d key from the HSPS cenrep!" ), key );
       
  2599 #endif                        
       
  2600                         
       
  2601         User::Leave( KErrNotFound );
       
  2602         }   
       
  2603     
       
  2604     // Check the key's value
       
  2605     // lets use KSysUtilVersionTextLength instead of KSysVersionInfoRevisionLength if we
       
  2606     // have to use SysUtil::GetSWVersion because KSysUtilVersionTextLength is bigger.
       
  2607     TBuf<KSysUtilVersionTextLength> fwVersion;
       
  2608     TInt errorCode = iCentralRepository->Get( key, fwVersion );
       
  2609     if( ( errorCode == KErrNone ) &&
       
  2610         ( fwVersion.Length() == 0 ) )
       
  2611         {
       
  2612         // Install manifest files from ROM
       
  2613         InstallManifestsFromRomDriveL();
       
  2614 
       
  2615         // Post RFS installations have been done, prevent re-installations at next startup
       
  2616         // by reading firmware version and saving it to cenrep.
       
  2617         GetFWVersion( fwVersion );
       
  2618         
       
  2619         errorCode = iCentralRepository->Set( key, fwVersion );
       
  2620         if ( errorCode  )
       
  2621             {
       
  2622 #ifdef HSPS_LOG_ACTIVE
       
  2623             iLogBus->LogText( _L( "ChspsThemeServer::HandleRomInstallationsL(): - Failed to update the cenrep!" ) );
       
  2624 #endif
       
  2625 
       
  2626             User::Leave( KErrWrite );
       
  2627             }
       
  2628         else
       
  2629             {
       
  2630 #ifdef HSPS_LOG_ACTIVE
       
  2631             iLogBus->LogText( _L( "ChspsThemeServer::HandleRomInstallationsL(): - ROM configurations were installed successfully." ) );
       
  2632 #endif
       
  2633             }
       
  2634         }
       
  2635     else
       
  2636         {
       
  2637         // We have to check whether phone software has been updated for example by FOTA.
       
  2638         // If the firmware version is different we need to run backup and restore to
       
  2639         // check whether there are any new widgets introduced and check can we use
       
  2640         // still same application configure. If firmware is same nothing needs to be done.
       
  2641         TBuf<KSysUtilVersionTextLength> currentFWVersion;
       
  2642         GetFWVersion( currentFWVersion );        
       
  2643         if ( fwVersion != currentFWVersion )
       
  2644             {
       
  2645             // Phone software has been updated.
       
  2646             CreateBackupDataL();
       
  2647             InstallManifestsFromRomDriveL();
       
  2648             RestoreApplicationConfigurationsL();
       
  2649             // Save new firmware version to cenrep
       
  2650             if ( errorCode == KErrNone )
       
  2651                 {
       
  2652                 errorCode = iCentralRepository->Set( key, currentFWVersion );                
       
  2653                 }
       
  2654             else if ( errorCode == KErrArgument )
       
  2655                 {
       
  2656                 // We came here because older version of HSPS cenrep value
       
  2657                 // of key was integer and now it is string.
       
  2658                 // Delete key and create new with different type
       
  2659                 iCentralRepository->Delete( key );
       
  2660                 errorCode = iCentralRepository->Create( key, currentFWVersion );                
       
  2661                 }
       
  2662             
       
  2663             if ( errorCode  )
       
  2664                 {
       
  2665 #ifdef HSPS_LOG_ACTIVE
       
  2666             iLogBus->LogText( _L( "ChspsThemeServer::HandleRomInstallationsL(): - Failed to update the cenrep!" ) );
       
  2667 #endif
       
  2668 
       
  2669                 User::Leave( KErrWrite );
       
  2670                 }
       
  2671             else
       
  2672                 {
       
  2673 #ifdef HSPS_LOG_ACTIVE
       
  2674             iLogBus->LogText( _L( "ChspsThemeServer::HandleRomInstallationsL(): - ROM configurations were installed successfully." ) );
       
  2675 #endif
       
  2676                 }
       
  2677             }
       
  2678         }
       
  2679     
       
  2680 #if defined(WINSCW) || defined(__WINS__)
       
  2681     // Activate client specific root configurations from active display resolution
       
  2682     ActivateRootConfigurationsL();
       
  2683 #endif // defined(WINSCW)    
       
  2684     
       
  2685     res.Close();    
       
  2686     CleanupStack::PopAndDestroy(1, &res); 
       
  2687     }
       
  2688 
       
  2689 // -----------------------------------------------------------------------------
       
  2690 // ChspsThemeServer::InstallManifestsFromRomDriveL()
       
  2691 // -----------------------------------------------------------------------------
       
  2692 //
       
  2693 void ChspsThemeServer::InstallManifestsFromRomDriveL()
       
  2694 	{							
       
  2695 	if ( iRomInstaller || iManifestFiles.Count() )
       
  2696 	    {
       
  2697 	    // Invalid usage
       
  2698 	    User::Leave( KErrGeneral );
       
  2699 	    }	
       
  2700 	iRomInstaller = ChspsRomInstaller::NewL( *this, iFsSession );	
       
  2701 #ifdef HSPS_LOG_ACTIVE            	
       
  2702 	iRomInstaller->SetLogBus( iLogBus );
       
  2703 #endif
       
  2704 	
       
  2705 	// An array for installation files with V2 directory structure
       
  2706 	RPointerArray<HBufC> pluginFolders;
       
  2707     CleanupClosePushL( pluginFolders );				
       
  2708 	        
       
  2709     // Retrieve an array of folder names 
       
  2710     iRomInstaller->GetInstallationFoldersL( pluginFolders );  
       
  2711 		
       
  2712     // Add manifest files from the subfolders
       
  2713     FindRomInstallationsV2L( pluginFolders );
       
  2714     
       
  2715     pluginFolders.ResetAndDestroy();
       
  2716         
       
  2717     if ( iManifestFiles.Count() < 1 )
       
  2718         {
       
  2719 #ifdef HSPS_LOG_ACTIVE            
       
  2720         iLogBus->LogText( _L( "ChspsThemeServer::InstallManifestsFromRomDriveL(): - mandatory plugins were not found from the ROM drive!" ) );
       
  2721 #endif                                  
       
  2722         // Mandatory plugins were missing from the ROM drive
       
  2723         User::Leave( KErrGeneral );
       
  2724         }
       
  2725     
       
  2726 	CleanupStack::PopAndDestroy( 1, &pluginFolders );
       
  2727 	
       
  2728 	
       
  2729 	// Install configurations from the manifest files 
       
  2730     ThspsServiceCompletedMessage ret = EhspsInstallThemeFailed;
       
  2731     for( TInt manifestIndex=0; manifestIndex < iManifestFiles.Count(); manifestIndex++ )
       
  2732         {                               
       
  2733         // Synchronous API call 
       
  2734         TPtr name( iManifestFiles[manifestIndex]->Des() );
       
  2735 #ifdef HSPS_LOG_ACTIVE            
       
  2736         iLogBus->LogText( _L( "ChspsThemeServer::InstallManifestsFromRomDriveL(): - installing ROM configuration from: %S" ), &name );
       
  2737 #endif      
       
  2738         ret = iRomInstaller->InstallThemeL( name  );
       
  2739         if ( ret != EhspsInstallThemeSuccess )
       
  2740             {
       
  2741 #ifdef HSPS_LOG_ACTIVE            
       
  2742             iLogBus->LogText( _L( "ChspsThemeServer::InstallManifestsFromRomDriveL(): - configuration is corrupted, critical error - shutting down!" ) );
       
  2743 #endif                  
       
  2744 //            User::Leave( KErrAbort );
       
  2745             }
       
  2746         }
       
  2747         
       
  2748     // Cancel any actions done in the previous functionality
       
  2749     iManifestFiles.ResetAndDestroy();
       
  2750 		
       
  2751 	// The ROM installer is not needed anymore and therefore it can be released
       
  2752 	delete iRomInstaller;
       
  2753 	iRomInstaller = NULL;
       
  2754 	
       
  2755 	// Force updating of the header cache
       
  2756     ThspsRepositoryInfo info( EhspsCacheUpdate );
       
  2757     iDefinitionRepository->RegisterNotification( info );
       
  2758 	}
       
  2759 
       
  2760 // -----------------------------------------------------------------------------
       
  2761 // ChspsThemeServer::FindRomInstallationsV2L()
       
  2762 // -----------------------------------------------------------------------------
       
  2763 //
       
  2764 void ChspsThemeServer::FindRomInstallationsV2L(        
       
  2765         RPointerArray<HBufC>& aPluginFolders )
       
  2766     {    
       
  2767     _LIT(KHspsFolder, "hsps\\");
       
  2768     TFileName hspsPath;
       
  2769     for( TInt folderIndex=0; folderIndex < aPluginFolders.Count(); folderIndex++ )
       
  2770         {
       
  2771         // Set path
       
  2772         hspsPath.Copy( aPluginFolders[folderIndex]->Des() );
       
  2773         hspsPath.Append( KHspsFolder );        
       
  2774                         
       
  2775         // Add full path into the installation queue
       
  2776         TInt len = hspsPath.Length() + 3 + KDoubleBackSlash().Length() + KManifestFile().Length();
       
  2777         HBufC* manifestBuf = HBufC::NewLC( len );        
       
  2778         manifestBuf->Des().Copy( hspsPath );
       
  2779         manifestBuf->Des().Append( _L("00") );                        
       
  2780         manifestBuf->Des().Append( KDoubleBackSlash );
       
  2781         manifestBuf->Des().Append( KManifestFile );        
       
  2782                 
       
  2783         iManifestFiles.AppendL( manifestBuf );                
       
  2784         CleanupStack::Pop( manifestBuf );                       
       
  2785         }
       
  2786     }
       
  2787 
       
  2788 // -----------------------------------------------------------------------------
       
  2789 // ChspsThemeServer::GetConfigurationHeader()
       
  2790 // -----------------------------------------------------------------------------
       
  2791 //
       
  2792 void ChspsThemeServer::GetConfigurationHeader(
       
  2793     ChspsODT& aSearchMask,
       
  2794     ChspsODT*& aConfHeader,
       
  2795     TInt& aPos )
       
  2796     {
       
  2797     ChspsODT* header( NULL );
       
  2798     TBool rootUidMatch( ETrue );
       
  2799     TBool themeUidMatch( ETrue );
       
  2800     TBool flagsMatch( ETrue );
       
  2801     TBool typeMatch( ETrue );
       
  2802     aConfHeader = NULL;
       
  2803     for ( TInt i = aPos; i < iHeaderListCache->Count() && aConfHeader == NULL; i++ )
       
  2804         {
       
  2805         header = iHeaderListCache->At(i);
       
  2806         if ( aSearchMask.RootUid() && 
       
  2807              aSearchMask.RootUid() != header->RootUid() )
       
  2808             {
       
  2809             // Root UID defined in search criteria and Root UID does not match
       
  2810             rootUidMatch = EFalse;
       
  2811             }
       
  2812         // else Root UID check ignored
       
  2813         if ( aSearchMask.ThemeUid() && 
       
  2814              aSearchMask.ThemeUid() != header->ThemeUid() )
       
  2815             {
       
  2816             // Theme UID defined in search criteria and Theme UID does not match
       
  2817             themeUidMatch = EFalse;
       
  2818             }
       
  2819         // else Theme UID check ignored
       
  2820         if ( aSearchMask.Flags() && 
       
  2821              !( aSearchMask.Flags() & header->Flags() ) )
       
  2822             {
       
  2823             // Header flags defined in search criteria and Header flags does not match
       
  2824             flagsMatch = EFalse;
       
  2825             }
       
  2826         // else header flags check ignored
       
  2827         if ( aSearchMask.ConfigurationType() &&
       
  2828              aSearchMask.ConfigurationType() != header->ConfigurationType() )
       
  2829             {
       
  2830             typeMatch = EFalse;
       
  2831             }
       
  2832         // else configuration type ignored
       
  2833         if ( rootUidMatch && themeUidMatch && flagsMatch && typeMatch )
       
  2834             {
       
  2835             aConfHeader = header;
       
  2836             aPos = i;
       
  2837             }
       
  2838         else
       
  2839             {
       
  2840             rootUidMatch = ETrue;
       
  2841             themeUidMatch = ETrue;
       
  2842             flagsMatch = ETrue;
       
  2843             typeMatch = ETrue;
       
  2844             }
       
  2845         }                                     
       
  2846     }
       
  2847 
       
  2848 // -----------------------------------------------------------------------------
       
  2849 // ChspsThemeServer::ReinstallConfL()
       
  2850 // -----------------------------------------------------------------------------
       
  2851 //
       
  2852 void ChspsThemeServer::ReinstallConfL(
       
  2853     TInt aAppUid,
       
  2854     TInt aConfUid )
       
  2855     {
       
  2856     
       
  2857     RFs fileserver;
       
  2858     CleanupClosePushL( fileserver );
       
  2859     User::LeaveIfError( fileserver.Connect() );   
       
  2860     
       
  2861     ChspsRomInstaller* installer = ChspsRomInstaller::NewL( *this, fileserver );
       
  2862     CleanupStack::PushL( installer );
       
  2863     
       
  2864     installer->ReinstallThemeL( aAppUid, aConfUid );
       
  2865     
       
  2866     CleanupStack::PopAndDestroy( installer );
       
  2867     CleanupStack::PopAndDestroy( &fileserver );
       
  2868 
       
  2869     }
       
  2870 
       
  2871 // -----------------------------------------------------------------------------
       
  2872 // ChspsThemeServer::HandleBREventL()
       
  2873 // -----------------------------------------------------------------------------
       
  2874 //
       
  2875 void ChspsThemeServer::HandleBREventL(
       
  2876     TInt aEvent )
       
  2877     {
       
  2878 
       
  2879     switch ( iBRState )
       
  2880         {
       
  2881         case EhspsBRRestore:
       
  2882             {
       
  2883             HandleBRRestoreStateL( aEvent );
       
  2884             }
       
  2885             break;
       
  2886         case EhspsBRBackup:
       
  2887             {
       
  2888             HandleBRBackupStateL( aEvent );
       
  2889             }
       
  2890             break;
       
  2891         default: // EhspsBRNormal
       
  2892             {
       
  2893             HandleBRNormalStateL( aEvent );
       
  2894             }
       
  2895             break;
       
  2896         }    
       
  2897 
       
  2898     }
       
  2899 
       
  2900 // -----------------------------------------------------------------------------
       
  2901 // ChspsThemeServer::HandleBRNormalStateL()
       
  2902 // -----------------------------------------------------------------------------
       
  2903 //
       
  2904 void ChspsThemeServer::HandleBRNormalStateL(
       
  2905     TInt aEvent )
       
  2906     {
       
  2907     if ( aEvent & conn::EBURBackupFull ||
       
  2908          aEvent & conn::EBURBackupPartial )
       
  2909         {
       
  2910         CreateBRHandlerL( aEvent );
       
  2911         if ( iBRHandler && iActiveBackupClient )
       
  2912             {
       
  2913             iBRState = EhspsBRBackup;
       
  2914 #ifdef HSPS_LOG_ACTIVE            
       
  2915             iLogBus->LogText( _L( "ChspsThemeServer::HandleBRNormalStateL(): - Backup starts" ) );
       
  2916 #endif                  
       
  2917             }
       
  2918         }
       
  2919 
       
  2920     if ( aEvent & conn::EBURRestoreFull ||
       
  2921          aEvent & conn::EBURRestorePartial )
       
  2922         {
       
  2923         CreateBRHandlerL( aEvent );
       
  2924         if ( iBRHandler && iActiveBackupClient )
       
  2925             {
       
  2926             iBRState = EhspsBRRestore;
       
  2927 #ifdef HSPS_LOG_ACTIVE            
       
  2928             iLogBus->LogText( _L( "ChspsThemeServer::HandleBRNormalStateL(): - Restore starts" ) );
       
  2929 #endif                  
       
  2930             }
       
  2931         }
       
  2932     }
       
  2933 
       
  2934 // -----------------------------------------------------------------------------
       
  2935 // ChspsThemeServer::HandleBRBackupStateL()
       
  2936 // -----------------------------------------------------------------------------
       
  2937 //
       
  2938 void ChspsThemeServer::HandleBRBackupStateL(
       
  2939     TInt aEvent )
       
  2940     {
       
  2941     if ( aEvent & conn::EBURNormal )
       
  2942         {
       
  2943         DeleteBRHandler();
       
  2944         iBRState = EhspsBRNormal;
       
  2945 #ifdef HSPS_LOG_ACTIVE            
       
  2946         iLogBus->LogText( _L( "ChspsThemeServer::HandleBRBackupStateL(): - Backup completed" ) );
       
  2947 #endif                  
       
  2948         }
       
  2949     }
       
  2950 
       
  2951 // -----------------------------------------------------------------------------
       
  2952 // ChspsThemeServer::HandleBRRestoreState()
       
  2953 // -----------------------------------------------------------------------------
       
  2954 //
       
  2955 void ChspsThemeServer::HandleBRRestoreStateL(
       
  2956     TInt aEvent )
       
  2957     {
       
  2958     if ( aEvent & conn::EBURNormal )
       
  2959         {
       
  2960         // Disable auto installations
       
  2961         DisableAutoInstallation();
       
  2962         // Restore imported configurations from backup/import folder
       
  2963         RestoreImportedConfigurationsL();
       
  2964         // Restore application configurations from backup/themes folder
       
  2965         RestoreApplicationConfigurationsL();
       
  2966         // Restore completed
       
  2967         DeleteBRHandler();
       
  2968         iBRState = EhspsBRNormal;
       
  2969         EnableAutoInstallationL();
       
  2970 #ifdef HSPS_LOG_ACTIVE            
       
  2971         iLogBus->LogText( _L( "ChspsThemeServer::HandleBRRestoreStateL(): - Restore completed" ) );
       
  2972 #endif                  
       
  2973         }
       
  2974     }
       
  2975 
       
  2976 // -----------------------------------------------------------------------------
       
  2977 // ChspsThemeServer::CreateBRHandlerL()
       
  2978 // -----------------------------------------------------------------------------
       
  2979 //
       
  2980 void ChspsThemeServer::CreateBRHandlerL(
       
  2981     TInt aEvent )
       
  2982     {
       
  2983     if ( !iBRHandler )
       
  2984         {
       
  2985         iBRHandler = CHSPSBRHandler::NewL( *this );
       
  2986         }
       
  2987     if ( !iActiveBackupClient )
       
  2988         {
       
  2989         iActiveBackupClient = conn::CActiveBackupClient::NewL( iBRHandler );
       
  2990         }
       
  2991     if ( aEvent & conn::EBURBackupPartial ||
       
  2992          aEvent & conn::EBURRestorePartial )
       
  2993         {
       
  2994         if( !iActiveBackupClient->DoesPartialBURAffectMeL() )
       
  2995             {
       
  2996             DeleteBRHandler();
       
  2997             }
       
  2998         }
       
  2999     if ( iBRHandler && iActiveBackupClient )
       
  3000         {
       
  3001         iActiveBackupClient->ConfirmReadyForBURL( KErrNone );
       
  3002         }
       
  3003     }
       
  3004 
       
  3005 // -----------------------------------------------------------------------------
       
  3006 // ChspsThemeServer::DeleteBRHandler()
       
  3007 // -----------------------------------------------------------------------------
       
  3008 //
       
  3009 void ChspsThemeServer::DeleteBRHandler()
       
  3010     {
       
  3011     delete iActiveBackupClient;
       
  3012     iActiveBackupClient = NULL;
       
  3013     delete iBRHandler;
       
  3014     iBRHandler = NULL;
       
  3015     }
       
  3016 
       
  3017 // -----------------------------------------------------------------------------
       
  3018 // ChspsThemeServer::CreateBackupDataL()
       
  3019 // -----------------------------------------------------------------------------
       
  3020 //
       
  3021 void ChspsThemeServer::CreateBackupDataL()
       
  3022     {
       
  3023 
       
  3024     // Clear previous backup data
       
  3025     iDefinitionRepository->ClearBackupsL();
       
  3026     
       
  3027     // Get first application configuration header
       
  3028     ChspsODT* searchMask = ChspsODT::NewL();
       
  3029     CleanupStack::PushL( searchMask );
       
  3030     searchMask->SetConfigurationType( EhspsAppConfiguration );
       
  3031     ChspsODT* confHeader( NULL );
       
  3032     TInt pos( 0 );
       
  3033     GetConfigurationHeader( *searchMask, confHeader, pos );
       
  3034     
       
  3035     while ( confHeader != NULL )
       
  3036         {
       
  3037         // Backup application configuration
       
  3038         iDefinitionRepository->BackupConfigurationL( *confHeader );
       
  3039         // Get next application configuration header
       
  3040         pos++;
       
  3041         GetConfigurationHeader( *searchMask, confHeader, pos );
       
  3042         }
       
  3043         
       
  3044     CleanupStack::PopAndDestroy( searchMask );
       
  3045     }
       
  3046 
       
  3047 // -----------------------------------------------------------------------------
       
  3048 // ChspsThemeServer::EnableAutoInstallationL()
       
  3049 // -----------------------------------------------------------------------------
       
  3050 //
       
  3051 void ChspsThemeServer::EnableAutoInstallationL()
       
  3052     {
       
  3053 
       
  3054     // Reset installation arrays
       
  3055     iInstallableSisxThemes.Reset();
       
  3056     iInstalledSisxThemes.ResetAndDestroy();
       
  3057     iManifestFiles.ResetAndDestroy();                   
       
  3058 
       
  3059     // Get installed configurations from the import folders 
       
  3060     CDir* fileList( NULL );    
       
  3061     TFindFile fileFinder( iFsSession );    
       
  3062     fileFinder.FindWildByDir( 
       
  3063         KFilterAllImportsV1, 
       
  3064         KImportDirectoryC, 
       
  3065         fileList );     
       
  3066     CleanupStack::PushL( fileList );    
       
  3067     if( fileList ) 
       
  3068         {            
       
  3069         const TInt installedCount = fileList->Count(); 
       
  3070         for( TInt i = 0; i < installedCount ; i++ )                   
       
  3071             {           
       
  3072             const TEntry& entry = (*fileList)[i];                                              
       
  3073             iInstalledSisxThemes.AppendL( entry.iName.AllocL() );            
       
  3074             }           
       
  3075         }           
       
  3076     CleanupStack::PopAndDestroy( fileList );        
       
  3077 
       
  3078     // Create file change listener to observe changes in import folder
       
  3079     delete iFileChangeListener;
       
  3080     iFileChangeListener = NULL;
       
  3081     iFileChangeListener = ChspsFileChangeListener::NewL( 
       
  3082         iFsSession, 
       
  3083         *this, 
       
  3084         ENotifyAll, 
       
  3085         KImportDirectoryC, 
       
  3086         TImportDirListener );    
       
  3087 #ifdef HSPS_LOG_ACTIVE    
       
  3088     iFileChangeListener->SetLogBus( iLogBus );
       
  3089 #endif        
       
  3090     iFileChangeListener->Setup();           
       
  3091 
       
  3092     }
       
  3093 
       
  3094 // -----------------------------------------------------------------------------
       
  3095 // ChspsThemeServer::DisableAutoInstallation()
       
  3096 // -----------------------------------------------------------------------------
       
  3097 //
       
  3098 void ChspsThemeServer::DisableAutoInstallation()
       
  3099     {
       
  3100     // Reset installation arrays
       
  3101     iInstallableSisxThemes.Reset();
       
  3102     iInstalledSisxThemes.ResetAndDestroy();
       
  3103     iManifestFiles.ResetAndDestroy();                   
       
  3104 
       
  3105     // Delete import folder observer
       
  3106     delete iFileChangeListener;
       
  3107     iFileChangeListener = NULL;
       
  3108     }
       
  3109 
       
  3110 // -----------------------------------------------------------------------------
       
  3111 // ChspsThemeServer::RestoreImportedConfigurationsL()
       
  3112 // -----------------------------------------------------------------------------
       
  3113 //
       
  3114 void ChspsThemeServer::RestoreImportedConfigurationsL()
       
  3115     {
       
  3116     // Check that import folder exists and can be opened
       
  3117     if ( BaflUtils::CheckFolder( iFsSession, KImportDirectoryC ) == KErrNone )
       
  3118         {
       
  3119         // Get list of restored configuration manifest.dat files included in 
       
  3120         // import directory
       
  3121         RArray <TFileName> files;
       
  3122         CleanupClosePushL( files );
       
  3123         hspsServerUtil::FindFilesL( 
       
  3124             KImportDirectoryC, 
       
  3125             KManifestFile, 
       
  3126             files );
       
  3127         
       
  3128         // Install found configurations
       
  3129         TInt err( KErrNone );
       
  3130         CHSPSInstaller* installer = CHSPSInstaller::NewL( *this );
       
  3131         CleanupStack::PushL( installer );
       
  3132         for ( TInt i = 0; i < files.Count(); i++ )
       
  3133             {
       
  3134 #ifdef HSPS_LOG_ACTIVE            
       
  3135             iLogBus->LogText( _L( "ChspsThemeServer::InstallRestoredConfigurationsL(): - Install configuration: %S" ), &files[ i ] );
       
  3136 #endif                  
       
  3137             TRAP( err, installer->InstallConfigurationL( files[ i ] ) );
       
  3138 #ifdef HSPS_LOG_ACTIVE            
       
  3139             if ( err != KErrNone )
       
  3140                 {
       
  3141                 iLogBus->LogText( _L( "ChspsThemeServer::InstallRestoredConfigurationsL(): - Installation failed: %S" ), &files[ i ] );
       
  3142                 }
       
  3143 #endif                  
       
  3144             }
       
  3145         CleanupStack::PopAndDestroy( installer );
       
  3146         CleanupStack::PopAndDestroy( &files );
       
  3147         }
       
  3148     }
       
  3149 
       
  3150 // -----------------------------------------------------------------------------
       
  3151 // ChspsThemeServer::RestoreApplicationConfigurationsL()
       
  3152 // -----------------------------------------------------------------------------
       
  3153 //
       
  3154 void ChspsThemeServer::RestoreApplicationConfigurationsL()
       
  3155     {
       
  3156     // Get the list of application configuration files included
       
  3157     // backup/themes directory
       
  3158     RArray <TFileName> files;
       
  3159     CleanupClosePushL( files );
       
  3160     hspsServerUtil::FindFilesL( 
       
  3161         KBackupThemesDirectoryC, 
       
  3162         KConfFile, 
       
  3163         files );
       
  3164     
       
  3165     // Get application configuration ODT's
       
  3166     TInt err( KErrNone );
       
  3167     CArrayPtrSeg<ChspsODT>* odts = 
       
  3168         new ( ELeave )CArrayPtrSeg<ChspsODT>( KHeaderListGranularity );
       
  3169     CleanupStack::PushL( TCleanupItem( ResetAndDestroyChspsODTArray, odts ) );
       
  3170     
       
  3171     for ( TInt i = 0; i < files.Count(); i++ )
       
  3172         {
       
  3173 #ifdef HSPS_LOG_ACTIVE            
       
  3174         iLogBus->LogText( _L( "ChspsThemeServer::RestoreApplicationConfigurationsL(): - Read application configuration ODT: %S" ), &files[ i ] );
       
  3175 #endif                  
       
  3176         ChspsODT* odt = ChspsODT::NewL();
       
  3177         CleanupStack::PushL( odt );
       
  3178         TRAP( err, iDefinitionRepository->GetOdtL( files[ i ], *odt ) );
       
  3179         if ( err == KErrNone )
       
  3180             {
       
  3181             // Add ODT to ODT list
       
  3182             odts->AppendL( odt );
       
  3183             CleanupStack::Pop( odt );
       
  3184             }
       
  3185         else
       
  3186             {
       
  3187 #ifdef HSPS_LOG_ACTIVE            
       
  3188             iLogBus->LogText( _L( "ChspsThemeServer::RestoreApplicationConfigurationsL(): - Application configuration ODT reading faild: %S" ), &files[ i ] );
       
  3189 #endif                  
       
  3190             CleanupStack::PopAndDestroy( odt );
       
  3191             }
       
  3192         }
       
  3193     
       
  3194     // Validate application configurations
       
  3195     for ( TInt i = 0; i < odts->Count(); i++ )
       
  3196         {
       
  3197         ChspsODT* odt = odts->At( i );
       
  3198 #ifdef HSPS_LOG_ACTIVE            
       
  3199         iLogBus->LogText( _L( "ChspsThemeServer::RestoreApplicationConfigurationsL(): - Validate applicartion configuration: %S" ), &odt->ThemeFullName() );
       
  3200 #endif                  
       
  3201         TRAP( err, ValidateRestoredConfigurationL( *odt ) );
       
  3202         if ( err == KErrNone )
       
  3203             {
       
  3204             // Restore application configuration
       
  3205             RestoreConfigurationL( *odt );
       
  3206             }
       
  3207         else
       
  3208             {
       
  3209 #ifdef HSPS_LOG_ACTIVE            
       
  3210             iLogBus->LogText( _L( "ChspsThemeServer::RestoreApplicationConfigurationsL(): - Validation failed: %S" ), &odt->ThemeFullName() );
       
  3211 #endif                  
       
  3212             }
       
  3213         }
       
  3214 
       
  3215     CleanupStack::PopAndDestroy( odts );
       
  3216     CleanupStack::PopAndDestroy( &files );
       
  3217 
       
  3218     }
       
  3219 
       
  3220 // -----------------------------------------------------------------------------
       
  3221 // ChspsThemeServer::ValidateRestoredConfigurationL()
       
  3222 // -----------------------------------------------------------------------------
       
  3223 //
       
  3224 void ChspsThemeServer::ValidateRestoredConfigurationL(
       
  3225     ChspsODT& aOdt )
       
  3226     {
       
  3227     
       
  3228     // Validate application configuration
       
  3229     TBuf8<10> appConfUid; 
       
  3230     _LIT8( KFormat8, "%X" );
       
  3231     _LIT8( KHexPrefix, "0x" );
       
  3232     appConfUid.Append( KHexPrefix );
       
  3233     appConfUid.AppendFormat( KFormat8, aOdt.ThemeUid() );
       
  3234     TPtrC8 appConfState;
       
  3235     hspsServerUtil::GetAttributeValueL(
       
  3236         aOdt,
       
  3237         KConfigurationElement,
       
  3238         KConfigurationAttrUid,
       
  3239         appConfUid,
       
  3240         KConfigurationAttrState,
       
  3241         appConfState );
       
  3242     
       
  3243     if ( appConfState.Compare( KConfStateError ) == 0 )
       
  3244         {
       
  3245         // Invalid application configuration
       
  3246         User::Leave( KErrGeneral );
       
  3247         }
       
  3248     
       
  3249     // Check application configuration version
       
  3250     ChspsODT* searchMask = ChspsODT::NewL();
       
  3251     CleanupStack::PushL( searchMask );
       
  3252     searchMask->SetThemeUid( aOdt.ThemeUid() );
       
  3253     ChspsODT* header( NULL );
       
  3254     TInt pos( 0 );
       
  3255     GetConfigurationHeader( *searchMask, header, pos );
       
  3256     if ( !header )
       
  3257         {
       
  3258         User::Leave( KErrNotFound );
       
  3259         }
       
  3260     else
       
  3261         {
       
  3262         hspsServerUtil::CheckConfigurationVersionL(
       
  3263                         aOdt,
       
  3264                         aOdt.ThemeUid(),
       
  3265                         header->ThemeVersion() );
       
  3266         }
       
  3267     CleanupStack::PopAndDestroy( searchMask );
       
  3268 
       
  3269     // Validate included plugins
       
  3270     // Get configuration UIDs from restored configuration
       
  3271     RArray <TInt> uids;
       
  3272     CleanupClosePushL( uids );
       
  3273     hspsServerUtil::FindUniquePluginsL(
       
  3274         aOdt,
       
  3275         uids );
       
  3276         
       
  3277     for ( TInt i = 0; i < uids.Count(); i++ )
       
  3278         {
       
  3279         // Check if the configuration is installed
       
  3280         TInt err( KErrNone );
       
  3281         ChspsODT* searchMask = ChspsODT::NewL();
       
  3282         CleanupStack::PushL( searchMask );
       
  3283         searchMask->SetThemeUid( uids[ i ] );
       
  3284         ChspsODT* header( NULL );
       
  3285         TInt pos( 0 );
       
  3286         GetConfigurationHeader( *searchMask, header, pos );
       
  3287         if ( !header ) // Configuration not found
       
  3288             {
       
  3289             err = KErrNotFound;
       
  3290             }
       
  3291         else // Configuration found
       
  3292             {
       
  3293             // Check the configuration version
       
  3294             TPtrC8 version;
       
  3295             TRAP( err, hspsServerUtil::CheckConfigurationVersionL(
       
  3296                 aOdt,
       
  3297                 uids[ i ],
       
  3298                 header->ThemeVersion() ) );
       
  3299             if ( err == KErrNone )
       
  3300                 {
       
  3301                 // Check that configuration resource files are found
       
  3302                 TRAP( err, hspsServerUtil::CheckResourceFilesL( 
       
  3303                     aOdt, 
       
  3304                     uids[ i ] ) );
       
  3305                 }
       
  3306             }
       
  3307         
       
  3308         // Check configuration state attribute
       
  3309         // Configuration UID as HEX string
       
  3310         // 10 -> TInt as 0xXXXXXXXX string
       
  3311         TBuf8<10> confUid; 
       
  3312         _LIT8( KFormat8, "%X" );
       
  3313         _LIT8( KHexPrefix, "0x" );
       
  3314         confUid.Append( KHexPrefix );
       
  3315         confUid.AppendFormat( KFormat8, uids[ i ] );
       
  3316         TPtrC8 state;
       
  3317         hspsServerUtil::GetAttributeValueL(
       
  3318             aOdt,
       
  3319             KConfigurationElement,
       
  3320             KConfigurationAttrUid,
       
  3321             confUid,
       
  3322             KConfigurationAttrState,
       
  3323             state );
       
  3324 
       
  3325         if ( err != KErrNone )        
       
  3326             {
       
  3327             // Invalid configuration
       
  3328             state.Set( KConfStateError );
       
  3329             }
       
  3330         else if ( state.CompareF( KConfStateError ) != 0 )
       
  3331             {
       
  3332             state.Set( KConfStateNotConfirmed );
       
  3333             }
       
  3334         hspsServerUtil::SetAttributeValueL(
       
  3335             aOdt,
       
  3336             KConfigurationElement,
       
  3337             KConfigurationAttrUid,
       
  3338             confUid,
       
  3339             KConfigurationAttrState,
       
  3340             state );
       
  3341         
       
  3342         CleanupStack::PopAndDestroy( searchMask );
       
  3343         }
       
  3344     
       
  3345     CleanupStack::PopAndDestroy( &uids );
       
  3346     }
       
  3347 
       
  3348 // -----------------------------------------------------------------------------
       
  3349 // ChspsThemeServer::RestoreConfigurationL()
       
  3350 // -----------------------------------------------------------------------------
       
  3351 //
       
  3352 void ChspsThemeServer::RestoreConfigurationL(
       
  3353     ChspsODT& aOdt )
       
  3354     {
       
  3355     
       
  3356     if ( iDefinitionRepository->Locked() )
       
  3357         {
       
  3358         User::Leave( KErrServerBusy );
       
  3359         }
       
  3360     iDefinitionRepository->Lock();
       
  3361     CleanupStack::PushL( TCleanupItem( UnlockRepository, iDefinitionRepository ) );
       
  3362     
       
  3363     iDefinitionRepository->SetOdtL( aOdt );
       
  3364     // repository is unlocked in UnlockRepository function during PopAndDestroy
       
  3365     CleanupStack::PopAndDestroy();
       
  3366     }
       
  3367 
       
  3368 // -----------------------------------------------------------------------------
       
  3369 // ChspsThemeServer::ActivateThemeL()
       
  3370 // -----------------------------------------------------------------------------
       
  3371 //
       
  3372 TInt ChspsThemeServer::ActivateThemeL( 
       
  3373         const ChspsODT& aSetMask, 
       
  3374         ChspsODT& aOdt )
       
  3375     {
       
  3376     TInt errorCode = GetConfigurationL( 
       
  3377             aSetMask.RootUid(), 
       
  3378             aSetMask.ThemeUid(), 
       
  3379             aOdt );    
       
  3380     if ( aOdt.ConfigurationType() != EhspsAppConfiguration )
       
  3381         {
       
  3382         errorCode = KErrArgument;
       
  3383         }        
       
  3384         
       
  3385     if( !errorCode )
       
  3386         {
       
  3387         const TUint32 KMask = 0xFFFFFFFF;
       
  3388         RArray<TUint32> res;
       
  3389         CleanupClosePushL( res );
       
  3390       
       
  3391         iCentralRepository->FindL( aSetMask.RootUid(), KMask, res );
       
  3392         if ( res.Count() == 0 )
       
  3393             { 
       
  3394             errorCode = iCentralRepository->Create( aSetMask.RootUid(), aSetMask.ThemeUid() );
       
  3395             }
       
  3396         else
       
  3397             {
       
  3398             errorCode = iCentralRepository->Set( aSetMask.RootUid(), aSetMask.ThemeUid() );
       
  3399             }        
       
  3400         if ( !errorCode )                
       
  3401             {
       
  3402             // Request re-evaluation of the new application configuration
       
  3403             hspsServerUtil::UpdateAppConfigurationStateL( 
       
  3404                 aOdt,
       
  3405                 KConfStateConfirmed,
       
  3406                 KConfStateNotConfirmed );
       
  3407                                       
       
  3408             // Store changes to the Plug-in Repository
       
  3409             iDefinitionRepository->SetOdtL( aOdt );                
       
  3410             }
       
  3411 
       
  3412         res.Close();
       
  3413         CleanupStack::PopAndDestroy(); // res
       
  3414         }
       
  3415     
       
  3416     return errorCode;
       
  3417     }
       
  3418 
       
  3419 // -----------------------------------------------------------------------------
       
  3420 // ChspsThemeServer::GetActivateAppConfigurationL()
       
  3421 // -----------------------------------------------------------------------------
       
  3422 //
       
  3423 TInt ChspsThemeServer::GetActivateAppConfigurationL(
       
  3424         const TInt aAppUid, 
       
  3425         ChspsODT& aOdt )
       
  3426     {            
       
  3427     // Get active application configuration for the provided application
       
  3428     TInt configurationUid = -1;
       
  3429     TInt errorCode = iCentralRepository->Get( 
       
  3430             aAppUid, 
       
  3431             configurationUid );
       
  3432     if ( !errorCode )
       
  3433         {
       
  3434         __ASSERT_DEBUG( configurationUid > 0, User::Leave( KErrArgument) );
       
  3435         
       
  3436         // Get the ODT object        
       
  3437         errorCode = GetConfigurationL( 
       
  3438                 aAppUid, 
       
  3439                 configurationUid, 
       
  3440                 aOdt );        
       
  3441         
       
  3442         __ASSERT_DEBUG( aOdt.ConfigurationType() == EhspsAppConfiguration, User::Leave( KErrArgument) );        
       
  3443         }
       
  3444             
       
  3445     return errorCode;        
       
  3446     }
       
  3447 
       
  3448 // -----------------------------------------------------------------------------
       
  3449 // ChspsThemeServer::GetActiveFamilyL()
       
  3450 // -----------------------------------------------------------------------------
       
  3451 //
       
  3452 TUint32 ChspsThemeServer::GetActiveFamilyL( 
       
  3453         const TInt aAppUid )
       
  3454     {
       
  3455     if ( aAppUid < 1 ) 
       
  3456         {
       
  3457         User::Leave( KErrArgument );
       
  3458         }    
       
  3459     TUint32 familyMask( 0 );
       
  3460     ChspsODT* appODT = ChspsODT::NewL();
       
  3461     CleanupStack::PushL( appODT );
       
  3462     if ( !GetActivateAppConfigurationL( aAppUid, *appODT ) )
       
  3463         {
       
  3464         familyMask = appODT->Family();
       
  3465         }
       
  3466     CleanupStack::PopAndDestroy( appODT );
       
  3467     return familyMask;
       
  3468     }
       
  3469 
       
  3470 #if defined(WINSCW) || defined(__WINS__)
       
  3471 void ChspsThemeServer::ActivateRootConfigurationsL()
       
  3472     {
       
  3473     // Get family from the active resolution
       
  3474     const ThspsFamily family = iFamilyListener->GetFamilyType();
       
  3475         
       
  3476     // Try to activate an application configuration which was designed 
       
  3477     // for the active resolution
       
  3478     if ( !HandleFamilyChangeL( family ) )
       
  3479         {
       
  3480         // Try default family or just use the last one installed        
       
  3481         HandleFamilyChangeL( KDefaultFamily );
       
  3482         }
       
  3483     }
       
  3484 
       
  3485 // -----------------------------------------------------------------------------
       
  3486 // ChspsThemeServer::HandleFamilyChangeL()
       
  3487 // -----------------------------------------------------------------------------
       
  3488 //
       
  3489 TBool ChspsThemeServer::HandleFamilyChangeL( 
       
  3490         const ThspsFamily aNewFamily )         
       
  3491     {
       
  3492 #ifdef HSPS_LOG_ACTIVE        
       
  3493     iLogBus->LogText( _L( "ChspsThemeServer::HandleFamilyChangeL(): resolution has changed, notifying clients..." ) );
       
  3494 #endif // HSPS_LOG_ACTIVE                
       
  3495        
       
  3496     __ASSERT_DEBUG( aNewFamily > EhspsFamilyUnknown && aNewFamily <= EhspsFamilyVga_tch, User::Leave( KErrArgument) );
       
  3497     
       
  3498     RArray<ChspsODT*> notificationTargets;
       
  3499     CleanupClosePushL( notificationTargets );
       
  3500     
       
  3501     if ( iDefinitionRepository->Locked() )
       
  3502         {
       
  3503         User::Leave( KErrAccessDenied );
       
  3504         }
       
  3505     // In case of error. repository is unlocked
       
  3506     iDefinitionRepository->Lock();           
       
  3507     CleanupStack::PushL( TCleanupItem( UnlockRepository, iDefinitionRepository ) );
       
  3508                          
       
  3509     // Find first application configurations with the provided family id    
       
  3510     ChspsODT* header = NULL;
       
  3511     ChspsODT* targetHeader = NULL;
       
  3512     for ( TInt headerIndex=0; headerIndex < iHeaderListCache->Count(); headerIndex++ )
       
  3513         {
       
  3514         header = iHeaderListCache->At( headerIndex );
       
  3515         
       
  3516         // It's totally acceptable that all clients haven't got resolution specific widgets,
       
  3517         // in that case the UI should remaing the same - others should reload the application configuration
       
  3518         // when appropriate for them
       
  3519         if ( header->ConfigurationType() == EhspsAppConfiguration )
       
  3520             {
       
  3521                         
       
  3522             // If the bit is set
       
  3523             if ( header->Family() & aNewFamily )                
       
  3524                 {
       
  3525             
       
  3526                 // Skip family change if it's already up to date
       
  3527                 TInt activeConfigurationUid = -1;
       
  3528                 User::LeaveIfError( 
       
  3529                     iCentralRepository->Get( 
       
  3530                         header->RootUid(), 
       
  3531                         activeConfigurationUid )
       
  3532                     );
       
  3533                 
       
  3534                 // Notify each client only once
       
  3535                 TBool alreadyIncluded = EFalse;
       
  3536                 for( TInt targetIndex=0; targetIndex < notificationTargets.Count(); targetIndex++ )
       
  3537                     {
       
  3538                     targetHeader = notificationTargets[ targetIndex ];
       
  3539                     if ( header->RootUid() == targetHeader->RootUid() )
       
  3540                         {
       
  3541                         alreadyIncluded = ETrue;
       
  3542                         break;
       
  3543                         }
       
  3544                     }                                                     
       
  3545                 if ( !alreadyIncluded )
       
  3546                     {                
       
  3547                     notificationTargets.Append( header );
       
  3548                              
       
  3549                     TBool updateRequired = ( activeConfigurationUid != header->ThemeUid() );                        
       
  3550                     if ( updateRequired )
       
  3551                         {                    
       
  3552                         // Activate the resolution specific theme
       
  3553                         ChspsODT* newOdt = ChspsODT::NewL();
       
  3554                         CleanupStack::PushL( newOdt );                
       
  3555                         ActivateThemeL( *header, *newOdt );                                                    
       
  3556                         CleanupStack::PopAndDestroy( newOdt );
       
  3557 #ifdef HSPS_LOG_ACTIVE        
       
  3558                         iLogBus->LogText( 
       
  3559                             _L( "ChspsThemeServer::HandleFamilyChangeL(): application configuration %D has been activated" ),                     
       
  3560                             header->ThemeUid()
       
  3561                             );
       
  3562 #endif // HSPS_LOG_ACTIVE                
       
  3563                         }            
       
  3564                     } // updateRequired    
       
  3565                 
       
  3566                 } // header type
       
  3567             
       
  3568             }
       
  3569         
       
  3570         } // headers loop                    
       
  3571     
       
  3572     iDefinitionRepository->Unlock();
       
  3573     CleanupStack::Pop(iDefinitionRepository);
       
  3574         
       
  3575     // Signal that we found an application configuration for the requested family
       
  3576     TBool activated = ( notificationTargets.Count() > 0 );    
       
  3577     
       
  3578     // Reloading notifications are sent from void ChspsClientRequestHandler::HandleCenRepChangeL()       
       
  3579     notificationTargets.Close();
       
  3580     CleanupStack::PopAndDestroy( &notificationTargets );
       
  3581     
       
  3582 #ifdef HSPS_LOG_ACTIVE        
       
  3583     iLogBus->LogText( _L( "ChspsThemeServer::HandleFamilyChangeL(): notifications sent" ) );
       
  3584 #endif // HSPS_LOG_ACTIVE    
       
  3585     
       
  3586     return activated;
       
  3587     }
       
  3588 
       
  3589 #endif // defined(WINSCW)
       
  3590 
       
  3591 // end of file
       
  3592