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 "linguistic/lngprops.hxx"
24 : #include "linguistic/misc.hxx"
25 : #include <tools/debug.hxx>
26 : #include <unotools/lingucfg.hxx>
27 :
28 : #include <uno/lbnames.h> // CPPU_CURRENT_LANGUAGE_BINDING_NAME macro, which specify the environment type
29 : #include <cppuhelper/implbase1.hxx> // helper for implementations
30 :
31 : #include <cppuhelper/factory.hxx> // helper for factories
32 : #include <com/sun/star/container/XNameAccess.hpp>
33 : #include <com/sun/star/registry/XSimpleRegistry.hpp>
34 : #include <com/sun/star/registry/XRegistryKey.hpp>
35 : #include <com/sun/star/lang/Locale.hpp>
36 : #include <com/sun/star/i18n/ScriptType.hpp>
37 : #include <i18npool/mslangid.hxx>
38 :
39 : using namespace utl;
40 : using namespace osl;
41 : using namespace com::sun::star;
42 : using namespace com::sun::star::container;
43 : using namespace com::sun::star::beans;
44 : using namespace com::sun::star::lang;
45 : using namespace com::sun::star::uno;
46 : using namespace com::sun::star::linguistic2;
47 : using namespace linguistic;
48 :
49 : using namespace com::sun::star::registry;
50 :
51 : using ::rtl::OUString;
52 :
53 :
54 :
55 : // static member intialization
56 : SvtLinguOptions * LinguOptions::pData = NULL;
57 : oslInterlockedCount LinguOptions::nRefCount;
58 :
59 :
60 13 : LinguOptions::LinguOptions()
61 : {
62 13 : if (!pData)
63 : {
64 7 : pData = new SvtLinguOptions;
65 7 : SvtLinguConfig aLinguCfg;
66 7 : aLinguCfg.GetOptions( *pData );
67 : }
68 :
69 13 : osl_atomic_increment( &nRefCount );
70 13 : }
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 7 : LinguOptions::~LinguOptions()
81 : {
82 7 : MutexGuard aGuard( GetLinguMutex() );
83 :
84 7 : if ( osl_atomic_decrement( &nRefCount ) == 0 )
85 : {
86 1 : delete pData; pData = NULL;
87 7 : }
88 7 : }
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 3 : static const SfxItemPropertyMapEntry* lcl_GetLinguProps()
146 : {
147 : static const SfxItemPropertyMapEntry aLinguProps[] =
148 : {
149 : { MAP_CHAR_LEN(UPN_DEFAULT_LANGUAGE), WID_DEFAULT_LANGUAGE,
150 3 : &::getCppuType( (sal_Int16*)0 ), 0, 0 },
151 : { MAP_CHAR_LEN(UPN_DEFAULT_LOCALE), WID_DEFAULT_LOCALE,
152 3 : &::getCppuType( (Locale* )0), 0, 0 },
153 : { MAP_CHAR_LEN(UPN_DEFAULT_LOCALE_CJK), WID_DEFAULT_LOCALE_CJK,
154 3 : &::getCppuType( (Locale* )0), 0, 0 },
155 : { MAP_CHAR_LEN(UPN_DEFAULT_LOCALE_CTL), WID_DEFAULT_LOCALE_CTL,
156 3 : &::getCppuType( (Locale* )0), 0, 0 },
157 : { MAP_CHAR_LEN(UPN_HYPH_MIN_LEADING), WID_HYPH_MIN_LEADING,
158 3 : &::getCppuType( (sal_Int16*)0 ), 0, 0 },
159 : { MAP_CHAR_LEN(UPN_HYPH_MIN_TRAILING), WID_HYPH_MIN_TRAILING,
160 3 : &::getCppuType( (sal_Int16*)0 ), 0, 0 },
161 : { MAP_CHAR_LEN(UPN_HYPH_MIN_WORD_LENGTH), WID_HYPH_MIN_WORD_LENGTH,
162 3 : &::getCppuType( (sal_Int16*)0 ), 0, 0 },
163 : { MAP_CHAR_LEN(UPN_IS_GERMAN_PRE_REFORM), WID_IS_GERMAN_PRE_REFORM, /*! deprecated !*/
164 3 : &::getBooleanCppuType(), 0, 0 },
165 : { MAP_CHAR_LEN(UPN_IS_HYPH_AUTO), WID_IS_HYPH_AUTO,
166 3 : &::getBooleanCppuType(), 0, 0 },
167 : { MAP_CHAR_LEN(UPN_IS_HYPH_SPECIAL), WID_IS_HYPH_SPECIAL,
168 3 : &::getBooleanCppuType(), 0, 0 },
169 : { MAP_CHAR_LEN(UPN_IS_IGNORE_CONTROL_CHARACTERS), WID_IS_IGNORE_CONTROL_CHARACTERS,
170 3 : &::getBooleanCppuType(), 0, 0 },
171 : { MAP_CHAR_LEN(UPN_IS_SPELL_AUTO), WID_IS_SPELL_AUTO,
172 3 : &::getBooleanCppuType(), 0, 0 },
173 : { MAP_CHAR_LEN(UPN_IS_SPELL_CAPITALIZATION), WID_IS_SPELL_CAPITALIZATION,
174 3 : &::getBooleanCppuType(), 0, 0 },
175 : { MAP_CHAR_LEN(UPN_IS_SPELL_HIDE), WID_IS_SPELL_HIDE, /*! deprecated !*/
176 3 : &::getBooleanCppuType(), 0, 0 },
177 : { MAP_CHAR_LEN(UPN_IS_SPELL_IN_ALL_LANGUAGES), WID_IS_SPELL_IN_ALL_LANGUAGES, /*! deprecated !*/
178 3 : &::getBooleanCppuType(), 0, 0 },
179 : { MAP_CHAR_LEN(UPN_IS_SPELL_SPECIAL), WID_IS_SPELL_SPECIAL,
180 3 : &::getBooleanCppuType(), 0, 0 },
181 : { MAP_CHAR_LEN(UPN_IS_SPELL_UPPER_CASE), WID_IS_SPELL_UPPER_CASE,
182 3 : &::getBooleanCppuType(), 0, 0 },
183 : { MAP_CHAR_LEN(UPN_IS_SPELL_WITH_DIGITS), WID_IS_SPELL_WITH_DIGITS,
184 3 : &::getBooleanCppuType(), 0, 0 },
185 : { MAP_CHAR_LEN(UPN_IS_USE_DICTIONARY_LIST), WID_IS_USE_DICTIONARY_LIST,
186 3 : &::getBooleanCppuType(), 0, 0 },
187 : { MAP_CHAR_LEN(UPN_IS_WRAP_REVERSE), WID_IS_WRAP_REVERSE,
188 3 : &::getBooleanCppuType(), 0, 0 },
189 : { 0,0,0,0,0,0 }
190 63 : };
191 3 : return aLinguProps;
192 : }
193 3 : LinguProps::LinguProps() :
194 3 : aEvtListeners (GetLinguMutex()),
195 3 : aPropListeners (GetLinguMutex()),
196 9 : aPropertyMap(lcl_GetLinguProps())
197 : {
198 3 : bDisposing = sal_False;
199 3 : }
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 3 : Reference< XInterface > SAL_CALL LinguProps_CreateInstance(
218 : const Reference< XMultiServiceFactory > & /*rSMgr*/ )
219 : throw(Exception)
220 : {
221 3 : Reference< XInterface > xService = (cppu::OWeakObject*)new LinguProps;
222 3 : return xService;
223 : }
224 :
225 0 : Reference< XPropertySetInfo > SAL_CALL LinguProps::getPropertySetInfo()
226 : throw(RuntimeException)
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)
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 18 : Any SAL_CALL LinguProps::getPropertyValue( const OUString& rPropertyName )
256 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
257 : {
258 18 : MutexGuard aGuard( GetLinguMutex() );
259 :
260 18 : Any aRet;
261 :
262 18 : const SfxItemPropertySimpleEntry* pCur = aPropertyMap.getByName( rPropertyName );
263 18 : if(pCur)
264 : {
265 18 : aRet = aConfig.GetProperty( pCur->nWID );
266 : }
267 :
268 18 : 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)
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)
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)
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)
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)
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)
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)
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)
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 3 : LinguProps::dispose()
384 : throw(RuntimeException)
385 : {
386 3 : MutexGuard aGuard( GetLinguMutex() );
387 :
388 3 : if (!bDisposing)
389 : {
390 3 : 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 3 : EventObject aEvtObj( (XPropertySet *) this );
397 3 : aEvtListeners.disposeAndClear( aEvtObj );
398 3 : aPropListeners.disposeAndClear( aEvtObj );
399 3 : }
400 3 : }
401 :
402 : void SAL_CALL
403 0 : LinguProps::addEventListener( const Reference< XEventListener >& rxListener )
404 : throw(RuntimeException)
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)
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)
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)
436 : {
437 0 : MutexGuard aGuard( GetLinguMutex() );
438 :
439 0 : uno::Sequence< OUString > aSNL = getSupportedServiceNames();
440 0 : const OUString * pArray = aSNL.getConstArray();
441 0 : for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
442 0 : if( pArray[i] == ServiceName )
443 0 : return sal_True;
444 0 : return sal_False;
445 : }
446 :
447 : // XServiceInfo
448 0 : uno::Sequence< OUString > SAL_CALL LinguProps::getSupportedServiceNames()
449 : throw(RuntimeException)
450 : {
451 0 : MutexGuard aGuard( GetLinguMutex() );
452 0 : return getSupportedServiceNames_Static();
453 : }
454 :
455 : // ORegistryServiceManager_Static
456 3 : uno::Sequence< OUString > LinguProps::getSupportedServiceNames_Static()
457 : throw()
458 : {
459 3 : MutexGuard aGuard( GetLinguMutex() );
460 :
461 3 : uno::Sequence< OUString > aSNS( 1 ); // more than 1 service possible
462 3 : aSNS.getArray()[0] = A2OU( SN_LINGU_PROPERTIES );
463 3 : return aSNS;
464 : }
465 :
466 10 : void * SAL_CALL LinguProps_getFactory( const sal_Char * pImplName,
467 : XMultiServiceFactory *pServiceManager, void * )
468 : {
469 10 : void * pRet = 0;
470 10 : if ( !LinguProps::getImplementationName_Static().compareToAscii( pImplName ) )
471 : {
472 : Reference< XSingleServiceFactory > xFactory =
473 : cppu::createOneInstanceFactory(
474 : pServiceManager,
475 : LinguProps::getImplementationName_Static(),
476 : LinguProps_CreateInstance,
477 3 : LinguProps::getSupportedServiceNames_Static());
478 : // acquire, because we return an interface pointer instead of a reference
479 3 : xFactory->acquire();
480 3 : pRet = xFactory.get();
481 : }
482 10 : return pRet;
483 : }
484 :
485 :
486 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|