LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/linguistic/source - iprcache.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 76 101 75.2 %
Date: 2013-07-09 Functions: 14 17 82.4 %
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          19 : static void lcl_AddAsPropertyChangeListener(
      61             :         Reference< XPropertyChangeListener > xListener,
      62             :         Reference< XLinguProperties > &rPropSet )
      63             : {
      64          19 :     if (xListener.is() && rPropSet.is())
      65             :     {
      66         133 :         for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
      67             :         {
      68         114 :             rPropSet->addPropertyChangeListener(
      69         114 :                     OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
      70             :         }
      71             :     }
      72          19 : }
      73             : 
      74             : 
      75          19 : static void lcl_RemoveAsPropertyChangeListener(
      76             :         Reference< XPropertyChangeListener > xListener,
      77             :         Reference< XLinguProperties > &rPropSet )
      78             : {
      79          19 :     if (xListener.is() && rPropSet.is())
      80             :     {
      81         133 :         for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
      82             :         {
      83         114 :             rPropSet->removePropertyChangeListener(
      84         114 :                     OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
      85             :         }
      86             :     }
      87          19 : }
      88             : 
      89             : 
      90           0 : static sal_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          19 : FlushListener::FlushListener( Flushable *pFO )
     103             : {
     104          19 :     SetFlushObj( pFO );
     105          19 : }
     106             : 
     107             : 
     108          38 : FlushListener::~FlushListener()
     109             : {
     110          38 : }
     111             : 
     112             : 
     113          38 : void FlushListener::SetDicList( Reference<XSearchableDictionaryList> &rDL )
     114             : {
     115          38 :     MutexGuard  aGuard( GetLinguMutex() );
     116             : 
     117          38 :     if (xDicList != rDL)
     118             :     {
     119          19 :         if (xDicList.is())
     120           0 :             xDicList->removeDictionaryListEventListener( this );
     121             : 
     122          19 :         xDicList = rDL;
     123          19 :         if (xDicList.is())
     124          19 :             xDicList->addDictionaryListEventListener( this, sal_False );
     125          38 :     }
     126          38 : }
     127             : 
     128             : 
     129          38 : void FlushListener::SetPropSet( Reference< XLinguProperties > &rPS )
     130             : {
     131          38 :     MutexGuard  aGuard( GetLinguMutex() );
     132             : 
     133          38 :     if (xPropSet != rPS)
     134             :     {
     135          19 :         if (xPropSet.is())
     136           0 :             lcl_RemoveAsPropertyChangeListener( this, xPropSet );
     137             : 
     138          19 :         xPropSet = rPS;
     139          19 :         if (xPropSet.is())
     140          19 :             lcl_AddAsPropertyChangeListener( this, xPropSet );
     141          38 :     }
     142          38 : }
     143             : 
     144             : 
     145         114 : void SAL_CALL FlushListener::disposing( const EventObject& rSource )
     146             :         throw(RuntimeException)
     147             : {
     148         114 :     MutexGuard  aGuard( GetLinguMutex() );
     149             : 
     150         114 :     if (xDicList.is()  &&  rSource.Source == xDicList)
     151             :     {
     152          19 :         xDicList->removeDictionaryListEventListener( this );
     153          19 :         xDicList = NULL;    //! release reference
     154             :     }
     155         114 :     if (xPropSet.is()  &&  rSource.Source == xPropSet)
     156             :     {
     157          19 :         lcl_RemoveAsPropertyChangeListener( this, xPropSet );
     158          19 :         xPropSet = NULL;    //! release reference
     159         114 :     }
     160         114 : }
     161             : 
     162             : 
     163           0 : void SAL_CALL FlushListener::processDictionaryListEvent(
     164             :             const DictionaryListEvent& rDicListEvent )
     165             :         throw(RuntimeException)
     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 :         sal_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)
     189             : {
     190           0 :     MutexGuard  aGuard( GetLinguMutex() );
     191             : 
     192           0 :     if (rEvt.Source == xPropSet)
     193             :     {
     194           0 :         sal_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          19 : SpellCache::SpellCache()
     205             : {
     206          19 :     pFlushLstnr = new FlushListener( this );
     207          19 :     xFlushLstnr = pFlushLstnr;
     208          19 :     Reference<XSearchableDictionaryList> aDictionaryList(GetDictionaryList());
     209          19 :     pFlushLstnr->SetDicList( aDictionaryList ); //! after reference is established
     210          38 :     Reference<XLinguProperties> aPropertySet(GetLinguProperties());
     211          38 :     pFlushLstnr->SetPropSet( aPropertySet );    //! after reference is established
     212          19 : }
     213             : 
     214          57 : SpellCache::~SpellCache()
     215             : {
     216          19 :     Reference<XSearchableDictionaryList>  aEmptyList;
     217          38 :     Reference<XLinguProperties>     aEmptySet;
     218          19 :     pFlushLstnr->SetDicList( aEmptyList );
     219          38 :     pFlushLstnr->SetPropSet( aEmptySet );
     220          38 : }
     221             : 
     222           2 : void SpellCache::Flush()
     223             : {
     224           2 :     MutexGuard  aGuard( GetLinguMutex() );
     225             :     // clear word list
     226           4 :     LangWordList_t aEmpty;
     227           4 :     aWordLists.swap( aEmpty );
     228           2 : }
     229             : 
     230       11471 : bool SpellCache::CheckWord( const OUString& rWord, LanguageType nLang )
     231             : {
     232       11471 :     MutexGuard  aGuard( GetLinguMutex() );
     233       11471 :     WordList_t &rList = aWordLists[ nLang ];
     234       11471 :     const WordList_t::const_iterator aIt = rList.find( rWord );
     235       11471 :     return aIt != rList.end();
     236             : }
     237             : 
     238        5468 : void SpellCache::AddWord( const OUString& rWord, LanguageType nLang )
     239             : {
     240        5468 :     MutexGuard  aGuard( GetLinguMutex() );
     241        5468 :     WordList_t & rList = aWordLists[ nLang ];
     242             :     // occasional clean-up...
     243        5468 :     if (rList.size() > 500)
     244          10 :         rList.clear();
     245        5468 :     rList.insert( rWord );
     246        5468 : }
     247             : 
     248             : }   // namespace linguistic
     249             : 
     250             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10