1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/uno/Any.hxx>
21 #include <com/sun/star/uno/Sequence.hxx>
23 #include "unitconv.hxx"
25 #include "viewopti.hxx"
28 using namespace com::sun::star::uno
;
30 const sal_Unicode cDelim
= 0x01; // Delimiter zwischen From und To
32 // ScUnitConverterData
33 ScUnitConverterData::ScUnitConverterData(
34 const OUString
& rFromUnit
, const OUString
& rToUnit
, double fValue
) :
35 maIndexString(BuildIndexString(rFromUnit
, rToUnit
)),
38 ScUnitConverterData::ScUnitConverterData( const ScUnitConverterData
& r
) :
39 maIndexString(r
.maIndexString
),
42 ScUnitConverterData::~ScUnitConverterData() {}
44 double ScUnitConverterData::GetValue() const
49 const OUString
& ScUnitConverterData::GetIndexString() const
54 OUString
ScUnitConverterData::BuildIndexString(
55 const OUString
& rFromUnit
, const OUString
& rToUnit
)
57 OUStringBuffer
aBuf(rFromUnit
);
60 return aBuf
.makeStringAndClear();
64 #define CFGPATH_UNIT "Office.Calc/UnitConversion"
65 #define CFGSTR_UNIT_FROM "FromUnit"
66 #define CFGSTR_UNIT_TO "ToUnit"
67 #define CFGSTR_UNIT_FACTOR "Factor"
69 ScUnitConverter::ScUnitConverter()
71 // read from configuration - "convert.ini" is no longer used
72 //! config item as member to allow change of values
74 ScLinkConfigItem
aConfigItem( OUString( CFGPATH_UNIT
) );
76 // empty node name -> use the config item's path itself
77 OUString aEmptyString
;
78 Sequence
<OUString
> aNodeNames
= aConfigItem
.GetNodeNames( aEmptyString
);
80 long nNodeCount
= aNodeNames
.getLength();
83 const OUString
* pNodeArray
= aNodeNames
.getConstArray();
84 Sequence
<OUString
> aValNames( nNodeCount
* 3 );
85 OUString
* pValNameArray
= aValNames
.getArray();
86 const OUString
sSlash('/');
89 for (long i
=0; i
<nNodeCount
; i
++)
91 OUString sPrefix
= pNodeArray
[i
];
94 pValNameArray
[nIndex
] = sPrefix
;
95 pValNameArray
[nIndex
++] += OUString( CFGSTR_UNIT_FROM
);
96 pValNameArray
[nIndex
] = sPrefix
;
97 pValNameArray
[nIndex
++] += OUString( CFGSTR_UNIT_TO
);
98 pValNameArray
[nIndex
] = sPrefix
;
99 pValNameArray
[nIndex
++] += OUString( CFGSTR_UNIT_FACTOR
);
102 Sequence
<Any
> aProperties
= aConfigItem
.GetProperties(aValNames
);
104 if (aProperties
.getLength() == aValNames
.getLength())
106 const Any
* pProperties
= aProperties
.getConstArray();
113 for (long i
=0; i
<nNodeCount
; i
++)
115 pProperties
[nIndex
++] >>= sFromUnit
;
116 pProperties
[nIndex
++] >>= sToUnit
;
117 pProperties
[nIndex
++] >>= fFactor
;
119 ScUnitConverterData
* pNew
= new ScUnitConverterData( sFromUnit
, sToUnit
, fFactor
);
120 OUString aIndex
= pNew
->GetIndexString();
121 maData
.insert(aIndex
, pNew
);
127 ScUnitConverter::~ScUnitConverter() {}
129 bool ScUnitConverter::GetValue(
130 double& fValue
, const OUString
& rFromUnit
, const OUString
& rToUnit
) const
132 OUString aIndex
= ScUnitConverterData::BuildIndexString(rFromUnit
, rToUnit
);
133 MapType::const_iterator it
= maData
.find(aIndex
);
134 if (it
== maData
.end())
140 fValue
= it
->second
->GetValue();
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */