extras/converter/engine/Src/CCnvCategory.cpp
changeset 0 3ee3dfdd8d69
equal deleted inserted replaced
-1:000000000000 0:3ee3dfdd8d69
       
     1 /*
       
     2 * Copyright (c) 2002 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:  
       
    15 *	  This is the implementation of the class defined in CCnvCategory.h
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include <barsread.h>	// TResourceReader
       
    22 #include <e32math.h>	// Math::Round()
       
    23 #include <s32strm.h>	// RReadStream, RWriteStream
       
    24 #include <Converter.rsg>
       
    25 #include "CCnvCategory.h"
       
    26 
       
    27 CCnvCategory::~CCnvCategory()
       
    28 	{
       
    29 	iUnitArray.Reset();
       
    30 	iTempUintForRestoreArray.Reset();
       
    31 	}
       
    32 
       
    33 CCnvCategory* CCnvCategory::NewLC( TResourceReader& aReader , TBool aIsCurrencyCategory)
       
    34 	{
       
    35 	CCnvCategory* self = new( ELeave ) CCnvCategory;
       
    36 	CleanupStack::PushL( self );
       
    37 	self->ConstructFromResourceL( aReader ,aIsCurrencyCategory );
       
    38 
       
    39 	return self;
       
    40 	}
       
    41 
       
    42 void CCnvCategory::ConstructFromResourceL( TResourceReader& aReader, TBool aIsCurrencyCategory)  
       
    43 	{
       
    44 	// read the category parameters
       
    45 	iName.Copy( aReader.ReadTPtrC() );
       
    46 	TInt isChineseorder = aReader.ReadInt8();
       
    47 	iBaseUnitIndex = aReader.ReadInt8();
       
    48 
       
    49 
       
    50 	if( isChineseorder == 1)
       
    51 		{
       
    52 		switch( User::Language() )
       
    53 			{
       
    54 			case ELangHongKongChinese:
       
    55 			case ELangTaiwanChinese:
       
    56 			case ELangPrcChinese:
       
    57 				iDestinationUnitIndex = aReader.ReadInt8();
       
    58 				iSourceUnitIndex = aReader.ReadInt8();		
       
    59 				break;
       
    60 			default:
       
    61 				iSourceUnitIndex = aReader.ReadInt8();
       
    62 				iDestinationUnitIndex = aReader.ReadInt8();
       
    63 				break;
       
    64 			}
       
    65 		}
       
    66 	else
       
    67 		{
       
    68 		iSourceUnitIndex = aReader.ReadInt8();
       
    69 		iDestinationUnitIndex = aReader.ReadInt8();
       
    70 		}
       
    71 	
       
    72 	TBool isTemperatureCategory( EFalse );
       
    73 	if( aReader.ReadInt8() != 0 )
       
    74 		{
       
    75 		isTemperatureCategory = ETrue;
       
    76 		}
       
    77 
       
    78 	// start reading the unit array
       
    79 	TInt numUnits = aReader.ReadInt16();
       
    80 
       
    81 	TCnvUnit unit;
       
    82 	for( TInt i = 0; i < numUnits; i++ )
       
    83 		{
       
    84 		unit.ReadFromResource( aReader, isTemperatureCategory, aIsCurrencyCategory );
       
    85 		iUnitArray.AppendL( unit );
       
    86 		}
       
    87 	}
       
    88 
       
    89 TInt CCnvCategory::MdcaCount() const
       
    90 	{
       
    91 	return iUnitArray.Count();
       
    92 	}
       
    93 
       
    94 // Returns the name of the unit at index 'aIndex'
       
    95 TPtrC CCnvCategory::MdcaPoint( TInt aIndex ) const
       
    96 	{
       
    97 	return TPtrC( iUnitArray.At( aIndex ).iName );
       
    98 	}
       
    99 
       
   100 TReal CCnvCategory::Convert( TUint aSourceUnit, TUint aDestinationUnit,
       
   101 							const TReal& aAmount, TUint aDecimals ) const
       
   102 	{
       
   103 	const TCnvUnit& srcUnit = iUnitArray.At( aSourceUnit );
       
   104 	const TCnvUnit& dstUnit = iUnitArray.At( aDestinationUnit );
       
   105 
       
   106 	TReal result( ( aAmount - srcUnit.iOffset ) / srcUnit.iFactor
       
   107 		* dstUnit.iFactor + dstUnit.iOffset );
       
   108 	Math::Round( result, result, aDecimals );
       
   109 	return result;
       
   110 	}
       
   111 
       
   112 void CCnvCategory::GetDefaultUnits( TUint& aSourceUnit,
       
   113 								   TUint& aDestinationUnit ) const
       
   114 	{
       
   115 	aSourceUnit = iSourceUnitIndex;
       
   116 	aDestinationUnit = iDestinationUnitIndex;
       
   117 	}
       
   118 
       
   119 void CCnvCategory::SetDefaultUnits( TUint aSourceUnit,
       
   120 								   TUint aDestinationUnit )
       
   121 	{
       
   122 	iSourceUnitIndex = aSourceUnit;
       
   123 	iDestinationUnitIndex = aDestinationUnit;
       
   124 	}
       
   125 
       
   126 TUint CCnvCategory::BaseUnit() const
       
   127 	{
       
   128 	return iBaseUnitIndex;
       
   129 	}
       
   130 
       
   131 void CCnvCategory::SetBaseUnit( const TUint& aBaseUnit )
       
   132 	{
       
   133 	iBaseUnitIndex = aBaseUnit;
       
   134 	}
       
   135 
       
   136 const TDes& CCnvCategory::Name() const
       
   137 	{
       
   138 	return iName;
       
   139 	}
       
   140 
       
   141 TCnvUnit& CCnvCategory::Unit( TUint aIndex )
       
   142 	{
       
   143 	return iUnitArray.At( aIndex );
       
   144 	}
       
   145 
       
   146 // Store the data in a stream.
       
   147 void CCnvCategory::ExternalizeL( RWriteStream& aStream ) const
       
   148 	{
       
   149 	aStream.WriteUint8L( iBaseUnitIndex );
       
   150 	aStream.WriteUint8L( iSourceUnitIndex );
       
   151 	aStream.WriteUint8L( iDestinationUnitIndex );
       
   152 
       
   153 	TUint len( iUnitArray.Count() );
       
   154 	aStream.WriteUint8L( iUnitArray.Count() );
       
   155 	for( TUint i = 0; i < len; i ++)
       
   156 		{
       
   157 		iUnitArray.At( i ).ExternalizeL( aStream );
       
   158 		}
       
   159 	}
       
   160 
       
   161 // Recover the data from a stream.
       
   162 void CCnvCategory::InternalizeL( RReadStream& aStream, TBool aIsCurrencyCategory )
       
   163 	{
       
   164 	TBuf<16> currencyNames[3];
       
   165 	
       
   166 	iBaseUnitIndex = aStream.ReadUint8L();
       
   167 	iSourceUnitIndex = aStream.ReadUint8L();
       
   168 	iDestinationUnitIndex = aStream.ReadUint8L();
       
   169 
       
   170 	TUint len( aStream.ReadUint8L() );
       
   171 	
       
   172 	//The ini file exists. So delete the existing iUnitArray and load it again 
       
   173 	//from the Configuration Settings file
       
   174 	
       
   175 	//Trying to use enum values as array indexes
       
   176 	currencyNames[1] = iUnitArray.At(0).iName; // Qtn_Cnv_Home
       
   177 	currencyNames[2] = iUnitArray.At(1).iName; // Qtn_Cnv_Foreign
       
   178 	
       
   179 	iUnitArray.Delete(0,iUnitArray.Count());	
       
   180 
       
   181 	for( TUint i = 0; i < len; i ++)
       
   182 		{
       
   183 		//Change: Loads the currencies directly from ini file. no searching..
       
   184 		//		TInt index;
       
   185 		TCnvUnit unit;
       
   186 		unit.InternalizeL( aStream , aIsCurrencyCategory);
       
   187 
       
   188 		iUnitArray.AppendL(unit);
       
   189 		}
       
   190 		
       
   191 	TCurrencyType type;
       
   192 	
       
   193 	for( TUint in = 0; in < len; in++)
       
   194 		{
       
   195 			type = iUnitArray.At(in).iNameType;
       
   196 			if(type == EHome)
       
   197 				iUnitArray.At(in).iName = currencyNames[1];
       
   198 			else if(type == EForeign)
       
   199 				iUnitArray.At(in).iName = currencyNames[2];
       
   200 		}
       
   201 	}
       
   202 
       
   203 
       
   204 void CCnvCategory::AddNewCurrencyL( TInt aCurrencyId, TDesC& aNewName, TReal& aNewFactor, TCurrencyType aType)
       
   205 	{
       
   206 	TCnvUnit unit;
       
   207 	TUint curencyId = aCurrencyId; // To remove the warnings
       
   208 	unit.iName  = aNewName;	
       
   209 	unit.iFactor = aNewFactor;
       
   210 	unit.iNameType = aType;
       
   211 	unit.iIsCurrencyUnit = ETrue;
       
   212 	//To calculate the id for the new currency added.
       
   213 	//will run thru the ids, gets the largest id and increments it and assigns to the new unit.
       
   214 	//Make sure that the ids for the currencies have the largest possible value in the hrh file.
       
   215 	TUint id;
       
   216 	id = iUnitArray.At(0).iId;
       
   217 	for(TInt i = 1; i < iUnitArray.Count(); i++)
       
   218 		{
       
   219 		if(id < iUnitArray.At(i).iId)
       
   220 			{
       
   221 			id = iUnitArray.At(i).iId;
       
   222 			}
       
   223 		}
       
   224 	id++;
       
   225 	unit.iId = id;
       
   226 
       
   227 	iUnitArray.InsertL( curencyId, unit );
       
   228 	//New element has been added. So set the base source and destination currencies accordingly
       
   229 	if( iBaseUnitIndex >= curencyId )
       
   230 		{		
       
   231 		iBaseUnitIndex++; 
       
   232 		}
       
   233 	if( iSourceUnitIndex >= curencyId )
       
   234 		{		
       
   235 		iSourceUnitIndex++; 
       
   236 		}
       
   237 	if( iDestinationUnitIndex >= curencyId )
       
   238 		{		
       
   239 		iDestinationUnitIndex++; 
       
   240 		}
       
   241 
       
   242 	}
       
   243 
       
   244 
       
   245 
       
   246 void CCnvCategory::DeleteCurrency(TInt aCurrencyId)
       
   247 	{
       
   248 	iUnitArray.Delete(aCurrencyId);
       
   249 
       
   250 	TUint unsignedCurrencyId = aCurrencyId; //to remove warnings
       
   251 
       
   252 	//An element has been deleted. So set the base source and destination currencies accordingly
       
   253 	if(iBaseUnitIndex >= unsignedCurrencyId   )
       
   254 		{
       
   255 		if( iBaseUnitIndex > 0)
       
   256 			iBaseUnitIndex--;
       
   257 		}
       
   258 	if(iSourceUnitIndex >= unsignedCurrencyId   )
       
   259 		{
       
   260 		if( iSourceUnitIndex > 0)
       
   261 			iSourceUnitIndex--;
       
   262 		}
       
   263 	if(iDestinationUnitIndex >= unsignedCurrencyId   )
       
   264 		{
       
   265 		if( iDestinationUnitIndex > 0)
       
   266 			iDestinationUnitIndex--;
       
   267 		}
       
   268 
       
   269 	}
       
   270 
       
   271 
       
   272 void CCnvCategory::CreateTempUnitArrayForRestore()
       
   273 	{
       
   274 	iTempUintForRestoreArray.Reset();
       
   275 	for ( TInt i = 0; i < iUnitArray.Count() ; i++ )
       
   276 		{
       
   277 		TRAP_IGNORE( iTempUintForRestoreArray.AppendL( iUnitArray.At( i ) ) );
       
   278 		}
       
   279 	}
       
   280 
       
   281 void CCnvCategory::RestoreUnitArray()
       
   282 	{
       
   283 	iUnitArray.Reset();
       
   284 	for ( TInt i = 0 ; i < iTempUintForRestoreArray.Count() ; i++ )
       
   285 		{
       
   286 		TRAP_IGNORE( iUnitArray.AppendL( iTempUintForRestoreArray.At( i ) ) );
       
   287 		}
       
   288 	}
       
   289 
       
   290 // End of File