1*3b8558fdSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*3b8558fdSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*3b8558fdSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*3b8558fdSAndrew Rist * distributed with this work for additional information
6*3b8558fdSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*3b8558fdSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*3b8558fdSAndrew Rist * "License"); you may not use this file except in compliance
9*3b8558fdSAndrew Rist * with the License. You may obtain a copy of the License at
10*3b8558fdSAndrew Rist *
11*3b8558fdSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*3b8558fdSAndrew Rist *
13*3b8558fdSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*3b8558fdSAndrew Rist * software distributed under the License is distributed on an
15*3b8558fdSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*3b8558fdSAndrew Rist * KIND, either express or implied. See the License for the
17*3b8558fdSAndrew Rist * specific language governing permissions and limitations
18*3b8558fdSAndrew Rist * under the License.
19*3b8558fdSAndrew Rist *
20*3b8558fdSAndrew Rist *************************************************************/
21*3b8558fdSAndrew Rist
22*3b8558fdSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_linguistic.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "linguistic/misc.hxx"
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include "sprophelp.hxx"
30cdf0e10cSrcweir #include "linguistic/lngprops.hxx"
31cdf0e10cSrcweir #include <tools/debug.hxx>
32cdf0e10cSrcweir
33cdf0e10cSrcweir #include <com/sun/star/linguistic2/LinguServiceEvent.hpp>
34cdf0e10cSrcweir #include <com/sun/star/linguistic2/LinguServiceEventFlags.hpp>
35cdf0e10cSrcweir #include <com/sun/star/linguistic2/XLinguServiceEventListener.hpp>
36cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
37cdf0e10cSrcweir #include <osl/mutex.hxx>
38cdf0e10cSrcweir
39cdf0e10cSrcweir //using namespace utl;
40cdf0e10cSrcweir using namespace osl;
41cdf0e10cSrcweir using namespace rtl;
42cdf0e10cSrcweir using namespace com::sun::star;
43cdf0e10cSrcweir using namespace com::sun::star::beans;
44cdf0e10cSrcweir using namespace com::sun::star::lang;
45cdf0e10cSrcweir using namespace com::sun::star::uno;
46cdf0e10cSrcweir using namespace com::sun::star::linguistic2;
47cdf0e10cSrcweir using namespace linguistic;
48cdf0e10cSrcweir
49cdf0e10cSrcweir
50cdf0e10cSrcweir #define A2OU(x) ::rtl::OUString::createFromAscii( x )
51cdf0e10cSrcweir
52cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
53cdf0e10cSrcweir
54cdf0e10cSrcweir
PropertyChgHelper(const Reference<XInterface> & rxSource,Reference<XPropertySet> & rxPropSet,const char * pPropNames[],USHORT nPropCount)55cdf0e10cSrcweir PropertyChgHelper::PropertyChgHelper(
56cdf0e10cSrcweir const Reference< XInterface > & rxSource,
57cdf0e10cSrcweir Reference< XPropertySet > &rxPropSet,
58cdf0e10cSrcweir const char *pPropNames[], USHORT nPropCount ) :
59cdf0e10cSrcweir xMyEvtObj (rxSource),
60cdf0e10cSrcweir xPropSet (rxPropSet),
61cdf0e10cSrcweir aPropNames (nPropCount),
62cdf0e10cSrcweir aLngSvcEvtListeners (GetLinguMutex())
63cdf0e10cSrcweir {
64cdf0e10cSrcweir OUString *pName = aPropNames.getArray();
65cdf0e10cSrcweir for (INT32 i = 0; i < nPropCount; ++i)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir pName[i] = A2OU( pPropNames[i] );
68cdf0e10cSrcweir }
69cdf0e10cSrcweir }
70cdf0e10cSrcweir
71cdf0e10cSrcweir
PropertyChgHelper(const PropertyChgHelper & rHelper)72cdf0e10cSrcweir PropertyChgHelper::PropertyChgHelper( const PropertyChgHelper &rHelper ) :
73cdf0e10cSrcweir aLngSvcEvtListeners (GetLinguMutex())
74cdf0e10cSrcweir {
75cdf0e10cSrcweir xPropSet = rHelper.xPropSet;
76cdf0e10cSrcweir aPropNames = rHelper.aPropNames;
77cdf0e10cSrcweir AddAsPropListener();
78cdf0e10cSrcweir
79cdf0e10cSrcweir xMyEvtObj = rHelper.xMyEvtObj;
80cdf0e10cSrcweir }
81cdf0e10cSrcweir
82cdf0e10cSrcweir
~PropertyChgHelper()83cdf0e10cSrcweir PropertyChgHelper::~PropertyChgHelper()
84cdf0e10cSrcweir {
85cdf0e10cSrcweir }
86cdf0e10cSrcweir
87cdf0e10cSrcweir
AddAsPropListener()88cdf0e10cSrcweir void PropertyChgHelper::AddAsPropListener()
89cdf0e10cSrcweir {
90cdf0e10cSrcweir if (xPropSet.is())
91cdf0e10cSrcweir {
92cdf0e10cSrcweir INT32 nLen = aPropNames.getLength();
93cdf0e10cSrcweir const OUString *pPropName = aPropNames.getConstArray();
94cdf0e10cSrcweir for (INT32 i = 0; i < nLen; ++i)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir if (pPropName[i].getLength())
97cdf0e10cSrcweir xPropSet->addPropertyChangeListener( pPropName[i], this );
98cdf0e10cSrcweir }
99cdf0e10cSrcweir }
100cdf0e10cSrcweir }
101cdf0e10cSrcweir
RemoveAsPropListener()102cdf0e10cSrcweir void PropertyChgHelper::RemoveAsPropListener()
103cdf0e10cSrcweir {
104cdf0e10cSrcweir if (xPropSet.is())
105cdf0e10cSrcweir {
106cdf0e10cSrcweir INT32 nLen = aPropNames.getLength();
107cdf0e10cSrcweir const OUString *pPropName = aPropNames.getConstArray();
108cdf0e10cSrcweir for (INT32 i = 0; i < nLen; ++i)
109cdf0e10cSrcweir {
110cdf0e10cSrcweir if (pPropName[i].getLength())
111cdf0e10cSrcweir xPropSet->removePropertyChangeListener( pPropName[i], this );
112cdf0e10cSrcweir }
113cdf0e10cSrcweir }
114cdf0e10cSrcweir }
115cdf0e10cSrcweir
116cdf0e10cSrcweir
LaunchEvent(const LinguServiceEvent & rEvt)117cdf0e10cSrcweir void PropertyChgHelper::LaunchEvent( const LinguServiceEvent &rEvt )
118cdf0e10cSrcweir {
119cdf0e10cSrcweir cppu::OInterfaceIteratorHelper aIt( aLngSvcEvtListeners );
120cdf0e10cSrcweir while (aIt.hasMoreElements())
121cdf0e10cSrcweir {
122cdf0e10cSrcweir Reference< XLinguServiceEventListener > xRef( aIt.next(), UNO_QUERY );
123cdf0e10cSrcweir if (xRef.is())
124cdf0e10cSrcweir xRef->processLinguServiceEvent( rEvt );
125cdf0e10cSrcweir }
126cdf0e10cSrcweir }
127cdf0e10cSrcweir
128cdf0e10cSrcweir
disposing(const EventObject & rSource)129cdf0e10cSrcweir void SAL_CALL PropertyChgHelper::disposing( const EventObject& rSource )
130cdf0e10cSrcweir throw(RuntimeException)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
133cdf0e10cSrcweir if (rSource.Source == xPropSet)
134cdf0e10cSrcweir {
135cdf0e10cSrcweir RemoveAsPropListener();
136cdf0e10cSrcweir xPropSet = NULL;
137cdf0e10cSrcweir aPropNames.realloc( 0 );
138cdf0e10cSrcweir }
139cdf0e10cSrcweir }
140cdf0e10cSrcweir
141cdf0e10cSrcweir
142cdf0e10cSrcweir sal_Bool SAL_CALL
addLinguServiceEventListener(const Reference<XLinguServiceEventListener> & rxListener)143cdf0e10cSrcweir PropertyChgHelper::addLinguServiceEventListener(
144cdf0e10cSrcweir const Reference< XLinguServiceEventListener >& rxListener )
145cdf0e10cSrcweir throw(RuntimeException)
146cdf0e10cSrcweir {
147cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
148cdf0e10cSrcweir
149cdf0e10cSrcweir BOOL bRes = FALSE;
150cdf0e10cSrcweir if (rxListener.is())
151cdf0e10cSrcweir {
152cdf0e10cSrcweir INT32 nCount = aLngSvcEvtListeners.getLength();
153cdf0e10cSrcweir bRes = aLngSvcEvtListeners.addInterface( rxListener ) != nCount;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir return bRes;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir
158cdf0e10cSrcweir
159cdf0e10cSrcweir sal_Bool SAL_CALL
removeLinguServiceEventListener(const Reference<XLinguServiceEventListener> & rxListener)160cdf0e10cSrcweir PropertyChgHelper::removeLinguServiceEventListener(
161cdf0e10cSrcweir const Reference< XLinguServiceEventListener >& rxListener )
162cdf0e10cSrcweir throw(RuntimeException)
163cdf0e10cSrcweir {
164cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
165cdf0e10cSrcweir
166cdf0e10cSrcweir BOOL bRes = FALSE;
167cdf0e10cSrcweir if (rxListener.is())
168cdf0e10cSrcweir {
169cdf0e10cSrcweir INT32 nCount = aLngSvcEvtListeners.getLength();
170cdf0e10cSrcweir bRes = aLngSvcEvtListeners.removeInterface( rxListener ) != nCount;
171cdf0e10cSrcweir }
172cdf0e10cSrcweir return bRes;
173cdf0e10cSrcweir }
174cdf0e10cSrcweir
175cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
176cdf0e10cSrcweir
177cdf0e10cSrcweir static const char *aSP[] =
178cdf0e10cSrcweir {
179cdf0e10cSrcweir UPN_IS_GERMAN_PRE_REFORM,
180cdf0e10cSrcweir UPN_IS_IGNORE_CONTROL_CHARACTERS,
181cdf0e10cSrcweir UPN_IS_USE_DICTIONARY_LIST,
182cdf0e10cSrcweir UPN_IS_SPELL_UPPER_CASE,
183cdf0e10cSrcweir UPN_IS_SPELL_WITH_DIGITS,
184cdf0e10cSrcweir UPN_IS_SPELL_CAPITALIZATION
185cdf0e10cSrcweir };
186cdf0e10cSrcweir
187cdf0e10cSrcweir
PropertyHelper_Spell(const Reference<XInterface> & rxSource,Reference<XPropertySet> & rxPropSet)188cdf0e10cSrcweir PropertyHelper_Spell::PropertyHelper_Spell(
189cdf0e10cSrcweir const Reference< XInterface > & rxSource,
190cdf0e10cSrcweir Reference< XPropertySet > &rxPropSet ) :
191cdf0e10cSrcweir PropertyChgHelper ( rxSource, rxPropSet, aSP, sizeof(aSP) / sizeof(aSP[0]) )
192cdf0e10cSrcweir {
193cdf0e10cSrcweir SetDefault();
194cdf0e10cSrcweir INT32 nLen = GetPropNames().getLength();
195cdf0e10cSrcweir if (rxPropSet.is() && nLen)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir const OUString *pPropName = GetPropNames().getConstArray();
198cdf0e10cSrcweir for (INT32 i = 0; i < nLen; ++i)
199cdf0e10cSrcweir {
200cdf0e10cSrcweir BOOL *pbVal = NULL,
201cdf0e10cSrcweir *pbResVal = NULL;
202cdf0e10cSrcweir
203cdf0e10cSrcweir if (A2OU( UPN_IS_GERMAN_PRE_REFORM ) == pPropName[i])
204cdf0e10cSrcweir {
205cdf0e10cSrcweir pbVal = &bIsGermanPreReform;
206cdf0e10cSrcweir pbResVal = &bResIsGermanPreReform;
207cdf0e10cSrcweir }
208cdf0e10cSrcweir else if (A2OU( UPN_IS_IGNORE_CONTROL_CHARACTERS ) == pPropName[i])
209cdf0e10cSrcweir {
210cdf0e10cSrcweir pbVal = &bIsIgnoreControlCharacters;
211cdf0e10cSrcweir pbResVal = &bResIsIgnoreControlCharacters;
212cdf0e10cSrcweir }
213cdf0e10cSrcweir else if (A2OU( UPN_IS_USE_DICTIONARY_LIST ) == pPropName[i])
214cdf0e10cSrcweir {
215cdf0e10cSrcweir pbVal = &bIsUseDictionaryList;
216cdf0e10cSrcweir pbResVal = &bResIsUseDictionaryList;
217cdf0e10cSrcweir }
218cdf0e10cSrcweir else if (A2OU( UPN_IS_SPELL_UPPER_CASE ) == pPropName[i])
219cdf0e10cSrcweir {
220cdf0e10cSrcweir pbVal = &bIsSpellUpperCase;
221cdf0e10cSrcweir pbResVal = &bResIsSpellUpperCase;
222cdf0e10cSrcweir }
223cdf0e10cSrcweir else if (A2OU( UPN_IS_SPELL_WITH_DIGITS ) == pPropName[i])
224cdf0e10cSrcweir {
225cdf0e10cSrcweir pbVal = &bIsSpellWithDigits;
226cdf0e10cSrcweir pbResVal = &bResIsSpellWithDigits;
227cdf0e10cSrcweir }
228cdf0e10cSrcweir else if (A2OU( UPN_IS_SPELL_CAPITALIZATION ) == pPropName[i])
229cdf0e10cSrcweir {
230cdf0e10cSrcweir pbVal = &bIsSpellCapitalization;
231cdf0e10cSrcweir pbResVal = &bResIsSpellCapitalization;
232cdf0e10cSrcweir }
233cdf0e10cSrcweir
234cdf0e10cSrcweir if (pbVal && pbResVal)
235cdf0e10cSrcweir {
236cdf0e10cSrcweir rxPropSet->getPropertyValue( pPropName[i] ) >>= *pbVal;
237cdf0e10cSrcweir *pbResVal = *pbVal;
238cdf0e10cSrcweir }
239cdf0e10cSrcweir }
240cdf0e10cSrcweir }
241cdf0e10cSrcweir }
242cdf0e10cSrcweir
243cdf0e10cSrcweir
~PropertyHelper_Spell()244cdf0e10cSrcweir PropertyHelper_Spell::~PropertyHelper_Spell()
245cdf0e10cSrcweir {
246cdf0e10cSrcweir }
247cdf0e10cSrcweir
248cdf0e10cSrcweir
SetDefault()249cdf0e10cSrcweir void PropertyHelper_Spell::SetDefault()
250cdf0e10cSrcweir {
251cdf0e10cSrcweir bResIsGermanPreReform = bIsGermanPreReform = FALSE;
252cdf0e10cSrcweir bResIsIgnoreControlCharacters = bIsIgnoreControlCharacters = TRUE;
253cdf0e10cSrcweir bResIsUseDictionaryList = bIsUseDictionaryList = TRUE;
254cdf0e10cSrcweir bResIsSpellUpperCase = bIsSpellUpperCase = FALSE;
255cdf0e10cSrcweir bResIsSpellWithDigits = bIsSpellWithDigits = FALSE;
256cdf0e10cSrcweir bResIsSpellCapitalization = bIsSpellCapitalization = TRUE;
257cdf0e10cSrcweir }
258cdf0e10cSrcweir
259cdf0e10cSrcweir
260cdf0e10cSrcweir void SAL_CALL
propertyChange(const PropertyChangeEvent & rEvt)261cdf0e10cSrcweir PropertyHelper_Spell::propertyChange( const PropertyChangeEvent& rEvt )
262cdf0e10cSrcweir throw(RuntimeException)
263cdf0e10cSrcweir {
264cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
265cdf0e10cSrcweir
266cdf0e10cSrcweir if (GetPropSet().is() && rEvt.Source == GetPropSet())
267cdf0e10cSrcweir {
268cdf0e10cSrcweir INT16 nLngSvcFlags = 0;
269cdf0e10cSrcweir BOOL bSCWA = FALSE, // SPELL_CORRECT_WORDS_AGAIN ?
270cdf0e10cSrcweir bSWWA = FALSE; // SPELL_WRONG_WORDS_AGAIN ?
271cdf0e10cSrcweir
272cdf0e10cSrcweir BOOL *pbVal = NULL;
273cdf0e10cSrcweir switch (rEvt.PropertyHandle)
274cdf0e10cSrcweir {
275cdf0e10cSrcweir case UPH_IS_IGNORE_CONTROL_CHARACTERS :
276cdf0e10cSrcweir {
277cdf0e10cSrcweir pbVal = &bIsIgnoreControlCharacters;
278cdf0e10cSrcweir break;
279cdf0e10cSrcweir }
280cdf0e10cSrcweir case UPH_IS_GERMAN_PRE_REFORM :
281cdf0e10cSrcweir {
282cdf0e10cSrcweir pbVal = &bIsGermanPreReform;
283cdf0e10cSrcweir bSCWA = bSWWA = TRUE;
284cdf0e10cSrcweir break;
285cdf0e10cSrcweir }
286cdf0e10cSrcweir case UPH_IS_USE_DICTIONARY_LIST :
287cdf0e10cSrcweir {
288cdf0e10cSrcweir pbVal = &bIsUseDictionaryList;
289cdf0e10cSrcweir bSCWA = bSWWA = TRUE;
290cdf0e10cSrcweir break;
291cdf0e10cSrcweir }
292cdf0e10cSrcweir case UPH_IS_SPELL_UPPER_CASE :
293cdf0e10cSrcweir {
294cdf0e10cSrcweir pbVal = &bIsSpellUpperCase;
295cdf0e10cSrcweir bSCWA = FALSE == *pbVal; // FALSE->TRUE change?
296cdf0e10cSrcweir bSWWA = !bSCWA; // TRUE->FALSE change?
297cdf0e10cSrcweir break;
298cdf0e10cSrcweir }
299cdf0e10cSrcweir case UPH_IS_SPELL_WITH_DIGITS :
300cdf0e10cSrcweir {
301cdf0e10cSrcweir pbVal = &bIsSpellWithDigits;
302cdf0e10cSrcweir bSCWA = FALSE == *pbVal; // FALSE->TRUE change?
303cdf0e10cSrcweir bSWWA = !bSCWA; // TRUE->FALSE change?
304cdf0e10cSrcweir break;
305cdf0e10cSrcweir }
306cdf0e10cSrcweir case UPH_IS_SPELL_CAPITALIZATION :
307cdf0e10cSrcweir {
308cdf0e10cSrcweir pbVal = &bIsSpellCapitalization;
309cdf0e10cSrcweir bSCWA = FALSE == *pbVal; // FALSE->TRUE change?
310cdf0e10cSrcweir bSWWA = !bSCWA; // TRUE->FALSE change?
311cdf0e10cSrcweir break;
312cdf0e10cSrcweir }
313cdf0e10cSrcweir default:
314cdf0e10cSrcweir DBG_ERROR( "unknown property" );
315cdf0e10cSrcweir }
316cdf0e10cSrcweir if (pbVal)
317cdf0e10cSrcweir rEvt.NewValue >>= *pbVal;
318cdf0e10cSrcweir
319cdf0e10cSrcweir if (bSCWA)
320cdf0e10cSrcweir nLngSvcFlags |= LinguServiceEventFlags::SPELL_CORRECT_WORDS_AGAIN;
321cdf0e10cSrcweir if (bSWWA)
322cdf0e10cSrcweir nLngSvcFlags |= LinguServiceEventFlags::SPELL_WRONG_WORDS_AGAIN;
323cdf0e10cSrcweir if (nLngSvcFlags)
324cdf0e10cSrcweir {
325cdf0e10cSrcweir LinguServiceEvent aEvt( GetEvtObj(), nLngSvcFlags );
326cdf0e10cSrcweir LaunchEvent( aEvt );
327cdf0e10cSrcweir }
328cdf0e10cSrcweir }
329cdf0e10cSrcweir }
330cdf0e10cSrcweir
331cdf0e10cSrcweir
SetTmpPropVals(const PropertyValues & rPropVals)332cdf0e10cSrcweir void PropertyHelper_Spell::SetTmpPropVals( const PropertyValues &rPropVals )
333cdf0e10cSrcweir {
334cdf0e10cSrcweir // set return value to default value unless there is an
335cdf0e10cSrcweir // explicitly supplied temporary value
336cdf0e10cSrcweir bResIsGermanPreReform = bIsGermanPreReform;
337cdf0e10cSrcweir bResIsIgnoreControlCharacters = bIsIgnoreControlCharacters;
338cdf0e10cSrcweir bResIsUseDictionaryList = bIsUseDictionaryList;
339cdf0e10cSrcweir bResIsSpellUpperCase = bIsSpellUpperCase;
340cdf0e10cSrcweir bResIsSpellWithDigits = bIsSpellWithDigits;
341cdf0e10cSrcweir bResIsSpellCapitalization = bIsSpellCapitalization;
342cdf0e10cSrcweir //
343cdf0e10cSrcweir INT32 nLen = rPropVals.getLength();
344cdf0e10cSrcweir if (nLen)
345cdf0e10cSrcweir {
346cdf0e10cSrcweir const PropertyValue *pVal = rPropVals.getConstArray();
347cdf0e10cSrcweir for (INT32 i = 0; i < nLen; ++i)
348cdf0e10cSrcweir {
349cdf0e10cSrcweir BOOL *pbResVal = NULL;
350cdf0e10cSrcweir switch (pVal[i].Handle)
351cdf0e10cSrcweir {
352cdf0e10cSrcweir case UPH_IS_GERMAN_PRE_REFORM : pbResVal = &bResIsGermanPreReform; break;
353cdf0e10cSrcweir case UPH_IS_IGNORE_CONTROL_CHARACTERS : pbResVal = &bResIsIgnoreControlCharacters; break;
354cdf0e10cSrcweir case UPH_IS_USE_DICTIONARY_LIST : pbResVal = &bResIsUseDictionaryList; break;
355cdf0e10cSrcweir case UPH_IS_SPELL_UPPER_CASE : pbResVal = &bResIsSpellUpperCase; break;
356cdf0e10cSrcweir case UPH_IS_SPELL_WITH_DIGITS : pbResVal = &bResIsSpellWithDigits; break;
357cdf0e10cSrcweir case UPH_IS_SPELL_CAPITALIZATION : pbResVal = &bResIsSpellCapitalization; break;
358cdf0e10cSrcweir default:
359cdf0e10cSrcweir DBG_ERROR( "unknown property" );
360cdf0e10cSrcweir }
361cdf0e10cSrcweir if (pbResVal)
362cdf0e10cSrcweir pVal[i].Value >>= *pbResVal;
363cdf0e10cSrcweir }
364cdf0e10cSrcweir }
365cdf0e10cSrcweir }
366cdf0e10cSrcweir
367cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
368cdf0e10cSrcweir
369