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 0 : LinguOptions::LinguOptions()
61 : {
62 0 : if (!pData)
63 : {
64 0 : pData = new SvtLinguOptions;
65 0 : SvtLinguConfig aLinguCfg;
66 0 : aLinguCfg.GetOptions( *pData );
67 : }
68 :
69 0 : osl_atomic_increment( &nRefCount );
70 0 : }
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 0 : LinguOptions::~LinguOptions()
81 : {
82 0 : MutexGuard aGuard( GetLinguMutex() );
83 :
84 0 : if ( osl_atomic_decrement( &nRefCount ) == 0 )
85 : {
86 0 : delete pData; pData = NULL;
87 0 : }
88 0 : }
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 0 : OUString LinguOptions::GetName( sal_Int32 nWID )
128 : {
129 0 : MutexGuard aGuard( GetLinguMutex() );
130 :
131 0 : OUString aRes;
132 :
133 0 : sal_Int32 nLen = sizeof (aWID_Name) / sizeof (aWID_Name[0]);
134 0 : if (0 <= nWID && nWID < nLen && aWID_Name[ nWID ].nWID == nWID)
135 0 : aRes = OUString::createFromAscii(aWID_Name[nWID].pPropertyName);
136 : else
137 : OSL_FAIL("lng : unknown WID");
138 :
139 0 : return aRes;
140 : }
141 :
142 :
143 :
144 : //! map must be sorted by first entry in alphabetical increasing order.
145 0 : static const SfxItemPropertyMapEntry* lcl_GetLinguProps()
146 : {
147 : static const SfxItemPropertyMapEntry aLinguProps[] =
148 : {
149 : { OUString(UPN_DEFAULT_LANGUAGE), WID_DEFAULT_LANGUAGE,
150 0 : ::getCppuType( (sal_Int16*)0 ), 0, 0 },
151 : { OUString(UPN_DEFAULT_LOCALE), WID_DEFAULT_LOCALE,
152 0 : ::getCppuType( (Locale* )0), 0, 0 },
153 : { OUString(UPN_DEFAULT_LOCALE_CJK), WID_DEFAULT_LOCALE_CJK,
154 0 : ::getCppuType( (Locale* )0), 0, 0 },
155 : { OUString(UPN_DEFAULT_LOCALE_CTL), WID_DEFAULT_LOCALE_CTL,
156 0 : ::getCppuType( (Locale* )0), 0, 0 },
157 : { OUString(UPN_HYPH_MIN_LEADING), WID_HYPH_MIN_LEADING,
158 0 : ::getCppuType( (sal_Int16*)0 ), 0, 0 },
159 : { OUString(UPN_HYPH_MIN_TRAILING), WID_HYPH_MIN_TRAILING,
160 0 : ::getCppuType( (sal_Int16*)0 ), 0, 0 },
161 : { OUString(UPN_HYPH_MIN_WORD_LENGTH), WID_HYPH_MIN_WORD_LENGTH,
162 0 : ::getCppuType( (sal_Int16*)0 ), 0, 0 },
163 : { OUString(UPN_IS_GERMAN_PRE_REFORM), WID_IS_GERMAN_PRE_REFORM, /*! deprecated !*/
164 0 : ::getBooleanCppuType(), 0, 0 },
165 : { OUString(UPN_IS_HYPH_AUTO), WID_IS_HYPH_AUTO,
166 0 : ::getBooleanCppuType(), 0, 0 },
167 : { OUString(UPN_IS_HYPH_SPECIAL), WID_IS_HYPH_SPECIAL,
168 0 : ::getBooleanCppuType(), 0, 0 },
169 : { OUString(UPN_IS_IGNORE_CONTROL_CHARACTERS), WID_IS_IGNORE_CONTROL_CHARACTERS,
170 0 : ::getBooleanCppuType(), 0, 0 },
171 : { OUString(UPN_IS_SPELL_AUTO), WID_IS_SPELL_AUTO,
172 0 : ::getBooleanCppuType(), 0, 0 },
173 : { OUString(UPN_IS_SPELL_CAPITALIZATION), WID_IS_SPELL_CAPITALIZATION,
174 0 : ::getBooleanCppuType(), 0, 0 },
175 : { OUString(UPN_IS_SPELL_HIDE), WID_IS_SPELL_HIDE, /*! deprecated !*/
176 0 : ::getBooleanCppuType(), 0, 0 },
177 : { OUString(UPN_IS_SPELL_IN_ALL_LANGUAGES), WID_IS_SPELL_IN_ALL_LANGUAGES, /*! deprecated !*/
178 0 : ::getBooleanCppuType(), 0, 0 },
179 : { OUString(UPN_IS_SPELL_SPECIAL), WID_IS_SPELL_SPECIAL,
180 0 : ::getBooleanCppuType(), 0, 0 },
181 : { OUString(UPN_IS_SPELL_UPPER_CASE), WID_IS_SPELL_UPPER_CASE,
182 0 : ::getBooleanCppuType(), 0, 0 },
183 : { OUString(UPN_IS_SPELL_WITH_DIGITS), WID_IS_SPELL_WITH_DIGITS,
184 0 : ::getBooleanCppuType(), 0, 0 },
185 : { OUString(UPN_IS_USE_DICTIONARY_LIST), WID_IS_USE_DICTIONARY_LIST,
186 0 : ::getBooleanCppuType(), 0, 0 },
187 : { OUString(UPN_IS_WRAP_REVERSE), WID_IS_WRAP_REVERSE,
188 0 : ::getBooleanCppuType(), 0, 0 },
189 : { OUString(), 0, css::uno::Type(), 0, 0 }
190 0 : };
191 0 : return aLinguProps;
192 : }
193 0 : LinguProps::LinguProps() :
194 0 : aEvtListeners (GetLinguMutex()),
195 0 : aPropListeners (GetLinguMutex()),
196 0 : aPropertyMap(lcl_GetLinguProps())
197 : {
198 0 : bDisposing = sal_False;
199 0 : }
200 :
201 0 : void LinguProps::launchEvent( const PropertyChangeEvent &rEvt ) const
202 : {
203 : cppu::OInterfaceContainerHelper *pContainer =
204 0 : aPropListeners.getContainer( rEvt.PropertyHandle );
205 0 : 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 0 : }
216 :
217 0 : Reference< XInterface > SAL_CALL LinguProps_CreateInstance(
218 : const Reference< XMultiServiceFactory > & /*rSMgr*/ )
219 : throw(Exception)
220 : {
221 0 : Reference< XInterface > xService = (cppu::OWeakObject*)new LinguProps;
222 0 : return xService;
223 : }
224 :
225 0 : Reference< XPropertySetInfo > SAL_CALL LinguProps::getPropertySetInfo()
226 : throw(RuntimeException, std::exception)
227 : {
228 0 : MutexGuard aGuard( GetLinguMutex() );
229 :
230 : static Reference< XPropertySetInfo > aRef =
231 0 : new SfxItemPropertySetInfo( aPropertyMap );
232 0 : return aRef;
233 : }
234 :
235 0 : void SAL_CALL LinguProps::setPropertyValue(
236 : const OUString& rPropertyName, const Any& rValue )
237 : throw(UnknownPropertyException, PropertyVetoException,
238 : IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
239 : {
240 0 : MutexGuard aGuard( GetLinguMutex() );
241 :
242 0 : const SfxItemPropertySimpleEntry* pCur = aPropertyMap.getByName( rPropertyName );
243 0 : if (pCur)
244 : {
245 0 : Any aOld( aConfig.GetProperty( pCur->nWID ) );
246 0 : if (aOld != rValue && aConfig.SetProperty( pCur->nWID, rValue ))
247 : {
248 : PropertyChangeEvent aChgEvt( (XPropertySet *) this, rPropertyName,
249 0 : sal_False, pCur->nWID, aOld, rValue );
250 0 : launchEvent( aChgEvt );
251 0 : }
252 0 : }
253 0 : }
254 :
255 0 : Any SAL_CALL LinguProps::getPropertyValue( const OUString& rPropertyName )
256 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
257 : {
258 0 : MutexGuard aGuard( GetLinguMutex() );
259 :
260 0 : Any aRet;
261 :
262 0 : const SfxItemPropertySimpleEntry* pCur = aPropertyMap.getByName( rPropertyName );
263 0 : if(pCur)
264 : {
265 0 : aRet = aConfig.GetProperty( pCur->nWID );
266 : }
267 :
268 0 : return aRet;
269 : }
270 :
271 0 : void SAL_CALL LinguProps::addPropertyChangeListener(
272 : const OUString& rPropertyName,
273 : const Reference< XPropertyChangeListener >& rxListener )
274 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
275 : {
276 0 : MutexGuard aGuard( GetLinguMutex() );
277 :
278 0 : if (!bDisposing && rxListener.is())
279 : {
280 0 : const SfxItemPropertySimpleEntry* pCur = aPropertyMap.getByName( rPropertyName );
281 0 : if(pCur)
282 0 : aPropListeners.addInterface( pCur->nWID, rxListener );
283 0 : }
284 0 : }
285 :
286 0 : void SAL_CALL LinguProps::removePropertyChangeListener(
287 : const OUString& rPropertyName,
288 : const Reference< XPropertyChangeListener >& rxListener )
289 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
290 : {
291 0 : MutexGuard aGuard( GetLinguMutex() );
292 :
293 0 : if (!bDisposing && rxListener.is())
294 : {
295 0 : const SfxItemPropertySimpleEntry* pCur = aPropertyMap.getByName( rPropertyName );
296 0 : if(pCur)
297 0 : aPropListeners.removeInterface( pCur->nWID, rxListener );
298 0 : }
299 0 : }
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 0 : void SAL_CALL LinguProps::setFastPropertyValue( sal_Int32 nHandle, const Any& rValue )
317 : throw(UnknownPropertyException, PropertyVetoException,
318 : IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
319 : {
320 0 : MutexGuard aGuard( GetLinguMutex() );
321 :
322 0 : Any aOld( aConfig.GetProperty( nHandle ) );
323 0 : if (aOld != rValue && aConfig.SetProperty( nHandle, rValue ))
324 : {
325 : PropertyChangeEvent aChgEvt( (XPropertySet *) this,
326 0 : LinguOptions::GetName( nHandle ), sal_False, nHandle, aOld, rValue );
327 0 : launchEvent( aChgEvt );
328 0 : }
329 0 : }
330 :
331 :
332 0 : Any SAL_CALL LinguProps::getFastPropertyValue( sal_Int32 nHandle )
333 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
334 : {
335 0 : MutexGuard aGuard( GetLinguMutex() );
336 :
337 0 : Any aRes( aConfig.GetProperty( nHandle ) );
338 0 : 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 0 : LinguProps::dispose()
384 : throw(RuntimeException, std::exception)
385 : {
386 0 : MutexGuard aGuard( GetLinguMutex() );
387 :
388 0 : if (!bDisposing)
389 : {
390 0 : bDisposing = sal_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 0 : EventObject aEvtObj( (XPropertySet *) this );
397 0 : aEvtListeners.disposeAndClear( aEvtObj );
398 0 : aPropListeners.disposeAndClear( aEvtObj );
399 0 : }
400 0 : }
401 :
402 : void SAL_CALL
403 0 : LinguProps::addEventListener( const Reference< XEventListener >& rxListener )
404 : throw(RuntimeException, std::exception)
405 : {
406 0 : MutexGuard aGuard( GetLinguMutex() );
407 :
408 0 : if (!bDisposing && rxListener.is())
409 0 : aEvtListeners.addInterface( rxListener );
410 0 : }
411 :
412 : void SAL_CALL
413 0 : LinguProps::removeEventListener( const Reference< XEventListener >& rxListener )
414 : throw(RuntimeException, std::exception)
415 : {
416 0 : MutexGuard aGuard( GetLinguMutex() );
417 :
418 0 : if (!bDisposing && rxListener.is())
419 0 : aEvtListeners.removeInterface( rxListener );
420 0 : }
421 :
422 :
423 : // Service specific part
424 :
425 : // XServiceInfo
426 0 : OUString SAL_CALL LinguProps::getImplementationName()
427 : throw(RuntimeException, std::exception)
428 : {
429 0 : MutexGuard aGuard( GetLinguMutex() );
430 0 : return getImplementationName_Static();
431 : }
432 :
433 : // XServiceInfo
434 0 : sal_Bool SAL_CALL LinguProps::supportsService( const OUString& ServiceName )
435 : throw(RuntimeException, std::exception)
436 : {
437 0 : return cppu::supportsService(this, ServiceName);
438 : }
439 :
440 : // XServiceInfo
441 0 : uno::Sequence< OUString > SAL_CALL LinguProps::getSupportedServiceNames()
442 : throw(RuntimeException, std::exception)
443 : {
444 0 : MutexGuard aGuard( GetLinguMutex() );
445 0 : return getSupportedServiceNames_Static();
446 : }
447 :
448 : // ORegistryServiceManager_Static
449 0 : uno::Sequence< OUString > LinguProps::getSupportedServiceNames_Static()
450 : throw()
451 : {
452 0 : MutexGuard aGuard( GetLinguMutex() );
453 :
454 0 : uno::Sequence< OUString > aSNS( 1 ); // more than 1 service possible
455 0 : aSNS.getArray()[0] = "com.sun.star.linguistic2.LinguProperties";
456 0 : return aSNS;
457 : }
458 :
459 0 : sal_Bool LinguProps::getPropertyBool(const OUString& aPropertyName) throw (css::uno::RuntimeException)
460 : {
461 0 : uno::Any any = getPropertyValue(aPropertyName);
462 0 : sal_Bool b = sal_False;
463 0 : any >>= b;
464 0 : 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 0 : void * SAL_CALL LinguProps_getFactory( const sal_Char * pImplName,
484 : XMultiServiceFactory *pServiceManager, void * )
485 : {
486 0 : void * pRet = 0;
487 0 : if ( LinguProps::getImplementationName_Static().equalsAscii( pImplName ) )
488 : {
489 : Reference< XSingleServiceFactory > xFactory =
490 : cppu::createOneInstanceFactory(
491 : pServiceManager,
492 : LinguProps::getImplementationName_Static(),
493 : LinguProps_CreateInstance,
494 0 : LinguProps::getSupportedServiceNames_Static());
495 : // acquire, because we return an interface pointer instead of a reference
496 0 : xFactory->acquire();
497 0 : pRet = xFactory.get();
498 : }
499 0 : return pRet;
500 : }
501 :
502 :
503 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|