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