xref: /trunk/main/svl/source/numbers/supservs.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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_svl.hxx"
30 #include "supservs.hxx"
31 #include <com/sun/star/lang/Locale.hpp>
32 #include <comphelper/sharedmutex.hxx>
33 #include <i18npool/mslangid.hxx>
34 #include <tools/debug.hxx>
35 #include <vos/mutex.hxx>
36 #include <tools/stream.hxx>
37 #include <svl/strmadpt.hxx>
38 #include <svl/instrm.hxx>
39 
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::io;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::util;
45 using namespace ::vos;
46 using namespace ::utl;
47 
48 #define PERSISTENT_SERVICE_NAME     ::rtl::OUString::createFromAscii("com.sun.star.util.NumberFormatsSupplier");
49 
50 //-------------------------------------------------------------------------
51 Reference< XInterface > SAL_CALL SvNumberFormatsSupplierServiceObject_CreateInstance(const Reference< XMultiServiceFactory >& _rxFactory)
52 {
53     return static_cast< ::cppu::OWeakObject* >(new SvNumberFormatsSupplierServiceObject(_rxFactory));
54 }
55 
56 //-------------------------------------------------------------------------
57 SvNumberFormatsSupplierServiceObject::SvNumberFormatsSupplierServiceObject(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB)
58     :m_pOwnFormatter(NULL)
59     ,m_xORB(_rxORB)
60 {
61 }
62 
63 //-------------------------------------------------------------------------
64 SvNumberFormatsSupplierServiceObject::~SvNumberFormatsSupplierServiceObject()
65 {
66     if (m_pOwnFormatter)
67     {
68         delete m_pOwnFormatter;
69         m_pOwnFormatter = NULL;
70     }
71 }
72 
73 //-------------------------------------------------------------------------
74 Any SAL_CALL SvNumberFormatsSupplierServiceObject::queryAggregation( const Type& _rType ) throw (RuntimeException)
75 {
76     Any aReturn = ::cppu::queryInterface(_rType,
77         static_cast< XInitialization* >(this),
78         static_cast< XPersistObject* >(this),
79         static_cast< XServiceInfo* >(this)
80     );
81 
82     if (!aReturn.hasValue())
83         aReturn = SvNumberFormatsSupplierObj::queryAggregation(_rType);
84 
85     return aReturn;
86 }
87 
88 //-------------------------------------------------------------------------
89 void SAL_CALL SvNumberFormatsSupplierServiceObject::initialize( const Sequence< Any >& _rArguments ) throw(Exception, RuntimeException)
90 {
91     ::osl::MutexGuard aGuard( getSharedMutex() );
92 
93     DBG_ASSERT(m_pOwnFormatter == NULL,
94         "SvNumberFormatsSupplierServiceObject::initialize : already initialized !");
95         // maybe you already called a method which needed the formatter
96         // you should use XMultiServiceFactory::createInstanceWithArguments to avoid that
97     if (m_pOwnFormatter)
98     {   // !!! this is only a emergency handling, normally this should not occur !!!
99         delete m_pOwnFormatter;
100         m_pOwnFormatter = NULL;
101         SetNumberFormatter(m_pOwnFormatter);
102     }
103 
104     Type aExpectedArgType = ::getCppuType(static_cast<Locale*>(NULL));
105     LanguageType eNewFormatterLanguage = LANGUAGE_ENGLISH_US;
106         // the default
107 
108     const Any* pArgs = _rArguments.getConstArray();
109     for (sal_Int32 i=0; i<_rArguments.getLength(); ++i, ++pArgs)
110     {
111         if (pArgs->getValueType().equals(aExpectedArgType))
112         {
113             Locale aLocale;
114             *pArgs >>= aLocale;
115             eNewFormatterLanguage = MsLangId::convertLocaleToLanguage( aLocale);
116         }
117 #ifdef DBG_UTIL
118         else
119         {
120             DBG_ERROR("SvNumberFormatsSupplierServiceObject::initialize : unknown argument !");
121         }
122 #endif
123     }
124 
125     m_pOwnFormatter = new SvNumberFormatter(m_xORB, eNewFormatterLanguage);
126     m_pOwnFormatter->SetEvalDateFormat( NF_EVALDATEFORMAT_FORMAT_INTL );
127     SetNumberFormatter(m_pOwnFormatter);
128 }
129 
130 //-------------------------------------------------------------------------
131 ::rtl::OUString SAL_CALL SvNumberFormatsSupplierServiceObject::getImplementationName(  ) throw(RuntimeException)
132 {
133     return ::rtl::OUString::createFromAscii("com.sun.star.uno.util.numbers.SvNumberFormatsSupplierServiceObject");
134 }
135 
136 //-------------------------------------------------------------------------
137 sal_Bool SAL_CALL SvNumberFormatsSupplierServiceObject::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
138 {
139     Sequence< ::rtl::OUString > aServices = getSupportedServiceNames();
140     const ::rtl::OUString* pServices = aServices.getConstArray();
141     for (sal_Int32 i=0; i<aServices.getLength(); ++i, ++pServices)
142         if (pServices->equals(_rServiceName))
143             return sal_True;
144 
145     return sal_False;
146 }
147 
148 //-------------------------------------------------------------------------
149 Sequence< ::rtl::OUString > SAL_CALL SvNumberFormatsSupplierServiceObject::getSupportedServiceNames(  ) throw(RuntimeException)
150 {
151     Sequence< ::rtl::OUString > aSupported(1);
152     aSupported.getArray()[0] = PERSISTENT_SERVICE_NAME;
153     return aSupported;
154 }
155 
156 //-------------------------------------------------------------------------
157 ::rtl::OUString SAL_CALL SvNumberFormatsSupplierServiceObject::getServiceName(  ) throw(RuntimeException)
158 {
159     return PERSISTENT_SERVICE_NAME;
160 }
161 
162 //-------------------------------------------------------------------------
163 void SAL_CALL SvNumberFormatsSupplierServiceObject::write( const Reference< XObjectOutputStream >& _rxOutStream ) throw(IOException, RuntimeException)
164 {
165     ::osl::MutexGuard aGuard( getSharedMutex() );
166     implEnsureFormatter();
167 
168     Reference< XOutputStream > xStream(_rxOutStream.get());
169     SvLockBytesRef aLockBytes = new SvOutputStreamOpenLockBytes(xStream);
170     SvStream aSvOutputSteam(aLockBytes);
171 
172     m_pOwnFormatter->Save(aSvOutputSteam);
173 }
174 
175 //-------------------------------------------------------------------------
176 void SAL_CALL SvNumberFormatsSupplierServiceObject::read( const Reference< XObjectInputStream >& _rxInStream ) throw(IOException, RuntimeException)
177 {
178     ::osl::MutexGuard aGuard( getSharedMutex() );
179     implEnsureFormatter();
180 
181     Reference< XInputStream > xStream(_rxInStream.get());
182     SvInputStream aSvInputSteam(xStream);
183 
184     m_pOwnFormatter->Load(aSvInputSteam);
185 }
186 
187 //-------------------------------------------------------------------------
188 Reference< XPropertySet > SAL_CALL SvNumberFormatsSupplierServiceObject::getNumberFormatSettings() throw(RuntimeException)
189 {
190     ::osl::MutexGuard aGuard( getSharedMutex() );
191     implEnsureFormatter();
192     return SvNumberFormatsSupplierObj::getNumberFormatSettings();
193 }
194 
195 //-------------------------------------------------------------------------
196 Reference< XNumberFormats > SAL_CALL SvNumberFormatsSupplierServiceObject::getNumberFormats() throw(RuntimeException)
197 {
198     ::osl::MutexGuard aGuard( getSharedMutex() );
199     implEnsureFormatter();
200     return SvNumberFormatsSupplierObj::getNumberFormats();
201 }
202 
203 //-------------------------------------------------------------------------
204 sal_Int64 SAL_CALL SvNumberFormatsSupplierServiceObject::getSomething( const Sequence< sal_Int8 >& aIdentifier ) throw (RuntimeException)
205 {
206     sal_Int64 nReturn = SvNumberFormatsSupplierObj::getSomething( aIdentifier );
207     if ( nReturn )
208         // if somebody accesses internals then we should have the formatter
209         implEnsureFormatter();
210     return nReturn;
211 }
212 
213 //-------------------------------------------------------------------------
214 void SvNumberFormatsSupplierServiceObject::implEnsureFormatter()
215 {
216     if (!m_pOwnFormatter)
217     {
218         // get the office's UI locale
219         SvtSysLocale aSysLocale;
220         Locale aOfficeLocale = aSysLocale.GetLocaleData().getLocale();
221 
222         // initi with this locale
223         Sequence< Any > aFakedInitProps( 1 );
224         aFakedInitProps[0] <<= aOfficeLocale;
225 
226         initialize( aFakedInitProps );
227     }
228 }
229 
230