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 : using ::rtl::OUString;
40 :
41 : namespace linguistic
42 : {
43 :
44 :
45 : #define NUM_FLUSH_PROPS 6
46 :
47 : static const struct
48 : {
49 : const char *pPropName;
50 : sal_Int32 nPropHdl;
51 : } aFlushProperties[ NUM_FLUSH_PROPS ] =
52 : {
53 : { UPN_IS_USE_DICTIONARY_LIST, UPH_IS_USE_DICTIONARY_LIST },
54 : { UPN_IS_IGNORE_CONTROL_CHARACTERS, UPH_IS_IGNORE_CONTROL_CHARACTERS },
55 : { UPN_IS_SPELL_UPPER_CASE, UPH_IS_SPELL_UPPER_CASE },
56 : { UPN_IS_SPELL_WITH_DIGITS, UPH_IS_SPELL_WITH_DIGITS },
57 : { UPN_IS_SPELL_CAPITALIZATION, UPH_IS_SPELL_CAPITALIZATION }
58 : };
59 :
60 :
61 0 : static void lcl_AddAsPropertyChangeListener(
62 : Reference< XPropertyChangeListener > xListener,
63 : Reference< XPropertySet > &rPropSet )
64 : {
65 0 : if (xListener.is() && rPropSet.is())
66 : {
67 0 : for (int i = 0; i < NUM_FLUSH_PROPS; ++i)
68 : {
69 0 : rPropSet->addPropertyChangeListener(
70 0 : ::rtl::OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
71 : }
72 : }
73 0 : }
74 :
75 :
76 0 : static void lcl_RemoveAsPropertyChangeListener(
77 : Reference< XPropertyChangeListener > xListener,
78 : Reference< XPropertySet > &rPropSet )
79 : {
80 0 : if (xListener.is() && rPropSet.is())
81 : {
82 0 : for (int i = 0; i < NUM_FLUSH_PROPS; ++i)
83 : {
84 0 : rPropSet->removePropertyChangeListener(
85 0 : ::rtl::OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
86 : }
87 : }
88 0 : }
89 :
90 :
91 0 : static sal_Bool lcl_IsFlushProperty( sal_Int32 nHandle )
92 : {
93 : int i;
94 0 : for (i = 0; i < NUM_FLUSH_PROPS; ++i)
95 : {
96 0 : if (nHandle == aFlushProperties[i].nPropHdl)
97 0 : break;
98 : }
99 0 : return i < NUM_FLUSH_PROPS;
100 : }
101 :
102 :
103 0 : FlushListener::FlushListener( Flushable *pFO )
104 : {
105 0 : SetFlushObj( pFO );
106 0 : }
107 :
108 :
109 0 : FlushListener::~FlushListener()
110 : {
111 0 : }
112 :
113 :
114 0 : void FlushListener::SetDicList( Reference<XDictionaryList> &rDL )
115 : {
116 0 : MutexGuard aGuard( GetLinguMutex() );
117 :
118 0 : if (xDicList != rDL)
119 : {
120 0 : if (xDicList.is())
121 0 : xDicList->removeDictionaryListEventListener( this );
122 :
123 0 : xDicList = rDL;
124 0 : if (xDicList.is())
125 0 : xDicList->addDictionaryListEventListener( this, sal_False );
126 0 : }
127 0 : }
128 :
129 :
130 0 : void FlushListener::SetPropSet( Reference< XPropertySet > &rPS )
131 : {
132 0 : MutexGuard aGuard( GetLinguMutex() );
133 :
134 0 : if (xPropSet != rPS)
135 : {
136 0 : if (xPropSet.is())
137 0 : lcl_RemoveAsPropertyChangeListener( this, xPropSet );
138 :
139 0 : xPropSet = rPS;
140 0 : if (xPropSet.is())
141 0 : lcl_AddAsPropertyChangeListener( this, xPropSet );
142 0 : }
143 0 : }
144 :
145 :
146 0 : void SAL_CALL FlushListener::disposing( const EventObject& rSource )
147 : throw(RuntimeException)
148 : {
149 0 : MutexGuard aGuard( GetLinguMutex() );
150 :
151 0 : if (xDicList.is() && rSource.Source == xDicList)
152 : {
153 0 : xDicList->removeDictionaryListEventListener( this );
154 0 : xDicList = NULL; //! release reference
155 : }
156 0 : if (xPropSet.is() && rSource.Source == xPropSet)
157 : {
158 0 : lcl_RemoveAsPropertyChangeListener( this, xPropSet );
159 0 : xPropSet = NULL; //! release reference
160 0 : }
161 0 : }
162 :
163 :
164 0 : void SAL_CALL FlushListener::processDictionaryListEvent(
165 : const DictionaryListEvent& rDicListEvent )
166 : throw(RuntimeException)
167 : {
168 0 : MutexGuard aGuard( GetLinguMutex() );
169 :
170 0 : if (rDicListEvent.Source == xDicList)
171 : {
172 0 : sal_Int16 nEvt = rDicListEvent.nCondensedEvent;
173 : sal_Int16 nFlushFlags =
174 : DictionaryListEventFlags::ADD_NEG_ENTRY |
175 : DictionaryListEventFlags::DEL_POS_ENTRY |
176 : DictionaryListEventFlags::ACTIVATE_NEG_DIC |
177 0 : DictionaryListEventFlags::DEACTIVATE_POS_DIC;
178 0 : sal_Bool bFlush = 0 != (nEvt & nFlushFlags);
179 :
180 : DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
181 0 : if (bFlush && pFlushObj != NULL)
182 0 : pFlushObj->Flush();
183 0 : }
184 0 : }
185 :
186 :
187 0 : void SAL_CALL FlushListener::propertyChange(
188 : const PropertyChangeEvent& rEvt )
189 : throw(RuntimeException)
190 : {
191 0 : MutexGuard aGuard( GetLinguMutex() );
192 :
193 0 : if (rEvt.Source == xPropSet)
194 : {
195 0 : sal_Bool bFlush = lcl_IsFlushProperty( rEvt.PropertyHandle );
196 :
197 : DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
198 0 : if (bFlush && pFlushObj != NULL)
199 0 : pFlushObj->Flush();
200 0 : }
201 0 : }
202 :
203 :
204 :
205 0 : SpellCache::SpellCache()
206 : {
207 0 : pFlushLstnr = new FlushListener( this );
208 0 : xFlushLstnr = pFlushLstnr;
209 0 : Reference<XDictionaryList> aDictionaryList(GetDictionaryList());
210 0 : pFlushLstnr->SetDicList( aDictionaryList ); //! after reference is established
211 0 : Reference<XPropertySet> aPropertySet(GetLinguProperties());
212 0 : pFlushLstnr->SetPropSet( aPropertySet ); //! after reference is established
213 0 : }
214 :
215 0 : SpellCache::~SpellCache()
216 : {
217 0 : Reference<XDictionaryList> aEmptyList;
218 0 : Reference<XPropertySet> aEmptySet;
219 0 : pFlushLstnr->SetDicList( aEmptyList );
220 0 : pFlushLstnr->SetPropSet( aEmptySet );
221 0 : }
222 :
223 0 : void SpellCache::Flush()
224 : {
225 0 : MutexGuard aGuard( GetLinguMutex() );
226 : // clear word list
227 0 : LangWordList_t aEmpty;
228 0 : aWordLists.swap( aEmpty );
229 0 : }
230 :
231 0 : bool SpellCache::CheckWord( const OUString& rWord, LanguageType nLang )
232 : {
233 0 : MutexGuard aGuard( GetLinguMutex() );
234 0 : WordList_t &rList = aWordLists[ nLang ];
235 0 : const WordList_t::const_iterator aIt = rList.find( rWord );
236 0 : return aIt != rList.end();
237 : }
238 :
239 0 : void SpellCache::AddWord( const OUString& rWord, LanguageType nLang )
240 : {
241 0 : MutexGuard aGuard( GetLinguMutex() );
242 0 : WordList_t & rList = aWordLists[ nLang ];
243 : // occasional clean-up...
244 0 : if (rList.size() > 500)
245 0 : rList.clear();
246 0 : rList.insert( rWord );
247 0 : }
248 :
249 : } // namespace linguistic
250 :
251 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|