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 22151 : NumberFormatCodeMapper::NumberFormatCodeMapper(
27 : const ::com::sun::star::uno::Reference <
28 : ::com::sun::star::uno::XComponentContext >& rxContext )
29 : :
30 : mxContext( rxContext ),
31 22151 : bFormatsValid( false )
32 : {
33 22151 : }
34 :
35 :
36 44302 : NumberFormatCodeMapper::~NumberFormatCodeMapper()
37 : {
38 44302 : }
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 12927 : NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException, std::exception)
71 : {
72 12927 : getFormats( rLocale );
73 :
74 14277 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
75 14277 : if(aFormatSeq[i].formatIndex == formatIndex) {
76 12927 : com::sun::star::i18n::NumberFormatCode anumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
77 12927 : mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
78 12927 : aFormatSeq[i].formatCode,
79 12927 : aFormatSeq[i].formatName,
80 12927 : aFormatSeq[i].formatKey,
81 12927 : aFormatSeq[i].formatIndex,
82 90489 : aFormatSeq[i].isDefault);
83 12927 : 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 30951 : NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException, std::exception)
95 : {
96 30951 : getFormats( rLocale );
97 :
98 : sal_Int32 i, count;
99 30951 : count = 0;
100 1810055 : for(i = 0; i < aFormatSeq.getLength(); i++) {
101 1779104 : sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
102 1779104 : if( elementUsage == formatUsage)
103 249888 : count++;
104 : }
105 :
106 30951 : ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(count);
107 30951 : sal_Int32 j = 0;
108 1810055 : for(i = 0; i < aFormatSeq.getLength(); i++) {
109 1779104 : sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
110 1779104 : if( elementUsage == formatUsage) {
111 1499328 : seq[j] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
112 : formatUsage,
113 249888 : aFormatSeq[i].formatCode,
114 249888 : aFormatSeq[i].formatName,
115 249888 : aFormatSeq[i].formatKey,
116 249888 : aFormatSeq[i].formatIndex,
117 499776 : aFormatSeq[i].isDefault);
118 249888 : j++;
119 : }
120 : }
121 30951 : return seq;
122 :
123 : }
124 :
125 :
126 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
127 3104 : NumberFormatCodeMapper::getAllFormatCodes( const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException, std::exception)
128 : {
129 3104 : getFormats( rLocale );
130 :
131 3104 : ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(aFormatSeq.getLength());
132 188925 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++)
133 : {
134 1300747 : seq[i] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
135 185821 : mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
136 185821 : aFormatSeq[i].formatCode,
137 185821 : aFormatSeq[i].formatName,
138 185821 : aFormatSeq[i].formatKey,
139 185821 : aFormatSeq[i].formatIndex,
140 371642 : aFormatSeq[i].isDefault);
141 : }
142 3104 : return seq;
143 : }
144 :
145 :
146 : // --- private implementation -----------------------------------------
147 :
148 46982 : void NumberFormatCodeMapper::setupLocale( const ::com::sun::star::lang::Locale& rLocale )
149 : {
150 93964 : if ( aLocale.Country != rLocale.Country
151 24936 : || aLocale.Language != rLocale.Language
152 71814 : || aLocale.Variant != rLocale.Variant )
153 : {
154 22150 : bFormatsValid = false;
155 22150 : aLocale = rLocale;
156 : }
157 46982 : }
158 :
159 :
160 46982 : void NumberFormatCodeMapper::getFormats( const ::com::sun::star::lang::Locale& rLocale )
161 : {
162 46982 : setupLocale( rLocale );
163 46982 : if ( !bFormatsValid )
164 : {
165 22150 : createLocaleDataObject();
166 22150 : if( !mxLocaleData.is() )
167 0 : aFormatSeq = ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::FormatElement > (0);
168 : else
169 22150 : aFormatSeq = mxLocaleData->getAllFormats( aLocale );
170 22150 : bFormatsValid = true;
171 : }
172 46982 : }
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 448636 : NumberFormatCodeMapper::mapElementTypeStringToShort(const OUString& formatType)
193 : {
194 448636 : if ( formatType == "short" )
195 80924 : return com::sun::star::i18n::KNumberFormatType::SHORT;
196 367712 : if ( formatType == "medium" )
197 274107 : return com::sun::star::i18n::KNumberFormatType::MEDIUM;
198 93605 : if ( formatType == "long" )
199 93605 : 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 3756956 : NumberFormatCodeMapper::mapElementUsageStringToShort(const OUString& formatUsage)
232 : {
233 3756956 : if ( formatUsage == "DATE" )
234 1893501 : return com::sun::star::i18n::KNumberFormatUsage::DATE;
235 1863455 : if ( formatUsage == "TIME" )
236 458069 : return com::sun::star::i18n::KNumberFormatUsage::TIME;
237 1405386 : if ( formatUsage == "DATE_TIME" )
238 132415 : return com::sun::star::i18n::KNumberFormatUsage::DATE_TIME;
239 1272971 : if ( formatUsage == "FIXED_NUMBER" )
240 513953 : return com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER;
241 759018 : if ( formatUsage == "FRACTION_NUMBER" )
242 0 : return com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER;
243 759018 : if ( formatUsage == "PERCENT_NUMBER" )
244 130738 : return com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER;
245 628280 : if ( formatUsage == "CURRENCY" )
246 498112 : return com::sun::star::i18n::KNumberFormatUsage::CURRENCY;
247 130168 : if ( formatUsage == "SCIENTIFIC_NUMBER" )
248 130168 : return com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER;
249 :
250 0 : return 0;
251 : }
252 :
253 :
254 : void
255 22150 : NumberFormatCodeMapper::createLocaleDataObject() {
256 :
257 22150 : if(mxLocaleData.is())
258 22150 : return;
259 :
260 22150 : 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 22151 : com_sun_star_i18n_NumberFormatCodeMapper_get_implementation(
286 : css::uno::XComponentContext *context,
287 : css::uno::Sequence<css::uno::Any> const &)
288 : {
289 22151 : return cppu::acquire(new NumberFormatCodeMapper(context));
290 : }
291 :
292 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|