homescreenpluginsrv/hspspluginregistry/src/hspsdefinitionrepository.cpp
changeset 0 79c6a41cd166
child 11 bd874ee5e5e2
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:  Service for maintaining contents of the "themes" folder
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <s32file.h>
       
    21 #include <s32mem.h>
       
    22 #include <sysutil.h>
       
    23 
       
    24 #include "hsps_builds_cfg.hrh"
       
    25 
       
    26 #include "hspsthememanagement.h"
       
    27 #include "hspsdefinitionrepository.h"
       
    28 #include "hspsodt.h"
       
    29 
       
    30 #ifdef HSPS_LOG_ACTIVE
       
    31 #include <hspslogbus.h>
       
    32 #endif
       
    33 
       
    34 // CONSTANTS
       
    35 const TInt KDefRepArrayGranularity = 5;
       
    36 
       
    37 _LIT(KImportFolder, "import");
       
    38 _LIT(KThemesFolder, "themes");
       
    39 _LIT(KSourceFolder, "sources");
       
    40 _LIT(KSourceFolderWithSeperators, "\\sources\\");
       
    41 _LIT(KLocaleFolder, "locales");
       
    42 _LIT(KBackupFolder, "backup");
       
    43 
       
    44 
       
    45 _LIT(KDoubleBackSlash,"\\");
       
    46 
       
    47 _LIT(KFolderFormat,"%d"); // folder format for localized resources
       
    48 
       
    49 _LIT(KMatchAll, "*");
       
    50 
       
    51 
       
    52 _LIT(KMatchSkeleton, ".o0000");
       
    53 
       
    54 const TInt KTIntMahspsumbers( 11 );
       
    55 
       
    56 // ============================= LOCAL FUNCTIONS ===============================
       
    57     
       
    58 // ============================ MEMBER FUNCTIONS ===============================
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // ChspsDefinitionRepository::ChspsDefinitionRepository
       
    62 // C++ default constructor can NOT contain any code, that
       
    63 // might leave.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 ChspsDefinitionRepository::ChspsDefinitionRepository()
       
    67     {
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // ChspsDefinitionRepository::ConstructL
       
    72 // Symbian 2nd phase constructor can leave.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 void ChspsDefinitionRepository::ConstructL()
       
    76     {
       
    77 	User::LeaveIfError( iFs.Connect() );
       
    78 	//Create private path if it doesn't exist already
       
    79 	TInt err=iFs.CreatePrivatePath(EDriveC);
       
    80 	if (err!=KErrNone && err!=KErrAlreadyExists)
       
    81 		{
       
    82 		User::Leave(err);	
       
    83 		}
       
    84 	HBufC* path = HBufC::NewLC( KMaxFileName );
       
    85     TPtr pathPtr = path->Des();
       
    86     User::LeaveIfError( iFs.PrivatePath( pathPtr ));     	
       
    87 	AppendDesCIntoPathL( pathPtr, KImportFolder );
       
    88     AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );
       
    89     iFs.MkDir( pathPtr );
       
    90     CleanupStack::PopAndDestroy( path );
       
    91     User::LeaveIfError(iFs.SetSessionToPrivate(EDriveC));
       
    92 	iLicenseDefault = EFalse;
       
    93 	iLockSemaphore = 0;
       
    94 	
       
    95 	iTempFileName1 = KNullDesC;
       
    96 	iTempFileName2 = KNullDesC;
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // ChspsDefinitionRepository::NewL
       
   101 // Two-phased constructor.
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 EXPORT_C ChspsDefinitionRepository* ChspsDefinitionRepository::NewL()
       
   105     {
       
   106     ChspsDefinitionRepository* self = new( ELeave ) ChspsDefinitionRepository;
       
   107     
       
   108     CleanupStack::PushL( self );
       
   109     self->ConstructL();
       
   110     CleanupStack::Pop( self );
       
   111 
       
   112     return self;
       
   113     }
       
   114     
       
   115 // Destructor
       
   116 ChspsDefinitionRepository::~ChspsDefinitionRepository()
       
   117     {
       
   118     iFs.Close();
       
   119     iRepositoryInfoQueue.Reset();
       
   120     iRepositoryInfoQueue.Close();
       
   121     iObservers.Reset();
       
   122     iObservers.Close(); 
       
   123     delete iPath;
       
   124     
       
   125 	iTempFileName1 = KNullDesC;
       
   126 	iTempFileName2 = KNullDesC;    
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // ChspsDefinitionRepository::SetLocaleL
       
   131 // Saves the dtd-file into the repository.
       
   132 // (other items were commented in a header).
       
   133 // -----------------------------------------------------------------------------
       
   134 EXPORT_C TInt ChspsDefinitionRepository::SetLocaleL( ChspsODT& aODT, ChspsResource& aResource )
       
   135     {
       
   136     // Populate a path based on ODT attributes and file extension of the resource file
       
   137     GetPathL( aODT, EResourceDTD );
       
   138     
       
   139     // Results are in iPath, copy resouce file from temp path to a new path in the repository
       
   140     TInt errorCode = CopyFileL( aResource.FileName() );
       
   141     if ( !errorCode )
       
   142     	{
       
   143     	// Store the new path
       
   144     	aResource.SetFileNameL( iPath->Des() );
       
   145     	}
       
   146     return errorCode;
       
   147     }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // ChspsDefinitionRepository::MakeODTPathL
       
   151 // Creates a path for given ODT.
       
   152 // (other items were commented in a header).
       
   153 // ----------------------------------------------------------------------------- 
       
   154 EXPORT_C void ChspsDefinitionRepository::MakeODTPathL( ChspsODT& aODT, ChspsResource& aResource )
       
   155     {
       
   156     // make sure the temporary buffer is empty
       
   157     iTempFileName1.Zero();
       
   158     
       
   159     // get the path for ODT-resource to store
       
   160     GetPathL( aODT, EResourceODT );
       
   161 
       
   162    	TPtr ptrPath = iPath->Des();
       
   163    	TParsePtr p( ptrPath );	
       
   164    	aODT.SetThemeShortNameL(p.Name());
       
   165     	
       
   166    	// iPath is modified
       
   167    	TPtrC pathPtr = iPath->Des();
       
   168    	iTempFileName1.Format( _L("%S%S"), &KCDrive, &pathPtr );
       
   169     aResource.SetFileNameL( iTempFileName1 ); 
       
   170    	    
       
   171     iTempFileName1.Zero(); 	    
       
   172     }
       
   173     
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // ChspsDefinitionRepository::SetOdtL
       
   177 // Saves the odt-structure into the repository.
       
   178 // (other items were commented in a header).
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 EXPORT_C TInt ChspsDefinitionRepository::SetOdtL( const ChspsODT &aODT )
       
   182     {
       
   183     TInt ret = KErrGeneral;
       
   184 
       
   185 #ifdef HSPS_LOG_ACTIVE
       
   186     if( iLogBus )
       
   187         {
       
   188         iLogBus->LogText( _L( "ChspsDefinitionRepository::SetOdtL(): - rewriting the binary file." ) );
       
   189         }
       
   190 #endif
       
   191 
       
   192     GetPathL( aODT, EResourceODT );
       
   193     ret = WriteToFileL( aODT);
       
   194 
       
   195 
       
   196 #ifdef HSPS_LOG_ACTIVE
       
   197     if( iLogBus )
       
   198         {
       
   199         iLogBus->LogText( _L( "ChspsDefinitionRepository::SetOdtL(): - rewriting done. return code: %d." ), ret );
       
   200         }
       
   201 #endif
       
   202 
       
   203     return ret;
       
   204     }
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // ChspsDefinitionRepository::GetOdtL
       
   208 // Retrieves the odt-structure from the repository as a reference.
       
   209 // (other items were commented in a header).
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 EXPORT_C TInt ChspsDefinitionRepository::GetOdtL( ChspsODT& aODT )
       
   213     {
       
   214     TInt errorCode = KErrNone;
       
   215     if ( aODT.Flags() & EhspsThemeStatusLicenceeDefault )
       
   216 		{
       
   217 		iLicenseDefault = ETrue;
       
   218 		}
       
   219 	else
       
   220 		{
       
   221 		iLicenseDefault = EFalse;		
       
   222 		}
       
   223 
       
   224     TRAP( errorCode, GetPathL( aODT, EResourceODT ));
       
   225     if ( !errorCode )
       
   226     	{
       
   227     	errorCode = ReadFromFileL( *iPath, aODT ); 
       
   228     	}
       
   229     iLicenseDefault = EFalse;  
       
   230     return errorCode;
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // ChspsDefinitionRepository::GetOdtHeaderL
       
   235 // Retrieves the odt-structure from the repository as a reference.
       
   236 // (other items were commented in a header).
       
   237 // -----------------------------------------------------------------------------
       
   238 //
       
   239 EXPORT_C TInt ChspsDefinitionRepository::GetOdtHeaderL( TDes& aPath, TLanguage /*aLang*/, ChspsODT& aODT )
       
   240     {    
       
   241     TInt err( KErrNotFound );
       
   242     
       
   243     CDesCArraySeg* themeList = new ( ELeave ) CDesCArraySeg( KDefRepArrayGranularity );
       
   244     CleanupStack::PushL( themeList );
       
   245     
       
   246     SelectFilesFromPathL( *themeList, aPath, ESortByDate, KMatchSkeleton );
       
   247     
       
   248     if( themeList->MdcaCount() > 0 )
       
   249         {
       
   250         
       
   251         TPtrC name = themeList->MdcaPoint( 0 );
       
   252         if ( name.FindF( KSourceFolderWithSeperators ) == KErrNotFound )
       
   253         	{
       
   254 	        HBufC8* header( StreamHeaderFromFileL( name ) );
       
   255 	        CleanupStack::PushL( header );
       
   256 	        
       
   257 	        if( header )
       
   258 	            {
       
   259 	            aODT.UnMarshalHeaderL( *header );            
       
   260 	            err = KErrNone;
       
   261 	            }
       
   262 	            
       
   263 	        CleanupStack::PopAndDestroy( header );            
       
   264         	}
       
   265         }
       
   266     
       
   267     CleanupStack::PopAndDestroy( themeList );
       
   268 
       
   269     User::LeaveIfError( err );
       
   270             
       
   271     return err;
       
   272     }
       
   273     
       
   274 // -----------------------------------------------------------------------------
       
   275 // ChspsDefinitionRepository::GetODTPathL
       
   276 // Retrieves the path of given ODT.
       
   277 // (other items were commented in a header).
       
   278 // ----------------------------------------------------------------------------- 
       
   279 //
       
   280 EXPORT_C void ChspsDefinitionRepository::GetResourcePathL( const ChspsODT& aODT, ThspsResourceType aResourceType, TDes& aFilePath )
       
   281     {
       
   282     // get the path for source resources to store
       
   283     GetPathL( aODT, aResourceType );
       
   284     TPtrC ptr = iPath->Des();
       
   285 	// modifying the resource file path on the resource
       
   286 	aFilePath.Format( _L("%S%S"), &KCDrive, &ptr );
       
   287     }    
       
   288 
       
   289     
       
   290 // -----------------------------------------------------------------------------
       
   291 // ChspsDefinitionRepository::GetThemePathListL
       
   292 // Retrieves the list of installed themes.
       
   293 // (other items were commented in a header).
       
   294 // -----------------------------------------------------------------------------
       
   295 EXPORT_C void ChspsDefinitionRepository::GetThemePathListL( CDesCArraySeg& aThemeList, const ChspsODT& aODT )
       
   296     {    
       
   297     CDesCArraySeg* pathList = new( ELeave ) CDesCArraySeg(KDefRepArrayGranularity);
       
   298     CleanupStack::PushL( pathList );
       
   299     
       
   300     CDesCArraySeg* tempPathList = new( ELeave ) CDesCArraySeg(KDefRepArrayGranularity);
       
   301     CleanupStack::PushL( tempPathList );
       
   302                
       
   303     // The "themes" folder must exist at this point (C drive)
       
   304     _LIT( KDriveC, "C:" );
       
   305     TFileName themesPath;
       
   306     User::LeaveIfError( iFs.PrivatePath( themesPath ));
       
   307     HBufC* path = HBufC::NewLC( KMaxFileName );
       
   308     TPtr pathPtr = path->Des();        
       
   309     AppendDesCIntoPathL( pathPtr, KDriveC );    
       
   310     AppendDesCIntoPathL( pathPtr, themesPath );
       
   311     AppendDesCIntoPathL( pathPtr, KThemesFolder );
       
   312     AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );    
       
   313     
       
   314     // Allocate memory for the folder name to be found (uid folder)
       
   315     HBufC* match = HBufC::NewLC( KTIntMahspsumbers );
       
   316     TPtr matchPtr = match->Des();
       
   317            
       
   318     // Setup a mask from the search criterias     
       
   319     TInt8 rootUid = aODT.RootUid() ? 1 : 0;
       
   320     TInt8 provUid = aODT.ProviderUid() ? 2 : 0;
       
   321     TInt8 themeUid = aODT.ThemeUid() ? 4 : 0;
       
   322     TInt8 mask = rootUid | provUid | themeUid;
       
   323     
       
   324     TInt index( 0 );
       
   325     
       
   326     switch ( mask )
       
   327         {
       
   328         case 0://No data in ODT.
       
   329             {
       
   330             MatchingFoldersFromAllDrivesL( KMatchAll, pathPtr, *pathList );
       
   331             }
       
   332             break;
       
   333         case 1://AppUid given.
       
   334         case 3://AppUid and ProviderUid given.
       
   335         case 7://AppUid, ProviderUid and ThemeUid given.
       
   336             {
       
   337             if ( aODT.RootUid() )
       
   338                 {
       
   339                 AppendNumIntoPathL( pathPtr, aODT.RootUid() );
       
   340                 AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );
       
   341                 }            
       
   342             if ( aODT.ProviderUid() )
       
   343                 {
       
   344                 AppendNumIntoPathL( pathPtr, aODT.ProviderUid() );
       
   345                 AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );
       
   346                 }                
       
   347             if ( aODT.ThemeUid() )
       
   348                 {
       
   349                 AppendNumIntoPathL( pathPtr, aODT.ThemeUid() );
       
   350                 AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );
       
   351                 }
       
   352             MatchingFoldersFromAllDrivesL( KMatchAll, pathPtr, *pathList );
       
   353             }
       
   354             break;
       
   355         case 2://ProviderUid given.
       
   356             {
       
   357             MatchingFoldersFromAllDrivesL( KMatchAll, pathPtr, *tempPathList );
       
   358             matchPtr.Zero();
       
   359             matchPtr.AppendNum( aODT.ProviderUid() );
       
   360             for ( index = 0; index < tempPathList->Count(); index++ )
       
   361                 {
       
   362                 MatchingFoldersL( *match, tempPathList->MdcaPoint( index ), *pathList );
       
   363                 }
       
   364             }
       
   365             break;
       
   366         case 4://ThemeUid given.
       
   367             {
       
   368             MatchingFoldersFromAllDrivesL( KMatchAll, pathPtr, *pathList );
       
   369             for ( index = 0; index < pathList->Count(); index++ )
       
   370                 {
       
   371                 MatchingFoldersL( *match, pathList->MdcaPoint( index ), *tempPathList );
       
   372                 }
       
   373             matchPtr.Zero();
       
   374             matchPtr.AppendNum( aODT.ThemeUid() );
       
   375             pathList->Reset();
       
   376             for ( index = 0; index < tempPathList->Count(); index++ )
       
   377                 {
       
   378                 MatchingFoldersL( *match, tempPathList->MdcaPoint( index ), *pathList );
       
   379                 }
       
   380             }
       
   381             break;
       
   382         case 6://ProviderUid and ThemeUid given.
       
   383             {
       
   384             MatchingFoldersFromAllDrivesL( KMatchAll, pathPtr, *pathList );
       
   385             matchPtr.Zero();
       
   386             matchPtr.AppendNum( aODT.ProviderUid() );
       
   387             for ( index = 0; index < pathList->Count(); index++ )
       
   388                 {
       
   389                 MatchingFoldersL( *match, pathList->MdcaPoint( index ), *tempPathList );
       
   390                 }
       
   391             matchPtr.Zero();
       
   392             matchPtr.AppendNum( aODT.ThemeUid() );
       
   393             pathList->Reset();
       
   394             for ( index = 0; index < tempPathList->Count(); index++ )
       
   395                 {
       
   396                 MatchingFoldersL( *match, tempPathList->MdcaPoint( index ), *pathList );
       
   397                 }
       
   398             }
       
   399             break;
       
   400         case 5://AppUid and ThemeUid given.
       
   401             {
       
   402             // Get paths matching the 1st argument into pathList array
       
   403             matchPtr.Zero();
       
   404             matchPtr.AppendNum( aODT.RootUid() );
       
   405             MatchingFoldersFromAllDrivesL( *match, pathPtr, *pathList );
       
   406             // From previous results, match and store results into tempPathList array
       
   407             matchPtr.Zero();
       
   408             matchPtr = KMatchAll;
       
   409             for ( index = 0; index < pathList->Count(); index++ )
       
   410                 {
       
   411                 MatchingFoldersL( *match, pathList->MdcaPoint( index ), *tempPathList );
       
   412                 }
       
   413             // From previous results, find mathing plugins and store them back to the pathList
       
   414             matchPtr.Zero();
       
   415             matchPtr.AppendNum( aODT.ThemeUid() );
       
   416             pathList->Reset();
       
   417             for ( index = 0; index < tempPathList->Count(); index++ )
       
   418                 {
       
   419                 MatchingFoldersL( *match, tempPathList->MdcaPoint( index ), *pathList );
       
   420                 }
       
   421             }
       
   422             break;
       
   423         default:
       
   424             {
       
   425             pathList->AppendL( pathPtr );
       
   426             }
       
   427             break;
       
   428         }
       
   429 
       
   430     TInt err( KErrNotFound );
       
   431     for ( TInt i = 0; i < pathList->MdcaCount(); i++ )
       
   432         {
       
   433         err = MatchingFoldersL( KMatchAll, pathList->MdcaPoint(i), *pathList );
       
   434         if ( err == KErrNotFound )
       
   435             {
       
   436             TPtrC pathPtr = pathList->MdcaPoint(i);
       
   437            	aThemeList.AppendL( pathPtr );	
       
   438             }
       
   439         }
       
   440     CleanupStack::PopAndDestroy( 4, pathList );//path, match, pathList, tempPathList
       
   441     }
       
   442 
       
   443 // -----------------------------------------------------------------------------
       
   444 // ChspsDefinitionRepository::GetThemeListAsStreamL
       
   445 // Retrieves the headers of installed themes as an array of streams.
       
   446 // (other items were commented in a header).
       
   447 // -----------------------------------------------------------------------------
       
   448 //
       
   449 EXPORT_C void ChspsDefinitionRepository::GetThemeListAsStreamL( CArrayPtrSeg<HBufC8>& aHeaderList, const ChspsODT& aODT )
       
   450     {
       
   451     CDesCArraySeg* pathList = new( ELeave ) CDesCArraySeg(KDefRepArrayGranularity);
       
   452     CleanupStack::PushL( pathList );
       
   453     GetThemePathListL( *pathList, aODT );
       
   454     CDesCArraySeg* themeList = new( ELeave ) CDesCArraySeg(KDefRepArrayGranularity);
       
   455     CleanupStack::PushL( themeList );
       
   456     
       
   457     for ( TInt i = 0; i < pathList->MdcaCount(); i++ )
       
   458         {
       
   459         SelectFilesFromPathL( *themeList,  pathList->MdcaPoint( i ), ESortByDate, KMatchSkeleton );
       
   460         }
       
   461     
       
   462     for ( TInt j = 0; j < themeList->MdcaCount(); j++ )
       
   463         {
       
   464         TPtrC ptr( themeList->MdcaPoint( j ) );
       
   465         if ( ptr.FindF( KSourceFolderWithSeperators ) == KErrNotFound )
       
   466         	{
       
   467 	        HBufC8* temp = StreamHeaderFromFileL( themeList->MdcaPoint( j ) );
       
   468 	        if ( temp )
       
   469 	            {
       
   470 	            aHeaderList.AppendL( temp );         
       
   471 	            }
       
   472         	}
       
   473         }
       
   474     CleanupStack::PopAndDestroy( 2, pathList );//themeList, pathList
       
   475     }
       
   476  
       
   477 // -----------------------------------------------------------------------------
       
   478 // ChspsDefinitionRepository::RemoveThemeL
       
   479 // Removes the specified theme.
       
   480 // (other items were commented in a header).
       
   481 // -----------------------------------------------------------------------------
       
   482 EXPORT_C void ChspsDefinitionRepository::RemoveThemeL( const ChspsODT& aODT )
       
   483     {
       
   484     GetPathL( aODT, EResourceNone );
       
   485     RemoveDirectoryL();
       
   486     // theme removal is not informed to the clients through RepositoryInfoArray 
       
   487     // because this function is used also by the theme server's clean up functions - 
       
   488     // and it not necessary inform about them.
       
   489     // insteads, maintenance handler does inform the removal.
       
   490     }
       
   491 
       
   492 
       
   493 // -----------------------------------------------------------------------------
       
   494 // ChspsDefinitionRepository::MakeResourcePathL
       
   495 // Makes a path in the repository that can be used to store the resource file. 
       
   496 // The path is written back into aResource  
       
   497 // The path depends on the locale under which the resource belongs.
       
   498 // The path information will be used to retrieve the source later.
       
   499 // (other items were commented in a header).
       
   500 // -----------------------------------------------------------------------------    
       
   501 EXPORT_C void ChspsDefinitionRepository::MakeResourcePathL( const ChspsODT& aODT, ChspsResource& aResource )
       
   502 	{
       
   503     // get the path for source resources to store (results are stored into the iPath instance)
       
   504     GetPathL( aODT, aResource.ResourceType() );
       
   505     TPtr pathPtr = iPath->Des();
       
   506     
       
   507     // If the resource is of type "Other" then skip renaming of the file extension
       
   508     if ( aResource.ResourceType() == EResourceOther )
       
   509     	{
       
   510     	if ( aResource.Language() != ELangNone )
       
   511     	    {    	    
       
   512     	    AddLocalePathL( pathPtr, aODT.OdtLanguage() );
       
   513     	    AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );
       
   514     	    }
       
   515 
       
   516     	// Append the iPath with the resource name
       
   517     	AppendDesCIntoPathL( pathPtr, aResource.ResourceId() );
       
   518     	
       
   519     	// Append with original file extension or fix it
       
   520     	const TDesC& name = aResource.FileName();
       
   521     	TParsePtr parsePtr( (TDes&)name );		   				
       
   522         TPtrC fileExtension = parsePtr.Ext();
       
   523         if ( fileExtension.Length() > 2 && fileExtension.Left(2).CompareF( _L(".o") ) == 0 )
       
   524             {            
       
   525             // Strip the first letter            
       
   526             TInt odtIndex(0);                  
       
   527             TLex lex( fileExtension.Mid(3) );
       
   528             if ( lex.Val( odtIndex ) == KErrNone && odtIndex >= 0 )
       
   529                 {                
       
   530                 fileExtension.Set( KMatchSkeleton );
       
   531                 }
       
   532             }		
       
   533 		AppendDesCIntoPathL( pathPtr, fileExtension );    		
       
   534     	}
       
   535     else
       
   536     	{
       
   537 	    // If replaceable resource
       
   538 	    if ( aResource.LockingPolicy() == EhspsUnlocked )
       
   539 	    	{
       
   540 	    	AppendDesCIntoPathL( pathPtr, aResource.ResourceId() );	    		    		    		
       
   541     		FileExtensionL( aResource.ResourceType(), aODT.OdtLanguage() );	    		
       
   542 	    	}
       
   543 	    else
       
   544 	    	{
       
   545 	    	// make phycical path
       
   546 		    User::LeaveIfError(iFs.SetSessionToPrivate(EDriveC));
       
   547 		    // create the directory structure on the path
       
   548 		    TInt err = iFs.MkDirAll( *iPath );
       
   549 		    if ( err != KErrNone && err != KErrAlreadyExists )
       
   550 		        {
       
   551 		        User::Leave( err );
       
   552 		        }
       
   553 		    AppendDesCIntoPathL( pathPtr, aResource.ResourceId() );    
       
   554 	   	    FileExtensionL( aResource.ResourceType(), aODT.OdtLanguage() );
       
   555 	    	}
       
   556     	}
       
   557     aResource.SetFileNameL( iPath->Des() );
       
   558 	}
       
   559 	
       
   560 // -----------------------------------------------------------------------------
       
   561 // ChspsDefinitionRepository::SetResourceListL
       
   562 // Stores the resource list to the repository.
       
   563 // (other items were commented in a header).
       
   564 // -----------------------------------------------------------------------------    
       
   565 EXPORT_C TInt ChspsDefinitionRepository::SetResourceListL( ChspsODT& aODT, const CArrayPtrSeg<ChspsResource>& aResourceList )
       
   566 	{
       
   567 	// make sure the temporary buffers are empty.
       
   568 	iTempFileName1.Zero();
       
   569 	iTempFileName2.Zero();
       
   570     
       
   571     TInt errorCode = KErrNone;
       
   572        
       
   573     TInt tempLanguage = aODT.OdtLanguage();    
       
   574             
       
   575     // Loop provided resources and copy them to correct places in the repository
       
   576     for ( TInt i=0; i<aResourceList.Count(); i++ )
       
   577     	{
       
   578     	ChspsResource* res = aResourceList.At(i);
       
   579     	
       
   580     	aODT.SetOdtLanguage( res->Language() );
       
   581     	
       
   582     	if ( res->ResourceType() == EResourceDTD )
       
   583 			{												
       
   584 			SetLocaleL( aODT, *res );							
       
   585 			}
       
   586     	else
       
   587     		{	    		    	
       
   588 			// save source path because res->FileName() will be modified by MakeResourcePath but 
       
   589 			// the original path is still needed
       
   590 	        HBufC* fileName = HBufC::NewLC( KMaxFileName );
       
   591 	        TPtr fileNamePtr = fileName->Des();
       
   592 	        fileNamePtr.Append( res->FileName() );                                            
       
   593 		    	    
       
   594 		    // strip the extension temporarily    	        	    
       
   595 		    TParsePtr k( fileNamePtr );
       
   596 			TPtrC name = k.Name();
       
   597 		    res->SetResourceIdL(name);
       
   598 		    
       
   599 		    // copy a non-cacheable resource from its location into the repository 
       
   600 	    	MakeResourcePathL( aODT, *res );
       
   601 	        
       
   602 	    	// restore the file extension
       
   603 	        TPtrC nameAndExt = k.NameAndExt();
       
   604 	        res->SetResourceIdL(nameAndExt);
       
   605 	        
       
   606 			// copy the resource file to the repository
       
   607 			errorCode = CopyFileL( *fileName, iPath->Des() );
       
   608 			if ( errorCode )
       
   609 			    {
       
   610 #ifdef HSPS_LOG_ACTIVE
       
   611                 if( iLogBus )
       
   612                     {
       
   613                     iLogBus->LogText( _L( "ChspsDefinitionRepository::SetResourceListL() - resource copying failed" ) );
       
   614                     }
       
   615 #endif
       
   616 			    }
       
   617 			
       
   618 			CleanupStack::PopAndDestroy( fileName );
       
   619 			
       
   620 			
       
   621 			// Set drive information
       
   622 			iTempFileName1.Format( _L("%S"), &res->FileName() );				
       
   623 			TParsePtr f( iTempFileName1 );		
       
   624 			TPtrC path = f.Path();
       
   625 			TPtrC file = f.NameAndExt();			    
       
   626 		    iTempFileName2.Format(_L("%S%S%S"), &KCDrive, &path, &file );
       
   627 	        res->SetFileNameL( iTempFileName2 );			    
       
   628     		}
       
   629     	aODT.AddResourceL( res );
       
   630         }
       
   631 
       
   632     aODT.SetOdtLanguage( tempLanguage );
       
   633 	iTempFileName1.Zero();
       
   634 	iTempFileName2.Zero();
       
   635 
       
   636 	return errorCode;
       
   637 	}
       
   638 
       
   639 // -----------------------------------------------------------------------------
       
   640 // ChspsDefinitionRepository::GetResourceListL
       
   641 // Retrieves the resource list from the repository. ODT-header given is a search mask.
       
   642 // (other items were commented in a header).
       
   643 // -----------------------------------------------------------------------------    
       
   644 EXPORT_C TInt ChspsDefinitionRepository::GetResourceListL( const ChspsODT& aODT, CArrayPtrSeg<ChspsResource>& aResourceList )
       
   645 	{
       
   646 	TInt errorCode = KErrNone;
       
   647 	GetPathL( aODT, EResourceODT );
       
   648 	CFileStore* store = OpenFileStoreLC( *iPath );
       
   649 	
       
   650 	if( !store )
       
   651 		{
       
   652 		errorCode = KErrNotFound;
       
   653 		}
       
   654 	// Construct and open the root stream which contains the representation of our objects.
       
   655 	if( !errorCode )
       
   656 		{	
       
   657 		RStoreReadStream instream;
       
   658 		CleanupClosePushL( instream );
       
   659 		
       
   660 		instream.OpenLC( *store, store->Root() );
       
   661 		
       
   662 		ChspsODT* odt = ChspsODT::NewL();
       
   663 	    CleanupStack::PushL( odt );
       
   664 			  
       
   665 	    odt->InternalizeHeaderL( instream );
       
   666         // consumes header delimiter
       
   667         instream.ReadInt16L();
       
   668         odt->InternalizeResourceListL( instream );    
       
   669 		// destroy the direct file store object (closes the file)
       
   670 		for ( TInt i = 0; i < odt->ResourceCount(); i++ ) 
       
   671 		    {
       
   672 		    aResourceList.AppendL( odt->ResourceL( i ).CloneL());
       
   673 		    }
       
   674 						    
       
   675         CleanupStack::PopAndDestroy( odt );
       
   676         
       
   677    		// Destroy the stream object and close the intstream
       
   678    		CleanupStack::PopAndDestroy( 2, &instream );
       
   679                 
       
   680         // Destroy the direct file store object (closes the file) 		    
       
   681 		CleanupStack::PopAndDestroy( store );
       
   682 		}
       
   683 	
       
   684 	return errorCode;
       
   685 	}
       
   686 	
       
   687 // -----------------------------------------------------------------------------
       
   688 // ChspsDefinitionRepository::GetRepositoryInfo
       
   689 // Returns a refrence to a RArray-object containing changes in the repository system.
       
   690 // It is a responsibilty of the caller to lock the repository for modifications.
       
   691 // (other items were commented in a header).
       
   692 // -----------------------------------------------------------------------------   
       
   693 EXPORT_C void ChspsDefinitionRepository::RegisterNotification( ThspsRepositoryInfo aRepositoryInfo )
       
   694     {
       
   695     iRepositoryInfoQueue.Append( aRepositoryInfo );
       
   696     
       
   697     for ( TInt i=0; i < iRepositoryInfoQueue.Count(); i++ )
       
   698         {
       
   699         // take an info from the queue
       
   700         ThspsRepositoryInfo info = iRepositoryInfoQueue[i];
       
   701         TBool consumed = EFalse;
       
   702         // notify the observers until someone consumes the info
       
   703         for ( TInt j=0; !consumed && j < iObservers.Count(); j++ )
       
   704             {    
       
   705             if ( iObservers[j]->HandleDefinitionRespositoryEvent( info ) )
       
   706                 {
       
   707                 // observer has handled the event, stop this loop
       
   708                 consumed = ETrue;
       
   709                 }    
       
   710             }
       
   711         // delete info from the queue   
       
   712         iRepositoryInfoQueue.Remove( i ); 
       
   713         }
       
   714     iRepositoryInfoQueue.Compress();    
       
   715     }
       
   716 
       
   717 // -----------------------------------------------------------------------------
       
   718 // ChspsDefinitionRepository::RegisterObserverL
       
   719 // Registers an observer. Observers will be notified when there are definition repository 
       
   720 // events available.
       
   721 // (other items were commented in a header).
       
   722 // -----------------------------------------------------------------------------  
       
   723 EXPORT_C void ChspsDefinitionRepository::RegisterObserverL
       
   724                                               ( const MhspsDefinitionRepositoryObserver& aObserver )
       
   725     {
       
   726     iObservers.Append( &aObserver );
       
   727     }
       
   728     
       
   729 // -----------------------------------------------------------------------------
       
   730 // ChspsDefinitionRepository::UnregisterObserver
       
   731 // When an observer stops observation of definition repository events, it must
       
   732 // unregister itself.
       
   733 // (other items were commented in a header).
       
   734 // -----------------------------------------------------------------------------  
       
   735 EXPORT_C void ChspsDefinitionRepository::UnregisterObserver
       
   736                                                 ( const MhspsDefinitionRepositoryObserver& aObserver )
       
   737     {
       
   738     TInt found = iObservers.Find( &aObserver ); 
       
   739     if ( !( found == KErrNotFound ))
       
   740         {
       
   741         iObservers.Remove( found );
       
   742         }
       
   743     }    
       
   744 
       
   745 
       
   746 // -----------------------------------------------------------------------------
       
   747 // ChspsDefinitionRepository::Lock
       
   748 // (other items were commented in a header).
       
   749 // -----------------------------------------------------------------------------    
       
   750 EXPORT_C void ChspsDefinitionRepository::Lock()
       
   751 	{
       
   752     iLockSemaphore++;
       
   753     
       
   754 #ifdef HSPS_LOG_ACTIVE
       
   755     if( iLogBus )
       
   756         {
       
   757         iLogBus->LogText( _L( "ChspsDefinitionRepository::Lock() - semaphore increased to %d" ),
       
   758                 iLockSemaphore );
       
   759         }
       
   760 #endif    
       
   761 	}
       
   762 
       
   763 // -----------------------------------------------------------------------------
       
   764 // ChspsDefinitionRepository::Unlock
       
   765 // (other items were commented in a header).
       
   766 // -----------------------------------------------------------------------------    
       
   767 EXPORT_C void ChspsDefinitionRepository::Unlock()
       
   768 	{
       
   769 	if (iLockSemaphore )
       
   770 		{
       
   771 		iLockSemaphore--;
       
   772 		
       
   773 #ifdef HSPS_LOG_ACTIVE
       
   774         if( iLogBus )
       
   775             {
       
   776             iLogBus->LogText( _L( "ChspsDefinitionRepository::Unlock() - semaphore decreased to %d" ),
       
   777                     iLockSemaphore );
       
   778             }
       
   779 #endif
       
   780 		}
       
   781 	}
       
   782 
       
   783 	
       
   784 // -----------------------------------------------------------------------------
       
   785 // ChspsDefinitionRepository::Locked
       
   786 // (other items were commented in a header).
       
   787 // -----------------------------------------------------------------------------    
       
   788 EXPORT_C TBool ChspsDefinitionRepository::Locked() const
       
   789 	{
       
   790     return iLockSemaphore;
       
   791 	}
       
   792 
       
   793 // -----------------------------------------------------------------------------
       
   794 // ChspsDefinitionRepository::GetODTPathLC
       
   795 // Creates a path from the theme header information.
       
   796 // (other items were commented in a header).
       
   797 // -----------------------------------------------------------------------------
       
   798 EXPORT_C void ChspsDefinitionRepository::GetODTPathL( const ChspsODT& aODT, TDes& aPath )
       
   799 	{
       
   800 	GetPathL( aODT, EResourceODT );
       
   801 	if ( iPath )
       
   802 		{
       
   803 		aPath.Copy( iPath->Des() );
       
   804 		}
       
   805 	else
       
   806 		User::Leave( KErrNotFound );
       
   807 	}
       
   808 
       
   809 // -----------------------------------------------------------------------------
       
   810 // ChspsDefinitionRepository::SetLogBus
       
   811 // Set log bus.
       
   812 // -----------------------------------------------------------------------------
       
   813 #ifdef HSPS_LOG_ACTIVE
       
   814 EXPORT_C void ChspsDefinitionRepository::SetLogBus( void* aLogBus )
       
   815     {
       
   816     iLogBus = (ChspsLogBus*)aLogBus;
       
   817     }
       
   818 #else
       
   819 EXPORT_C void ChspsDefinitionRepository::SetLogBus( void* /*aLogBus*/ )
       
   820     {
       
   821     }
       
   822 #endif
       
   823 
       
   824 // -----------------------------------------------------------------------------
       
   825 // ChspsDefinitionRepository::GetPathL
       
   826 // Creates a path from the theme header information.
       
   827 // (other items were commented in a header).
       
   828 // -----------------------------------------------------------------------------
       
   829 void ChspsDefinitionRepository::GetPathL( const ChspsODT& aODT, ThspsResourceType aResourceType )
       
   830     {
       
   831     delete iPath;
       
   832     iPath = NULL;
       
   833     iPath = HBufC::NewL( KMaxFileName );
       
   834     TPtr pathPtr = iPath->Des();
       
   835     
       
   836     TInt rootUid( aODT.RootUid() );
       
   837     TInt providerUid( aODT.ProviderUid() );
       
   838     TInt themeUid( aODT.ThemeUid() );
       
   839     
       
   840     const TDesC& providerName = aODT.ProviderName();
       
   841     const TDesC& themeShortName = aODT.ThemeShortName();
       
   842     const TDesC& fileVersion = aODT.ThemeVersion();
       
   843     
       
   844 	User::LeaveIfError( iFs.PrivatePath( pathPtr ));
       
   845 	
       
   846 	AppendDesCIntoPathL( pathPtr, KThemesFolder );
       
   847     AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );
       
   848     
       
   849     if ( rootUid )
       
   850         {
       
   851         // Append root uid
       
   852         AppendNumIntoPathL( pathPtr, rootUid );
       
   853         AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );
       
   854         }
       
   855     if ( providerUid )
       
   856         {
       
   857         // Append provider uid
       
   858         AppendNumIntoPathL( pathPtr, providerUid );
       
   859         AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );
       
   860         }
       
   861     if ( themeUid )
       
   862         {
       
   863         // Append configuration uid
       
   864         AppendNumIntoPathL( pathPtr, themeUid );
       
   865         AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );
       
   866         } 
       
   867     if ( fileVersion.Length() )
       
   868         {
       
   869         // Append version
       
   870         AppendDesCIntoPathL( pathPtr, fileVersion );
       
   871         AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );
       
   872         }
       
   873    
       
   874     // path for source files
       
   875     if ( aResourceType > EResourceODT )
       
   876         {        
       
   877         if ( aResourceType != EResourceXML )                
       
   878         	{
       
   879         	if ( aResourceType == EResourceDTD )
       
   880                 {
       
   881                 // <ver> locales <language>                
       
   882                 AppendDesCIntoPathL( pathPtr, KLocaleFolder );
       
   883                 AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );
       
   884                 AddLocalePathL( pathPtr, aODT.OdtLanguage() );                                        
       
   885                 }
       
   886         	else
       
   887         	    {	                  
       
   888                 // <ver> sources
       
   889                 AppendDesCIntoPathL( pathPtr, KSourceFolder );                   
       
   890                 }
       
   891         	AppendDesCIntoPathL( pathPtr, KDoubleBackSlash );	        	                	
       
   892         	}
       
   893         
       
   894         }
       
   895         
       
   896     // resource source files do not need full path and filename
       
   897     // because they must be stored as is 
       
   898     if ( aResourceType > EResourceNone && aResourceType < EResourceRES ) 
       
   899     	{
       
   900         if ( themeShortName.Length() )
       
   901             {
       
   902             // Append configuration name
       
   903             AppendDesCIntoPathL( pathPtr, themeShortName );
       
   904             }
       
   905         else if ( themeUid )
       
   906             {
       
   907             AppendNumIntoPathL( pathPtr, themeUid );
       
   908             }
       
   909 
       
   910         FileExtensionL( aResourceType, aODT.OdtLanguage() );
       
   911     	}
       
   912     }
       
   913    
       
   914 // -----------------------------------------------------------------------------
       
   915 // ChspsDefinitionRepository::AppendDesCIntoPathL
       
   916 // Appends a descriptor into path. Leaves if fails.
       
   917 // (other items were commented in a header).
       
   918 // -----------------------------------------------------------------------------
       
   919 void ChspsDefinitionRepository::AppendDesCIntoPathL( TDes& aPath, const TDesC& aText )
       
   920     {
       
   921     if ( ( KMaxFileName - aPath.Length() ) >=  aText.Length() )
       
   922         {
       
   923         aPath.Append( aText );
       
   924         }
       
   925     else
       
   926         {
       
   927         User::Leave( KErrOverflow );
       
   928         }
       
   929     }
       
   930 
       
   931 // -----------------------------------------------------------------------------
       
   932 // ChspsDefinitionRepository::AppendNumIntoPathL
       
   933 // Appends a number into path. Leaves if fails.
       
   934 // (other items were commented in a header).
       
   935 // -----------------------------------------------------------------------------
       
   936 void ChspsDefinitionRepository::AppendNumIntoPathL( TDes& aPath, const TUint aNum )
       
   937     {
       
   938     TInt len = 8;
       
   939     if ( ( KMaxFileName - aPath.Length() ) >=  len )
       
   940         {
       
   941         aPath.AppendNum( aNum );
       
   942         }
       
   943     else
       
   944         {
       
   945         User::Leave( KErrOverflow );
       
   946         }
       
   947     }
       
   948 
       
   949 // -----------------------------------------------------------------------------
       
   950 // ChspsDefinitionRepository::FileExtensionL
       
   951 // Returns a file extension for the given TDefRepFileExtension.
       
   952 // (other items were commented in a header).
       
   953 // -----------------------------------------------------------------------------
       
   954 void ChspsDefinitionRepository::FileExtensionL( ThspsResourceType aResourceType,
       
   955                                                                 TInt /*aLanguage*/ )
       
   956     {
       
   957     TPtr pathPtr = iPath->Des();
       
   958     switch ( aResourceType )
       
   959         {
       
   960         case EResourceNone:
       
   961             {
       
   962             }
       
   963             break;
       
   964         case EResourceODT:
       
   965             {
       
   966             TBuf<6> extension( KODTFileExtension );
       
   967             AppendDesCIntoPathL( pathPtr, extension );
       
   968             }
       
   969             break;
       
   970         case EResourceDTD:
       
   971             {
       
   972             AppendDesCIntoPathL( pathPtr, KDTDFileExtension );
       
   973             }
       
   974             break;
       
   975         case EResourceXML:
       
   976             {
       
   977             AppendDesCIntoPathL( pathPtr, KXMLFileExtension );
       
   978             }
       
   979             break;        
       
   980         case EResourceDAT:
       
   981             {
       
   982             AppendDesCIntoPathL( pathPtr, KDATFileExtension );
       
   983             }
       
   984             break;
       
   985         default:
       
   986             {
       
   987             User::Leave( KErrArgument );
       
   988             }
       
   989             break;
       
   990         };
       
   991     }
       
   992     
       
   993 // -----------------------------------------------------------------------------
       
   994 // ChspsDefinitionRepository::PathExtensionL
       
   995 // Returns a file extension for the given TDefRepFileExtension.
       
   996 // (other items were commented in a header).
       
   997 // -----------------------------------------------------------------------------
       
   998 void ChspsDefinitionRepository::AddLocalePathL( TDes& aPath, TInt aLanguage )
       
   999     {        
       
  1000     TBuf<6> extension;
       
  1001     extension.AppendFormat( KFolderFormat, aLanguage );
       
  1002     AppendDesCIntoPathL( aPath, extension );   
       
  1003     }    
       
  1004 
       
  1005 // -----------------------------------------------------------------------------
       
  1006 // ChspsDefinitionRepository::WriteToFileL
       
  1007 // Writes the ODT structure into file.
       
  1008 // (other items were commented in a header).
       
  1009 // -----------------------------------------------------------------------------
       
  1010 TInt ChspsDefinitionRepository::WriteToFileL( const ChspsODT& aODT )
       
  1011     {
       
  1012     TInt errorCode = KErrNone;
       
  1013     errorCode = iFs.SetSessionToPrivate( EDriveC );
       
  1014     if ( SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, 0, EDriveC ) )
       
  1015         {
       
  1016         errorCode = KErrDiskFull;
       
  1017 
       
  1018 #ifdef HSPS_LOG_ACTIVE
       
  1019         if( iLogBus )
       
  1020             {
       
  1021             iLogBus->LogText( _L( "ChspsDefinitionRepository::WriteToFileL(): - disk full." ) );
       
  1022             }
       
  1023 #endif        
       
  1024         }
       
  1025         
       
  1026     if ( !errorCode )
       
  1027    	    {
       
  1028         // Create the directory structure
       
  1029 	    TInt err = iFs.MkDirAll( *iPath );
       
  1030 	    if ( err != KErrNone && err != KErrAlreadyExists )
       
  1031 	        {
       
  1032 	        errorCode = err;
       
  1033 	        
       
  1034 #ifdef HSPS_LOG_ACTIVE
       
  1035             if( iLogBus )
       
  1036                 {
       
  1037                 iLogBus->LogText( _L( "ChspsDefinitionRepository::WriteToFileL(): - error %d." ),
       
  1038                         err );
       
  1039                 }
       
  1040 #endif	        
       
  1041 	        }
       
  1042 	   	       
       
  1043 	    if ( !errorCode )    
       
  1044 		    {
       
  1045             // Create (replace) the direct file store
       
  1046 	        CFileStore* store = CDirectFileStore::ReplaceLC( iFs, *iPath, EFileWrite);
       
  1047 
       
  1048 	        // Must say what kind of file store.
       
  1049 	        store->SetTypeL( KDirectFileStoreLayoutUid );
       
  1050     	
       
  1051 	        // Construct the output stream.
       
  1052 	        RStoreWriteStream outstream;
       
  1053 	        CleanupClosePushL( outstream );
       
  1054 	        
       
  1055 	        TStreamId id = outstream.CreateLC(*store);
       
  1056     
       
  1057 	        // Stream out the ChspsODT
       
  1058 	        outstream << aODT;
       
  1059 	        outstream.CommitL();
       
  1060 	        	        
       
  1061 	        // Destroy the stream object and close the outstream
       
  1062             CleanupStack::PopAndDestroy( 2, &outstream );
       
  1063 
       
  1064 	        // Set this stream id as the root
       
  1065 	        store->SetRootL(id);
       
  1066 	        store->CommitL();
       
  1067 
       
  1068 	        // Destroy the direct file store object (closes the file),
       
  1069 	        CleanupStack::PopAndDestroy( store );
       
  1070 		    }
       
  1071    	    }
       
  1072 	return errorCode;
       
  1073     }
       
  1074    
       
  1075 // -----------------------------------------------------------------------------
       
  1076 // ChspsDefinitionRepository::ReadFromFileL
       
  1077 // Reads the ODT structure from the file.
       
  1078 // (other items were commented in a header).
       
  1079 // -----------------------------------------------------------------------------
       
  1080 TInt ChspsDefinitionRepository::ReadFromFileL( const TDesC& aPath, ChspsODT& aODT )
       
  1081     {
       
  1082 	TInt errorCode = KErrNone;
       
  1083 	CFileStore* store = OpenFileStoreLC( aPath );
       
  1084 	if ( !store )
       
  1085 		{
       
  1086 		errorCode = KErrNotFound;
       
  1087 		}
       
  1088 	// Construct and open the root stream which 
       
  1089 	// contains the representation of our objects.
       
  1090 	if ( !errorCode )
       
  1091 		{
       
  1092 		RStoreReadStream instream;
       
  1093 		CleanupClosePushL( instream );
       
  1094 		
       
  1095 		instream.OpenLC(*store,store->Root());
       
  1096 		
       
  1097 		// Stream in the ODT
       
  1098     	instream >> aODT;
       
  1099 
       
  1100         // Destroy the stream object and close the instream
       
  1101         CleanupStack::PopAndDestroy( 2, &instream );
       
  1102 
       
  1103         // Destroy the direct file store object (closes the file),
       
  1104         CleanupStack::PopAndDestroy( store );
       
  1105 		}
       
  1106 		
       
  1107 	return errorCode;
       
  1108     }
       
  1109               
       
  1110 // -----------------------------------------------------------------------------
       
  1111 // ChspsDefinitionRepository::StreamHeaderFromFileL
       
  1112 // Streams the ODT header from the file into descriptor.
       
  1113 // (other items were commented in a header).
       
  1114 // -----------------------------------------------------------------------------    
       
  1115 HBufC8* ChspsDefinitionRepository::StreamHeaderFromFileL( const TDesC& aPath )
       
  1116     {
       
  1117     HBufC8* dataStream( NULL );
       
  1118     CFileStore* store( NULL );
       
  1119     
       
  1120     store = OpenFileStoreLC( aPath );
       
  1121 	
       
  1122 	if( store )
       
  1123 		{
       
  1124     	// Construct and open the root stream, which 
       
  1125 		// contains the representation of our objects.
       
  1126 		RStoreReadStream instream;
       
  1127 		CleanupClosePushL( instream );
       
  1128 		
       
  1129 		instream.OpenLC( *store, store->Root() );
       
  1130     
       
  1131     	ChspsODT* odt = ChspsODT::NewL();
       
  1132     	CleanupStack::PushL( odt );
       
  1133 
       
  1134     	odt->InternalizeHeaderL( instream );   	
       
  1135     	
       
  1136 	
       
  1137     	dataStream = odt->MarshalHeaderL();
       
  1138 
       
  1139     	CleanupStack::PopAndDestroy( odt );
       
  1140     	
       
  1141    		// Destroy the stream object and close the instream
       
  1142    		CleanupStack::PopAndDestroy( 2, &instream );
       
  1143    		
       
  1144    		// Destroy the direct file store object (closes the file) 
       
  1145 		CleanupStack::PopAndDestroy( store ); 
       
  1146 		}
       
  1147 		
       
  1148     return dataStream;
       
  1149     }
       
  1150     
       
  1151 // -----------------------------------------------------------------------------
       
  1152 // ChspsDefinitionRepository::OpenFileStoreLC
       
  1153 // Searches and opens the CFileStore from the any drive.
       
  1154 // (other items were commented in a header).
       
  1155 // -----------------------------------------------------------------------------    
       
  1156 CFileStore* ChspsDefinitionRepository::OpenFileStoreLC( const TDesC& aPath )
       
  1157     {
       
  1158     RFile file;        
       
  1159     TDriveList drivelist;
       
  1160     CFileStore* store = NULL;
       
  1161     
       
  1162     TInt err( KErrNotFound );
       
  1163 	iFs.SetSessionToPrivate( EDriveC );
       
  1164 	err = file.Open(iFs, aPath, EFileShareReadersOnly );
       
  1165 	if ( !err )
       
  1166 	    {
       
  1167         // if the file is empty CDirectFileStore::FromLC leaves.
       
  1168 	    TInt size(0);
       
  1169 	    file.Size(size);
       
  1170 	    if(size)
       
  1171 	        {
       
  1172 	        // Oownership of the file passes to the store
       
  1173     	    store = CDirectFileStore::FromLC( file );    	    
       
  1174 	        }
       
  1175 	    }
       
  1176     
       
  1177     if( err || !store )    
       
  1178         {
       
  1179         // Just for sanity
       
  1180         file.Close();
       
  1181         }
       
  1182         
       
  1183 	iLicenseDefault = EFalse;
       
  1184 	
       
  1185 	return store;
       
  1186     }
       
  1187     
       
  1188     
       
  1189 // -----------------------------------------------------------------------------
       
  1190 // ChspsDefinitionRepository::SelectFilesFromPathL
       
  1191 // Retrieves all the files from the given path.
       
  1192 // (other items were commented in a header).
       
  1193 // -----------------------------------------------------------------------------
       
  1194 void ChspsDefinitionRepository::SelectFilesFromPathL( CDesCArraySeg& aFileList, const TDesC& aPath, 
       
  1195                                                   TEntryKey aSortFlag, const TDesC& aFileExtension )
       
  1196     {
       
  1197     CDir* dirList;
       
  1198     User::LeaveIfError( iFs.GetDir( aPath, KEntryAttMaskSupported, aSortFlag, dirList ));
       
  1199     for ( TInt i = 0; i < dirList->Count(); i++)
       
  1200         {
       
  1201         if ( !(*dirList)[i].IsDir() )
       
  1202             {
       
  1203             HBufC* path = HBufC::NewLC( aPath.Length() + (*dirList)[i].iName.Length() );
       
  1204             TPtr pathPtr = path->Des();
       
  1205             AppendDesCIntoPathL( pathPtr, aPath );
       
  1206             AppendDesCIntoPathL( pathPtr, (*dirList)[i].iName );
       
  1207             TParse p;
       
  1208             p.Set(pathPtr,NULL,NULL);
       
  1209             
       
  1210             // If the path is not pointing to the sources folder (e.g. is not a resource file)
       
  1211             if ( pathPtr.FindF( KSourceFolderWithSeperators ) == KErrNotFound )
       
  1212             	{       
       
  1213             	// If requested and parsed file extensions match            	
       
  1214                 if ( !p.Ext().CompareF( aFileExtension ) )
       
  1215             	    {
       
  1216                     aFileList.AppendL( pathPtr );
       
  1217              	    }	                
       
  1218             	}
       
  1219             CleanupStack::PopAndDestroy( path );  
       
  1220             }
       
  1221         }
       
  1222     delete dirList;
       
  1223     }
       
  1224     
       
  1225 
       
  1226 // -----------------------------------------------------------------------------
       
  1227 // ChspsDefinitionRepository::CopyFileL
       
  1228 // Copies a file from the given location into path,
       
  1229 // which is stored in the iPath member variable.
       
  1230 // (other items were commented in a header).
       
  1231 // -----------------------------------------------------------------------------
       
  1232 TInt ChspsDefinitionRepository::CopyFileL( const TDesC& aSourceFile, const TDesC& aDestinationFile )
       
  1233     {
       
  1234     TInt errorCode = KErrNone;
       
  1235     iFs.SetSessionToPrivate( EDriveC );
       
  1236     // create the directory structure if one does not exist
       
  1237 	TInt err = iFs.MkDirAll( aDestinationFile );
       
  1238 	if ( err != KErrNone && err != KErrAlreadyExists )
       
  1239 	    {
       
  1240 	    errorCode = err;
       
  1241 	    }
       
  1242 	if ( !errorCode )
       
  1243 	    {
       
  1244 
       
  1245 	    TEntry entry;
       
  1246         errorCode = iFs.Entry( aSourceFile, entry );
       
  1247         if ( !errorCode && SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, entry.iSize, EDriveC ) )
       
  1248             {
       
  1249             errorCode = KErrDiskFull;
       
  1250             }
       
  1251     
       
  1252         if ( !errorCode )
       
  1253     	    {
       
  1254     	    CFileMan* fileMan = CFileMan::NewL( iFs );
       
  1255     	    CleanupStack::PushL( fileMan );
       
  1256     	    errorCode = fileMan->Copy( aSourceFile, aDestinationFile, ( CFileMan::ERecurse | CFileMan::EOverWrite ) );
       
  1257     	    errorCode = fileMan->Attribs( *iPath, KEntryAttNormal,
       
  1258                                                     KEntryAttReadOnly | KEntryAttHidden, TTime(0) );
       
  1259     	    CleanupStack::PopAndDestroy( fileMan );
       
  1260         	}
       
  1261 	    }
       
  1262     return errorCode;
       
  1263     }
       
  1264 
       
  1265 // -----------------------------------------------------------------------------
       
  1266 // ChspsDefinitionRepository::MatchingFoldersFromAllDrivesL
       
  1267 // Retrieves all the matching folders from the given path from every drive.
       
  1268 // (other items were commented in a header).
       
  1269 // -----------------------------------------------------------------------------
       
  1270 void ChspsDefinitionRepository::MatchingFoldersFromAllDrivesL( const TDesC& aWildName,
       
  1271                               const TDesC& aScanDir, CDesCArraySeg& aPathList )
       
  1272     {
       
  1273     TFindFile fileFinder( iFs );
       
  1274     CDir* fileList;
       
  1275     
       
  1276     // Exclude external and ROM drives from the following search
       
  1277     fileFinder.SetFindMask( KDriveAttExclude | KDriveAttRemovable |KDriveAttRemote |KDriveAttRom ); 
       
  1278            
       
  1279     // Searches, using wildcards, for one or more files/directories in a specified directory.
       
  1280     // If no matching file is found in that directory, all available drives are searched in  
       
  1281     // descending alphabetical order, from Y: to A:, and ending with the Z: drive.
       
  1282     // The search ends when one or more matching filenames are found, or when every available  
       
  1283     // drive has been unsuccessfully searched. 
       
  1284     // To begin searching again after a successful match has been made, use FindWild().    
       
  1285     TInt err = fileFinder.FindWildByDir( aWildName, aScanDir, fileList );
       
  1286     while ( err == KErrNone )
       
  1287         {
       
  1288         TInt i;
       
  1289         for ( i = 0; i < fileList->Count(); i++ )
       
  1290             {
       
  1291             TPtrC name( (*fileList)[i].iName );
       
  1292             if( (*fileList)[i].IsDir() 
       
  1293                     && KSourceFolder().CompareF( name ) != 0
       
  1294                     && KLocaleFolder().CompareF( name ) != 0 )
       
  1295                 {
       
  1296                 TParse fullentry;
       
  1297                 fullentry.Set( name, &fileFinder.File(), NULL);
       
  1298 
       
  1299                 HBufC* path = HBufC::NewLC( fullentry.FullName().Length() + KDoubleBackSlash().Length() );
       
  1300                 TPtr pathPtr = path->Des();
       
  1301                 pathPtr.Append( fullentry.FullName() );                
       
  1302                 pathPtr.Append( KDoubleBackSlash );
       
  1303                 aPathList.AppendL( pathPtr );
       
  1304                 CleanupStack::PopAndDestroy( path );
       
  1305                 }
       
  1306             }//for
       
  1307         delete fileList;
       
  1308         //Go trough all the drives a->z
       
  1309         err = fileFinder.FindWild( fileList );
       
  1310         }//while
       
  1311     }
       
  1312 
       
  1313 // -----------------------------------------------------------------------------
       
  1314 // ChspsDefinitionRepository::MatchingFoldersL
       
  1315 // Retrieves all the matching folders from the given path and drive.
       
  1316 // (other items were commented in a header).
       
  1317 // -----------------------------------------------------------------------------
       
  1318 TInt ChspsDefinitionRepository::MatchingFoldersL( const TDesC& aWildName,
       
  1319                               const TDesC& aScanDir, CDesCArraySeg& aPathList )
       
  1320     {
       
  1321     TInt foldersExists( KErrCancel );
       
  1322     TFindFile fileFinder( iFs );
       
  1323     CDir* fileList;
       
  1324     
       
  1325     TInt err = fileFinder.FindWildByDir( aWildName, aScanDir, fileList );
       
  1326     if ( err == KErrNone )
       
  1327         {
       
  1328         TInt i;
       
  1329         for ( i = 0; i < fileList->Count(); i++ )
       
  1330             {
       
  1331             TPtrC name( (*fileList)[i].iName );
       
  1332             if( (*fileList)[i].IsDir() 
       
  1333                     && KSourceFolder().CompareF( name ) != 0
       
  1334                     && KLocaleFolder().CompareF( name ) != 0 )
       
  1335                 {
       
  1336                 foldersExists = KErrNone;
       
  1337                 TParse fullentry;
       
  1338                 fullentry.Set( name, &fileFinder.File(), NULL);
       
  1339 
       
  1340                 HBufC* path = HBufC::NewLC( fullentry.FullName().Length() + KDoubleBackSlash().Length() );
       
  1341                 TPtr pathPtr = path->Des();
       
  1342                 pathPtr.Append( fullentry.FullName() );                
       
  1343                 pathPtr.Append( KDoubleBackSlash );
       
  1344                 aPathList.AppendL( pathPtr );
       
  1345                 CleanupStack::PopAndDestroy( path );
       
  1346                 }
       
  1347             else
       
  1348                 {
       
  1349                 foldersExists = KErrNotFound;
       
  1350                 }
       
  1351             }//for
       
  1352         delete fileList;
       
  1353         }//if
       
  1354         
       
  1355     return foldersExists;
       
  1356     }
       
  1357 
       
  1358 // -----------------------------------------------------------------------------
       
  1359 // ChspsDefinitionRepository::CopyFileL
       
  1360 // Copies a file from the given location into path,
       
  1361 // which is stored in the iPath member variable.
       
  1362 // (other items were commented in a header).
       
  1363 // -----------------------------------------------------------------------------
       
  1364 TInt ChspsDefinitionRepository::CopyFileL( const TDesC& aSourceFile )
       
  1365     {
       
  1366     TInt errorCode = KErrNone;
       
  1367     iFs.SetSessionToPrivate( EDriveC );
       
  1368     // create the directory structure if one does not exist
       
  1369 	TInt err = iFs.MkDirAll( *iPath );
       
  1370 	if ( err != KErrNone && err != KErrAlreadyExists )
       
  1371 	    {
       
  1372 	    errorCode = err;
       
  1373 	    }
       
  1374 	if ( !errorCode )
       
  1375 		{
       
  1376 		TEntry entry;
       
  1377     	errorCode = iFs.Entry( aSourceFile, entry );
       
  1378     	if ( !errorCode && SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, entry.iSize, EDriveC ) )
       
  1379         	{
       
  1380         	errorCode = KErrDiskFull;
       
  1381         	}
       
  1382         if ( !errorCode )       
       
  1383         	{
       
  1384     		CFileMan* fileMan = CFileMan::NewL( iFs );
       
  1385     		CleanupStack::PushL( fileMan );
       
  1386     		errorCode = fileMan->Copy( aSourceFile, *iPath, CFileMan::EOverWrite );
       
  1387     		errorCode = fileMan->Attribs( *iPath, KEntryAttNormal,
       
  1388                         KEntryAttReadOnly | KEntryAttHidden, TTime(0) );
       
  1389     		CleanupStack::PopAndDestroy( fileMan );
       
  1390         	}
       
  1391 		}
       
  1392 	return errorCode;
       
  1393     }
       
  1394     
       
  1395 // -----------------------------------------------------------------------------
       
  1396 // ChspsDefinitionRepository::RemoveDirectoryL
       
  1397 // Deletes the files and folders beneath the path,
       
  1398 // which is stored in the iPath member variable.
       
  1399 // (other items were commented in a header).
       
  1400 // -----------------------------------------------------------------------------
       
  1401 void ChspsDefinitionRepository::RemoveDirectoryL()
       
  1402     {
       
  1403     TInt errorCode = KErrNone;
       
  1404     iFs.SetSessionToPrivate( EDriveC );
       
  1405     CFileMan* fileMan = CFileMan::NewL( iFs );
       
  1406     CleanupStack::PushL( fileMan );
       
  1407     TPtr pathPtr = iPath->Des();
       
  1408     TInt err = fileMan->RmDir( pathPtr );
       
  1409     if ( err != KErrNone && err != KErrPathNotFound )
       
  1410         {
       
  1411         errorCode = err; // probably KErrInUse
       
  1412         }
       
  1413     else
       
  1414     	{
       
  1415      	//If the folder is empty -> remove it
       
  1416     	TParsePtr path( pathPtr );
       
  1417     	TInt flag( ETrue );
       
  1418     	do
       
  1419         	{
       
  1420         	TInt err = path.PopDir();
       
  1421         	if ( !err )
       
  1422         	    {
       
  1423         	    CDir* dirList;
       
  1424         	    err = iFs.GetDir( path.Path(), KEntryAttMaskSupported,ESortByName, dirList );
       
  1425             	if ( !err && dirList->Count() == 0 )
       
  1426             	    {
       
  1427             	    err = fileMan->RmDir( path.Path() );
       
  1428             	    }
       
  1429         	    else
       
  1430             	    {
       
  1431             	    flag = EFalse;
       
  1432             	    }
       
  1433         	    delete dirList;
       
  1434         	    }
       
  1435         	}
       
  1436      	while ( flag );
       
  1437     	}
       
  1438     CleanupStack::PopAndDestroy( fileMan );
       
  1439     User::LeaveIfError( errorCode );
       
  1440     }
       
  1441 
       
  1442     
       
  1443 // -----------------------------------------------------------------------------
       
  1444 // ChspsDefinitionRepository::RestoreBackupConfiguration
       
  1445 // Restores defined configuration from backup folder
       
  1446 // (other items were commented in a header).
       
  1447 // -----------------------------------------------------------------------------
       
  1448 EXPORT_C TInt ChspsDefinitionRepository::RestoreBackupConfiguration(
       
  1449     const ChspsODT& /*aODT*/ )
       
  1450     {
       
  1451     TInt err( KErrNotFound );
       
  1452     
       
  1453     return err;
       
  1454     }
       
  1455 
       
  1456 // -----------------------------------------------------------------------------
       
  1457 // ChspsDefinitionRepository::BackupConfigurationL
       
  1458 // Copies defined configuration to theme backup folder
       
  1459 // (other items were commented in a header).
       
  1460 // -----------------------------------------------------------------------------
       
  1461 EXPORT_C void ChspsDefinitionRepository::BackupConfigurationL(
       
  1462     const ChspsODT& aODT )
       
  1463     {
       
  1464     HBufC* backupPath = HBufC::NewL( KMaxFileName );
       
  1465     CleanupStack::PushL( backupPath );
       
  1466     TPtr backupPathPtr = backupPath->Des();
       
  1467     
       
  1468     User::LeaveIfError( iFs.PrivatePath( backupPathPtr ));
       
  1469 
       
  1470     AppendDesCIntoPathL( backupPathPtr, KBackupFolder );
       
  1471     AppendDesCIntoPathL( backupPathPtr, KDoubleBackSlash );
       
  1472     AppendDesCIntoPathL( backupPathPtr, KThemesFolder );
       
  1473     AppendDesCIntoPathL( backupPathPtr, KDoubleBackSlash );
       
  1474 
       
  1475     // Backup configuration file
       
  1476     GetPathL( aODT, EResourceODT );
       
  1477     CopyFileL( iPath->Des(), backupPathPtr );
       
  1478 
       
  1479     CleanupStack::PopAndDestroy( backupPath );
       
  1480     
       
  1481     }
       
  1482 
       
  1483 // -----------------------------------------------------------------------------
       
  1484 // ChspsDefinitionRepository::ClearBackupsL
       
  1485 // Clears previous backup data
       
  1486 // (other items were commented in a header).
       
  1487 // -----------------------------------------------------------------------------
       
  1488 EXPORT_C void ChspsDefinitionRepository::ClearBackupsL()
       
  1489     {
       
  1490 
       
  1491     HBufC* backupPath = HBufC::NewL( KMaxFileName );
       
  1492     CleanupStack::PushL( backupPath );
       
  1493     TPtr backupPathPtr = backupPath->Des();
       
  1494     
       
  1495     User::LeaveIfError( iFs.PrivatePath( backupPathPtr ));
       
  1496 
       
  1497     AppendDesCIntoPathL( backupPathPtr, KBackupFolder );
       
  1498     AppendDesCIntoPathL( backupPathPtr, KDoubleBackSlash );
       
  1499 
       
  1500     RemoveDirectoryL( backupPathPtr );
       
  1501     
       
  1502     CleanupStack::PopAndDestroy( backupPath );
       
  1503     
       
  1504     }
       
  1505 
       
  1506 // -----------------------------------------------------------------------------
       
  1507 // ChspsDefinitionRepository::GetOdtL
       
  1508 // Retrieves the odt-structure from the repository as a reference.
       
  1509 // (other items were commented in a header).
       
  1510 // -----------------------------------------------------------------------------
       
  1511 //
       
  1512 EXPORT_C void ChspsDefinitionRepository::GetOdtL( 
       
  1513     const TDes& aPath, 
       
  1514     ChspsODT& aODT )
       
  1515     {    
       
  1516     User::LeaveIfError( ReadFromFileL( aPath, aODT ) );
       
  1517     }
       
  1518 
       
  1519 // -----------------------------------------------------------------------------
       
  1520 // ChspsDefinitionRepository::RemoveDirectoryL
       
  1521 // Deletes the defined directory ( files and folders recursively )
       
  1522 // (other items were commented in a header).
       
  1523 // -----------------------------------------------------------------------------
       
  1524 void ChspsDefinitionRepository::RemoveDirectoryL(
       
  1525     TDesC& aDirPath )
       
  1526     {
       
  1527     
       
  1528     iFs.SetSessionToPrivate( EDriveC );
       
  1529     CFileMan* fileMan = CFileMan::NewL( iFs );
       
  1530     CleanupStack::PushL( fileMan );
       
  1531     TInt err = fileMan->RmDir( aDirPath );
       
  1532     CleanupStack::PopAndDestroy( fileMan );
       
  1533     
       
  1534     }
       
  1535 
       
  1536 //  End of File