Update ooo320-m1
[ooovba.git] / linguistic / source / iprcache.cxx
blobf5d050b30a2f01df5b7ae1c3428fe2f2dbc7a8a5
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: iprcache.cxx,v $
10 * $Revision: 1.9 $
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 <string.h>
36 #include "iprcache.hxx"
37 #include "misc.hxx"
39 #include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp>
40 #include <tools/debug.hxx>
41 #include <osl/mutex.hxx>
43 //#define IPR_DEF_CACHE_SIZE 503
44 #define IPR_DEF_CACHE_MAX 375
45 #define IPR_DEF_CACHE_MAXINPUT 200
47 #ifdef DBG_STATISTIC
48 #include <tools/stream.hxx>
50 //#define IPR_CACHE_SIZE nTblSize
51 #define IPR_CACHE_MAX nMax
52 #define IPR_CACHE_MAXINPUT nMaxInput
54 #else
56 //#define IPR_CACHE_SIZE IPR_DEF_CACHE_SIZE
57 #define IPR_CACHE_MAX IPR_DEF_CACHE_MAX
58 #define IPR_CACHE_MAXINPUT IPR_DEF_CACHE_MAXINPUT
60 #endif
61 #include <unotools/processfactory.hxx>
63 #include <lngprops.hxx>
65 using namespace utl;
66 using namespace osl;
67 using namespace rtl;
68 using namespace com::sun::star;
69 using namespace com::sun::star::beans;
70 using namespace com::sun::star::lang;
71 using namespace com::sun::star::uno;
72 using namespace com::sun::star::linguistic2;
75 namespace linguistic
78 ///////////////////////////////////////////////////////////////////////////
80 #define NUM_FLUSH_PROPS 6
82 static const struct
84 const char *pPropName;
85 INT32 nPropHdl;
86 } aFlushProperties[ NUM_FLUSH_PROPS ] =
88 { UPN_IS_USE_DICTIONARY_LIST, UPH_IS_USE_DICTIONARY_LIST },
89 { UPN_IS_IGNORE_CONTROL_CHARACTERS, UPH_IS_IGNORE_CONTROL_CHARACTERS },
90 { UPN_IS_SPELL_UPPER_CASE, UPH_IS_SPELL_UPPER_CASE },
91 { UPN_IS_SPELL_WITH_DIGITS, UPH_IS_SPELL_WITH_DIGITS },
92 { UPN_IS_SPELL_CAPITALIZATION, UPH_IS_SPELL_CAPITALIZATION }
96 static void lcl_AddAsPropertyChangeListener(
97 Reference< XPropertyChangeListener > xListener,
98 Reference< XPropertySet > &rPropSet )
100 if (xListener.is() && rPropSet.is())
102 for (int i = 0; i < NUM_FLUSH_PROPS; ++i)
104 rPropSet->addPropertyChangeListener(
105 A2OU(aFlushProperties[i].pPropName), xListener );
111 static void lcl_RemoveAsPropertyChangeListener(
112 Reference< XPropertyChangeListener > xListener,
113 Reference< XPropertySet > &rPropSet )
115 if (xListener.is() && rPropSet.is())
117 for (int i = 0; i < NUM_FLUSH_PROPS; ++i)
119 rPropSet->removePropertyChangeListener(
120 A2OU(aFlushProperties[i].pPropName), xListener );
126 static BOOL lcl_IsFlushProperty( INT32 nHandle )
128 int i;
129 for (i = 0; i < NUM_FLUSH_PROPS; ++i)
131 if (nHandle == aFlushProperties[i].nPropHdl)
132 break;
134 return i < NUM_FLUSH_PROPS;
138 FlushListener::FlushListener( Flushable *pFO )
140 SetFlushObj( pFO );
144 FlushListener::~FlushListener()
149 void FlushListener::SetDicList( Reference<XDictionaryList> &rDL )
151 MutexGuard aGuard( GetLinguMutex() );
153 if (xDicList != rDL)
155 if (xDicList.is())
156 xDicList->removeDictionaryListEventListener( this );
158 xDicList = rDL;
159 if (xDicList.is())
160 xDicList->addDictionaryListEventListener( this, FALSE );
165 void FlushListener::SetPropSet( Reference< XPropertySet > &rPS )
167 MutexGuard aGuard( GetLinguMutex() );
169 if (xPropSet != rPS)
171 if (xPropSet.is())
172 lcl_RemoveAsPropertyChangeListener( this, xPropSet );
174 xPropSet = rPS;
175 if (xPropSet.is())
176 lcl_AddAsPropertyChangeListener( this, xPropSet );
181 void SAL_CALL FlushListener::disposing( const EventObject& rSource )
182 throw(RuntimeException)
184 MutexGuard aGuard( GetLinguMutex() );
186 if (xDicList.is() && rSource.Source == xDicList)
188 xDicList->removeDictionaryListEventListener( this );
189 xDicList = NULL; //! release reference
191 if (xPropSet.is() && rSource.Source == xPropSet)
193 lcl_RemoveAsPropertyChangeListener( this, xPropSet );
194 xPropSet = NULL; //! release reference
199 void SAL_CALL FlushListener::processDictionaryListEvent(
200 const DictionaryListEvent& rDicListEvent )
201 throw(RuntimeException)
203 MutexGuard aGuard( GetLinguMutex() );
205 if (rDicListEvent.Source == xDicList)
207 INT16 nEvt = rDicListEvent.nCondensedEvent;
208 INT16 nFlushFlags =
209 DictionaryListEventFlags::ADD_NEG_ENTRY |
210 DictionaryListEventFlags::DEL_POS_ENTRY |
211 DictionaryListEventFlags::ACTIVATE_NEG_DIC |
212 DictionaryListEventFlags::DEACTIVATE_POS_DIC;
213 BOOL bFlush = 0 != (nEvt & nFlushFlags);
215 DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
216 if (bFlush && pFlushObj != NULL)
217 pFlushObj->Flush();
222 void SAL_CALL FlushListener::propertyChange(
223 const PropertyChangeEvent& rEvt )
224 throw(RuntimeException)
226 MutexGuard aGuard( GetLinguMutex() );
228 if (rEvt.Source == xPropSet)
230 BOOL bFlush = lcl_IsFlushProperty( rEvt.PropertyHandle );
232 DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
233 if (bFlush && pFlushObj != NULL)
234 pFlushObj->Flush();
239 ///////////////////////////////////////////////////////////////////////////
241 SpellCache::SpellCache()
243 pFlushLstnr = new FlushListener( this );
244 xFlushLstnr = pFlushLstnr;
245 Reference<XDictionaryList> aDictionaryList(GetDictionaryList());
246 pFlushLstnr->SetDicList( aDictionaryList ); //! after reference is established
247 Reference<XPropertySet> aPropertySet(GetLinguProperties());
248 pFlushLstnr->SetPropSet( aPropertySet ); //! after reference is established
251 SpellCache::~SpellCache()
253 Reference<XDictionaryList> aEmptyList;
254 Reference<XPropertySet> aEmptySet;
255 pFlushLstnr->SetDicList( aEmptyList );
256 pFlushLstnr->SetPropSet( aEmptySet );
259 void SpellCache::Flush()
261 MutexGuard aGuard( GetLinguMutex() );
262 // clear word list
263 LangWordList_t aEmpty;
264 aWordLists.swap( aEmpty );
267 bool SpellCache::CheckWord( const OUString& rWord, LanguageType nLang )
269 MutexGuard aGuard( GetLinguMutex() );
270 WordList_t &rList = aWordLists[ nLang ];
271 const WordList_t::const_iterator aIt = rList.find( rWord );
272 return aIt != rList.end();
275 void SpellCache::AddWord( const OUString& rWord, LanguageType nLang )
277 MutexGuard aGuard( GetLinguMutex() );
278 WordList_t & rList = aWordLists[ nLang ];
279 // occasional clean-up...
280 if (rList.size() > 500)
281 rList.clear();
282 rList.insert( rWord );
284 ///////////////////////////////////////////////////////////////////////////
286 } // namespace linguistic