LibreOffice Module forms (master) 1
propertybaghelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <propertybaghelper.hxx>
21
22#include <property.hxx>
23
24#include <com/sun/star/lang/DisposedException.hpp>
25#include <com/sun/star/beans/PropertyAttribute.hpp>
26#include <com/sun/star/beans/PropertyExistException.hpp>
27#include <com/sun/star/beans/XMultiPropertySet.hpp>
28#include <com/sun/star/beans/NotRemoveableException.hpp>
29#include <com/sun/star/beans/UnknownPropertyException.hpp>
30
32
34
35
36#define NEW_HANDLE_BASE 10000
37
38
39namespace frm
40{
41
42
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::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;
56
57 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
58
59
60 //= helper
61
62 namespace
63 {
64
65 ::comphelper::IPropertyInfoService& lcl_getPropertyInfos()
66 {
67 static ConcreteInfoService s_aPropInfos;
68 return s_aPropInfos;
69 }
70 }
71
73 :m_rContext( _rContext )
74 ,m_bDisposed( false )
75 {
76 }
77
78
80 {
81 }
82
83
85 {
86 m_bDisposed = true;
87 }
88
89
91 {
92 if ( m_bDisposed )
93 throw DisposedException();
94 }
95
96
98 {
100 }
101
102
103 sal_Int32 PropertyBagHelper::impl_findFreeHandle( const OUString& _rPropertyName )
104 {
106
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;
111
112 // search a free handle in <math>F_1009</math>
113 if ( nHandle == -1 )
114 {
115 sal_Int32 const nPrime = 1009;
116 sal_Int32 nFactor = 11;
117 sal_Int32 nNum = nFactor;
118 while ( nNum != 1 )
119 {
120 if ( !rPropInfo.fillPropertyMembersByHandle( nullptr, nullptr, nNum + NEW_HANDLE_BASE ) )
121 {
122 // handle not used, yet
123 nHandle = nNum + NEW_HANDLE_BASE;
124 break;
125 }
126 nNum = ( nNum * nFactor ) % nPrime;
127 }
128 }
129
130 // search a free handle greater NEW_HANDLE_BASE
131 if ( nHandle == -1 )
132 {
133 nHandle = NEW_HANDLE_BASE + 1009;
134 while ( rPropInfo.fillPropertyMembersByHandle( nullptr, nullptr, nHandle ) )
135 ++nHandle;
136 }
137
138 return nHandle;
139 }
140
141
143 {
145 if ( !p )
146 {
147 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
149 if ( !p )
150 {
151 // our own fixed and our aggregate's properties
152 Sequence< Property > aFixedProps;
153 Sequence< Property > aAggregateProps;
154 m_rContext.describeFixedAndAggregateProperties( aFixedProps, aAggregateProps );
155
156 // our dynamic properties
157 Sequence< Property > aDynamicProps;
158 m_aDynamicProperties.describeProperties( aDynamicProps );
159
160 Sequence< Property > aOwnProps(
161 ::comphelper::concatSequences( aFixedProps, aDynamicProps ) );
162
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 );
166 }
167 } // if ( !p )
168 else
169 {
170 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
171 }
172 return *p;
173 }
174
175
176 void PropertyBagHelper::addProperty( const OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue )
177 {
178 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
180
181
182 // check name sanity
184 if ( aPropInfo.hasPropertyByName( _rName ) )
185 throw PropertyExistException( _rName, m_rContext.getPropertiesInterface() );
186
187
188 // normalize the REMOVABLE attribute - the FormComponent service
189 // requires that all dynamic properties are REMOVABLE
190 _nAttributes |= PropertyAttribute::REMOVABLE;
191
192
193 // find a free handle
194 sal_Int32 nHandle = impl_findFreeHandle( _rName );
195
196
197 // register the property, and invalidate our property meta data
198 m_aDynamicProperties.addProperty( _rName, nHandle, _nAttributes, _rInitialValue );
200 }
201
202
203 void PropertyBagHelper::removeProperty( const OUString& _rName )
204 {
205 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
207
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 );
214
215 m_aDynamicProperties.removeProperty( _rName );
217 }
218
219
220 namespace
221 {
222
223 struct SelectNameOfProperty
224 {
225 const OUString& operator()( const Property& _rProp ) const { return _rProp.Name; }
226 };
227
228
229 struct SelectNameOfPropertyValue
230 {
231 const OUString& operator()( const PropertyValue& _rProp ) const { return _rProp.Name; }
232 };
233
234
235 struct SelectValueOfPropertyValue
236 {
237 const Any& operator()( const PropertyValue& _rProp ) const { return _rProp.Value; }
238 };
239
240
241 struct PropertyValueLessByName
242 {
243 bool operator()( const PropertyValue& _lhs, const PropertyValue& _rhs ) const
244 {
245 return _lhs.Name < _rhs.Name;
246 }
247 };
248 }
249
250
251 Sequence< PropertyValue > PropertyBagHelper::getPropertyValues()
252 {
253 ::osl::MutexGuard aGuard( m_rContext.getMutex() );
255
256 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), css::uno::UNO_SET_THROW );
257 Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), css::uno::UNO_SET_THROW );
258
259 const Sequence< Property > aProperties( xPSI->getProperties() );
260 Sequence< OUString > aPropertyNames( aProperties.getLength() );
261 ::std::transform( aProperties.begin(), aProperties.end(),
262 aPropertyNames.getArray(), SelectNameOfProperty() );
263
264 Sequence< Any > aValues;
265 try
266 {
267 aValues = xMe->getPropertyValues( aPropertyNames );
268
269 if ( aValues.getLength() != aPropertyNames.getLength() )
270 throw RuntimeException();
271 }
272 catch( const RuntimeException& ) { throw; }
273 catch( const Exception& )
274 {
275 DBG_UNHANDLED_EXCEPTION("forms.component");
276 }
277 Sequence< PropertyValue > aPropertyValues( aValues.getLength() );
278 PropertyValue* pPropertyValue = aPropertyValues.getArray();
279
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 )
284 {
285 pPropertyValue->Name = *pName;
286 pPropertyValue->Value = *pValue;
287 }
288
289 return aPropertyValues;
290 }
291
292
293 void PropertyBagHelper::setPropertyValues( const Sequence< PropertyValue >& _rProps )
294 {
295 ::osl::ClearableMutexGuard aGuard( m_rContext.getMutex() );
297
298 sal_Int32 nPropertyValues = _rProps.getLength();
299
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() );
304
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.
309 for ( const PropertyValue* pProperties = aSortedProps.getConstArray();
310 pProperties != aSortedProps.getConstArray() + nPropertyValues;
311 ++pProperties
312 )
313 {
314 if ( !rArrayHelper.hasPropertyByName( pProperties->Name ) )
315 throw UnknownPropertyException( pProperties->Name, m_rContext.getPropertiesInterface() );
316 }
317
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() );
323
324 Sequence< Any > aValues( nPropertyValues );
325 ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
326 aValues.getArray(), SelectValueOfPropertyValue() );
327
328 Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), css::uno::UNO_SET_THROW );
329
330 aGuard.clear();
331 xMe->setPropertyValues( aNames, aValues );
332 }
333
334
335} // namespace frm
336
337
338/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertyValueVector_t aPropertyValues
PropertiesInfo aProperties
const char * pName
virtual sal_Bool SAL_CALL fillPropertyMembersByHandle(OUString *_pPropName, sal_Int16 *_pAttributes, sal_Int32 _nHandle) override
virtual sal_Bool SAL_CALL hasPropertyByName(const OUString &_rPropertyName) override
virtual ::osl::Mutex & getMutex()=0
virtual void describeFixedAndAggregateProperties(css::uno::Sequence< css::beans::Property > &_out_rFixedProperties, css::uno::Sequence< css::beans::Property > &_out_rAggregateProperties) const =0
virtual css::uno::Reference< css::beans::XMultiPropertySet > getPropertiesInterface()=0
PropertyBagHelper(IPropertyBagHelperContext &_rContext)
::comphelper::PropertyBag m_aDynamicProperties
css::uno::Sequence< css::beans::PropertyValue > getPropertyValues()
void removeProperty(const OUString &_rName)
sal_Int32 impl_findFreeHandle(const OUString &_rPropertyName)
finds a free property handle
void addProperty(const OUString &_rName, ::sal_Int16 _nAttributes, const css::uno::Any &_rInitialValue)
void setPropertyValues(const css::uno::Sequence< css::beans::PropertyValue > &_rProps)
::comphelper::OPropertyArrayAggregationHelper & impl_ts_getArrayHelper() const
returns the IPropertyArrayHelper instance used by |this|
void impl_nts_checkDisposed_throw() const
IPropertyBagHelperContext & m_rContext
std::unique_ptr<::comphelper::OPropertyArrayAggregationHelper > m_pPropertyArrayHelper
void impl_nts_invalidatePropertySetInfo()
invalidates our property set info, so subsequent calls to impl_ts_getArrayHelper and thus getInfoHelp...
#define DBG_UNHANDLED_EXCEPTION(...)
bool m_bDisposed
void * p
@ Exception
ListBox is a bit confusing / different from other form components, so here are a few notes:
Definition: BaseListBox.hxx:25
#define NEW_HANDLE_BASE
sal_Int32 nHandle
const Reference< XComponentContext > & m_rContext