LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / forms / source / component / propertybaghelper.cxx
blobf5c77c707a99eb857cca55240c7dbde4bbfde075
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>
35 #define NEW_HANDLE_BASE 10000
38 namespace frm
42 using ::com::sun::star::lang::DisposedException;
43 using ::com::sun::star::uno::Sequence;
44 using ::com::sun::star::beans::Property;
45 using ::com::sun::star::uno::Any;
46 using ::com::sun::star::beans::PropertyExistException;
47 using ::com::sun::star::beans::PropertyValue;
48 using ::com::sun::star::uno::Reference;
49 using ::com::sun::star::uno::UNO_QUERY_THROW;
50 using ::com::sun::star::beans::XMultiPropertySet;
51 using ::com::sun::star::beans::XPropertySetInfo;
52 using ::com::sun::star::uno::RuntimeException;
53 using ::com::sun::star::uno::Exception;
54 using ::com::sun::star::beans::NotRemoveableException;
55 using ::com::sun::star::beans::UnknownPropertyException;
57 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
60 //= helper
62 namespace
65 ::comphelper::IPropertyInfoService& lcl_getPropertyInfos()
67 static ConcreteInfoService s_aPropInfos;
68 return s_aPropInfos;
72 PropertyBagHelper::PropertyBagHelper( IPropertyBagHelperContext& _rContext )
73 :m_rContext( _rContext )
74 ,m_bDisposed( false )
79 PropertyBagHelper::~PropertyBagHelper()
84 void PropertyBagHelper::dispose()
86 m_bDisposed = true;
90 void PropertyBagHelper::impl_nts_checkDisposed_throw() const
92 if ( m_bDisposed )
93 throw DisposedException();
97 void PropertyBagHelper::impl_nts_invalidatePropertySetInfo()
99 m_pPropertyArrayHelper.reset();
103 sal_Int32 PropertyBagHelper::impl_findFreeHandle( const OUString& _rPropertyName )
105 ::comphelper::OPropertyArrayAggregationHelper& rPropInfo( impl_ts_getArrayHelper() );
107 // check the preferred handle
108 sal_Int32 nHandle = lcl_getPropertyInfos().getPreferredPropertyId( _rPropertyName );
109 if ( ( nHandle != -1 ) && rPropInfo.fillPropertyMembersByHandle( nullptr, nullptr, nHandle ) )
110 nHandle = -1;
112 // search a free handle in <math>F_1009</math>
113 if ( nHandle == -1 )
115 sal_Int32 const nPrime = 1009;
116 sal_Int32 nFactor = 11;
117 sal_Int32 nNum = nFactor;
118 while ( nNum != 1 )
120 if ( !rPropInfo.fillPropertyMembersByHandle( nullptr, nullptr, nNum + NEW_HANDLE_BASE ) )
122 // handle not used, yet
123 nHandle = nNum + NEW_HANDLE_BASE;
124 break;
126 nNum = ( nNum * nFactor ) % nPrime;
130 // search a free handle greater NEW_HANDLE_BASE
131 if ( nHandle == -1 )
133 nHandle = NEW_HANDLE_BASE + 1009;
134 while ( rPropInfo.fillPropertyMembersByHandle( nullptr, nullptr, nHandle ) )
135 ++nHandle;
138 return nHandle;
142 ::comphelper::OPropertyArrayAggregationHelper& PropertyBagHelper::impl_ts_getArrayHelper() const
144 OPropertyArrayAggregationHelper* p = m_pPropertyArrayHelper.get();
145 if ( !p )
147 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
148 p = m_pPropertyArrayHelper.get();
149 if ( !p )
151 // our own fixed and our aggregate's properties
152 Sequence< Property > aFixedProps;
153 Sequence< Property > aAggregateProps;
154 m_rContext.describeFixedAndAggregateProperties( aFixedProps, aAggregateProps );
156 // our dynamic properties
157 Sequence< Property > aDynamicProps;
158 m_aDynamicProperties.describeProperties( aDynamicProps );
160 Sequence< Property > aOwnProps(
161 ::comphelper::concatSequences( aFixedProps, aDynamicProps ) );
163 p = new OPropertyArrayAggregationHelper( aOwnProps, aAggregateProps, &lcl_getPropertyInfos(), NEW_HANDLE_BASE );
164 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
165 const_cast< PropertyBagHelper* >( this )->m_pPropertyArrayHelper.reset( p );
167 } // if ( !p )
168 else
170 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
172 return *p;
176 void PropertyBagHelper::addProperty( const OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue )
178 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
179 impl_nts_checkDisposed_throw();
182 // check name sanity
183 ::comphelper::OPropertyArrayAggregationHelper& aPropInfo( impl_ts_getArrayHelper() );
184 if ( aPropInfo.hasPropertyByName( _rName ) )
185 throw PropertyExistException( _rName, m_rContext.getPropertiesInterface() );
188 // normalize the REMOVABLE attribute - the FormComponent service
189 // requires that all dynamic properties are REMOVABLE
190 _nAttributes |= PropertyAttribute::REMOVABLE;
193 // find a free handle
194 sal_Int32 nHandle = impl_findFreeHandle( _rName );
197 // register the property, and invalidate our property meta data
198 m_aDynamicProperties.addProperty( _rName, nHandle, _nAttributes, _rInitialValue );
199 impl_nts_invalidatePropertySetInfo();
203 void PropertyBagHelper::removeProperty( const OUString& _rName )
205 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
206 impl_nts_checkDisposed_throw();
208 // check whether it's removable at all
209 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), css::uno::UNO_SET_THROW );
210 Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), css::uno::UNO_SET_THROW );
211 Property aProperty( xPSI->getPropertyByName( _rName ) );
212 if ( ( aProperty.Attributes & PropertyAttribute::REMOVABLE ) == 0 )
213 throw NotRemoveableException( _rName, xMe );
215 m_aDynamicProperties.removeProperty( _rName );
216 impl_nts_invalidatePropertySetInfo();
220 namespace
223 struct SelectNameOfProperty
225 const OUString& operator()( const Property& _rProp ) const { return _rProp.Name; }
229 struct SelectNameOfPropertyValue
231 const OUString& operator()( const PropertyValue& _rProp ) const { return _rProp.Name; }
235 struct SelectValueOfPropertyValue
237 const Any& operator()( const PropertyValue& _rProp ) const { return _rProp.Value; }
241 struct PropertyValueLessByName
243 bool operator()( const PropertyValue& _lhs, const PropertyValue& _rhs ) const
245 return _lhs.Name < _rhs.Name;
251 Sequence< PropertyValue > PropertyBagHelper::getPropertyValues()
253 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
254 impl_nts_checkDisposed_throw();
256 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), css::uno::UNO_SET_THROW );
257 Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), css::uno::UNO_SET_THROW );
259 const Sequence< Property > aProperties( xPSI->getProperties() );
260 Sequence< OUString > aPropertyNames( aProperties.getLength() );
261 ::std::transform( aProperties.begin(), aProperties.end(),
262 aPropertyNames.getArray(), SelectNameOfProperty() );
264 Sequence< Any > aValues;
267 aValues = xMe->getPropertyValues( aPropertyNames );
269 if ( aValues.getLength() != aPropertyNames.getLength() )
270 throw RuntimeException();
272 catch( const RuntimeException& ) { throw; }
273 catch( const Exception& )
275 DBG_UNHANDLED_EXCEPTION("forms.component");
277 Sequence< PropertyValue > aPropertyValues( aValues.getLength() );
278 PropertyValue* pPropertyValue = aPropertyValues.getArray();
280 const OUString* pName = aPropertyNames.getConstArray();
281 const OUString* pNameEnd = aPropertyNames.getConstArray() + aPropertyNames.getLength();
282 const Any* pValue = aValues.getConstArray();
283 for ( ; pName != pNameEnd; ++pName, ++pValue, ++pPropertyValue )
285 pPropertyValue->Name = *pName;
286 pPropertyValue->Value = *pValue;
289 return aPropertyValues;
293 void PropertyBagHelper::setPropertyValues( const Sequence< PropertyValue >& _rProps )
295 ::osl::ClearableMutexGuard aGuard( m_rContext.getMutex() );
296 impl_nts_checkDisposed_throw();
298 sal_Int32 nPropertyValues = _rProps.getLength();
300 // XMultiPropertySet::setPropertyValues expects its arguments to be sorted by name
301 // while XPropertyAccess::setPropertyValues doesn't. So first of all, sort.
302 Sequence< PropertyValue > aSortedProps( _rProps );
303 ::std::sort( aSortedProps.getArray(), aSortedProps.getArray() + nPropertyValues, PropertyValueLessByName() );
305 // also, XPropertyAccess::setPropertyValues is expected to throw an UnknownPropertyException
306 // for unsupported properties, while XMultiPropertySet::setPropertyValues is expected to ignore
307 // those. So, check for unsupported properties first.
308 ::comphelper::OPropertyArrayAggregationHelper& rArrayHelper( impl_ts_getArrayHelper() );
309 for ( const PropertyValue* pProperties = aSortedProps.getConstArray();
310 pProperties != aSortedProps.getConstArray() + nPropertyValues;
311 ++pProperties
314 if ( !rArrayHelper.hasPropertyByName( pProperties->Name ) )
315 throw UnknownPropertyException( pProperties->Name, m_rContext.getPropertiesInterface() );
318 // Now finally split into a Name and a Value sequence, and forward to
319 // XMultiPropertySet::setPropertyValues
320 Sequence< OUString > aNames( nPropertyValues );
321 ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
322 aNames.getArray(), SelectNameOfPropertyValue() );
324 Sequence< Any > aValues( nPropertyValues );
325 ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
326 aValues.getArray(), SelectValueOfPropertyValue() );
328 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), css::uno::UNO_SET_THROW );
330 aGuard.clear();
331 xMe->setPropertyValues( aNames, aValues );
335 } // namespace frm
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */