/src/libreoffice/comphelper/source/misc/numbers.cxx
Line | Count | Source |
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 <comphelper/numbers.hxx> |
21 | | #include <osl/diagnose.h> |
22 | | #include <sal/log.hxx> |
23 | | #include <com/sun/star/util/NumberFormat.hpp> |
24 | | #include <com/sun/star/util/XNumberFormatter.hpp> |
25 | | #include <com/sun/star/beans/XPropertySet.hpp> |
26 | | |
27 | | |
28 | | namespace comphelper |
29 | | { |
30 | | |
31 | | sal_Int16 getNumberFormatType(const css::uno::Reference<css::util::XNumberFormats>& xFormats, sal_Int32 nKey) |
32 | 0 | { |
33 | 0 | sal_Int16 nReturn(css::util::NumberFormat::UNDEFINED); |
34 | 0 | if (xFormats.is()) |
35 | 0 | { |
36 | 0 | try |
37 | 0 | { |
38 | 0 | css::uno::Reference<css::beans::XPropertySet> xFormat(xFormats->getByKey(nKey)); |
39 | 0 | if (xFormat.is()) |
40 | 0 | xFormat->getPropertyValue(u"Type"_ustr) >>= nReturn; |
41 | 0 | } |
42 | 0 | catch(...) |
43 | 0 | { |
44 | 0 | SAL_WARN("comphelper", "getNumberFormatType : invalid key! (maybe created with another formatter ?)"); |
45 | 0 | } |
46 | 0 | } |
47 | 0 | return nReturn; |
48 | 0 | } |
49 | | |
50 | | |
51 | | sal_Int16 getNumberFormatType(const css::uno::Reference<css::util::XNumberFormatter>& xFormatter, sal_Int32 nKey) |
52 | 0 | { |
53 | 0 | OSL_ENSURE(xFormatter.is(), "getNumberFormatType : the formatter isn't valid !"); |
54 | 0 | css::uno::Reference<css::util::XNumberFormatsSupplier> xSupplier( xFormatter->getNumberFormatsSupplier()); |
55 | 0 | OSL_ENSURE(xSupplier.is(), "getNumberFormatType : the formatter doesn't implement a supplier !"); |
56 | 0 | css::uno::Reference<css::util::XNumberFormats> xFormats( xSupplier->getNumberFormats()); |
57 | 0 | return getNumberFormatType(xFormats, nKey); |
58 | 0 | } |
59 | | |
60 | | |
61 | | css::uno::Any getNumberFormatDecimals(const css::uno::Reference<css::util::XNumberFormats>& xFormats, sal_Int32 nKey) |
62 | 0 | { |
63 | 0 | if (xFormats.is()) |
64 | 0 | { |
65 | 0 | try |
66 | 0 | { |
67 | 0 | css::uno::Reference<css::beans::XPropertySet> xFormat( xFormats->getByKey(nKey)); |
68 | 0 | if (xFormat.is()) |
69 | 0 | { |
70 | 0 | return xFormat->getPropertyValue( u"Decimals"_ustr ); |
71 | 0 | } |
72 | 0 | } |
73 | 0 | catch(...) |
74 | 0 | { |
75 | 0 | SAL_WARN("comphelper", "getNumberFormatDecimals : invalid key! (may be created with another formatter ?)"); |
76 | 0 | } |
77 | 0 | } |
78 | 0 | return css::uno::Any(sal_Int16(0)); |
79 | 0 | } |
80 | | |
81 | | |
82 | | using namespace ::com::sun::star::uno; |
83 | | using namespace ::com::sun::star::util; |
84 | | using namespace ::com::sun::star::beans; |
85 | | |
86 | | |
87 | | Any getNumberFormatProperty( const Reference< XNumberFormatter >& _rxFormatter, sal_Int32 _nKey, const OUString& _rPropertyName ) |
88 | 0 | { |
89 | 0 | Any aReturn; |
90 | |
|
91 | 0 | OSL_ENSURE( _rxFormatter.is() && !_rPropertyName.isEmpty(), "getNumberFormatProperty: invalid arguments!" ); |
92 | 0 | try |
93 | 0 | { |
94 | 0 | Reference< XNumberFormatsSupplier > xSupplier; |
95 | 0 | Reference< XNumberFormats > xFormats; |
96 | 0 | Reference< XPropertySet > xFormatProperties; |
97 | |
|
98 | 0 | if ( _rxFormatter.is() ) |
99 | 0 | xSupplier = _rxFormatter->getNumberFormatsSupplier(); |
100 | 0 | if ( xSupplier.is() ) |
101 | 0 | xFormats = xSupplier->getNumberFormats(); |
102 | 0 | if ( xFormats.is() ) |
103 | 0 | xFormatProperties = xFormats->getByKey( _nKey ); |
104 | |
|
105 | 0 | if ( xFormatProperties.is() ) |
106 | 0 | aReturn = xFormatProperties->getPropertyValue( _rPropertyName ); |
107 | 0 | } |
108 | 0 | catch( const Exception& ) |
109 | 0 | { |
110 | 0 | OSL_FAIL( "::getNumberFormatProperty: caught an exception (did you create the key with another formatter?)!" ); |
111 | 0 | } |
112 | |
|
113 | 0 | return aReturn; |
114 | 0 | } |
115 | | |
116 | | |
117 | | } // namespace comphelper |
118 | | |
119 | | |
120 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |