xref: /trunk/main/sc/source/core/tool/unitconv.cxx (revision b3f79822)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 
29 #include <com/sun/star/uno/Any.hxx>
30 #include <com/sun/star/uno/Sequence.hxx>
31 
32 #include "unitconv.hxx"
33 #include "global.hxx"
34 #include "viewopti.hxx"			//! move ScLinkConfigItem to separate header!
35 
36 using namespace utl;
37 using namespace rtl;
38 using namespace com::sun::star::uno;
39 
40 // --------------------------------------------------------------------
41 
42 const sal_Unicode cDelim = 0x01;		// Delimiter zwischen From und To
43 
44 
45 // --- ScUnitConverterData --------------------------------------------
46 
ScUnitConverterData(const String & rFromUnit,const String & rToUnit,double fVal)47 ScUnitConverterData::ScUnitConverterData( const String& rFromUnit,
48 			const String& rToUnit, double fVal )
49 		:
50 		StrData( rFromUnit ),
51 		fValue( fVal )
52 {
53 	String aTmp;
54 	ScUnitConverterData::BuildIndexString( aTmp, rFromUnit, rToUnit );
55 	SetString( aTmp );
56 }
57 
58 
ScUnitConverterData(const ScUnitConverterData & r)59 ScUnitConverterData::ScUnitConverterData( const ScUnitConverterData& r )
60 		:
61 		StrData( r ),
62 		fValue( r.fValue )
63 {
64 }
65 
66 
Clone() const67 ScDataObject* ScUnitConverterData::Clone() const
68 {
69 	return new ScUnitConverterData( *this );
70 }
71 
72 
73 // static
BuildIndexString(String & rStr,const String & rFromUnit,const String & rToUnit)74 void ScUnitConverterData::BuildIndexString( String& rStr,
75 			const String& rFromUnit, const String& rToUnit )
76 {
77 #if 1
78 // case sensitive
79 	rStr = rFromUnit;
80 	rStr += cDelim;
81 	rStr += rToUnit;
82 #else
83 // not case sensitive
84 	rStr = rFromUnit;
85 	String aTo( rToUnit );
86 	ScGlobal::pCharClass->toUpper( rStr );
87 	ScGlobal::pCharClass->toUpper( aTo );
88 	rStr += cDelim;
89 	rStr += aTo;
90 #endif
91 }
92 
93 
94 // --- ScUnitConverter ------------------------------------------------
95 
96 #define CFGPATH_UNIT		"Office.Calc/UnitConversion"
97 #define CFGSTR_UNIT_FROM	"FromUnit"
98 #define CFGSTR_UNIT_TO		"ToUnit"
99 #define CFGSTR_UNIT_FACTOR	"Factor"
100 
ScUnitConverter(sal_uInt16 nInit,sal_uInt16 nDeltaP)101 ScUnitConverter::ScUnitConverter( sal_uInt16 nInit, sal_uInt16 nDeltaP ) :
102         ScStrCollection( nInit, nDeltaP, sal_False )
103 {
104 	//	read from configuration - "convert.ini" is no longer used
105 	//!	config item as member to allow change of values
106 
107 	ScLinkConfigItem aConfigItem( OUString::createFromAscii( CFGPATH_UNIT ) );
108 
109 	// empty node name -> use the config item's path itself
110     OUString aEmptyString;
111 	Sequence<OUString> aNodeNames = aConfigItem.GetNodeNames( aEmptyString );
112 
113 	long nNodeCount = aNodeNames.getLength();
114 	if ( nNodeCount )
115 	{
116 		const OUString* pNodeArray = aNodeNames.getConstArray();
117 		Sequence<OUString> aValNames( nNodeCount * 3 );
118 		OUString* pValNameArray = aValNames.getArray();
119 		const OUString sSlash('/');
120 
121 		long nIndex = 0;
122 		for (long i=0; i<nNodeCount; i++)
123 		{
124 			OUString sPrefix = pNodeArray[i];
125 			sPrefix += sSlash;
126 
127 			pValNameArray[nIndex] = sPrefix;
128 			pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_FROM );
129 			pValNameArray[nIndex] = sPrefix;
130 			pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_TO );
131 			pValNameArray[nIndex] = sPrefix;
132 			pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_FACTOR );
133 		}
134 
135 		Sequence<Any> aProperties = aConfigItem.GetProperties(aValNames);
136 
137 		if (aProperties.getLength() == aValNames.getLength())
138 		{
139 			const Any* pProperties = aProperties.getConstArray();
140 
141 			OUString sFromUnit;
142 			OUString sToUnit;
143 			double fFactor = 0;
144 
145 			nIndex = 0;
146 			for (long i=0; i<nNodeCount; i++)
147 			{
148 				pProperties[nIndex++] >>= sFromUnit;
149 				pProperties[nIndex++] >>= sToUnit;
150 				pProperties[nIndex++] >>= fFactor;
151 
152 				ScUnitConverterData* pNew = new ScUnitConverterData( sFromUnit, sToUnit, fFactor );
153 				if ( !Insert( pNew ) )
154 					delete pNew;
155 			}
156 		}
157 	}
158 }
159 
GetValue(double & fValue,const String & rFromUnit,const String & rToUnit) const160 sal_Bool ScUnitConverter::GetValue( double& fValue, const String& rFromUnit,
161 				const String& rToUnit ) const
162 {
163 	ScUnitConverterData aSearch( rFromUnit, rToUnit );
164 	sal_uInt16 nIndex;
165 	if ( Search( &aSearch, nIndex ) )
166 	{
167 		fValue = ((const ScUnitConverterData*)(At( nIndex )))->GetValue();
168 		return sal_True;
169 	}
170 	fValue = 1.0;
171 	return sal_False;
172 }
173 
174 
175