LCOV - code coverage report
Current view: top level - linguistic/source - iprcache.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 76 101 75.2 %
Date: 2012-08-25 Functions: 14 17 82.4 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 86 200 43.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : 
      30                 :            : #include <string.h>
      31                 :            : 
      32                 :            : #include "iprcache.hxx"
      33                 :            : #include "linguistic/misc.hxx"
      34                 :            : 
      35                 :            : #include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp>
      36                 :            : #include <tools/debug.hxx>
      37                 :            : #include <osl/mutex.hxx>
      38                 :            : #include <linguistic/lngprops.hxx>
      39                 :            : 
      40                 :            : using namespace utl;
      41                 :            : using namespace osl;
      42                 :            : using namespace com::sun::star;
      43                 :            : using namespace com::sun::star::beans;
      44                 :            : using namespace com::sun::star::lang;
      45                 :            : using namespace com::sun::star::uno;
      46                 :            : using namespace com::sun::star::linguistic2;
      47                 :            : 
      48                 :            : using ::rtl::OUString;
      49                 :            : 
      50                 :            : namespace linguistic
      51                 :            : {
      52                 :            : 
      53                 :            : 
      54                 :            : #define NUM_FLUSH_PROPS     6
      55                 :            : 
      56                 :            : static const struct
      57                 :            : {
      58                 :            :     const char *pPropName;
      59                 :            :     sal_Int32       nPropHdl;
      60                 :            : } aFlushProperties[ NUM_FLUSH_PROPS ] =
      61                 :            : {
      62                 :            :     { UPN_IS_USE_DICTIONARY_LIST,         UPH_IS_USE_DICTIONARY_LIST },
      63                 :            :     { UPN_IS_IGNORE_CONTROL_CHARACTERS,   UPH_IS_IGNORE_CONTROL_CHARACTERS },
      64                 :            :     { UPN_IS_SPELL_UPPER_CASE,            UPH_IS_SPELL_UPPER_CASE },
      65                 :            :     { UPN_IS_SPELL_WITH_DIGITS,           UPH_IS_SPELL_WITH_DIGITS },
      66                 :            :     { UPN_IS_SPELL_CAPITALIZATION,        UPH_IS_SPELL_CAPITALIZATION }
      67                 :            : };
      68                 :            : 
      69                 :            : 
      70                 :         40 : static void lcl_AddAsPropertyChangeListener(
      71                 :            :         Reference< XPropertyChangeListener > xListener,
      72                 :            :         Reference< XPropertySet > &rPropSet )
      73                 :            : {
      74 [ +  - ][ +  - ]:         40 :     if (xListener.is() && rPropSet.is())
                 [ +  - ]
      75                 :            :     {
      76         [ +  + ]:        280 :         for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
      77                 :            :         {
      78                 :        240 :             rPropSet->addPropertyChangeListener(
      79         [ +  - ]:        240 :                     ::rtl::OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
      80                 :            :         }
      81                 :            :     }
      82                 :         40 : }
      83                 :            : 
      84                 :            : 
      85                 :         40 : static void lcl_RemoveAsPropertyChangeListener(
      86                 :            :         Reference< XPropertyChangeListener > xListener,
      87                 :            :         Reference< XPropertySet > &rPropSet )
      88                 :            : {
      89 [ +  - ][ +  - ]:         40 :     if (xListener.is() && rPropSet.is())
                 [ +  - ]
      90                 :            :     {
      91         [ +  + ]:        280 :         for (int i = 0;  i < NUM_FLUSH_PROPS;  ++i)
      92                 :            :         {
      93                 :        240 :             rPropSet->removePropertyChangeListener(
      94         [ +  - ]:        240 :                     ::rtl::OUString::createFromAscii(aFlushProperties[i].pPropName), xListener );
      95                 :            :         }
      96                 :            :     }
      97                 :         40 : }
      98                 :            : 
      99                 :            : 
     100                 :          0 : static sal_Bool lcl_IsFlushProperty( sal_Int32 nHandle )
     101                 :            : {
     102                 :            :     int i;
     103         [ #  # ]:          0 :     for (i = 0;  i < NUM_FLUSH_PROPS;  ++i)
     104                 :            :     {
     105         [ #  # ]:          0 :         if (nHandle == aFlushProperties[i].nPropHdl)
     106                 :          0 :             break;
     107                 :            :     }
     108                 :          0 :     return i < NUM_FLUSH_PROPS;
     109                 :            : }
     110                 :            : 
     111                 :            : 
     112                 :         40 : FlushListener::FlushListener( Flushable *pFO )
     113                 :            : {
     114                 :         40 :     SetFlushObj( pFO );
     115                 :         40 : }
     116                 :            : 
     117                 :            : 
     118                 :         38 : FlushListener::~FlushListener()
     119                 :            : {
     120         [ -  + ]:         76 : }
     121                 :            : 
     122                 :            : 
     123                 :         78 : void FlushListener::SetDicList( Reference<XDictionaryList> &rDL )
     124                 :            : {
     125 [ +  - ][ +  - ]:         78 :     MutexGuard  aGuard( GetLinguMutex() );
     126                 :            : 
     127 [ +  - ][ +  + ]:         78 :     if (xDicList != rDL)
     128                 :            :     {
     129         [ -  + ]:         40 :         if (xDicList.is())
     130 [ #  # ][ #  # ]:          0 :             xDicList->removeDictionaryListEventListener( this );
                 [ #  # ]
     131                 :            : 
     132         [ +  - ]:         40 :         xDicList = rDL;
     133         [ +  - ]:         40 :         if (xDicList.is())
     134 [ +  - ][ +  - ]:         40 :             xDicList->addDictionaryListEventListener( this, sal_False );
                 [ +  - ]
     135         [ +  - ]:         78 :     }
     136                 :         78 : }
     137                 :            : 
     138                 :            : 
     139                 :         78 : void FlushListener::SetPropSet( Reference< XPropertySet > &rPS )
     140                 :            : {
     141 [ +  - ][ +  - ]:         78 :     MutexGuard  aGuard( GetLinguMutex() );
     142                 :            : 
     143 [ +  - ][ +  + ]:         78 :     if (xPropSet != rPS)
     144                 :            :     {
     145         [ -  + ]:         40 :         if (xPropSet.is())
     146 [ #  # ][ #  # ]:          0 :             lcl_RemoveAsPropertyChangeListener( this, xPropSet );
     147                 :            : 
     148         [ +  - ]:         40 :         xPropSet = rPS;
     149         [ +  - ]:         40 :         if (xPropSet.is())
     150 [ +  - ][ +  - ]:         40 :             lcl_AddAsPropertyChangeListener( this, xPropSet );
     151         [ +  - ]:         78 :     }
     152                 :         78 : }
     153                 :            : 
     154                 :            : 
     155                 :        240 : void SAL_CALL FlushListener::disposing( const EventObject& rSource )
     156                 :            :         throw(RuntimeException)
     157                 :            : {
     158 [ +  - ][ +  - ]:        240 :     MutexGuard  aGuard( GetLinguMutex() );
     159                 :            : 
     160 [ +  - ][ +  - ]:        240 :     if (xDicList.is()  &&  rSource.Source == xDicList)
         [ +  + ][ +  + ]
     161                 :            :     {
     162 [ +  - ][ +  - ]:         40 :         xDicList->removeDictionaryListEventListener( this );
                 [ +  - ]
     163         [ +  - ]:         40 :         xDicList = NULL;    //! release reference
     164                 :            :     }
     165 [ +  + ][ +  - ]:        240 :     if (xPropSet.is()  &&  rSource.Source == xPropSet)
         [ +  - ][ +  + ]
     166                 :            :     {
     167 [ +  - ][ +  - ]:         40 :         lcl_RemoveAsPropertyChangeListener( this, xPropSet );
     168         [ +  - ]:         40 :         xPropSet = NULL;    //! release reference
     169         [ +  - ]:        240 :     }
     170                 :        240 : }
     171                 :            : 
     172                 :            : 
     173                 :          0 : void SAL_CALL FlushListener::processDictionaryListEvent(
     174                 :            :             const DictionaryListEvent& rDicListEvent )
     175                 :            :         throw(RuntimeException)
     176                 :            : {
     177 [ #  # ][ #  # ]:          0 :     MutexGuard  aGuard( GetLinguMutex() );
     178                 :            : 
     179 [ #  # ][ #  # ]:          0 :     if (rDicListEvent.Source == xDicList)
     180                 :            :     {
     181                 :          0 :         sal_Int16 nEvt = rDicListEvent.nCondensedEvent;
     182                 :            :         sal_Int16 nFlushFlags =
     183                 :            :                 DictionaryListEventFlags::ADD_NEG_ENTRY     |
     184                 :            :                 DictionaryListEventFlags::DEL_POS_ENTRY     |
     185                 :            :                 DictionaryListEventFlags::ACTIVATE_NEG_DIC  |
     186                 :          0 :                 DictionaryListEventFlags::DEACTIVATE_POS_DIC;
     187                 :          0 :         sal_Bool bFlush = 0 != (nEvt & nFlushFlags);
     188                 :            : 
     189                 :            :         DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
     190 [ #  # ][ #  # ]:          0 :         if (bFlush && pFlushObj != NULL)
     191         [ #  # ]:          0 :             pFlushObj->Flush();
     192         [ #  # ]:          0 :     }
     193                 :          0 : }
     194                 :            : 
     195                 :            : 
     196                 :          0 : void SAL_CALL FlushListener::propertyChange(
     197                 :            :             const PropertyChangeEvent& rEvt )
     198                 :            :         throw(RuntimeException)
     199                 :            : {
     200 [ #  # ][ #  # ]:          0 :     MutexGuard  aGuard( GetLinguMutex() );
     201                 :            : 
     202 [ #  # ][ #  # ]:          0 :     if (rEvt.Source == xPropSet)
     203                 :            :     {
     204                 :          0 :         sal_Bool bFlush = lcl_IsFlushProperty( rEvt.PropertyHandle );
     205                 :            : 
     206                 :            :         DBG_ASSERT( pFlushObj, "missing object (NULL pointer)" );
     207 [ #  # ][ #  # ]:          0 :         if (bFlush && pFlushObj != NULL)
     208         [ #  # ]:          0 :             pFlushObj->Flush();
     209         [ #  # ]:          0 :     }
     210                 :          0 : }
     211                 :            : 
     212                 :            : 
     213                 :            : 
     214         [ +  - ]:         40 : SpellCache::SpellCache()
     215                 :            : {
     216         [ +  - ]:         40 :     pFlushLstnr = new FlushListener( this );
     217 [ +  - ][ +  - ]:         40 :     xFlushLstnr = pFlushLstnr;
     218         [ +  - ]:         40 :     Reference<XDictionaryList> aDictionaryList(GetDictionaryList());
     219         [ +  - ]:         40 :     pFlushLstnr->SetDicList( aDictionaryList ); //! after reference is established
     220         [ +  - ]:         40 :     Reference<XPropertySet> aPropertySet(GetLinguProperties());
     221         [ +  - ]:         40 :     pFlushLstnr->SetPropSet( aPropertySet );    //! after reference is established
     222                 :         40 : }
     223                 :            : 
     224                 :         38 : SpellCache::~SpellCache()
     225                 :            : {
     226                 :         38 :     Reference<XDictionaryList>  aEmptyList;
     227                 :         38 :     Reference<XPropertySet>     aEmptySet;
     228         [ +  - ]:         38 :     pFlushLstnr->SetDicList( aEmptyList );
     229         [ +  - ]:         38 :     pFlushLstnr->SetPropSet( aEmptySet );
     230         [ -  + ]:         76 : }
     231                 :            : 
     232                 :         88 : void SpellCache::Flush()
     233                 :            : {
     234 [ +  - ][ +  - ]:         88 :     MutexGuard  aGuard( GetLinguMutex() );
     235                 :            :     // clear word list
     236         [ +  - ]:         88 :     LangWordList_t aEmpty;
     237 [ +  - ][ +  - ]:         88 :     aWordLists.swap( aEmpty );
     238                 :         88 : }
     239                 :            : 
     240                 :      32229 : bool SpellCache::CheckWord( const OUString& rWord, LanguageType nLang )
     241                 :            : {
     242 [ +  - ][ +  - ]:      32229 :     MutexGuard  aGuard( GetLinguMutex() );
     243         [ +  - ]:      32229 :     WordList_t &rList = aWordLists[ nLang ];
     244         [ +  - ]:      32229 :     const WordList_t::const_iterator aIt = rList.find( rWord );
     245         [ +  - ]:      32229 :     return aIt != rList.end();
     246                 :            : }
     247                 :            : 
     248                 :      10701 : void SpellCache::AddWord( const OUString& rWord, LanguageType nLang )
     249                 :            : {
     250 [ +  - ][ +  - ]:      10701 :     MutexGuard  aGuard( GetLinguMutex() );
     251         [ +  - ]:      10701 :     WordList_t & rList = aWordLists[ nLang ];
     252                 :            :     // occasional clean-up...
     253         [ +  + ]:      10701 :     if (rList.size() > 500)
     254                 :         18 :         rList.clear();
     255 [ +  - ][ +  - ]:      10701 :     rList.insert( rWord );
     256                 :      10701 : }
     257                 :            : 
     258                 :            : }   // namespace linguistic
     259                 :            : 
     260                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10