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