Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / tool / unitconv.cxx
blob8b84683953512895a9ab37ca68a231e4a06ea2b9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/uno/Any.hxx>
21 #include <com/sun/star/uno/Sequence.hxx>
23 #include "unitconv.hxx"
24 #include "global.hxx"
25 #include "viewopti.hxx"
27 using namespace utl;
28 using namespace com::sun::star::uno;
30 const sal_Unicode cDelim = 0x01; // Delimiter zwischen From und To
32 // ScUnitConverterData
33 ScUnitConverterData::ScUnitConverterData(
34 const OUString& rFromUnit, const OUString& rToUnit, double fValue ) :
35 maIndexString(BuildIndexString(rFromUnit, rToUnit)),
36 mfValue(fValue) {}
38 ScUnitConverterData::ScUnitConverterData( const ScUnitConverterData& r ) :
39 maIndexString(r.maIndexString),
40 mfValue(r.mfValue) {}
42 ScUnitConverterData::~ScUnitConverterData() {}
44 double ScUnitConverterData::GetValue() const
46 return mfValue;
49 const OUString& ScUnitConverterData::GetIndexString() const
51 return maIndexString;
54 OUString ScUnitConverterData::BuildIndexString(
55 const OUString& rFromUnit, const OUString& rToUnit )
57 OUStringBuffer aBuf(rFromUnit);
58 aBuf.append(cDelim);
59 aBuf.append(rToUnit);
60 return aBuf.makeStringAndClear();
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"
69 ScUnitConverter::ScUnitConverter()
71 // read from configuration - "convert.ini" is no longer used
72 //! config item as member to allow change of values
74 ScLinkConfigItem aConfigItem( OUString( CFGPATH_UNIT ) );
76 // empty node name -> use the config item's path itself
77 OUString aEmptyString;
78 Sequence<OUString> aNodeNames = aConfigItem.GetNodeNames( aEmptyString );
80 long nNodeCount = aNodeNames.getLength();
81 if ( nNodeCount )
83 const OUString* pNodeArray = aNodeNames.getConstArray();
84 Sequence<OUString> aValNames( nNodeCount * 3 );
85 OUString* pValNameArray = aValNames.getArray();
86 const OUString sSlash('/');
88 long nIndex = 0;
89 for (long i=0; i<nNodeCount; i++)
91 OUString sPrefix = pNodeArray[i];
92 sPrefix += sSlash;
94 pValNameArray[nIndex] = sPrefix;
95 pValNameArray[nIndex++] += OUString( CFGSTR_UNIT_FROM );
96 pValNameArray[nIndex] = sPrefix;
97 pValNameArray[nIndex++] += OUString( CFGSTR_UNIT_TO );
98 pValNameArray[nIndex] = sPrefix;
99 pValNameArray[nIndex++] += OUString( CFGSTR_UNIT_FACTOR );
102 Sequence<Any> aProperties = aConfigItem.GetProperties(aValNames);
104 if (aProperties.getLength() == aValNames.getLength())
106 const Any* pProperties = aProperties.getConstArray();
108 OUString sFromUnit;
109 OUString sToUnit;
110 double fFactor = 0;
112 nIndex = 0;
113 for (long i=0; i<nNodeCount; i++)
115 pProperties[nIndex++] >>= sFromUnit;
116 pProperties[nIndex++] >>= sToUnit;
117 pProperties[nIndex++] >>= fFactor;
119 ScUnitConverterData* pNew = new ScUnitConverterData( sFromUnit, sToUnit, fFactor );
120 OUString aIndex = pNew->GetIndexString();
121 maData.insert(aIndex, pNew);
127 ScUnitConverter::~ScUnitConverter() {}
129 bool ScUnitConverter::GetValue(
130 double& fValue, const OUString& rFromUnit, const OUString& rToUnit ) const
132 OUString aIndex = ScUnitConverterData::BuildIndexString(rFromUnit, rToUnit);
133 MapType::const_iterator it = maData.find(aIndex);
134 if (it == maData.end())
136 fValue = 1.0;
137 return false;
140 fValue = it->second->GetValue();
141 return true;
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */