bump product version to 7.6.3.2-android
[LibreOffice.git] / linguistic / workben / sprophelp.cxx
blob9da626319a7255a606808c113ef6e3140fcd3e03
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
33 using namespace osl;
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)
47 , xMyEvtObj(rxSource)
48 , aLngSvcEvtListeners(GetLinguMutex())
49 , xPropSet(rxPropSet)
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)
65 AddAsPropListener();
70 PropertyChgHelper::~PropertyChgHelper()
75 void PropertyChgHelper::AddAsPropListener()
77 if (xPropSet.is())
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()
91 if (xPropSet.is())
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 );
110 if (xRef.is())
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();
123 xPropSet = NULL;
124 aPropNames.realloc( 0 );
129 sal_Bool SAL_CALL
130 PropertyChgHelper::addLinguServiceEventListener(
131 const Reference< XLinguServiceEventListener >& rxListener )
132 throw(RuntimeException)
134 MutexGuard aGuard( GetLinguMutex() );
136 sal_Bool bRes = sal_False;
137 if (rxListener.is())
139 sal_Int32 nCount = aLngSvcEvtListeners.getLength();
140 bRes = aLngSvcEvtListeners.addInterface( rxListener ) != nCount;
142 return bRes;
146 sal_Bool SAL_CALL
147 PropertyChgHelper::removeLinguServiceEventListener(
148 const Reference< XLinguServiceEventListener >& rxListener )
149 throw(RuntimeException)
151 MutexGuard aGuard( GetLinguMutex() );
153 sal_Bool bRes = sal_False;
154 if (rxListener.is())
156 sal_Int32 nCount = aLngSvcEvtListeners.getLength();
157 bRes = aLngSvcEvtListeners.removeInterface( rxListener ) != nCount;
159 return bRes;
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,
171 UPN_IS_SPELL_CLOSED_COMPOUND,
172 UPN_IS_SPELL_HYPHENATED_COMPOUND
176 PropertyHelper_Spell::PropertyHelper_Spell(
177 const Reference< XInterface > & rxSource,
178 Reference< XPropertySet > &rxPropSet ) :
179 PropertyChgHelper ( rxSource, rxPropSet, aSP, SAL_N_ELEMENTS(aSP) )
181 SetDefault();
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,
189 *pbResVal = 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;
221 else if (OUString( UPN_IS_SPELL_CLOSED_COMPOUND ) == pPropName[i])
223 pbVal = &bIsSpellClosedCompound;
224 pbResVal = &bResIsSpellClosedCompound;
226 else if (OUString( UPN_IS_SPELL_HYPHENATED_COMPOUND ) == pPropName[i])
228 pbVal = &bIsSpellHyphenatedCompound;
229 pbResVal = &bResIsSpellHyphenatedCompound;
232 if (pbVal && pbResVal)
234 rxPropSet->getPropertyValue( pPropName[i] ) >>= *pbVal;
235 *pbResVal = *pbVal;
242 PropertyHelper_Spell::~PropertyHelper_Spell()
247 void PropertyHelper_Spell::SetDefault()
249 bResIsGermanPreReform = bIsGermanPreReform = sal_False;
250 bResIsIgnoreControlCharacters = bIsIgnoreControlCharacters = sal_True;
251 bResIsUseDictionaryList = bIsUseDictionaryList = sal_True;
252 bResIsSpellUpperCase = bIsSpellUpperCase = sal_False;
253 bResIsSpellWithDigits = bIsSpellWithDigits = sal_False;
254 bResIsSpellCapitalization = bIsSpellCapitalization = sal_True;
255 bResIsSpellClosedCompound = bIsSpellClosedCompound = sal_True;
256 bResIsSpellHyphenatedCompound = bIsSpellHyphenatedCompound = sal_True;
260 void SAL_CALL
261 PropertyHelper_Spell::propertyChange( const PropertyChangeEvent& rEvt )
262 throw(RuntimeException)
264 MutexGuard aGuard( GetLinguMutex() );
266 if (GetPropSet().is() && rEvt.Source == GetPropSet())
268 sal_Int16 nLngSvcFlags = 0;
269 sal_Bool bSCWA = sal_False, // SPELL_CORRECT_WORDS_AGAIN ?
270 bSWWA = sal_False; // SPELL_WRONG_WORDS_AGAIN ?
272 sal_Bool *pbVal = NULL;
273 switch (rEvt.PropertyHandle)
275 case UPH_IS_IGNORE_CONTROL_CHARACTERS :
277 pbVal = &bIsIgnoreControlCharacters;
278 break;
280 case UPH_IS_GERMAN_PRE_REFORM :
282 pbVal = &bIsGermanPreReform;
283 bSCWA = bSWWA = sal_True;
284 break;
286 case UPH_IS_USE_DICTIONARY_LIST :
288 pbVal = &bIsUseDictionaryList;
289 bSCWA = bSWWA = sal_True;
290 break;
292 case UPH_IS_SPELL_UPPER_CASE :
294 pbVal = &bIsSpellUpperCase;
295 bSCWA = sal_False == *pbVal; // sal_False->sal_True change?
296 bSWWA = !bSCWA; // sal_True->sal_False change?
297 break;
299 case UPH_IS_SPELL_WITH_DIGITS :
301 pbVal = &bIsSpellWithDigits;
302 bSCWA = sal_False == *pbVal; // sal_False->sal_True change?
303 bSWWA = !bSCWA; // sal_True->sal_False change?
304 break;
306 case UPH_IS_SPELL_CAPITALIZATION :
308 pbVal = &bIsSpellCapitalization;
309 bSCWA = sal_False == *pbVal; // sal_False->sal_True change?
310 bSWWA = !bSCWA; // sal_True->sal_False change?
311 break;
313 case UPH_IS_SPELL_CLOSED_COMPOUND :
315 pbVal = &bIsSpellClosedCompound;
316 bSCWA = sal_False == *pbVal; // sal_False->sal_True change?
317 bSWWA = !bSCWA; // sal_True->sal_False change?
318 break;
320 case UPH_IS_SPELL_HYPHENATED_COMPOUND :
322 pbVal = &bIsSpellHyphenatedCompound;
323 bSCWA = sal_False == *pbVal; // sal_False->sal_True change?
324 bSWWA = !bSCWA; // sal_True->sal_False change?
325 break;
328 default:
329 OSL_FAIL( "unknown property" );
331 if (pbVal)
332 rEvt.NewValue >>= *pbVal;
334 if (bSCWA)
335 nLngSvcFlags |= LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN;
336 if (bSWWA)
337 nLngSvcFlags |= LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN;
338 if (nLngSvcFlags)
340 LinguServiceEvent aEvt( GetEvtObj(), nLngSvcFlags );
341 LaunchEvent( aEvt );
347 void PropertyHelper_Spell::SetTmpPropVals( const PropertyValues &rPropVals )
349 // set return value to default value unless there is an
350 // explicitly supplied temporary value
351 bResIsGermanPreReform = bIsGermanPreReform;
352 bResIsIgnoreControlCharacters = bIsIgnoreControlCharacters;
353 bResIsUseDictionaryList = bIsUseDictionaryList;
354 bResIsSpellUpperCase = bIsSpellUpperCase;
355 bResIsSpellWithDigits = bIsSpellWithDigits;
356 bResIsSpellCapitalization = bIsSpellCapitalization;
357 bResIsSpellClosedCompound = bIsSpellClosedCompound;
358 bResIsSpellHyphenatedCompound = bIsSpellHyphenatedCompound;
360 sal_Int32 nLen = rPropVals.getLength();
361 if (nLen)
363 const PropertyValue *pVal = rPropVals.getConstArray();
364 for (sal_Int32 i = 0; i < nLen; ++i)
366 sal_Bool *pbResVal = NULL;
367 switch (pVal[i].Handle)
369 case UPH_IS_GERMAN_PRE_REFORM : pbResVal = &bResIsGermanPreReform; break;
370 case UPH_IS_IGNORE_CONTROL_CHARACTERS : pbResVal = &bResIsIgnoreControlCharacters; break;
371 case UPH_IS_USE_DICTIONARY_LIST : pbResVal = &bResIsUseDictionaryList; break;
372 case UPH_IS_SPELL_UPPER_CASE : pbResVal = &bResIsSpellUpperCase; break;
373 case UPH_IS_SPELL_WITH_DIGITS : pbResVal = &bResIsSpellWithDigits; break;
374 case UPH_IS_SPELL_CAPITALIZATION : pbResVal = &bResIsSpellCapitalization; break;
375 case UPH_IS_SPELL_CLOSED_COMPOUND : pbResVal = &bResIsSpellClosedCompound; break;
376 case UPH_IS_SPELL_HYPHENATED_COMPOUND : pbResVal = &bResIsSpellHyphenatedCompound; break;
377 default:
378 OSL_FAIL( "unknown property" );
380 if (pbResVal)
381 pVal[i].Value >>= *pbResVal;
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */