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