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 53830 : NumberFormatCodeMapper::NumberFormatCodeMapper(
27 : const ::com::sun::star::uno::Reference <
28 : ::com::sun::star::uno::XComponentContext >& rxContext )
29 : :
30 : mxContext( rxContext ),
31 53830 : bFormatsValid( false )
32 : {
33 53830 : }
34 :
35 :
36 107660 : NumberFormatCodeMapper::~NumberFormatCodeMapper()
37 : {
38 107660 : }
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 30804 : NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException, std::exception)
71 : {
72 30804 : getFormats( rLocale );
73 :
74 33564 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
75 33564 : if(aFormatSeq[i].formatIndex == formatIndex) {
76 30804 : com::sun::star::i18n::NumberFormatCode anumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
77 30804 : mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
78 30804 : aFormatSeq[i].formatCode,
79 30804 : aFormatSeq[i].formatName,
80 30804 : aFormatSeq[i].formatKey,
81 30804 : aFormatSeq[i].formatIndex,
82 215628 : aFormatSeq[i].isDefault);
83 30804 : 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 77792 : NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException, std::exception)
95 : {
96 77792 : getFormats( rLocale );
97 :
98 : sal_Int32 i, count;
99 77792 : count = 0;
100 4549084 : for(i = 0; i < aFormatSeq.getLength(); i++) {
101 4471292 : sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
102 4471292 : if( elementUsage == formatUsage)
103 627920 : count++;
104 : }
105 :
106 77792 : ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(count);
107 77792 : sal_Int32 j = 0;
108 4549084 : for(i = 0; i < aFormatSeq.getLength(); i++) {
109 4471292 : sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
110 4471292 : if( elementUsage == formatUsage) {
111 3767520 : seq[j] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
112 : formatUsage,
113 627920 : aFormatSeq[i].formatCode,
114 627920 : aFormatSeq[i].formatName,
115 627920 : aFormatSeq[i].formatKey,
116 627920 : aFormatSeq[i].formatIndex,
117 1255840 : aFormatSeq[i].isDefault);
118 627920 : j++;
119 : }
120 : }
121 77792 : return seq;
122 :
123 : }
124 :
125 :
126 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
127 7824 : NumberFormatCodeMapper::getAllFormatCodes( const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException, std::exception)
128 : {
129 7824 : getFormats( rLocale );
130 :
131 7824 : ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(aFormatSeq.getLength());
132 476050 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++)
133 : {
134 3277582 : seq[i] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
135 468226 : mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
136 468226 : aFormatSeq[i].formatCode,
137 468226 : aFormatSeq[i].formatName,
138 468226 : aFormatSeq[i].formatKey,
139 468226 : aFormatSeq[i].formatIndex,
140 936452 : aFormatSeq[i].isDefault);
141 : }
142 7824 : return seq;
143 : }
144 :
145 :
146 : // --- private implementation -----------------------------------------
147 :
148 116420 : void NumberFormatCodeMapper::setupLocale( const ::com::sun::star::lang::Locale& rLocale )
149 : {
150 232840 : if ( aLocale.Country != rLocale.Country
151 62840 : || aLocale.Language != rLocale.Language
152 179012 : || aLocale.Variant != rLocale.Variant )
153 : {
154 53828 : bFormatsValid = false;
155 53828 : aLocale = rLocale;
156 : }
157 116420 : }
158 :
159 :
160 116420 : void NumberFormatCodeMapper::getFormats( const ::com::sun::star::lang::Locale& rLocale )
161 : {
162 116420 : setupLocale( rLocale );
163 116420 : if ( !bFormatsValid )
164 : {
165 53828 : createLocaleDataObject();
166 53828 : if( !mxLocaleData.is() )
167 0 : aFormatSeq = ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::FormatElement > (0);
168 : else
169 53828 : aFormatSeq = mxLocaleData->getAllFormats( aLocale );
170 53828 : bFormatsValid = true;
171 : }
172 116420 : }
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 1126950 : NumberFormatCodeMapper::mapElementTypeStringToShort(const OUString& formatType)
193 : {
194 1126950 : if ( formatType == "short" )
195 203602 : return com::sun::star::i18n::KNumberFormatType::SHORT;
196 923348 : if ( formatType == "medium" )
197 687614 : return com::sun::star::i18n::KNumberFormatType::MEDIUM;
198 235734 : if ( formatType == "long" )
199 235734 : 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 9441614 : NumberFormatCodeMapper::mapElementUsageStringToShort(const OUString& formatUsage)
232 : {
233 9441614 : if ( formatUsage == "DATE" )
234 4761032 : return com::sun::star::i18n::KNumberFormatUsage::DATE;
235 4680582 : if ( formatUsage == "TIME" )
236 1151018 : return com::sun::star::i18n::KNumberFormatUsage::TIME;
237 3529564 : if ( formatUsage == "DATE_TIME" )
238 332580 : return com::sun::star::i18n::KNumberFormatUsage::DATE_TIME;
239 3196984 : if ( formatUsage == "FIXED_NUMBER" )
240 1289540 : return com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER;
241 1907444 : if ( formatUsage == "FRACTION_NUMBER" )
242 0 : return com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER;
243 1907444 : if ( formatUsage == "PERCENT_NUMBER" )
244 328508 : return com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER;
245 1578936 : if ( formatUsage == "CURRENCY" )
246 1251748 : return com::sun::star::i18n::KNumberFormatUsage::CURRENCY;
247 327188 : if ( formatUsage == "SCIENTIFIC_NUMBER" )
248 327188 : return com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER;
249 :
250 0 : return 0;
251 : }
252 :
253 :
254 : void
255 53828 : NumberFormatCodeMapper::createLocaleDataObject() {
256 :
257 53828 : if(mxLocaleData.is())
258 53828 : return;
259 :
260 53828 : mxLocaleData.set( com::sun::star::i18n::LocaleData::create(mxContext) );
261 : }
262 :
263 : OUString SAL_CALL
264 0 : NumberFormatCodeMapper::getImplementationName(void)
265 : throw( ::com::sun::star::uno::RuntimeException, std::exception )
266 : {
267 0 : 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 0 : NumberFormatCodeMapper::getSupportedServiceNames(void) throw( ::com::sun::star::uno::RuntimeException, std::exception )
278 : {
279 0 : ::com::sun::star::uno::Sequence< OUString > aRet(1);
280 0 : aRet[0] = "com.sun.star.i18n.NumberFormatMapper";
281 0 : return aRet;
282 : }
283 :
284 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
285 53830 : com_sun_star_i18n_NumberFormatCodeMapper_get_implementation(
286 : css::uno::XComponentContext *context,
287 : css::uno::Sequence<css::uno::Any> const &)
288 : {
289 53830 : return cppu::acquire(new NumberFormatCodeMapper(context));
290 : }
291 :
292 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|