2010-10-12 15:59:00 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-11-30 12:23:25 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2000-09-18 23:16:46 +00:00
|
|
|
|
2000-09-22 06:56:13 +00:00
|
|
|
#include <com/sun/star/uno/Sequence.hxx>
|
|
|
|
|
2000-09-18 23:16:46 +00:00
|
|
|
#include "unitconv.hxx"
|
|
|
|
#include "global.hxx"
|
2013-10-22 15:58:57 +03:00
|
|
|
#include "viewopti.hxx"
|
2000-09-22 06:56:13 +00:00
|
|
|
|
|
|
|
using namespace utl;
|
|
|
|
using namespace com::sun::star::uno;
|
|
|
|
|
2000-09-18 23:16:46 +00:00
|
|
|
const sal_Unicode cDelim = 0x01; // Delimiter zwischen From und To
|
|
|
|
|
2013-09-05 17:39:16 +02:00
|
|
|
// ScUnitConverterData
|
2012-02-01 21:30:57 -05:00
|
|
|
ScUnitConverterData::ScUnitConverterData(
|
2013-02-28 19:11:16 +01:00
|
|
|
const OUString& rFromUnit, const OUString& rToUnit, double fValue ) :
|
2012-02-01 21:30:57 -05:00
|
|
|
maIndexString(BuildIndexString(rFromUnit, rToUnit)),
|
|
|
|
mfValue(fValue) {}
|
2000-09-18 23:16:46 +00:00
|
|
|
|
2012-02-01 21:30:57 -05:00
|
|
|
ScUnitConverterData::ScUnitConverterData( const ScUnitConverterData& r ) :
|
|
|
|
maIndexString(r.maIndexString),
|
|
|
|
mfValue(r.mfValue) {}
|
2000-09-18 23:16:46 +00:00
|
|
|
|
2012-02-01 21:30:57 -05:00
|
|
|
ScUnitConverterData::~ScUnitConverterData() {}
|
|
|
|
|
2013-02-28 19:11:16 +01:00
|
|
|
OUString ScUnitConverterData::BuildIndexString(
|
|
|
|
const OUString& rFromUnit, const OUString& rToUnit )
|
2000-09-18 23:16:46 +00:00
|
|
|
{
|
2013-02-28 19:11:16 +01:00
|
|
|
OUStringBuffer aBuf(rFromUnit);
|
2012-02-01 21:30:57 -05:00
|
|
|
aBuf.append(cDelim);
|
|
|
|
aBuf.append(rToUnit);
|
|
|
|
return aBuf.makeStringAndClear();
|
2000-09-18 23:16:46 +00:00
|
|
|
}
|
|
|
|
|
2013-09-05 17:39:16 +02:00
|
|
|
// ScUnitConverter
|
2000-09-22 06:56:13 +00:00
|
|
|
#define CFGPATH_UNIT "Office.Calc/UnitConversion"
|
|
|
|
#define CFGSTR_UNIT_FROM "FromUnit"
|
|
|
|
#define CFGSTR_UNIT_TO "ToUnit"
|
|
|
|
#define CFGSTR_UNIT_FACTOR "Factor"
|
|
|
|
|
2012-02-01 21:30:57 -05:00
|
|
|
ScUnitConverter::ScUnitConverter()
|
2000-09-18 23:16:46 +00:00
|
|
|
{
|
2014-11-11 04:51:00 -05:00
|
|
|
// read from configuration - "convert.ini" is no longer used
|
|
|
|
//TODO: config item as member to allow change of values
|
2000-09-18 23:16:46 +00:00
|
|
|
|
2013-02-28 19:11:16 +01:00
|
|
|
ScLinkConfigItem aConfigItem( OUString( CFGPATH_UNIT ) );
|
2000-09-18 23:16:46 +00:00
|
|
|
|
2000-09-22 06:56:13 +00:00
|
|
|
// empty node name -> use the config item's path itself
|
2001-10-23 14:32:02 +00:00
|
|
|
OUString aEmptyString;
|
|
|
|
Sequence<OUString> aNodeNames = aConfigItem.GetNodeNames( aEmptyString );
|
2000-09-18 23:16:46 +00:00
|
|
|
|
2000-09-22 06:56:13 +00:00
|
|
|
long nNodeCount = aNodeNames.getLength();
|
|
|
|
if ( nNodeCount )
|
2000-09-18 23:16:46 +00:00
|
|
|
{
|
2000-09-22 06:56:13 +00:00
|
|
|
const OUString* pNodeArray = aNodeNames.getConstArray();
|
|
|
|
Sequence<OUString> aValNames( nNodeCount * 3 );
|
|
|
|
OUString* pValNameArray = aValNames.getArray();
|
|
|
|
const OUString sSlash('/');
|
2000-09-18 23:16:46 +00:00
|
|
|
|
2000-09-22 06:56:13 +00:00
|
|
|
long nIndex = 0;
|
|
|
|
for (long i=0; i<nNodeCount; i++)
|
|
|
|
{
|
|
|
|
OUString sPrefix = pNodeArray[i];
|
|
|
|
sPrefix += sSlash;
|
|
|
|
|
2015-06-26 13:53:58 +02:00
|
|
|
pValNameArray[nIndex++] = sPrefix + CFGSTR_UNIT_FROM;
|
|
|
|
pValNameArray[nIndex++] = sPrefix + CFGSTR_UNIT_TO;
|
|
|
|
pValNameArray[nIndex++] = sPrefix + CFGSTR_UNIT_FACTOR;
|
2000-09-22 06:56:13 +00:00
|
|
|
}
|
2000-09-18 23:16:46 +00:00
|
|
|
|
2000-09-22 06:56:13 +00:00
|
|
|
Sequence<Any> aProperties = aConfigItem.GetProperties(aValNames);
|
2000-09-18 23:16:46 +00:00
|
|
|
|
2000-09-22 06:56:13 +00:00
|
|
|
if (aProperties.getLength() == aValNames.getLength())
|
2000-09-18 23:16:46 +00:00
|
|
|
{
|
2000-09-22 06:56:13 +00:00
|
|
|
const Any* pProperties = aProperties.getConstArray();
|
|
|
|
|
|
|
|
OUString sFromUnit;
|
|
|
|
OUString sToUnit;
|
2007-03-05 13:42:10 +00:00
|
|
|
double fFactor = 0;
|
2000-09-22 06:56:13 +00:00
|
|
|
|
|
|
|
nIndex = 0;
|
|
|
|
for (long i=0; i<nNodeCount; i++)
|
2000-09-18 23:16:46 +00:00
|
|
|
{
|
2000-09-22 06:56:13 +00:00
|
|
|
pProperties[nIndex++] >>= sFromUnit;
|
|
|
|
pProperties[nIndex++] >>= sToUnit;
|
|
|
|
pProperties[nIndex++] >>= fFactor;
|
|
|
|
|
2016-01-12 11:50:32 +01:00
|
|
|
ScUnitConverterData aNew(sFromUnit, sToUnit, fFactor);
|
|
|
|
OUString const aIndex = aNew.GetIndexString();
|
|
|
|
maData.insert(std::make_pair(aIndex, aNew));
|
2000-09-18 23:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 21:30:57 -05:00
|
|
|
ScUnitConverter::~ScUnitConverter() {}
|
|
|
|
|
|
|
|
bool ScUnitConverter::GetValue(
|
2013-02-28 19:11:16 +01:00
|
|
|
double& fValue, const OUString& rFromUnit, const OUString& rToUnit ) const
|
2000-09-18 23:16:46 +00:00
|
|
|
{
|
2013-02-28 19:11:16 +01:00
|
|
|
OUString aIndex = ScUnitConverterData::BuildIndexString(rFromUnit, rToUnit);
|
2012-02-01 21:30:57 -05:00
|
|
|
MapType::const_iterator it = maData.find(aIndex);
|
|
|
|
if (it == maData.end())
|
2000-09-18 23:16:46 +00:00
|
|
|
{
|
2012-02-01 21:30:57 -05:00
|
|
|
fValue = 1.0;
|
|
|
|
return false;
|
2000-09-18 23:16:46 +00:00
|
|
|
}
|
2012-02-01 21:30:57 -05:00
|
|
|
|
2016-01-12 11:50:32 +01:00
|
|
|
fValue = it->second.GetValue();
|
2012-02-01 21:30:57 -05:00
|
|
|
return true;
|
2000-09-18 23:16:46 +00:00
|
|
|
}
|
|
|
|
|
2010-10-12 15:59:00 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|