Line data Source code
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 <numberformatcode.hxx>
21 : #include <com/sun/star/i18n/KNumberFormatUsage.hpp>
22 : #include <com/sun/star/i18n/KNumberFormatType.hpp>
23 : #include <com/sun/star/i18n/LocaleData.hpp>
24 : #include <cppuhelper/supportsservice.hxx>
25 :
26 31491 : NumberFormatCodeMapper::NumberFormatCodeMapper(
27 : const ::com::sun::star::uno::Reference <
28 : ::com::sun::star::uno::XComponentContext >& rxContext )
29 : :
30 : mxContext( rxContext ),
31 31491 : bFormatsValid( false )
32 : {
33 31491 : }
34 :
35 :
36 62982 : NumberFormatCodeMapper::~NumberFormatCodeMapper()
37 : {
38 62982 : }
39 :
40 :
41 : ::com::sun::star::i18n::NumberFormatCode SAL_CALL
42 0 : NumberFormatCodeMapper::getDefault( sal_Int16 formatType, sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException, std::exception)
43 : {
44 :
45 0 : OUString elementType = mapElementTypeShortToString(formatType);
46 0 : OUString elementUsage = mapElementUsageShortToString(formatUsage);
47 :
48 0 : getFormats( rLocale );
49 :
50 0 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
51 0 : if(aFormatSeq[i].isDefault && aFormatSeq[i].formatType == elementType &&
52 0 : aFormatSeq[i].formatUsage == elementUsage) {
53 : com::sun::star::i18n::NumberFormatCode anumberFormatCode(formatType,
54 : formatUsage,
55 0 : aFormatSeq[i].formatCode,
56 0 : aFormatSeq[i].formatName,
57 0 : aFormatSeq[i].formatKey,
58 0 : aFormatSeq[i].formatIndex,
59 0 : sal_True);
60 0 : return anumberFormatCode;
61 : }
62 : }
63 0 : com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
64 0 : return defaultNumberFormatCode;
65 : }
66 :
67 :
68 :
69 : ::com::sun::star::i18n::NumberFormatCode SAL_CALL
70 15978 : NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException, std::exception)
71 : {
72 15978 : getFormats( rLocale );
73 :
74 20808 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
75 20808 : if(aFormatSeq[i].formatIndex == formatIndex) {
76 15978 : com::sun::star::i18n::NumberFormatCode anumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
77 15978 : mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
78 15978 : aFormatSeq[i].formatCode,
79 15978 : aFormatSeq[i].formatName,
80 15978 : aFormatSeq[i].formatKey,
81 15978 : aFormatSeq[i].formatIndex,
82 111846 : aFormatSeq[i].isDefault);
83 15978 : return anumberFormatCode;
84 : }
85 : }
86 0 : com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
87 0 : return defaultNumberFormatCode;
88 :
89 : }
90 :
91 :
92 :
93 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
94 49203 : NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException, std::exception)
95 : {
96 49203 : getFormats( rLocale );
97 :
98 : sal_Int32 i, count;
99 49203 : count = 0;
100 2907208 : for(i = 0; i < aFormatSeq.getLength(); i++) {
101 2858005 : sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
102 2858005 : if( elementUsage == formatUsage)
103 398555 : count++;
104 : }
105 :
106 49203 : ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(count);
107 49203 : sal_Int32 j = 0;
108 2907208 : for(i = 0; i < aFormatSeq.getLength(); i++) {
109 2858005 : sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
110 2858005 : if( elementUsage == formatUsage) {
111 2391330 : seq[j] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
112 : formatUsage,
113 398555 : aFormatSeq[i].formatCode,
114 398555 : aFormatSeq[i].formatName,
115 398555 : aFormatSeq[i].formatKey,
116 398555 : aFormatSeq[i].formatIndex,
117 797110 : aFormatSeq[i].isDefault);
118 398555 : j++;
119 : }
120 : }
121 49203 : return seq;
122 :
123 : }
124 :
125 :
126 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
127 4813 : NumberFormatCodeMapper::getAllFormatCodes( const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException, std::exception)
128 : {
129 4813 : getFormats( rLocale );
130 :
131 4813 : ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(aFormatSeq.getLength());
132 296911 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++)
133 : {
134 2044686 : seq[i] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
135 292098 : mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
136 292098 : aFormatSeq[i].formatCode,
137 292098 : aFormatSeq[i].formatName,
138 292098 : aFormatSeq[i].formatKey,
139 292098 : aFormatSeq[i].formatIndex,
140 584196 : aFormatSeq[i].isDefault);
141 : }
142 4813 : return seq;
143 : }
144 :
145 :
146 : // --- private implementation -----------------------------------------
147 :
148 69994 : void NumberFormatCodeMapper::setupLocale( const ::com::sun::star::lang::Locale& rLocale )
149 : {
150 139988 : if ( aLocale.Country != rLocale.Country
151 38677 : || aLocale.Language != rLocale.Language
152 108498 : || aLocale.Variant != rLocale.Variant )
153 : {
154 31490 : bFormatsValid = false;
155 31490 : aLocale = rLocale;
156 : }
157 69994 : }
158 :
159 :
160 69994 : void NumberFormatCodeMapper::getFormats( const ::com::sun::star::lang::Locale& rLocale )
161 : {
162 69994 : setupLocale( rLocale );
163 69994 : if ( !bFormatsValid )
164 : {
165 31490 : createLocaleDataObject();
166 31490 : if( !mxLocaleData.is() )
167 0 : aFormatSeq = ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::FormatElement > (0);
168 : else
169 31490 : aFormatSeq = mxLocaleData->getAllFormats( aLocale );
170 31490 : bFormatsValid = true;
171 : }
172 69994 : }
173 :
174 :
175 : OUString
176 0 : NumberFormatCodeMapper::mapElementTypeShortToString(sal_Int16 formatType)
177 : {
178 :
179 0 : switch ( formatType )
180 : {
181 : case com::sun::star::i18n::KNumberFormatType::SHORT :
182 0 : return OUString( "short" );
183 : case com::sun::star::i18n::KNumberFormatType::MEDIUM :
184 0 : return OUString( "medium" );
185 : case com::sun::star::i18n::KNumberFormatType::LONG :
186 0 : return OUString( "long" );
187 : }
188 0 : return OUString();
189 : }
190 :
191 : sal_Int16
192 706631 : NumberFormatCodeMapper::mapElementTypeStringToShort(const OUString& formatType)
193 : {
194 706631 : if ( formatType == "short" )
195 126394 : return com::sun::star::i18n::KNumberFormatType::SHORT;
196 580237 : if ( formatType == "medium" )
197 435738 : return com::sun::star::i18n::KNumberFormatType::MEDIUM;
198 144499 : if ( formatType == "long" )
199 144499 : return com::sun::star::i18n::KNumberFormatType::LONG;
200 :
201 0 : return com::sun::star::i18n::KNumberFormatType::SHORT;
202 : }
203 :
204 : OUString
205 0 : NumberFormatCodeMapper::mapElementUsageShortToString(sal_Int16 formatUsage)
206 : {
207 0 : switch ( formatUsage )
208 : {
209 : case com::sun::star::i18n::KNumberFormatUsage::DATE :
210 0 : return OUString( "DATE" );
211 : case com::sun::star::i18n::KNumberFormatUsage::TIME :
212 0 : return OUString( "TIME" );
213 : case com::sun::star::i18n::KNumberFormatUsage::DATE_TIME :
214 0 : return OUString( "DATE_TIME" );
215 : case com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER :
216 0 : return OUString( "FIXED_NUMBER" );
217 : case com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER :
218 0 : return OUString( "FRACTION_NUMBER" );
219 : case com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER :
220 0 : return OUString( "PERCENT_NUMBER" );
221 : case com::sun::star::i18n::KNumberFormatUsage::CURRENCY :
222 0 : return OUString( "CURRENCY" );
223 : case com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER :
224 0 : return OUString( "SCIENTIFIC_NUMBER" );
225 : }
226 0 : return OUString();
227 : }
228 :
229 :
230 : sal_Int16
231 6024086 : NumberFormatCodeMapper::mapElementUsageStringToShort(const OUString& formatUsage)
232 : {
233 6024086 : if ( formatUsage == "DATE" )
234 2978165 : return com::sun::star::i18n::KNumberFormatUsage::DATE;
235 3045921 : if ( formatUsage == "TIME" )
236 727362 : return com::sun::star::i18n::KNumberFormatUsage::TIME;
237 2318559 : if ( formatUsage == "DATE_TIME" )
238 210304 : return com::sun::star::i18n::KNumberFormatUsage::DATE_TIME;
239 2108255 : if ( formatUsage == "FIXED_NUMBER" )
240 805816 : return com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER;
241 1302439 : if ( formatUsage == "FRACTION_NUMBER" )
242 0 : return com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER;
243 1302439 : if ( formatUsage == "PERCENT_NUMBER" )
244 207572 : return com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER;
245 1094867 : if ( formatUsage == "CURRENCY" )
246 784952 : return com::sun::star::i18n::KNumberFormatUsage::CURRENCY;
247 309915 : if ( formatUsage == "SCIENTIFIC_NUMBER" )
248 309915 : return com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER;
249 :
250 0 : return 0;
251 : }
252 :
253 :
254 : void
255 31490 : NumberFormatCodeMapper::createLocaleDataObject() {
256 :
257 31490 : if(mxLocaleData.is())
258 31490 : return;
259 :
260 31490 : mxLocaleData.set( com::sun::star::i18n::LocaleData::create(mxContext) );
261 : }
262 :
263 : OUString SAL_CALL
264 1 : NumberFormatCodeMapper::getImplementationName()
265 : throw( ::com::sun::star::uno::RuntimeException, std::exception )
266 : {
267 1 : return OUString("com.sun.star.i18n.NumberFormatCodeMapper");
268 : }
269 :
270 0 : sal_Bool SAL_CALL NumberFormatCodeMapper::supportsService(const OUString& rServiceName)
271 : throw( ::com::sun::star::uno::RuntimeException, std::exception )
272 : {
273 0 : return cppu::supportsService(this, rServiceName);
274 : }
275 :
276 : ::com::sun::star::uno::Sequence< OUString > SAL_CALL
277 1 : NumberFormatCodeMapper::getSupportedServiceNames() throw( ::com::sun::star::uno::RuntimeException, std::exception )
278 : {
279 1 : ::com::sun::star::uno::Sequence< OUString > aRet(1);
280 1 : aRet[0] = "com.sun.star.i18n.NumberFormatMapper";
281 1 : return aRet;
282 : }
283 :
284 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
285 31491 : com_sun_star_i18n_NumberFormatCodeMapper_get_implementation(
286 : css::uno::XComponentContext *context,
287 : css::uno::Sequence<css::uno::Any> const &)
288 : {
289 31491 : return cppu::acquire(new NumberFormatCodeMapper(context));
290 : }
291 :
292 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|