merge the formfield patch from ooo-build
[ooovba.git] / sc / source / core / tool / unitconv.cxx
blob235e6545aa98588af6919ec6ac96f6e25dcb6b2f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unitconv.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 #include <com/sun/star/uno/Any.hxx>
37 #include <com/sun/star/uno/Sequence.hxx>
39 #include "unitconv.hxx"
40 #include "global.hxx"
41 #include "viewopti.hxx" //! move ScLinkConfigItem to separate header!
43 using namespace utl;
44 using namespace rtl;
45 using namespace com::sun::star::uno;
47 // --------------------------------------------------------------------
49 const sal_Unicode cDelim = 0x01; // Delimiter zwischen From und To
52 // --- ScUnitConverterData --------------------------------------------
54 ScUnitConverterData::ScUnitConverterData( const String& rFromUnit,
55 const String& rToUnit, double fVal )
57 StrData( rFromUnit ),
58 fValue( fVal )
60 String aTmp;
61 ScUnitConverterData::BuildIndexString( aTmp, rFromUnit, rToUnit );
62 SetString( aTmp );
66 ScUnitConverterData::ScUnitConverterData( const ScUnitConverterData& r )
68 StrData( r ),
69 fValue( r.fValue )
74 ScDataObject* ScUnitConverterData::Clone() const
76 return new ScUnitConverterData( *this );
80 // static
81 void ScUnitConverterData::BuildIndexString( String& rStr,
82 const String& rFromUnit, const String& rToUnit )
84 #if 1
85 // case sensitive
86 rStr = rFromUnit;
87 rStr += cDelim;
88 rStr += rToUnit;
89 #else
90 // not case sensitive
91 rStr = rFromUnit;
92 String aTo( rToUnit );
93 ScGlobal::pCharClass->toUpper( rStr );
94 ScGlobal::pCharClass->toUpper( aTo );
95 rStr += cDelim;
96 rStr += aTo;
97 #endif
101 // --- ScUnitConverter ------------------------------------------------
103 #define CFGPATH_UNIT "Office.Calc/UnitConversion"
104 #define CFGSTR_UNIT_FROM "FromUnit"
105 #define CFGSTR_UNIT_TO "ToUnit"
106 #define CFGSTR_UNIT_FACTOR "Factor"
108 ScUnitConverter::ScUnitConverter( USHORT nInit, USHORT nDeltaP ) :
109 ScStrCollection( nInit, nDeltaP, FALSE )
111 // read from configuration - "convert.ini" is no longer used
112 //! config item as member to allow change of values
114 ScLinkConfigItem aConfigItem( OUString::createFromAscii( CFGPATH_UNIT ) );
116 // empty node name -> use the config item's path itself
117 OUString aEmptyString;
118 Sequence<OUString> aNodeNames = aConfigItem.GetNodeNames( aEmptyString );
120 long nNodeCount = aNodeNames.getLength();
121 if ( nNodeCount )
123 const OUString* pNodeArray = aNodeNames.getConstArray();
124 Sequence<OUString> aValNames( nNodeCount * 3 );
125 OUString* pValNameArray = aValNames.getArray();
126 const OUString sSlash('/');
128 long nIndex = 0;
129 for (long i=0; i<nNodeCount; i++)
131 OUString sPrefix = pNodeArray[i];
132 sPrefix += sSlash;
134 pValNameArray[nIndex] = sPrefix;
135 pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_FROM );
136 pValNameArray[nIndex] = sPrefix;
137 pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_TO );
138 pValNameArray[nIndex] = sPrefix;
139 pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_FACTOR );
142 Sequence<Any> aProperties = aConfigItem.GetProperties(aValNames);
144 if (aProperties.getLength() == aValNames.getLength())
146 const Any* pProperties = aProperties.getConstArray();
148 OUString sFromUnit;
149 OUString sToUnit;
150 double fFactor = 0;
152 nIndex = 0;
153 for (long i=0; i<nNodeCount; i++)
155 pProperties[nIndex++] >>= sFromUnit;
156 pProperties[nIndex++] >>= sToUnit;
157 pProperties[nIndex++] >>= fFactor;
159 ScUnitConverterData* pNew = new ScUnitConverterData( sFromUnit, sToUnit, fFactor );
160 if ( !Insert( pNew ) )
161 delete pNew;
167 BOOL ScUnitConverter::GetValue( double& fValue, const String& rFromUnit,
168 const String& rToUnit ) const
170 ScUnitConverterData aSearch( rFromUnit, rToUnit );
171 USHORT nIndex;
172 if ( Search( &aSearch, nIndex ) )
174 fValue = ((const ScUnitConverterData*)(At( nIndex )))->GetValue();
175 return TRUE;
177 fValue = 1.0;
178 return FALSE;