xref: /aoo42x/main/linguistic/source/iprcache.cxx (revision 3b8558fd)
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 <string.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "iprcache.hxx"
30cdf0e10cSrcweir #include "linguistic/misc.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp>
33cdf0e10cSrcweir #include <tools/debug.hxx>
34cdf0e10cSrcweir #include <osl/mutex.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir //#define IPR_DEF_CACHE_SIZE		503
37cdf0e10cSrcweir #define IPR_DEF_CACHE_MAX		375
38cdf0e10cSrcweir #define IPR_DEF_CACHE_MAXINPUT	200
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #ifdef DBG_STATISTIC
41cdf0e10cSrcweir #include <tools/stream.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir //#define IPR_CACHE_SIZE 		nTblSize
44cdf0e10cSrcweir #define IPR_CACHE_MAX 		nMax
45cdf0e10cSrcweir #define IPR_CACHE_MAXINPUT 	nMaxInput
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #else
48cdf0e10cSrcweir 
49cdf0e10cSrcweir //#define IPR_CACHE_SIZE		IPR_DEF_CACHE_SIZE
50cdf0e10cSrcweir #define IPR_CACHE_MAX		IPR_DEF_CACHE_MAX
51cdf0e10cSrcweir #define IPR_CACHE_MAXINPUT	IPR_DEF_CACHE_MAXINPUT
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #endif
54cdf0e10cSrcweir #include <unotools/processfactory.hxx>
55cdf0e10cSrcweir 
56cdf0e10cSrcweir #include <linguistic/lngprops.hxx>
57cdf0e10cSrcweir 
58cdf0e10cSrcweir using namespace utl;
59cdf0e10cSrcweir using namespace osl;
60cdf0e10cSrcweir using namespace rtl;
61cdf0e10cSrcweir using namespace com::sun::star;
62cdf0e10cSrcweir using namespace com::sun::star::beans;
63cdf0e10cSrcweir using namespace com::sun::star::lang;
64cdf0e10cSrcweir using namespace com::sun::star::uno;
65cdf0e10cSrcweir using namespace com::sun::star::linguistic2;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 
68cdf0e10cSrcweir namespace linguistic
69cdf0e10cSrcweir {
70cdf0e10cSrcweir 
71cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
72cdf0e10cSrcweir 
73cdf0e10cSrcweir #define NUM_FLUSH_PROPS		6
74cdf0e10cSrcweir 
75cdf0e10cSrcweir static const struct
76cdf0e10cSrcweir {
77cdf0e10cSrcweir 	const char *pPropName;
78cdf0e10cSrcweir 	sal_Int32		nPropHdl;
79cdf0e10cSrcweir } aFlushProperties[ NUM_FLUSH_PROPS ] =
80cdf0e10cSrcweir {
81cdf0e10cSrcweir     { UPN_IS_USE_DICTIONARY_LIST,         UPH_IS_USE_DICTIONARY_LIST },
82cdf0e10cSrcweir     { UPN_IS_IGNORE_CONTROL_CHARACTERS,   UPH_IS_IGNORE_CONTROL_CHARACTERS },
83cdf0e10cSrcweir     { UPN_IS_SPELL_UPPER_CASE,            UPH_IS_SPELL_UPPER_CASE },
84cdf0e10cSrcweir     { UPN_IS_SPELL_WITH_DIGITS,           UPH_IS_SPELL_WITH_DIGITS },
85cdf0e10cSrcweir     { UPN_IS_SPELL_CAPITALIZATION,        UPH_IS_SPELL_CAPITALIZATION }
86cdf0e10cSrcweir };
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 
lcl_AddAsPropertyChangeListener(Reference<XPropertyChangeListener> xListener,Reference<XPropertySet> & rPropSet)89cdf0e10cSrcweir static void lcl_AddAsPropertyChangeListener(
90cdf0e10cSrcweir 		Reference< XPropertyChangeListener > xListener,
91cdf0e10cSrcweir 		Reference< XPropertySet > &rPropSet )
92cdf0e10cSrcweir {
93cdf0e10cSrcweir 	if (xListener.is() && rPropSet.is())
94cdf0e10cSrcweir 	{
95cdf0e10cSrcweir 		for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
96cdf0e10cSrcweir 		{
97cdf0e10cSrcweir 			rPropSet->addPropertyChangeListener(
98cdf0e10cSrcweir 					A2OU(aFlushProperties[i].pPropName), xListener );
99cdf0e10cSrcweir 		}
100cdf0e10cSrcweir 	}
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 
lcl_RemoveAsPropertyChangeListener(Reference<XPropertyChangeListener> xListener,Reference<XPropertySet> & rPropSet)104cdf0e10cSrcweir static void lcl_RemoveAsPropertyChangeListener(
105cdf0e10cSrcweir 		Reference< XPropertyChangeListener > xListener,
106cdf0e10cSrcweir 		Reference< XPropertySet > &rPropSet )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir 	if (xListener.is() && rPropSet.is())
109cdf0e10cSrcweir 	{
110cdf0e10cSrcweir 		for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
111cdf0e10cSrcweir 		{
112cdf0e10cSrcweir 			rPropSet->removePropertyChangeListener(
113cdf0e10cSrcweir 					A2OU(aFlushProperties[i].pPropName), xListener );
114cdf0e10cSrcweir 		}
115cdf0e10cSrcweir 	}
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 
lcl_IsFlushProperty(sal_Int32 nHandle)119cdf0e10cSrcweir static sal_Bool lcl_IsFlushProperty( sal_Int32 nHandle )
120cdf0e10cSrcweir {
121cdf0e10cSrcweir 	int i;
122cdf0e10cSrcweir 	for (i = 0;  i < NUM_FLUSH_PROPS;  ++i)
123cdf0e10cSrcweir 	{
124cdf0e10cSrcweir 		if (nHandle == aFlushProperties[i].nPropHdl)
125cdf0e10cSrcweir 			break;
126cdf0e10cSrcweir 	}
127cdf0e10cSrcweir 	return i < NUM_FLUSH_PROPS;
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 
FlushListener(Flushable * pFO)131cdf0e10cSrcweir FlushListener::FlushListener( Flushable *pFO )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir 	SetFlushObj( pFO );
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 
~FlushListener()137cdf0e10cSrcweir FlushListener::~FlushListener()
138cdf0e10cSrcweir {
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 
SetDicList(Reference<XDictionaryList> & rDL)142cdf0e10cSrcweir void FlushListener::SetDicList(	Reference<XDictionaryList> &rDL )
143cdf0e10cSrcweir {
144cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 	if (xDicList != rDL)
147cdf0e10cSrcweir 	{
148cdf0e10cSrcweir 		if (xDicList.is())
149cdf0e10cSrcweir 			xDicList->removeDictionaryListEventListener( this );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 		xDicList = rDL;
152cdf0e10cSrcweir 		if (xDicList.is())
153cdf0e10cSrcweir 			xDicList->addDictionaryListEventListener( this, sal_False );
154cdf0e10cSrcweir 	}
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 
SetPropSet(Reference<XPropertySet> & rPS)158cdf0e10cSrcweir void FlushListener::SetPropSet( Reference< XPropertySet > &rPS )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	if (xPropSet != rPS)
163cdf0e10cSrcweir 	{
164cdf0e10cSrcweir 		if (xPropSet.is())
165cdf0e10cSrcweir 			lcl_RemoveAsPropertyChangeListener( this, xPropSet );
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 		xPropSet = rPS;
168cdf0e10cSrcweir 		if (xPropSet.is())
169cdf0e10cSrcweir 			lcl_AddAsPropertyChangeListener( this, xPropSet );
170cdf0e10cSrcweir 	}
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 
disposing(const EventObject & rSource)174cdf0e10cSrcweir void SAL_CALL FlushListener::disposing( const EventObject& rSource )
175cdf0e10cSrcweir 		throw(RuntimeException)
176cdf0e10cSrcweir {
177cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 	if (xDicList.is()  &&  rSource.Source == xDicList)
180cdf0e10cSrcweir 	{
181cdf0e10cSrcweir 		xDicList->removeDictionaryListEventListener( this );
182cdf0e10cSrcweir 		xDicList = NULL;	//! release reference
183cdf0e10cSrcweir 	}
184cdf0e10cSrcweir 	if (xPropSet.is()  &&  rSource.Source == xPropSet)
185cdf0e10cSrcweir 	{
186cdf0e10cSrcweir 		lcl_RemoveAsPropertyChangeListener( this, xPropSet );
187cdf0e10cSrcweir 		xPropSet = NULL;	//! release reference
188cdf0e10cSrcweir 	}
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 
processDictionaryListEvent(const DictionaryListEvent & rDicListEvent)192cdf0e10cSrcweir void SAL_CALL FlushListener::processDictionaryListEvent(
193cdf0e10cSrcweir 			const DictionaryListEvent& rDicListEvent )
194cdf0e10cSrcweir 		throw(RuntimeException)
195cdf0e10cSrcweir {
196cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 	if (rDicListEvent.Source == xDicList)
199cdf0e10cSrcweir 	{
200cdf0e10cSrcweir 		sal_Int16 nEvt = rDicListEvent.nCondensedEvent;
201cdf0e10cSrcweir 		sal_Int16 nFlushFlags =
202cdf0e10cSrcweir 				DictionaryListEventFlags::ADD_NEG_ENTRY		|
203cdf0e10cSrcweir 				DictionaryListEventFlags::DEL_POS_ENTRY		|
204cdf0e10cSrcweir 				DictionaryListEventFlags::ACTIVATE_NEG_DIC	|
205cdf0e10cSrcweir 				DictionaryListEventFlags::DEACTIVATE_POS_DIC;
206cdf0e10cSrcweir 		sal_Bool bFlush	= 0 != (nEvt & nFlushFlags);
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 		DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
209cdf0e10cSrcweir 		if (bFlush && pFlushObj != NULL)
210cdf0e10cSrcweir 			pFlushObj->Flush();
211cdf0e10cSrcweir 	}
212cdf0e10cSrcweir }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 
propertyChange(const PropertyChangeEvent & rEvt)215cdf0e10cSrcweir void SAL_CALL FlushListener::propertyChange(
216cdf0e10cSrcweir 			const PropertyChangeEvent& rEvt )
217cdf0e10cSrcweir 		throw(RuntimeException)
218cdf0e10cSrcweir {
219cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 	if (rEvt.Source == xPropSet)
222cdf0e10cSrcweir 	{
223cdf0e10cSrcweir 		sal_Bool bFlush	= lcl_IsFlushProperty( rEvt.PropertyHandle );
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 		DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
226cdf0e10cSrcweir 		if (bFlush && pFlushObj != NULL)
227cdf0e10cSrcweir 			pFlushObj->Flush();
228cdf0e10cSrcweir 	}
229cdf0e10cSrcweir }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir 
232cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
233cdf0e10cSrcweir 
SpellCache()234cdf0e10cSrcweir SpellCache::SpellCache()
235cdf0e10cSrcweir {
236cdf0e10cSrcweir 	pFlushLstnr = new FlushListener( this );
237cdf0e10cSrcweir 	xFlushLstnr = pFlushLstnr;
238cdf0e10cSrcweir     Reference<XDictionaryList> aDictionaryList(GetDictionaryList());
239cdf0e10cSrcweir 	pFlushLstnr->SetDicList( aDictionaryList );	//! after reference is established
240cdf0e10cSrcweir     Reference<XPropertySet> aPropertySet(GetLinguProperties());
241cdf0e10cSrcweir 	pFlushLstnr->SetPropSet( aPropertySet );	//! after reference is established
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
~SpellCache()244cdf0e10cSrcweir SpellCache::~SpellCache()
245cdf0e10cSrcweir {
246cdf0e10cSrcweir     Reference<XDictionaryList>  aEmptyList;
247cdf0e10cSrcweir     Reference<XPropertySet>     aEmptySet;
248cdf0e10cSrcweir     pFlushLstnr->SetDicList( aEmptyList );
249cdf0e10cSrcweir     pFlushLstnr->SetPropSet( aEmptySet );
250cdf0e10cSrcweir }
251cdf0e10cSrcweir 
Flush()252cdf0e10cSrcweir void SpellCache::Flush()
253cdf0e10cSrcweir {
254cdf0e10cSrcweir     MutexGuard	aGuard( GetLinguMutex() );
255cdf0e10cSrcweir     // clear word list
256cdf0e10cSrcweir     LangWordList_t aEmpty;
257cdf0e10cSrcweir     aWordLists.swap( aEmpty );
258cdf0e10cSrcweir }
259cdf0e10cSrcweir 
CheckWord(const OUString & rWord,LanguageType nLang)260cdf0e10cSrcweir bool SpellCache::CheckWord( const OUString& rWord, LanguageType nLang )
261cdf0e10cSrcweir {
262cdf0e10cSrcweir     MutexGuard	aGuard( GetLinguMutex() );
263cdf0e10cSrcweir     WordList_t &rList = aWordLists[ nLang ];
264cdf0e10cSrcweir     const WordList_t::const_iterator aIt = rList.find( rWord );
265cdf0e10cSrcweir     return aIt != rList.end();
266cdf0e10cSrcweir }
267cdf0e10cSrcweir 
AddWord(const OUString & rWord,LanguageType nLang)268cdf0e10cSrcweir void SpellCache::AddWord( const OUString& rWord, LanguageType nLang )
269cdf0e10cSrcweir {
270cdf0e10cSrcweir     MutexGuard	aGuard( GetLinguMutex() );
271cdf0e10cSrcweir     WordList_t & rList = aWordLists[ nLang ];
272cdf0e10cSrcweir     // occasional clean-up...
273cdf0e10cSrcweir     if (rList.size() > 500)
274cdf0e10cSrcweir         rList.clear();
275cdf0e10cSrcweir     rList.insert( rWord );
276cdf0e10cSrcweir }
277cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
278cdf0e10cSrcweir 
279cdf0e10cSrcweir }	// namespace linguistic
280cdf0e10cSrcweir 
281