bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / misc / numbers.cxx
blob29b460641b9852841d4cfcc4553366b91291860c
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 <comphelper/numbers.hxx>
21 #include <osl/diagnose.h>
22 #include <com/sun/star/util/NumberFormat.hpp>
23 #include <com/sun/star/util/XNumberFormatTypes.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/lang/Locale.hpp>
28 namespace comphelper
31 sal_Int16 getNumberFormatType(const css::uno::Reference<css::util::XNumberFormats>& xFormats, sal_Int32 nKey)
33 sal_Int16 nReturn(css::util::NumberFormat::UNDEFINED);
34 if (xFormats.is())
36 try
38 css::uno::Reference<css::beans::XPropertySet> xFormat(xFormats->getByKey(nKey));
39 if (xFormat.is())
40 xFormat->getPropertyValue("Type") >>= nReturn;
42 catch(...)
44 OSL_TRACE("getNumberFormatType : invalid key! (maybe created with another formatter ?)");
47 return nReturn;
51 sal_Int16 getNumberFormatType(const css::uno::Reference<css::util::XNumberFormatter>& xFormatter, sal_Int32 nKey)
53 OSL_ENSURE(xFormatter.is(), "getNumberFormatType : the formatter isn't valid !");
54 css::uno::Reference<css::util::XNumberFormatsSupplier> xSupplier( xFormatter->getNumberFormatsSupplier());
55 OSL_ENSURE(xSupplier.is(), "getNumberFormatType : the formatter doesn't implement a supplier !");
56 css::uno::Reference<css::util::XNumberFormats> xFormats( xSupplier->getNumberFormats());
57 return getNumberFormatType(xFormats, nKey);
61 css::uno::Any getNumberFormatDecimals(const css::uno::Reference<css::util::XNumberFormats>& xFormats, sal_Int32 nKey)
63 if (xFormats.is())
65 try
67 css::uno::Reference<css::beans::XPropertySet> xFormat( xFormats->getByKey(nKey));
68 if (xFormat.is())
70 return xFormat->getPropertyValue( "Decimals" );
73 catch(...)
75 OSL_TRACE("getNumberFormatDecimals : invalid key! (may be created with another formatter ?)");
78 return css::uno::makeAny((sal_Int16)0);
83 sal_Int32 getStandardFormat(
84 const css::uno::Reference<css::util::XNumberFormatter>& xFormatter,
85 sal_Int16 nType,
86 const css::lang::Locale& _rLocale)
88 css::uno::Reference<css::util::XNumberFormatsSupplier> xSupplier( xFormatter.is() ? xFormatter->getNumberFormatsSupplier() : css::uno::Reference<css::util::XNumberFormatsSupplier>(NULL));
89 css::uno::Reference<css::util::XNumberFormats> xFormats( xSupplier.is() ? xSupplier->getNumberFormats() : css::uno::Reference<css::util::XNumberFormats>(NULL));
90 css::uno::Reference<css::util::XNumberFormatTypes> xTypes(xFormats, css::uno::UNO_QUERY);
91 OSL_ENSURE(xTypes.is(), "getStandardFormat : no format types !");
93 return xTypes.is() ? xTypes->getStandardFormat(nType, _rLocale) : 0;
97 using namespace ::com::sun::star::uno;
98 using namespace ::com::sun::star::util;
99 using namespace ::com::sun::star::beans;
102 Any getNumberFormatProperty( const Reference< XNumberFormatter >& _rxFormatter, sal_Int32 _nKey, const OUString& _rPropertyName )
104 Any aReturn;
106 OSL_ENSURE( _rxFormatter.is() && !_rPropertyName.isEmpty(), "getNumberFormatProperty: invalid arguments!" );
109 Reference< XNumberFormatsSupplier > xSupplier;
110 Reference< XNumberFormats > xFormats;
111 Reference< XPropertySet > xFormatProperties;
113 if ( _rxFormatter.is() )
114 xSupplier = _rxFormatter->getNumberFormatsSupplier();
115 if ( xSupplier.is() )
116 xFormats = xSupplier->getNumberFormats();
117 if ( xFormats.is() )
118 xFormatProperties = xFormats->getByKey( _nKey );
120 if ( xFormatProperties.is() )
121 aReturn = xFormatProperties->getPropertyValue( _rPropertyName );
123 catch( const Exception& )
125 OSL_FAIL( "::getNumberFormatProperty: caught an exception (did you create the key with another formatter?)!" );
128 return aReturn;
132 } // namespace comphelper
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */