2000-09-18 23:16:46 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2005-09-08 17:51:48 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2000-09-18 23:16:46 +00:00
|
|
|
*
|
2005-09-08 17:51:48 +00:00
|
|
|
* $RCSfile: unitconv.cxx,v $
|
2000-09-18 23:16:46 +00:00
|
|
|
*
|
2005-09-08 17:51:48 +00:00
|
|
|
* $Revision: 1.4 $
|
2000-09-18 23:16:46 +00:00
|
|
|
*
|
2005-09-08 17:51:48 +00:00
|
|
|
* last change: $Author: rt $ $Date: 2005-09-08 18:51:48 $
|
2000-09-18 23:16:46 +00:00
|
|
|
*
|
2005-09-08 17:51:48 +00:00
|
|
|
* The Contents of this file are made available subject to
|
|
|
|
* the terms of GNU Lesser General Public License Version 2.1.
|
2000-09-18 23:16:46 +00:00
|
|
|
*
|
|
|
|
*
|
2005-09-08 17:51:48 +00:00
|
|
|
* GNU Lesser General Public License Version 2.1
|
|
|
|
* =============================================
|
|
|
|
* Copyright 2005 by Sun Microsystems, Inc.
|
|
|
|
* 901 San Antonio Road, Palo Alto, CA 94303, USA
|
2000-09-18 23:16:46 +00:00
|
|
|
*
|
2005-09-08 17:51:48 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software Foundation.
|
2000-09-18 23:16:46 +00:00
|
|
|
*
|
2005-09-08 17:51:48 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2000-09-18 23:16:46 +00:00
|
|
|
*
|
2005-09-08 17:51:48 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
2000-09-18 23:16:46 +00:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#ifdef PCH
|
|
|
|
#include "core_pch.hxx"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#pragma hdrstop
|
|
|
|
|
2000-09-22 06:56:13 +00:00
|
|
|
#include <com/sun/star/uno/Any.hxx>
|
|
|
|
#include <com/sun/star/uno/Sequence.hxx>
|
|
|
|
|
2000-09-18 23:16:46 +00:00
|
|
|
#include "unitconv.hxx"
|
|
|
|
#include "global.hxx"
|
2000-09-22 06:56:13 +00:00
|
|
|
#include "viewopti.hxx" //! move ScLinkConfigItem to separate header!
|
|
|
|
|
|
|
|
using namespace utl;
|
|
|
|
using namespace rtl;
|
|
|
|
using namespace com::sun::star::uno;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
2000-09-18 23:16:46 +00:00
|
|
|
|
|
|
|
const sal_Unicode cDelim = 0x01; // Delimiter zwischen From und To
|
|
|
|
|
|
|
|
|
|
|
|
// --- ScUnitConverterData --------------------------------------------
|
|
|
|
|
|
|
|
ScUnitConverterData::ScUnitConverterData( const String& rFromUnit,
|
|
|
|
const String& rToUnit, double fVal )
|
|
|
|
:
|
|
|
|
StrData( rFromUnit ),
|
|
|
|
fValue( fVal )
|
|
|
|
{
|
|
|
|
String aTmp;
|
|
|
|
ScUnitConverterData::BuildIndexString( aTmp, rFromUnit, rToUnit );
|
|
|
|
SetString( aTmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ScUnitConverterData::ScUnitConverterData( const ScUnitConverterData& r )
|
|
|
|
:
|
|
|
|
StrData( r ),
|
|
|
|
fValue( r.fValue )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DataObject* ScUnitConverterData::Clone() const
|
|
|
|
{
|
|
|
|
return new ScUnitConverterData( *this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// static
|
|
|
|
void ScUnitConverterData::BuildIndexString( String& rStr,
|
|
|
|
const String& rFromUnit, const String& rToUnit )
|
|
|
|
{
|
|
|
|
#if 1
|
|
|
|
// case sensitive
|
|
|
|
rStr = rFromUnit;
|
|
|
|
rStr += cDelim;
|
|
|
|
rStr += rToUnit;
|
|
|
|
#else
|
|
|
|
// not case sensitive
|
|
|
|
rStr = rFromUnit;
|
|
|
|
String aTo( rToUnit );
|
|
|
|
ScGlobal::pCharClass->toUpper( rStr );
|
|
|
|
ScGlobal::pCharClass->toUpper( aTo );
|
|
|
|
rStr += cDelim;
|
|
|
|
rStr += aTo;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --- 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"
|
|
|
|
|
|
|
|
ScUnitConverter::ScUnitConverter( USHORT nInit, USHORT nDelta ) :
|
2000-09-18 23:16:46 +00:00
|
|
|
StrCollection( nInit, nDelta, FALSE )
|
|
|
|
{
|
2000-09-22 06:56:13 +00:00
|
|
|
// read from configuration - "convert.ini" is no longer used
|
|
|
|
//! config item as member to allow change of values
|
2000-09-18 23:16:46 +00:00
|
|
|
|
2000-09-22 06:56:13 +00:00
|
|
|
ScLinkConfigItem aConfigItem( OUString::createFromAscii( 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;
|
|
|
|
|
|
|
|
pValNameArray[nIndex] = sPrefix;
|
|
|
|
pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_FROM );
|
|
|
|
pValNameArray[nIndex] = sPrefix;
|
|
|
|
pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_TO );
|
|
|
|
pValNameArray[nIndex] = sPrefix;
|
|
|
|
pValNameArray[nIndex++] += OUString::createFromAscii( CFGSTR_UNIT_FACTOR );
|
|
|
|
}
|
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;
|
|
|
|
double fFactor;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
ScUnitConverterData* pNew = new ScUnitConverterData( sFromUnit, sToUnit, fFactor );
|
|
|
|
if ( !Insert( pNew ) )
|
|
|
|
delete pNew;
|
2000-09-18 23:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ScUnitConverter::GetValue( double& fValue, const String& rFromUnit,
|
|
|
|
const String& rToUnit ) const
|
|
|
|
{
|
|
|
|
ScUnitConverterData aSearch( rFromUnit, rToUnit );
|
|
|
|
USHORT nIndex;
|
|
|
|
if ( Search( &aSearch, nIndex ) )
|
|
|
|
{
|
|
|
|
fValue = ((const ScUnitConverterData*)(At( nIndex )))->GetValue();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
fValue = 1.0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|