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 <tools/debug.hxx>
28 #include <osl/mutex.hxx>
29 #include <linguistic/lngprops.hxx>
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
;
44 #define NUM_FLUSH_PROPS 6
48 const char *pPropName
;
50 } aFlushProperties
[ NUM_FLUSH_PROPS
] =
52 { UPN_IS_USE_DICTIONARY_LIST
, UPH_IS_USE_DICTIONARY_LIST
},
53 { UPN_IS_IGNORE_CONTROL_CHARACTERS
, UPH_IS_IGNORE_CONTROL_CHARACTERS
},
54 { UPN_IS_SPELL_UPPER_CASE
, UPH_IS_SPELL_UPPER_CASE
},
55 { UPN_IS_SPELL_WITH_DIGITS
, UPH_IS_SPELL_WITH_DIGITS
},
56 { UPN_IS_SPELL_CAPITALIZATION
, UPH_IS_SPELL_CAPITALIZATION
}
60 static void lcl_AddAsPropertyChangeListener(
61 Reference
< XPropertyChangeListener
> xListener
,
62 Reference
< XLinguProperties
> &rPropSet
)
64 if (xListener
.is() && rPropSet
.is())
66 for (int i
= 0; i
< NUM_FLUSH_PROPS
; ++i
)
68 rPropSet
->addPropertyChangeListener(
69 OUString::createFromAscii(aFlushProperties
[i
].pPropName
), xListener
);
75 static void lcl_RemoveAsPropertyChangeListener(
76 Reference
< XPropertyChangeListener
> xListener
,
77 Reference
< XLinguProperties
> &rPropSet
)
79 if (xListener
.is() && rPropSet
.is())
81 for (int i
= 0; i
< NUM_FLUSH_PROPS
; ++i
)
83 rPropSet
->removePropertyChangeListener(
84 OUString::createFromAscii(aFlushProperties
[i
].pPropName
), xListener
);
90 static bool lcl_IsFlushProperty( sal_Int32 nHandle
)
93 for (i
= 0; i
< NUM_FLUSH_PROPS
; ++i
)
95 if (nHandle
== aFlushProperties
[i
].nPropHdl
)
98 return i
< NUM_FLUSH_PROPS
;
102 FlushListener::FlushListener( Flushable
*pFO
)
108 FlushListener::~FlushListener()
113 void FlushListener::SetDicList( Reference
<XSearchableDictionaryList
> &rDL
)
115 MutexGuard
aGuard( GetLinguMutex() );
120 xDicList
->removeDictionaryListEventListener( this );
124 xDicList
->addDictionaryListEventListener( this, sal_False
);
129 void FlushListener::SetPropSet( Reference
< XLinguProperties
> &rPS
)
131 MutexGuard
aGuard( GetLinguMutex() );
136 lcl_RemoveAsPropertyChangeListener( this, xPropSet
);
140 lcl_AddAsPropertyChangeListener( this, xPropSet
);
145 void SAL_CALL
FlushListener::disposing( const EventObject
& rSource
)
146 throw(RuntimeException
, std::exception
)
148 MutexGuard
aGuard( GetLinguMutex() );
150 if (xDicList
.is() && rSource
.Source
== xDicList
)
152 xDicList
->removeDictionaryListEventListener( this );
153 xDicList
= NULL
; //! release reference
155 if (xPropSet
.is() && rSource
.Source
== xPropSet
)
157 lcl_RemoveAsPropertyChangeListener( this, xPropSet
);
158 xPropSet
= NULL
; //! release reference
163 void SAL_CALL
FlushListener::processDictionaryListEvent(
164 const DictionaryListEvent
& rDicListEvent
)
165 throw(RuntimeException
, std::exception
)
167 MutexGuard
aGuard( GetLinguMutex() );
169 if (rDicListEvent
.Source
== xDicList
)
171 sal_Int16 nEvt
= rDicListEvent
.nCondensedEvent
;
172 sal_Int16 nFlushFlags
=
173 DictionaryListEventFlags::ADD_NEG_ENTRY
|
174 DictionaryListEventFlags::DEL_POS_ENTRY
|
175 DictionaryListEventFlags::ACTIVATE_NEG_DIC
|
176 DictionaryListEventFlags::DEACTIVATE_POS_DIC
;
177 bool bFlush
= 0 != (nEvt
& nFlushFlags
);
179 DBG_ASSERT( pFlushObj
, "missing object (NULL pointer)" );
180 if (bFlush
&& pFlushObj
!= NULL
)
186 void SAL_CALL
FlushListener::propertyChange(
187 const PropertyChangeEvent
& rEvt
)
188 throw(RuntimeException
, std::exception
)
190 MutexGuard
aGuard( GetLinguMutex() );
192 if (rEvt
.Source
== xPropSet
)
194 bool bFlush
= lcl_IsFlushProperty( rEvt
.PropertyHandle
);
196 DBG_ASSERT( pFlushObj
, "missing object (NULL pointer)" );
197 if (bFlush
&& pFlushObj
!= NULL
)
204 SpellCache::SpellCache()
206 pFlushLstnr
= new FlushListener( this );
207 xFlushLstnr
= pFlushLstnr
;
208 Reference
<XSearchableDictionaryList
> aDictionaryList(GetDictionaryList());
209 pFlushLstnr
->SetDicList( aDictionaryList
); //! after reference is established
210 Reference
<XLinguProperties
> aPropertySet(GetLinguProperties());
211 pFlushLstnr
->SetPropSet( aPropertySet
); //! after reference is established
214 SpellCache::~SpellCache()
216 Reference
<XSearchableDictionaryList
> aEmptyList
;
217 Reference
<XLinguProperties
> aEmptySet
;
218 pFlushLstnr
->SetDicList( aEmptyList
);
219 pFlushLstnr
->SetPropSet( aEmptySet
);
222 void SpellCache::Flush()
224 MutexGuard
aGuard( GetLinguMutex() );
226 LangWordList_t aEmpty
;
227 aWordLists
.swap( aEmpty
);
230 bool SpellCache::CheckWord( const OUString
& rWord
, LanguageType nLang
)
232 MutexGuard
aGuard( GetLinguMutex() );
233 WordList_t
&rList
= aWordLists
[ nLang
];
234 const WordList_t::const_iterator aIt
= rList
.find( rWord
);
235 return aIt
!= rList
.end();
238 void SpellCache::AddWord( const OUString
& rWord
, LanguageType nLang
)
240 MutexGuard
aGuard( GetLinguMutex() );
241 WordList_t
& rList
= aWordLists
[ nLang
];
242 // occasional clean-up...
243 if (rList
.size() > 500)
245 rList
.insert( rWord
);
248 } // namespace linguistic
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */