1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_sc.hxx" 30 31 32 33 #include <com/sun/star/uno/Any.hxx> 34 #include <com/sun/star/uno/Sequence.hxx> 35 36 #include "unitconv.hxx" 37 #include "global.hxx" 38 #include "viewopti.hxx" //! move ScLinkConfigItem to separate header! 39 40 using namespace utl; 41 using namespace rtl; 42 using namespace com::sun::star::uno; 43 44 // -------------------------------------------------------------------- 45 46 const sal_Unicode cDelim = 0x01; // Delimiter zwischen From und To 47 48 49 // --- ScUnitConverterData -------------------------------------------- 50 51 ScUnitConverterData::ScUnitConverterData( const String& rFromUnit, 52 const String& rToUnit, double fVal ) 53 : 54 StrData( rFromUnit ), 55 fValue( fVal ) 56 { 57 String aTmp; 58 ScUnitConverterData::BuildIndexString( aTmp, rFromUnit, rToUnit ); 59 SetString( aTmp ); 60 } 61 62 63 ScUnitConverterData::ScUnitConverterData( const ScUnitConverterData& r ) 64 : 65 StrData( r ), 66 fValue( r.fValue ) 67 { 68 } 69 70 71 ScDataObject* ScUnitConverterData::Clone() const 72 { 73 return new ScUnitConverterData( *this ); 74 } 75 76 77 // static 78 void ScUnitConverterData::BuildIndexString( String& rStr, 79 const String& rFromUnit, const String& rToUnit ) 80 { 81 #if 1 82 // case sensitive 83 rStr = rFromUnit; 84 rStr += cDelim; 85 rStr += rToUnit; 86 #else 87 // not case sensitive 88 rStr = rFromUnit; 89 String aTo( rToUnit ); 90 ScGlobal::pCharClass->toUpper( rStr ); 91 ScGlobal::pCharClass->toUpper( aTo ); 92 rStr += cDelim; 93 rStr += aTo; 94 #endif 95 } 96 97 98 // --- ScUnitConverter ------------------------------------------------ 99 100 #define CFGPATH_UNIT "Office.Calc/UnitConversion" 101 #define CFGSTR_UNIT_FROM "FromUnit" 102 #define CFGSTR_UNIT_TO "ToUnit" 103 #define CFGSTR_UNIT_FACTOR "Factor" 104 105 ScUnitConverter::ScUnitConverter( sal_uInt16 nInit, sal_uInt16 nDeltaP ) : 106 ScStrCollection( nInit, nDeltaP, sal_False ) 107 { 108 // read from configuration - "convert.ini" is no longer used 109 //! config item as member to allow change of values 110 111 ScLinkConfigItem aConfigItem( OUString::createFromAscii( CFGPATH_UNIT ) ); 112 113 // empty node name -> use the config item's path itself 114 OUString aEmptyString; 115 Sequence<OUString> aNodeNames = aConfigItem.GetNodeNames( aEmptyString ); 116 117 long nNodeCount = aNodeNames.getLength(); 118 if ( nNodeCount ) 119 { 120 const OUString* pNodeArray = aNodeNames.getConstArray(); 121 Sequence<OUString> aValNames( nNodeCount * 3 ); 122 OUString* pValNameArray = aValNames.getArray(); 123 const OUString sSlash('/'); 124 125 long nIndex = 0; 126 for (long i=0; i<nNodeCount; i++) 127 { 128 OUString sPrefix = pNodeArray[i]; 129 sPrefix += sSlash; 130 131 pValNameArray[nIndex] = sPrefix; 132 pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_FROM ); 133 pValNameArray[nIndex] = sPrefix; 134 pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_TO ); 135 pValNameArray[nIndex] = sPrefix; 136 pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_FACTOR ); 137 } 138 139 Sequence<Any> aProperties = aConfigItem.GetProperties(aValNames); 140 141 if (aProperties.getLength() == aValNames.getLength()) 142 { 143 const Any* pProperties = aProperties.getConstArray(); 144 145 OUString sFromUnit; 146 OUString sToUnit; 147 double fFactor = 0; 148 149 nIndex = 0; 150 for (long i=0; i<nNodeCount; i++) 151 { 152 pProperties[nIndex++] >>= sFromUnit; 153 pProperties[nIndex++] >>= sToUnit; 154 pProperties[nIndex++] >>= fFactor; 155 156 ScUnitConverterData* pNew = new ScUnitConverterData( sFromUnit, sToUnit, fFactor ); 157 if ( !Insert( pNew ) ) 158 delete pNew; 159 } 160 } 161 } 162 } 163 164 sal_Bool ScUnitConverter::GetValue( double& fValue, const String& rFromUnit, 165 const String& rToUnit ) const 166 { 167 ScUnitConverterData aSearch( rFromUnit, rToUnit ); 168 sal_uInt16 nIndex; 169 if ( Search( &aSearch, nIndex ) ) 170 { 171 fValue = ((const ScUnitConverterData*)(At( nIndex )))->GetValue(); 172 return sal_True; 173 } 174 fValue = 1.0; 175 return sal_False; 176 } 177 178 179