1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::lang
;
33 using namespace ::com::sun::star::io
;
34 using namespace ::com::sun::star::beans
;
35 using namespace ::com::sun::star::util
;
36 using namespace ::utl
;
38 #define PERSISTENT_SERVICE_NAME OUString("com.sun.star.util.NumberFormatsSupplier");
40 Reference
< XInterface
> SAL_CALL
SvNumberFormatsSupplierServiceObject_CreateInstance(const Reference
< XMultiServiceFactory
>& _rxFactory
)
42 return static_cast< ::cppu::OWeakObject
* >(new SvNumberFormatsSupplierServiceObject( comphelper::getComponentContext(_rxFactory
) ));
45 SvNumberFormatsSupplierServiceObject::SvNumberFormatsSupplierServiceObject(const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& _rxORB
)
46 :m_pOwnFormatter(NULL
)
51 SvNumberFormatsSupplierServiceObject::~SvNumberFormatsSupplierServiceObject()
55 delete m_pOwnFormatter
;
56 m_pOwnFormatter
= NULL
;
60 Any SAL_CALL
SvNumberFormatsSupplierServiceObject::queryAggregation( const Type
& _rType
) throw (RuntimeException
)
62 Any aReturn
= ::cppu::queryInterface(_rType
,
63 static_cast< XInitialization
* >(this),
64 static_cast< XPersistObject
* >(this),
65 static_cast< XServiceInfo
* >(this)
68 if (!aReturn
.hasValue())
69 aReturn
= SvNumberFormatsSupplierObj::queryAggregation(_rType
);
74 void SAL_CALL
SvNumberFormatsSupplierServiceObject::initialize( const Sequence
< Any
>& _rArguments
) throw(Exception
, RuntimeException
)
76 ::osl::MutexGuard
aGuard( getSharedMutex() );
78 DBG_ASSERT(m_pOwnFormatter
== NULL
,
79 "SvNumberFormatsSupplierServiceObject::initialize : already initialized !");
80 // maybe you already called a method which needed the formatter
81 // you should use XMultiServiceFactory::createInstanceWithArguments to avoid that
83 { // !!! this is only a emergency handling, normally this should not occur !!!
84 delete m_pOwnFormatter
;
85 m_pOwnFormatter
= NULL
;
86 SetNumberFormatter(m_pOwnFormatter
);
89 Type aExpectedArgType
= ::getCppuType(static_cast<Locale
*>(NULL
));
90 LanguageType eNewFormatterLanguage
= LANGUAGE_ENGLISH_US
;
93 const Any
* pArgs
= _rArguments
.getConstArray();
94 for (sal_Int32 i
=0; i
<_rArguments
.getLength(); ++i
, ++pArgs
)
96 if (pArgs
->getValueType().equals(aExpectedArgType
))
100 eNewFormatterLanguage
= LanguageTag::convertToLanguageType( aLocale
, false);
105 OSL_FAIL("SvNumberFormatsSupplierServiceObject::initialize : unknown argument !");
110 m_pOwnFormatter
= new SvNumberFormatter( m_xORB
, eNewFormatterLanguage
);
111 m_pOwnFormatter
->SetEvalDateFormat( NF_EVALDATEFORMAT_FORMAT_INTL
);
112 SetNumberFormatter(m_pOwnFormatter
);
115 OUString SAL_CALL
SvNumberFormatsSupplierServiceObject::getImplementationName( ) throw(RuntimeException
)
117 return OUString("com.sun.star.uno.util.numbers.SvNumberFormatsSupplierServiceObject");
120 sal_Bool SAL_CALL
SvNumberFormatsSupplierServiceObject::supportsService( const OUString
& _rServiceName
) throw(RuntimeException
)
122 return cppu::supportsService(this, _rServiceName
);
125 Sequence
< OUString
> SAL_CALL
SvNumberFormatsSupplierServiceObject::getSupportedServiceNames( ) throw(RuntimeException
)
127 Sequence
< OUString
> aSupported(1);
128 aSupported
.getArray()[0] = PERSISTENT_SERVICE_NAME
;
132 OUString SAL_CALL
SvNumberFormatsSupplierServiceObject::getServiceName( ) throw(RuntimeException
)
134 return PERSISTENT_SERVICE_NAME
;
137 void SAL_CALL
SvNumberFormatsSupplierServiceObject::write( const Reference
< XObjectOutputStream
>& _rxOutStream
) throw(IOException
, RuntimeException
)
139 ::osl::MutexGuard
aGuard( getSharedMutex() );
140 implEnsureFormatter();
142 Reference
< XOutputStream
> xStream(_rxOutStream
.get());
143 SvLockBytesRef aLockBytes
= new SvOutputStreamOpenLockBytes(xStream
);
144 SvStream
aSvOutputSteam(aLockBytes
);
146 m_pOwnFormatter
->Save(aSvOutputSteam
);
149 void SAL_CALL
SvNumberFormatsSupplierServiceObject::read( const Reference
< XObjectInputStream
>& _rxInStream
) throw(IOException
, RuntimeException
)
151 ::osl::MutexGuard
aGuard( getSharedMutex() );
152 implEnsureFormatter();
154 Reference
< XInputStream
> xStream(_rxInStream
.get());
155 SvInputStream
aSvInputSteam(xStream
);
157 m_pOwnFormatter
->Load(aSvInputSteam
);
160 Reference
< XPropertySet
> SAL_CALL
SvNumberFormatsSupplierServiceObject::getNumberFormatSettings() throw(RuntimeException
)
162 ::osl::MutexGuard
aGuard( getSharedMutex() );
163 implEnsureFormatter();
164 return SvNumberFormatsSupplierObj::getNumberFormatSettings();
167 Reference
< XNumberFormats
> SAL_CALL
SvNumberFormatsSupplierServiceObject::getNumberFormats() throw(RuntimeException
)
169 ::osl::MutexGuard
aGuard( getSharedMutex() );
170 implEnsureFormatter();
171 return SvNumberFormatsSupplierObj::getNumberFormats();
174 sal_Int64 SAL_CALL
SvNumberFormatsSupplierServiceObject::getSomething( const Sequence
< sal_Int8
>& aIdentifier
) throw (RuntimeException
)
176 sal_Int64 nReturn
= SvNumberFormatsSupplierObj::getSomething( aIdentifier
);
178 // if somebody accesses internals then we should have the formatter
179 implEnsureFormatter();
183 void SvNumberFormatsSupplierServiceObject::implEnsureFormatter()
185 if (!m_pOwnFormatter
)
187 // get the office's UI locale
188 SvtSysLocale aSysLocale
;
189 Locale aOfficeLocale
= aSysLocale
.GetLocaleData().getLanguageTag().getLocale();
191 // initi with this locale
192 Sequence
< Any
> aFakedInitProps( 1 );
193 aFakedInitProps
[0] <<= aOfficeLocale
;
195 initialize( aFakedInitProps
);
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */