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
;
42 PropertyChgHelper::PropertyChgHelper(
43 const Reference
< XInterface
> & rxSource
,
44 Reference
< XPropertySet
> &rxPropSet
,
45 const char *pPropNames
[], sal_uInt16 nPropCount
)
46 : aPropNames(nPropCount
)
48 , aLngSvcEvtListeners(GetLinguMutex())
51 OUString
*pName
= aPropNames
.getArray();
52 for (sal_Int32 i
= 0; i
< nPropCount
; ++i
)
54 pName
[i
] = OUString::createFromAscii( pPropNames
[i
] );
59 PropertyChgHelper::PropertyChgHelper( const PropertyChgHelper
&rHelper
) :
60 aPropNames(rHelper
.aPropNames
),
61 xMyEvtObj(rHelper
.xMyEvtObj
),
62 aLngSvcEvtListeners (GetLinguMutex()),
63 xPropSet(rHelper
.xPropSet
)
70 PropertyChgHelper::~PropertyChgHelper()
75 void PropertyChgHelper::AddAsPropListener()
79 sal_Int32 nLen
= aPropNames
.getLength();
80 const OUString
*pPropName
= aPropNames
.getConstArray();
81 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
83 if (pPropName
[i
].getLength())
84 xPropSet
->addPropertyChangeListener( pPropName
[i
], this );
89 void PropertyChgHelper::RemoveAsPropListener()
93 sal_Int32 nLen
= aPropNames
.getLength();
94 const OUString
*pPropName
= aPropNames
.getConstArray();
95 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
97 if (pPropName
[i
].getLength())
98 xPropSet
->removePropertyChangeListener( pPropName
[i
], this );
104 void PropertyChgHelper::LaunchEvent( const LinguServiceEvent
&rEvt
)
106 cppu::OInterfaceIteratorHelper
aIt( aLngSvcEvtListeners
);
107 while (aIt
.hasMoreElements())
109 Reference
< XLinguServiceEventListener
> xRef( aIt
.next(), UNO_QUERY
);
111 xRef
->processLinguServiceEvent( rEvt
);
116 void SAL_CALL
PropertyChgHelper::disposing( const EventObject
& rSource
)
117 throw(RuntimeException
)
119 MutexGuard
aGuard( GetLinguMutex() );
120 if (rSource
.Source
== xPropSet
)
122 RemoveAsPropListener();
124 aPropNames
.realloc( 0 );
130 PropertyChgHelper::addLinguServiceEventListener(
131 const Reference
< XLinguServiceEventListener
>& rxListener
)
132 throw(RuntimeException
)
134 MutexGuard
aGuard( GetLinguMutex() );
136 sal_Bool bRes
= sal_False
;
139 sal_Int32 nCount
= aLngSvcEvtListeners
.getLength();
140 bRes
= aLngSvcEvtListeners
.addInterface( rxListener
) != nCount
;
147 PropertyChgHelper::removeLinguServiceEventListener(
148 const Reference
< XLinguServiceEventListener
>& rxListener
)
149 throw(RuntimeException
)
151 MutexGuard
aGuard( GetLinguMutex() );
153 sal_Bool bRes
= sal_False
;
156 sal_Int32 nCount
= aLngSvcEvtListeners
.getLength();
157 bRes
= aLngSvcEvtListeners
.removeInterface( rxListener
) != nCount
;
163 static const char *aSP
[] =
165 UPN_IS_GERMAN_PRE_REFORM
,
166 UPN_IS_IGNORE_CONTROL_CHARACTERS
,
167 UPN_IS_USE_DICTIONARY_LIST
,
168 UPN_IS_SPELL_UPPER_CASE
,
169 UPN_IS_SPELL_WITH_DIGITS
,
170 UPN_IS_SPELL_CAPITALIZATION
174 PropertyHelper_Spell::PropertyHelper_Spell(
175 const Reference
< XInterface
> & rxSource
,
176 Reference
< XPropertySet
> &rxPropSet
) :
177 PropertyChgHelper ( rxSource
, rxPropSet
, aSP
, SAL_N_ELEMENTS(aSP
) )
180 sal_Int32 nLen
= GetPropNames().getLength();
181 if (rxPropSet
.is() && nLen
)
183 const OUString
*pPropName
= GetPropNames().getConstArray();
184 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
186 sal_Bool
*pbVal
= NULL
,
189 if (OUString( UPN_IS_GERMAN_PRE_REFORM
) == pPropName
[i
])
191 pbVal
= &bIsGermanPreReform
;
192 pbResVal
= &bResIsGermanPreReform
;
194 else if (OUString( UPN_IS_IGNORE_CONTROL_CHARACTERS
) == pPropName
[i
])
196 pbVal
= &bIsIgnoreControlCharacters
;
197 pbResVal
= &bResIsIgnoreControlCharacters
;
199 else if (OUString( UPN_IS_USE_DICTIONARY_LIST
) == pPropName
[i
])
201 pbVal
= &bIsUseDictionaryList
;
202 pbResVal
= &bResIsUseDictionaryList
;
204 else if (OUString( UPN_IS_SPELL_UPPER_CASE
) == pPropName
[i
])
206 pbVal
= &bIsSpellUpperCase
;
207 pbResVal
= &bResIsSpellUpperCase
;
209 else if (OUString( UPN_IS_SPELL_WITH_DIGITS
) == pPropName
[i
])
211 pbVal
= &bIsSpellWithDigits
;
212 pbResVal
= &bResIsSpellWithDigits
;
214 else if (OUString( UPN_IS_SPELL_CAPITALIZATION
) == pPropName
[i
])
216 pbVal
= &bIsSpellCapitalization
;
217 pbResVal
= &bResIsSpellCapitalization
;
220 if (pbVal
&& pbResVal
)
222 rxPropSet
->getPropertyValue( pPropName
[i
] ) >>= *pbVal
;
230 PropertyHelper_Spell::~PropertyHelper_Spell()
235 void PropertyHelper_Spell::SetDefault()
237 bResIsGermanPreReform
= bIsGermanPreReform
= sal_False
;
238 bResIsIgnoreControlCharacters
= bIsIgnoreControlCharacters
= sal_True
;
239 bResIsUseDictionaryList
= bIsUseDictionaryList
= sal_True
;
240 bResIsSpellUpperCase
= bIsSpellUpperCase
= sal_False
;
241 bResIsSpellWithDigits
= bIsSpellWithDigits
= sal_False
;
242 bResIsSpellCapitalization
= bIsSpellCapitalization
= sal_True
;
247 PropertyHelper_Spell::propertyChange( const PropertyChangeEvent
& rEvt
)
248 throw(RuntimeException
)
250 MutexGuard
aGuard( GetLinguMutex() );
252 if (GetPropSet().is() && rEvt
.Source
== GetPropSet())
254 sal_Int16 nLngSvcFlags
= 0;
255 sal_Bool bSCWA
= sal_False
, // SPELL_CORRECT_WORDS_AGAIN ?
256 bSWWA
= sal_False
; // SPELL_WRONG_WORDS_AGAIN ?
258 sal_Bool
*pbVal
= NULL
;
259 switch (rEvt
.PropertyHandle
)
261 case UPH_IS_IGNORE_CONTROL_CHARACTERS
:
263 pbVal
= &bIsIgnoreControlCharacters
;
266 case UPH_IS_GERMAN_PRE_REFORM
:
268 pbVal
= &bIsGermanPreReform
;
269 bSCWA
= bSWWA
= sal_True
;
272 case UPH_IS_USE_DICTIONARY_LIST
:
274 pbVal
= &bIsUseDictionaryList
;
275 bSCWA
= bSWWA
= sal_True
;
278 case UPH_IS_SPELL_UPPER_CASE
:
280 pbVal
= &bIsSpellUpperCase
;
281 bSCWA
= sal_False
== *pbVal
; // sal_False->sal_True change?
282 bSWWA
= !bSCWA
; // sal_True->sal_False change?
285 case UPH_IS_SPELL_WITH_DIGITS
:
287 pbVal
= &bIsSpellWithDigits
;
288 bSCWA
= sal_False
== *pbVal
; // sal_False->sal_True change?
289 bSWWA
= !bSCWA
; // sal_True->sal_False change?
292 case UPH_IS_SPELL_CAPITALIZATION
:
294 pbVal
= &bIsSpellCapitalization
;
295 bSCWA
= sal_False
== *pbVal
; // sal_False->sal_True change?
296 bSWWA
= !bSCWA
; // sal_True->sal_False change?
300 OSL_FAIL( "unknown property" );
303 rEvt
.NewValue
>>= *pbVal
;
306 nLngSvcFlags
|= LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN
;
308 nLngSvcFlags
|= LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN
;
311 LinguServiceEvent
aEvt( GetEvtObj(), nLngSvcFlags
);
318 void PropertyHelper_Spell::SetTmpPropVals( const PropertyValues
&rPropVals
)
320 // set return value to default value unless there is an
321 // explicitly supplied temporary value
322 bResIsGermanPreReform
= bIsGermanPreReform
;
323 bResIsIgnoreControlCharacters
= bIsIgnoreControlCharacters
;
324 bResIsUseDictionaryList
= bIsUseDictionaryList
;
325 bResIsSpellUpperCase
= bIsSpellUpperCase
;
326 bResIsSpellWithDigits
= bIsSpellWithDigits
;
327 bResIsSpellCapitalization
= bIsSpellCapitalization
;
329 sal_Int32 nLen
= rPropVals
.getLength();
332 const PropertyValue
*pVal
= rPropVals
.getConstArray();
333 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
335 sal_Bool
*pbResVal
= NULL
;
336 switch (pVal
[i
].Handle
)
338 case UPH_IS_GERMAN_PRE_REFORM
: pbResVal
= &bResIsGermanPreReform
; break;
339 case UPH_IS_IGNORE_CONTROL_CHARACTERS
: pbResVal
= &bResIsIgnoreControlCharacters
; break;
340 case UPH_IS_USE_DICTIONARY_LIST
: pbResVal
= &bResIsUseDictionaryList
; break;
341 case UPH_IS_SPELL_UPPER_CASE
: pbResVal
= &bResIsSpellUpperCase
; break;
342 case UPH_IS_SPELL_WITH_DIGITS
: pbResVal
= &bResIsSpellWithDigits
; break;
343 case UPH_IS_SPELL_CAPITALIZATION
: pbResVal
= &bResIsSpellCapitalization
; break;
345 OSL_FAIL( "unknown property" );
348 pVal
[i
].Value
>>= *pbResVal
;
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */