xref: /trunk/main/linguistic/source/iprcache.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_linguistic.hxx"
26 
27 #include <string.h>
28 
29 #include "iprcache.hxx"
30 #include "linguistic/misc.hxx"
31 
32 #include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp>
33 #include <tools/debug.hxx>
34 #include <osl/mutex.hxx>
35 
36 //#define IPR_DEF_CACHE_SIZE        503
37 #define IPR_DEF_CACHE_MAX       375
38 #define IPR_DEF_CACHE_MAXINPUT  200
39 
40 #ifdef DBG_STATISTIC
41 #include <tools/stream.hxx>
42 
43 //#define IPR_CACHE_SIZE        nTblSize
44 #define IPR_CACHE_MAX       nMax
45 #define IPR_CACHE_MAXINPUT  nMaxInput
46 
47 #else
48 
49 //#define IPR_CACHE_SIZE        IPR_DEF_CACHE_SIZE
50 #define IPR_CACHE_MAX       IPR_DEF_CACHE_MAX
51 #define IPR_CACHE_MAXINPUT  IPR_DEF_CACHE_MAXINPUT
52 
53 #endif
54 #include <unotools/processfactory.hxx>
55 
56 #include <linguistic/lngprops.hxx>
57 
58 using namespace utl;
59 using namespace osl;
60 using namespace rtl;
61 using namespace com::sun::star;
62 using namespace com::sun::star::beans;
63 using namespace com::sun::star::lang;
64 using namespace com::sun::star::uno;
65 using namespace com::sun::star::linguistic2;
66 
67 
68 namespace linguistic
69 {
70 
71 ///////////////////////////////////////////////////////////////////////////
72 
73 #define NUM_FLUSH_PROPS     6
74 
75 static const struct
76 {
77     const char *pPropName;
78     sal_Int32       nPropHdl;
79 } aFlushProperties[ NUM_FLUSH_PROPS ] =
80 {
81     { UPN_IS_USE_DICTIONARY_LIST,         UPH_IS_USE_DICTIONARY_LIST },
82     { UPN_IS_IGNORE_CONTROL_CHARACTERS,   UPH_IS_IGNORE_CONTROL_CHARACTERS },
83     { UPN_IS_SPELL_UPPER_CASE,            UPH_IS_SPELL_UPPER_CASE },
84     { UPN_IS_SPELL_WITH_DIGITS,           UPH_IS_SPELL_WITH_DIGITS },
85     { UPN_IS_SPELL_CAPITALIZATION,        UPH_IS_SPELL_CAPITALIZATION }
86 };
87 
88 
lcl_AddAsPropertyChangeListener(Reference<XPropertyChangeListener> xListener,Reference<XPropertySet> & rPropSet)89 static void lcl_AddAsPropertyChangeListener(
90         Reference< XPropertyChangeListener > xListener,
91         Reference< XPropertySet > &rPropSet )
92 {
93     if (xListener.is() && rPropSet.is())
94     {
95         for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
96         {
97             rPropSet->addPropertyChangeListener(
98                     A2OU(aFlushProperties[i].pPropName), xListener );
99         }
100     }
101 }
102 
103 
lcl_RemoveAsPropertyChangeListener(Reference<XPropertyChangeListener> xListener,Reference<XPropertySet> & rPropSet)104 static void lcl_RemoveAsPropertyChangeListener(
105         Reference< XPropertyChangeListener > xListener,
106         Reference< XPropertySet > &rPropSet )
107 {
108     if (xListener.is() && rPropSet.is())
109     {
110         for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
111         {
112             rPropSet->removePropertyChangeListener(
113                     A2OU(aFlushProperties[i].pPropName), xListener );
114         }
115     }
116 }
117 
118 
lcl_IsFlushProperty(sal_Int32 nHandle)119 static sal_Bool lcl_IsFlushProperty( sal_Int32 nHandle )
120 {
121     int i;
122     for (i = 0;  i < NUM_FLUSH_PROPS;  ++i)
123     {
124         if (nHandle == aFlushProperties[i].nPropHdl)
125             break;
126     }
127     return i < NUM_FLUSH_PROPS;
128 }
129 
130 
FlushListener(Flushable * pFO)131 FlushListener::FlushListener( Flushable *pFO )
132 {
133     SetFlushObj( pFO );
134 }
135 
136 
~FlushListener()137 FlushListener::~FlushListener()
138 {
139 }
140 
141 
SetDicList(Reference<XDictionaryList> & rDL)142 void FlushListener::SetDicList( Reference<XDictionaryList> &rDL )
143 {
144     MutexGuard  aGuard( GetLinguMutex() );
145 
146     if (xDicList != rDL)
147     {
148         if (xDicList.is())
149             xDicList->removeDictionaryListEventListener( this );
150 
151         xDicList = rDL;
152         if (xDicList.is())
153             xDicList->addDictionaryListEventListener( this, sal_False );
154     }
155 }
156 
157 
SetPropSet(Reference<XPropertySet> & rPS)158 void FlushListener::SetPropSet( Reference< XPropertySet > &rPS )
159 {
160     MutexGuard  aGuard( GetLinguMutex() );
161 
162     if (xPropSet != rPS)
163     {
164         if (xPropSet.is())
165             lcl_RemoveAsPropertyChangeListener( this, xPropSet );
166 
167         xPropSet = rPS;
168         if (xPropSet.is())
169             lcl_AddAsPropertyChangeListener( this, xPropSet );
170     }
171 }
172 
173 
disposing(const EventObject & rSource)174 void SAL_CALL FlushListener::disposing( const EventObject& rSource )
175 {
176     MutexGuard  aGuard( GetLinguMutex() );
177 
178     if (xDicList.is()  &&  rSource.Source == xDicList)
179     {
180         xDicList->removeDictionaryListEventListener( this );
181         xDicList = NULL;    //! release reference
182     }
183     if (xPropSet.is()  &&  rSource.Source == xPropSet)
184     {
185         lcl_RemoveAsPropertyChangeListener( this, xPropSet );
186         xPropSet = NULL;    //! release reference
187     }
188 }
189 
190 
processDictionaryListEvent(const DictionaryListEvent & rDicListEvent)191 void SAL_CALL FlushListener::processDictionaryListEvent(
192             const DictionaryListEvent& rDicListEvent )
193 {
194     MutexGuard  aGuard( GetLinguMutex() );
195 
196     if (rDicListEvent.Source == xDicList)
197     {
198         sal_Int16 nEvt = rDicListEvent.nCondensedEvent;
199         sal_Int16 nFlushFlags =
200                 DictionaryListEventFlags::ADD_NEG_ENTRY     |
201                 DictionaryListEventFlags::DEL_POS_ENTRY     |
202                 DictionaryListEventFlags::ACTIVATE_NEG_DIC  |
203                 DictionaryListEventFlags::DEACTIVATE_POS_DIC;
204         sal_Bool bFlush = 0 != (nEvt & nFlushFlags);
205 
206         DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
207         if (bFlush && pFlushObj != NULL)
208             pFlushObj->Flush();
209     }
210 }
211 
212 
propertyChange(const PropertyChangeEvent & rEvt)213 void SAL_CALL FlushListener::propertyChange(
214             const PropertyChangeEvent& rEvt )
215 {
216     MutexGuard  aGuard( GetLinguMutex() );
217 
218     if (rEvt.Source == xPropSet)
219     {
220         sal_Bool bFlush = lcl_IsFlushProperty( rEvt.PropertyHandle );
221 
222         DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
223         if (bFlush && pFlushObj != NULL)
224             pFlushObj->Flush();
225     }
226 }
227 
228 
229 ///////////////////////////////////////////////////////////////////////////
230 
SpellCache()231 SpellCache::SpellCache()
232 {
233     pFlushLstnr = new FlushListener( this );
234     xFlushLstnr = pFlushLstnr;
235     Reference<XDictionaryList> aDictionaryList(GetDictionaryList());
236     pFlushLstnr->SetDicList( aDictionaryList ); //! after reference is established
237     Reference<XPropertySet> aPropertySet(GetLinguProperties());
238     pFlushLstnr->SetPropSet( aPropertySet );    //! after reference is established
239 }
240 
~SpellCache()241 SpellCache::~SpellCache()
242 {
243     Reference<XDictionaryList>  aEmptyList;
244     Reference<XPropertySet>     aEmptySet;
245     pFlushLstnr->SetDicList( aEmptyList );
246     pFlushLstnr->SetPropSet( aEmptySet );
247 }
248 
Flush()249 void SpellCache::Flush()
250 {
251     MutexGuard  aGuard( GetLinguMutex() );
252     // clear word list
253     LangWordList_t aEmpty;
254     aWordLists.swap( aEmpty );
255 }
256 
CheckWord(const OUString & rWord,LanguageType nLang)257 bool SpellCache::CheckWord( const OUString& rWord, LanguageType nLang )
258 {
259     MutexGuard  aGuard( GetLinguMutex() );
260     WordList_t &rList = aWordLists[ nLang ];
261     const WordList_t::const_iterator aIt = rList.find( rWord );
262     return aIt != rList.end();
263 }
264 
AddWord(const OUString & rWord,LanguageType nLang)265 void SpellCache::AddWord( const OUString& rWord, LanguageType nLang )
266 {
267     MutexGuard  aGuard( GetLinguMutex() );
268     WordList_t & rList = aWordLists[ nLang ];
269     // occasional clean-up...
270     if (rList.size() > 500)
271         rList.clear();
272     rList.insert( rWord );
273 }
274 ///////////////////////////////////////////////////////////////////////////
275 
276 }   // namespace linguistic
277