Update ooo320-m1
[ooovba.git] / linguistic / workben / sprophelp.cxx
blob5fe9f29cf4259ab7dc8d01c99aeca48209af17e1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sprophelp.cxx,v $
10 * $Revision: 1.4 $
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 "misc.hxx"
36 #include "sprophelp.hxx"
37 #include "lngprops.hxx"
38 #include <tools/debug.hxx>
40 #include <com/sun/star/linguistic2/LinguServiceEvent.hpp>
41 #include <com/sun/star/linguistic2/LinguServiceEventFlags.hpp>
42 #include <com/sun/star/linguistic2/XLinguServiceEventListener.hpp>
43 #include <com/sun/star/beans/XPropertySet.hpp>
44 #include <osl/mutex.hxx>
46 //using namespace utl;
47 using namespace osl;
48 using namespace rtl;
49 using namespace com::sun::star;
50 using namespace com::sun::star::beans;
51 using namespace com::sun::star::lang;
52 using namespace com::sun::star::uno;
53 using namespace com::sun::star::linguistic2;
54 using namespace linguistic;
57 #define A2OU(x) ::rtl::OUString::createFromAscii( x )
59 ///////////////////////////////////////////////////////////////////////////
62 PropertyChgHelper::PropertyChgHelper(
63 const Reference< XInterface > & rxSource,
64 Reference< XPropertySet > &rxPropSet,
65 const char *pPropNames[], USHORT nPropCount ) :
66 xMyEvtObj (rxSource),
67 xPropSet (rxPropSet),
68 aPropNames (nPropCount),
69 aLngSvcEvtListeners (GetLinguMutex())
71 OUString *pName = aPropNames.getArray();
72 for (INT32 i = 0; i < nPropCount; ++i)
74 pName[i] = A2OU( pPropNames[i] );
79 PropertyChgHelper::PropertyChgHelper( const PropertyChgHelper &rHelper ) :
80 aLngSvcEvtListeners (GetLinguMutex())
82 xPropSet = rHelper.xPropSet;
83 aPropNames = rHelper.aPropNames;
84 AddAsPropListener();
86 xMyEvtObj = rHelper.xMyEvtObj;
90 PropertyChgHelper::~PropertyChgHelper()
95 void PropertyChgHelper::AddAsPropListener()
97 if (xPropSet.is())
99 INT32 nLen = aPropNames.getLength();
100 const OUString *pPropName = aPropNames.getConstArray();
101 for (INT32 i = 0; i < nLen; ++i)
103 if (pPropName[i].getLength())
104 xPropSet->addPropertyChangeListener( pPropName[i], this );
109 void PropertyChgHelper::RemoveAsPropListener()
111 if (xPropSet.is())
113 INT32 nLen = aPropNames.getLength();
114 const OUString *pPropName = aPropNames.getConstArray();
115 for (INT32 i = 0; i < nLen; ++i)
117 if (pPropName[i].getLength())
118 xPropSet->removePropertyChangeListener( pPropName[i], this );
124 void PropertyChgHelper::LaunchEvent( const LinguServiceEvent &rEvt )
126 cppu::OInterfaceIteratorHelper aIt( aLngSvcEvtListeners );
127 while (aIt.hasMoreElements())
129 Reference< XLinguServiceEventListener > xRef( aIt.next(), UNO_QUERY );
130 if (xRef.is())
131 xRef->processLinguServiceEvent( rEvt );
136 void SAL_CALL PropertyChgHelper::disposing( const EventObject& rSource )
137 throw(RuntimeException)
139 MutexGuard aGuard( GetLinguMutex() );
140 if (rSource.Source == xPropSet)
142 RemoveAsPropListener();
143 xPropSet = NULL;
144 aPropNames.realloc( 0 );
149 sal_Bool SAL_CALL
150 PropertyChgHelper::addLinguServiceEventListener(
151 const Reference< XLinguServiceEventListener >& rxListener )
152 throw(RuntimeException)
154 MutexGuard aGuard( GetLinguMutex() );
156 BOOL bRes = FALSE;
157 if (rxListener.is())
159 INT32 nCount = aLngSvcEvtListeners.getLength();
160 bRes = aLngSvcEvtListeners.addInterface( rxListener ) != nCount;
162 return bRes;
166 sal_Bool SAL_CALL
167 PropertyChgHelper::removeLinguServiceEventListener(
168 const Reference< XLinguServiceEventListener >& rxListener )
169 throw(RuntimeException)
171 MutexGuard aGuard( GetLinguMutex() );
173 BOOL bRes = FALSE;
174 if (rxListener.is())
176 INT32 nCount = aLngSvcEvtListeners.getLength();
177 bRes = aLngSvcEvtListeners.removeInterface( rxListener ) != nCount;
179 return bRes;
182 ///////////////////////////////////////////////////////////////////////////
184 static const char *aSP[] =
186 UPN_IS_GERMAN_PRE_REFORM,
187 UPN_IS_IGNORE_CONTROL_CHARACTERS,
188 UPN_IS_USE_DICTIONARY_LIST,
189 UPN_IS_SPELL_UPPER_CASE,
190 UPN_IS_SPELL_WITH_DIGITS,
191 UPN_IS_SPELL_CAPITALIZATION
195 PropertyHelper_Spell::PropertyHelper_Spell(
196 const Reference< XInterface > & rxSource,
197 Reference< XPropertySet > &rxPropSet ) :
198 PropertyChgHelper ( rxSource, rxPropSet, aSP, sizeof(aSP) / sizeof(aSP[0]) )
200 SetDefault();
201 INT32 nLen = GetPropNames().getLength();
202 if (rxPropSet.is() && nLen)
204 const OUString *pPropName = GetPropNames().getConstArray();
205 for (INT32 i = 0; i < nLen; ++i)
207 BOOL *pbVal = NULL,
208 *pbResVal = NULL;
210 if (A2OU( UPN_IS_GERMAN_PRE_REFORM ) == pPropName[i])
212 pbVal = &bIsGermanPreReform;
213 pbResVal = &bResIsGermanPreReform;
215 else if (A2OU( UPN_IS_IGNORE_CONTROL_CHARACTERS ) == pPropName[i])
217 pbVal = &bIsIgnoreControlCharacters;
218 pbResVal = &bResIsIgnoreControlCharacters;
220 else if (A2OU( UPN_IS_USE_DICTIONARY_LIST ) == pPropName[i])
222 pbVal = &bIsUseDictionaryList;
223 pbResVal = &bResIsUseDictionaryList;
225 else if (A2OU( UPN_IS_SPELL_UPPER_CASE ) == pPropName[i])
227 pbVal = &bIsSpellUpperCase;
228 pbResVal = &bResIsSpellUpperCase;
230 else if (A2OU( UPN_IS_SPELL_WITH_DIGITS ) == pPropName[i])
232 pbVal = &bIsSpellWithDigits;
233 pbResVal = &bResIsSpellWithDigits;
235 else if (A2OU( UPN_IS_SPELL_CAPITALIZATION ) == pPropName[i])
237 pbVal = &bIsSpellCapitalization;
238 pbResVal = &bResIsSpellCapitalization;
241 if (pbVal && pbResVal)
243 rxPropSet->getPropertyValue( pPropName[i] ) >>= *pbVal;
244 *pbResVal = *pbVal;
251 PropertyHelper_Spell::~PropertyHelper_Spell()
256 void PropertyHelper_Spell::SetDefault()
258 bResIsGermanPreReform = bIsGermanPreReform = FALSE;
259 bResIsIgnoreControlCharacters = bIsIgnoreControlCharacters = TRUE;
260 bResIsUseDictionaryList = bIsUseDictionaryList = TRUE;
261 bResIsSpellUpperCase = bIsSpellUpperCase = FALSE;
262 bResIsSpellWithDigits = bIsSpellWithDigits = FALSE;
263 bResIsSpellCapitalization = bIsSpellCapitalization = TRUE;
267 void SAL_CALL
268 PropertyHelper_Spell::propertyChange( const PropertyChangeEvent& rEvt )
269 throw(RuntimeException)
271 MutexGuard aGuard( GetLinguMutex() );
273 if (GetPropSet().is() && rEvt.Source == GetPropSet())
275 INT16 nLngSvcFlags = 0;
276 BOOL bSCWA = FALSE, // SPELL_CORRECT_WORDS_AGAIN ?
277 bSWWA = FALSE; // SPELL_WRONG_WORDS_AGAIN ?
279 BOOL *pbVal = NULL;
280 switch (rEvt.PropertyHandle)
282 case UPH_IS_IGNORE_CONTROL_CHARACTERS :
284 pbVal = &bIsIgnoreControlCharacters;
285 break;
287 case UPH_IS_GERMAN_PRE_REFORM :
289 pbVal = &bIsGermanPreReform;
290 bSCWA = bSWWA = TRUE;
291 break;
293 case UPH_IS_USE_DICTIONARY_LIST :
295 pbVal = &bIsUseDictionaryList;
296 bSCWA = bSWWA = TRUE;
297 break;
299 case UPH_IS_SPELL_UPPER_CASE :
301 pbVal = &bIsSpellUpperCase;
302 bSCWA = FALSE == *pbVal; // FALSE->TRUE change?
303 bSWWA = !bSCWA; // TRUE->FALSE change?
304 break;
306 case UPH_IS_SPELL_WITH_DIGITS :
308 pbVal = &bIsSpellWithDigits;
309 bSCWA = FALSE == *pbVal; // FALSE->TRUE change?
310 bSWWA = !bSCWA; // TRUE->FALSE change?
311 break;
313 case UPH_IS_SPELL_CAPITALIZATION :
315 pbVal = &bIsSpellCapitalization;
316 bSCWA = FALSE == *pbVal; // FALSE->TRUE change?
317 bSWWA = !bSCWA; // TRUE->FALSE change?
318 break;
320 default:
321 DBG_ERROR( "unknown property" );
323 if (pbVal)
324 rEvt.NewValue >>= *pbVal;
326 if (bSCWA)
327 nLngSvcFlags |= LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN;
328 if (bSWWA)
329 nLngSvcFlags |= LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN;
330 if (nLngSvcFlags)
332 LinguServiceEvent aEvt( GetEvtObj(), nLngSvcFlags );
333 LaunchEvent( aEvt );
339 void PropertyHelper_Spell::SetTmpPropVals( const PropertyValues &rPropVals )
341 // set return value to default value unless there is an
342 // explicitly supplied temporary value
343 bResIsGermanPreReform = bIsGermanPreReform;
344 bResIsIgnoreControlCharacters = bIsIgnoreControlCharacters;
345 bResIsUseDictionaryList = bIsUseDictionaryList;
346 bResIsSpellUpperCase = bIsSpellUpperCase;
347 bResIsSpellWithDigits = bIsSpellWithDigits;
348 bResIsSpellCapitalization = bIsSpellCapitalization;
350 INT32 nLen = rPropVals.getLength();
351 if (nLen)
353 const PropertyValue *pVal = rPropVals.getConstArray();
354 for (INT32 i = 0; i < nLen; ++i)
356 BOOL *pbResVal = NULL;
357 switch (pVal[i].Handle)
359 case UPH_IS_GERMAN_PRE_REFORM : pbResVal = &bResIsGermanPreReform; break;
360 case UPH_IS_IGNORE_CONTROL_CHARACTERS : pbResVal = &bResIsIgnoreControlCharacters; break;
361 case UPH_IS_USE_DICTIONARY_LIST : pbResVal = &bResIsUseDictionaryList; break;
362 case UPH_IS_SPELL_UPPER_CASE : pbResVal = &bResIsSpellUpperCase; break;
363 case UPH_IS_SPELL_WITH_DIGITS : pbResVal = &bResIsSpellWithDigits; break;
364 case UPH_IS_SPELL_CAPITALIZATION : pbResVal = &bResIsSpellCapitalization; break;
365 default:
366 DBG_ERROR( "unknown property" );
368 if (pbResVal)
369 pVal[i].Value >>= *pbResVal;
374 ///////////////////////////////////////////////////////////////////////////