look for java, javac and javadoc with the .exe suffix on windows
[LibreOffice.git] / linguistic / source / lngopt.cxx
blob0ecf4d455a5396140ce25c54601612a77189cbe2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
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>
28 #include <comphelper/sequence.hxx>
29 #include <cppuhelper/factory.hxx>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <cppuhelper/weak.hxx>
32 #include <com/sun/star/lang/Locale.hpp>
34 using namespace osl;
35 using namespace com::sun::star;
36 using namespace com::sun::star::beans;
37 using namespace com::sun::star::lang;
38 using namespace com::sun::star::uno;
39 using namespace com::sun::star::linguistic2;
40 using namespace linguistic;
42 using namespace com::sun::star::registry;
45 // static member initialization
46 SvtLinguOptions * LinguOptions::pData = nullptr;
47 oslInterlockedCount LinguOptions::nRefCount;
50 LinguOptions::LinguOptions()
52 if (!pData)
54 pData = new SvtLinguOptions;
55 SvtLinguConfig aLinguCfg;
56 aLinguCfg.GetOptions( *pData );
59 osl_atomic_increment( &nRefCount );
63 LinguOptions::LinguOptions(const LinguOptions & /*rOpt*/)
65 DBG_ASSERT( pData, "lng : data missing" );
66 osl_atomic_increment( &nRefCount );
70 LinguOptions::~LinguOptions()
72 MutexGuard aGuard( GetLinguMutex() );
74 if ( osl_atomic_decrement( &nRefCount ) == 0 )
76 delete pData; pData = nullptr;
80 namespace {
82 struct WID_Name
84 sal_Int32 nWID;
85 OUString aPropertyName;
90 //! order of entries is import (see LinguOptions::GetName)
91 //! since the WID is used as index in this table!
92 WID_Name const aWID_Name[] =
94 { 0, "" },
95 { UPH_IS_USE_DICTIONARY_LIST, UPN_IS_USE_DICTIONARY_LIST },
96 { UPH_IS_IGNORE_CONTROL_CHARACTERS, UPN_IS_IGNORE_CONTROL_CHARACTERS },
97 { UPH_IS_SPELL_UPPER_CASE, UPN_IS_SPELL_UPPER_CASE },
98 { UPH_IS_SPELL_WITH_DIGITS, UPN_IS_SPELL_WITH_DIGITS },
99 { UPH_HYPH_MIN_LEADING, UPN_HYPH_MIN_LEADING },
100 { UPH_HYPH_MIN_TRAILING, UPN_HYPH_MIN_TRAILING },
101 { UPH_HYPH_MIN_WORD_LENGTH, UPN_HYPH_MIN_WORD_LENGTH },
102 { UPH_DEFAULT_LOCALE, UPN_DEFAULT_LOCALE },
103 { UPH_IS_SPELL_AUTO, UPN_IS_SPELL_AUTO },
104 { 0, "" },
105 { 0, "" },
106 { UPH_IS_SPELL_SPECIAL, UPN_IS_SPELL_SPECIAL },
107 { UPH_IS_HYPH_AUTO, UPN_IS_HYPH_AUTO },
108 { UPH_IS_HYPH_SPECIAL, UPN_IS_HYPH_SPECIAL },
109 { UPH_IS_WRAP_REVERSE, UPN_IS_WRAP_REVERSE },
110 { 0, "" },
111 { 0, "" },
112 { 0, "" },
113 { 0, "" },
114 { UPH_DEFAULT_LANGUAGE, UPN_DEFAULT_LANGUAGE },
115 { UPH_DEFAULT_LOCALE_CJK, UPN_DEFAULT_LOCALE_CJK },
116 { UPH_DEFAULT_LOCALE_CTL, UPN_DEFAULT_LOCALE_CTL },
117 { UPH_IS_SPELL_CLOSED_COMPOUND, UPN_IS_SPELL_CLOSED_COMPOUND },
118 { UPH_IS_SPELL_HYPHENATED_COMPOUND, UPN_IS_SPELL_HYPHENATED_COMPOUND }
122 OUString LinguOptions::GetName( sal_Int32 nWID )
124 MutexGuard aGuard( GetLinguMutex() );
126 OUString aRes;
128 if (0 <= nWID && o3tl::make_unsigned(nWID) < SAL_N_ELEMENTS(aWID_Name)
129 && aWID_Name[ nWID ].nWID == nWID)
130 aRes = aWID_Name[nWID].aPropertyName;
131 else
132 OSL_FAIL("lng : unknown WID");
134 return aRes;
138 //! map must be sorted by first entry in alphabetical increasing order.
139 static std::span<const SfxItemPropertyMapEntry> lcl_GetLinguProps()
141 static const SfxItemPropertyMapEntry aLinguProps[] =
143 { UPN_DEFAULT_LANGUAGE, UPH_DEFAULT_LANGUAGE,
144 ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
145 { UPN_DEFAULT_LOCALE, UPH_DEFAULT_LOCALE,
146 ::cppu::UnoType<Locale>::get(), 0, 0 },
147 { UPN_DEFAULT_LOCALE_CJK, UPH_DEFAULT_LOCALE_CJK,
148 ::cppu::UnoType<Locale>::get(), 0, 0 },
149 { UPN_DEFAULT_LOCALE_CTL, UPH_DEFAULT_LOCALE_CTL,
150 ::cppu::UnoType<Locale>::get(), 0, 0 },
151 { UPN_HYPH_MIN_LEADING, UPH_HYPH_MIN_LEADING,
152 ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
153 { UPN_HYPH_MIN_TRAILING, UPH_HYPH_MIN_TRAILING,
154 ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
155 { UPN_HYPH_MIN_WORD_LENGTH, UPH_HYPH_MIN_WORD_LENGTH,
156 ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
157 { UPN_IS_HYPH_AUTO, UPH_IS_HYPH_AUTO,
158 cppu::UnoType<bool>::get(), 0, 0 },
159 { UPN_IS_HYPH_SPECIAL, UPH_IS_HYPH_SPECIAL,
160 cppu::UnoType<bool>::get(), 0, 0 },
161 { UPN_IS_IGNORE_CONTROL_CHARACTERS, UPH_IS_IGNORE_CONTROL_CHARACTERS,
162 cppu::UnoType<bool>::get(), 0, 0 },
163 { UPN_IS_SPELL_AUTO, UPH_IS_SPELL_AUTO,
164 cppu::UnoType<bool>::get(), 0, 0 },
165 { UPN_IS_SPELL_CLOSED_COMPOUND, UPH_IS_SPELL_CLOSED_COMPOUND,
166 cppu::UnoType<bool>::get(), 0, 0 },
167 { UPN_IS_SPELL_HYPHENATED_COMPOUND, UPH_IS_SPELL_HYPHENATED_COMPOUND,
168 cppu::UnoType<bool>::get(), 0, 0 },
169 { UPN_IS_SPELL_SPECIAL, UPH_IS_SPELL_SPECIAL,
170 cppu::UnoType<bool>::get(), 0, 0 },
171 { UPN_IS_SPELL_UPPER_CASE, UPH_IS_SPELL_UPPER_CASE,
172 cppu::UnoType<bool>::get(), 0, 0 },
173 { UPN_IS_SPELL_WITH_DIGITS, UPH_IS_SPELL_WITH_DIGITS,
174 cppu::UnoType<bool>::get(), 0, 0 },
175 { UPN_IS_USE_DICTIONARY_LIST, UPH_IS_USE_DICTIONARY_LIST,
176 cppu::UnoType<bool>::get(), 0, 0 },
177 { UPN_IS_WRAP_REVERSE, UPH_IS_WRAP_REVERSE,
178 cppu::UnoType<bool>::get(), 0, 0 },
180 return aLinguProps;
182 LinguProps::LinguProps() :
183 aEvtListeners (GetLinguMutex()),
184 aPropListeners (GetLinguMutex()),
185 aPropertyMap(lcl_GetLinguProps())
187 bDisposing = false;
190 void LinguProps::launchEvent( const PropertyChangeEvent &rEvt ) const
192 comphelper::OInterfaceContainerHelper3<XPropertyChangeListener> *pContainer =
193 aPropListeners.getContainer( rEvt.PropertyHandle );
194 if (pContainer)
195 pContainer->notifyEach( &XPropertyChangeListener::propertyChange, rEvt );
198 Reference< XPropertySetInfo > SAL_CALL LinguProps::getPropertySetInfo()
200 MutexGuard aGuard( GetLinguMutex() );
202 static Reference< XPropertySetInfo > aRef =
203 new SfxItemPropertySetInfo( aPropertyMap );
204 return aRef;
207 void SAL_CALL LinguProps::setPropertyValue(
208 const OUString& rPropertyName, const Any& rValue )
210 MutexGuard aGuard( GetLinguMutex() );
212 const SfxItemPropertyMapEntry* pCur = aPropertyMap.getByName( rPropertyName );
213 if (pCur)
215 Any aOld( aConfig.GetProperty( pCur->nWID ) );
216 if (aOld != rValue && aConfig.SetProperty( pCur->nWID, rValue ))
218 PropertyChangeEvent aChgEvt( static_cast<XPropertySet *>(this), rPropertyName,
219 false, pCur->nWID, aOld, rValue );
220 launchEvent( aChgEvt );
225 Any SAL_CALL LinguProps::getPropertyValue( const OUString& rPropertyName )
227 MutexGuard aGuard( GetLinguMutex() );
229 Any aRet;
231 const SfxItemPropertyMapEntry* pCur = aPropertyMap.getByName( rPropertyName );
232 if(pCur)
234 aRet = aConfig.GetProperty( pCur->nWID );
237 return aRet;
240 void SAL_CALL LinguProps::addPropertyChangeListener(
241 const OUString& rPropertyName,
242 const Reference< XPropertyChangeListener >& rxListener )
244 MutexGuard aGuard( GetLinguMutex() );
246 if (!bDisposing && rxListener.is())
248 const SfxItemPropertyMapEntry* pCur = aPropertyMap.getByName( rPropertyName );
249 if(pCur)
250 aPropListeners.addInterface( pCur->nWID, rxListener );
254 void SAL_CALL LinguProps::removePropertyChangeListener(
255 const OUString& rPropertyName,
256 const Reference< XPropertyChangeListener >& rxListener )
258 MutexGuard aGuard( GetLinguMutex() );
260 if (!bDisposing && rxListener.is())
262 const SfxItemPropertyMapEntry* pCur = aPropertyMap.getByName( rPropertyName );
263 if(pCur)
264 aPropListeners.removeInterface( pCur->nWID, rxListener );
268 void SAL_CALL LinguProps::addVetoableChangeListener(
269 const OUString& /*rPropertyName*/,
270 const Reference< XVetoableChangeListener >& /*xListener*/ )
274 void SAL_CALL LinguProps::removeVetoableChangeListener(
275 const OUString& /*rPropertyName*/,
276 const Reference< XVetoableChangeListener >& /*xListener*/ )
281 void SAL_CALL LinguProps::setFastPropertyValue( sal_Int32 nHandle, const Any& rValue )
283 MutexGuard aGuard( GetLinguMutex() );
285 Any aOld( aConfig.GetProperty( nHandle ) );
286 if (aOld != rValue && aConfig.SetProperty( nHandle, rValue ))
288 PropertyChangeEvent aChgEvt( static_cast<XPropertySet *>(this),
289 LinguOptions::GetName( nHandle ), false, nHandle, aOld, rValue );
290 launchEvent( aChgEvt );
295 Any SAL_CALL LinguProps::getFastPropertyValue( sal_Int32 nHandle )
297 MutexGuard aGuard( GetLinguMutex() );
299 Any aRes( aConfig.GetProperty( nHandle ) );
300 return aRes;
304 Sequence< PropertyValue > SAL_CALL
305 LinguProps::getPropertyValues()
307 MutexGuard aGuard( GetLinguMutex() );
309 std::vector<PropertyValue> aProps;
310 aProps.reserve(aPropertyMap.getPropertyEntries().size());
311 for(auto pEntry : aPropertyMap.getPropertyEntries())
312 aProps.push_back(PropertyValue(pEntry->aName, pEntry->nWID,
313 aConfig.GetProperty(pEntry->nWID),
314 css::beans::PropertyState_DIRECT_VALUE));
315 return comphelper::containerToSequence(aProps);
318 void SAL_CALL
319 LinguProps::setPropertyValues( const Sequence< PropertyValue >& rProps )
321 MutexGuard aGuard( GetLinguMutex() );
323 for (const PropertyValue &rVal : rProps)
325 setPropertyValue( rVal.Name, rVal.Value );
329 void SAL_CALL
330 LinguProps::dispose()
332 MutexGuard aGuard( GetLinguMutex() );
334 if (!bDisposing)
336 bDisposing = true;
338 //! it's too late to save the options here!
339 // (see AppExitListener for saving)
340 //aOpt.Save(); // save (possible) changes before exiting
342 EventObject aEvtObj( static_cast<XPropertySet *>(this) );
343 aEvtListeners.disposeAndClear( aEvtObj );
344 aPropListeners.disposeAndClear( aEvtObj );
348 void SAL_CALL
349 LinguProps::addEventListener( const Reference< XEventListener >& rxListener )
351 MutexGuard aGuard( GetLinguMutex() );
353 if (!bDisposing && rxListener.is())
354 aEvtListeners.addInterface( rxListener );
357 void SAL_CALL
358 LinguProps::removeEventListener( const Reference< XEventListener >& rxListener )
360 MutexGuard aGuard( GetLinguMutex() );
362 if (!bDisposing && rxListener.is())
363 aEvtListeners.removeInterface( rxListener );
367 // Service specific part
369 // XServiceInfo
370 OUString SAL_CALL LinguProps::getImplementationName()
372 return "com.sun.star.lingu2.LinguProps";
375 // XServiceInfo
376 sal_Bool SAL_CALL LinguProps::supportsService( const OUString& ServiceName )
378 return cppu::supportsService(this, ServiceName);
381 // XServiceInfo
382 uno::Sequence< OUString > SAL_CALL LinguProps::getSupportedServiceNames()
384 return { "com.sun.star.linguistic2.LinguProperties" };
387 bool LinguProps::getPropertyBool(const OUString& aPropertyName)
389 uno::Any any = getPropertyValue(aPropertyName);
390 bool b = false;
391 any >>= b;
392 return b;
395 sal_Int16 LinguProps::getPropertyInt16(const OUString& aPropertyName)
397 uno::Any any = getPropertyValue(aPropertyName);
398 sal_Int16 b = 0;
399 any >>= b;
400 return b;
403 Locale LinguProps::getPropertyLocale(const OUString& aPropertyName)
405 uno::Any any = getPropertyValue(aPropertyName);
406 css::lang::Locale b;
407 any >>= b;
408 return b;
411 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
412 linguistic_LinguProps_get_implementation(
413 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
415 return cppu::acquire(new LinguProps());
419 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */