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