Line data Source code
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/Any.hxx>
21 : #include <com/sun/star/uno/Sequence.hxx>
22 :
23 : #include "unitconv.hxx"
24 : #include "global.hxx"
25 : #include "viewopti.hxx"
26 :
27 : using namespace utl;
28 : using namespace com::sun::star::uno;
29 :
30 : const sal_Unicode cDelim = 0x01; // Delimiter zwischen From und To
31 :
32 : // ScUnitConverterData
33 0 : ScUnitConverterData::ScUnitConverterData(
34 : const OUString& rFromUnit, const OUString& rToUnit, double fValue ) :
35 : maIndexString(BuildIndexString(rFromUnit, rToUnit)),
36 0 : mfValue(fValue) {}
37 :
38 0 : ScUnitConverterData::ScUnitConverterData( const ScUnitConverterData& r ) :
39 : maIndexString(r.maIndexString),
40 0 : mfValue(r.mfValue) {}
41 :
42 0 : ScUnitConverterData::~ScUnitConverterData() {}
43 :
44 0 : OUString ScUnitConverterData::BuildIndexString(
45 : const OUString& rFromUnit, const OUString& rToUnit )
46 : {
47 0 : OUStringBuffer aBuf(rFromUnit);
48 0 : aBuf.append(cDelim);
49 0 : aBuf.append(rToUnit);
50 0 : return aBuf.makeStringAndClear();
51 : }
52 :
53 : // ScUnitConverter
54 : #define CFGPATH_UNIT "Office.Calc/UnitConversion"
55 : #define CFGSTR_UNIT_FROM "FromUnit"
56 : #define CFGSTR_UNIT_TO "ToUnit"
57 : #define CFGSTR_UNIT_FACTOR "Factor"
58 :
59 0 : ScUnitConverter::ScUnitConverter()
60 : {
61 : // read from configuration - "convert.ini" is no longer used
62 : //! config item as member to allow change of values
63 :
64 0 : ScLinkConfigItem aConfigItem( OUString( CFGPATH_UNIT ) );
65 :
66 : // empty node name -> use the config item's path itself
67 0 : OUString aEmptyString;
68 0 : Sequence<OUString> aNodeNames = aConfigItem.GetNodeNames( aEmptyString );
69 :
70 0 : long nNodeCount = aNodeNames.getLength();
71 0 : if ( nNodeCount )
72 : {
73 0 : const OUString* pNodeArray = aNodeNames.getConstArray();
74 0 : Sequence<OUString> aValNames( nNodeCount * 3 );
75 0 : OUString* pValNameArray = aValNames.getArray();
76 0 : const OUString sSlash('/');
77 :
78 0 : long nIndex = 0;
79 0 : for (long i=0; i<nNodeCount; i++)
80 : {
81 0 : OUString sPrefix = pNodeArray[i];
82 0 : sPrefix += sSlash;
83 :
84 0 : pValNameArray[nIndex] = sPrefix;
85 0 : pValNameArray[nIndex++] += OUString( CFGSTR_UNIT_FROM );
86 0 : pValNameArray[nIndex] = sPrefix;
87 0 : pValNameArray[nIndex++] += OUString( CFGSTR_UNIT_TO );
88 0 : pValNameArray[nIndex] = sPrefix;
89 0 : pValNameArray[nIndex++] += OUString( CFGSTR_UNIT_FACTOR );
90 0 : }
91 :
92 0 : Sequence<Any> aProperties = aConfigItem.GetProperties(aValNames);
93 :
94 0 : if (aProperties.getLength() == aValNames.getLength())
95 : {
96 0 : const Any* pProperties = aProperties.getConstArray();
97 :
98 0 : OUString sFromUnit;
99 0 : OUString sToUnit;
100 0 : double fFactor = 0;
101 :
102 0 : nIndex = 0;
103 0 : for (long i=0; i<nNodeCount; i++)
104 : {
105 0 : pProperties[nIndex++] >>= sFromUnit;
106 0 : pProperties[nIndex++] >>= sToUnit;
107 0 : pProperties[nIndex++] >>= fFactor;
108 :
109 0 : ScUnitConverterData* pNew = new ScUnitConverterData( sFromUnit, sToUnit, fFactor );
110 0 : OUString aIndex = pNew->GetIndexString();
111 0 : maData.insert(aIndex, pNew);
112 0 : }
113 0 : }
114 0 : }
115 0 : }
116 :
117 0 : ScUnitConverter::~ScUnitConverter() {}
118 :
119 0 : bool ScUnitConverter::GetValue(
120 : double& fValue, const OUString& rFromUnit, const OUString& rToUnit ) const
121 : {
122 0 : OUString aIndex = ScUnitConverterData::BuildIndexString(rFromUnit, rToUnit);
123 0 : MapType::const_iterator it = maData.find(aIndex);
124 0 : if (it == maData.end())
125 : {
126 0 : fValue = 1.0;
127 0 : return false;
128 : }
129 :
130 0 : fValue = it->second->GetValue();
131 0 : return true;
132 228 : }
133 :
134 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|