LCOV - code coverage report
Current view: top level - libreoffice/framework/source/fwi/classes - propertysethelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 40 172 23.3 %
Date: 2012-12-17 Functions: 7 20 35.0 %
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             : #include <classes/propertysethelper.hxx>
      21             : #include <threadhelp/transactionguard.hxx>
      22             : #include <threadhelp/readguard.hxx>
      23             : #include <threadhelp/writeguard.hxx>
      24             : 
      25             : 
      26             : namespace framework{
      27             : 
      28             : 
      29             : //-----------------------------------------------------------------------------
      30         516 : PropertySetHelper::PropertySetHelper(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR                       ,
      31             :                                            LockHelper*                                             pExternalLock               ,
      32             :                                            TransactionManager*                                     pExternalTransactionManager ,
      33             :                                            sal_Bool                                                bReleaseLockOnCall          )
      34             :     : m_xSMGR                (xSMGR                                )
      35         516 :     , m_lSimpleChangeListener(pExternalLock->getShareableOslMutex())
      36         516 :     , m_lVetoChangeListener  (pExternalLock->getShareableOslMutex())
      37             :     , m_bReleaseLockOnCall   (bReleaseLockOnCall                   )
      38             :     , m_rLock                (*pExternalLock                       )
      39        1548 :     , m_rTransactionManager  (*pExternalTransactionManager         )
      40             : {
      41         516 : }
      42             : 
      43             : //-----------------------------------------------------------------------------
      44          98 : PropertySetHelper::~PropertySetHelper()
      45             : {
      46          98 : }
      47             : 
      48             : //-----------------------------------------------------------------------------
      49         516 : void PropertySetHelper::impl_setPropertyChangeBroadcaster(const css::uno::Reference< css::uno::XInterface >& xBroadcaster)
      50             : {
      51         516 :     TransactionGuard aTransaction(m_rTransactionManager, E_SOFTEXCEPTIONS);
      52             : 
      53             :     // SAFE ->
      54         516 :     WriteGuard aWriteLock(m_rLock);
      55         516 :     m_xBroadcaster = xBroadcaster;
      56         516 :     aWriteLock.unlock();
      57             :     // <- SAFE
      58         516 : }
      59             : 
      60             : //-----------------------------------------------------------------------------
      61        2580 : void SAL_CALL PropertySetHelper::impl_addPropertyInfo(const css::beans::Property& aProperty)
      62             :     throw(css::beans::PropertyExistException,
      63             :           css::uno::Exception               )
      64             : {
      65        2580 :     TransactionGuard aTransaction(m_rTransactionManager, E_SOFTEXCEPTIONS);
      66             : 
      67             :     // SAFE ->
      68        2580 :     WriteGuard aWriteLock(m_rLock);
      69             : 
      70        2580 :     PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(aProperty.Name);
      71        2580 :     if (pIt != m_lProps.end())
      72           0 :         throw css::beans::PropertyExistException();
      73             : 
      74        2580 :     m_lProps[aProperty.Name] = aProperty;
      75             :     // <- SAFE
      76        2580 : }
      77             : 
      78             : //-----------------------------------------------------------------------------
      79           0 : void SAL_CALL PropertySetHelper::impl_removePropertyInfo(const ::rtl::OUString& sProperty)
      80             :     throw(css::beans::UnknownPropertyException,
      81             :           css::uno::Exception                 )
      82             : {
      83           0 :     TransactionGuard aTransaction(m_rTransactionManager, E_SOFTEXCEPTIONS);
      84             : 
      85             :     // SAFE ->
      86           0 :     WriteGuard aWriteLock(m_rLock);
      87             : 
      88           0 :     PropertySetHelper::TPropInfoHash::iterator pIt = m_lProps.find(sProperty);
      89           0 :     if (pIt == m_lProps.end())
      90           0 :         throw css::beans::UnknownPropertyException();
      91             : 
      92           0 :     m_lProps.erase(pIt);
      93             :     // <- SAFE
      94           0 : }
      95             : 
      96             : //-----------------------------------------------------------------------------
      97         516 : void SAL_CALL PropertySetHelper::impl_enablePropertySet()
      98             : {
      99         516 : }
     100             : 
     101             : //-----------------------------------------------------------------------------
     102          98 : void SAL_CALL PropertySetHelper::impl_disablePropertySet()
     103             : {
     104          98 :     TransactionGuard aTransaction(m_rTransactionManager, E_SOFTEXCEPTIONS);
     105             : 
     106             :     // SAFE ->
     107          98 :     WriteGuard aWriteLock(m_rLock);
     108             : 
     109          98 :     css::uno::Reference< css::uno::XInterface > xThis(static_cast< css::beans::XPropertySet* >(this), css::uno::UNO_QUERY);
     110          98 :     css::lang::EventObject aEvent(xThis);
     111             : 
     112          98 :     m_lSimpleChangeListener.disposeAndClear(aEvent);
     113          98 :     m_lVetoChangeListener.disposeAndClear(aEvent);
     114          98 :     m_lProps.free();
     115             : 
     116          98 :     aWriteLock.unlock();
     117             :     // <- SAFE
     118          98 : }
     119             : 
     120             : //-----------------------------------------------------------------------------
     121           0 : sal_Bool PropertySetHelper::impl_existsVeto(const css::beans::PropertyChangeEvent& aEvent)
     122             : {
     123             :     /*  Dont use the lock here!
     124             :         The used helper is threadsafe and it lives for the whole lifetime of
     125             :         our own object.
     126             :     */
     127           0 :     ::cppu::OInterfaceContainerHelper* pVetoListener = m_lVetoChangeListener.getContainer(aEvent.PropertyName);
     128           0 :     if (! pVetoListener)
     129           0 :         return sal_False;
     130             : 
     131           0 :     ::cppu::OInterfaceIteratorHelper pListener(*pVetoListener);
     132           0 :     while (pListener.hasMoreElements())
     133             :     {
     134             :         try
     135             :         {
     136             :             css::uno::Reference< css::beans::XVetoableChangeListener > xListener(
     137           0 :                 ((css::beans::XVetoableChangeListener*)pListener.next()),
     138           0 :                 css::uno::UNO_QUERY_THROW);
     139           0 :             xListener->vetoableChange(aEvent);
     140             :         }
     141           0 :         catch(const css::uno::RuntimeException&)
     142           0 :             { pListener.remove(); }
     143           0 :         catch(const css::beans::PropertyVetoException&)
     144           0 :             { return sal_True; }
     145             :     }
     146             : 
     147           0 :     return sal_False;
     148             : }
     149             : 
     150             : //-----------------------------------------------------------------------------
     151           0 : void PropertySetHelper::impl_notifyChangeListener(const css::beans::PropertyChangeEvent& aEvent)
     152             : {
     153             :     /*  Dont use the lock here!
     154             :         The used helper is threadsafe and it lives for the whole lifetime of
     155             :         our own object.
     156             :     */
     157           0 :     ::cppu::OInterfaceContainerHelper* pSimpleListener = m_lSimpleChangeListener.getContainer(aEvent.PropertyName);
     158           0 :     if (! pSimpleListener)
     159           0 :         return;
     160             : 
     161           0 :     ::cppu::OInterfaceIteratorHelper pListener(*pSimpleListener);
     162           0 :     while (pListener.hasMoreElements())
     163             :     {
     164             :         try
     165             :         {
     166             :             css::uno::Reference< css::beans::XPropertyChangeListener > xListener(
     167           0 :                 ((css::beans::XVetoableChangeListener*)pListener.next()),
     168           0 :                 css::uno::UNO_QUERY_THROW);
     169           0 :             xListener->propertyChange(aEvent);
     170             :         }
     171           0 :         catch(const css::uno::RuntimeException&)
     172           0 :             { pListener.remove(); }
     173           0 :     }
     174             : }
     175             : 
     176             : //-----------------------------------------------------------------------------
     177           0 : css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL PropertySetHelper::getPropertySetInfo()
     178             :     throw(css::uno::RuntimeException)
     179             : {
     180           0 :     TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
     181             : 
     182           0 :     css::uno::Reference< css::beans::XPropertySetInfo > xInfo(static_cast< css::beans::XPropertySetInfo* >(this), css::uno::UNO_QUERY_THROW);
     183           0 :     return xInfo;
     184             : }
     185             : 
     186             : //-----------------------------------------------------------------------------
     187           0 : void SAL_CALL PropertySetHelper::setPropertyValue(const ::rtl::OUString& sProperty,
     188             :                                                   const css::uno::Any&   aValue   )
     189             :     throw(css::beans::UnknownPropertyException,
     190             :           css::beans::PropertyVetoException   ,
     191             :           css::lang::IllegalArgumentException ,
     192             :           css::lang::WrappedTargetException   ,
     193             :           css::uno::RuntimeException          )
     194             : {
     195             :     // TODO look for e.g. readonly props and reject setProp() call!
     196             : 
     197           0 :     TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
     198             : 
     199             :     // SAFE ->
     200           0 :     WriteGuard aWriteLock(m_rLock);
     201             : 
     202           0 :     PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sProperty);
     203           0 :     if (pIt == m_lProps.end())
     204           0 :         throw css::beans::UnknownPropertyException();
     205             : 
     206           0 :     css::beans::Property aPropInfo = pIt->second;
     207             : 
     208           0 :     sal_Bool bLocked = sal_True;
     209           0 :     if (m_bReleaseLockOnCall)
     210             :     {
     211           0 :         aWriteLock.unlock();
     212           0 :         bLocked = sal_False;
     213             :         // <- SAFE
     214             :     }
     215             : 
     216           0 :     css::uno::Any aCurrentValue = impl_getPropertyValue(aPropInfo.Name, aPropInfo.Handle);
     217             : 
     218           0 :     if (! bLocked)
     219             :     {
     220             :         // SAFE ->
     221           0 :         aWriteLock.lock();
     222           0 :         bLocked = sal_True;
     223             :     }
     224             : 
     225           0 :     sal_Bool bWillBeChanged = (aCurrentValue != aValue);
     226           0 :     if (! bWillBeChanged)
     227           0 :         return;
     228             : 
     229           0 :     css::beans::PropertyChangeEvent aEvent;
     230           0 :     aEvent.PropertyName   = aPropInfo.Name;
     231           0 :     aEvent.Further        = sal_False;
     232           0 :     aEvent.PropertyHandle = aPropInfo.Handle;
     233           0 :     aEvent.OldValue       = aCurrentValue;
     234           0 :     aEvent.NewValue       = aValue;
     235           0 :     aEvent.Source         = css::uno::Reference< css::uno::XInterface >(m_xBroadcaster.get(), css::uno::UNO_QUERY);
     236             : 
     237           0 :     if (m_bReleaseLockOnCall)
     238             :     {
     239           0 :         aWriteLock.unlock();
     240           0 :         bLocked = sal_False;
     241             :         // <- SAFE
     242             :     }
     243             : 
     244           0 :     if (impl_existsVeto(aEvent))
     245           0 :         throw css::beans::PropertyVetoException();
     246             : 
     247           0 :     impl_setPropertyValue(aPropInfo.Name, aPropInfo.Handle, aValue);
     248             : 
     249           0 :     impl_notifyChangeListener(aEvent);
     250             : }
     251             : 
     252             : //-----------------------------------------------------------------------------
     253       43120 : css::uno::Any SAL_CALL PropertySetHelper::getPropertyValue(const ::rtl::OUString& sProperty)
     254             :     throw(css::beans::UnknownPropertyException,
     255             :           css::lang::WrappedTargetException   ,
     256             :           css::uno::RuntimeException          )
     257             : {
     258       43120 :     TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
     259             : 
     260             :     // SAFE ->
     261       43120 :     ReadGuard aReadLock(m_rLock);
     262             : 
     263       43120 :     PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sProperty);
     264       43120 :     if (pIt == m_lProps.end())
     265           0 :         throw css::beans::UnknownPropertyException();
     266             : 
     267       43120 :     css::beans::Property aPropInfo = pIt->second;
     268             : 
     269       43120 :     if (m_bReleaseLockOnCall)
     270           0 :         aReadLock.unlock();
     271             : 
     272       43120 :     return impl_getPropertyValue(aPropInfo.Name, aPropInfo.Handle);
     273             : }
     274             : 
     275             : //-----------------------------------------------------------------------------
     276           0 : void SAL_CALL PropertySetHelper::addPropertyChangeListener(const ::rtl::OUString&                                            sProperty,
     277             :                                                            const css::uno::Reference< css::beans::XPropertyChangeListener >& xListener)
     278             :     throw(css::beans::UnknownPropertyException,
     279             :           css::lang::WrappedTargetException   ,
     280             :           css::uno::RuntimeException          )
     281             : {
     282           0 :     TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
     283             : 
     284             :     // SAFE ->
     285           0 :     ReadGuard aReadLock(m_rLock);
     286             : 
     287           0 :     PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sProperty);
     288           0 :     if (pIt == m_lProps.end())
     289           0 :         throw css::beans::UnknownPropertyException();
     290             : 
     291           0 :     aReadLock.unlock();
     292             :     // <- SAFE
     293             : 
     294           0 :     m_lSimpleChangeListener.addInterface(sProperty, xListener);
     295           0 : }
     296             : 
     297             : //-----------------------------------------------------------------------------
     298           0 : void SAL_CALL PropertySetHelper::removePropertyChangeListener(const ::rtl::OUString&                                            sProperty,
     299             :                                                               const css::uno::Reference< css::beans::XPropertyChangeListener >& xListener)
     300             :     throw(css::beans::UnknownPropertyException,
     301             :           css::lang::WrappedTargetException   ,
     302             :           css::uno::RuntimeException          )
     303             : {
     304           0 :     TransactionGuard aTransaction(m_rTransactionManager, E_SOFTEXCEPTIONS);
     305             : 
     306             :     // SAFE ->
     307           0 :     ReadGuard aReadLock(m_rLock);
     308             : 
     309           0 :     PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sProperty);
     310           0 :     if (pIt == m_lProps.end())
     311           0 :         throw css::beans::UnknownPropertyException();
     312             : 
     313           0 :     aReadLock.unlock();
     314             :     // <- SAFE
     315             : 
     316           0 :     m_lSimpleChangeListener.removeInterface(sProperty, xListener);
     317           0 : }
     318             : 
     319             : //-----------------------------------------------------------------------------
     320           0 : void SAL_CALL PropertySetHelper::addVetoableChangeListener(const ::rtl::OUString&                                            sProperty,
     321             :                                                            const css::uno::Reference< css::beans::XVetoableChangeListener >& xListener)
     322             :     throw(css::beans::UnknownPropertyException,
     323             :           css::lang::WrappedTargetException   ,
     324             :           css::uno::RuntimeException          )
     325             : {
     326           0 :     TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
     327             : 
     328             :     // SAFE ->
     329           0 :     ReadGuard aReadLock(m_rLock);
     330             : 
     331           0 :     PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sProperty);
     332           0 :     if (pIt == m_lProps.end())
     333           0 :         throw css::beans::UnknownPropertyException();
     334             : 
     335           0 :     aReadLock.unlock();
     336             :     // <- SAFE
     337             : 
     338           0 :     m_lVetoChangeListener.addInterface(sProperty, xListener);
     339           0 : }
     340             : 
     341             : //-----------------------------------------------------------------------------
     342           0 : void SAL_CALL PropertySetHelper::removeVetoableChangeListener(const ::rtl::OUString&                                            sProperty,
     343             :                                                               const css::uno::Reference< css::beans::XVetoableChangeListener >& xListener)
     344             :     throw(css::beans::UnknownPropertyException,
     345             :           css::lang::WrappedTargetException   ,
     346             :           css::uno::RuntimeException          )
     347             : {
     348           0 :     TransactionGuard aTransaction(m_rTransactionManager, E_SOFTEXCEPTIONS);
     349             : 
     350             :     // SAFE ->
     351           0 :     ReadGuard aReadLock(m_rLock);
     352             : 
     353           0 :     PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sProperty);
     354           0 :     if (pIt == m_lProps.end())
     355           0 :         throw css::beans::UnknownPropertyException();
     356             : 
     357           0 :     aReadLock.unlock();
     358             :     // <- SAFE
     359             : 
     360           0 :     m_lVetoChangeListener.removeInterface(sProperty, xListener);
     361           0 : }
     362             : 
     363             : //-----------------------------------------------------------------------------
     364           0 : css::uno::Sequence< css::beans::Property > SAL_CALL PropertySetHelper::getProperties()
     365             :     throw(css::uno::RuntimeException)
     366             : {
     367           0 :     TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
     368             : 
     369             :     // SAFE ->
     370           0 :     ReadGuard aReadLock(m_rLock);
     371             : 
     372           0 :     sal_Int32                                        c     = (sal_Int32)m_lProps.size();
     373           0 :     css::uno::Sequence< css::beans::Property >       lProps(c);
     374           0 :     PropertySetHelper::TPropInfoHash::const_iterator pIt   ;
     375             : 
     376           0 :     for (  pIt  = m_lProps.begin();
     377           0 :            pIt != m_lProps.end()  ;
     378             :          ++pIt                    )
     379             :     {
     380           0 :         lProps[--c] = pIt->second;
     381             :     }
     382             : 
     383           0 :     return lProps;
     384             :     // <- SAFE
     385             : }
     386             : 
     387             : //-----------------------------------------------------------------------------
     388           0 : css::beans::Property SAL_CALL PropertySetHelper::getPropertyByName(const ::rtl::OUString& sName)
     389             :     throw(css::beans::UnknownPropertyException,
     390             :           css::uno::RuntimeException          )
     391             : {
     392           0 :     TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
     393             : 
     394             :     // SAFE ->
     395           0 :     ReadGuard aReadLock(m_rLock);
     396             : 
     397           0 :     PropertySetHelper::TPropInfoHash::const_iterator pIt = m_lProps.find(sName);
     398           0 :     if (pIt == m_lProps.end())
     399           0 :         throw css::beans::UnknownPropertyException();
     400             : 
     401           0 :     return pIt->second;
     402             :     // <- SAFE
     403             : }
     404             : 
     405             : //-----------------------------------------------------------------------------
     406           0 : sal_Bool SAL_CALL PropertySetHelper::hasPropertyByName(const ::rtl::OUString& sName)
     407             :     throw(css::uno::RuntimeException)
     408             : {
     409           0 :     TransactionGuard aTransaction(m_rTransactionManager, E_HARDEXCEPTIONS);
     410             : 
     411             :     // SAFE ->
     412           0 :     ReadGuard aReadLock(m_rLock);
     413             : 
     414           0 :     PropertySetHelper::TPropInfoHash::iterator pIt    = m_lProps.find(sName);
     415           0 :     sal_Bool                                   bExist = (pIt != m_lProps.end());
     416             : 
     417           0 :     return bExist;
     418             :     // <- SAFE
     419             : }
     420             : 
     421             : } // namespace framework
     422             : 
     423             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10