1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "linguistic/misc.hxx"
24 #include "sprophelp.hxx"
25 #include "linguistic/lngprops.hxx"
27 #include <com/sun/star/linguistic2/LinguServiceEvent.hpp>
28 #include <com/sun/star/linguistic2/LinguServiceEventFlags.hpp>
29 #include <com/sun/star/linguistic2/XLinguServiceEventListener.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <osl/mutex.hxx>
34 using namespace com::sun::star
;
35 using namespace com::sun::star::beans
;
36 using namespace com::sun::star::lang
;
37 using namespace com::sun::star::uno
;
38 using namespace com::sun::star::linguistic2
;
39 using namespace linguistic
;
44 PropertyChgHelper::PropertyChgHelper(
45 const Reference
< XInterface
> & rxSource
,
46 Reference
< XPropertySet
> &rxPropSet
,
47 const char *pPropNames
[], sal_uInt16 nPropCount
)
48 : aPropNames(nPropCount
)
50 , aLngSvcEvtListeners(GetLinguMutex())
53 OUString
*pName
= aPropNames
.getArray();
54 for (sal_Int32 i
= 0; i
< nPropCount
; ++i
)
56 pName
[i
] = OUString::createFromAscii( pPropNames
[i
] );
61 PropertyChgHelper::PropertyChgHelper( const PropertyChgHelper
&rHelper
) :
62 aLngSvcEvtListeners (GetLinguMutex())
64 xPropSet
= rHelper
.xPropSet
;
65 aPropNames
= rHelper
.aPropNames
;
68 xMyEvtObj
= rHelper
.xMyEvtObj
;
72 PropertyChgHelper::~PropertyChgHelper()
77 void PropertyChgHelper::AddAsPropListener()
81 sal_Int32 nLen
= aPropNames
.getLength();
82 const OUString
*pPropName
= aPropNames
.getConstArray();
83 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
85 if (pPropName
[i
].getLength())
86 xPropSet
->addPropertyChangeListener( pPropName
[i
], this );
91 void PropertyChgHelper::RemoveAsPropListener()
95 sal_Int32 nLen
= aPropNames
.getLength();
96 const OUString
*pPropName
= aPropNames
.getConstArray();
97 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
99 if (pPropName
[i
].getLength())
100 xPropSet
->removePropertyChangeListener( pPropName
[i
], this );
106 void PropertyChgHelper::LaunchEvent( const LinguServiceEvent
&rEvt
)
108 cppu::OInterfaceIteratorHelper
aIt( aLngSvcEvtListeners
);
109 while (aIt
.hasMoreElements())
111 Reference
< XLinguServiceEventListener
> xRef( aIt
.next(), UNO_QUERY
);
113 xRef
->processLinguServiceEvent( rEvt
);
118 void SAL_CALL
PropertyChgHelper::disposing( const EventObject
& rSource
)
119 throw(RuntimeException
)
121 MutexGuard
aGuard( GetLinguMutex() );
122 if (rSource
.Source
== xPropSet
)
124 RemoveAsPropListener();
126 aPropNames
.realloc( 0 );
132 PropertyChgHelper::addLinguServiceEventListener(
133 const Reference
< XLinguServiceEventListener
>& rxListener
)
134 throw(RuntimeException
)
136 MutexGuard
aGuard( GetLinguMutex() );
138 sal_Bool bRes
= sal_False
;
141 sal_Int32 nCount
= aLngSvcEvtListeners
.getLength();
142 bRes
= aLngSvcEvtListeners
.addInterface( rxListener
) != nCount
;
149 PropertyChgHelper::removeLinguServiceEventListener(
150 const Reference
< XLinguServiceEventListener
>& rxListener
)
151 throw(RuntimeException
)
153 MutexGuard
aGuard( GetLinguMutex() );
155 sal_Bool bRes
= sal_False
;
158 sal_Int32 nCount
= aLngSvcEvtListeners
.getLength();
159 bRes
= aLngSvcEvtListeners
.removeInterface( rxListener
) != nCount
;
165 static const char *aSP
[] =
167 UPN_IS_GERMAN_PRE_REFORM
,
168 UPN_IS_IGNORE_CONTROL_CHARACTERS
,
169 UPN_IS_USE_DICTIONARY_LIST
,
170 UPN_IS_SPELL_UPPER_CASE
,
171 UPN_IS_SPELL_WITH_DIGITS
,
172 UPN_IS_SPELL_CAPITALIZATION
176 PropertyHelper_Spell::PropertyHelper_Spell(
177 const Reference
< XInterface
> & rxSource
,
178 Reference
< XPropertySet
> &rxPropSet
) :
179 PropertyChgHelper ( rxSource
, rxPropSet
, aSP
, sizeof(aSP
) / sizeof(aSP
[0]) )
182 sal_Int32 nLen
= GetPropNames().getLength();
183 if (rxPropSet
.is() && nLen
)
185 const OUString
*pPropName
= GetPropNames().getConstArray();
186 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
188 sal_Bool
*pbVal
= NULL
,
191 if (OUString( UPN_IS_GERMAN_PRE_REFORM
) == pPropName
[i
])
193 pbVal
= &bIsGermanPreReform
;
194 pbResVal
= &bResIsGermanPreReform
;
196 else if (OUString( UPN_IS_IGNORE_CONTROL_CHARACTERS
) == pPropName
[i
])
198 pbVal
= &bIsIgnoreControlCharacters
;
199 pbResVal
= &bResIsIgnoreControlCharacters
;
201 else if (OUString( UPN_IS_USE_DICTIONARY_LIST
) == pPropName
[i
])
203 pbVal
= &bIsUseDictionaryList
;
204 pbResVal
= &bResIsUseDictionaryList
;
206 else if (OUString( UPN_IS_SPELL_UPPER_CASE
) == pPropName
[i
])
208 pbVal
= &bIsSpellUpperCase
;
209 pbResVal
= &bResIsSpellUpperCase
;
211 else if (OUString( UPN_IS_SPELL_WITH_DIGITS
) == pPropName
[i
])
213 pbVal
= &bIsSpellWithDigits
;
214 pbResVal
= &bResIsSpellWithDigits
;
216 else if (OUString( UPN_IS_SPELL_CAPITALIZATION
) == pPropName
[i
])
218 pbVal
= &bIsSpellCapitalization
;
219 pbResVal
= &bResIsSpellCapitalization
;
222 if (pbVal
&& pbResVal
)
224 rxPropSet
->getPropertyValue( pPropName
[i
] ) >>= *pbVal
;
232 PropertyHelper_Spell::~PropertyHelper_Spell()
237 void PropertyHelper_Spell::SetDefault()
239 bResIsGermanPreReform
= bIsGermanPreReform
= sal_False
;
240 bResIsIgnoreControlCharacters
= bIsIgnoreControlCharacters
= sal_True
;
241 bResIsUseDictionaryList
= bIsUseDictionaryList
= sal_True
;
242 bResIsSpellUpperCase
= bIsSpellUpperCase
= sal_False
;
243 bResIsSpellWithDigits
= bIsSpellWithDigits
= sal_False
;
244 bResIsSpellCapitalization
= bIsSpellCapitalization
= sal_True
;
249 PropertyHelper_Spell::propertyChange( const PropertyChangeEvent
& rEvt
)
250 throw(RuntimeException
)
252 MutexGuard
aGuard( GetLinguMutex() );
254 if (GetPropSet().is() && rEvt
.Source
== GetPropSet())
256 sal_Int16 nLngSvcFlags
= 0;
257 sal_Bool bSCWA
= sal_False
, // SPELL_CORRECT_WORDS_AGAIN ?
258 bSWWA
= sal_False
; // SPELL_WRONG_WORDS_AGAIN ?
260 sal_Bool
*pbVal
= NULL
;
261 switch (rEvt
.PropertyHandle
)
263 case UPH_IS_IGNORE_CONTROL_CHARACTERS
:
265 pbVal
= &bIsIgnoreControlCharacters
;
268 case UPH_IS_GERMAN_PRE_REFORM
:
270 pbVal
= &bIsGermanPreReform
;
271 bSCWA
= bSWWA
= sal_True
;
274 case UPH_IS_USE_DICTIONARY_LIST
:
276 pbVal
= &bIsUseDictionaryList
;
277 bSCWA
= bSWWA
= sal_True
;
280 case UPH_IS_SPELL_UPPER_CASE
:
282 pbVal
= &bIsSpellUpperCase
;
283 bSCWA
= sal_False
== *pbVal
; // sal_False->sal_True change?
284 bSWWA
= !bSCWA
; // sal_True->sal_False change?
287 case UPH_IS_SPELL_WITH_DIGITS
:
289 pbVal
= &bIsSpellWithDigits
;
290 bSCWA
= sal_False
== *pbVal
; // sal_False->sal_True change?
291 bSWWA
= !bSCWA
; // sal_True->sal_False change?
294 case UPH_IS_SPELL_CAPITALIZATION
:
296 pbVal
= &bIsSpellCapitalization
;
297 bSCWA
= sal_False
== *pbVal
; // sal_False->sal_True change?
298 bSWWA
= !bSCWA
; // sal_True->sal_False change?
302 OSL_FAIL( "unknown property" );
305 rEvt
.NewValue
>>= *pbVal
;
308 nLngSvcFlags
|= LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN
;
310 nLngSvcFlags
|= LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN
;
313 LinguServiceEvent
aEvt( GetEvtObj(), nLngSvcFlags
);
320 void PropertyHelper_Spell::SetTmpPropVals( const PropertyValues
&rPropVals
)
322 // set return value to default value unless there is an
323 // explicitly supplied temporary value
324 bResIsGermanPreReform
= bIsGermanPreReform
;
325 bResIsIgnoreControlCharacters
= bIsIgnoreControlCharacters
;
326 bResIsUseDictionaryList
= bIsUseDictionaryList
;
327 bResIsSpellUpperCase
= bIsSpellUpperCase
;
328 bResIsSpellWithDigits
= bIsSpellWithDigits
;
329 bResIsSpellCapitalization
= bIsSpellCapitalization
;
331 sal_Int32 nLen
= rPropVals
.getLength();
334 const PropertyValue
*pVal
= rPropVals
.getConstArray();
335 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
337 sal_Bool
*pbResVal
= NULL
;
338 switch (pVal
[i
].Handle
)
340 case UPH_IS_GERMAN_PRE_REFORM
: pbResVal
= &bResIsGermanPreReform
; break;
341 case UPH_IS_IGNORE_CONTROL_CHARACTERS
: pbResVal
= &bResIsIgnoreControlCharacters
; break;
342 case UPH_IS_USE_DICTIONARY_LIST
: pbResVal
= &bResIsUseDictionaryList
; break;
343 case UPH_IS_SPELL_UPPER_CASE
: pbResVal
= &bResIsSpellUpperCase
; break;
344 case UPH_IS_SPELL_WITH_DIGITS
: pbResVal
= &bResIsSpellWithDigits
; break;
345 case UPH_IS_SPELL_CAPITALIZATION
: pbResVal
= &bResIsSpellCapitalization
; break;
347 OSL_FAIL( "unknown property" );
350 pVal
[i
].Value
>>= *pbResVal
;
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */