bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / component / propertybaghelper.cxx
blob410d88fde5e392feba46adec590eb66d769639d9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "propertybaghelper.hxx"
22 #include "property.hxx"
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>
30 #include <tools/diagnose_ex.h>
32 #include <comphelper/sequence.hxx>
33 #include "rtl/instance.hxx"
36 #define NEW_HANDLE_BASE 10000
39 namespace frm
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;
58 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
61 //= helper
63 namespace
66 static ::comphelper::IPropertyInfoService& lcl_getPropertyInfos()
68 static ConcreteInfoService s_aPropInfos;
69 return s_aPropInfos;
73 PropertyBagHelper::PropertyBagHelper( IPropertyBagHelperContext& _rContext )
74 :m_rContext( _rContext )
75 ,m_pPropertyArrayHelper( NULL )
76 ,m_bDisposed( false )
81 PropertyBagHelper::~PropertyBagHelper()
83 delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
87 void PropertyBagHelper::dispose()
89 m_bDisposed = true;
93 void PropertyBagHelper::impl_nts_checkDisposed_throw() const
95 if ( m_bDisposed )
96 throw DisposedException();
100 void PropertyBagHelper::impl_nts_invalidatePropertySetInfo()
102 delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
106 sal_Int32 PropertyBagHelper::impl_findFreeHandle( const OUString& _rPropertyName )
108 ::comphelper::OPropertyArrayAggregationHelper& rPropInfo( impl_ts_getArrayHelper() );
110 // check the preferred handle
111 sal_Int32 nHandle = lcl_getPropertyInfos().getPreferredPropertyId( _rPropertyName );
112 if ( ( nHandle != -1 ) && rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
113 nHandle = -1;
115 // search a free handle in <math>F_1009</math>
116 if ( nHandle == -1 )
118 sal_Int32 nPrime = 1009;
119 sal_Int32 nFactor = 11;
120 sal_Int32 nNum = nFactor;
121 while ( nNum != 1 )
123 if ( !rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nNum + NEW_HANDLE_BASE ) )
125 // handle not used, yet
126 nHandle = nNum + NEW_HANDLE_BASE;
127 break;
129 nNum = ( nNum * nFactor ) % nPrime;
133 // search a free handle greater NEW_HANDLE_BASE
134 if ( nHandle == -1 )
136 nHandle = NEW_HANDLE_BASE + 1009;
137 while ( rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
138 ++nHandle;
141 return nHandle;
145 ::comphelper::OPropertyArrayAggregationHelper& PropertyBagHelper::impl_ts_getArrayHelper() const
147 OPropertyArrayAggregationHelper* p = m_pPropertyArrayHelper;
148 if ( !p )
150 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
151 p = m_pPropertyArrayHelper;
152 if ( !p )
154 // our own fixed and our aggregate's properties
155 Sequence< Property > aFixedProps;
156 Sequence< Property > aAggregateProps;
157 m_rContext.describeFixedAndAggregateProperties( aFixedProps, aAggregateProps );
159 // our dynamic properties
160 Sequence< Property > aDynamicProps;
161 m_aDynamicProperties.describeProperties( aDynamicProps );
163 Sequence< Property > aOwnProps(
164 ::comphelper::concatSequences( aFixedProps, aDynamicProps ) );
166 p = new OPropertyArrayAggregationHelper( aOwnProps, aAggregateProps, &lcl_getPropertyInfos(), NEW_HANDLE_BASE );
167 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
168 const_cast< PropertyBagHelper* >( this )->m_pPropertyArrayHelper = p;
170 } // if ( !p )
171 else
173 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
175 return *p;
179 void PropertyBagHelper::addProperty( const OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue )
181 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
182 impl_nts_checkDisposed_throw();
185 // check name sanity
186 ::comphelper::OPropertyArrayAggregationHelper& aPropInfo( impl_ts_getArrayHelper() );
187 if ( aPropInfo.hasPropertyByName( _rName ) )
188 throw PropertyExistException( _rName, m_rContext.getPropertiesInterface() );
191 // normalize the REMOVABLE attribute - the FormComponent service
192 // requires that all dynamic properties are REMOVABLE
193 _nAttributes |= PropertyAttribute::REMOVABLE;
196 // find a free handle
197 sal_Int32 nHandle = impl_findFreeHandle( _rName );
200 // register the property, and invalidate our property meta data
201 m_aDynamicProperties.addProperty( _rName, nHandle, _nAttributes, _rInitialValue );
202 impl_nts_invalidatePropertySetInfo();
206 void PropertyBagHelper::removeProperty( const OUString& _rName )
208 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
209 impl_nts_checkDisposed_throw();
211 // check whether it's removable at all
212 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
213 Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
214 Property aProperty( xPSI->getPropertyByName( _rName ) );
215 if ( ( aProperty.Attributes & PropertyAttribute::REMOVABLE ) == 0 )
216 throw NotRemoveableException( _rName, xMe );
218 m_aDynamicProperties.removeProperty( _rName );
219 impl_nts_invalidatePropertySetInfo();
223 namespace
226 struct SelectNameOfProperty : public ::std::unary_function< Property, OUString >
228 const OUString& operator()( const Property& _rProp ) const { return _rProp.Name; }
232 struct SelectNameOfPropertyValue : public ::std::unary_function< PropertyValue, OUString >
234 const OUString& operator()( const PropertyValue& _rProp ) const { return _rProp.Name; }
238 struct SelectValueOfPropertyValue : public ::std::unary_function< PropertyValue, Any >
240 const Any& operator()( const PropertyValue& _rProp ) const { return _rProp.Value; }
244 struct PropertyValueLessByName : public ::std::binary_function< PropertyValue, PropertyValue, bool >
246 bool operator()( const PropertyValue& _lhs, const PropertyValue _rhs ) const
248 return _lhs.Name < _rhs.Name;
254 Sequence< PropertyValue > PropertyBagHelper::getPropertyValues()
256 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
257 impl_nts_checkDisposed_throw();
259 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
260 Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
262 Sequence< Property > aProperties( xPSI->getProperties() );
263 Sequence< OUString > aPropertyNames( aProperties.getLength() );
264 ::std::transform( aProperties.getConstArray(), aProperties.getConstArray() + aProperties.getLength(),
265 aPropertyNames.getArray(), SelectNameOfProperty() );
267 Sequence< Any > aValues;
270 aValues = xMe->getPropertyValues( aPropertyNames );
272 if ( aValues.getLength() != aPropertyNames.getLength() )
273 throw RuntimeException();
275 catch( const RuntimeException& ) { throw; }
276 catch( const Exception& )
278 DBG_UNHANDLED_EXCEPTION();
280 Sequence< PropertyValue > aPropertyValues( aValues.getLength() );
281 PropertyValue* pPropertyValue = aPropertyValues.getArray();
283 const OUString* pName = aPropertyNames.getConstArray();
284 const OUString* pNameEnd = aPropertyNames.getConstArray() + aPropertyNames.getLength();
285 const Any* pValue = aValues.getConstArray();
286 for ( ; pName != pNameEnd; ++pName, ++pValue, ++pPropertyValue )
288 pPropertyValue->Name = *pName;
289 pPropertyValue->Value = *pValue;
292 return aPropertyValues;
296 void PropertyBagHelper::setPropertyValues( const Sequence< PropertyValue >& _rProps )
298 ::osl::ClearableMutexGuard aGuard( m_rContext.getMutex() );
299 impl_nts_checkDisposed_throw();
301 sal_Int32 nPropertyValues = _rProps.getLength();
303 // XMultiPropertySet::setPropertyValues expects its arguments to be sorted by name
304 // while XPropertyAccess::setPropertyValues doesn't. So first of all, sort.
305 Sequence< PropertyValue > aSortedProps( _rProps );
306 ::std::sort( aSortedProps.getArray(), aSortedProps.getArray() + nPropertyValues, PropertyValueLessByName() );
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 ::comphelper::OPropertyArrayAggregationHelper& rArrayHelper( impl_ts_getArrayHelper() );
312 for ( const PropertyValue* pProperties = aSortedProps.getConstArray();
313 pProperties != aSortedProps.getConstArray() + nPropertyValues;
314 ++pProperties
317 if ( !rArrayHelper.hasPropertyByName( pProperties->Name ) )
318 throw UnknownPropertyException( pProperties->Name, m_rContext.getPropertiesInterface() );
321 // Now finally split into a Name and a Value sequence, and forward to
322 // XMultiPropertySet::setPropertyValues
323 Sequence< OUString > aNames( nPropertyValues );
324 ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
325 aNames.getArray(), SelectNameOfPropertyValue() );
327 Sequence< Any > aValues( nPropertyValues );
328 ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
329 aValues.getArray(), SelectValueOfPropertyValue() );
331 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
333 aGuard.clear();
334 xMe->setPropertyValues( aNames, aValues );
338 } // namespace frm
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */