update credits
[LibreOffice.git] / forms / source / component / propertybaghelper.cxx
blobfb4c0d99cc8d61a0ef9f678350a495e90f89c752
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/logfile.hxx>
34 #include "rtl/instance.hxx"
37 #define NEW_HANDLE_BASE 10000
39 //........................................................................
40 namespace frm
42 //........................................................................
44 using ::com::sun::star::lang::DisposedException;
45 using ::com::sun::star::uno::Sequence;
46 using ::com::sun::star::beans::Property;
47 using ::com::sun::star::uno::Any;
48 using ::com::sun::star::beans::PropertyExistException;
49 using ::com::sun::star::beans::PropertyValue;
50 using ::com::sun::star::uno::Reference;
51 using ::com::sun::star::uno::UNO_QUERY_THROW;
52 using ::com::sun::star::beans::XMultiPropertySet;
53 using ::com::sun::star::beans::XPropertySetInfo;
54 using ::com::sun::star::uno::RuntimeException;
55 using ::com::sun::star::uno::Exception;
56 using ::com::sun::star::beans::NotRemoveableException;
57 using ::com::sun::star::beans::UnknownPropertyException;
59 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
61 //====================================================================
62 //= helper
63 //====================================================================
64 namespace
66 //----------------------------------------------------------------
67 static ::comphelper::IPropertyInfoService& lcl_getPropertyInfos()
69 static ConcreteInfoService s_aPropInfos;
70 return s_aPropInfos;
74 //====================================================================
75 //= PropertyBagHelper
76 //====================================================================
77 //--------------------------------------------------------------------
78 PropertyBagHelper::PropertyBagHelper( IPropertyBagHelperContext& _rContext )
79 :m_rContext( _rContext )
80 ,m_pPropertyArrayHelper( NULL )
81 ,m_bDisposed( false )
85 //--------------------------------------------------------------------
86 PropertyBagHelper::~PropertyBagHelper()
88 delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
91 //--------------------------------------------------------------------
92 void PropertyBagHelper::dispose()
94 m_bDisposed = true;
97 //--------------------------------------------------------------------
98 void PropertyBagHelper::impl_nts_checkDisposed_throw() const
100 if ( m_bDisposed )
101 throw DisposedException();
104 //--------------------------------------------------------------------
105 void PropertyBagHelper::impl_nts_invalidatePropertySetInfo()
107 delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
110 //--------------------------------------------------------------------
111 sal_Int32 PropertyBagHelper::impl_findFreeHandle( const OUString& _rPropertyName )
113 ::comphelper::OPropertyArrayAggregationHelper& rPropInfo( impl_ts_getArrayHelper() );
115 // check the preferred handle
116 sal_Int32 nHandle = lcl_getPropertyInfos().getPreferedPropertyId( _rPropertyName );
117 if ( ( nHandle != -1 ) && rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
118 nHandle = -1;
120 // seach a free handle in <math>F_1009</math>
121 if ( nHandle == -1 )
123 sal_Int32 nPrime = 1009;
124 sal_Int32 nFactor = 11;
125 sal_Int32 nNum = nFactor;
126 while ( nNum != 1 )
128 if ( !rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nNum + NEW_HANDLE_BASE ) )
130 // handle not used, yet
131 nHandle = nNum + NEW_HANDLE_BASE;
132 break;
134 nNum = ( nNum * nFactor ) % nPrime;
138 // search a free handle greater NEW_HANDLE_BASE
139 if ( nHandle == -1 )
141 nHandle = NEW_HANDLE_BASE + 1009;
142 while ( rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
143 ++nHandle;
146 return nHandle;
149 //--------------------------------------------------------------------
150 ::comphelper::OPropertyArrayAggregationHelper& PropertyBagHelper::impl_ts_getArrayHelper() const
152 OPropertyArrayAggregationHelper* p = m_pPropertyArrayHelper;
153 if ( !p )
155 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
156 p = m_pPropertyArrayHelper;
157 if ( !p )
159 // our own fixed and our aggregate's properties
160 Sequence< Property > aFixedProps;
161 Sequence< Property > aAggregateProps;
162 m_rContext.describeFixedAndAggregateProperties( aFixedProps, aAggregateProps );
164 // our dynamic properties
165 Sequence< Property > aDynamicProps;
166 m_aDynamicProperties.describeProperties( aDynamicProps );
168 Sequence< Property > aOwnProps(
169 ::comphelper::concatSequences( aFixedProps, aDynamicProps ) );
171 p = new OPropertyArrayAggregationHelper( aOwnProps, aAggregateProps, &lcl_getPropertyInfos(), NEW_HANDLE_BASE );
172 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
173 const_cast< PropertyBagHelper* >( this )->m_pPropertyArrayHelper = p;
175 } // if ( !p )
176 else
178 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
180 return *p;
183 //--------------------------------------------------------------------
184 void PropertyBagHelper::addProperty( const OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue )
186 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
187 impl_nts_checkDisposed_throw();
189 //----------------------------------------------
190 // check name sanity
191 ::comphelper::OPropertyArrayAggregationHelper& aPropInfo( impl_ts_getArrayHelper() );
192 if ( aPropInfo.hasPropertyByName( _rName ) )
193 throw PropertyExistException( _rName, m_rContext.getPropertiesInterface() );
195 //----------------------------------------------
196 // normalize the REMOVABLE attribute - the FormComponent service
197 // requires that all dynamic properties are REMOVABLE
198 _nAttributes |= PropertyAttribute::REMOVABLE;
200 //----------------------------------------------
201 // find a free handle
202 sal_Int32 nHandle = impl_findFreeHandle( _rName );
204 //----------------------------------------------
205 // register the property, and invalidate our property meta data
206 m_aDynamicProperties.addProperty( _rName, nHandle, _nAttributes, _rInitialValue );
207 impl_nts_invalidatePropertySetInfo();
210 //--------------------------------------------------------------------
211 void PropertyBagHelper::removeProperty( const OUString& _rName )
213 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
214 impl_nts_checkDisposed_throw();
216 // check whether it's removable at all
217 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
218 Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
219 Property aProperty( xPSI->getPropertyByName( _rName ) );
220 if ( ( aProperty.Attributes & PropertyAttribute::REMOVABLE ) == 0 )
221 throw NotRemoveableException( _rName, xMe );
223 m_aDynamicProperties.removeProperty( _rName );
224 impl_nts_invalidatePropertySetInfo();
227 //--------------------------------------------------------------------
228 namespace
230 //----------------------------------------------------------------
231 struct SelectNameOfProperty : public ::std::unary_function< Property, OUString >
233 const OUString& operator()( const Property& _rProp ) const { return _rProp.Name; }
236 //----------------------------------------------------------------
237 struct SelectNameOfPropertyValue : public ::std::unary_function< PropertyValue, OUString >
239 const OUString& operator()( const PropertyValue& _rProp ) const { return _rProp.Name; }
242 //----------------------------------------------------------------
243 struct SelectValueOfPropertyValue : public ::std::unary_function< PropertyValue, Any >
245 const Any& operator()( const PropertyValue& _rProp ) const { return _rProp.Value; }
248 //----------------------------------------------------------------
249 struct PropertyValueLessByName : public ::std::binary_function< PropertyValue, PropertyValue, bool >
251 bool operator()( const PropertyValue& _lhs, const PropertyValue _rhs ) const
253 return _lhs.Name < _rhs.Name;
258 //--------------------------------------------------------------------
259 Sequence< PropertyValue > PropertyBagHelper::getPropertyValues()
261 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
262 impl_nts_checkDisposed_throw();
264 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
265 Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
267 Sequence< Property > aProperties( xPSI->getProperties() );
268 Sequence< OUString > aPropertyNames( aProperties.getLength() );
269 ::std::transform( aProperties.getConstArray(), aProperties.getConstArray() + aProperties.getLength(),
270 aPropertyNames.getArray(), SelectNameOfProperty() );
272 Sequence< Any > aValues;
275 aValues = xMe->getPropertyValues( aPropertyNames );
277 if ( aValues.getLength() != aPropertyNames.getLength() )
278 throw RuntimeException();
280 catch( const RuntimeException& ) { throw; }
281 catch( const Exception& )
283 DBG_UNHANDLED_EXCEPTION();
285 Sequence< PropertyValue > aPropertyValues( aValues.getLength() );
286 PropertyValue* pPropertyValue = aPropertyValues.getArray();
288 const OUString* pName = aPropertyNames.getConstArray();
289 const OUString* pNameEnd = aPropertyNames.getConstArray() + aPropertyNames.getLength();
290 const Any* pValue = aValues.getConstArray();
291 for ( ; pName != pNameEnd; ++pName, ++pValue, ++pPropertyValue )
293 pPropertyValue->Name = *pName;
294 pPropertyValue->Value = *pValue;
297 return aPropertyValues;
300 //--------------------------------------------------------------------
301 void PropertyBagHelper::setPropertyValues( const Sequence< PropertyValue >& _rProps )
303 ::osl::ClearableMutexGuard aGuard( m_rContext.getMutex() );
304 impl_nts_checkDisposed_throw();
306 sal_Int32 nPropertyValues = _rProps.getLength();
308 // XMultiPropertySet::setPropertyValues expects its arguments to be sorted by name
309 // while XPropertyAccess::setPropertyValues doesn't. So first of all, sort.
310 Sequence< PropertyValue > aSortedProps( _rProps );
311 ::std::sort( aSortedProps.getArray(), aSortedProps.getArray() + nPropertyValues, PropertyValueLessByName() );
313 // also, XPropertyAccess::setPropertyValues is expected to throw an UnknownPropertyException
314 // for unsupported properties, while XMultiPropertySet::setPropertyValues is expected to ignore
315 // those. So, check for unsupported properties first.
316 ::comphelper::OPropertyArrayAggregationHelper& rArrayHelper( impl_ts_getArrayHelper() );
317 for ( const PropertyValue* pProperties = aSortedProps.getConstArray();
318 pProperties != aSortedProps.getConstArray() + nPropertyValues;
319 ++pProperties
322 if ( !rArrayHelper.hasPropertyByName( pProperties->Name ) )
323 throw UnknownPropertyException( pProperties->Name, m_rContext.getPropertiesInterface() );
326 // Now finally split into a Name and a Value sequence, and forward to
327 // XMultiPropertySet::setPropertyValues
328 Sequence< OUString > aNames( nPropertyValues );
329 ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
330 aNames.getArray(), SelectNameOfPropertyValue() );
332 Sequence< Any > aValues( nPropertyValues );
333 ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
334 aValues.getArray(), SelectValueOfPropertyValue() );
336 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
338 aGuard.clear();
339 xMe->setPropertyValues( aNames, aValues );
342 //........................................................................
343 } // namespace frm
344 //........................................................................
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */