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 .
21 #include <iprcache.hxx>
22 #include <linguistic/misc.hxx>
24 #include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp>
25 #include <osl/mutex.hxx>
26 #include <unotools/linguprops.hxx>
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
;
40 #define NUM_FLUSH_PROPS 8
46 } aFlushProperties
[ NUM_FLUSH_PROPS
] =
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_CAPITALIZATION
, UPH_IS_SPELL_CAPITALIZATION
},
53 { UPN_IS_SPELL_CLOSED_COMPOUND
, UPH_IS_SPELL_CLOSED_COMPOUND
},
54 { UPN_IS_SPELL_HYPHENATED_COMPOUND
, UPH_IS_SPELL_HYPHENATED_COMPOUND
}
58 static void lcl_AddAsPropertyChangeListener(
59 const Reference
< XPropertyChangeListener
>& xListener
,
60 Reference
< XLinguProperties
> const &rPropSet
)
62 if (xListener
.is() && rPropSet
.is())
64 for (auto& aFlushProperty
: aFlushProperties
)
66 rPropSet
->addPropertyChangeListener(
67 aFlushProperty
.aPropName
, xListener
);
73 static void lcl_RemoveAsPropertyChangeListener(
74 const Reference
< XPropertyChangeListener
>& xListener
,
75 Reference
< XLinguProperties
> const &rPropSet
)
77 if (xListener
.is() && rPropSet
.is())
79 for (auto& aFlushProperty
: aFlushProperties
)
81 rPropSet
->removePropertyChangeListener(
82 aFlushProperty
.aPropName
, xListener
);
88 static bool lcl_IsFlushProperty( sal_Int32 nHandle
)
91 for (i
= 0; i
< NUM_FLUSH_PROPS
; ++i
)
93 if (nHandle
== aFlushProperties
[i
].nPropHdl
)
96 return i
< NUM_FLUSH_PROPS
;
100 void FlushListener::SetDicList( Reference
<XSearchableDictionaryList
> const &rDL
)
102 MutexGuard
aGuard( GetLinguMutex() );
107 xDicList
->removeDictionaryListEventListener( this );
111 xDicList
->addDictionaryListEventListener( this, false );
116 void FlushListener::SetPropSet( Reference
< XLinguProperties
> const &rPS
)
118 MutexGuard
aGuard( GetLinguMutex() );
123 lcl_RemoveAsPropertyChangeListener( this, xPropSet
);
127 lcl_AddAsPropertyChangeListener( this, xPropSet
);
132 void SAL_CALL
FlushListener::disposing( const EventObject
& rSource
)
134 MutexGuard
aGuard( GetLinguMutex() );
136 if (xDicList
.is() && rSource
.Source
== xDicList
)
138 xDicList
->removeDictionaryListEventListener( this );
139 xDicList
= nullptr; //! release reference
141 if (xPropSet
.is() && rSource
.Source
== xPropSet
)
143 lcl_RemoveAsPropertyChangeListener( this, xPropSet
);
144 xPropSet
= nullptr; //! release reference
149 void SAL_CALL
FlushListener::processDictionaryListEvent(
150 const DictionaryListEvent
& rDicListEvent
)
152 MutexGuard
aGuard( GetLinguMutex() );
154 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();
170 void SAL_CALL
FlushListener::propertyChange(
171 const PropertyChangeEvent
& rEvt
)
173 MutexGuard
aGuard( GetLinguMutex() );
175 if (rEvt
.Source
== xPropSet
)
177 bool bFlush
= lcl_IsFlushProperty( rEvt
.PropertyHandle
);
180 mrSpellCache
.Flush();
185 SpellCache::SpellCache()
187 mxFlushLstnr
= new FlushListener( *this );
188 Reference
<XSearchableDictionaryList
> aDictionaryList(GetDictionaryList());
189 mxFlushLstnr
->SetDicList( aDictionaryList
); //! after reference is established
190 Reference
<XLinguProperties
> aPropertySet(GetLinguProperties());
191 mxFlushLstnr
->SetPropSet( aPropertySet
); //! after reference is established
194 SpellCache::~SpellCache()
196 Reference
<XSearchableDictionaryList
> aEmptyList
;
197 Reference
<XLinguProperties
> aEmptySet
;
198 mxFlushLstnr
->SetDicList( aEmptyList
);
199 mxFlushLstnr
->SetPropSet( aEmptySet
);
202 void SpellCache::Flush()
204 MutexGuard
aGuard( GetLinguMutex() );
206 LangWordList_t().swap(aWordLists
);
209 bool SpellCache::CheckWord( const OUString
& rWord
, LanguageType nLang
)
211 MutexGuard
aGuard( GetLinguMutex() );
212 WordList_t
&rList
= aWordLists
[ nLang
];
213 const WordList_t::const_iterator aIt
= rList
.find( rWord
);
214 return aIt
!= rList
.end();
217 void SpellCache::AddWord( const OUString
& rWord
, LanguageType nLang
)
219 MutexGuard
aGuard( GetLinguMutex() );
220 WordList_t
& rList
= aWordLists
[ nLang
];
221 // occasional clean-up...
222 if (rList
.size() > 500)
224 rList
.insert( rWord
);
227 } // namespace linguistic
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */