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

Generated by: LCOV version 1.10