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 :
21 : #include <sal/macros.h>
22 : #include "lngopt.hxx"
23 : #include "lngreg.hxx"
24 : #include "linguistic/lngprops.hxx"
25 : #include "linguistic/misc.hxx"
26 : #include <tools/debug.hxx>
27 : #include <unotools/lingucfg.hxx>
28 :
29 : #include <cppuhelper/implbase1.hxx>
30 :
31 : #include <cppuhelper/factory.hxx>
32 : #include <cppuhelper/supportsservice.hxx>
33 : #include <com/sun/star/container/XNameAccess.hpp>
34 : #include <com/sun/star/registry/XSimpleRegistry.hpp>
35 : #include <com/sun/star/registry/XRegistryKey.hpp>
36 : #include <com/sun/star/lang/Locale.hpp>
37 : #include <com/sun/star/i18n/ScriptType.hpp>
38 : #include <i18nlangtag/mslangid.hxx>
39 :
40 : using namespace utl;
41 : using namespace osl;
42 : using namespace com::sun::star;
43 : using namespace com::sun::star::container;
44 : using namespace com::sun::star::beans;
45 : using namespace com::sun::star::lang;
46 : using namespace com::sun::star::uno;
47 : using namespace com::sun::star::linguistic2;
48 : using namespace linguistic;
49 :
50 : using namespace com::sun::star::registry;
51 :
52 :
53 :
54 :
55 : // static member intialization
56 : SvtLinguOptions * LinguOptions::pData = NULL;
57 : oslInterlockedCount LinguOptions::nRefCount;
58 :
59 :
60 148 : LinguOptions::LinguOptions()
61 : {
62 148 : if (!pData)
63 : {
64 86 : pData = new SvtLinguOptions;
65 86 : SvtLinguConfig aLinguCfg;
66 86 : aLinguCfg.GetOptions( *pData );
67 : }
68 :
69 148 : osl_atomic_increment( &nRefCount );
70 148 : }
71 :
72 :
73 0 : LinguOptions::LinguOptions(const LinguOptions & /*rOpt*/)
74 : {
75 : DBG_ASSERT( pData, "lng : data missing" );
76 0 : osl_atomic_increment( &nRefCount );
77 0 : }
78 :
79 :
80 138 : LinguOptions::~LinguOptions()
81 : {
82 138 : MutexGuard aGuard( GetLinguMutex() );
83 :
84 138 : if ( osl_atomic_decrement( &nRefCount ) == 0 )
85 : {
86 79 : delete pData; pData = NULL;
87 138 : }
88 138 : }
89 :
90 : struct WID_Name
91 : {
92 : sal_Int32 nWID;
93 : const char *pPropertyName;
94 : };
95 :
96 : //! order of entries is import (see LinguOptions::GetName)
97 : //! since the WID is used as index in this table!
98 : WID_Name aWID_Name[] =
99 : {
100 : { 0, 0 },
101 : { WID_IS_USE_DICTIONARY_LIST, UPN_IS_USE_DICTIONARY_LIST },
102 : { WID_IS_IGNORE_CONTROL_CHARACTERS, UPN_IS_IGNORE_CONTROL_CHARACTERS },
103 : { WID_IS_SPELL_UPPER_CASE, UPN_IS_SPELL_UPPER_CASE },
104 : { WID_IS_SPELL_WITH_DIGITS, UPN_IS_SPELL_WITH_DIGITS },
105 : { WID_IS_SPELL_CAPITALIZATION, UPN_IS_SPELL_CAPITALIZATION },
106 : { WID_HYPH_MIN_LEADING, UPN_HYPH_MIN_LEADING },
107 : { WID_HYPH_MIN_TRAILING, UPN_HYPH_MIN_TRAILING },
108 : { WID_HYPH_MIN_WORD_LENGTH, UPN_HYPH_MIN_WORD_LENGTH },
109 : { WID_DEFAULT_LOCALE, UPN_DEFAULT_LOCALE },
110 : { WID_IS_SPELL_AUTO, UPN_IS_SPELL_AUTO },
111 : { 0, 0 },
112 : { 0, 0 },
113 : { WID_IS_SPELL_SPECIAL, UPN_IS_SPELL_SPECIAL },
114 : { WID_IS_HYPH_AUTO, UPN_IS_HYPH_AUTO },
115 : { WID_IS_HYPH_SPECIAL, UPN_IS_HYPH_SPECIAL },
116 : { WID_IS_WRAP_REVERSE, UPN_IS_WRAP_REVERSE },
117 : { 0, 0 },
118 : { 0, 0 },
119 : { 0, 0 },
120 : { 0, 0 },
121 : { WID_DEFAULT_LANGUAGE, UPN_DEFAULT_LANGUAGE },
122 : { WID_DEFAULT_LOCALE_CJK, UPN_DEFAULT_LOCALE_CJK },
123 : { WID_DEFAULT_LOCALE_CTL, UPN_DEFAULT_LOCALE_CTL }
124 : };
125 :
126 :
127 62 : OUString LinguOptions::GetName( sal_Int32 nWID )
128 : {
129 62 : MutexGuard aGuard( GetLinguMutex() );
130 :
131 62 : OUString aRes;
132 :
133 62 : sal_Int32 nLen = sizeof (aWID_Name) / sizeof (aWID_Name[0]);
134 62 : if (0 <= nWID && nWID < nLen && aWID_Name[ nWID ].nWID == nWID)
135 62 : aRes = OUString::createFromAscii(aWID_Name[nWID].pPropertyName);
136 : else
137 : OSL_FAIL("lng : unknown WID");
138 :
139 62 : return aRes;
140 : }
141 :
142 :
143 :
144 : //! map must be sorted by first entry in alphabetical increasing order.
145 68 : static const SfxItemPropertyMapEntry* lcl_GetLinguProps()
146 : {
147 : static const SfxItemPropertyMapEntry aLinguProps[] =
148 : {
149 : { OUString(UPN_DEFAULT_LANGUAGE), WID_DEFAULT_LANGUAGE,
150 68 : ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
151 : { OUString(UPN_DEFAULT_LOCALE), WID_DEFAULT_LOCALE,
152 68 : ::cppu::UnoType<Locale>::get(), 0, 0 },
153 : { OUString(UPN_DEFAULT_LOCALE_CJK), WID_DEFAULT_LOCALE_CJK,
154 68 : ::cppu::UnoType<Locale>::get(), 0, 0 },
155 : { OUString(UPN_DEFAULT_LOCALE_CTL), WID_DEFAULT_LOCALE_CTL,
156 68 : ::cppu::UnoType<Locale>::get(), 0, 0 },
157 : { OUString(UPN_HYPH_MIN_LEADING), WID_HYPH_MIN_LEADING,
158 68 : ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
159 : { OUString(UPN_HYPH_MIN_TRAILING), WID_HYPH_MIN_TRAILING,
160 68 : ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
161 : { OUString(UPN_HYPH_MIN_WORD_LENGTH), WID_HYPH_MIN_WORD_LENGTH,
162 68 : ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
163 : { OUString(UPN_IS_GERMAN_PRE_REFORM), WID_IS_GERMAN_PRE_REFORM, /*! deprecated !*/
164 68 : ::getBooleanCppuType(), 0, 0 },
165 : { OUString(UPN_IS_HYPH_AUTO), WID_IS_HYPH_AUTO,
166 68 : ::getBooleanCppuType(), 0, 0 },
167 : { OUString(UPN_IS_HYPH_SPECIAL), WID_IS_HYPH_SPECIAL,
168 68 : ::getBooleanCppuType(), 0, 0 },
169 : { OUString(UPN_IS_IGNORE_CONTROL_CHARACTERS), WID_IS_IGNORE_CONTROL_CHARACTERS,
170 68 : ::getBooleanCppuType(), 0, 0 },
171 : { OUString(UPN_IS_SPELL_AUTO), WID_IS_SPELL_AUTO,
172 68 : ::getBooleanCppuType(), 0, 0 },
173 : { OUString(UPN_IS_SPELL_CAPITALIZATION), WID_IS_SPELL_CAPITALIZATION,
174 68 : ::getBooleanCppuType(), 0, 0 },
175 : { OUString(UPN_IS_SPELL_HIDE), WID_IS_SPELL_HIDE, /*! deprecated !*/
176 68 : ::getBooleanCppuType(), 0, 0 },
177 : { OUString(UPN_IS_SPELL_IN_ALL_LANGUAGES), WID_IS_SPELL_IN_ALL_LANGUAGES, /*! deprecated !*/
178 68 : ::getBooleanCppuType(), 0, 0 },
179 : { OUString(UPN_IS_SPELL_SPECIAL), WID_IS_SPELL_SPECIAL,
180 68 : ::getBooleanCppuType(), 0, 0 },
181 : { OUString(UPN_IS_SPELL_UPPER_CASE), WID_IS_SPELL_UPPER_CASE,
182 68 : ::getBooleanCppuType(), 0, 0 },
183 : { OUString(UPN_IS_SPELL_WITH_DIGITS), WID_IS_SPELL_WITH_DIGITS,
184 68 : ::getBooleanCppuType(), 0, 0 },
185 : { OUString(UPN_IS_USE_DICTIONARY_LIST), WID_IS_USE_DICTIONARY_LIST,
186 68 : ::getBooleanCppuType(), 0, 0 },
187 : { OUString(UPN_IS_WRAP_REVERSE), WID_IS_WRAP_REVERSE,
188 68 : ::getBooleanCppuType(), 0, 0 },
189 : { OUString(), 0, css::uno::Type(), 0, 0 }
190 1496 : };
191 68 : return aLinguProps;
192 : }
193 68 : LinguProps::LinguProps() :
194 68 : aEvtListeners (GetLinguMutex()),
195 68 : aPropListeners (GetLinguMutex()),
196 204 : aPropertyMap(lcl_GetLinguProps())
197 : {
198 68 : bDisposing = false;
199 68 : }
200 :
201 126 : void LinguProps::launchEvent( const PropertyChangeEvent &rEvt ) const
202 : {
203 : cppu::OInterfaceContainerHelper *pContainer =
204 126 : aPropListeners.getContainer( rEvt.PropertyHandle );
205 126 : if (pContainer)
206 : {
207 0 : cppu::OInterfaceIteratorHelper aIt( *pContainer );
208 0 : while (aIt.hasMoreElements())
209 : {
210 0 : Reference< XPropertyChangeListener > xRef( aIt.next(), UNO_QUERY );
211 0 : if (xRef.is())
212 0 : xRef->propertyChange( rEvt );
213 0 : }
214 : }
215 126 : }
216 :
217 68 : Reference< XInterface > SAL_CALL LinguProps_CreateInstance(
218 : const Reference< XMultiServiceFactory > & /*rSMgr*/ )
219 : throw(Exception)
220 : {
221 68 : Reference< XInterface > xService = (cppu::OWeakObject*)new LinguProps;
222 68 : return xService;
223 : }
224 :
225 6 : Reference< XPropertySetInfo > SAL_CALL LinguProps::getPropertySetInfo()
226 : throw(RuntimeException, std::exception)
227 : {
228 6 : MutexGuard aGuard( GetLinguMutex() );
229 :
230 : static Reference< XPropertySetInfo > aRef =
231 6 : new SfxItemPropertySetInfo( aPropertyMap );
232 6 : return aRef;
233 : }
234 :
235 68 : void SAL_CALL LinguProps::setPropertyValue(
236 : const OUString& rPropertyName, const Any& rValue )
237 : throw(UnknownPropertyException, PropertyVetoException,
238 : IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
239 : {
240 68 : MutexGuard aGuard( GetLinguMutex() );
241 :
242 68 : const SfxItemPropertySimpleEntry* pCur = aPropertyMap.getByName( rPropertyName );
243 68 : if (pCur)
244 : {
245 68 : Any aOld( aConfig.GetProperty( pCur->nWID ) );
246 68 : if (aOld != rValue && aConfig.SetProperty( pCur->nWID, rValue ))
247 : {
248 : PropertyChangeEvent aChgEvt( (XPropertySet *) this, rPropertyName,
249 64 : sal_False, pCur->nWID, aOld, rValue );
250 64 : launchEvent( aChgEvt );
251 68 : }
252 68 : }
253 68 : }
254 :
255 776 : Any SAL_CALL LinguProps::getPropertyValue( const OUString& rPropertyName )
256 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
257 : {
258 776 : MutexGuard aGuard( GetLinguMutex() );
259 :
260 776 : Any aRet;
261 :
262 776 : const SfxItemPropertySimpleEntry* pCur = aPropertyMap.getByName( rPropertyName );
263 776 : if(pCur)
264 : {
265 776 : aRet = aConfig.GetProperty( pCur->nWID );
266 : }
267 :
268 776 : return aRet;
269 : }
270 :
271 402 : void SAL_CALL LinguProps::addPropertyChangeListener(
272 : const OUString& rPropertyName,
273 : const Reference< XPropertyChangeListener >& rxListener )
274 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
275 : {
276 402 : MutexGuard aGuard( GetLinguMutex() );
277 :
278 402 : if (!bDisposing && rxListener.is())
279 : {
280 402 : const SfxItemPropertySimpleEntry* pCur = aPropertyMap.getByName( rPropertyName );
281 402 : if(pCur)
282 370 : aPropListeners.addInterface( pCur->nWID, rxListener );
283 402 : }
284 402 : }
285 :
286 402 : void SAL_CALL LinguProps::removePropertyChangeListener(
287 : const OUString& rPropertyName,
288 : const Reference< XPropertyChangeListener >& rxListener )
289 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
290 : {
291 402 : MutexGuard aGuard( GetLinguMutex() );
292 :
293 402 : if (!bDisposing && rxListener.is())
294 : {
295 396 : const SfxItemPropertySimpleEntry* pCur = aPropertyMap.getByName( rPropertyName );
296 396 : if(pCur)
297 365 : aPropListeners.removeInterface( pCur->nWID, rxListener );
298 402 : }
299 402 : }
300 :
301 0 : void SAL_CALL LinguProps::addVetoableChangeListener(
302 : const OUString& /*rPropertyName*/,
303 : const Reference< XVetoableChangeListener >& /*xListener*/ )
304 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
305 : {
306 0 : }
307 :
308 0 : void SAL_CALL LinguProps::removeVetoableChangeListener(
309 : const OUString& /*rPropertyName*/,
310 : const Reference< XVetoableChangeListener >& /*xListener*/ )
311 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
312 : {
313 0 : }
314 :
315 :
316 70 : void SAL_CALL LinguProps::setFastPropertyValue( sal_Int32 nHandle, const Any& rValue )
317 : throw(UnknownPropertyException, PropertyVetoException,
318 : IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
319 : {
320 70 : MutexGuard aGuard( GetLinguMutex() );
321 :
322 140 : Any aOld( aConfig.GetProperty( nHandle ) );
323 70 : if (aOld != rValue && aConfig.SetProperty( nHandle, rValue ))
324 : {
325 : PropertyChangeEvent aChgEvt( (XPropertySet *) this,
326 62 : LinguOptions::GetName( nHandle ), sal_False, nHandle, aOld, rValue );
327 62 : launchEvent( aChgEvt );
328 70 : }
329 70 : }
330 :
331 :
332 64088 : Any SAL_CALL LinguProps::getFastPropertyValue( sal_Int32 nHandle )
333 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
334 : {
335 64088 : MutexGuard aGuard( GetLinguMutex() );
336 :
337 64088 : Any aRes( aConfig.GetProperty( nHandle ) );
338 64088 : return aRes;
339 : }
340 :
341 :
342 : Sequence< PropertyValue > SAL_CALL
343 0 : LinguProps::getPropertyValues()
344 : throw(RuntimeException, std::exception)
345 : {
346 0 : MutexGuard aGuard( GetLinguMutex() );
347 :
348 0 : sal_Int32 nLen = aPropertyMap.getSize();
349 0 : Sequence< PropertyValue > aProps( nLen );
350 0 : PropertyValue *pProp = aProps.getArray();
351 0 : PropertyEntryVector_t aPropEntries = aPropertyMap.getPropertyEntries();
352 0 : PropertyEntryVector_t::const_iterator aIt = aPropEntries.begin();
353 0 : for (sal_Int32 i = 0; i < nLen; ++i, ++aIt)
354 : {
355 0 : PropertyValue &rVal = pProp[i];
356 0 : Any aAny( aConfig.GetProperty( aIt->nWID ) );
357 :
358 0 : rVal.Name = aIt->sName;
359 0 : rVal.Handle = aIt->nWID;
360 0 : rVal.Value = aAny;
361 0 : rVal.State = PropertyState_DIRECT_VALUE ;
362 0 : }
363 0 : return aProps;
364 : }
365 :
366 : void SAL_CALL
367 0 : LinguProps::setPropertyValues( const Sequence< PropertyValue >& rProps )
368 : throw(UnknownPropertyException, PropertyVetoException,
369 : IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
370 : {
371 0 : MutexGuard aGuard( GetLinguMutex() );
372 :
373 0 : sal_Int32 nLen = rProps.getLength();
374 0 : const PropertyValue *pVal = rProps.getConstArray();
375 0 : for (sal_Int32 i = 0; i < nLen; ++i)
376 : {
377 0 : const PropertyValue &rVal = pVal[i];
378 0 : setPropertyValue( rVal.Name, rVal.Value );
379 0 : }
380 0 : }
381 :
382 : void SAL_CALL
383 72 : LinguProps::dispose()
384 : throw(RuntimeException, std::exception)
385 : {
386 72 : MutexGuard aGuard( GetLinguMutex() );
387 :
388 72 : if (!bDisposing)
389 : {
390 68 : bDisposing = true;
391 :
392 : //! its too late to save the options here!
393 : // (see AppExitListener for saving)
394 : //aOpt.Save(); // save (possible) changes before exiting
395 :
396 68 : EventObject aEvtObj( (XPropertySet *) this );
397 68 : aEvtListeners.disposeAndClear( aEvtObj );
398 68 : aPropListeners.disposeAndClear( aEvtObj );
399 72 : }
400 72 : }
401 :
402 : void SAL_CALL
403 4 : LinguProps::addEventListener( const Reference< XEventListener >& rxListener )
404 : throw(RuntimeException, std::exception)
405 : {
406 4 : MutexGuard aGuard( GetLinguMutex() );
407 :
408 4 : if (!bDisposing && rxListener.is())
409 4 : aEvtListeners.addInterface( rxListener );
410 4 : }
411 :
412 : void SAL_CALL
413 2 : LinguProps::removeEventListener( const Reference< XEventListener >& rxListener )
414 : throw(RuntimeException, std::exception)
415 : {
416 2 : MutexGuard aGuard( GetLinguMutex() );
417 :
418 2 : if (!bDisposing && rxListener.is())
419 2 : aEvtListeners.removeInterface( rxListener );
420 2 : }
421 :
422 :
423 : // Service specific part
424 :
425 : // XServiceInfo
426 6 : OUString SAL_CALL LinguProps::getImplementationName()
427 : throw(RuntimeException, std::exception)
428 : {
429 6 : MutexGuard aGuard( GetLinguMutex() );
430 6 : return getImplementationName_Static();
431 : }
432 :
433 : // XServiceInfo
434 2 : sal_Bool SAL_CALL LinguProps::supportsService( const OUString& ServiceName )
435 : throw(RuntimeException, std::exception)
436 : {
437 2 : return cppu::supportsService(this, ServiceName);
438 : }
439 :
440 : // XServiceInfo
441 6 : uno::Sequence< OUString > SAL_CALL LinguProps::getSupportedServiceNames()
442 : throw(RuntimeException, std::exception)
443 : {
444 6 : MutexGuard aGuard( GetLinguMutex() );
445 6 : return getSupportedServiceNames_Static();
446 : }
447 :
448 : // ORegistryServiceManager_Static
449 74 : uno::Sequence< OUString > LinguProps::getSupportedServiceNames_Static()
450 : throw()
451 : {
452 74 : MutexGuard aGuard( GetLinguMutex() );
453 :
454 74 : uno::Sequence< OUString > aSNS( 1 ); // more than 1 service possible
455 74 : aSNS.getArray()[0] = "com.sun.star.linguistic2.LinguProperties";
456 74 : return aSNS;
457 : }
458 :
459 390 : bool LinguProps::getPropertyBool(const OUString& aPropertyName) throw (css::uno::RuntimeException)
460 : {
461 390 : uno::Any any = getPropertyValue(aPropertyName);
462 390 : bool b = false;
463 390 : any >>= b;
464 390 : return b;
465 : }
466 :
467 0 : sal_Int16 LinguProps::getPropertyInt16(const OUString& aPropertyName) throw (css::uno::RuntimeException)
468 : {
469 0 : uno::Any any = getPropertyValue(aPropertyName);
470 0 : sal_Int16 b = sal_False;
471 0 : any >>= b;
472 0 : return b;
473 : }
474 :
475 0 : Locale LinguProps::getPropertyLocale(const OUString& aPropertyName) throw (css::uno::RuntimeException)
476 : {
477 0 : uno::Any any = getPropertyValue(aPropertyName);
478 0 : css::lang::Locale b;
479 0 : any >>= b;
480 0 : return b;
481 : }
482 :
483 150 : void * SAL_CALL LinguProps_getFactory( const sal_Char * pImplName,
484 : XMultiServiceFactory *pServiceManager, void * )
485 : {
486 150 : void * pRet = 0;
487 150 : if ( LinguProps::getImplementationName_Static().equalsAscii( pImplName ) )
488 : {
489 : Reference< XSingleServiceFactory > xFactory =
490 : cppu::createOneInstanceFactory(
491 : pServiceManager,
492 : LinguProps::getImplementationName_Static(),
493 : LinguProps_CreateInstance,
494 68 : LinguProps::getSupportedServiceNames_Static());
495 : // acquire, because we return an interface pointer instead of a reference
496 68 : xFactory->acquire();
497 68 : pRet = xFactory.get();
498 : }
499 150 : return pRet;
500 : }
501 :
502 :
503 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|