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