/src/libreoffice/linguistic/source/iprcache.cxx
Line | Count | Source |
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 <iprcache.hxx> |
22 | | #include <linguistic/misc.hxx> |
23 | | |
24 | | #include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp> |
25 | | #include <osl/mutex.hxx> |
26 | | #include <unotools/linguprops.hxx> |
27 | | |
28 | | using namespace osl; |
29 | | using namespace com::sun::star; |
30 | | using namespace com::sun::star::beans; |
31 | | using namespace com::sun::star::lang; |
32 | | using namespace com::sun::star::uno; |
33 | | using namespace com::sun::star::linguistic2; |
34 | | |
35 | | |
36 | | namespace linguistic |
37 | | { |
38 | | |
39 | | |
40 | 0 | #define NUM_FLUSH_PROPS 8 |
41 | | |
42 | | const struct |
43 | | { |
44 | | OUString aPropName; |
45 | | sal_Int32 nPropHdl; |
46 | | } aFlushProperties[ NUM_FLUSH_PROPS ] = |
47 | | { |
48 | | { UPN_IS_USE_DICTIONARY_LIST, UPH_IS_USE_DICTIONARY_LIST }, |
49 | | { UPN_IS_IGNORE_CONTROL_CHARACTERS, UPH_IS_IGNORE_CONTROL_CHARACTERS }, |
50 | | { UPN_IS_SPELL_UPPER_CASE, UPH_IS_SPELL_UPPER_CASE }, |
51 | | { UPN_IS_SPELL_WITH_DIGITS, UPH_IS_SPELL_WITH_DIGITS }, |
52 | | { UPN_IS_SPELL_CLOSED_COMPOUND, UPH_IS_SPELL_CLOSED_COMPOUND }, |
53 | | { UPN_IS_SPELL_HYPHENATED_COMPOUND, UPH_IS_SPELL_HYPHENATED_COMPOUND } |
54 | | }; |
55 | | |
56 | | |
57 | | static void lcl_AddAsPropertyChangeListener( |
58 | | const Reference< XPropertyChangeListener >& xListener, |
59 | | Reference< XLinguProperties > const &rPropSet ) |
60 | 0 | { |
61 | 0 | if (xListener.is() && rPropSet.is()) |
62 | 0 | { |
63 | 0 | for (auto& aFlushProperty : aFlushProperties) |
64 | 0 | { |
65 | 0 | rPropSet->addPropertyChangeListener( |
66 | 0 | aFlushProperty.aPropName, xListener ); |
67 | 0 | } |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | |
72 | | static void lcl_RemoveAsPropertyChangeListener( |
73 | | const Reference< XPropertyChangeListener >& xListener, |
74 | | Reference< XLinguProperties > const &rPropSet ) |
75 | 0 | { |
76 | 0 | if (xListener.is() && rPropSet.is()) |
77 | 0 | { |
78 | 0 | for (auto& aFlushProperty : aFlushProperties) |
79 | 0 | { |
80 | 0 | rPropSet->removePropertyChangeListener( |
81 | 0 | aFlushProperty.aPropName, xListener ); |
82 | 0 | } |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | |
87 | | static bool lcl_IsFlushProperty( sal_Int32 nHandle ) |
88 | 0 | { |
89 | 0 | int i; |
90 | 0 | for (i = 0; i < NUM_FLUSH_PROPS; ++i) |
91 | 0 | { |
92 | 0 | if (nHandle == aFlushProperties[i].nPropHdl) |
93 | 0 | break; |
94 | 0 | } |
95 | 0 | return i < NUM_FLUSH_PROPS; |
96 | 0 | } |
97 | | |
98 | | |
99 | | void FlushListener::SetDicList( Reference<XSearchableDictionaryList> const &rDL ) |
100 | 0 | { |
101 | 0 | MutexGuard aGuard( GetLinguMutex() ); |
102 | |
|
103 | 0 | if (xDicList != rDL) |
104 | 0 | { |
105 | 0 | if (xDicList.is()) |
106 | 0 | xDicList->removeDictionaryListEventListener( this ); |
107 | |
|
108 | 0 | xDicList = rDL; |
109 | 0 | if (xDicList.is()) |
110 | 0 | xDicList->addDictionaryListEventListener( this, false ); |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | | |
115 | | void FlushListener::SetPropSet( Reference< XLinguProperties > const &rPS ) |
116 | 0 | { |
117 | 0 | MutexGuard aGuard( GetLinguMutex() ); |
118 | |
|
119 | 0 | if (xPropSet != rPS) |
120 | 0 | { |
121 | 0 | if (xPropSet.is()) |
122 | 0 | lcl_RemoveAsPropertyChangeListener( this, xPropSet ); |
123 | |
|
124 | 0 | xPropSet = rPS; |
125 | 0 | if (xPropSet.is()) |
126 | 0 | lcl_AddAsPropertyChangeListener( this, xPropSet ); |
127 | 0 | } |
128 | 0 | } |
129 | | |
130 | | |
131 | | void SAL_CALL FlushListener::disposing( const EventObject& rSource ) |
132 | 0 | { |
133 | 0 | MutexGuard aGuard( GetLinguMutex() ); |
134 | |
|
135 | 0 | if (xDicList.is() && rSource.Source == xDicList) |
136 | 0 | { |
137 | 0 | xDicList->removeDictionaryListEventListener( this ); |
138 | 0 | xDicList = nullptr; //! release reference |
139 | 0 | } |
140 | 0 | if (xPropSet.is() && rSource.Source == xPropSet) |
141 | 0 | { |
142 | 0 | lcl_RemoveAsPropertyChangeListener( this, xPropSet ); |
143 | 0 | xPropSet = nullptr; //! release reference |
144 | 0 | } |
145 | 0 | } |
146 | | |
147 | | |
148 | | void SAL_CALL FlushListener::processDictionaryListEvent( |
149 | | const DictionaryListEvent& rDicListEvent ) |
150 | 0 | { |
151 | 0 | MutexGuard aGuard( GetLinguMutex() ); |
152 | |
|
153 | 0 | if (rDicListEvent.Source != xDicList) |
154 | 0 | return; |
155 | | |
156 | 0 | sal_Int16 nEvt = rDicListEvent.nCondensedEvent; |
157 | 0 | sal_Int16 const nFlushFlags = |
158 | 0 | DictionaryListEventFlags::ADD_NEG_ENTRY | |
159 | 0 | DictionaryListEventFlags::DEL_POS_ENTRY | |
160 | 0 | DictionaryListEventFlags::ACTIVATE_NEG_DIC | |
161 | 0 | DictionaryListEventFlags::DEACTIVATE_POS_DIC; |
162 | 0 | bool bFlush = 0 != (nEvt & nFlushFlags); |
163 | |
|
164 | 0 | if (bFlush) |
165 | 0 | mrSpellCache.Flush(); |
166 | 0 | } |
167 | | |
168 | | |
169 | | void SAL_CALL FlushListener::propertyChange( |
170 | | const PropertyChangeEvent& rEvt ) |
171 | 0 | { |
172 | 0 | MutexGuard aGuard( GetLinguMutex() ); |
173 | |
|
174 | 0 | if (rEvt.Source == xPropSet) |
175 | 0 | { |
176 | 0 | bool bFlush = lcl_IsFlushProperty( rEvt.PropertyHandle ); |
177 | |
|
178 | 0 | if (bFlush) |
179 | 0 | mrSpellCache.Flush(); |
180 | 0 | } |
181 | 0 | } |
182 | | |
183 | | |
184 | | SpellCache::SpellCache() |
185 | 0 | { |
186 | 0 | mxFlushLstnr = new FlushListener( *this ); |
187 | 0 | Reference<XSearchableDictionaryList> aDictionaryList(GetDictionaryList()); |
188 | 0 | mxFlushLstnr->SetDicList( aDictionaryList ); //! after reference is established |
189 | 0 | Reference<XLinguProperties> aPropertySet(GetLinguProperties()); |
190 | 0 | mxFlushLstnr->SetPropSet( aPropertySet ); //! after reference is established |
191 | 0 | } |
192 | | |
193 | | SpellCache::~SpellCache() |
194 | 0 | { |
195 | 0 | Reference<XSearchableDictionaryList> aEmptyList; |
196 | 0 | Reference<XLinguProperties> aEmptySet; |
197 | 0 | mxFlushLstnr->SetDicList( aEmptyList ); |
198 | 0 | mxFlushLstnr->SetPropSet( aEmptySet ); |
199 | 0 | } |
200 | | |
201 | | void SpellCache::Flush() |
202 | 0 | { |
203 | 0 | MutexGuard aGuard( GetLinguMutex() ); |
204 | | // clear word list |
205 | 0 | LangWordList_t().swap(aWordLists); |
206 | 0 | } |
207 | | |
208 | | bool SpellCache::CheckWord( const OUString& rWord, LanguageType nLang ) |
209 | 0 | { |
210 | 0 | MutexGuard aGuard( GetLinguMutex() ); |
211 | 0 | WordList_t &rList = aWordLists[ nLang ]; |
212 | 0 | const WordList_t::const_iterator aIt = rList.find( rWord ); |
213 | 0 | return aIt != rList.end(); |
214 | 0 | } |
215 | | |
216 | | void SpellCache::AddWord( const OUString& rWord, LanguageType nLang ) |
217 | 0 | { |
218 | 0 | MutexGuard aGuard( GetLinguMutex() ); |
219 | 0 | WordList_t & rList = aWordLists[ nLang ]; |
220 | | // occasional clean-up... |
221 | 0 | if (rList.size() > 500) |
222 | 0 | rList.clear(); |
223 | 0 | rList.insert( rWord ); |
224 | 0 | } |
225 | | |
226 | | } // namespace linguistic |
227 | | |
228 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |