LCOV - code coverage report
Current view: top level - libreoffice/linguistic/source - iprcache.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 70 101 69.3 %
Date: 2012-12-17 Functions: 13 17 76.5 %
Legend: Lines: hit not hit

          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           1 : static void lcl_AddAsPropertyChangeListener(
      62             :         Reference< XPropertyChangeListener > xListener,
      63             :         Reference< XPropertySet > &rPropSet )
      64             : {
      65           1 :     if (xListener.is() && rPropSet.is())
      66             :     {
      67           7 :         for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
      68             :         {
      69           6 :             rPropSet->addPropertyChangeListener(
      70           6 :                     ::rtl::OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
      71             :         }
      72             :     }
      73           1 : }
      74             : 
      75             : 
      76           1 : static void lcl_RemoveAsPropertyChangeListener(
      77             :         Reference< XPropertyChangeListener > xListener,
      78             :         Reference< XPropertySet > &rPropSet )
      79             : {
      80           1 :     if (xListener.is() && rPropSet.is())
      81             :     {
      82           7 :         for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
      83             :         {
      84           6 :             rPropSet->removePropertyChangeListener(
      85           6 :                     ::rtl::OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
      86             :         }
      87             :     }
      88           1 : }
      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           1 : FlushListener::FlushListener( Flushable *pFO )
     104             : {
     105           1 :     SetFlushObj( pFO );
     106           1 : }
     107             : 
     108             : 
     109           2 : FlushListener::~FlushListener()
     110             : {
     111           2 : }
     112             : 
     113             : 
     114           2 : void FlushListener::SetDicList( Reference<XDictionaryList> &rDL )
     115             : {
     116           2 :     MutexGuard  aGuard( GetLinguMutex() );
     117             : 
     118           2 :     if (xDicList != rDL)
     119             :     {
     120           1 :         if (xDicList.is())
     121           0 :             xDicList->removeDictionaryListEventListener( this );
     122             : 
     123           1 :         xDicList = rDL;
     124           1 :         if (xDicList.is())
     125           1 :             xDicList->addDictionaryListEventListener( this, sal_False );
     126           2 :     }
     127           2 : }
     128             : 
     129             : 
     130           2 : void FlushListener::SetPropSet( Reference< XPropertySet > &rPS )
     131             : {
     132           2 :     MutexGuard  aGuard( GetLinguMutex() );
     133             : 
     134           2 :     if (xPropSet != rPS)
     135             :     {
     136           1 :         if (xPropSet.is())
     137           0 :             lcl_RemoveAsPropertyChangeListener( this, xPropSet );
     138             : 
     139           1 :         xPropSet = rPS;
     140           1 :         if (xPropSet.is())
     141           1 :             lcl_AddAsPropertyChangeListener( this, xPropSet );
     142           2 :     }
     143           2 : }
     144             : 
     145             : 
     146           6 : void SAL_CALL FlushListener::disposing( const EventObject& rSource )
     147             :         throw(RuntimeException)
     148             : {
     149           6 :     MutexGuard  aGuard( GetLinguMutex() );
     150             : 
     151           6 :     if (xDicList.is()  &&  rSource.Source == xDicList)
     152             :     {
     153           1 :         xDicList->removeDictionaryListEventListener( this );
     154           1 :         xDicList = NULL;    //! release reference
     155             :     }
     156           6 :     if (xPropSet.is()  &&  rSource.Source == xPropSet)
     157             :     {
     158           1 :         lcl_RemoveAsPropertyChangeListener( this, xPropSet );
     159           1 :         xPropSet = NULL;    //! release reference
     160           6 :     }
     161           6 : }
     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           1 : SpellCache::SpellCache()
     206             : {
     207           1 :     pFlushLstnr = new FlushListener( this );
     208           1 :     xFlushLstnr = pFlushLstnr;
     209           1 :     Reference<XDictionaryList> aDictionaryList(GetDictionaryList());
     210           1 :     pFlushLstnr->SetDicList( aDictionaryList ); //! after reference is established
     211           1 :     Reference<XPropertySet> aPropertySet(GetLinguProperties());
     212           1 :     pFlushLstnr->SetPropSet( aPropertySet );    //! after reference is established
     213           1 : }
     214             : 
     215           3 : SpellCache::~SpellCache()
     216             : {
     217           1 :     Reference<XDictionaryList>  aEmptyList;
     218           1 :     Reference<XPropertySet>     aEmptySet;
     219           1 :     pFlushLstnr->SetDicList( aEmptyList );
     220           1 :     pFlushLstnr->SetPropSet( aEmptySet );
     221           2 : }
     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          35 : bool SpellCache::CheckWord( const OUString& rWord, LanguageType nLang )
     232             : {
     233          35 :     MutexGuard  aGuard( GetLinguMutex() );
     234          35 :     WordList_t &rList = aWordLists[ nLang ];
     235          35 :     const WordList_t::const_iterator aIt = rList.find( rWord );
     236          35 :     return aIt != rList.end();
     237             : }
     238             : 
     239          21 : void SpellCache::AddWord( const OUString& rWord, LanguageType nLang )
     240             : {
     241          21 :     MutexGuard  aGuard( GetLinguMutex() );
     242          21 :     WordList_t & rList = aWordLists[ nLang ];
     243             :     // occasional clean-up...
     244          21 :     if (rList.size() > 500)
     245           0 :         rList.clear();
     246          21 :     rList.insert( rWord );
     247          21 : }
     248             : 
     249             : }   // namespace linguistic
     250             : 
     251             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10