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