videofeeds/utils/src/CIptvResourceLoader.cpp
changeset 0 96612d01cf9f
equal deleted inserted replaced
-1:000000000000 0:96612d01cf9f
       
     1 /*
       
     2 * Copyright (c) 2004-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 the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    Resource file loader for components that cannot use the*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 #include <e32base.h>
       
    21 #include <bautils.h>
       
    22 #include <coemain.h>
       
    23 #include "IptvDebug.h"
       
    24 #include "CIptvResourceLoader.h"
       
    25 
       
    26 _LIT( KIptvEDrive, "E:" );
       
    27 _LIT( KIptvFDrive, "F:" );
       
    28 _LIT( KIptvZDrive, "Z:" );
       
    29 _LIT( KIptvRsc, ".RSC" );
       
    30 _LIT( KIptvRxx, ".R" );
       
    31 const TInt KIptvLangCodeWidth2( 2 );
       
    32 const TInt KIptvLangExtLength( 10 );
       
    33 
       
    34 // ---------------------------------------------------------
       
    35 // CIptvResourceLoader::NewL
       
    36 // Two-phased constructor.
       
    37 // ---------------------------------------------------------
       
    38 //
       
    39 EXPORT_C CIptvResourceLoader* CIptvResourceLoader::NewL( CCoeEnv& aCoeEnv )
       
    40     {
       
    41     IPTVLOGSTRING_LOW_LEVEL( "CIptvResourceLoader::NewL" );
       
    42 
       
    43     CIptvResourceLoader* self = new( ELeave ) CIptvResourceLoader( aCoeEnv );
       
    44     CleanupStack::PushL( self );
       
    45     self->ConstructL();
       
    46     CleanupStack::Pop( self );
       
    47     return self;
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------
       
    51 // CIptvResourceLoader::CIptvResourceLoader
       
    52 // C++ default constructor
       
    53 // ---------------------------------------------------------
       
    54 //
       
    55 CIptvResourceLoader::CIptvResourceLoader( CCoeEnv& aCoeEnv )
       
    56   : iCoeEnv( aCoeEnv )
       
    57     {
       
    58     // None
       
    59     }
       
    60 
       
    61 // ---------------------------------------------------------
       
    62 // CIptvResourceLoader::ConstructL
       
    63 // Symbian 2nd phase constructor can leave.
       
    64 // ---------------------------------------------------------
       
    65 //
       
    66 void CIptvResourceLoader::ConstructL()
       
    67     {    
       
    68     // None
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------
       
    72 // CIptvResourceLoader::~CIptvResourceLoader
       
    73 // Destructor
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 CIptvResourceLoader::~CIptvResourceLoader()
       
    77     {        
       
    78     IPTVLOGSTRING_LOW_LEVEL( "CIptvResourceLoader::~CIptvResourceLoader" );
       
    79     
       
    80     if ( iAdded )
       
    81         {
       
    82         iCoeEnv.DeleteResourceFile( iOffset );    
       
    83         }
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------
       
    87 // CIptvResourceLoader::AddResourceL
       
    88 // 
       
    89 // ---------------------------------------------------------
       
    90 //
       
    91 EXPORT_C void CIptvResourceLoader::AddResourceL(
       
    92     const TDesC& aResourceFile )
       
    93     {
       
    94     IPTVLOGSTRING_LOW_LEVEL( "CIptvResourceLoader::AddResourceL()" );
       
    95 
       
    96     // Language
       
    97     TLanguage language = User::Language();
       
    98     IPTVLOGSTRING2_LOW_LEVEL( "CIptvResourceLoader::AddResourceL(), language: %d", language );
       
    99     TBuf<KIptvLangExtLength> langExt( KNullDesC );
       
   100     if ( language <= ELangOther ) // Langs 0 - 99
       
   101         {
       
   102         langExt.NumFixedWidth( language, EDecimal, KIptvLangCodeWidth2 );
       
   103         }
       
   104     else // Languages 100 - ELangMaximum
       
   105         {
       
   106         langExt.Num( language, EDecimal );
       
   107         }
       
   108 
       
   109     // File parser 
       
   110     TParse* fp = new( ELeave ) TParse();
       
   111     CleanupStack::PushL( fp );
       
   112     User::LeaveIfError( fp->Set( aResourceFile, NULL, NULL ) );
       
   113 
       
   114     // Search with language code
       
   115     TFileName file( fp->Path() );
       
   116     file.Append( fp->Name() );
       
   117     file.Append( KIptvRxx );
       
   118     file.Append( langExt );
       
   119     TInt error( SearchResourceFile( file ) );
       
   120 
       
   121     // If no language file found, change ext to .RSC
       
   122     if ( error )
       
   123         {
       
   124         file.Copy( fp->Path() );
       
   125         file.Append( fp->Name() );
       
   126         file.Append( KIptvRsc );
       
   127         file.ZeroTerminate();
       
   128         error = SearchResourceFile( file );
       
   129         }
       
   130 
       
   131     // See result
       
   132     if ( error )
       
   133         {
       
   134         IPTVLOGSTRING_LOW_LEVEL( "CIptvResourceLoader::AddResourceL(), NO RESOURCE FILE ADDED!" );
       
   135         iAdded = EFalse;
       
   136         }                   
       
   137     else
       
   138         {
       
   139         iAdded = ETrue;
       
   140         }
       
   141     
       
   142     CleanupStack::PopAndDestroy( fp );
       
   143     User::LeaveIfError( error );
       
   144     }
       
   145 
       
   146 // ---------------------------------------------------------
       
   147 // CIptvResourceLoader::SearchResourceFile
       
   148 // 
       
   149 // ---------------------------------------------------------
       
   150 //
       
   151 TInt CIptvResourceLoader::SearchResourceFile( const TDesC& aFile )
       
   152     {
       
   153     // Try to load resource file without drive letter
       
   154     TInt error( AddResourceFile( aFile ) );
       
   155 
       
   156     if ( error )
       
   157         {
       
   158         // Try to load resource file from E-drive
       
   159         TFileName eDrive( KIptvEDrive );
       
   160         eDrive.Append( aFile );
       
   161         error = AddResourceFile( eDrive );
       
   162         }
       
   163 
       
   164     if ( error )
       
   165         {
       
   166         // Try to load resource file from F-drive
       
   167         TFileName fDrive( KIptvFDrive);
       
   168         fDrive.Append( aFile );
       
   169         error = AddResourceFile( fDrive );
       
   170         }
       
   171 
       
   172     if ( error )
       
   173         {
       
   174         // Try to load resource file from Z-drive
       
   175         TFileName zDrive( KIptvZDrive);
       
   176         zDrive.Append( aFile );
       
   177         error = AddResourceFile( zDrive );
       
   178         }
       
   179 
       
   180     // If language variant was not found, next try to find nearest one
       
   181     if ( error )
       
   182         {
       
   183         TFileName tempFile( aFile );
       
   184         BaflUtils::NearestLanguageFile( iCoeEnv.FsSession(), tempFile );
       
   185         // Did BaflUtil change the file
       
   186         if ( tempFile.Compare( aFile ) != 0 )
       
   187             {
       
   188             error = AddResourceFile( tempFile );
       
   189             }
       
   190         }
       
   191     
       
   192     return error;
       
   193     }
       
   194 
       
   195 // ---------------------------------------------------------
       
   196 // CIptvResourceLoader::AddResourceFile
       
   197 // 
       
   198 // ---------------------------------------------------------
       
   199 //
       
   200 TInt CIptvResourceLoader::AddResourceFile( const TDesC& aFile )
       
   201     {
       
   202     TInt error( KErrNotFound );
       
   203     if ( BaflUtils::FileExists( iCoeEnv.FsSession(), aFile ) )
       
   204         {
       
   205         TRAP( error, iOffset = iCoeEnv.AddResourceFileL( aFile ) );
       
   206         if ( error == KErrNone )
       
   207             {
       
   208             IPTVLOGSTRING2_LOW_LEVEL( "CIptvResourceLoader::AddResourceFile(), Added: %S", &aFile );    
       
   209             }
       
   210         }    
       
   211 
       
   212     return error;
       
   213     }
       
   214