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 0 : static void lcl_AddAsPropertyChangeListener(
61 : Reference< XPropertyChangeListener > xListener,
62 : Reference< XLinguProperties > &rPropSet )
63 : {
64 0 : if (xListener.is() && rPropSet.is())
65 : {
66 0 : for (int i = 0; i < NUM_FLUSH_PROPS; ++i)
67 : {
68 0 : rPropSet->addPropertyChangeListener(
69 0 : OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
70 : }
71 : }
72 0 : }
73 :
74 :
75 0 : static void lcl_RemoveAsPropertyChangeListener(
76 : Reference< XPropertyChangeListener > xListener,
77 : Reference< XLinguProperties > &rPropSet )
78 : {
79 0 : if (xListener.is() && rPropSet.is())
80 : {
81 0 : for (int i = 0; i < NUM_FLUSH_PROPS; ++i)
82 : {
83 0 : rPropSet->removePropertyChangeListener(
84 0 : OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
85 : }
86 : }
87 0 : }
88 :
89 :
90 0 : static sal_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 0 : FlushListener::FlushListener( Flushable *pFO )
103 : {
104 0 : SetFlushObj( pFO );
105 0 : }
106 :
107 :
108 0 : FlushListener::~FlushListener()
109 : {
110 0 : }
111 :
112 :
113 0 : void FlushListener::SetDicList( Reference<XSearchableDictionaryList> &rDL )
114 : {
115 0 : MutexGuard aGuard( GetLinguMutex() );
116 :
117 0 : if (xDicList != rDL)
118 : {
119 0 : if (xDicList.is())
120 0 : xDicList->removeDictionaryListEventListener( this );
121 :
122 0 : xDicList = rDL;
123 0 : if (xDicList.is())
124 0 : xDicList->addDictionaryListEventListener( this, sal_False );
125 0 : }
126 0 : }
127 :
128 :
129 0 : void FlushListener::SetPropSet( Reference< XLinguProperties > &rPS )
130 : {
131 0 : MutexGuard aGuard( GetLinguMutex() );
132 :
133 0 : if (xPropSet != rPS)
134 : {
135 0 : if (xPropSet.is())
136 0 : lcl_RemoveAsPropertyChangeListener( this, xPropSet );
137 :
138 0 : xPropSet = rPS;
139 0 : if (xPropSet.is())
140 0 : lcl_AddAsPropertyChangeListener( this, xPropSet );
141 0 : }
142 0 : }
143 :
144 :
145 0 : void SAL_CALL FlushListener::disposing( const EventObject& rSource )
146 : throw(RuntimeException, std::exception)
147 : {
148 0 : MutexGuard aGuard( GetLinguMutex() );
149 :
150 0 : if (xDicList.is() && rSource.Source == xDicList)
151 : {
152 0 : xDicList->removeDictionaryListEventListener( this );
153 0 : xDicList = NULL; //! release reference
154 : }
155 0 : if (xPropSet.is() && rSource.Source == xPropSet)
156 : {
157 0 : lcl_RemoveAsPropertyChangeListener( this, xPropSet );
158 0 : xPropSet = NULL; //! release reference
159 0 : }
160 0 : }
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 : sal_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 : sal_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 0 : SpellCache::SpellCache()
205 : {
206 0 : pFlushLstnr = new FlushListener( this );
207 0 : xFlushLstnr = pFlushLstnr;
208 0 : Reference<XSearchableDictionaryList> aDictionaryList(GetDictionaryList());
209 0 : pFlushLstnr->SetDicList( aDictionaryList ); //! after reference is established
210 0 : Reference<XLinguProperties> aPropertySet(GetLinguProperties());
211 0 : pFlushLstnr->SetPropSet( aPropertySet ); //! after reference is established
212 0 : }
213 :
214 0 : SpellCache::~SpellCache()
215 : {
216 0 : Reference<XSearchableDictionaryList> aEmptyList;
217 0 : Reference<XLinguProperties> aEmptySet;
218 0 : pFlushLstnr->SetDicList( aEmptyList );
219 0 : pFlushLstnr->SetPropSet( aEmptySet );
220 0 : }
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 0 : bool SpellCache::CheckWord( const OUString& rWord, LanguageType nLang )
231 : {
232 0 : MutexGuard aGuard( GetLinguMutex() );
233 0 : WordList_t &rList = aWordLists[ nLang ];
234 0 : const WordList_t::const_iterator aIt = rList.find( rWord );
235 0 : return aIt != rList.end();
236 : }
237 :
238 0 : void SpellCache::AddWord( const OUString& rWord, LanguageType nLang )
239 : {
240 0 : MutexGuard aGuard( GetLinguMutex() );
241 0 : WordList_t & rList = aWordLists[ nLang ];
242 : // occasional clean-up...
243 0 : if (rList.size() > 500)
244 0 : rList.clear();
245 0 : rList.insert( rWord );
246 0 : }
247 :
248 : } // namespace linguistic
249 :
250 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|