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 6
44 const char *pPropName
;
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
}
56 static void lcl_AddAsPropertyChangeListener(
57 const Reference
< XPropertyChangeListener
>& xListener
,
58 Reference
< XLinguProperties
> const &rPropSet
)
60 if (xListener
.is() && rPropSet
.is())
62 for (auto& aFlushPropertie
: aFlushProperties
)
64 rPropSet
->addPropertyChangeListener(
65 OUString::createFromAscii(aFlushPropertie
.pPropName
), xListener
);
71 static void lcl_RemoveAsPropertyChangeListener(
72 const Reference
< XPropertyChangeListener
>& xListener
,
73 Reference
< XLinguProperties
> const &rPropSet
)
75 if (xListener
.is() && rPropSet
.is())
77 for (auto& aFlushPropertie
: aFlushProperties
)
79 rPropSet
->removePropertyChangeListener(
80 OUString::createFromAscii(aFlushPropertie
.pPropName
), xListener
);
86 static bool lcl_IsFlushProperty( sal_Int32 nHandle
)
89 for (i
= 0; i
< NUM_FLUSH_PROPS
; ++i
)
91 if (nHandle
== aFlushProperties
[i
].nPropHdl
)
94 return i
< NUM_FLUSH_PROPS
;
98 void FlushListener::SetDicList( Reference
<XSearchableDictionaryList
> const &rDL
)
100 MutexGuard
aGuard( GetLinguMutex() );
105 xDicList
->removeDictionaryListEventListener( this );
109 xDicList
->addDictionaryListEventListener( this, false );
114 void FlushListener::SetPropSet( Reference
< XLinguProperties
> const &rPS
)
116 MutexGuard
aGuard( GetLinguMutex() );
121 lcl_RemoveAsPropertyChangeListener( this, xPropSet
);
125 lcl_AddAsPropertyChangeListener( this, xPropSet
);
130 void SAL_CALL
FlushListener::disposing( const EventObject
& rSource
)
132 MutexGuard
aGuard( GetLinguMutex() );
134 if (xDicList
.is() && rSource
.Source
== xDicList
)
136 xDicList
->removeDictionaryListEventListener( this );
137 xDicList
= nullptr; //! release reference
139 if (xPropSet
.is() && rSource
.Source
== xPropSet
)
141 lcl_RemoveAsPropertyChangeListener( this, xPropSet
);
142 xPropSet
= nullptr; //! release reference
147 void SAL_CALL
FlushListener::processDictionaryListEvent(
148 const DictionaryListEvent
& rDicListEvent
)
150 MutexGuard
aGuard( GetLinguMutex() );
152 if (rDicListEvent
.Source
== xDicList
)
154 sal_Int16 nEvt
= rDicListEvent
.nCondensedEvent
;
155 sal_Int16
const nFlushFlags
=
156 DictionaryListEventFlags::ADD_NEG_ENTRY
|
157 DictionaryListEventFlags::DEL_POS_ENTRY
|
158 DictionaryListEventFlags::ACTIVATE_NEG_DIC
|
159 DictionaryListEventFlags::DEACTIVATE_POS_DIC
;
160 bool bFlush
= 0 != (nEvt
& nFlushFlags
);
163 mrSpellCache
.Flush();
168 void SAL_CALL
FlushListener::propertyChange(
169 const PropertyChangeEvent
& rEvt
)
171 MutexGuard
aGuard( GetLinguMutex() );
173 if (rEvt
.Source
== xPropSet
)
175 bool bFlush
= lcl_IsFlushProperty( rEvt
.PropertyHandle
);
178 mrSpellCache
.Flush();
183 SpellCache::SpellCache()
185 mxFlushLstnr
= new FlushListener( *this );
186 Reference
<XSearchableDictionaryList
> aDictionaryList(GetDictionaryList());
187 mxFlushLstnr
->SetDicList( aDictionaryList
); //! after reference is established
188 Reference
<XLinguProperties
> aPropertySet(GetLinguProperties());
189 mxFlushLstnr
->SetPropSet( aPropertySet
); //! after reference is established
192 SpellCache::~SpellCache()
194 Reference
<XSearchableDictionaryList
> aEmptyList
;
195 Reference
<XLinguProperties
> aEmptySet
;
196 mxFlushLstnr
->SetDicList( aEmptyList
);
197 mxFlushLstnr
->SetPropSet( aEmptySet
);
200 void SpellCache::Flush()
202 MutexGuard
aGuard( GetLinguMutex() );
204 LangWordList_t aEmpty
;
205 aWordLists
.swap( aEmpty
);
208 bool SpellCache::CheckWord( const OUString
& rWord
, LanguageType nLang
)
210 MutexGuard
aGuard( GetLinguMutex() );
211 WordList_t
&rList
= aWordLists
[ nLang
];
212 const WordList_t::const_iterator aIt
= rList
.find( rWord
);
213 return aIt
!= rList
.end();
216 void SpellCache::AddWord( const OUString
& rWord
, LanguageType nLang
)
218 MutexGuard
aGuard( GetLinguMutex() );
219 WordList_t
& rList
= aWordLists
[ nLang
];
220 // occasional clean-up...
221 if (rList
.size() > 500)
223 rList
.insert( rWord
);
226 } // namespace linguistic
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */