1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_forms.hxx"
30*cdf0e10cSrcweir #include "propertybaghelper.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include "property.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir /** === begin UNO includes === **/
35*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyExistException.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/beans/XMultiPropertySet.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/beans/NotRemoveableException.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/beans/UnknownPropertyException.hpp>
40*cdf0e10cSrcweir /** === end UNO includes === **/
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <comphelper/sequence.hxx>
45*cdf0e10cSrcweir #include <rtl/logfile.hxx>
46*cdf0e10cSrcweir #include "rtl/instance.hxx"
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #define NEW_HANDLE_BASE 10000
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //........................................................................
52*cdf0e10cSrcweir namespace frm
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir //........................................................................
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 	/** === begin UNO using === **/
57*cdf0e10cSrcweir     using ::com::sun::star::lang::DisposedException;
58*cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
59*cdf0e10cSrcweir     using ::com::sun::star::beans::Property;
60*cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
61*cdf0e10cSrcweir     using ::com::sun::star::beans::PropertyExistException;
62*cdf0e10cSrcweir     using ::com::sun::star::beans::PropertyValue;
63*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
64*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
65*cdf0e10cSrcweir     using ::com::sun::star::beans::XMultiPropertySet;
66*cdf0e10cSrcweir     using ::com::sun::star::beans::XPropertySetInfo;
67*cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
68*cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
69*cdf0e10cSrcweir     using ::com::sun::star::beans::NotRemoveableException;
70*cdf0e10cSrcweir     using ::com::sun::star::beans::UnknownPropertyException;
71*cdf0e10cSrcweir 	/** === end UNO using === **/
72*cdf0e10cSrcweir     namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 	//====================================================================
75*cdf0e10cSrcweir 	//= helper
76*cdf0e10cSrcweir 	//====================================================================
77*cdf0e10cSrcweir     namespace
78*cdf0e10cSrcweir     {
79*cdf0e10cSrcweir     	//----------------------------------------------------------------
80*cdf0e10cSrcweir         static ::comphelper::IPropertyInfoService& lcl_getPropertyInfos()
81*cdf0e10cSrcweir         {
82*cdf0e10cSrcweir 	        static ConcreteInfoService s_aPropInfos;
83*cdf0e10cSrcweir             return s_aPropInfos;
84*cdf0e10cSrcweir         }
85*cdf0e10cSrcweir     }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 	//====================================================================
88*cdf0e10cSrcweir 	//= PropertyBagHelper
89*cdf0e10cSrcweir 	//====================================================================
90*cdf0e10cSrcweir 	//--------------------------------------------------------------------
91*cdf0e10cSrcweir     PropertyBagHelper::PropertyBagHelper( IPropertyBagHelperContext& _rContext )
92*cdf0e10cSrcweir         :m_rContext( _rContext )
93*cdf0e10cSrcweir         ,m_pPropertyArrayHelper( NULL )
94*cdf0e10cSrcweir         ,m_bDisposed( false )
95*cdf0e10cSrcweir     {
96*cdf0e10cSrcweir         // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::PropertyBagHelper" );
97*cdf0e10cSrcweir     }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 	//--------------------------------------------------------------------
100*cdf0e10cSrcweir     PropertyBagHelper::~PropertyBagHelper()
101*cdf0e10cSrcweir     {
102*cdf0e10cSrcweir         delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     //--------------------------------------------------------------------
106*cdf0e10cSrcweir     void PropertyBagHelper::dispose()
107*cdf0e10cSrcweir     {
108*cdf0e10cSrcweir         // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::dispose" );
109*cdf0e10cSrcweir         m_bDisposed = true;
110*cdf0e10cSrcweir     }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     //--------------------------------------------------------------------
113*cdf0e10cSrcweir     void PropertyBagHelper::impl_nts_checkDisposed_throw() const
114*cdf0e10cSrcweir     {
115*cdf0e10cSrcweir         // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::impl_nts_checkDisposed_throw" );
116*cdf0e10cSrcweir         if ( m_bDisposed )
117*cdf0e10cSrcweir             throw DisposedException();
118*cdf0e10cSrcweir     }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     //--------------------------------------------------------------------
121*cdf0e10cSrcweir     void PropertyBagHelper::impl_nts_invalidatePropertySetInfo()
122*cdf0e10cSrcweir     {
123*cdf0e10cSrcweir         // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::impl_nts_invalidatePropertySetInfo" );
124*cdf0e10cSrcweir         delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     //--------------------------------------------------------------------
128*cdf0e10cSrcweir     sal_Int32 PropertyBagHelper::impl_findFreeHandle( const ::rtl::OUString& _rPropertyName )
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir         // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::impl_findFreeHandle" );
131*cdf0e10cSrcweir         ::comphelper::OPropertyArrayAggregationHelper& rPropInfo( impl_ts_getArrayHelper() );
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir         // check the preferred handle
134*cdf0e10cSrcweir         sal_Int32 nHandle = lcl_getPropertyInfos().getPreferedPropertyId( _rPropertyName );
135*cdf0e10cSrcweir         if ( ( nHandle != -1 ) && rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
136*cdf0e10cSrcweir             nHandle = -1;
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         // seach a free handle in <math>F_1009</math>
139*cdf0e10cSrcweir         if ( nHandle == -1 )
140*cdf0e10cSrcweir         {
141*cdf0e10cSrcweir             sal_Int32 nPrime = 1009;
142*cdf0e10cSrcweir             sal_Int32 nFactor = 11;
143*cdf0e10cSrcweir             sal_Int32 nNum = nFactor;
144*cdf0e10cSrcweir             while ( nNum != 1 )
145*cdf0e10cSrcweir             {
146*cdf0e10cSrcweir                 if ( !rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nNum + NEW_HANDLE_BASE ) )
147*cdf0e10cSrcweir                 {
148*cdf0e10cSrcweir                     // handle not used, yet
149*cdf0e10cSrcweir                     nHandle = nNum + NEW_HANDLE_BASE;
150*cdf0e10cSrcweir                     break;
151*cdf0e10cSrcweir                 }
152*cdf0e10cSrcweir                 nNum = ( nNum * nFactor ) % nPrime;
153*cdf0e10cSrcweir             }
154*cdf0e10cSrcweir         }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir         // search a free handle greater NEW_HANDLE_BASE
157*cdf0e10cSrcweir         if ( nHandle == -1 )
158*cdf0e10cSrcweir         {
159*cdf0e10cSrcweir             nHandle = NEW_HANDLE_BASE + 1009;
160*cdf0e10cSrcweir             while ( rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
161*cdf0e10cSrcweir                 ++nHandle;
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir         return nHandle;
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     //--------------------------------------------------------------------
168*cdf0e10cSrcweir     ::comphelper::OPropertyArrayAggregationHelper& PropertyBagHelper::impl_ts_getArrayHelper() const
169*cdf0e10cSrcweir     {
170*cdf0e10cSrcweir         // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::impl_ts_getArrayHelper" );
171*cdf0e10cSrcweir         //::osl::MutexGuard aGuard( m_rContext.getMutex() );
172*cdf0e10cSrcweir         OPropertyArrayAggregationHelper* p = m_pPropertyArrayHelper;
173*cdf0e10cSrcweir         if ( !p )
174*cdf0e10cSrcweir         {
175*cdf0e10cSrcweir             ::osl::MutexGuard aGuard( m_rContext.getMutex() );
176*cdf0e10cSrcweir             p = m_pPropertyArrayHelper;
177*cdf0e10cSrcweir             if ( !p )
178*cdf0e10cSrcweir             {
179*cdf0e10cSrcweir                 // our own fixed and our aggregate's properties
180*cdf0e10cSrcweir 	            Sequence< Property > aFixedProps;
181*cdf0e10cSrcweir 	            Sequence< Property > aAggregateProps;
182*cdf0e10cSrcweir                 m_rContext.describeFixedAndAggregateProperties( aFixedProps, aAggregateProps );
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir                 // our dynamic properties
185*cdf0e10cSrcweir                 Sequence< Property > aDynamicProps;
186*cdf0e10cSrcweir                 m_aDynamicProperties.describeProperties( aDynamicProps );
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir                 Sequence< Property > aOwnProps(
189*cdf0e10cSrcweir                     ::comphelper::concatSequences( aFixedProps, aDynamicProps ) );
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir                 p = new OPropertyArrayAggregationHelper( aOwnProps, aAggregateProps, &lcl_getPropertyInfos(), NEW_HANDLE_BASE );
192*cdf0e10cSrcweir                 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
193*cdf0e10cSrcweir                 const_cast< PropertyBagHelper* >( this )->m_pPropertyArrayHelper = p;
194*cdf0e10cSrcweir             }
195*cdf0e10cSrcweir         } // if ( !p )
196*cdf0e10cSrcweir         else
197*cdf0e10cSrcweir         {
198*cdf0e10cSrcweir             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
199*cdf0e10cSrcweir         }
200*cdf0e10cSrcweir         return *p;
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     //--------------------------------------------------------------------
204*cdf0e10cSrcweir     void PropertyBagHelper::addProperty( const ::rtl::OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue )
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir         // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::addProperty" );
207*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_rContext.getMutex() );
208*cdf0e10cSrcweir         impl_nts_checkDisposed_throw();
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir         //----------------------------------------------
211*cdf0e10cSrcweir         // check name sanity
212*cdf0e10cSrcweir         ::comphelper::OPropertyArrayAggregationHelper& aPropInfo( impl_ts_getArrayHelper() );
213*cdf0e10cSrcweir         if ( aPropInfo.hasPropertyByName( _rName ) )
214*cdf0e10cSrcweir             throw PropertyExistException( _rName, m_rContext.getPropertiesInterface() );
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir         //----------------------------------------------
217*cdf0e10cSrcweir         // normalize the REMOVEABLE attribute - the FormComponent service
218*cdf0e10cSrcweir         // requires that all dynamic properties are REMOVEABLE
219*cdf0e10cSrcweir         _nAttributes |= PropertyAttribute::REMOVEABLE;
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir         //----------------------------------------------
222*cdf0e10cSrcweir         // find a free handle
223*cdf0e10cSrcweir         sal_Int32 nHandle = impl_findFreeHandle( _rName );
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir         //----------------------------------------------
226*cdf0e10cSrcweir         // register the property, and invalidate our property meta data
227*cdf0e10cSrcweir         m_aDynamicProperties.addProperty( _rName, nHandle, _nAttributes, _rInitialValue );
228*cdf0e10cSrcweir         impl_nts_invalidatePropertySetInfo();
229*cdf0e10cSrcweir     }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir     //--------------------------------------------------------------------
232*cdf0e10cSrcweir     void PropertyBagHelper::removeProperty( const ::rtl::OUString& _rName )
233*cdf0e10cSrcweir     {
234*cdf0e10cSrcweir         // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::removeProperty" );
235*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_rContext.getMutex() );
236*cdf0e10cSrcweir         impl_nts_checkDisposed_throw();
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir         // check whether it's removeable at all
239*cdf0e10cSrcweir         Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
240*cdf0e10cSrcweir         Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
241*cdf0e10cSrcweir         Property aProperty( xPSI->getPropertyByName( _rName ) );
242*cdf0e10cSrcweir         if ( ( aProperty.Attributes & PropertyAttribute::REMOVEABLE ) == 0 )
243*cdf0e10cSrcweir             throw NotRemoveableException( _rName, xMe );
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir         m_aDynamicProperties.removeProperty( _rName );
246*cdf0e10cSrcweir         impl_nts_invalidatePropertySetInfo();
247*cdf0e10cSrcweir     }
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir     //--------------------------------------------------------------------
250*cdf0e10cSrcweir     namespace
251*cdf0e10cSrcweir     {
252*cdf0e10cSrcweir         //----------------------------------------------------------------
253*cdf0e10cSrcweir         struct SelectNameOfProperty : public ::std::unary_function< Property, ::rtl::OUString >
254*cdf0e10cSrcweir         {
255*cdf0e10cSrcweir             const ::rtl::OUString& operator()( const Property& _rProp ) const { return _rProp.Name; }
256*cdf0e10cSrcweir         };
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir         //----------------------------------------------------------------
259*cdf0e10cSrcweir         struct SelectNameOfPropertyValue : public ::std::unary_function< PropertyValue, ::rtl::OUString >
260*cdf0e10cSrcweir         {
261*cdf0e10cSrcweir             const ::rtl::OUString& operator()( const PropertyValue& _rProp ) const { return _rProp.Name; }
262*cdf0e10cSrcweir         };
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir         //----------------------------------------------------------------
265*cdf0e10cSrcweir         struct SelectValueOfPropertyValue : public ::std::unary_function< PropertyValue, Any >
266*cdf0e10cSrcweir         {
267*cdf0e10cSrcweir             const Any& operator()( const PropertyValue& _rProp ) const { return _rProp.Value; }
268*cdf0e10cSrcweir         };
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir         //----------------------------------------------------------------
271*cdf0e10cSrcweir         struct PropertyValueLessByName : public ::std::binary_function< PropertyValue, PropertyValue, bool >
272*cdf0e10cSrcweir         {
273*cdf0e10cSrcweir             bool operator()( const PropertyValue& _lhs, const PropertyValue _rhs ) const
274*cdf0e10cSrcweir             {
275*cdf0e10cSrcweir                 return _lhs.Name < _rhs.Name;
276*cdf0e10cSrcweir             }
277*cdf0e10cSrcweir         };
278*cdf0e10cSrcweir     }
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir     //--------------------------------------------------------------------
281*cdf0e10cSrcweir     Sequence< PropertyValue > PropertyBagHelper::getPropertyValues()
282*cdf0e10cSrcweir     {
283*cdf0e10cSrcweir         // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::getPropertyValues" );
284*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_rContext.getMutex() );
285*cdf0e10cSrcweir         impl_nts_checkDisposed_throw();
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir         Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
288*cdf0e10cSrcweir         Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir         Sequence< Property > aProperties( xPSI->getProperties() );
291*cdf0e10cSrcweir         Sequence< ::rtl::OUString > aPropertyNames( aProperties.getLength() );
292*cdf0e10cSrcweir         ::std::transform( aProperties.getConstArray(), aProperties.getConstArray() + aProperties.getLength(),
293*cdf0e10cSrcweir             aPropertyNames.getArray(), SelectNameOfProperty() );
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir         Sequence< Any > aValues;
296*cdf0e10cSrcweir         try
297*cdf0e10cSrcweir         {
298*cdf0e10cSrcweir             aValues = xMe->getPropertyValues( aPropertyNames );
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir             if ( aValues.getLength() != aPropertyNames.getLength() )
301*cdf0e10cSrcweir                 throw RuntimeException();
302*cdf0e10cSrcweir         }
303*cdf0e10cSrcweir         catch( const RuntimeException& ) { throw; }
304*cdf0e10cSrcweir         catch( const Exception& )
305*cdf0e10cSrcweir         {
306*cdf0e10cSrcweir     	    DBG_UNHANDLED_EXCEPTION();
307*cdf0e10cSrcweir         }
308*cdf0e10cSrcweir         Sequence< PropertyValue > aPropertyValues( aValues.getLength() );
309*cdf0e10cSrcweir         PropertyValue* pPropertyValue = aPropertyValues.getArray();
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir         const ::rtl::OUString* pName = aPropertyNames.getConstArray();
312*cdf0e10cSrcweir         const ::rtl::OUString* pNameEnd = aPropertyNames.getConstArray() + aPropertyNames.getLength();
313*cdf0e10cSrcweir         const Any* pValue = aValues.getConstArray();
314*cdf0e10cSrcweir         for ( ; pName != pNameEnd; ++pName, ++pValue, ++pPropertyValue )
315*cdf0e10cSrcweir         {
316*cdf0e10cSrcweir             pPropertyValue->Name = *pName;
317*cdf0e10cSrcweir             pPropertyValue->Value = *pValue;
318*cdf0e10cSrcweir         }
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir         return aPropertyValues;
321*cdf0e10cSrcweir     }
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir     //--------------------------------------------------------------------
324*cdf0e10cSrcweir     void PropertyBagHelper::setPropertyValues( const Sequence< PropertyValue >& _rProps )
325*cdf0e10cSrcweir     {
326*cdf0e10cSrcweir         // RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "forms", "dev@dba.openoffice.org", "PropertyBagHelper::setPropertyValues" );
327*cdf0e10cSrcweir         ::osl::ClearableMutexGuard aGuard( m_rContext.getMutex() );
328*cdf0e10cSrcweir         impl_nts_checkDisposed_throw();
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir         sal_Int32 nPropertyValues = _rProps.getLength();
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir         // XMultiPropertySet::setPropertyValues expects its arguments to be sorted by name
333*cdf0e10cSrcweir         // while XPropertyAccess::setPropertyValues doesn't. So first of all, sort.
334*cdf0e10cSrcweir         Sequence< PropertyValue > aSortedProps( _rProps );
335*cdf0e10cSrcweir         ::std::sort( aSortedProps.getArray(), aSortedProps.getArray() + nPropertyValues, PropertyValueLessByName() );
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir         // also, XPropertyAccess::setPropertyValues is expected to throw an UnknownPropertyException
338*cdf0e10cSrcweir         // for unsupported properties, while XMultiPropertySet::setPropertyValues is expected to ignore
339*cdf0e10cSrcweir         // those. So, check for unsupported properties first.
340*cdf0e10cSrcweir         ::comphelper::OPropertyArrayAggregationHelper& rArrayHelper( impl_ts_getArrayHelper() );
341*cdf0e10cSrcweir         for (   const PropertyValue* pProperties = aSortedProps.getConstArray();
342*cdf0e10cSrcweir                 pProperties != aSortedProps.getConstArray() + nPropertyValues;
343*cdf0e10cSrcweir                 ++pProperties
344*cdf0e10cSrcweir             )
345*cdf0e10cSrcweir         {
346*cdf0e10cSrcweir             if ( !rArrayHelper.hasPropertyByName( pProperties->Name ) )
347*cdf0e10cSrcweir                 throw UnknownPropertyException( pProperties->Name, m_rContext.getPropertiesInterface() );
348*cdf0e10cSrcweir         }
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir         // Now finally split into a Name and a Value sequence, and forward to
351*cdf0e10cSrcweir         // XMultiPropertySet::setPropertyValues
352*cdf0e10cSrcweir         Sequence< ::rtl::OUString > aNames( nPropertyValues );
353*cdf0e10cSrcweir         ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
354*cdf0e10cSrcweir             aNames.getArray(), SelectNameOfPropertyValue() );
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir         Sequence< Any > aValues( nPropertyValues );
357*cdf0e10cSrcweir         ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
358*cdf0e10cSrcweir             aValues.getArray(), SelectValueOfPropertyValue() );
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir         Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir         aGuard.clear();
363*cdf0e10cSrcweir         xMe->setPropertyValues( aNames, aValues );
364*cdf0e10cSrcweir     }
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir //........................................................................
367*cdf0e10cSrcweir } // namespace frm
368*cdf0e10cSrcweir //........................................................................
369*cdf0e10cSrcweir 
370