1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
23 #include <iprcache.hxx>
24 #include <linguistic/misc.hxx>
26 #include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp>
27 #include <osl/mutex.hxx>
28 #include <linguistic/lngprops.hxx>
29 #include <unotools/linguprops.hxx>
32 using namespace com::sun::star
;
33 using namespace com::sun::star::beans
;
34 using namespace com::sun::star::lang
;
35 using namespace com::sun::star::uno
;
36 using namespace com::sun::star::linguistic2
;
43 #define NUM_FLUSH_PROPS 6
47 const char *pPropName
;
49 } aFlushProperties
[ NUM_FLUSH_PROPS
] =
51 { UPN_IS_USE_DICTIONARY_LIST
, UPH_IS_USE_DICTIONARY_LIST
},
52 { UPN_IS_IGNORE_CONTROL_CHARACTERS
, UPH_IS_IGNORE_CONTROL_CHARACTERS
},
53 { UPN_IS_SPELL_UPPER_CASE
, UPH_IS_SPELL_UPPER_CASE
},
54 { UPN_IS_SPELL_WITH_DIGITS
, UPH_IS_SPELL_WITH_DIGITS
},
55 { UPN_IS_SPELL_CAPITALIZATION
, UPH_IS_SPELL_CAPITALIZATION
}
59 static void lcl_AddAsPropertyChangeListener(
60 const Reference
< XPropertyChangeListener
>& xListener
,
61 Reference
< XLinguProperties
> const &rPropSet
)
63 if (xListener
.is() && rPropSet
.is())
65 for (auto& aFlushPropertie
: aFlushProperties
)
67 rPropSet
->addPropertyChangeListener(
68 OUString::createFromAscii(aFlushPropertie
.pPropName
), xListener
);
74 static void lcl_RemoveAsPropertyChangeListener(
75 const Reference
< XPropertyChangeListener
>& xListener
,
76 Reference
< XLinguProperties
> const &rPropSet
)
78 if (xListener
.is() && rPropSet
.is())
80 for (auto& aFlushPropertie
: aFlushProperties
)
82 rPropSet
->removePropertyChangeListener(
83 OUString::createFromAscii(aFlushPropertie
.pPropName
), xListener
);
89 static bool lcl_IsFlushProperty( sal_Int32 nHandle
)
92 for (i
= 0; i
< NUM_FLUSH_PROPS
; ++i
)
94 if (nHandle
== aFlushProperties
[i
].nPropHdl
)
97 return i
< NUM_FLUSH_PROPS
;
101 void FlushListener::SetDicList( Reference
<XSearchableDictionaryList
> const &rDL
)
103 MutexGuard
aGuard( GetLinguMutex() );
108 xDicList
->removeDictionaryListEventListener( this );
112 xDicList
->addDictionaryListEventListener( this, false );
117 void FlushListener::SetPropSet( Reference
< XLinguProperties
> const &rPS
)
119 MutexGuard
aGuard( GetLinguMutex() );
124 lcl_RemoveAsPropertyChangeListener( this, xPropSet
);
128 lcl_AddAsPropertyChangeListener( this, xPropSet
);
133 void SAL_CALL
FlushListener::disposing( const EventObject
& rSource
)
135 MutexGuard
aGuard( GetLinguMutex() );
137 if (xDicList
.is() && rSource
.Source
== xDicList
)
139 xDicList
->removeDictionaryListEventListener( this );
140 xDicList
= nullptr; //! release reference
142 if (xPropSet
.is() && rSource
.Source
== xPropSet
)
144 lcl_RemoveAsPropertyChangeListener( this, xPropSet
);
145 xPropSet
= nullptr; //! release reference
150 void SAL_CALL
FlushListener::processDictionaryListEvent(
151 const DictionaryListEvent
& rDicListEvent
)
153 MutexGuard
aGuard( GetLinguMutex() );
155 if (rDicListEvent
.Source
== xDicList
)
157 sal_Int16 nEvt
= rDicListEvent
.nCondensedEvent
;
158 sal_Int16
const nFlushFlags
=
159 DictionaryListEventFlags::ADD_NEG_ENTRY
|
160 DictionaryListEventFlags::DEL_POS_ENTRY
|
161 DictionaryListEventFlags::ACTIVATE_NEG_DIC
|
162 DictionaryListEventFlags::DEACTIVATE_POS_DIC
;
163 bool bFlush
= 0 != (nEvt
& nFlushFlags
);
166 mrSpellCache
.Flush();
171 void SAL_CALL
FlushListener::propertyChange(
172 const PropertyChangeEvent
& rEvt
)
174 MutexGuard
aGuard( GetLinguMutex() );
176 if (rEvt
.Source
== xPropSet
)
178 bool bFlush
= lcl_IsFlushProperty( rEvt
.PropertyHandle
);
181 mrSpellCache
.Flush();
186 SpellCache::SpellCache()
188 mxFlushLstnr
= new FlushListener( *this );
189 Reference
<XSearchableDictionaryList
> aDictionaryList(GetDictionaryList());
190 mxFlushLstnr
->SetDicList( aDictionaryList
); //! after reference is established
191 Reference
<XLinguProperties
> aPropertySet(GetLinguProperties());
192 mxFlushLstnr
->SetPropSet( aPropertySet
); //! after reference is established
195 SpellCache::~SpellCache()
197 Reference
<XSearchableDictionaryList
> aEmptyList
;
198 Reference
<XLinguProperties
> aEmptySet
;
199 mxFlushLstnr
->SetDicList( aEmptyList
);
200 mxFlushLstnr
->SetPropSet( aEmptySet
);
203 void SpellCache::Flush()
205 MutexGuard
aGuard( GetLinguMutex() );
207 LangWordList_t aEmpty
;
208 aWordLists
.swap( aEmpty
);
211 bool SpellCache::CheckWord( const OUString
& rWord
, LanguageType nLang
)
213 MutexGuard
aGuard( GetLinguMutex() );
214 WordList_t
&rList
= aWordLists
[ nLang
];
215 const WordList_t::const_iterator aIt
= rList
.find( rWord
);
216 return aIt
!= rList
.end();
219 void SpellCache::AddWord( const OUString
& rWord
, LanguageType nLang
)
221 MutexGuard
aGuard( GetLinguMutex() );
222 WordList_t
& rList
= aWordLists
[ nLang
];
223 // occasional clean-up...
224 if (rList
.size() > 500)
226 rList
.insert( rWord
);
229 } // namespace linguistic
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */