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 :
21 : #include <numberformatcode.hxx>
22 : #include <com/sun/star/i18n/KNumberFormatUsage.hpp>
23 : #include <com/sun/star/i18n/KNumberFormatType.hpp>
24 : #include <com/sun/star/i18n/LocaleData.hpp>
25 : #include <comphelper/componentcontext.hxx>
26 :
27 :
28 :
29 5292 : NumberFormatCodeMapper::NumberFormatCodeMapper(
30 : const ::com::sun::star::uno::Reference <
31 : ::com::sun::star::uno::XComponentContext >& rxContext )
32 : :
33 : mxContext( rxContext ),
34 5292 : bFormatsValid( false )
35 : {
36 5292 : }
37 :
38 :
39 10584 : NumberFormatCodeMapper::~NumberFormatCodeMapper()
40 : {
41 10584 : }
42 :
43 :
44 : ::com::sun::star::i18n::NumberFormatCode SAL_CALL
45 0 : NumberFormatCodeMapper::getDefault( sal_Int16 formatType, sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
46 : {
47 :
48 0 : ::rtl::OUString elementType = mapElementTypeShortToString(formatType);
49 0 : ::rtl::OUString elementUsage = mapElementUsageShortToString(formatUsage);
50 :
51 0 : getFormats( rLocale );
52 :
53 0 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
54 0 : if(aFormatSeq[i].isDefault && aFormatSeq[i].formatType == elementType &&
55 0 : aFormatSeq[i].formatUsage == elementUsage) {
56 : com::sun::star::i18n::NumberFormatCode anumberFormatCode(formatType,
57 : formatUsage,
58 0 : aFormatSeq[i].formatCode,
59 0 : aFormatSeq[i].formatName,
60 0 : aFormatSeq[i].formatKey,
61 0 : aFormatSeq[i].formatIndex,
62 0 : sal_True);
63 0 : return anumberFormatCode;
64 : }
65 : }
66 0 : com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
67 0 : return defaultNumberFormatCode;
68 : }
69 :
70 :
71 :
72 : ::com::sun::star::i18n::NumberFormatCode SAL_CALL
73 3928 : NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
74 : {
75 3928 : getFormats( rLocale );
76 :
77 3928 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
78 3928 : if(aFormatSeq[i].formatIndex == formatIndex) {
79 3928 : com::sun::star::i18n::NumberFormatCode anumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
80 3928 : mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
81 3928 : aFormatSeq[i].formatCode,
82 3928 : aFormatSeq[i].formatName,
83 3928 : aFormatSeq[i].formatKey,
84 3928 : aFormatSeq[i].formatIndex,
85 27496 : aFormatSeq[i].isDefault);
86 3928 : return anumberFormatCode;
87 : }
88 : }
89 0 : com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
90 0 : return defaultNumberFormatCode;
91 :
92 : }
93 :
94 :
95 :
96 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
97 4661 : NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
98 : {
99 4661 : getFormats( rLocale );
100 :
101 : sal_Int32 i, count;
102 4661 : count = 0;
103 273173 : for(i = 0; i < aFormatSeq.getLength(); i++) {
104 268512 : sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
105 268512 : if( elementUsage == formatUsage)
106 37719 : count++;
107 : }
108 :
109 4661 : ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(count);
110 4661 : sal_Int32 j = 0;
111 273173 : for(i = 0; i < aFormatSeq.getLength(); i++) {
112 268512 : sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
113 268512 : if( elementUsage == formatUsage) {
114 75438 : seq[j] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
115 : formatUsage,
116 37719 : aFormatSeq[i].formatCode,
117 37719 : aFormatSeq[i].formatName,
118 37719 : aFormatSeq[i].formatKey,
119 37719 : aFormatSeq[i].formatIndex,
120 264033 : aFormatSeq[i].isDefault);
121 37719 : j++;
122 : }
123 : }
124 4661 : return seq;
125 :
126 : }
127 :
128 :
129 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
130 471 : NumberFormatCodeMapper::getAllFormatCodes( const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
131 : {
132 471 : getFormats( rLocale );
133 :
134 471 : ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(aFormatSeq.getLength());
135 28712 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++)
136 : {
137 56482 : seq[i] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
138 28241 : mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
139 28241 : aFormatSeq[i].formatCode,
140 28241 : aFormatSeq[i].formatName,
141 28241 : aFormatSeq[i].formatKey,
142 28241 : aFormatSeq[i].formatIndex,
143 225928 : aFormatSeq[i].isDefault);
144 : }
145 471 : return seq;
146 : }
147 :
148 :
149 : // --- private implementation -----------------------------------------
150 :
151 9060 : void NumberFormatCodeMapper::setupLocale( const ::com::sun::star::lang::Locale& rLocale )
152 : {
153 16612 : if ( aLocale.Country != rLocale.Country
154 3784 : || aLocale.Language != rLocale.Language
155 3768 : || aLocale.Variant != rLocale.Variant )
156 : {
157 5292 : bFormatsValid = false;
158 5292 : aLocale = rLocale;
159 : }
160 9060 : }
161 :
162 :
163 9060 : void NumberFormatCodeMapper::getFormats( const ::com::sun::star::lang::Locale& rLocale )
164 : {
165 9060 : setupLocale( rLocale );
166 9060 : if ( !bFormatsValid )
167 : {
168 5292 : createLocaleDataObject();
169 5292 : if( !mxLocaleData.is() )
170 0 : aFormatSeq = ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::FormatElement > (0);
171 : else
172 5292 : aFormatSeq = mxLocaleData->getAllFormats( aLocale );
173 5292 : bFormatsValid = true;
174 : }
175 9060 : }
176 :
177 :
178 : ::rtl::OUString
179 0 : NumberFormatCodeMapper::mapElementTypeShortToString(sal_Int16 formatType)
180 : {
181 :
182 0 : switch ( formatType )
183 : {
184 : case com::sun::star::i18n::KNumberFormatType::SHORT :
185 0 : return ::rtl::OUString( "short" );
186 : case com::sun::star::i18n::KNumberFormatType::MEDIUM :
187 0 : return ::rtl::OUString( "medium" );
188 : case com::sun::star::i18n::KNumberFormatType::LONG :
189 0 : return ::rtl::OUString( "long" );
190 : }
191 0 : return ::rtl::OUString();
192 : }
193 :
194 : sal_Int16
195 69888 : NumberFormatCodeMapper::mapElementTypeStringToShort(const ::rtl::OUString& formatType)
196 : {
197 69888 : if ( formatType == "short" )
198 12243 : return com::sun::star::i18n::KNumberFormatType::SHORT;
199 57645 : if ( formatType == "medium" )
200 43412 : return com::sun::star::i18n::KNumberFormatType::MEDIUM;
201 14233 : if ( formatType == "long" )
202 14233 : return com::sun::star::i18n::KNumberFormatType::LONG;
203 :
204 0 : return com::sun::star::i18n::KNumberFormatType::SHORT;
205 : }
206 :
207 : ::rtl::OUString
208 0 : NumberFormatCodeMapper::mapElementUsageShortToString(sal_Int16 formatUsage)
209 : {
210 0 : switch ( formatUsage )
211 : {
212 : case com::sun::star::i18n::KNumberFormatUsage::DATE :
213 0 : return ::rtl::OUString( "DATE" );
214 : case com::sun::star::i18n::KNumberFormatUsage::TIME :
215 0 : return ::rtl::OUString( "TIME" );
216 : case com::sun::star::i18n::KNumberFormatUsage::DATE_TIME :
217 0 : return ::rtl::OUString( "DATE_TIME" );
218 : case com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER :
219 0 : return ::rtl::OUString( "FIXED_NUMBER" );
220 : case com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER :
221 0 : return ::rtl::OUString( "FRACTION_NUMBER" );
222 : case com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER :
223 0 : return ::rtl::OUString( "PERCENT_NUMBER" );
224 : case com::sun::star::i18n::KNumberFormatUsage::CURRENCY :
225 0 : return ::rtl::OUString( "CURRENCY" );
226 : case com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER :
227 0 : return ::rtl::OUString( "SCIENTIFIC_NUMBER" );
228 : }
229 0 : return ::rtl::OUString();
230 : }
231 :
232 :
233 : sal_Int16
234 569193 : NumberFormatCodeMapper::mapElementUsageStringToShort(const ::rtl::OUString& formatUsage)
235 : {
236 569193 : if ( formatUsage == "DATE" )
237 286443 : return com::sun::star::i18n::KNumberFormatUsage::DATE;
238 282750 : if ( formatUsage == "TIME" )
239 69086 : return com::sun::star::i18n::KNumberFormatUsage::TIME;
240 213664 : if ( formatUsage == "DATE_TIME" )
241 19908 : return com::sun::star::i18n::KNumberFormatUsage::DATE_TIME;
242 193756 : if ( formatUsage == "FIXED_NUMBER" )
243 79372 : return com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER;
244 114384 : if ( formatUsage == "FRACTION_NUMBER" )
245 0 : return com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER;
246 114384 : if ( formatUsage == "PERCENT_NUMBER" )
247 19650 : return com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER;
248 94734 : if ( formatUsage == "CURRENCY" )
249 75140 : return com::sun::star::i18n::KNumberFormatUsage::CURRENCY;
250 19594 : if ( formatUsage == "SCIENTIFIC_NUMBER" )
251 19594 : return com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER;
252 :
253 0 : return 0;
254 : }
255 :
256 :
257 : void
258 5292 : NumberFormatCodeMapper::createLocaleDataObject() {
259 :
260 5292 : if(mxLocaleData.is())
261 5292 : return;
262 :
263 5292 : mxLocaleData.set( com::sun::star::i18n::LocaleData::create(mxContext) );
264 : }
265 :
266 : ::rtl::OUString SAL_CALL
267 0 : NumberFormatCodeMapper::getImplementationName(void)
268 : throw( ::com::sun::star::uno::RuntimeException )
269 : {
270 0 : return ::rtl::OUString("com.sun.star.i18n.NumberFormatCodeMapper");
271 : }
272 :
273 : const sal_Char cNumFormat[] = "com.sun.star.i18n.NumberFormatMapper";
274 :
275 : sal_Bool SAL_CALL
276 0 : NumberFormatCodeMapper::supportsService(const rtl::OUString& rServiceName)
277 : throw( ::com::sun::star::uno::RuntimeException )
278 : {
279 0 : return !rServiceName.compareToAscii(cNumFormat);
280 : }
281 :
282 : ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL
283 0 : NumberFormatCodeMapper::getSupportedServiceNames(void) throw( ::com::sun::star::uno::RuntimeException )
284 : {
285 0 : ::com::sun::star::uno::Sequence< ::rtl::OUString > aRet(1);
286 0 : aRet[0] = ::rtl::OUString::createFromAscii(cNumFormat);
287 0 : return aRet;
288 : }
289 :
290 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|