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 <cppuhelper/supportsservice.hxx>
24 : #include <i18nlangtag/mslangid.hxx>
25 : #include <tools/debug.hxx>
26 : #include <osl/mutex.hxx>
27 : #include <tools/stream.hxx>
28 : #include <svl/strmadpt.hxx>
29 : #include <svl/instrm.hxx>
30 :
31 : #include <registerservices.hxx>
32 :
33 : using namespace ::com::sun::star::uno;
34 : using namespace ::com::sun::star::lang;
35 : using namespace ::com::sun::star::io;
36 : using namespace ::com::sun::star::beans;
37 : using namespace ::com::sun::star::util;
38 : using namespace ::utl;
39 :
40 : #define PERSISTENT_SERVICE_NAME OUString("com.sun.star.util.NumberFormatsSupplier");
41 :
42 0 : Reference< XInterface > SAL_CALL SvNumberFormatsSupplierServiceObject_CreateInstance(const Reference< XMultiServiceFactory >& _rxFactory)
43 : {
44 0 : return static_cast< ::cppu::OWeakObject* >(new SvNumberFormatsSupplierServiceObject( comphelper::getComponentContext(_rxFactory) ));
45 : }
46 :
47 0 : SvNumberFormatsSupplierServiceObject::SvNumberFormatsSupplierServiceObject(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxORB)
48 : :m_pOwnFormatter(NULL)
49 0 : ,m_xORB(_rxORB)
50 : {
51 0 : }
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 0 : Any SAL_CALL SvNumberFormatsSupplierServiceObject::queryAggregation( const Type& _rType ) throw (RuntimeException, std::exception)
63 : {
64 : Any aReturn = ::cppu::queryInterface(_rType,
65 : static_cast< XInitialization* >(this),
66 : static_cast< XPersistObject* >(this),
67 : static_cast< XServiceInfo* >(this)
68 0 : );
69 :
70 0 : if (!aReturn.hasValue())
71 0 : aReturn = SvNumberFormatsSupplierObj::queryAggregation(_rType);
72 :
73 0 : return aReturn;
74 : }
75 :
76 0 : void SAL_CALL SvNumberFormatsSupplierServiceObject::initialize( const Sequence< Any >& _rArguments ) throw(Exception, RuntimeException, std::exception)
77 : {
78 0 : ::osl::MutexGuard aGuard( getSharedMutex() );
79 :
80 : DBG_ASSERT(m_pOwnFormatter == NULL,
81 : "SvNumberFormatsSupplierServiceObject::initialize : already initialized !");
82 : // maybe you already called a method which needed the formatter
83 : // you should use XMultiServiceFactory::createInstanceWithArguments to avoid that
84 0 : if (m_pOwnFormatter)
85 : { // !!! this is only a emergency handling, normally this should not occur !!!
86 0 : delete m_pOwnFormatter;
87 0 : m_pOwnFormatter = NULL;
88 0 : SetNumberFormatter(m_pOwnFormatter);
89 : }
90 :
91 0 : Type aExpectedArgType = ::getCppuType(static_cast<Locale*>(NULL));
92 0 : LanguageType eNewFormatterLanguage = LANGUAGE_ENGLISH_US;
93 : // the default
94 :
95 0 : const Any* pArgs = _rArguments.getConstArray();
96 0 : for (sal_Int32 i=0; i<_rArguments.getLength(); ++i, ++pArgs)
97 : {
98 0 : if (pArgs->getValueType().equals(aExpectedArgType))
99 : {
100 0 : Locale aLocale;
101 0 : *pArgs >>= aLocale;
102 0 : eNewFormatterLanguage = LanguageTag::convertToLanguageType( aLocale, false);
103 : }
104 : #ifdef DBG_UTIL
105 : else
106 : {
107 : OSL_FAIL("SvNumberFormatsSupplierServiceObject::initialize : unknown argument !");
108 : }
109 : #endif
110 : }
111 :
112 0 : m_pOwnFormatter = new SvNumberFormatter( m_xORB, eNewFormatterLanguage);
113 0 : m_pOwnFormatter->SetEvalDateFormat( NF_EVALDATEFORMAT_FORMAT_INTL );
114 0 : SetNumberFormatter(m_pOwnFormatter);
115 0 : }
116 :
117 0 : OUString SAL_CALL SvNumberFormatsSupplierServiceObject::getImplementationName( ) throw(RuntimeException, std::exception)
118 : {
119 0 : return OUString("com.sun.star.uno.util.numbers.SvNumberFormatsSupplierServiceObject");
120 : }
121 :
122 0 : sal_Bool SAL_CALL SvNumberFormatsSupplierServiceObject::supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception)
123 : {
124 0 : return cppu::supportsService(this, _rServiceName);
125 : }
126 :
127 0 : Sequence< OUString > SAL_CALL SvNumberFormatsSupplierServiceObject::getSupportedServiceNames( ) throw(RuntimeException, std::exception)
128 : {
129 0 : Sequence< OUString > aSupported(1);
130 0 : aSupported.getArray()[0] = PERSISTENT_SERVICE_NAME;
131 0 : return aSupported;
132 : }
133 :
134 0 : OUString SAL_CALL SvNumberFormatsSupplierServiceObject::getServiceName( ) throw(RuntimeException, std::exception)
135 : {
136 0 : return PERSISTENT_SERVICE_NAME;
137 : }
138 :
139 0 : void SAL_CALL SvNumberFormatsSupplierServiceObject::write( const Reference< XObjectOutputStream >& _rxOutStream ) throw(IOException, RuntimeException, std::exception)
140 : {
141 0 : ::osl::MutexGuard aGuard( getSharedMutex() );
142 0 : implEnsureFormatter();
143 :
144 0 : Reference< XOutputStream > xStream(_rxOutStream.get());
145 0 : SvLockBytesRef aLockBytes = new SvOutputStreamOpenLockBytes(xStream);
146 0 : SvStream aSvOutputSteam(aLockBytes);
147 :
148 0 : m_pOwnFormatter->Save(aSvOutputSteam);
149 0 : }
150 :
151 0 : void SAL_CALL SvNumberFormatsSupplierServiceObject::read( const Reference< XObjectInputStream >& _rxInStream ) throw(IOException, RuntimeException, std::exception)
152 : {
153 0 : ::osl::MutexGuard aGuard( getSharedMutex() );
154 0 : implEnsureFormatter();
155 :
156 0 : Reference< XInputStream > xStream(_rxInStream.get());
157 0 : SvInputStream aSvInputSteam(xStream);
158 :
159 0 : m_pOwnFormatter->Load(aSvInputSteam);
160 0 : }
161 :
162 0 : Reference< XPropertySet > SAL_CALL SvNumberFormatsSupplierServiceObject::getNumberFormatSettings() throw(RuntimeException, std::exception)
163 : {
164 0 : ::osl::MutexGuard aGuard( getSharedMutex() );
165 0 : implEnsureFormatter();
166 0 : return SvNumberFormatsSupplierObj::getNumberFormatSettings();
167 : }
168 :
169 0 : Reference< XNumberFormats > SAL_CALL SvNumberFormatsSupplierServiceObject::getNumberFormats() throw(RuntimeException, std::exception)
170 : {
171 0 : ::osl::MutexGuard aGuard( getSharedMutex() );
172 0 : implEnsureFormatter();
173 0 : return SvNumberFormatsSupplierObj::getNumberFormats();
174 : }
175 :
176 0 : sal_Int64 SAL_CALL SvNumberFormatsSupplierServiceObject::getSomething( const Sequence< sal_Int8 >& aIdentifier ) throw (RuntimeException, std::exception)
177 : {
178 0 : sal_Int64 nReturn = SvNumberFormatsSupplierObj::getSomething( aIdentifier );
179 0 : if ( nReturn )
180 : // if somebody accesses internals then we should have the formatter
181 0 : implEnsureFormatter();
182 0 : return nReturn;
183 : }
184 :
185 0 : void SvNumberFormatsSupplierServiceObject::implEnsureFormatter()
186 : {
187 0 : if (!m_pOwnFormatter)
188 : {
189 : // get the office's UI locale
190 0 : SvtSysLocale aSysLocale;
191 0 : Locale aOfficeLocale = aSysLocale.GetLocaleData().getLanguageTag().getLocale();
192 :
193 : // initi with this locale
194 0 : Sequence< Any > aFakedInitProps( 1 );
195 0 : aFakedInitProps[0] <<= aOfficeLocale;
196 :
197 0 : initialize( aFakedInitProps );
198 : }
199 0 : }
200 :
201 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|