srsf/sisrscontrollerplugin/src/asrplugindataloader.cpp
branchRCL_3
changeset 19 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2004 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:  Language package data file loader 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "asrplugindataloader.h"
       
    21 #include "sysconfigprovider.h"
       
    22 
       
    23 
       
    24 #ifdef __SIND_AUTOCONF_LID
       
    25 #include <syslangutil.h>
       
    26 /* MSysConfigProvider is a wrapper for get syslangutil. This makes it easier to test without */
       
    27 /* dependency to the actual language configuration in the emulator environment. The test  */
       
    28 /* class implements this interface. */
       
    29 class TDefaultConfig : public MSysConfigProvider {
       
    30 public:
       
    31 	TDefaultConfig() {};
       
    32 	~TDefaultConfig() {};
       
    33 	TInt GetInstalledLanguages( CArrayFixFlat<TInt>*& aLanguages, RFs* aFileServerSession = NULL ) 
       
    34 	{
       
    35 		return SysLangUtil::GetInstalledLanguages(aLanguages, aFileServerSession);		
       
    36 	};
       
    37 };
       
    38 #endif  // __SIND_AUTOCONF_LID
       
    39 
       
    40 // Local constants
       
    41 
       
    42 _LIT( KZDriveLetter, "z" ); 
       
    43  
       
    44 
       
    45 #ifdef __SIND_AUTOCONF_LID
       
    46 const TUint32 KTtpAutoConfLidPackageType = 1;
       
    47 const TUint32 KTtpAutoConfLidPackageID   = 1;
       
    48 const TInt KLidBufGranularity = 1000;
       
    49 #endif  // __SIND_AUTOCONF_LID
       
    50 
       
    51 
       
    52 // ============================ MEMBER FUNCTIONS ===============================
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CDataLoaderLoader::CDataLoader
       
    56 // C++ default constructor can NOT contain any code, that
       
    57 // might leave.
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CDataLoader::CDataLoader() 
       
    61     {
       
    62     // Nothing
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CDataLoader::ConstructL
       
    67 // Symbian 2nd phase constructor can leave.
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 void CDataLoader::ConstructL( const TDesC& aPrefixOfFileName,
       
    71 							  const TDesC& aSeparator,                                     
       
    72                               const TDesC& aPostfixOfFileName,
       
    73                               MSysConfigProvider *aProvider )
       
    74     {
       
    75     iPrefixOfFileName = HBufC::NewL( aPrefixOfFileName.Length() );
       
    76     (*iPrefixOfFileName) = aPrefixOfFileName;
       
    77     iSeparator = HBufC::NewL( aSeparator.Length() );
       
    78     (*iSeparator) = aSeparator;
       
    79     iPostfixOfFileName = HBufC::NewL( aPostfixOfFileName.Length() );
       
    80     (*iPostfixOfFileName) = aPostfixOfFileName;
       
    81     
       
    82 	iProvider = aProvider;
       
    83 
       
    84     // Open file session
       
    85     User::LeaveIfError( iFs.Connect() );
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CDataLoader::NewL
       
    90 // Two-phased constructor.
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 CDataLoader* CDataLoader::NewL( const TDesC& aPrefixOfFileName,
       
    94 								const TDesC& aSeparator,                                     
       
    95                                 const TDesC& aPostfixOfFileName,
       
    96                                 MSysConfigProvider *aProvider )
       
    97     {
       
    98     CDataLoader* self = new ( ELeave ) CDataLoader();
       
    99     
       
   100     CleanupStack::PushL( self );
       
   101     self->ConstructL( aPrefixOfFileName,
       
   102 					  aSeparator,                                     
       
   103                       aPostfixOfFileName,
       
   104                       aProvider);
       
   105     CleanupStack::Pop( self );
       
   106     
       
   107     return self;
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CDataLoader::~CDataLoader
       
   112 // Destructor.
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 CDataLoader::~CDataLoader()
       
   116     {
       
   117     delete iPrefixOfFileName,
       
   118 	delete iSeparator,                                     
       
   119     delete iPostfixOfFileName;    
       
   120     
       
   121     iFs.Close();
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CDataLoader::PackageName
       
   126 // Construct filename for requested package
       
   127 // -----------------------------------------------------------------------------
       
   128 //	
       
   129 void CDataLoader::PackageName (TFileName &aFileName, TUint32 aPackageType, TUint32 aPackageID) 
       
   130 {
       
   131 	// Resolve the name of the file
       
   132 	// filename is <iPrefixOfFileName>_<PackageType>_<aPackageID><iPostfixOfFileName>
       
   133 
       
   134 	aFileName.Copy( iPrefixOfFileName->Des() );          //  path/SRSF_
       
   135 	aFileName.AppendNumUC( aPackageType );               //  path/SRSF_1
       
   136 	aFileName.Append( iSeparator->Des() );               //  path/SRSF_1_
       
   137 	aFileName.AppendNumUC( aPackageID );                 //  path/SRSF_1_326
       
   138 	aFileName.Append( iPostfixOfFileName->Des() );       //  path/SRSF_1_326.bin
       
   139 }
       
   140 
       
   141 #ifdef __SIND_AUTOCONF_LID
       
   142 // -----------------------------------------------------------------------------
       
   143 // CDataLoader::AppendLoadL
       
   144 // Load language package file to the end of buffer
       
   145 // -----------------------------------------------------------------------------
       
   146 //	
       
   147 void CDataLoader::AppendLoadL( CBufBase *aBuffer,
       
   148 								TUint32 aPackageType, 
       
   149 								TUint32 aPackageID ) 
       
   150 {
       
   151 	// Resolve filename
       
   152 	TFileName fileName;
       
   153 	PackageName (fileName, aPackageType, aPackageID);
       
   154 
       
   155 	// Load data
       
   156 	HBufC8* buffer = NULL;
       
   157 	
       
   158 	TRAPD( err, buffer = DoLoadL( fileName, 0, KMaxTUint32 ));  // IMPLICIT: LID packages are loaded completely
       
   159  	if( err == KErrNone && buffer ) 
       
   160  	{
       
   161 		CleanupStack::PushL( buffer );                    // ResizeL may leave below
       
   162 
       
   163 		TInt prevSize = aBuffer->Size();                  // size before resize
       
   164 		TInt newSize  = aBuffer->Size() + buffer->Size(); // new size for lid data buffer
       
   165 
       
   166 		aBuffer->ResizeL( newSize );                      // resize lid data buffer
       
   167 		aBuffer->Write( prevSize, *buffer );              // append data from read buffer
       
   168 
       
   169 		CleanupStack::PopAndDestroy(buffer);              // buffer copied -> free
       
   170 	}
       
   171 }
       
   172 
       
   173 
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CDataLoader::LoadSysLangPackagesL
       
   177 // Load LID language data to a buffer according to installed system languages
       
   178 // -----------------------------------------------------------------------------
       
   179 //	
       
   180 void CDataLoader::SysLangPackagesToBufferL( CBufBase *aBuffer ) 
       
   181 {        
       
   182 	MSysConfigProvider *config = iProvider;
       
   183 	TDefaultConfig defaultProvider;
       
   184 	if (!config) config = &defaultProvider;
       
   185 
       
   186 	// Get installed languages
       
   187 	CArrayFixFlat<TInt>* languages( NULL );
       
   188 
       
   189 	//User::LeaveIfError (SysLangUtil::GetInstalledLanguages(languages, &iFs));
       
   190 	User::LeaveIfError (config->GetInstalledLanguages(languages, &iFs));
       
   191 	CleanupStack::PushL( languages ) ;
       
   192 
       
   193 	// Loop over installed languages
       
   194 	for ( TInt i = 0 ; i < languages->Count(); i++ )
       
   195 	{
       
   196 		AppendLoadL(aBuffer, KTtpAutoConfLidPackageType, languages->At(i));
       
   197 	}// next language
       
   198 	
       
   199 	CleanupStack::PopAndDestroy( languages );
       
   200 }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CDataLoader::ConstructLidDataL
       
   204 // Load LID language data to a buffer according to installed system languages
       
   205 // -----------------------------------------------------------------------------
       
   206 //	
       
   207 HBufC8* CDataLoader::ConstructLidDataL()
       
   208 {
       
   209 	CBufBase* lidDataBuffer = CBufSeg::NewL( KLidBufGranularity ) ; // accumulates the binary data from lid model files
       
   210 	CleanupStack::PushL( lidDataBuffer ) ;
       
   211 
       
   212 	SysLangPackagesToBufferL (lidDataBuffer);
       
   213 
       
   214 	// Prepare returnable data
       
   215 	HBufC8* dataBuffer = NULL;
       
   216 	
       
   217 	if( lidDataBuffer->Size() > 0 ) 
       
   218 	{
       
   219 		// If any data was loaded, create a buffer for it
       
   220 		dataBuffer = HBufC8::NewL( lidDataBuffer->Size() );     // create a returnable buffer
       
   221 		TPtr8 dataBufferPtr = dataBuffer->Des();                        // get a modifiable pointer to it
       
   222 		lidDataBuffer->Read( 0, dataBufferPtr, lidDataBuffer->Size() ); // put the lid data into it
       
   223 	}
       
   224 
       
   225 	CleanupStack::PopAndDestroy( lidDataBuffer ) ;  
       
   226 
       
   227 	return dataBuffer;
       
   228 }
       
   229 #endif  // __SIND_AUTOCONF_LID
       
   230 
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CDataLoader::LoadData
       
   234 // Load language package file. It resolves the name of the language package
       
   235 // -----------------------------------------------------------------------------
       
   236 //	
       
   237 HBufC8* CDataLoader::LoadData( TUint32 aPackageType, 
       
   238 								TUint32 aPackageID,
       
   239 								TUint32 aStartPosition,
       
   240 								TUint32 aEndPosition ) {
       
   241 
       
   242 	TInt err;
       
   243 	HBufC8* buffer = NULL;
       
   244 
       
   245 #ifdef __SIND_AUTOCONF_LID
       
   246 	// If LID data is requested, construct it from installed LID binary files
       
   247 	// Otherwise run normal data loading
       
   248 
       
   249 	if( aPackageType == KTtpAutoConfLidPackageType &&
       
   250 		aPackageID   == KTtpAutoConfLidPackageID )
       
   251 	{
       
   252 		TRAP( err, buffer = ConstructLidDataL() );
       
   253 	}
       
   254 #endif  // __SIND_AUTOCONF_LID
       
   255 
       
   256 	if (!buffer)	{
       
   257 		TFileName fileName;
       
   258 		PackageName (fileName, aPackageType, aPackageID);
       
   259 	
       
   260 		// Load data    
       
   261 		TRAP( err, buffer = DoLoadL( fileName, aStartPosition, aEndPosition ) );
       
   262 	}
       
   263 
       
   264 	if ( err == KErrNone )
       
   265 	{
       
   266 		return buffer;
       
   267   }
       
   268 	else
       
   269 	{
       
   270 		return NULL;
       
   271 	}    
       
   272 }
       
   273                                          
       
   274 
       
   275 
       
   276 // -----------------------------------------------------------------------------
       
   277 // CDataLoader::DoLoadL
       
   278 // Actual file loading function
       
   279 // -----------------------------------------------------------------------------
       
   280 //	
       
   281 HBufC8* CDataLoader::DoLoadL( TFileName& aFileName,
       
   282                               TUint32 aStartPosition, 
       
   283                               TUint32 aEndPosition )
       
   284     {
       
   285     TInt fileSize( 0 );
       
   286     RFile dataFile;
       
   287     TInt readLength;
       
   288 
       
   289     // Open file	
       
   290     TInt err = dataFile.Open( iFs, aFileName, EFileRead);
       
   291 	 
       
   292 		if ( err != KErrNone )	 
       
   293 		{		
       
   294 			aFileName.Replace(0,1,KZDriveLetter); // "z:\"
       
   295 			TInt err = dataFile.Open( iFs, aFileName, EFileRead);		 
       
   296 			if ( err != KErrNone )
       
   297           {
       
   298     			return NULL;
       
   299           }
       
   300 		}
       
   301 
       
   302 	CleanupClosePushL( dataFile );
       
   303     // Try to resolve how much should be read
       
   304     readLength = aEndPosition - aStartPosition;
       
   305     User::LeaveIfError( dataFile.Size( fileSize ) );
       
   306 
       
   307     if ( aEndPosition != KMaxTUint32 )
       
   308         {
       
   309         // Cannot read more than filesize
       
   310         if ( ( readLength > fileSize ) || ( readLength < 0 ) )
       
   311             {
       
   312             readLength = fileSize;
       
   313             }
       
   314         }
       
   315     else
       
   316         {
       
   317         readLength = fileSize;
       
   318         }
       
   319     
       
   320     // Seek the starting point
       
   321     // Casting needed to convert from uint to int which is needed by Seek()
       
   322     TInt startPosition = ( TInt ) aStartPosition;
       
   323     dataFile.Seek( ESeekStart, startPosition );
       
   324 
       
   325     // Reserve enough memory for the data
       
   326     HBufC8* fileBuffer = HBufC8::NewLC( readLength );
       
   327     TPtr8 bufferPtr( fileBuffer->Des() );
       
   328     
       
   329     User::LeaveIfError( dataFile.Read( bufferPtr ) );
       
   330 
       
   331     CleanupStack::Pop( fileBuffer );
       
   332     CleanupStack::PopAndDestroy( &dataFile );
       
   333     return fileBuffer;
       
   334     }
       
   335 
       
   336 // End of File