LibreOffice Module linguistic (master) 1
lngopt.cxx
Go to the documentation of this file.
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
21#include <sal/macros.h>
22#include "lngopt.hxx"
23#include <linguistic/misc.hxx>
24#include <o3tl/safeint.hxx>
25#include <tools/debug.hxx>
26#include <unotools/lingucfg.hxx>
27
31#include <cppuhelper/weak.hxx>
32#include <com/sun/star/lang/Locale.hpp>
33
34using namespace utl;
35using namespace osl;
36using namespace com::sun::star;
37using namespace com::sun::star::beans;
38using namespace com::sun::star::lang;
39using namespace com::sun::star::uno;
40using namespace com::sun::star::linguistic2;
41using namespace linguistic;
42
43using namespace com::sun::star::registry;
44
45
46// static member initialization
48oslInterlockedCount LinguOptions::nRefCount;
49
50
52{
53 if (!pData)
54 {
56 SvtLinguConfig aLinguCfg;
57 aLinguCfg.GetOptions( *pData );
58 }
59
60 osl_atomic_increment( &nRefCount );
61}
62
63
65{
66 DBG_ASSERT( pData, "lng : data missing" );
67 osl_atomic_increment( &nRefCount );
68}
69
70
72{
73 MutexGuard aGuard( GetLinguMutex() );
74
75 if ( osl_atomic_decrement( &nRefCount ) == 0 )
76 {
77 delete pData; pData = nullptr;
78 }
79}
80
81namespace {
82
83struct WID_Name
84{
85 sal_Int32 nWID;
86 OUString aPropertyName;
87};
88
89}
90
93WID_Name const aWID_Name[] =
94{
95 { 0, "" },
106 { 0, "" },
107 { 0, "" },
112 { 0, "" },
113 { 0, "" },
114 { 0, "" },
115 { 0, "" },
121};
122
123
124OUString LinguOptions::GetName( sal_Int32 nWID )
125{
126 MutexGuard aGuard( GetLinguMutex() );
127
128 OUString aRes;
129
130 if (0 <= nWID && o3tl::make_unsigned(nWID) < SAL_N_ELEMENTS(aWID_Name)
131 && aWID_Name[ nWID ].nWID == nWID)
132 aRes = aWID_Name[nWID].aPropertyName;
133 else
134 OSL_FAIL("lng : unknown WID");
135
136 return aRes;
137}
138
139
142{
143 static const SfxItemPropertyMapEntry aLinguProps[] =
144 {
160 cppu::UnoType<bool>::get(), 0, 0 },
162 cppu::UnoType<bool>::get(), 0, 0 },
164 cppu::UnoType<bool>::get(), 0, 0 },
166 cppu::UnoType<bool>::get(), 0, 0 },
168 cppu::UnoType<bool>::get(), 0, 0 },
170 cppu::UnoType<bool>::get(), 0, 0 },
172 cppu::UnoType<bool>::get(), 0, 0 },
174 cppu::UnoType<bool>::get(), 0, 0 },
176 cppu::UnoType<bool>::get(), 0, 0 },
178 cppu::UnoType<bool>::get(), 0, 0 },
180 cppu::UnoType<bool>::get(), 0, 0 },
182 cppu::UnoType<bool>::get(), 0, 0 },
184 cppu::UnoType<bool>::get(), 0, 0 },
185 };
186 return aLinguProps;
187}
189 aEvtListeners (GetLinguMutex()),
190 aPropListeners (GetLinguMutex()),
191 aPropertyMap(lcl_GetLinguProps())
192{
193 bDisposing = false;
194}
195
196void LinguProps::launchEvent( const PropertyChangeEvent &rEvt ) const
197{
199 aPropListeners.getContainer( rEvt.PropertyHandle );
200 if (pContainer)
201 pContainer->notifyEach( &XPropertyChangeListener::propertyChange, rEvt );
202}
203
205{
206 MutexGuard aGuard( GetLinguMutex() );
207
210 return aRef;
211}
212
214 const OUString& rPropertyName, const Any& rValue )
215{
216 MutexGuard aGuard( GetLinguMutex() );
217
218 const SfxItemPropertyMapEntry* pCur = aPropertyMap.getByName( rPropertyName );
219 if (pCur)
220 {
221 Any aOld( aConfig.GetProperty( pCur->nWID ) );
222 if (aOld != rValue && aConfig.SetProperty( pCur->nWID, rValue ))
223 {
224 PropertyChangeEvent aChgEvt( static_cast<XPropertySet *>(this), rPropertyName,
225 false, pCur->nWID, aOld, rValue );
226 launchEvent( aChgEvt );
227 }
228 }
229}
230
231Any SAL_CALL LinguProps::getPropertyValue( const OUString& rPropertyName )
232{
233 MutexGuard aGuard( GetLinguMutex() );
234
235 Any aRet;
236
237 const SfxItemPropertyMapEntry* pCur = aPropertyMap.getByName( rPropertyName );
238 if(pCur)
239 {
240 aRet = aConfig.GetProperty( pCur->nWID );
241 }
242
243 return aRet;
244}
245
247 const OUString& rPropertyName,
248 const Reference< XPropertyChangeListener >& rxListener )
249{
250 MutexGuard aGuard( GetLinguMutex() );
251
252 if (!bDisposing && rxListener.is())
253 {
254 const SfxItemPropertyMapEntry* pCur = aPropertyMap.getByName( rPropertyName );
255 if(pCur)
256 aPropListeners.addInterface( pCur->nWID, rxListener );
257 }
258}
259
261 const OUString& rPropertyName,
262 const Reference< XPropertyChangeListener >& rxListener )
263{
264 MutexGuard aGuard( GetLinguMutex() );
265
266 if (!bDisposing && rxListener.is())
267 {
268 const SfxItemPropertyMapEntry* pCur = aPropertyMap.getByName( rPropertyName );
269 if(pCur)
270 aPropListeners.removeInterface( pCur->nWID, rxListener );
271 }
272}
273
275 const OUString& /*rPropertyName*/,
276 const Reference< XVetoableChangeListener >& /*xListener*/ )
277{
278}
279
281 const OUString& /*rPropertyName*/,
282 const Reference< XVetoableChangeListener >& /*xListener*/ )
283{
284}
285
286
287void SAL_CALL LinguProps::setFastPropertyValue( sal_Int32 nHandle, const Any& rValue )
288{
289 MutexGuard aGuard( GetLinguMutex() );
290
291 Any aOld( aConfig.GetProperty( nHandle ) );
292 if (aOld != rValue && aConfig.SetProperty( nHandle, rValue ))
293 {
294 PropertyChangeEvent aChgEvt( static_cast<XPropertySet *>(this),
295 LinguOptions::GetName( nHandle ), false, nHandle, aOld, rValue );
296 launchEvent( aChgEvt );
297 }
298}
299
300
301Any SAL_CALL LinguProps::getFastPropertyValue( sal_Int32 nHandle )
302{
303 MutexGuard aGuard( GetLinguMutex() );
304
305 Any aRes( aConfig.GetProperty( nHandle ) );
306 return aRes;
307}
308
309
312{
313 MutexGuard aGuard( GetLinguMutex() );
314
315 std::vector<PropertyValue> aProps;
316 aProps.reserve(aPropertyMap.getPropertyEntries().size());
317 for(auto pEntry : aPropertyMap.getPropertyEntries())
318 aProps.push_back(PropertyValue(pEntry->aName, pEntry->nWID,
319 aConfig.GetProperty(pEntry->nWID),
320 css::beans::PropertyState_DIRECT_VALUE));
321 return comphelper::containerToSequence(aProps);
322}
323
324void SAL_CALL
326{
327 MutexGuard aGuard( GetLinguMutex() );
328
329 for (const PropertyValue &rVal : rProps)
330 {
331 setPropertyValue( rVal.Name, rVal.Value );
332 }
333}
334
335void SAL_CALL
337{
338 MutexGuard aGuard( GetLinguMutex() );
339
340 if (!bDisposing)
341 {
342 bDisposing = true;
343
345 // (see AppExitListener for saving)
346 //aOpt.Save(); // save (possible) changes before exiting
347
348 EventObject aEvtObj( static_cast<XPropertySet *>(this) );
351 }
352}
353
354void SAL_CALL
356{
357 MutexGuard aGuard( GetLinguMutex() );
358
359 if (!bDisposing && rxListener.is())
360 aEvtListeners.addInterface( rxListener );
361}
362
363void SAL_CALL
365{
366 MutexGuard aGuard( GetLinguMutex() );
367
368 if (!bDisposing && rxListener.is())
369 aEvtListeners.removeInterface( rxListener );
370}
371
372
373// Service specific part
374
375// XServiceInfo
377{
378 return "com.sun.star.lingu2.LinguProps";
379}
380
381// XServiceInfo
382sal_Bool SAL_CALL LinguProps::supportsService( const OUString& ServiceName )
383{
385}
386
387// XServiceInfo
388uno::Sequence< OUString > SAL_CALL LinguProps::getSupportedServiceNames()
389{
390 return { "com.sun.star.linguistic2.LinguProperties" };
391}
392
393bool LinguProps::getPropertyBool(const OUString& aPropertyName)
394{
395 uno::Any any = getPropertyValue(aPropertyName);
396 bool b = false;
397 any >>= b;
398 return b;
399}
400
401sal_Int16 LinguProps::getPropertyInt16(const OUString& aPropertyName)
402{
403 uno::Any any = getPropertyValue(aPropertyName);
404 sal_Int16 b = 0;
405 any >>= b;
406 return b;
407}
408
409Locale LinguProps::getPropertyLocale(const OUString& aPropertyName)
410{
411 uno::Any any = getPropertyValue(aPropertyName);
412 css::lang::Locale b;
413 any >>= b;
414 return b;
415}
416
417extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
419 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
420{
421 return cppu::acquire(new LinguProps());
422}
423
424
425/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const Any & any
static oslInterlockedCount nRefCount
Definition: lngopt.hxx:48
~LinguOptions()
Definition: lngopt.cxx:71
static OUString GetName(sal_Int32 nWID)
Definition: lngopt.cxx:124
static SvtLinguOptions * pData
Definition: lngopt.hxx:47
LinguOptions()
Definition: lngopt.cxx:51
::comphelper::OInterfaceContainerHelper3< css::lang::XEventListener > aEvtListeners
Definition: lngopt.hxx:74
virtual void SAL_CALL setPropertyValues(const css::uno::Sequence< css::beans::PropertyValue > &aProps) override
Definition: lngopt.cxx:325
virtual css::uno::Sequence< css::beans::PropertyValue > SAL_CALL getPropertyValues() override
Definition: lngopt.cxx:311
bool bDisposing
Definition: lngopt.hxx:80
OPropertyListenerContainerHelper aPropListeners
Definition: lngopt.hxx:75
SfxItemPropertyMap aPropertyMap
Definition: lngopt.hxx:77
virtual void SAL_CALL removeVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &rxListener) override
Definition: lngopt.cxx:280
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: lngopt.cxx:388
virtual void SAL_CALL dispose() override
Definition: lngopt.cxx:336
LinguProps()
Definition: lngopt.cxx:188
css::lang::Locale getPropertyLocale(const OUString &aPropertyName)
Definition: lngopt.cxx:409
void launchEvent(const css::beans::PropertyChangeEvent &rEvt) const
Definition: lngopt.cxx:196
virtual void SAL_CALL removePropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &rxListener) override
Definition: lngopt.cxx:260
virtual void SAL_CALL addVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &rxListener) override
Definition: lngopt.cxx:274
virtual OUString SAL_CALL getImplementationName() override
Definition: lngopt.cxx:376
virtual void SAL_CALL setPropertyValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
Definition: lngopt.cxx:213
virtual css::uno::Any SAL_CALL getFastPropertyValue(sal_Int32 nHandle) override
Definition: lngopt.cxx:301
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &rxListener) override
Definition: lngopt.cxx:355
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
Definition: lngopt.cxx:204
sal_Int16 getPropertyInt16(const OUString &aPropertyName)
Definition: lngopt.cxx:401
virtual void SAL_CALL addPropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &rxListener) override
Definition: lngopt.cxx:246
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &PropertyName) override
Definition: lngopt.cxx:231
virtual void SAL_CALL setFastPropertyValue(sal_Int32 nHandle, const css::uno::Any &aValue) override
Definition: lngopt.cxx:287
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &rxListener) override
Definition: lngopt.cxx:364
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: lngopt.cxx:382
SvtLinguConfig aConfig
Definition: lngopt.hxx:78
bool getPropertyBool(const OUString &aPropertyName)
Definition: lngopt.cxx:393
const SfxItemPropertyMapEntry * getByName(std::u16string_view rName) const
const o3tl::sorted_vector< const SfxItemPropertyMapEntry *, SfxItemPropertyMapCompare > & getPropertyEntries() const
void GetOptions(SvtLinguOptions &rOptions) const
css::uno::Any GetProperty(std::u16string_view rPropertyName) const
bool SetProperty(std::u16string_view rPropertyName, const css::uno::Any &rValue)
sal_Int32 addInterface(const css::uno::Reference< ListenerT > &rxIFace)
void disposeAndClear(const css::lang::EventObject &rEvt)
sal_Int32 removeInterface(const css::uno::Reference< ListenerT > &rxIFace)
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
sal_Int32 addInterface(const key &rKey, const css::uno::Reference< listener > &rListener)
sal_Int32 removeInterface(const key &rKey, const css::uno::Reference< listener > &rListener)
void disposeAndClear(const css::lang::EventObject &rEvt)
OInterfaceContainerHelper3< listener > * getContainer(const key &rKey) const
css::uno::Type const & get()
size_type size() const
#define DBG_ASSERT(sCon, aError)
constexpr OUStringLiteral UPN_IS_GERMAN_PRE_REFORM
#define UPH_HYPH_MIN_WORD_LENGTH
#define UPH_IS_SPELL_WITH_DIGITS
constexpr OUStringLiteral UPN_IS_SPELL_AUTO
#define UPH_IS_SPELL_SPECIAL
#define UPH_IS_SPELL_CLOSED_COMPOUND
constexpr OUStringLiteral UPN_IS_HYPH_AUTO
#define UPH_DEFAULT_LOCALE_CTL
#define UPH_IS_HYPH_SPECIAL
#define UPH_HYPH_MIN_LEADING
constexpr OUStringLiteral UPN_IS_SPELL_UPPER_CASE
#define UPH_IS_SPELL_HYPHENATED_COMPOUND
constexpr OUStringLiteral UPN_IS_WRAP_REVERSE
constexpr OUStringLiteral UPN_IS_SPELL_CLOSED_COMPOUND
#define UPH_IS_GERMAN_PRE_REFORM
constexpr OUStringLiteral UPN_IS_USE_DICTIONARY_LIST
constexpr OUStringLiteral UPN_HYPH_MIN_WORD_LENGTH
#define UPH_DEFAULT_LANGUAGE
constexpr OUStringLiteral UPN_HYPH_MIN_LEADING
constexpr OUStringLiteral UPN_IS_SPELL_WITH_DIGITS
constexpr OUStringLiteral UPN_IS_SPELL_HYPHENATED_COMPOUND
#define UPH_IS_HYPH_AUTO
#define UPH_IS_SPELL_UPPER_CASE
constexpr OUStringLiteral UPN_IS_IGNORE_CONTROL_CHARACTERS
#define UPH_HYPH_MIN_TRAILING
constexpr OUStringLiteral UPN_DEFAULT_LANGUAGE
constexpr OUStringLiteral UPN_IS_HYPH_SPECIAL
constexpr OUStringLiteral UPN_DEFAULT_LOCALE
constexpr OUStringLiteral UPN_DEFAULT_LOCALE_CTL
constexpr OUStringLiteral UPN_HYPH_MIN_TRAILING
#define UPH_DEFAULT_LOCALE
#define UPH_IS_WRAP_REVERSE
constexpr OUStringLiteral UPN_DEFAULT_LOCALE_CJK
#define UPH_IS_USE_DICTIONARY_LIST
#define UPH_IS_SPELL_CAPITALIZATION
#define UPH_DEFAULT_LOCALE_CJK
#define UPH_IS_SPELL_AUTO
constexpr OUStringLiteral UPN_IS_SPELL_CAPITALIZATION
constexpr OUStringLiteral UPN_IS_SPELL_SPECIAL
#define UPH_IS_IGNORE_CONTROL_CHARACTERS
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * linguistic_LinguProps_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Definition: lngopt.cxx:418
static o3tl::span< const SfxItemPropertyMapEntry > lcl_GetLinguProps()
map must be sorted by first entry in alphabetical increasing order.
Definition: lngopt.cxx:141
WID_Name const aWID_Name[]
order of entries is import (see LinguOptions::GetName) since the WID is used as index in this table!
Definition: lngopt.cxx:93
#define SAL_N_ELEMENTS(arr)
class SAL_NO_VTABLE XPropertySet
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
osl::Mutex & GetLinguMutex()
! multi-thread safe mutex for all platforms !!
Definition: misc.cxx:60
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
sal_Int32 nHandle
unsigned char sal_Bool