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

Generated by: LCOV version 1.11