xref: /aoo42x/main/svl/source/numbers/supservs.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svl.hxx"
30*cdf0e10cSrcweir #include "supservs.hxx"
31*cdf0e10cSrcweir #include <com/sun/star/lang/Locale.hpp>
32*cdf0e10cSrcweir #include <comphelper/sharedmutex.hxx>
33*cdf0e10cSrcweir #include <i18npool/mslangid.hxx>
34*cdf0e10cSrcweir #include <tools/debug.hxx>
35*cdf0e10cSrcweir #include <vos/mutex.hxx>
36*cdf0e10cSrcweir #include <tools/stream.hxx>
37*cdf0e10cSrcweir #include <svl/strmadpt.hxx>
38*cdf0e10cSrcweir #include <svl/instrm.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
41*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
42*cdf0e10cSrcweir using namespace ::com::sun::star::io;
43*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
44*cdf0e10cSrcweir using namespace ::com::sun::star::util;
45*cdf0e10cSrcweir using namespace ::vos;
46*cdf0e10cSrcweir using namespace ::utl;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #define PERSISTENT_SERVICE_NAME		::rtl::OUString::createFromAscii("com.sun.star.util.NumberFormatsSupplier");
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir //-------------------------------------------------------------------------
51*cdf0e10cSrcweir Reference< XInterface > SAL_CALL SvNumberFormatsSupplierServiceObject_CreateInstance(const Reference< XMultiServiceFactory >& _rxFactory)
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir 	return static_cast< ::cppu::OWeakObject* >(new SvNumberFormatsSupplierServiceObject(_rxFactory));
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir //-------------------------------------------------------------------------
57*cdf0e10cSrcweir SvNumberFormatsSupplierServiceObject::SvNumberFormatsSupplierServiceObject(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB)
58*cdf0e10cSrcweir 	:m_pOwnFormatter(NULL)
59*cdf0e10cSrcweir 	,m_xORB(_rxORB)
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir //-------------------------------------------------------------------------
64*cdf0e10cSrcweir SvNumberFormatsSupplierServiceObject::~SvNumberFormatsSupplierServiceObject()
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir 	if (m_pOwnFormatter)
67*cdf0e10cSrcweir 	{
68*cdf0e10cSrcweir 		delete m_pOwnFormatter;
69*cdf0e10cSrcweir 		m_pOwnFormatter = NULL;
70*cdf0e10cSrcweir 	}
71*cdf0e10cSrcweir }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir //-------------------------------------------------------------------------
74*cdf0e10cSrcweir Any SAL_CALL SvNumberFormatsSupplierServiceObject::queryAggregation( const Type& _rType ) throw (RuntimeException)
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir 	Any aReturn = ::cppu::queryInterface(_rType,
77*cdf0e10cSrcweir 		static_cast< XInitialization* >(this),
78*cdf0e10cSrcweir 		static_cast< XPersistObject* >(this),
79*cdf0e10cSrcweir 		static_cast< XServiceInfo* >(this)
80*cdf0e10cSrcweir 	);
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 	if (!aReturn.hasValue())
83*cdf0e10cSrcweir 		aReturn = SvNumberFormatsSupplierObj::queryAggregation(_rType);
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	return aReturn;
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir //-------------------------------------------------------------------------
89*cdf0e10cSrcweir void SAL_CALL SvNumberFormatsSupplierServiceObject::initialize( const Sequence< Any >& _rArguments ) throw(Exception, RuntimeException)
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( getSharedMutex() );
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	DBG_ASSERT(m_pOwnFormatter == NULL,
94*cdf0e10cSrcweir 		"SvNumberFormatsSupplierServiceObject::initialize : already initialized !");
95*cdf0e10cSrcweir 		// maybe you already called a method which needed the formatter
96*cdf0e10cSrcweir 		// you should use XMultiServiceFactory::createInstanceWithArguments to avoid that
97*cdf0e10cSrcweir 	if (m_pOwnFormatter)
98*cdf0e10cSrcweir 	{	// !!! this is only a emergency handling, normally this should not occur !!!
99*cdf0e10cSrcweir 		delete m_pOwnFormatter;
100*cdf0e10cSrcweir 		m_pOwnFormatter = NULL;
101*cdf0e10cSrcweir 		SetNumberFormatter(m_pOwnFormatter);
102*cdf0e10cSrcweir 	}
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	Type aExpectedArgType = ::getCppuType(static_cast<Locale*>(NULL));
105*cdf0e10cSrcweir 	LanguageType eNewFormatterLanguage = LANGUAGE_ENGLISH_US;
106*cdf0e10cSrcweir 		// the default
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 	const Any* pArgs = _rArguments.getConstArray();
109*cdf0e10cSrcweir 	for (sal_Int32 i=0; i<_rArguments.getLength(); ++i, ++pArgs)
110*cdf0e10cSrcweir 	{
111*cdf0e10cSrcweir 		if (pArgs->getValueType().equals(aExpectedArgType))
112*cdf0e10cSrcweir 		{
113*cdf0e10cSrcweir 			Locale aLocale;
114*cdf0e10cSrcweir 			*pArgs >>= aLocale;
115*cdf0e10cSrcweir 			eNewFormatterLanguage = MsLangId::convertLocaleToLanguage( aLocale);
116*cdf0e10cSrcweir 		}
117*cdf0e10cSrcweir #ifdef DBG_UTIL
118*cdf0e10cSrcweir 		else
119*cdf0e10cSrcweir 		{
120*cdf0e10cSrcweir 			DBG_ERROR("SvNumberFormatsSupplierServiceObject::initialize : unknown argument !");
121*cdf0e10cSrcweir 		}
122*cdf0e10cSrcweir #endif
123*cdf0e10cSrcweir 	}
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 	m_pOwnFormatter = new SvNumberFormatter(m_xORB, eNewFormatterLanguage);
126*cdf0e10cSrcweir     m_pOwnFormatter->SetEvalDateFormat( NF_EVALDATEFORMAT_FORMAT_INTL );
127*cdf0e10cSrcweir 	SetNumberFormatter(m_pOwnFormatter);
128*cdf0e10cSrcweir }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir //-------------------------------------------------------------------------
131*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvNumberFormatsSupplierServiceObject::getImplementationName(  ) throw(RuntimeException)
132*cdf0e10cSrcweir {
133*cdf0e10cSrcweir 	return ::rtl::OUString::createFromAscii("com.sun.star.uno.util.numbers.SvNumberFormatsSupplierServiceObject");
134*cdf0e10cSrcweir }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir //-------------------------------------------------------------------------
137*cdf0e10cSrcweir sal_Bool SAL_CALL SvNumberFormatsSupplierServiceObject::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aServices = getSupportedServiceNames();
140*cdf0e10cSrcweir 	const ::rtl::OUString* pServices = aServices.getConstArray();
141*cdf0e10cSrcweir 	for (sal_Int32 i=0; i<aServices.getLength(); ++i, ++pServices)
142*cdf0e10cSrcweir 		if (pServices->equals(_rServiceName))
143*cdf0e10cSrcweir 			return sal_True;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	return sal_False;
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir //-------------------------------------------------------------------------
149*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL SvNumberFormatsSupplierServiceObject::getSupportedServiceNames(  ) throw(RuntimeException)
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aSupported(1);
152*cdf0e10cSrcweir 	aSupported.getArray()[0] = PERSISTENT_SERVICE_NAME;
153*cdf0e10cSrcweir 	return aSupported;
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir //-------------------------------------------------------------------------
157*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvNumberFormatsSupplierServiceObject::getServiceName(  ) throw(RuntimeException)
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir 	return PERSISTENT_SERVICE_NAME;
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir //-------------------------------------------------------------------------
163*cdf0e10cSrcweir void SAL_CALL SvNumberFormatsSupplierServiceObject::write( const Reference< XObjectOutputStream >& _rxOutStream ) throw(IOException, RuntimeException)
164*cdf0e10cSrcweir {
165*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( getSharedMutex() );
166*cdf0e10cSrcweir 	implEnsureFormatter();
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 	Reference< XOutputStream > xStream(_rxOutStream.get());
169*cdf0e10cSrcweir 	SvLockBytesRef aLockBytes = new SvOutputStreamOpenLockBytes(xStream);
170*cdf0e10cSrcweir 	SvStream aSvOutputSteam(aLockBytes);
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 	m_pOwnFormatter->Save(aSvOutputSteam);
173*cdf0e10cSrcweir }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir //-------------------------------------------------------------------------
176*cdf0e10cSrcweir void SAL_CALL SvNumberFormatsSupplierServiceObject::read( const Reference< XObjectInputStream >& _rxInStream ) throw(IOException, RuntimeException)
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( getSharedMutex() );
179*cdf0e10cSrcweir 	implEnsureFormatter();
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 	Reference< XInputStream > xStream(_rxInStream.get());
182*cdf0e10cSrcweir 	SvInputStream aSvInputSteam(xStream);
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 	m_pOwnFormatter->Load(aSvInputSteam);
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir //-------------------------------------------------------------------------
188*cdf0e10cSrcweir Reference< XPropertySet > SAL_CALL SvNumberFormatsSupplierServiceObject::getNumberFormatSettings() throw(RuntimeException)
189*cdf0e10cSrcweir {
190*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( getSharedMutex() );
191*cdf0e10cSrcweir 	implEnsureFormatter();
192*cdf0e10cSrcweir 	return SvNumberFormatsSupplierObj::getNumberFormatSettings();
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir //-------------------------------------------------------------------------
196*cdf0e10cSrcweir Reference< XNumberFormats > SAL_CALL SvNumberFormatsSupplierServiceObject::getNumberFormats() throw(RuntimeException)
197*cdf0e10cSrcweir {
198*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( getSharedMutex() );
199*cdf0e10cSrcweir 	implEnsureFormatter();
200*cdf0e10cSrcweir 	return SvNumberFormatsSupplierObj::getNumberFormats();
201*cdf0e10cSrcweir }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir //-------------------------------------------------------------------------
204*cdf0e10cSrcweir sal_Int64 SAL_CALL SvNumberFormatsSupplierServiceObject::getSomething( const Sequence< sal_Int8 >& aIdentifier ) throw (RuntimeException)
205*cdf0e10cSrcweir {
206*cdf0e10cSrcweir 	sal_Int64 nReturn = SvNumberFormatsSupplierObj::getSomething( aIdentifier );
207*cdf0e10cSrcweir 	if ( nReturn )
208*cdf0e10cSrcweir 		// if somebody accesses internals then we should have the formatter
209*cdf0e10cSrcweir 		implEnsureFormatter();
210*cdf0e10cSrcweir 	return nReturn;
211*cdf0e10cSrcweir }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir //-------------------------------------------------------------------------
214*cdf0e10cSrcweir void SvNumberFormatsSupplierServiceObject::implEnsureFormatter()
215*cdf0e10cSrcweir {
216*cdf0e10cSrcweir 	if (!m_pOwnFormatter)
217*cdf0e10cSrcweir 	{
218*cdf0e10cSrcweir         // get the office's UI locale
219*cdf0e10cSrcweir 		SvtSysLocale aSysLocale;
220*cdf0e10cSrcweir 		Locale aOfficeLocale = aSysLocale.GetLocaleData().getLocale();
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir         // initi with this locale
223*cdf0e10cSrcweir 		Sequence< Any > aFakedInitProps( 1 );
224*cdf0e10cSrcweir         aFakedInitProps[0] <<= aOfficeLocale;
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 		initialize( aFakedInitProps );
227*cdf0e10cSrcweir 	}
228*cdf0e10cSrcweir }
229*cdf0e10cSrcweir 
230