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 : double ScUnitConverterData::GetValue() const
45 : {
46 0 : return mfValue;
47 : }
48 :
49 0 : const OUString& ScUnitConverterData::GetIndexString() const
50 : {
51 0 : return maIndexString;
52 : }
53 :
54 0 : OUString ScUnitConverterData::BuildIndexString(
55 : const OUString& rFromUnit, const OUString& rToUnit )
56 : {
57 0 : OUStringBuffer aBuf(rFromUnit);
58 0 : aBuf.append(cDelim);
59 0 : aBuf.append(rToUnit);
60 0 : return aBuf.makeStringAndClear();
61 : }
62 :
63 : // ScUnitConverter
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"
68 :
69 0 : ScUnitConverter::ScUnitConverter()
70 : {
71 : // read from configuration - "convert.ini" is no longer used
72 : //! config item as member to allow change of values
73 :
74 0 : ScLinkConfigItem aConfigItem( OUString( CFGPATH_UNIT ) );
75 :
76 : // empty node name -> use the config item's path itself
77 0 : OUString aEmptyString;
78 0 : Sequence<OUString> aNodeNames = aConfigItem.GetNodeNames( aEmptyString );
79 :
80 0 : long nNodeCount = aNodeNames.getLength();
81 0 : if ( nNodeCount )
82 : {
83 0 : const OUString* pNodeArray = aNodeNames.getConstArray();
84 0 : Sequence<OUString> aValNames( nNodeCount * 3 );
85 0 : OUString* pValNameArray = aValNames.getArray();
86 0 : const OUString sSlash('/');
87 :
88 0 : long nIndex = 0;
89 0 : for (long i=0; i<nNodeCount; i++)
90 : {
91 0 : OUString sPrefix = pNodeArray[i];
92 0 : sPrefix += sSlash;
93 :
94 0 : pValNameArray[nIndex] = sPrefix;
95 0 : pValNameArray[nIndex++] += OUString( CFGSTR_UNIT_FROM );
96 0 : pValNameArray[nIndex] = sPrefix;
97 0 : pValNameArray[nIndex++] += OUString( CFGSTR_UNIT_TO );
98 0 : pValNameArray[nIndex] = sPrefix;
99 0 : pValNameArray[nIndex++] += OUString( CFGSTR_UNIT_FACTOR );
100 0 : }
101 :
102 0 : Sequence<Any> aProperties = aConfigItem.GetProperties(aValNames);
103 :
104 0 : if (aProperties.getLength() == aValNames.getLength())
105 : {
106 0 : const Any* pProperties = aProperties.getConstArray();
107 :
108 0 : OUString sFromUnit;
109 0 : OUString sToUnit;
110 0 : double fFactor = 0;
111 :
112 0 : nIndex = 0;
113 0 : for (long i=0; i<nNodeCount; i++)
114 : {
115 0 : pProperties[nIndex++] >>= sFromUnit;
116 0 : pProperties[nIndex++] >>= sToUnit;
117 0 : pProperties[nIndex++] >>= fFactor;
118 :
119 0 : ScUnitConverterData* pNew = new ScUnitConverterData( sFromUnit, sToUnit, fFactor );
120 0 : OUString aIndex = pNew->GetIndexString();
121 0 : maData.insert(aIndex, pNew);
122 0 : }
123 0 : }
124 0 : }
125 0 : }
126 :
127 0 : ScUnitConverter::~ScUnitConverter() {}
128 :
129 0 : bool ScUnitConverter::GetValue(
130 : double& fValue, const OUString& rFromUnit, const OUString& rToUnit ) const
131 : {
132 0 : OUString aIndex = ScUnitConverterData::BuildIndexString(rFromUnit, rToUnit);
133 0 : MapType::const_iterator it = maData.find(aIndex);
134 0 : if (it == maData.end())
135 : {
136 0 : fValue = 1.0;
137 0 : return false;
138 : }
139 :
140 0 : fValue = it->second->GetValue();
141 0 : return true;
142 0 : }
143 :
144 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|