Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <string.h>
22 :
23 : #include "iprcache.hxx"
24 : #include "linguistic/misc.hxx"
25 :
26 : #include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp>
27 : #include <tools/debug.hxx>
28 : #include <osl/mutex.hxx>
29 : #include <linguistic/lngprops.hxx>
30 :
31 : using namespace utl;
32 : using namespace osl;
33 : using namespace com::sun::star;
34 : using namespace com::sun::star::beans;
35 : using namespace com::sun::star::lang;
36 : using namespace com::sun::star::uno;
37 : using namespace com::sun::star::linguistic2;
38 :
39 :
40 : namespace linguistic
41 : {
42 :
43 :
44 : #define NUM_FLUSH_PROPS 6
45 :
46 : static const struct
47 : {
48 : const char *pPropName;
49 : sal_Int32 nPropHdl;
50 : } aFlushProperties[ NUM_FLUSH_PROPS ] =
51 : {
52 : { UPN_IS_USE_DICTIONARY_LIST, UPH_IS_USE_DICTIONARY_LIST },
53 : { UPN_IS_IGNORE_CONTROL_CHARACTERS, UPH_IS_IGNORE_CONTROL_CHARACTERS },
54 : { UPN_IS_SPELL_UPPER_CASE, UPH_IS_SPELL_UPPER_CASE },
55 : { UPN_IS_SPELL_WITH_DIGITS, UPH_IS_SPELL_WITH_DIGITS },
56 : { UPN_IS_SPELL_CAPITALIZATION, UPH_IS_SPELL_CAPITALIZATION }
57 : };
58 :
59 :
60 27 : static void lcl_AddAsPropertyChangeListener(
61 : Reference< XPropertyChangeListener > xListener,
62 : Reference< XLinguProperties > &rPropSet )
63 : {
64 27 : if (xListener.is() && rPropSet.is())
65 : {
66 189 : for (int i = 0; i < NUM_FLUSH_PROPS; ++i)
67 : {
68 162 : rPropSet->addPropertyChangeListener(
69 162 : OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
70 : }
71 : }
72 27 : }
73 :
74 :
75 27 : static void lcl_RemoveAsPropertyChangeListener(
76 : Reference< XPropertyChangeListener > xListener,
77 : Reference< XLinguProperties > &rPropSet )
78 : {
79 27 : if (xListener.is() && rPropSet.is())
80 : {
81 189 : for (int i = 0; i < NUM_FLUSH_PROPS; ++i)
82 : {
83 162 : rPropSet->removePropertyChangeListener(
84 162 : OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
85 : }
86 : }
87 27 : }
88 :
89 :
90 0 : static bool lcl_IsFlushProperty( sal_Int32 nHandle )
91 : {
92 : int i;
93 0 : for (i = 0; i < NUM_FLUSH_PROPS; ++i)
94 : {
95 0 : if (nHandle == aFlushProperties[i].nPropHdl)
96 0 : break;
97 : }
98 0 : return i < NUM_FLUSH_PROPS;
99 : }
100 :
101 :
102 27 : FlushListener::FlushListener( Flushable *pFO )
103 : {
104 27 : SetFlushObj( pFO );
105 27 : }
106 :
107 :
108 48 : FlushListener::~FlushListener()
109 : {
110 48 : }
111 :
112 :
113 51 : void FlushListener::SetDicList( Reference<XSearchableDictionaryList> &rDL )
114 : {
115 51 : MutexGuard aGuard( GetLinguMutex() );
116 :
117 51 : if (xDicList != rDL)
118 : {
119 51 : if (xDicList.is())
120 24 : xDicList->removeDictionaryListEventListener( this );
121 :
122 51 : xDicList = rDL;
123 51 : if (xDicList.is())
124 27 : xDicList->addDictionaryListEventListener( this, sal_False );
125 51 : }
126 51 : }
127 :
128 :
129 51 : void FlushListener::SetPropSet( Reference< XLinguProperties > &rPS )
130 : {
131 51 : MutexGuard aGuard( GetLinguMutex() );
132 :
133 51 : if (xPropSet != rPS)
134 : {
135 51 : if (xPropSet.is())
136 24 : lcl_RemoveAsPropertyChangeListener( this, xPropSet );
137 :
138 51 : xPropSet = rPS;
139 51 : if (xPropSet.is())
140 27 : lcl_AddAsPropertyChangeListener( this, xPropSet );
141 51 : }
142 51 : }
143 :
144 :
145 18 : void SAL_CALL FlushListener::disposing( const EventObject& rSource )
146 : throw(RuntimeException, std::exception)
147 : {
148 18 : MutexGuard aGuard( GetLinguMutex() );
149 :
150 18 : if (xDicList.is() && rSource.Source == xDicList)
151 : {
152 3 : xDicList->removeDictionaryListEventListener( this );
153 3 : xDicList = NULL; //! release reference
154 : }
155 18 : if (xPropSet.is() && rSource.Source == xPropSet)
156 : {
157 3 : lcl_RemoveAsPropertyChangeListener( this, xPropSet );
158 3 : xPropSet = NULL; //! release reference
159 18 : }
160 18 : }
161 :
162 :
163 0 : void SAL_CALL FlushListener::processDictionaryListEvent(
164 : const DictionaryListEvent& rDicListEvent )
165 : throw(RuntimeException, std::exception)
166 : {
167 0 : MutexGuard aGuard( GetLinguMutex() );
168 :
169 0 : if (rDicListEvent.Source == xDicList)
170 : {
171 0 : sal_Int16 nEvt = rDicListEvent.nCondensedEvent;
172 : sal_Int16 nFlushFlags =
173 : DictionaryListEventFlags::ADD_NEG_ENTRY |
174 : DictionaryListEventFlags::DEL_POS_ENTRY |
175 : DictionaryListEventFlags::ACTIVATE_NEG_DIC |
176 0 : DictionaryListEventFlags::DEACTIVATE_POS_DIC;
177 0 : bool bFlush = 0 != (nEvt & nFlushFlags);
178 :
179 : DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
180 0 : if (bFlush && pFlushObj != NULL)
181 0 : pFlushObj->Flush();
182 0 : }
183 0 : }
184 :
185 :
186 0 : void SAL_CALL FlushListener::propertyChange(
187 : const PropertyChangeEvent& rEvt )
188 : throw(RuntimeException, std::exception)
189 : {
190 0 : MutexGuard aGuard( GetLinguMutex() );
191 :
192 0 : if (rEvt.Source == xPropSet)
193 : {
194 0 : bool bFlush = lcl_IsFlushProperty( rEvt.PropertyHandle );
195 :
196 : DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
197 0 : if (bFlush && pFlushObj != NULL)
198 0 : pFlushObj->Flush();
199 0 : }
200 0 : }
201 :
202 :
203 :
204 27 : SpellCache::SpellCache()
205 : {
206 27 : pFlushLstnr = new FlushListener( this );
207 27 : xFlushLstnr = pFlushLstnr;
208 27 : Reference<XSearchableDictionaryList> aDictionaryList(GetDictionaryList());
209 27 : pFlushLstnr->SetDicList( aDictionaryList ); //! after reference is established
210 54 : Reference<XLinguProperties> aPropertySet(GetLinguProperties());
211 54 : pFlushLstnr->SetPropSet( aPropertySet ); //! after reference is established
212 27 : }
213 :
214 72 : SpellCache::~SpellCache()
215 : {
216 24 : Reference<XSearchableDictionaryList> aEmptyList;
217 48 : Reference<XLinguProperties> aEmptySet;
218 24 : pFlushLstnr->SetDicList( aEmptyList );
219 48 : pFlushLstnr->SetPropSet( aEmptySet );
220 48 : }
221 :
222 0 : void SpellCache::Flush()
223 : {
224 0 : MutexGuard aGuard( GetLinguMutex() );
225 : // clear word list
226 0 : LangWordList_t aEmpty;
227 0 : aWordLists.swap( aEmpty );
228 0 : }
229 :
230 85377296 : bool SpellCache::CheckWord( const OUString& rWord, LanguageType nLang )
231 : {
232 85377296 : MutexGuard aGuard( GetLinguMutex() );
233 85377296 : WordList_t &rList = aWordLists[ nLang ];
234 85377296 : const WordList_t::const_iterator aIt = rList.find( rWord );
235 85377296 : return aIt != rList.end();
236 : }
237 :
238 6943 : void SpellCache::AddWord( const OUString& rWord, LanguageType nLang )
239 : {
240 6943 : MutexGuard aGuard( GetLinguMutex() );
241 6943 : WordList_t & rList = aWordLists[ nLang ];
242 : // occasional clean-up...
243 6943 : if (rList.size() > 500)
244 12 : rList.clear();
245 6943 : rList.insert( rWord );
246 6943 : }
247 :
248 : } // namespace linguistic
249 :
250 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|