1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: prophelp.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_linguistic.hxx"
34 #include <tools/debug.hxx>
36 #include <com/sun/star/linguistic2/LinguServiceEvent.hpp>
37 #include <com/sun/star/linguistic2/LinguServiceEventFlags.hpp>
38 #include <com/sun/star/linguistic2/XLinguServiceEventListener.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <osl/mutex.hxx>
43 #include <lngprops.hxx>
45 #include <lngprophelp.hxx>
48 //using namespace utl;
51 using namespace com::sun::star
;
52 using namespace com::sun::star::beans
;
53 using namespace com::sun::star::lang
;
54 using namespace com::sun::star::uno
;
55 using namespace com::sun::star::linguistic2
;
56 using namespace linguistic
;
61 ///////////////////////////////////////////////////////////////////////////
63 static const char *aCH
[] =
65 UPN_IS_IGNORE_CONTROL_CHARACTERS
,
66 UPN_IS_USE_DICTIONARY_LIST
,
69 static int nCHCount
= sizeof(aCH
) / sizeof(aCH
[0]);
72 PropertyChgHelper::PropertyChgHelper(
73 const Reference
< XInterface
> &rxSource
,
74 Reference
< XPropertySet
> &rxPropSet
,
75 int nAllowedEvents
) :
76 PropertyChgHelperBase(),
77 aPropNames (nCHCount
),
79 aLngSvcEvtListeners (GetLinguMutex()),
81 nEvtFlags (nAllowedEvents
)
83 OUString
*pName
= aPropNames
.getArray();
84 for (INT32 i
= 0; i
< nCHCount
; ++i
)
86 pName
[i
] = A2OU( aCH
[i
] );
93 PropertyChgHelper::PropertyChgHelper( const PropertyChgHelper
&rHelper
) :
94 PropertyChgHelperBase(),
95 aLngSvcEvtListeners (GetLinguMutex())
97 RemoveAsPropListener();
98 aPropNames
= rHelper
.aPropNames
;
99 xMyEvtObj
= rHelper
.xMyEvtObj
;
100 xPropSet
= rHelper
.xPropSet
;
101 nEvtFlags
= rHelper
.nEvtFlags
;
109 PropertyChgHelper::~PropertyChgHelper()
114 void PropertyChgHelper::AddPropNames( const char *pNewNames
[], INT32 nCount
)
116 if (pNewNames
&& nCount
)
118 INT32 nLen
= GetPropNames().getLength();
119 GetPropNames().realloc( nLen
+ nCount
);
120 OUString
*pName
= GetPropNames().getArray();
121 for (INT32 i
= 0; i
< nCount
; ++i
)
123 pName
[ nLen
+ i
] = A2OU( pNewNames
[ i
] );
129 void PropertyChgHelper::SetDefaultValues()
131 bResIsIgnoreControlCharacters
= bIsIgnoreControlCharacters
= TRUE
;
132 bResIsUseDictionaryList
= bIsUseDictionaryList
= TRUE
;
136 void PropertyChgHelper::GetCurrentValues()
138 INT32 nLen
= GetPropNames().getLength();
139 if (GetPropSet().is() && nLen
)
141 const OUString
*pPropName
= GetPropNames().getConstArray();
142 for (INT32 i
= 0; i
< nLen
; ++i
)
147 if (pPropName
[i
].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_IS_IGNORE_CONTROL_CHARACTERS
) ))
149 pbVal
= &bIsIgnoreControlCharacters
;
150 pbResVal
= &bResIsIgnoreControlCharacters
;
152 else if (pPropName
[i
].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_IS_USE_DICTIONARY_LIST
) ))
154 pbVal
= &bIsUseDictionaryList
;
155 pbResVal
= &bResIsUseDictionaryList
;
158 if (pbVal
&& pbResVal
)
160 GetPropSet()->getPropertyValue( pPropName
[i
] ) >>= *pbVal
;
168 void PropertyChgHelper::SetTmpPropVals( const PropertyValues
&rPropVals
)
170 // return value is default value unless there is an explicitly supplied
172 bResIsIgnoreControlCharacters
= bIsIgnoreControlCharacters
;
173 bResIsUseDictionaryList
= bIsUseDictionaryList
;
175 INT32 nLen
= rPropVals
.getLength();
178 const PropertyValue
*pVal
= rPropVals
.getConstArray();
179 for (INT32 i
= 0; i
< nLen
; ++i
)
181 BOOL
*pbResVal
= NULL
;
182 switch (pVal
[i
].Handle
)
184 case UPH_IS_IGNORE_CONTROL_CHARACTERS
:
185 pbResVal
= &bResIsIgnoreControlCharacters
; break;
186 case UPH_IS_USE_DICTIONARY_LIST
:
187 pbResVal
= &bResIsUseDictionaryList
; break;
190 //DBG_ASSERT( 0, "unknown property" );
193 pVal
[i
].Value
>>= *pbResVal
;
199 BOOL
PropertyChgHelper::propertyChange_Impl( const PropertyChangeEvent
& rEvt
)
203 if (GetPropSet().is() && rEvt
.Source
== GetPropSet())
205 INT16 nLngSvcFlags
= (nEvtFlags
& AE_HYPHENATOR
) ?
206 LinguServiceEventFlags::HYPHENATE_AGAIN
: 0;
207 BOOL bSCWA
= FALSE
, // SPELL_CORRECT_WORDS_AGAIN ?
208 bSWWA
= FALSE
; // SPELL_WRONG_WORDS_AGAIN ?
211 switch (rEvt
.PropertyHandle
)
213 case UPH_IS_IGNORE_CONTROL_CHARACTERS
:
215 pbVal
= &bIsIgnoreControlCharacters
;
219 case UPH_IS_USE_DICTIONARY_LIST
:
221 pbVal
= &bIsUseDictionaryList
;
222 bSCWA
= bSWWA
= TRUE
;
228 //DBG_ASSERT( 0, "unknown property" );
232 rEvt
.NewValue
>>= *pbVal
;
234 bRes
= 0 != pbVal
; // sth changed?
237 BOOL bSpellEvts
= (nEvtFlags
& AE_SPELLCHECKER
) ? TRUE
: FALSE
;
238 if (bSCWA
&& bSpellEvts
)
239 nLngSvcFlags
|= LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN
;
240 if (bSWWA
&& bSpellEvts
)
241 nLngSvcFlags
|= LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN
;
244 LinguServiceEvent
aEvt( GetEvtObj(), nLngSvcFlags
);
255 PropertyChgHelper::propertyChange( const PropertyChangeEvent
& rEvt
)
256 throw(RuntimeException
)
258 MutexGuard
aGuard( GetLinguMutex() );
259 propertyChange_Impl( rEvt
);
263 void PropertyChgHelper::AddAsPropListener()
267 INT32 nLen
= aPropNames
.getLength();
268 const OUString
*pPropName
= aPropNames
.getConstArray();
269 for (INT32 i
= 0; i
< nLen
; ++i
)
271 if (pPropName
[i
].getLength())
272 xPropSet
->addPropertyChangeListener( pPropName
[i
], this );
277 void PropertyChgHelper::RemoveAsPropListener()
281 INT32 nLen
= aPropNames
.getLength();
282 const OUString
*pPropName
= aPropNames
.getConstArray();
283 for (INT32 i
= 0; i
< nLen
; ++i
)
285 if (pPropName
[i
].getLength())
286 xPropSet
->removePropertyChangeListener( pPropName
[i
], this );
292 void PropertyChgHelper::LaunchEvent( const LinguServiceEvent
&rEvt
)
294 cppu::OInterfaceIteratorHelper
aIt( aLngSvcEvtListeners
);
295 while (aIt
.hasMoreElements())
297 Reference
< XLinguServiceEventListener
> xRef( aIt
.next(), UNO_QUERY
);
299 xRef
->processLinguServiceEvent( rEvt
);
304 void SAL_CALL
PropertyChgHelper::disposing( const EventObject
& rSource
)
305 throw(RuntimeException
)
307 MutexGuard
aGuard( GetLinguMutex() );
308 if (rSource
.Source
== xPropSet
)
310 RemoveAsPropListener();
312 aPropNames
.realloc( 0 );
318 PropertyChgHelper::addLinguServiceEventListener(
319 const Reference
< XLinguServiceEventListener
>& rxListener
)
320 throw(RuntimeException
)
322 MutexGuard
aGuard( GetLinguMutex() );
327 INT32 nCount
= aLngSvcEvtListeners
.getLength();
328 bRes
= aLngSvcEvtListeners
.addInterface( rxListener
) != nCount
;
335 PropertyChgHelper::removeLinguServiceEventListener(
336 const Reference
< XLinguServiceEventListener
>& rxListener
)
337 throw(RuntimeException
)
339 MutexGuard
aGuard( GetLinguMutex() );
344 INT32 nCount
= aLngSvcEvtListeners
.getLength();
345 bRes
= aLngSvcEvtListeners
.removeInterface( rxListener
) != nCount
;
350 ///////////////////////////////////////////////////////////////////////////
353 PropertyHelper_Thes::PropertyHelper_Thes(
354 const Reference
< XInterface
> &rxSource
,
355 Reference
< XPropertySet
> &rxPropSet
) :
356 PropertyChgHelper ( rxSource
, rxPropSet
, 0 )
363 PropertyHelper_Thes::~PropertyHelper_Thes()
369 PropertyHelper_Thes::propertyChange( const PropertyChangeEvent
& rEvt
)
370 throw(RuntimeException
)
372 MutexGuard
aGuard( GetLinguMutex() );
373 PropertyChgHelper::propertyChange_Impl( rEvt
);
377 ///////////////////////////////////////////////////////////////////////////
379 // list of properties from the property set to be used
381 static const char *aSP
[] =
383 UPN_IS_SPELL_UPPER_CASE
,
384 UPN_IS_SPELL_WITH_DIGITS
,
385 UPN_IS_SPELL_CAPITALIZATION
389 PropertyHelper_Spell::PropertyHelper_Spell(
390 const Reference
< XInterface
> & rxSource
,
391 Reference
< XPropertySet
> &rxPropSet
) :
392 PropertyChgHelper ( rxSource
, rxPropSet
, AE_SPELLCHECKER
)
394 AddPropNames( aSP
, sizeof(aSP
) / sizeof(aSP
[0]) );
398 nResMaxNumberOfSuggestions
= GetDefaultNumberOfSuggestions();
402 PropertyHelper_Spell::~PropertyHelper_Spell()
407 void PropertyHelper_Spell::SetDefaultValues()
409 PropertyChgHelper::SetDefaultValues();
411 bResIsSpellUpperCase
= bIsSpellUpperCase
= FALSE
;
412 bResIsSpellWithDigits
= bIsSpellWithDigits
= FALSE
;
413 bResIsSpellCapitalization
= bIsSpellCapitalization
= TRUE
;
417 void PropertyHelper_Spell::GetCurrentValues()
419 PropertyChgHelper::GetCurrentValues();
421 INT32 nLen
= GetPropNames().getLength();
422 if (GetPropSet().is() && nLen
)
424 const OUString
*pPropName
= GetPropNames().getConstArray();
425 for (INT32 i
= 0; i
< nLen
; ++i
)
430 if (pPropName
[i
].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_IS_SPELL_UPPER_CASE
) ))
432 pbVal
= &bIsSpellUpperCase
;
433 pbResVal
= &bResIsSpellUpperCase
;
435 else if (pPropName
[i
].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_IS_SPELL_WITH_DIGITS
) ))
437 pbVal
= &bIsSpellWithDigits
;
438 pbResVal
= &bResIsSpellWithDigits
;
440 else if (pPropName
[i
].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_IS_SPELL_CAPITALIZATION
) ))
442 pbVal
= &bIsSpellCapitalization
;
443 pbResVal
= &bResIsSpellCapitalization
;
446 if (pbVal
&& pbResVal
)
448 GetPropSet()->getPropertyValue( pPropName
[i
] ) >>= *pbVal
;
456 BOOL
PropertyHelper_Spell::propertyChange_Impl( const PropertyChangeEvent
& rEvt
)
458 BOOL bRes
= PropertyChgHelper::propertyChange_Impl( rEvt
);
460 if (!bRes
&& GetPropSet().is() && rEvt
.Source
== GetPropSet())
462 INT16 nLngSvcFlags
= 0;
463 BOOL bSCWA
= FALSE
, // SPELL_CORRECT_WORDS_AGAIN ?
464 bSWWA
= FALSE
; // SPELL_WRONG_WORDS_AGAIN ?
467 switch (rEvt
.PropertyHandle
)
469 case UPH_IS_SPELL_UPPER_CASE
:
471 pbVal
= &bIsSpellUpperCase
;
472 bSCWA
= FALSE
== *pbVal
; // FALSE->TRUE change?
473 bSWWA
= !bSCWA
; // TRUE->FALSE change?
476 case UPH_IS_SPELL_WITH_DIGITS
:
478 pbVal
= &bIsSpellWithDigits
;
479 bSCWA
= FALSE
== *pbVal
; // FALSE->TRUE change?
480 bSWWA
= !bSCWA
; // TRUE->FALSE change?
483 case UPH_IS_SPELL_CAPITALIZATION
:
485 pbVal
= &bIsSpellCapitalization
;
486 bSCWA
= FALSE
== *pbVal
; // FALSE->TRUE change?
487 bSWWA
= !bSCWA
; // TRUE->FALSE change?
491 DBG_ASSERT( 0, "unknown property" );
494 rEvt
.NewValue
>>= *pbVal
;
500 nLngSvcFlags
|= LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN
;
502 nLngSvcFlags
|= LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN
;
505 LinguServiceEvent
aEvt( GetEvtObj(), nLngSvcFlags
);
516 PropertyHelper_Spell::propertyChange( const PropertyChangeEvent
& rEvt
)
517 throw(RuntimeException
)
519 MutexGuard
aGuard( GetLinguMutex() );
520 propertyChange_Impl( rEvt
);
524 void PropertyHelper_Spell::SetTmpPropVals( const PropertyValues
&rPropVals
)
526 PropertyChgHelper::SetTmpPropVals( rPropVals
);
528 // return value is default value unless there is an explicitly supplied
530 nResMaxNumberOfSuggestions
= GetDefaultNumberOfSuggestions();
531 bResIsSpellWithDigits
= bIsSpellWithDigits
;
532 bResIsSpellCapitalization
= bIsSpellCapitalization
;
534 INT32 nLen
= rPropVals
.getLength();
537 const PropertyValue
*pVal
= rPropVals
.getConstArray();
538 for (INT32 i
= 0; i
< nLen
; ++i
)
540 if (pVal
[i
].Name
.equalsAscii( UPN_MAX_NUMBER_OF_SUGGESTIONS
))
542 pVal
[i
].Value
>>= nResMaxNumberOfSuggestions
;
546 BOOL
*pbResVal
= NULL
;
547 switch (pVal
[i
].Handle
)
549 case UPH_IS_SPELL_UPPER_CASE
: pbResVal
= &bResIsSpellUpperCase
; break;
550 case UPH_IS_SPELL_WITH_DIGITS
: pbResVal
= &bResIsSpellWithDigits
; break;
551 case UPH_IS_SPELL_CAPITALIZATION
: pbResVal
= &bResIsSpellCapitalization
; break;
553 DBG_ASSERT( 0, "unknown property" );
556 pVal
[i
].Value
>>= *pbResVal
;
562 INT16
PropertyHelper_Spell::GetDefaultNumberOfSuggestions() const
567 ///////////////////////////////////////////////////////////////////////////
569 static const char *aHP
[] =
571 UPN_HYPH_MIN_LEADING
,
572 UPN_HYPH_MIN_TRAILING
,
573 UPN_HYPH_MIN_WORD_LENGTH
577 PropertyHelper_Hyphen::PropertyHelper_Hyphen(
578 const Reference
< XInterface
> & rxSource
,
579 Reference
< XPropertySet
> &rxPropSet
) :
580 PropertyChgHelper ( rxSource
, rxPropSet
, AE_HYPHENATOR
)
582 AddPropNames( aHP
, sizeof(aHP
) / sizeof(aHP
[0]) );
588 PropertyHelper_Hyphen::~PropertyHelper_Hyphen()
593 void PropertyHelper_Hyphen::SetDefaultValues()
595 PropertyChgHelper::SetDefaultValues();
597 nResHyphMinLeading
= nHyphMinLeading
= 2;
598 nResHyphMinTrailing
= nHyphMinTrailing
= 2;
599 nResHyphMinWordLength
= nHyphMinWordLength
= 0;
603 void PropertyHelper_Hyphen::GetCurrentValues()
605 PropertyChgHelper::GetCurrentValues();
607 INT32 nLen
= GetPropNames().getLength();
608 if (GetPropSet().is() && nLen
)
610 const OUString
*pPropName
= GetPropNames().getConstArray();
611 for (INT32 i
= 0; i
< nLen
; ++i
)
616 if (pPropName
[i
].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_HYPH_MIN_LEADING
) ))
618 pnVal
= &nHyphMinLeading
;
619 pnResVal
= &nResHyphMinLeading
;
621 else if (pPropName
[i
].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_HYPH_MIN_TRAILING
) ))
623 pnVal
= &nHyphMinTrailing
;
624 pnResVal
= &nResHyphMinTrailing
;
626 else if (pPropName
[i
].equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( UPN_HYPH_MIN_WORD_LENGTH
) ))
628 pnVal
= &nHyphMinWordLength
;
629 pnResVal
= &nResHyphMinWordLength
;
632 if (pnVal
&& pnResVal
)
634 GetPropSet()->getPropertyValue( pPropName
[i
] ) >>= *pnVal
;
642 BOOL
PropertyHelper_Hyphen::propertyChange_Impl( const PropertyChangeEvent
& rEvt
)
644 BOOL bRes
= PropertyChgHelper::propertyChange_Impl( rEvt
);
646 if (!bRes
&& GetPropSet().is() && rEvt
.Source
== GetPropSet())
648 INT16 nLngSvcFlags
= LinguServiceEventFlags::HYPHENATE_AGAIN
;
651 switch (rEvt
.PropertyHandle
)
653 case UPH_HYPH_MIN_LEADING
: pnVal
= &nHyphMinLeading
; break;
654 case UPH_HYPH_MIN_TRAILING
: pnVal
= &nHyphMinTrailing
; break;
655 case UPH_HYPH_MIN_WORD_LENGTH
: pnVal
= &nHyphMinWordLength
; break;
657 DBG_ASSERT( 0, "unknown property" );
660 rEvt
.NewValue
>>= *pnVal
;
667 LinguServiceEvent
aEvt( GetEvtObj(), nLngSvcFlags
);
678 PropertyHelper_Hyphen::propertyChange( const PropertyChangeEvent
& rEvt
)
679 throw(RuntimeException
)
681 MutexGuard
aGuard( GetLinguMutex() );
682 propertyChange_Impl( rEvt
);
686 void PropertyHelper_Hyphen::SetTmpPropVals( const PropertyValues
&rPropVals
)
688 PropertyChgHelper::SetTmpPropVals( rPropVals
);
690 // return value is default value unless there is an explicitly supplied
692 nResHyphMinLeading
= nHyphMinLeading
;
693 nResHyphMinTrailing
= nHyphMinTrailing
;
694 nResHyphMinWordLength
= nHyphMinWordLength
;
696 INT32 nLen
= rPropVals
.getLength();
699 const PropertyValue
*pVal
= rPropVals
.getConstArray();
700 for (INT32 i
= 0; i
< nLen
; ++i
)
702 INT16
*pnResVal
= NULL
;
703 switch (pVal
[i
].Handle
)
705 case UPH_HYPH_MIN_LEADING
: pnResVal
= &nResHyphMinLeading
; break;
706 case UPH_HYPH_MIN_TRAILING
: pnResVal
= &nResHyphMinTrailing
; break;
707 case UPH_HYPH_MIN_WORD_LENGTH
: pnResVal
= &nResHyphMinWordLength
; break;
709 DBG_ASSERT( 0, "unknown property" );
712 pVal
[i
].Value
>>= *pnResVal
;
717 ///////////////////////////////////////////////////////////////////////////
719 } // namespace linguistic