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