LCOV - code coverage report
Current view: top level - forms/source/component - propertybaghelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 116 126 92.1 %
Date: 2012-08-25 Functions: 15 16 93.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 120 229 52.4 %

           Branch data     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 "propertybaghelper.hxx"
      21                 :            : 
      22                 :            : #include "property.hxx"
      23                 :            : 
      24                 :            : #include <com/sun/star/lang/DisposedException.hpp>
      25                 :            : #include <com/sun/star/beans/PropertyExistException.hpp>
      26                 :            : #include <com/sun/star/beans/XMultiPropertySet.hpp>
      27                 :            : #include <com/sun/star/beans/NotRemoveableException.hpp>
      28                 :            : #include <com/sun/star/beans/UnknownPropertyException.hpp>
      29                 :            : 
      30                 :            : #include <tools/diagnose_ex.h>
      31                 :            : 
      32                 :            : #include <comphelper/sequence.hxx>
      33                 :            : #include <rtl/logfile.hxx>
      34                 :            : #include "rtl/instance.hxx"
      35                 :            : 
      36                 :            : 
      37                 :            : #define NEW_HANDLE_BASE 10000
      38                 :            : 
      39                 :            : //........................................................................
      40                 :            : namespace frm
      41                 :            : {
      42                 :            : //........................................................................
      43                 :            : 
      44                 :            :     /** === begin UNO using === **/
      45                 :            :     using ::com::sun::star::lang::DisposedException;
      46                 :            :     using ::com::sun::star::uno::Sequence;
      47                 :            :     using ::com::sun::star::beans::Property;
      48                 :            :     using ::com::sun::star::uno::Any;
      49                 :            :     using ::com::sun::star::beans::PropertyExistException;
      50                 :            :     using ::com::sun::star::beans::PropertyValue;
      51                 :            :     using ::com::sun::star::uno::Reference;
      52                 :            :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      53                 :            :     using ::com::sun::star::beans::XMultiPropertySet;
      54                 :            :     using ::com::sun::star::beans::XPropertySetInfo;
      55                 :            :     using ::com::sun::star::uno::RuntimeException;
      56                 :            :     using ::com::sun::star::uno::Exception;
      57                 :            :     using ::com::sun::star::beans::NotRemoveableException;
      58                 :            :     using ::com::sun::star::beans::UnknownPropertyException;
      59                 :            :     /** === end UNO using === **/
      60                 :            :     namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
      61                 :            : 
      62                 :            :     //====================================================================
      63                 :            :     //= helper
      64                 :            :     //====================================================================
      65                 :            :     namespace
      66                 :            :     {
      67                 :            :         //----------------------------------------------------------------
      68                 :       1424 :         static ::comphelper::IPropertyInfoService& lcl_getPropertyInfos()
      69                 :            :         {
      70 [ +  + ][ +  - ]:       1424 :             static ConcreteInfoService s_aPropInfos;
      71                 :       1424 :             return s_aPropInfos;
      72                 :            :         }
      73                 :            :     }
      74                 :            : 
      75                 :            :     //====================================================================
      76                 :            :     //= PropertyBagHelper
      77                 :            :     //====================================================================
      78                 :            :     //--------------------------------------------------------------------
      79                 :       1275 :     PropertyBagHelper::PropertyBagHelper( IPropertyBagHelperContext& _rContext )
      80                 :            :         :m_rContext( _rContext )
      81                 :            :         ,m_pPropertyArrayHelper( NULL )
      82         [ +  - ]:       1275 :         ,m_bDisposed( false )
      83                 :            :     {
      84                 :       1275 :     }
      85                 :            : 
      86                 :            :     //--------------------------------------------------------------------
      87         [ +  - ]:       1200 :     PropertyBagHelper::~PropertyBagHelper()
      88                 :            :     {
      89 [ +  + ][ +  - ]:       1200 :         delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
      90                 :       1200 :     }
      91                 :            : 
      92                 :            :     //--------------------------------------------------------------------
      93                 :       1215 :     void PropertyBagHelper::dispose()
      94                 :            :     {
      95                 :       1215 :         m_bDisposed = true;
      96                 :       1215 :     }
      97                 :            : 
      98                 :            :     //--------------------------------------------------------------------
      99                 :        418 :     void PropertyBagHelper::impl_nts_checkDisposed_throw() const
     100                 :            :     {
     101         [ -  + ]:        418 :         if ( m_bDisposed )
     102         [ #  # ]:          0 :             throw DisposedException();
     103                 :        418 :     }
     104                 :            : 
     105                 :            :     //--------------------------------------------------------------------
     106                 :         76 :     void PropertyBagHelper::impl_nts_invalidatePropertySetInfo()
     107                 :            :     {
     108         [ +  - ]:         76 :         delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
     109                 :         76 :     }
     110                 :            : 
     111                 :            :     //--------------------------------------------------------------------
     112                 :        114 :     sal_Int32 PropertyBagHelper::impl_findFreeHandle( const ::rtl::OUString& _rPropertyName )
     113                 :            :     {
     114                 :        114 :         ::comphelper::OPropertyArrayAggregationHelper& rPropInfo( impl_ts_getArrayHelper() );
     115                 :            : 
     116                 :            :         // check the preferred handle
     117                 :        114 :         sal_Int32 nHandle = lcl_getPropertyInfos().getPreferedPropertyId( _rPropertyName );
     118 [ #  # ][ -  + ]:        114 :         if ( ( nHandle != -1 ) && rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
                 [ -  + ]
     119                 :          0 :             nHandle = -1;
     120                 :            : 
     121                 :            :         // seach a free handle in <math>F_1009</math>
     122         [ +  - ]:        114 :         if ( nHandle == -1 )
     123                 :            :         {
     124                 :        114 :             sal_Int32 nPrime = 1009;
     125                 :        114 :             sal_Int32 nFactor = 11;
     126                 :        114 :             sal_Int32 nNum = nFactor;
     127         [ +  - ]:        334 :             while ( nNum != 1 )
     128                 :            :             {
     129         [ +  + ]:        220 :                 if ( !rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nNum + NEW_HANDLE_BASE ) )
     130                 :            :                 {
     131                 :            :                     // handle not used, yet
     132                 :        114 :                     nHandle = nNum + NEW_HANDLE_BASE;
     133                 :        114 :                     break;
     134                 :            :                 }
     135                 :        106 :                 nNum = ( nNum * nFactor ) % nPrime;
     136                 :            :             }
     137                 :            :         }
     138                 :            : 
     139                 :            :         // search a free handle greater NEW_HANDLE_BASE
     140         [ -  + ]:        114 :         if ( nHandle == -1 )
     141                 :            :         {
     142                 :          0 :             nHandle = NEW_HANDLE_BASE + 1009;
     143         [ #  # ]:          0 :             while ( rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
     144                 :          0 :                 ++nHandle;
     145                 :            :         }
     146                 :            : 
     147                 :        114 :         return nHandle;
     148                 :            :     }
     149                 :            : 
     150                 :            :     //--------------------------------------------------------------------
     151                 :     334345 :     ::comphelper::OPropertyArrayAggregationHelper& PropertyBagHelper::impl_ts_getArrayHelper() const
     152                 :            :     {
     153                 :     334345 :         OPropertyArrayAggregationHelper* p = m_pPropertyArrayHelper;
     154         [ +  + ]:     334345 :         if ( !p )
     155                 :            :         {
     156 [ +  - ][ +  - ]:       1310 :             ::osl::MutexGuard aGuard( m_rContext.getMutex() );
     157                 :       1310 :             p = m_pPropertyArrayHelper;
     158         [ +  - ]:       1310 :             if ( !p )
     159                 :            :             {
     160                 :            :                 // our own fixed and our aggregate's properties
     161         [ +  - ]:       1310 :                 Sequence< Property > aFixedProps;
     162         [ +  - ]:       1310 :                 Sequence< Property > aAggregateProps;
     163         [ +  - ]:       1310 :                 m_rContext.describeFixedAndAggregateProperties( aFixedProps, aAggregateProps );
     164                 :            : 
     165                 :            :                 // our dynamic properties
     166         [ +  - ]:       1310 :                 Sequence< Property > aDynamicProps;
     167         [ +  - ]:       1310 :                 m_aDynamicProperties.describeProperties( aDynamicProps );
     168                 :            : 
     169                 :            :                 Sequence< Property > aOwnProps(
     170         [ +  - ]:       1310 :                     ::comphelper::concatSequences( aFixedProps, aDynamicProps ) );
     171                 :            : 
     172         [ +  - ]:       1310 :                 p = new OPropertyArrayAggregationHelper( aOwnProps, aAggregateProps, &lcl_getPropertyInfos(), NEW_HANDLE_BASE );
     173                 :            :                 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     174 [ +  - ][ +  - ]:       1310 :                 const_cast< PropertyBagHelper* >( this )->m_pPropertyArrayHelper = p;
         [ +  - ][ +  - ]
     175         [ +  - ]:       1310 :             }
     176                 :            :         } // if ( !p )
     177                 :            :         else
     178                 :            :         {
     179                 :            :             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     180                 :            :         }
     181                 :     334345 :         return *p;
     182                 :            :     }
     183                 :            : 
     184                 :            :     //--------------------------------------------------------------------
     185                 :        152 :     void PropertyBagHelper::addProperty( const ::rtl::OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue )
     186                 :            :     {
     187 [ +  - ][ +  - ]:        152 :         ::osl::MutexGuard aGuard( m_rContext.getMutex() );
     188         [ +  - ]:        152 :         impl_nts_checkDisposed_throw();
     189                 :            : 
     190                 :            :         //----------------------------------------------
     191                 :            :         // check name sanity
     192         [ +  - ]:        152 :         ::comphelper::OPropertyArrayAggregationHelper& aPropInfo( impl_ts_getArrayHelper() );
     193 [ +  - ][ +  + ]:        152 :         if ( aPropInfo.hasPropertyByName( _rName ) )
     194 [ +  - ][ +  - ]:         38 :             throw PropertyExistException( _rName, m_rContext.getPropertiesInterface() );
     195                 :            : 
     196                 :            :         //----------------------------------------------
     197                 :            :         // normalize the REMOVEABLE attribute - the FormComponent service
     198                 :            :         // requires that all dynamic properties are REMOVEABLE
     199                 :        114 :         _nAttributes |= PropertyAttribute::REMOVEABLE;
     200                 :            : 
     201                 :            :         //----------------------------------------------
     202                 :            :         // find a free handle
     203         [ +  - ]:        114 :         sal_Int32 nHandle = impl_findFreeHandle( _rName );
     204                 :            : 
     205                 :            :         //----------------------------------------------
     206                 :            :         // register the property, and invalidate our property meta data
     207         [ +  + ]:        114 :         m_aDynamicProperties.addProperty( _rName, nHandle, _nAttributes, _rInitialValue );
     208 [ +  - ][ +  - ]:        152 :         impl_nts_invalidatePropertySetInfo();
     209                 :         38 :     }
     210                 :            : 
     211                 :            :     //--------------------------------------------------------------------
     212                 :        114 :     void PropertyBagHelper::removeProperty( const ::rtl::OUString& _rName )
     213                 :            :     {
     214 [ +  - ][ +  - ]:        114 :         ::osl::MutexGuard aGuard( m_rContext.getMutex() );
     215         [ +  - ]:        114 :         impl_nts_checkDisposed_throw();
     216                 :            : 
     217                 :            :         // check whether it's removeable at all
     218 [ +  - ][ +  - ]:        114 :         Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
     219 [ +  - ][ +  - ]:        114 :         Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
                 [ +  - ]
     220 [ +  + ][ +  - ]:        114 :         Property aProperty( xPSI->getPropertyByName( _rName ) );
     221         [ +  + ]:         76 :         if ( ( aProperty.Attributes & PropertyAttribute::REMOVEABLE ) == 0 )
     222         [ +  - ]:         38 :             throw NotRemoveableException( _rName, xMe );
     223                 :            : 
     224         [ +  - ]:         38 :         m_aDynamicProperties.removeProperty( _rName );
     225 [ +  - ][ +  - ]:        114 :         impl_nts_invalidatePropertySetInfo();
     226                 :         38 :     }
     227                 :            : 
     228                 :            :     //--------------------------------------------------------------------
     229                 :            :     namespace
     230                 :            :     {
     231                 :            :         //----------------------------------------------------------------
     232                 :            :         struct SelectNameOfProperty : public ::std::unary_function< Property, ::rtl::OUString >
     233                 :            :         {
     234                 :       1870 :             const ::rtl::OUString& operator()( const Property& _rProp ) const { return _rProp.Name; }
     235                 :            :         };
     236                 :            : 
     237                 :            :         //----------------------------------------------------------------
     238                 :            :         struct SelectNameOfPropertyValue : public ::std::unary_function< PropertyValue, ::rtl::OUString >
     239                 :            :         {
     240                 :         76 :             const ::rtl::OUString& operator()( const PropertyValue& _rProp ) const { return _rProp.Name; }
     241                 :            :         };
     242                 :            : 
     243                 :            :         //----------------------------------------------------------------
     244                 :            :         struct SelectValueOfPropertyValue : public ::std::unary_function< PropertyValue, Any >
     245                 :            :         {
     246                 :         76 :             const Any& operator()( const PropertyValue& _rProp ) const { return _rProp.Value; }
     247                 :            :         };
     248                 :            : 
     249                 :            :         //----------------------------------------------------------------
     250                 :            :         struct PropertyValueLessByName : public ::std::binary_function< PropertyValue, PropertyValue, bool >
     251                 :            :         {
     252                 :          0 :             bool operator()( const PropertyValue& _lhs, const PropertyValue _rhs ) const
     253                 :            :             {
     254                 :          0 :                 return _lhs.Name < _rhs.Name;
     255                 :            :             }
     256                 :            :         };
     257                 :            :     }
     258                 :            : 
     259                 :            :     //--------------------------------------------------------------------
     260                 :         38 :     Sequence< PropertyValue > PropertyBagHelper::getPropertyValues()
     261                 :            :     {
     262 [ +  - ][ +  - ]:         38 :         ::osl::MutexGuard aGuard( m_rContext.getMutex() );
     263         [ +  - ]:         38 :         impl_nts_checkDisposed_throw();
     264                 :            : 
     265 [ +  - ][ +  - ]:         38 :         Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
     266 [ +  - ][ +  - ]:         38 :         Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
                 [ +  - ]
     267                 :            : 
     268 [ +  - ][ +  - ]:         38 :         Sequence< Property > aProperties( xPSI->getProperties() );
     269         [ +  - ]:         38 :         Sequence< ::rtl::OUString > aPropertyNames( aProperties.getLength() );
     270                 :         38 :         ::std::transform( aProperties.getConstArray(), aProperties.getConstArray() + aProperties.getLength(),
     271         [ +  - ]:         76 :             aPropertyNames.getArray(), SelectNameOfProperty() );
     272                 :            : 
     273         [ +  - ]:         38 :         Sequence< Any > aValues;
     274                 :            :         try
     275                 :            :         {
     276 [ +  - ][ +  - ]:         38 :             aValues = xMe->getPropertyValues( aPropertyNames );
         [ +  - ][ +  - ]
     277                 :            : 
     278         [ -  + ]:         38 :             if ( aValues.getLength() != aPropertyNames.getLength() )
     279         [ #  # ]:          0 :                 throw RuntimeException();
     280                 :            :         }
     281      [ #  #  # ]:          0 :         catch( const RuntimeException& ) { throw; }
     282         [ #  # ]:          0 :         catch( const Exception& )
     283                 :            :         {
     284                 :            :             DBG_UNHANDLED_EXCEPTION();
     285                 :            :         }
     286         [ +  - ]:         38 :         Sequence< PropertyValue > aPropertyValues( aValues.getLength() );
     287         [ +  - ]:         38 :         PropertyValue* pPropertyValue = aPropertyValues.getArray();
     288                 :            : 
     289                 :         38 :         const ::rtl::OUString* pName = aPropertyNames.getConstArray();
     290                 :         38 :         const ::rtl::OUString* pNameEnd = aPropertyNames.getConstArray() + aPropertyNames.getLength();
     291                 :         38 :         const Any* pValue = aValues.getConstArray();
     292         [ +  + ]:       1908 :         for ( ; pName != pNameEnd; ++pName, ++pValue, ++pPropertyValue )
     293                 :            :         {
     294                 :       1870 :             pPropertyValue->Name = *pName;
     295                 :       1870 :             pPropertyValue->Value = *pValue;
     296                 :            :         }
     297                 :            : 
     298 [ +  - ][ +  - ]:         38 :         return aPropertyValues;
         [ +  - ][ +  - ]
     299                 :            :     }
     300                 :            : 
     301                 :            :     //--------------------------------------------------------------------
     302                 :        114 :     void PropertyBagHelper::setPropertyValues( const Sequence< PropertyValue >& _rProps )
     303                 :            :     {
     304 [ +  - ][ +  - ]:        114 :         ::osl::ClearableMutexGuard aGuard( m_rContext.getMutex() );
     305         [ +  - ]:        114 :         impl_nts_checkDisposed_throw();
     306                 :            : 
     307                 :        114 :         sal_Int32 nPropertyValues = _rProps.getLength();
     308                 :            : 
     309                 :            :         // XMultiPropertySet::setPropertyValues expects its arguments to be sorted by name
     310                 :            :         // while XPropertyAccess::setPropertyValues doesn't. So first of all, sort.
     311         [ +  - ]:        114 :         Sequence< PropertyValue > aSortedProps( _rProps );
     312 [ +  - ][ +  - ]:        114 :         ::std::sort( aSortedProps.getArray(), aSortedProps.getArray() + nPropertyValues, PropertyValueLessByName() );
                 [ +  - ]
     313                 :            : 
     314                 :            :         // also, XPropertyAccess::setPropertyValues is expected to throw an UnknownPropertyException
     315                 :            :         // for unsupported properties, while XMultiPropertySet::setPropertyValues is expected to ignore
     316                 :            :         // those. So, check for unsupported properties first.
     317         [ +  - ]:        114 :         ::comphelper::OPropertyArrayAggregationHelper& rArrayHelper( impl_ts_getArrayHelper() );
     318         [ +  + ]:        380 :         for (   const PropertyValue* pProperties = aSortedProps.getConstArray();
     319                 :        190 :                 pProperties != aSortedProps.getConstArray() + nPropertyValues;
     320                 :            :                 ++pProperties
     321                 :            :             )
     322                 :            :         {
     323 [ +  - ][ +  + ]:        114 :             if ( !rArrayHelper.hasPropertyByName( pProperties->Name ) )
     324 [ +  - ][ +  - ]:         38 :                 throw UnknownPropertyException( pProperties->Name, m_rContext.getPropertiesInterface() );
     325                 :            :         }
     326                 :            : 
     327                 :            :         // Now finally split into a Name and a Value sequence, and forward to
     328                 :            :         // XMultiPropertySet::setPropertyValues
     329         [ +  - ]:         76 :         Sequence< ::rtl::OUString > aNames( nPropertyValues );
     330                 :         76 :         ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
     331         [ +  - ]:        152 :             aNames.getArray(), SelectNameOfPropertyValue() );
     332                 :            : 
     333         [ +  - ]:         76 :         Sequence< Any > aValues( nPropertyValues );
     334                 :         76 :         ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
     335         [ +  - ]:        152 :             aValues.getArray(), SelectValueOfPropertyValue() );
     336                 :            : 
     337 [ +  - ][ +  - ]:         76 :         Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
     338                 :            : 
     339         [ +  - ]:         76 :         aGuard.clear();
     340 [ +  - ][ +  + ]:        114 :         xMe->setPropertyValues( aNames, aValues );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     341                 :         38 :     }
     342                 :            : 
     343                 :            : //........................................................................
     344                 :            : } // namespace frm
     345                 :            : //........................................................................
     346                 :            : 
     347                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10