LCOV - code coverage report
Current view: top level - forms/source/component - propertybaghelper.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 126 0.0 %
Date: 2014-04-14 Functions: 0 16 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "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           0 :         static ::comphelper::IPropertyInfoService& lcl_getPropertyInfos()
      67             :         {
      68           0 :             static ConcreteInfoService s_aPropInfos;
      69           0 :             return s_aPropInfos;
      70             :         }
      71             :     }
      72             : 
      73             : 
      74             :     //= PropertyBagHelper
      75             : 
      76             : 
      77           0 :     PropertyBagHelper::PropertyBagHelper( IPropertyBagHelperContext& _rContext )
      78             :         :m_rContext( _rContext )
      79             :         ,m_pPropertyArrayHelper( NULL )
      80           0 :         ,m_bDisposed( false )
      81             :     {
      82           0 :     }
      83             : 
      84             : 
      85           0 :     PropertyBagHelper::~PropertyBagHelper()
      86             :     {
      87           0 :         delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
      88           0 :     }
      89             : 
      90             : 
      91           0 :     void PropertyBagHelper::dispose()
      92             :     {
      93           0 :         m_bDisposed = true;
      94           0 :     }
      95             : 
      96             : 
      97           0 :     void PropertyBagHelper::impl_nts_checkDisposed_throw() const
      98             :     {
      99           0 :         if ( m_bDisposed )
     100           0 :             throw DisposedException();
     101           0 :     }
     102             : 
     103             : 
     104           0 :     void PropertyBagHelper::impl_nts_invalidatePropertySetInfo()
     105             :     {
     106           0 :         delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
     107           0 :     }
     108             : 
     109             : 
     110           0 :     sal_Int32 PropertyBagHelper::impl_findFreeHandle( const OUString& _rPropertyName )
     111             :     {
     112           0 :         ::comphelper::OPropertyArrayAggregationHelper& rPropInfo( impl_ts_getArrayHelper() );
     113             : 
     114             :         // check the preferred handle
     115           0 :         sal_Int32 nHandle = lcl_getPropertyInfos().getPreferredPropertyId( _rPropertyName );
     116           0 :         if ( ( nHandle != -1 ) && rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
     117           0 :             nHandle = -1;
     118             : 
     119             :         // seach a free handle in <math>F_1009</math>
     120           0 :         if ( nHandle == -1 )
     121             :         {
     122           0 :             sal_Int32 nPrime = 1009;
     123           0 :             sal_Int32 nFactor = 11;
     124           0 :             sal_Int32 nNum = nFactor;
     125           0 :             while ( nNum != 1 )
     126             :             {
     127           0 :                 if ( !rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nNum + NEW_HANDLE_BASE ) )
     128             :                 {
     129             :                     // handle not used, yet
     130           0 :                     nHandle = nNum + NEW_HANDLE_BASE;
     131           0 :                     break;
     132             :                 }
     133           0 :                 nNum = ( nNum * nFactor ) % nPrime;
     134             :             }
     135             :         }
     136             : 
     137             :         // search a free handle greater NEW_HANDLE_BASE
     138           0 :         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           0 :         return nHandle;
     146             :     }
     147             : 
     148             : 
     149           0 :     ::comphelper::OPropertyArrayAggregationHelper& PropertyBagHelper::impl_ts_getArrayHelper() const
     150             :     {
     151           0 :         OPropertyArrayAggregationHelper* p = m_pPropertyArrayHelper;
     152           0 :         if ( !p )
     153             :         {
     154           0 :             ::osl::MutexGuard aGuard( m_rContext.getMutex() );
     155           0 :             p = m_pPropertyArrayHelper;
     156           0 :             if ( !p )
     157             :             {
     158             :                 // our own fixed and our aggregate's properties
     159           0 :                 Sequence< Property > aFixedProps;
     160           0 :                 Sequence< Property > aAggregateProps;
     161           0 :                 m_rContext.describeFixedAndAggregateProperties( aFixedProps, aAggregateProps );
     162             : 
     163             :                 // our dynamic properties
     164           0 :                 Sequence< Property > aDynamicProps;
     165           0 :                 m_aDynamicProperties.describeProperties( aDynamicProps );
     166             : 
     167             :                 Sequence< Property > aOwnProps(
     168           0 :                     ::comphelper::concatSequences( aFixedProps, aDynamicProps ) );
     169             : 
     170           0 :                 p = new OPropertyArrayAggregationHelper( aOwnProps, aAggregateProps, &lcl_getPropertyInfos(), NEW_HANDLE_BASE );
     171             :                 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     172           0 :                 const_cast< PropertyBagHelper* >( this )->m_pPropertyArrayHelper = p;
     173           0 :             }
     174             :         } // if ( !p )
     175             :         else
     176             :         {
     177             :             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     178             :         }
     179           0 :         return *p;
     180             :     }
     181             : 
     182             : 
     183           0 :     void PropertyBagHelper::addProperty( const OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue )
     184             :     {
     185           0 :         ::osl::MutexGuard aGuard( m_rContext.getMutex() );
     186           0 :         impl_nts_checkDisposed_throw();
     187             : 
     188             : 
     189             :         // check name sanity
     190           0 :         ::comphelper::OPropertyArrayAggregationHelper& aPropInfo( impl_ts_getArrayHelper() );
     191           0 :         if ( aPropInfo.hasPropertyByName( _rName ) )
     192           0 :             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           0 :         _nAttributes |= PropertyAttribute::REMOVABLE;
     198             : 
     199             : 
     200             :         // find a free handle
     201           0 :         sal_Int32 nHandle = impl_findFreeHandle( _rName );
     202             : 
     203             : 
     204             :         // register the property, and invalidate our property meta data
     205           0 :         m_aDynamicProperties.addProperty( _rName, nHandle, _nAttributes, _rInitialValue );
     206           0 :         impl_nts_invalidatePropertySetInfo();
     207           0 :     }
     208             : 
     209             : 
     210           0 :     void PropertyBagHelper::removeProperty( const OUString& _rName )
     211             :     {
     212           0 :         ::osl::MutexGuard aGuard( m_rContext.getMutex() );
     213           0 :         impl_nts_checkDisposed_throw();
     214             : 
     215             :         // check whether it's removable at all
     216           0 :         Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
     217           0 :         Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
     218           0 :         Property aProperty( xPSI->getPropertyByName( _rName ) );
     219           0 :         if ( ( aProperty.Attributes & PropertyAttribute::REMOVABLE ) == 0 )
     220           0 :             throw NotRemoveableException( _rName, xMe );
     221             : 
     222           0 :         m_aDynamicProperties.removeProperty( _rName );
     223           0 :         impl_nts_invalidatePropertySetInfo();
     224           0 :     }
     225             : 
     226             : 
     227             :     namespace
     228             :     {
     229             : 
     230             :         struct SelectNameOfProperty : public ::std::unary_function< Property, OUString >
     231             :         {
     232           0 :             const OUString& operator()( const Property& _rProp ) const { return _rProp.Name; }
     233             :         };
     234             : 
     235             : 
     236             :         struct SelectNameOfPropertyValue : public ::std::unary_function< PropertyValue, OUString >
     237             :         {
     238           0 :             const OUString& operator()( const PropertyValue& _rProp ) const { return _rProp.Name; }
     239             :         };
     240             : 
     241             : 
     242             :         struct SelectValueOfPropertyValue : public ::std::unary_function< PropertyValue, Any >
     243             :         {
     244           0 :             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           0 :     Sequence< PropertyValue > PropertyBagHelper::getPropertyValues()
     259             :     {
     260           0 :         ::osl::MutexGuard aGuard( m_rContext.getMutex() );
     261           0 :         impl_nts_checkDisposed_throw();
     262             : 
     263           0 :         Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
     264           0 :         Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
     265             : 
     266           0 :         Sequence< Property > aProperties( xPSI->getProperties() );
     267           0 :         Sequence< OUString > aPropertyNames( aProperties.getLength() );
     268           0 :         ::std::transform( aProperties.getConstArray(), aProperties.getConstArray() + aProperties.getLength(),
     269           0 :             aPropertyNames.getArray(), SelectNameOfProperty() );
     270             : 
     271           0 :         Sequence< Any > aValues;
     272             :         try
     273             :         {
     274           0 :             aValues = xMe->getPropertyValues( aPropertyNames );
     275             : 
     276           0 :             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           0 :         Sequence< PropertyValue > aPropertyValues( aValues.getLength() );
     285           0 :         PropertyValue* pPropertyValue = aPropertyValues.getArray();
     286             : 
     287           0 :         const OUString* pName = aPropertyNames.getConstArray();
     288           0 :         const OUString* pNameEnd = aPropertyNames.getConstArray() + aPropertyNames.getLength();
     289           0 :         const Any* pValue = aValues.getConstArray();
     290           0 :         for ( ; pName != pNameEnd; ++pName, ++pValue, ++pPropertyValue )
     291             :         {
     292           0 :             pPropertyValue->Name = *pName;
     293           0 :             pPropertyValue->Value = *pValue;
     294             :         }
     295             : 
     296           0 :         return aPropertyValues;
     297             :     }
     298             : 
     299             : 
     300           0 :     void PropertyBagHelper::setPropertyValues( const Sequence< PropertyValue >& _rProps )
     301             :     {
     302           0 :         ::osl::ClearableMutexGuard aGuard( m_rContext.getMutex() );
     303           0 :         impl_nts_checkDisposed_throw();
     304             : 
     305           0 :         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           0 :         Sequence< PropertyValue > aSortedProps( _rProps );
     310           0 :         ::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           0 :         ::comphelper::OPropertyArrayAggregationHelper& rArrayHelper( impl_ts_getArrayHelper() );
     316           0 :         for (   const PropertyValue* pProperties = aSortedProps.getConstArray();
     317           0 :                 pProperties != aSortedProps.getConstArray() + nPropertyValues;
     318             :                 ++pProperties
     319             :             )
     320             :         {
     321           0 :             if ( !rArrayHelper.hasPropertyByName( pProperties->Name ) )
     322           0 :                 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           0 :         Sequence< OUString > aNames( nPropertyValues );
     328           0 :         ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
     329           0 :             aNames.getArray(), SelectNameOfPropertyValue() );
     330             : 
     331           0 :         Sequence< Any > aValues( nPropertyValues );
     332           0 :         ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
     333           0 :             aValues.getArray(), SelectValueOfPropertyValue() );
     334             : 
     335           0 :         Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
     336             : 
     337           0 :         aGuard.clear();
     338           0 :         xMe->setPropertyValues( aNames, aValues );
     339           0 :     }
     340             : 
     341             : 
     342             : } // namespace frm
     343             : 
     344             : 
     345             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10