1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <com/sun/star/uno/Sequence.hxx>
21 
22 #include <unitconv.hxx>
23 #include <global.hxx>
24 #include <optutil.hxx>
25 
26 using namespace utl;
27 using namespace com::sun::star::uno;
28 
29 const sal_Unicode cDelim = 0x01;        // delimiter between From and To
30 
31 // ScUnitConverterData
ScUnitConverterData(std::u16string_view rFromUnit,std::u16string_view rToUnit,double fValue)32 ScUnitConverterData::ScUnitConverterData(
33     std::u16string_view rFromUnit, std::u16string_view rToUnit, double fValue ) :
34     maIndexString(BuildIndexString(rFromUnit, rToUnit)),
35     mfValue(fValue) {}
36 
BuildIndexString(std::u16string_view rFromUnit,std::u16string_view rToUnit)37 OUString ScUnitConverterData::BuildIndexString(
38     std::u16string_view rFromUnit, std::u16string_view rToUnit )
39 {
40     return rFromUnit + OUStringChar(cDelim) + rToUnit;
41 }
42 
43 // ScUnitConverter
44 #define CFGPATH_UNIT        "Office.Calc/UnitConversion"
45 #define CFGSTR_UNIT_FROM    "FromUnit"
46 #define CFGSTR_UNIT_TO      "ToUnit"
47 #define CFGSTR_UNIT_FACTOR  "Factor"
48 
ScUnitConverter()49 ScUnitConverter::ScUnitConverter()
50 {
51     // read from configuration - "convert.ini" is no longer used
52     //TODO: config item as member to allow change of values
53 
54     ScLinkConfigItem aConfigItem( CFGPATH_UNIT );
55 
56     // empty node name -> use the config item's path itself
57     const Sequence<OUString> aNodeNames = aConfigItem.GetNodeNames( "" );
58 
59     tools::Long nNodeCount = aNodeNames.getLength();
60     if ( !nNodeCount )
61         return;
62 
63     Sequence<OUString> aValNames( nNodeCount * 3 );
64     OUString* pValNameArray = aValNames.getArray();
65     const OUString sSlash('/');
66 
67     tools::Long nIndex = 0;
68     for (const OUString& rNode : aNodeNames)
69     {
70         OUString sPrefix = rNode + sSlash;
71 
72         pValNameArray[nIndex++] = sPrefix + CFGSTR_UNIT_FROM;
73         pValNameArray[nIndex++] = sPrefix + CFGSTR_UNIT_TO;
74         pValNameArray[nIndex++] = sPrefix + CFGSTR_UNIT_FACTOR;
75     }
76 
77     Sequence<Any> aProperties = aConfigItem.GetProperties(aValNames);
78 
79     if (aProperties.getLength() != aValNames.getLength())
80         return;
81 
82     const Any* pProperties = aProperties.getConstArray();
83 
84     OUString sFromUnit;
85     OUString sToUnit;
86     double fFactor = 0;
87 
88     nIndex = 0;
89     for (tools::Long i=0; i<nNodeCount; i++)
90     {
91         pProperties[nIndex++] >>= sFromUnit;
92         pProperties[nIndex++] >>= sToUnit;
93         pProperties[nIndex++] >>= fFactor;
94 
95         ScUnitConverterData aNew(sFromUnit, sToUnit, fFactor);
96         OUString const aIndex = aNew.GetIndexString();
97         maData.insert(std::make_pair(aIndex, aNew));
98     }
99 }
100 
~ScUnitConverter()101 ScUnitConverter::~ScUnitConverter() {}
102 
GetValue(double & fValue,std::u16string_view rFromUnit,std::u16string_view rToUnit) const103 bool ScUnitConverter::GetValue(
104     double& fValue, std::u16string_view rFromUnit, std::u16string_view rToUnit ) const
105 {
106     OUString aIndex = ScUnitConverterData::BuildIndexString(rFromUnit, rToUnit);
107     MapType::const_iterator it = maData.find(aIndex);
108     if (it == maData.end())
109     {
110         fValue = 1.0;
111         return false;
112     }
113 
114     fValue = it->second.GetValue();
115     return true;
116 }
117 
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
119