xref: /AOO41X/main/linguistic/source/iprcache.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_linguistic.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <string.h>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "iprcache.hxx"
34*cdf0e10cSrcweir #include "linguistic/misc.hxx"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp>
37*cdf0e10cSrcweir #include <tools/debug.hxx>
38*cdf0e10cSrcweir #include <osl/mutex.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir //#define IPR_DEF_CACHE_SIZE		503
41*cdf0e10cSrcweir #define IPR_DEF_CACHE_MAX		375
42*cdf0e10cSrcweir #define IPR_DEF_CACHE_MAXINPUT	200
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #ifdef DBG_STATISTIC
45*cdf0e10cSrcweir #include <tools/stream.hxx>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir //#define IPR_CACHE_SIZE 		nTblSize
48*cdf0e10cSrcweir #define IPR_CACHE_MAX 		nMax
49*cdf0e10cSrcweir #define IPR_CACHE_MAXINPUT 	nMaxInput
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir #else
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir //#define IPR_CACHE_SIZE		IPR_DEF_CACHE_SIZE
54*cdf0e10cSrcweir #define IPR_CACHE_MAX		IPR_DEF_CACHE_MAX
55*cdf0e10cSrcweir #define IPR_CACHE_MAXINPUT	IPR_DEF_CACHE_MAXINPUT
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #endif
58*cdf0e10cSrcweir #include <unotools/processfactory.hxx>
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #include <linguistic/lngprops.hxx>
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir using namespace utl;
63*cdf0e10cSrcweir using namespace osl;
64*cdf0e10cSrcweir using namespace rtl;
65*cdf0e10cSrcweir using namespace com::sun::star;
66*cdf0e10cSrcweir using namespace com::sun::star::beans;
67*cdf0e10cSrcweir using namespace com::sun::star::lang;
68*cdf0e10cSrcweir using namespace com::sun::star::uno;
69*cdf0e10cSrcweir using namespace com::sun::star::linguistic2;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir namespace linguistic
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir #define NUM_FLUSH_PROPS		6
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir static const struct
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir 	const char *pPropName;
82*cdf0e10cSrcweir 	sal_Int32		nPropHdl;
83*cdf0e10cSrcweir } aFlushProperties[ NUM_FLUSH_PROPS ] =
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir     { UPN_IS_USE_DICTIONARY_LIST,         UPH_IS_USE_DICTIONARY_LIST },
86*cdf0e10cSrcweir     { UPN_IS_IGNORE_CONTROL_CHARACTERS,   UPH_IS_IGNORE_CONTROL_CHARACTERS },
87*cdf0e10cSrcweir     { UPN_IS_SPELL_UPPER_CASE,            UPH_IS_SPELL_UPPER_CASE },
88*cdf0e10cSrcweir     { UPN_IS_SPELL_WITH_DIGITS,           UPH_IS_SPELL_WITH_DIGITS },
89*cdf0e10cSrcweir     { UPN_IS_SPELL_CAPITALIZATION,        UPH_IS_SPELL_CAPITALIZATION }
90*cdf0e10cSrcweir };
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir static void lcl_AddAsPropertyChangeListener(
94*cdf0e10cSrcweir 		Reference< XPropertyChangeListener > xListener,
95*cdf0e10cSrcweir 		Reference< XPropertySet > &rPropSet )
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	if (xListener.is() && rPropSet.is())
98*cdf0e10cSrcweir 	{
99*cdf0e10cSrcweir 		for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
100*cdf0e10cSrcweir 		{
101*cdf0e10cSrcweir 			rPropSet->addPropertyChangeListener(
102*cdf0e10cSrcweir 					A2OU(aFlushProperties[i].pPropName), xListener );
103*cdf0e10cSrcweir 		}
104*cdf0e10cSrcweir 	}
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir static void lcl_RemoveAsPropertyChangeListener(
109*cdf0e10cSrcweir 		Reference< XPropertyChangeListener > xListener,
110*cdf0e10cSrcweir 		Reference< XPropertySet > &rPropSet )
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir 	if (xListener.is() && rPropSet.is())
113*cdf0e10cSrcweir 	{
114*cdf0e10cSrcweir 		for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
115*cdf0e10cSrcweir 		{
116*cdf0e10cSrcweir 			rPropSet->removePropertyChangeListener(
117*cdf0e10cSrcweir 					A2OU(aFlushProperties[i].pPropName), xListener );
118*cdf0e10cSrcweir 		}
119*cdf0e10cSrcweir 	}
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir static sal_Bool lcl_IsFlushProperty( sal_Int32 nHandle )
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir 	int i;
126*cdf0e10cSrcweir 	for (i = 0;  i < NUM_FLUSH_PROPS;  ++i)
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		if (nHandle == aFlushProperties[i].nPropHdl)
129*cdf0e10cSrcweir 			break;
130*cdf0e10cSrcweir 	}
131*cdf0e10cSrcweir 	return i < NUM_FLUSH_PROPS;
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir FlushListener::FlushListener( Flushable *pFO )
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir 	SetFlushObj( pFO );
138*cdf0e10cSrcweir }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir FlushListener::~FlushListener()
142*cdf0e10cSrcweir {
143*cdf0e10cSrcweir }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir void FlushListener::SetDicList(	Reference<XDictionaryList> &rDL )
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 	if (xDicList != rDL)
151*cdf0e10cSrcweir 	{
152*cdf0e10cSrcweir 		if (xDicList.is())
153*cdf0e10cSrcweir 			xDicList->removeDictionaryListEventListener( this );
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 		xDicList = rDL;
156*cdf0e10cSrcweir 		if (xDicList.is())
157*cdf0e10cSrcweir 			xDicList->addDictionaryListEventListener( this, sal_False );
158*cdf0e10cSrcweir 	}
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir void FlushListener::SetPropSet( Reference< XPropertySet > &rPS )
163*cdf0e10cSrcweir {
164*cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	if (xPropSet != rPS)
167*cdf0e10cSrcweir 	{
168*cdf0e10cSrcweir 		if (xPropSet.is())
169*cdf0e10cSrcweir 			lcl_RemoveAsPropertyChangeListener( this, xPropSet );
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 		xPropSet = rPS;
172*cdf0e10cSrcweir 		if (xPropSet.is())
173*cdf0e10cSrcweir 			lcl_AddAsPropertyChangeListener( this, xPropSet );
174*cdf0e10cSrcweir 	}
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir void SAL_CALL FlushListener::disposing( const EventObject& rSource )
179*cdf0e10cSrcweir 		throw(RuntimeException)
180*cdf0e10cSrcweir {
181*cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 	if (xDicList.is()  &&  rSource.Source == xDicList)
184*cdf0e10cSrcweir 	{
185*cdf0e10cSrcweir 		xDicList->removeDictionaryListEventListener( this );
186*cdf0e10cSrcweir 		xDicList = NULL;	//! release reference
187*cdf0e10cSrcweir 	}
188*cdf0e10cSrcweir 	if (xPropSet.is()  &&  rSource.Source == xPropSet)
189*cdf0e10cSrcweir 	{
190*cdf0e10cSrcweir 		lcl_RemoveAsPropertyChangeListener( this, xPropSet );
191*cdf0e10cSrcweir 		xPropSet = NULL;	//! release reference
192*cdf0e10cSrcweir 	}
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir void SAL_CALL FlushListener::processDictionaryListEvent(
197*cdf0e10cSrcweir 			const DictionaryListEvent& rDicListEvent )
198*cdf0e10cSrcweir 		throw(RuntimeException)
199*cdf0e10cSrcweir {
200*cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 	if (rDicListEvent.Source == xDicList)
203*cdf0e10cSrcweir 	{
204*cdf0e10cSrcweir 		sal_Int16 nEvt = rDicListEvent.nCondensedEvent;
205*cdf0e10cSrcweir 		sal_Int16 nFlushFlags =
206*cdf0e10cSrcweir 				DictionaryListEventFlags::ADD_NEG_ENTRY		|
207*cdf0e10cSrcweir 				DictionaryListEventFlags::DEL_POS_ENTRY		|
208*cdf0e10cSrcweir 				DictionaryListEventFlags::ACTIVATE_NEG_DIC	|
209*cdf0e10cSrcweir 				DictionaryListEventFlags::DEACTIVATE_POS_DIC;
210*cdf0e10cSrcweir 		sal_Bool bFlush	= 0 != (nEvt & nFlushFlags);
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 		DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
213*cdf0e10cSrcweir 		if (bFlush && pFlushObj != NULL)
214*cdf0e10cSrcweir 			pFlushObj->Flush();
215*cdf0e10cSrcweir 	}
216*cdf0e10cSrcweir }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir void SAL_CALL FlushListener::propertyChange(
220*cdf0e10cSrcweir 			const PropertyChangeEvent& rEvt )
221*cdf0e10cSrcweir 		throw(RuntimeException)
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 	if (rEvt.Source == xPropSet)
226*cdf0e10cSrcweir 	{
227*cdf0e10cSrcweir 		sal_Bool bFlush	= lcl_IsFlushProperty( rEvt.PropertyHandle );
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 		DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
230*cdf0e10cSrcweir 		if (bFlush && pFlushObj != NULL)
231*cdf0e10cSrcweir 			pFlushObj->Flush();
232*cdf0e10cSrcweir 	}
233*cdf0e10cSrcweir }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir SpellCache::SpellCache()
239*cdf0e10cSrcweir {
240*cdf0e10cSrcweir 	pFlushLstnr = new FlushListener( this );
241*cdf0e10cSrcweir 	xFlushLstnr = pFlushLstnr;
242*cdf0e10cSrcweir     Reference<XDictionaryList> aDictionaryList(GetDictionaryList());
243*cdf0e10cSrcweir 	pFlushLstnr->SetDicList( aDictionaryList );	//! after reference is established
244*cdf0e10cSrcweir     Reference<XPropertySet> aPropertySet(GetLinguProperties());
245*cdf0e10cSrcweir 	pFlushLstnr->SetPropSet( aPropertySet );	//! after reference is established
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir SpellCache::~SpellCache()
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir     Reference<XDictionaryList>  aEmptyList;
251*cdf0e10cSrcweir     Reference<XPropertySet>     aEmptySet;
252*cdf0e10cSrcweir     pFlushLstnr->SetDicList( aEmptyList );
253*cdf0e10cSrcweir     pFlushLstnr->SetPropSet( aEmptySet );
254*cdf0e10cSrcweir }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir void SpellCache::Flush()
257*cdf0e10cSrcweir {
258*cdf0e10cSrcweir     MutexGuard	aGuard( GetLinguMutex() );
259*cdf0e10cSrcweir     // clear word list
260*cdf0e10cSrcweir     LangWordList_t aEmpty;
261*cdf0e10cSrcweir     aWordLists.swap( aEmpty );
262*cdf0e10cSrcweir }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir bool SpellCache::CheckWord( const OUString& rWord, LanguageType nLang )
265*cdf0e10cSrcweir {
266*cdf0e10cSrcweir     MutexGuard	aGuard( GetLinguMutex() );
267*cdf0e10cSrcweir     WordList_t &rList = aWordLists[ nLang ];
268*cdf0e10cSrcweir     const WordList_t::const_iterator aIt = rList.find( rWord );
269*cdf0e10cSrcweir     return aIt != rList.end();
270*cdf0e10cSrcweir }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir void SpellCache::AddWord( const OUString& rWord, LanguageType nLang )
273*cdf0e10cSrcweir {
274*cdf0e10cSrcweir     MutexGuard	aGuard( GetLinguMutex() );
275*cdf0e10cSrcweir     WordList_t & rList = aWordLists[ nLang ];
276*cdf0e10cSrcweir     // occasional clean-up...
277*cdf0e10cSrcweir     if (rList.size() > 500)
278*cdf0e10cSrcweir         rList.clear();
279*cdf0e10cSrcweir     rList.insert( rWord );
280*cdf0e10cSrcweir }
281*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir }	// namespace linguistic
284*cdf0e10cSrcweir 
285