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