Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / i18npool / source / numberformatcode / numberformatcode.cxx
blobe6fbed0ece9375945657d9e6bfc5d541bd6513ba
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <numberformatcode.hxx>
31 #include <com/sun/star/i18n/KNumberFormatUsage.hpp>
32 #include <com/sun/star/i18n/KNumberFormatType.hpp>
36 NumberFormatCodeMapper::NumberFormatCodeMapper(
37 const ::com::sun::star::uno::Reference <
38 ::com::sun::star::lang::XMultiServiceFactory >& rxMSF )
40 xMSF( rxMSF ),
41 bFormatsValid( sal_False )
46 NumberFormatCodeMapper::~NumberFormatCodeMapper()
51 ::com::sun::star::i18n::NumberFormatCode SAL_CALL
52 NumberFormatCodeMapper::getDefault( sal_Int16 formatType, sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
55 ::rtl::OUString elementType = mapElementTypeShortToString(formatType);
56 ::rtl::OUString elementUsage = mapElementUsageShortToString(formatUsage);
58 getFormats( rLocale );
60 for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
61 if(aFormatSeq[i].isDefault && aFormatSeq[i].formatType == elementType &&
62 aFormatSeq[i].formatUsage == elementUsage) {
63 com::sun::star::i18n::NumberFormatCode anumberFormatCode(formatType,
64 formatUsage,
65 aFormatSeq[i].formatCode,
66 aFormatSeq[i].formatName,
67 aFormatSeq[i].formatKey,
68 aFormatSeq[i].formatIndex,
69 sal_True);
70 return anumberFormatCode;
73 com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
74 return defaultNumberFormatCode;
79 ::com::sun::star::i18n::NumberFormatCode SAL_CALL
80 NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
82 getFormats( rLocale );
84 for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
85 if(aFormatSeq[i].formatIndex == formatIndex) {
86 com::sun::star::i18n::NumberFormatCode anumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
87 mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
88 aFormatSeq[i].formatCode,
89 aFormatSeq[i].formatName,
90 aFormatSeq[i].formatKey,
91 aFormatSeq[i].formatIndex,
92 aFormatSeq[i].isDefault);
93 return anumberFormatCode;
96 com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
97 return defaultNumberFormatCode;
103 ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
104 NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
106 getFormats( rLocale );
108 sal_Int32 i, count;
109 count = 0;
110 for(i = 0; i < aFormatSeq.getLength(); i++) {
111 sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
112 if( elementUsage == formatUsage)
113 count++;
116 ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(count);
117 sal_Int32 j = 0;
118 for(i = 0; i < aFormatSeq.getLength(); i++) {
119 sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
120 if( elementUsage == formatUsage) {
121 seq[j] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
122 formatUsage,
123 aFormatSeq[i].formatCode,
124 aFormatSeq[i].formatName,
125 aFormatSeq[i].formatKey,
126 aFormatSeq[i].formatIndex,
127 aFormatSeq[i].isDefault);
128 j++;
131 return seq;
136 ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
137 NumberFormatCodeMapper::getAllFormatCodes( const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
139 getFormats( rLocale );
141 ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(aFormatSeq.getLength());
142 for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++)
144 seq[i] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
145 mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
146 aFormatSeq[i].formatCode,
147 aFormatSeq[i].formatName,
148 aFormatSeq[i].formatKey,
149 aFormatSeq[i].formatIndex,
150 aFormatSeq[i].isDefault);
152 return seq;
156 // --- private implementation -----------------------------------------
158 void NumberFormatCodeMapper::setupLocale( const ::com::sun::star::lang::Locale& rLocale )
160 if ( aLocale.Country != rLocale.Country
161 || aLocale.Language != rLocale.Language
162 || aLocale.Variant != rLocale.Variant )
164 bFormatsValid = sal_False;
165 aLocale = rLocale;
170 void NumberFormatCodeMapper::getFormats( const ::com::sun::star::lang::Locale& rLocale )
172 setupLocale( rLocale );
173 if ( !bFormatsValid )
175 createLocaleDataObject();
176 if( !xlocaleData.is() )
177 aFormatSeq = ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::FormatElement > (0);
178 else
179 aFormatSeq = xlocaleData->getAllFormats( aLocale );
180 bFormatsValid = sal_True;
185 ::rtl::OUString
186 NumberFormatCodeMapper::mapElementTypeShortToString(sal_Int16 formatType)
189 switch ( formatType )
191 case com::sun::star::i18n::KNumberFormatType::SHORT :
192 return ::rtl::OUString( "short" );
193 case com::sun::star::i18n::KNumberFormatType::MEDIUM :
194 return ::rtl::OUString( "medium" );
195 case com::sun::star::i18n::KNumberFormatType::LONG :
196 return ::rtl::OUString( "long" );
198 return ::rtl::OUString();
201 sal_Int16
202 NumberFormatCodeMapper::mapElementTypeStringToShort(const ::rtl::OUString& formatType)
204 if ( formatType == "short" )
205 return com::sun::star::i18n::KNumberFormatType::SHORT;
206 if ( formatType == "medium" )
207 return com::sun::star::i18n::KNumberFormatType::MEDIUM;
208 if ( formatType == "long" )
209 return com::sun::star::i18n::KNumberFormatType::LONG;
211 return com::sun::star::i18n::KNumberFormatType::SHORT;
214 ::rtl::OUString
215 NumberFormatCodeMapper::mapElementUsageShortToString(sal_Int16 formatUsage)
217 switch ( formatUsage )
219 case com::sun::star::i18n::KNumberFormatUsage::DATE :
220 return ::rtl::OUString( "DATE" );
221 case com::sun::star::i18n::KNumberFormatUsage::TIME :
222 return ::rtl::OUString( "TIME" );
223 case com::sun::star::i18n::KNumberFormatUsage::DATE_TIME :
224 return ::rtl::OUString( "DATE_TIME" );
225 case com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER :
226 return ::rtl::OUString( "FIXED_NUMBER" );
227 case com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER :
228 return ::rtl::OUString( "FRACTION_NUMBER" );
229 case com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER :
230 return ::rtl::OUString( "PERCENT_NUMBER" );
231 case com::sun::star::i18n::KNumberFormatUsage::CURRENCY :
232 return ::rtl::OUString( "CURRENCY" );
233 case com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER :
234 return ::rtl::OUString( "SCIENTIFIC_NUMBER" );
236 return ::rtl::OUString();
240 sal_Int16
241 NumberFormatCodeMapper::mapElementUsageStringToShort(const ::rtl::OUString& formatUsage)
243 if ( formatUsage == "DATE" )
244 return com::sun::star::i18n::KNumberFormatUsage::DATE;
245 if ( formatUsage == "TIME" )
246 return com::sun::star::i18n::KNumberFormatUsage::TIME;
247 if ( formatUsage == "DATE_TIME" )
248 return com::sun::star::i18n::KNumberFormatUsage::DATE_TIME;
249 if ( formatUsage == "FIXED_NUMBER" )
250 return com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER;
251 if ( formatUsage == "FRACTION_NUMBER" )
252 return com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER;
253 if ( formatUsage == "PERCENT_NUMBER" )
254 return com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER;
255 if ( formatUsage == "CURRENCY" )
256 return com::sun::star::i18n::KNumberFormatUsage::CURRENCY;
257 if ( formatUsage == "SCIENTIFIC_NUMBER" )
258 return com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER;
260 return 0;
264 void
265 NumberFormatCodeMapper::createLocaleDataObject() {
267 if(xlocaleData.is())
268 return;
270 ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface >
271 xI = xMSF->createInstance(
272 ::rtl::OUString( "com.sun.star.i18n.LocaleData" ));
274 if ( xI.is() ) {
275 ::com::sun::star::uno::Any x = xI->queryInterface( ::getCppuType((const ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XLocaleData >*)0) );
276 x >>= xlocaleData;
280 ::rtl::OUString SAL_CALL
281 NumberFormatCodeMapper::getImplementationName(void)
282 throw( ::com::sun::star::uno::RuntimeException )
284 return ::rtl::OUString("com.sun.star.i18n.NumberFormatCodeMapper");
287 const sal_Char cNumFormat[] = "com.sun.star.i18n.NumberFormatMapper";
289 sal_Bool SAL_CALL
290 NumberFormatCodeMapper::supportsService(const rtl::OUString& rServiceName)
291 throw( ::com::sun::star::uno::RuntimeException )
293 return !rServiceName.compareToAscii(cNumFormat);
296 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL
297 NumberFormatCodeMapper::getSupportedServiceNames(void) throw( ::com::sun::star::uno::RuntimeException )
299 ::com::sun::star::uno::Sequence< ::rtl::OUString > aRet(1);
300 aRet[0] = ::rtl::OUString::createFromAscii(cNumFormat);
301 return aRet;
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */