merge the formfield patch from ooo-build
[ooovba.git] / forms / source / component / propertybaghelper.cxx
blob575418feaa642eb32d539cc96c8674f9a887a216
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: propertybaghelper.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_forms.hxx"
33 #include "propertybaghelper.hxx"
35 #include "property.hxx"
37 /** === begin UNO includes === **/
38 #include <com/sun/star/lang/DisposedException.hpp>
39 #include <com/sun/star/beans/PropertyExistException.hpp>
40 #include <com/sun/star/beans/XMultiPropertySet.hpp>
41 #include <com/sun/star/beans/NotRemoveableException.hpp>
42 #include <com/sun/star/beans/UnknownPropertyException.hpp>
43 /** === end UNO includes === **/
45 #include <tools/diagnose_ex.h>
47 #include <comphelper/sequence.hxx>
48 #include <rtl/logfile.hxx>
49 #include "rtl/instance.hxx"
52 #define NEW_HANDLE_BASE 10000
54 //........................................................................
55 namespace frm
57 //........................................................................
59 /** === begin UNO using === **/
60 using ::com::sun::star::lang::DisposedException;
61 using ::com::sun::star::uno::Sequence;
62 using ::com::sun::star::beans::Property;
63 using ::com::sun::star::uno::Any;
64 using ::com::sun::star::beans::PropertyExistException;
65 using ::com::sun::star::beans::PropertyValue;
66 using ::com::sun::star::uno::Reference;
67 using ::com::sun::star::uno::UNO_QUERY_THROW;
68 using ::com::sun::star::beans::XMultiPropertySet;
69 using ::com::sun::star::beans::XPropertySetInfo;
70 using ::com::sun::star::uno::RuntimeException;
71 using ::com::sun::star::uno::Exception;
72 using ::com::sun::star::beans::NotRemoveableException;
73 using ::com::sun::star::beans::UnknownPropertyException;
74 /** === end UNO using === **/
75 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
77 //====================================================================
78 //= helper
79 //====================================================================
80 namespace
82 //----------------------------------------------------------------
83 static ::comphelper::IPropertyInfoService& lcl_getPropertyInfos()
85 static ConcreteInfoService s_aPropInfos;
86 return s_aPropInfos;
90 //====================================================================
91 //= PropertyBagHelper
92 //====================================================================
93 //--------------------------------------------------------------------
94 PropertyBagHelper::PropertyBagHelper( IPropertyBagHelperContext& _rContext )
95 :m_rContext( _rContext )
96 ,m_pPropertyArrayHelper( NULL )
97 ,m_bDisposed( false )
99 // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::PropertyBagHelper" );
102 //--------------------------------------------------------------------
103 PropertyBagHelper::~PropertyBagHelper()
105 delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
108 //--------------------------------------------------------------------
109 void PropertyBagHelper::dispose()
111 // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::dispose" );
112 m_bDisposed = true;
115 //--------------------------------------------------------------------
116 void PropertyBagHelper::impl_nts_checkDisposed_throw() const
118 // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::impl_nts_checkDisposed_throw" );
119 if ( m_bDisposed )
120 throw DisposedException();
123 //--------------------------------------------------------------------
124 void PropertyBagHelper::impl_nts_invalidatePropertySetInfo()
126 // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::impl_nts_invalidatePropertySetInfo" );
127 delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
130 //--------------------------------------------------------------------
131 sal_Int32 PropertyBagHelper::impl_findFreeHandle( const ::rtl::OUString& _rPropertyName )
133 // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::impl_findFreeHandle" );
134 ::comphelper::OPropertyArrayAggregationHelper& rPropInfo( impl_ts_getArrayHelper() );
136 // check the preferred handle
137 sal_Int32 nHandle = lcl_getPropertyInfos().getPreferedPropertyId( _rPropertyName );
138 if ( ( nHandle != -1 ) && rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
139 nHandle = -1;
141 // seach a free handle in <math>F_1009</math>
142 if ( nHandle == -1 )
144 sal_Int32 nPrime = 1009;
145 sal_Int32 nFactor = 11;
146 sal_Int32 nNum = nFactor;
147 while ( nNum != 1 )
149 if ( !rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nNum + NEW_HANDLE_BASE ) )
151 // handle not used, yet
152 nHandle = nNum + NEW_HANDLE_BASE;
153 break;
155 nNum = ( nNum * nFactor ) % nPrime;
159 // search a free handle greater NEW_HANDLE_BASE
160 if ( nHandle == -1 )
162 nHandle = NEW_HANDLE_BASE + 1009;
163 while ( rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
164 ++nHandle;
167 return nHandle;
170 //--------------------------------------------------------------------
171 ::comphelper::OPropertyArrayAggregationHelper& PropertyBagHelper::impl_ts_getArrayHelper() const
173 // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::impl_ts_getArrayHelper" );
174 //::osl::MutexGuard aGuard( m_rContext.getMutex() );
175 OPropertyArrayAggregationHelper* p = m_pPropertyArrayHelper;
176 if ( !p )
178 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
179 p = m_pPropertyArrayHelper;
180 if ( !p )
182 // our own fixed and our aggregate's properties
183 Sequence< Property > aFixedProps;
184 Sequence< Property > aAggregateProps;
185 m_rContext.describeFixedAndAggregateProperties( aFixedProps, aAggregateProps );
187 // our dynamic properties
188 Sequence< Property > aDynamicProps;
189 m_aDynamicProperties.describeProperties( aDynamicProps );
191 Sequence< Property > aOwnProps(
192 ::comphelper::concatSequences( aFixedProps, aDynamicProps ) );
194 p = new OPropertyArrayAggregationHelper( aOwnProps, aAggregateProps, &lcl_getPropertyInfos(), NEW_HANDLE_BASE );
195 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
196 const_cast< PropertyBagHelper* >( this )->m_pPropertyArrayHelper = p;
198 } // if ( !p )
199 else
201 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
203 return *p;
206 //--------------------------------------------------------------------
207 void PropertyBagHelper::addProperty( const ::rtl::OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue )
209 // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::addProperty" );
210 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
211 impl_nts_checkDisposed_throw();
213 //----------------------------------------------
214 // check name sanity
215 ::comphelper::OPropertyArrayAggregationHelper& aPropInfo( impl_ts_getArrayHelper() );
216 if ( aPropInfo.hasPropertyByName( _rName ) )
217 throw PropertyExistException( _rName, m_rContext.getPropertiesInterface() );
219 //----------------------------------------------
220 // normalize the REMOVEABLE attribute - the FormComponent service
221 // requires that all dynamic properties are REMOVEABLE
222 _nAttributes |= PropertyAttribute::REMOVEABLE;
224 //----------------------------------------------
225 // find a free handle
226 sal_Int32 nHandle = impl_findFreeHandle( _rName );
228 //----------------------------------------------
229 // register the property, and invalidate our property meta data
230 m_aDynamicProperties.addProperty( _rName, nHandle, _nAttributes, _rInitialValue );
231 impl_nts_invalidatePropertySetInfo();
234 //--------------------------------------------------------------------
235 void PropertyBagHelper::removeProperty( const ::rtl::OUString& _rName )
237 // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::removeProperty" );
238 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
239 impl_nts_checkDisposed_throw();
241 // check whether it's removeable at all
242 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
243 Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
244 Property aProperty( xPSI->getPropertyByName( _rName ) );
245 if ( ( aProperty.Attributes & PropertyAttribute::REMOVEABLE ) == 0 )
246 throw NotRemoveableException( _rName, xMe );
248 m_aDynamicProperties.removeProperty( _rName );
249 impl_nts_invalidatePropertySetInfo();
252 //--------------------------------------------------------------------
253 namespace
255 //----------------------------------------------------------------
256 struct SelectNameOfProperty : public ::std::unary_function< Property, ::rtl::OUString >
258 const ::rtl::OUString& operator()( const Property& _rProp ) const { return _rProp.Name; }
261 //----------------------------------------------------------------
262 struct SelectNameOfPropertyValue : public ::std::unary_function< PropertyValue, ::rtl::OUString >
264 const ::rtl::OUString& operator()( const PropertyValue& _rProp ) const { return _rProp.Name; }
267 //----------------------------------------------------------------
268 struct SelectValueOfPropertyValue : public ::std::unary_function< PropertyValue, Any >
270 const Any& operator()( const PropertyValue& _rProp ) const { return _rProp.Value; }
273 //----------------------------------------------------------------
274 struct PropertyValueLessByName : public ::std::binary_function< PropertyValue, PropertyValue, bool >
276 bool operator()( const PropertyValue& _lhs, const PropertyValue _rhs ) const
278 return _lhs.Name < _rhs.Name;
283 //--------------------------------------------------------------------
284 Sequence< PropertyValue > PropertyBagHelper::getPropertyValues()
286 // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::getPropertyValues" );
287 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
288 impl_nts_checkDisposed_throw();
290 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
291 Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
293 Sequence< Property > aProperties( xPSI->getProperties() );
294 Sequence< ::rtl::OUString > aPropertyNames( aProperties.getLength() );
295 ::std::transform( aProperties.getConstArray(), aProperties.getConstArray() + aProperties.getLength(),
296 aPropertyNames.getArray(), SelectNameOfProperty() );
298 Sequence< Any > aValues;
301 aValues = xMe->getPropertyValues( aPropertyNames );
303 if ( aValues.getLength() != aPropertyNames.getLength() )
304 throw RuntimeException();
306 catch( const RuntimeException& ) { throw; }
307 catch( const Exception& )
309 DBG_UNHANDLED_EXCEPTION();
311 Sequence< PropertyValue > aPropertyValues( aValues.getLength() );
312 PropertyValue* pPropertyValue = aPropertyValues.getArray();
314 const ::rtl::OUString* pName = aPropertyNames.getConstArray();
315 const ::rtl::OUString* pNameEnd = aPropertyNames.getConstArray() + aPropertyNames.getLength();
316 const Any* pValue = aValues.getConstArray();
317 for ( ; pName != pNameEnd; ++pName, ++pValue, ++pPropertyValue )
319 pPropertyValue->Name = *pName;
320 pPropertyValue->Value = *pValue;
323 return aPropertyValues;
326 //--------------------------------------------------------------------
327 void PropertyBagHelper::setPropertyValues( const Sequence< PropertyValue >& _rProps )
329 // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::setPropertyValues" );
330 ::osl::ClearableMutexGuard aGuard( m_rContext.getMutex() );
331 impl_nts_checkDisposed_throw();
333 sal_Int32 nPropertyValues = _rProps.getLength();
335 // XMultiPropertySet::setPropertyValues expects its arguments to be sorted by name
336 // while XPropertyAccess::setPropertyValues doesn't. So first of all, sort.
337 Sequence< PropertyValue > aSortedProps( _rProps );
338 ::std::sort( aSortedProps.getArray(), aSortedProps.getArray() + nPropertyValues, PropertyValueLessByName() );
340 // also, XPropertyAccess::setPropertyValues is expected to throw an UnknownPropertyException
341 // for unsupported properties, while XMultiPropertySet::setPropertyValues is expected to ignore
342 // those. So, check for unsupported properties first.
343 ::comphelper::OPropertyArrayAggregationHelper& rArrayHelper( impl_ts_getArrayHelper() );
344 for ( const PropertyValue* pProperties = aSortedProps.getConstArray();
345 pProperties != aSortedProps.getConstArray() + nPropertyValues;
346 ++pProperties
349 if ( !rArrayHelper.hasPropertyByName( pProperties->Name ) )
350 throw UnknownPropertyException( pProperties->Name, m_rContext.getPropertiesInterface() );
353 // Now finally split into a Name and a Value sequence, and forward to
354 // XMultiPropertySet::setPropertyValues
355 Sequence< ::rtl::OUString > aNames( nPropertyValues );
356 ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
357 aNames.getArray(), SelectNameOfPropertyValue() );
359 Sequence< Any > aValues( nPropertyValues );
360 ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
361 aValues.getArray(), SelectValueOfPropertyValue() );
363 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
365 aGuard.clear();
366 xMe->setPropertyValues( aNames, aValues );
369 //........................................................................
370 } // namespace frm
371 //........................................................................