1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unitconv.cxx,v $
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"
41 #include "viewopti.hxx" //! move ScLinkConfigItem to separate header!
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
)
61 ScUnitConverterData::BuildIndexString( aTmp
, rFromUnit
, rToUnit
);
66 ScUnitConverterData::ScUnitConverterData( const ScUnitConverterData
& r
)
74 ScDataObject
* ScUnitConverterData::Clone() const
76 return new ScUnitConverterData( *this );
81 void ScUnitConverterData::BuildIndexString( String
& rStr
,
82 const String
& rFromUnit
, const String
& rToUnit
)
92 String
aTo( rToUnit
);
93 ScGlobal::pCharClass
->toUpper( rStr
);
94 ScGlobal::pCharClass
->toUpper( aTo
);
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();
123 const OUString
* pNodeArray
= aNodeNames
.getConstArray();
124 Sequence
<OUString
> aValNames( nNodeCount
* 3 );
125 OUString
* pValNameArray
= aValNames
.getArray();
126 const OUString
sSlash('/');
129 for (long i
=0; i
<nNodeCount
; i
++)
131 OUString sPrefix
= pNodeArray
[i
];
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();
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
) )
167 BOOL
ScUnitConverter::GetValue( double& fValue
, const String
& rFromUnit
,
168 const String
& rToUnit
) const
170 ScUnitConverterData
aSearch( rFromUnit
, rToUnit
);
172 if ( Search( &aSearch
, nIndex
) )
174 fValue
= ((const ScUnitConverterData
*)(At( nIndex
)))->GetValue();