srsf/vcommandmanager/src/vcresource.cpp
branchRCL_3
changeset 23 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
22:cad71a31b7fc 23:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2005 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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "vcresource.h"
       
    21 #include "rubydebug.h"
       
    22 #include <defaultvoicecommands.rsg>
       
    23 #include <barsc.h>
       
    24 #include <barsread.h>
       
    25 #include <bautils.h>
       
    26 #include <data_caging_path_literals.hrh>
       
    27 #include <numberconversion.h>
       
    28 
       
    29 // CONSTANTS
       
    30 
       
    31 // File name extension for EngineeringEnglish resource file
       
    32 _LIT( KDefaultLanguageExtension, ".rsc" );
       
    33 
       
    34 // Drives where resource file will be searched
       
    35 const TBuf<2> KResourceDrives = _L("cz");
       
    36 
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CVcResource::CVcResource
       
    42 // C++ default constructor can NOT contain any code, that
       
    43 // might leave.
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CVcResource::CVcResource( RFs& aRFs )
       
    47  : iRFs( aRFs )
       
    48     {
       
    49     // Nothing
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CVcResource::ConstructL
       
    54 // Symbian 2nd phase constructor can leave.
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 void CVcResource::ConstructL( const TDesC& aFileName )
       
    58     {
       
    59     RUBY_DEBUG_BLOCKL( "CVcResource::ConstructL" );
       
    60     
       
    61     ResolveResourceFileL( aFileName );
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CVcResource::NewL
       
    66 // Two-phased constructor.
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CVcResource* CVcResource::NewL( RFs& aRFs, const TDesC& aFileName )
       
    70     {
       
    71     CVcResource* self = new( ELeave ) CVcResource( aRFs );
       
    72     
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL( aFileName );
       
    75     CleanupStack::Pop( self );
       
    76     
       
    77     return self;
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CVcResource::~CVcResource
       
    82 // Destructor.
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 CVcResource::~CVcResource()
       
    86     {
       
    87     iKeys.Close();
       
    88     iCommands.ResetAndDestroy();
       
    89     iCommands.Close();
       
    90     iIntegerKeys.Close();
       
    91     iIntegerValues.Close();
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CVcResource::GetCommand
       
    96 // Gets the localized string based on integer key
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 HBufC& CVcResource::GetCommandL( TInt aKey )
       
   100     {
       
   101     RUBY_DEBUG_BLOCKL( "CVcResource::GetCommandL(TInt)" );
       
   102     
       
   103     TInt index = iKeys.Find( aKey );
       
   104     User::LeaveIfError( index );
       
   105 
       
   106     return *iCommands[ index ];
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CVcXmlParser::ReadValueFromLocFile
       
   111 // Gets the localized string based on descriptor key 
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 HBufC& CVcResource::GetCommandL( const TDesC& aKey )
       
   115     {
       
   116     RUBY_DEBUG_BLOCKL( "CVcResource::GetCommandL(TDesC)" );
       
   117     
       
   118     TInt length( 0 );
       
   119     TInt result( 0 );
       
   120     TDigitType dt( EDigitTypeWestern );
       
   121     result = NumberConversion::ConvertFirstNumber( aKey, length, dt );
       
   122 
       
   123     if ( length == 0 )
       
   124         {
       
   125         User::Leave( KErrNotFound );
       
   126         }
       
   127 
       
   128     return GetCommandL( result );
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CVcResource::GetValueL
       
   133 // Gets the localized string based on integer key
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 TInt CVcResource::GetValueL( TInt aKey )
       
   137     {
       
   138     RUBY_DEBUG_BLOCKL( "CVcResource::GetValueL(TInt)" );
       
   139     
       
   140     TInt index = iIntegerKeys.Find( aKey );
       
   141     User::LeaveIfError( index );
       
   142 
       
   143     return iIntegerValues[ index ];
       
   144     }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // CVcXmlParser::ReadValueFromLocFile
       
   148 // Gets the localized string based on descriptor key 
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 TInt CVcResource::GetValueL( const TDesC& aKey )
       
   152     {
       
   153     RUBY_DEBUG_BLOCKL( "CVcResource::GetValueL(TDesC)" );
       
   154     
       
   155     TInt length( 0 );
       
   156     TInt result( 0 );
       
   157     TDigitType dt( EDigitTypeWestern );
       
   158     result = NumberConversion::ConvertFirstNumber( aKey, length, dt );
       
   159 
       
   160     if ( length == 0 )
       
   161         {
       
   162         User::Leave( KErrNotFound );
       
   163         }
       
   164 
       
   165     return GetValueL( result );
       
   166     }
       
   167 
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CVcResource::ResolveResourceFileL
       
   171 // Resolves the correct localized resource file name
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 void CVcResource::ResolveResourceFileL( const TDesC& aFileName )
       
   175     {
       
   176     RUBY_DEBUG_BLOCKL( "CVcXmlParser::ResolveResourceFileL" );
       
   177     
       
   178     // Resource file found/not found flag
       
   179     TBool found( EFalse );
       
   180     
       
   181     TFileName name;
       
   182 
       
   183     name.Append( 'a' ); // this will be replaced by real drive letter
       
   184     name.Append( ':' );
       
   185     name.Append( KDC_RESOURCE_FILES_DIR );
       
   186     name.Append( aFileName );
       
   187     name.Append( KDefaultLanguageExtension );
       
   188              
       
   189     TDriveList drivelist;
       
   190     User::LeaveIfError( iRFs.DriveList( drivelist ) ); 
       
   191     TInt drive( EDriveA );
       
   192     
       
   193     while ( !found && drive <= EDriveZ )
       
   194         {
       
   195         if ( drivelist[drive] ) 
       
   196             {
       
   197             TChar driveLetter; 
       
   198 			User::LeaveIfError( iRFs.DriveToChar( drive, driveLetter ) );
       
   199             name[0] = driveLetter;
       
   200                     
       
   201             BaflUtils::NearestLanguageFile( iRFs, name );
       
   202          
       
   203             if ( BaflUtils::FileExists( iRFs, name ) )
       
   204                 {
       
   205                 // Read resource
       
   206                 found = ETrue;
       
   207                 ReadResourceFileL( iRFs, name );
       
   208                 }
       
   209             }
       
   210         drive++;
       
   211         }
       
   212         
       
   213     }
       
   214 
       
   215 // -----------------------------------------------------------------------------
       
   216 // CVcResource::ReadResourceFileL
       
   217 // Loads the resource file content
       
   218 // -----------------------------------------------------------------------------
       
   219 //
       
   220 void CVcResource::ReadResourceFileL( RFs& aRfs, const TDesC& aFileName )
       
   221     {
       
   222     RUBY_DEBUG_BLOCKL( "CVcXmlParser::ReadResourceFileL" );
       
   223     
       
   224     RResourceFile resourceFile;
       
   225 
       
   226     resourceFile.OpenL( aRfs, aFileName );
       
   227     CleanupClosePushL( resourceFile );
       
   228 
       
   229 	HBufC8* res = resourceFile.AllocReadLC( VOICECOMMANDS );
       
   230 
       
   231 	TResourceReader reader;
       
   232 	reader.SetBuffer( res );
       
   233 
       
   234     TInt index( 0 );
       
   235     TInt number( 0 );
       
   236     
       
   237     // The first WORD contains the number 
       
   238     // of structs within the resource
       
   239     number = reader.ReadInt16();
       
   240 
       
   241     for ( index = 0; index < number ; index++ )
       
   242         {
       
   243         TInt key = reader.ReadInt16();
       
   244         iKeys.Append( key );
       
   245         HBufC* command = reader.ReadHBufCL();
       
   246         iCommands.Append( command );
       
   247         }
       
   248 
       
   249 	CleanupStack::PopAndDestroy( res );
       
   250 	
       
   251 	
       
   252 	res = resourceFile.AllocReadLC( VCOMMANDINTEGERS );
       
   253 
       
   254 	reader.SetBuffer( res );
       
   255 
       
   256     index = 0;
       
   257     number = 0;
       
   258     
       
   259     // The first WORD contains the number 
       
   260     // of structs within the resource
       
   261     number = reader.ReadInt16();
       
   262 
       
   263     for ( index = 0; index < number ; index++ )
       
   264         {
       
   265         iIntegerKeys.Append( reader.ReadInt16() );
       
   266         iIntegerValues.Append( reader.ReadInt16() );
       
   267         }
       
   268 
       
   269 	CleanupStack::PopAndDestroy( res );
       
   270 	CleanupStack::PopAndDestroy( &resourceFile );
       
   271     }
       
   272     
       
   273 // End of File