LCOV - code coverage report
Current view: top level - libreoffice/forms/source/component - propertybaghelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 28 126 22.2 %
Date: 2012-12-27 Functions: 5 16 31.2 %
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/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          17 :         static ::comphelper::IPropertyInfoService& lcl_getPropertyInfos()
      69             :         {
      70          17 :             static ConcreteInfoService s_aPropInfos;
      71          17 :             return s_aPropInfos;
      72             :         }
      73             :     }
      74             : 
      75             :     //====================================================================
      76             :     //= PropertyBagHelper
      77             :     //====================================================================
      78             :     //--------------------------------------------------------------------
      79          17 :     PropertyBagHelper::PropertyBagHelper( IPropertyBagHelperContext& _rContext )
      80             :         :m_rContext( _rContext )
      81             :         ,m_pPropertyArrayHelper( NULL )
      82          17 :         ,m_bDisposed( false )
      83             :     {
      84          17 :     }
      85             : 
      86             :     //--------------------------------------------------------------------
      87          18 :     PropertyBagHelper::~PropertyBagHelper()
      88             :     {
      89           9 :         delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
      90           9 :     }
      91             : 
      92             :     //--------------------------------------------------------------------
      93          14 :     void PropertyBagHelper::dispose()
      94             :     {
      95          14 :         m_bDisposed = true;
      96          14 :     }
      97             : 
      98             :     //--------------------------------------------------------------------
      99           0 :     void PropertyBagHelper::impl_nts_checkDisposed_throw() const
     100             :     {
     101           0 :         if ( m_bDisposed )
     102           0 :             throw DisposedException();
     103           0 :     }
     104             : 
     105             :     //--------------------------------------------------------------------
     106           0 :     void PropertyBagHelper::impl_nts_invalidatePropertySetInfo()
     107             :     {
     108           0 :         delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
     109           0 :     }
     110             : 
     111             :     //--------------------------------------------------------------------
     112           0 :     sal_Int32 PropertyBagHelper::impl_findFreeHandle( const ::rtl::OUString& _rPropertyName )
     113             :     {
     114           0 :         ::comphelper::OPropertyArrayAggregationHelper& rPropInfo( impl_ts_getArrayHelper() );
     115             : 
     116             :         // check the preferred handle
     117           0 :         sal_Int32 nHandle = lcl_getPropertyInfos().getPreferedPropertyId( _rPropertyName );
     118           0 :         if ( ( nHandle != -1 ) && rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
     119           0 :             nHandle = -1;
     120             : 
     121             :         // seach a free handle in <math>F_1009</math>
     122           0 :         if ( nHandle == -1 )
     123             :         {
     124           0 :             sal_Int32 nPrime = 1009;
     125           0 :             sal_Int32 nFactor = 11;
     126           0 :             sal_Int32 nNum = nFactor;
     127           0 :             while ( nNum != 1 )
     128             :             {
     129           0 :                 if ( !rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nNum + NEW_HANDLE_BASE ) )
     130             :                 {
     131             :                     // handle not used, yet
     132           0 :                     nHandle = nNum + NEW_HANDLE_BASE;
     133           0 :                     break;
     134             :                 }
     135           0 :                 nNum = ( nNum * nFactor ) % nPrime;
     136             :             }
     137             :         }
     138             : 
     139             :         // search a free handle greater NEW_HANDLE_BASE
     140           0 :         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           0 :         return nHandle;
     148             :     }
     149             : 
     150             :     //--------------------------------------------------------------------
     151        2297 :     ::comphelper::OPropertyArrayAggregationHelper& PropertyBagHelper::impl_ts_getArrayHelper() const
     152             :     {
     153        2297 :         OPropertyArrayAggregationHelper* p = m_pPropertyArrayHelper;
     154        2297 :         if ( !p )
     155             :         {
     156          17 :             ::osl::MutexGuard aGuard( m_rContext.getMutex() );
     157          17 :             p = m_pPropertyArrayHelper;
     158          17 :             if ( !p )
     159             :             {
     160             :                 // our own fixed and our aggregate's properties
     161          17 :                 Sequence< Property > aFixedProps;
     162          17 :                 Sequence< Property > aAggregateProps;
     163          17 :                 m_rContext.describeFixedAndAggregateProperties( aFixedProps, aAggregateProps );
     164             : 
     165             :                 // our dynamic properties
     166          17 :                 Sequence< Property > aDynamicProps;
     167          17 :                 m_aDynamicProperties.describeProperties( aDynamicProps );
     168             : 
     169             :                 Sequence< Property > aOwnProps(
     170          17 :                     ::comphelper::concatSequences( aFixedProps, aDynamicProps ) );
     171             : 
     172          17 :                 p = new OPropertyArrayAggregationHelper( aOwnProps, aAggregateProps, &lcl_getPropertyInfos(), NEW_HANDLE_BASE );
     173             :                 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     174          17 :                 const_cast< PropertyBagHelper* >( this )->m_pPropertyArrayHelper = p;
     175          17 :             }
     176             :         } // if ( !p )
     177             :         else
     178             :         {
     179             :             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     180             :         }
     181        2297 :         return *p;
     182             :     }
     183             : 
     184             :     //--------------------------------------------------------------------
     185           0 :     void PropertyBagHelper::addProperty( const ::rtl::OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue )
     186             :     {
     187           0 :         ::osl::MutexGuard aGuard( m_rContext.getMutex() );
     188           0 :         impl_nts_checkDisposed_throw();
     189             : 
     190             :         //----------------------------------------------
     191             :         // check name sanity
     192           0 :         ::comphelper::OPropertyArrayAggregationHelper& aPropInfo( impl_ts_getArrayHelper() );
     193           0 :         if ( aPropInfo.hasPropertyByName( _rName ) )
     194           0 :             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           0 :         _nAttributes |= PropertyAttribute::REMOVEABLE;
     200             : 
     201             :         //----------------------------------------------
     202             :         // find a free handle
     203           0 :         sal_Int32 nHandle = impl_findFreeHandle( _rName );
     204             : 
     205             :         //----------------------------------------------
     206             :         // register the property, and invalidate our property meta data
     207           0 :         m_aDynamicProperties.addProperty( _rName, nHandle, _nAttributes, _rInitialValue );
     208           0 :         impl_nts_invalidatePropertySetInfo();
     209           0 :     }
     210             : 
     211             :     //--------------------------------------------------------------------
     212           0 :     void PropertyBagHelper::removeProperty( const ::rtl::OUString& _rName )
     213             :     {
     214           0 :         ::osl::MutexGuard aGuard( m_rContext.getMutex() );
     215           0 :         impl_nts_checkDisposed_throw();
     216             : 
     217             :         // check whether it's removeable at all
     218           0 :         Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
     219           0 :         Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
     220           0 :         Property aProperty( xPSI->getPropertyByName( _rName ) );
     221           0 :         if ( ( aProperty.Attributes & PropertyAttribute::REMOVEABLE ) == 0 )
     222           0 :             throw NotRemoveableException( _rName, xMe );
     223             : 
     224           0 :         m_aDynamicProperties.removeProperty( _rName );
     225           0 :         impl_nts_invalidatePropertySetInfo();
     226           0 :     }
     227             : 
     228             :     //--------------------------------------------------------------------
     229             :     namespace
     230             :     {
     231             :         //----------------------------------------------------------------
     232             :         struct SelectNameOfProperty : public ::std::unary_function< Property, ::rtl::OUString >
     233             :         {
     234           0 :             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           0 :             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           0 :             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           0 :     Sequence< PropertyValue > PropertyBagHelper::getPropertyValues()
     261             :     {
     262           0 :         ::osl::MutexGuard aGuard( m_rContext.getMutex() );
     263           0 :         impl_nts_checkDisposed_throw();
     264             : 
     265           0 :         Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
     266           0 :         Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
     267             : 
     268           0 :         Sequence< Property > aProperties( xPSI->getProperties() );
     269           0 :         Sequence< ::rtl::OUString > aPropertyNames( aProperties.getLength() );
     270           0 :         ::std::transform( aProperties.getConstArray(), aProperties.getConstArray() + aProperties.getLength(),
     271           0 :             aPropertyNames.getArray(), SelectNameOfProperty() );
     272             : 
     273           0 :         Sequence< Any > aValues;
     274             :         try
     275             :         {
     276           0 :             aValues = xMe->getPropertyValues( aPropertyNames );
     277             : 
     278           0 :             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           0 :         Sequence< PropertyValue > aPropertyValues( aValues.getLength() );
     287           0 :         PropertyValue* pPropertyValue = aPropertyValues.getArray();
     288             : 
     289           0 :         const ::rtl::OUString* pName = aPropertyNames.getConstArray();
     290           0 :         const ::rtl::OUString* pNameEnd = aPropertyNames.getConstArray() + aPropertyNames.getLength();
     291           0 :         const Any* pValue = aValues.getConstArray();
     292           0 :         for ( ; pName != pNameEnd; ++pName, ++pValue, ++pPropertyValue )
     293             :         {
     294           0 :             pPropertyValue->Name = *pName;
     295           0 :             pPropertyValue->Value = *pValue;
     296             :         }
     297             : 
     298           0 :         return aPropertyValues;
     299             :     }
     300             : 
     301             :     //--------------------------------------------------------------------
     302           0 :     void PropertyBagHelper::setPropertyValues( const Sequence< PropertyValue >& _rProps )
     303             :     {
     304           0 :         ::osl::ClearableMutexGuard aGuard( m_rContext.getMutex() );
     305           0 :         impl_nts_checkDisposed_throw();
     306             : 
     307           0 :         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           0 :         Sequence< PropertyValue > aSortedProps( _rProps );
     312           0 :         ::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           0 :         ::comphelper::OPropertyArrayAggregationHelper& rArrayHelper( impl_ts_getArrayHelper() );
     318           0 :         for (   const PropertyValue* pProperties = aSortedProps.getConstArray();
     319           0 :                 pProperties != aSortedProps.getConstArray() + nPropertyValues;
     320             :                 ++pProperties
     321             :             )
     322             :         {
     323           0 :             if ( !rArrayHelper.hasPropertyByName( pProperties->Name ) )
     324           0 :                 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           0 :         Sequence< ::rtl::OUString > aNames( nPropertyValues );
     330           0 :         ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
     331           0 :             aNames.getArray(), SelectNameOfPropertyValue() );
     332             : 
     333           0 :         Sequence< Any > aValues( nPropertyValues );
     334           0 :         ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
     335           0 :             aValues.getArray(), SelectValueOfPropertyValue() );
     336             : 
     337           0 :         Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
     338             : 
     339           0 :         aGuard.clear();
     340           0 :         xMe->setPropertyValues( aNames, aValues );
     341           0 :     }
     342             : 
     343             : //........................................................................
     344             : } // namespace frm
     345             : //........................................................................
     346             : 
     347             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10