LCOV - code coverage report
Current view: top level - linguistic/source - iprcache.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 73 101 72.3 %
Date: 2014-11-03 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             : 
      40             : namespace linguistic
      41             : {
      42             : 
      43             : 
      44             : #define NUM_FLUSH_PROPS     6
      45             : 
      46             : static const struct
      47             : {
      48             :     const char *pPropName;
      49             :     sal_Int32       nPropHdl;
      50             : } aFlushProperties[ NUM_FLUSH_PROPS ] =
      51             : {
      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 }
      57             : };
      58             : 
      59             : 
      60          32 : static void lcl_AddAsPropertyChangeListener(
      61             :         Reference< XPropertyChangeListener > xListener,
      62             :         Reference< XLinguProperties > &rPropSet )
      63             : {
      64          32 :     if (xListener.is() && rPropSet.is())
      65             :     {
      66         224 :         for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
      67             :         {
      68         192 :             rPropSet->addPropertyChangeListener(
      69         192 :                     OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
      70             :         }
      71             :     }
      72          32 : }
      73             : 
      74             : 
      75          32 : static void lcl_RemoveAsPropertyChangeListener(
      76             :         Reference< XPropertyChangeListener > xListener,
      77             :         Reference< XLinguProperties > &rPropSet )
      78             : {
      79          32 :     if (xListener.is() && rPropSet.is())
      80             :     {
      81         224 :         for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
      82             :         {
      83         192 :             rPropSet->removePropertyChangeListener(
      84         192 :                     OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
      85             :         }
      86             :     }
      87          32 : }
      88             : 
      89             : 
      90           0 : static bool lcl_IsFlushProperty( sal_Int32 nHandle )
      91             : {
      92             :     int i;
      93           0 :     for (i = 0;  i < NUM_FLUSH_PROPS;  ++i)
      94             :     {
      95           0 :         if (nHandle == aFlushProperties[i].nPropHdl)
      96           0 :             break;
      97             :     }
      98           0 :     return i < NUM_FLUSH_PROPS;
      99             : }
     100             : 
     101             : 
     102          32 : FlushListener::FlushListener( Flushable *pFO )
     103             : {
     104          32 :     SetFlushObj( pFO );
     105          32 : }
     106             : 
     107             : 
     108          62 : FlushListener::~FlushListener()
     109             : {
     110          62 : }
     111             : 
     112             : 
     113          63 : void FlushListener::SetDicList( Reference<XSearchableDictionaryList> &rDL )
     114             : {
     115          63 :     MutexGuard  aGuard( GetLinguMutex() );
     116             : 
     117          63 :     if (xDicList != rDL)
     118             :     {
     119          63 :         if (xDicList.is())
     120          31 :             xDicList->removeDictionaryListEventListener( this );
     121             : 
     122          63 :         xDicList = rDL;
     123          63 :         if (xDicList.is())
     124          32 :             xDicList->addDictionaryListEventListener( this, sal_False );
     125          63 :     }
     126          63 : }
     127             : 
     128             : 
     129          63 : void FlushListener::SetPropSet( Reference< XLinguProperties > &rPS )
     130             : {
     131          63 :     MutexGuard  aGuard( GetLinguMutex() );
     132             : 
     133          63 :     if (xPropSet != rPS)
     134             :     {
     135          63 :         if (xPropSet.is())
     136          31 :             lcl_RemoveAsPropertyChangeListener( this, xPropSet );
     137             : 
     138          63 :         xPropSet = rPS;
     139          63 :         if (xPropSet.is())
     140          32 :             lcl_AddAsPropertyChangeListener( this, xPropSet );
     141          63 :     }
     142          63 : }
     143             : 
     144             : 
     145           6 : void SAL_CALL FlushListener::disposing( const EventObject& rSource )
     146             :         throw(RuntimeException, std::exception)
     147             : {
     148           6 :     MutexGuard  aGuard( GetLinguMutex() );
     149             : 
     150           6 :     if (xDicList.is()  &&  rSource.Source == xDicList)
     151             :     {
     152           1 :         xDicList->removeDictionaryListEventListener( this );
     153           1 :         xDicList = NULL;    //! release reference
     154             :     }
     155           6 :     if (xPropSet.is()  &&  rSource.Source == xPropSet)
     156             :     {
     157           1 :         lcl_RemoveAsPropertyChangeListener( this, xPropSet );
     158           1 :         xPropSet = NULL;    //! release reference
     159           6 :     }
     160           6 : }
     161             : 
     162             : 
     163           0 : void SAL_CALL FlushListener::processDictionaryListEvent(
     164             :             const DictionaryListEvent& rDicListEvent )
     165             :         throw(RuntimeException, std::exception)
     166             : {
     167           0 :     MutexGuard  aGuard( GetLinguMutex() );
     168             : 
     169           0 :     if (rDicListEvent.Source == xDicList)
     170             :     {
     171           0 :         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           0 :                 DictionaryListEventFlags::DEACTIVATE_POS_DIC;
     177           0 :         bool bFlush = 0 != (nEvt & nFlushFlags);
     178             : 
     179             :         DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
     180           0 :         if (bFlush && pFlushObj != NULL)
     181           0 :             pFlushObj->Flush();
     182           0 :     }
     183           0 : }
     184             : 
     185             : 
     186           0 : void SAL_CALL FlushListener::propertyChange(
     187             :             const PropertyChangeEvent& rEvt )
     188             :         throw(RuntimeException, std::exception)
     189             : {
     190           0 :     MutexGuard  aGuard( GetLinguMutex() );
     191             : 
     192           0 :     if (rEvt.Source == xPropSet)
     193             :     {
     194           0 :         bool bFlush = lcl_IsFlushProperty( rEvt.PropertyHandle );
     195             : 
     196             :         DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
     197           0 :         if (bFlush && pFlushObj != NULL)
     198           0 :             pFlushObj->Flush();
     199           0 :     }
     200           0 : }
     201             : 
     202             : 
     203             : 
     204          32 : SpellCache::SpellCache()
     205             : {
     206          32 :     pFlushLstnr = new FlushListener( this );
     207          32 :     xFlushLstnr = pFlushLstnr;
     208          32 :     Reference<XSearchableDictionaryList> aDictionaryList(GetDictionaryList());
     209          32 :     pFlushLstnr->SetDicList( aDictionaryList ); //! after reference is established
     210          64 :     Reference<XLinguProperties> aPropertySet(GetLinguProperties());
     211          64 :     pFlushLstnr->SetPropSet( aPropertySet );    //! after reference is established
     212          32 : }
     213             : 
     214          93 : SpellCache::~SpellCache()
     215             : {
     216          31 :     Reference<XSearchableDictionaryList>  aEmptyList;
     217          62 :     Reference<XLinguProperties>     aEmptySet;
     218          31 :     pFlushLstnr->SetDicList( aEmptyList );
     219          62 :     pFlushLstnr->SetPropSet( aEmptySet );
     220          62 : }
     221             : 
     222           0 : void SpellCache::Flush()
     223             : {
     224           0 :     MutexGuard  aGuard( GetLinguMutex() );
     225             :     // clear word list
     226           0 :     LangWordList_t aEmpty;
     227           0 :     aWordLists.swap( aEmpty );
     228           0 : }
     229             : 
     230       10365 : bool SpellCache::CheckWord( const OUString& rWord, LanguageType nLang )
     231             : {
     232       10365 :     MutexGuard  aGuard( GetLinguMutex() );
     233       10365 :     WordList_t &rList = aWordLists[ nLang ];
     234       10365 :     const WordList_t::const_iterator aIt = rList.find( rWord );
     235       10365 :     return aIt != rList.end();
     236             : }
     237             : 
     238        7444 : void SpellCache::AddWord( const OUString& rWord, LanguageType nLang )
     239             : {
     240        7444 :     MutexGuard  aGuard( GetLinguMutex() );
     241        7444 :     WordList_t & rList = aWordLists[ nLang ];
     242             :     // occasional clean-up...
     243        7444 :     if (rList.size() > 500)
     244          12 :         rList.clear();
     245        7444 :     rList.insert( rWord );
     246        7444 : }
     247             : 
     248             : }   // namespace linguistic
     249             : 
     250             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10