xref: /trunk/main/unotools/source/i18n/numberformatcodewrapper.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_unotools.hxx"
30 
31 #include <unotools/numberformatcodewrapper.hxx>
32 #include <tools/debug.hxx>
33 
34 #ifndef _COMPHELPER_COMPONENTFACTORY_HXX_
35 #include <comphelper/componentfactory.hxx>
36 #endif
37 #include <com/sun/star/uno/XInterface.hpp>
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 
40 #define LOCALEDATA_LIBRARYNAME "i18npool"
41 #define LOCALEDATA_SERVICENAME "com.sun.star.i18n.NumberFormatMapper"
42 
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::i18n;
45 using namespace ::com::sun::star::uno;
46 
47 
48 NumberFormatCodeWrapper::NumberFormatCodeWrapper(
49             const Reference< lang::XMultiServiceFactory > & xSF,
50             const lang::Locale& rLocale
51             )
52         :
53         xSMgr( xSF )
54 {
55     setLocale( rLocale );
56     if ( xSMgr.is() )
57     {
58         try
59         {
60             xNFC = Reference< XNumberFormatCode > ( xSMgr->createInstance(
61                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( LOCALEDATA_SERVICENAME ) ) ),
62                 uno::UNO_QUERY );
63         }
64         catch ( Exception& e )
65         {
66             (void)e;
67             DBG_ERRORFILE( "NumberFormatCodeWrapper ctor: Exception caught!" );
68         }
69     }
70     else
71     {   // try to get an instance somehow
72         DBG_ERRORFILE( "NumberFormatCodeWrapper: no service manager, trying own" );
73         try
74         {
75             Reference< XInterface > xI = ::comphelper::getComponentInstance(
76                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( LLCF_LIBNAME( LOCALEDATA_LIBRARYNAME ) ) ),
77                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( LOCALEDATA_SERVICENAME ) ) );
78             if ( xI.is() )
79             {
80                 Any x = xI->queryInterface( ::getCppuType((const Reference< XNumberFormatCode >*)0) );
81                 x >>= xNFC;
82             }
83         }
84         catch ( Exception& e )
85         {
86             (void)e;
87             DBG_ERRORFILE( "getComponentInstance: Exception caught!" );
88         }
89     }
90     DBG_ASSERT( xNFC.is(), "NumberFormatCodeWrapper: no NumberFormatMapper" );
91 }
92 
93 
94 NumberFormatCodeWrapper::~NumberFormatCodeWrapper()
95 {
96 }
97 
98 
99 void NumberFormatCodeWrapper::setLocale( const ::com::sun::star::lang::Locale& rLocale )
100 {
101     aLocale = rLocale;
102 }
103 
104 
105 ::com::sun::star::i18n::NumberFormatCode
106 NumberFormatCodeWrapper::getDefault( sal_Int16 formatType, sal_Int16 formatUsage ) const
107 {
108     try
109     {
110         if ( xNFC.is() )
111             return xNFC->getDefault( formatType, formatUsage, aLocale );
112     }
113     catch ( Exception& e )
114     {
115         (void)e;
116         DBG_ERRORFILE( "getDefault: Exception caught!" );
117     }
118     return ::com::sun::star::i18n::NumberFormatCode();
119 }
120 
121 
122 ::com::sun::star::i18n::NumberFormatCode
123 NumberFormatCodeWrapper::getFormatCode( sal_Int16 formatIndex ) const
124 {
125     try
126     {
127         if ( xNFC.is() )
128             return xNFC->getFormatCode( formatIndex, aLocale );
129     }
130     catch ( Exception& e )
131     {
132         (void)e;
133         DBG_ERRORFILE( "getFormatCode: Exception caught!" );
134     }
135     return ::com::sun::star::i18n::NumberFormatCode();
136 }
137 
138 
139 ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode >
140 NumberFormatCodeWrapper::getAllFormatCode( sal_Int16 formatUsage ) const
141 {
142     try
143     {
144         if ( xNFC.is() )
145             return xNFC->getAllFormatCode( formatUsage, aLocale );
146     }
147     catch ( Exception& e )
148     {
149         (void)e;
150         DBG_ERRORFILE( "getAllFormatCode: Exception caught!" );
151     }
152     return ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > (0);
153 }
154 
155 
156 ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode >
157 NumberFormatCodeWrapper::getAllFormatCodes() const
158 {
159     try
160     {
161         if ( xNFC.is() )
162             return xNFC->getAllFormatCodes( aLocale );
163     }
164     catch ( Exception& e )
165     {
166         (void)e;
167         DBG_ERRORFILE( "getAllFormatCodes: Exception caught!" );
168     }
169     return ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > (0);
170 }
171