Line data Source code
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/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>
29 :
30 : #include <tools/diagnose_ex.h>
31 :
32 : #include <comphelper/sequence.hxx>
33 : #include "rtl/instance.hxx"
34 :
35 :
36 : #define NEW_HANDLE_BASE 10000
37 :
38 :
39 : namespace 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::uno::UNO_QUERY_THROW;
51 : using ::com::sun::star::beans::XMultiPropertySet;
52 : using ::com::sun::star::beans::XPropertySetInfo;
53 : using ::com::sun::star::uno::RuntimeException;
54 : using ::com::sun::star::uno::Exception;
55 : using ::com::sun::star::beans::NotRemoveableException;
56 : using ::com::sun::star::beans::UnknownPropertyException;
57 :
58 : namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
59 :
60 :
61 : //= helper
62 :
63 : namespace
64 : {
65 :
66 1644 : static ::comphelper::IPropertyInfoService& lcl_getPropertyInfos()
67 : {
68 1644 : static ConcreteInfoService s_aPropInfos;
69 1644 : return s_aPropInfos;
70 : }
71 : }
72 :
73 1640 : PropertyBagHelper::PropertyBagHelper( IPropertyBagHelperContext& _rContext )
74 : :m_rContext( _rContext )
75 : ,m_pPropertyArrayHelper( NULL )
76 1640 : ,m_bDisposed( false )
77 : {
78 1640 : }
79 :
80 :
81 3140 : PropertyBagHelper::~PropertyBagHelper()
82 : {
83 1570 : delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
84 1570 : }
85 :
86 :
87 1438 : void PropertyBagHelper::dispose()
88 : {
89 1438 : m_bDisposed = true;
90 1438 : }
91 :
92 :
93 418 : void PropertyBagHelper::impl_nts_checkDisposed_throw() const
94 : {
95 418 : if ( m_bDisposed )
96 0 : throw DisposedException();
97 418 : }
98 :
99 :
100 76 : void PropertyBagHelper::impl_nts_invalidatePropertySetInfo()
101 : {
102 76 : delete m_pPropertyArrayHelper, m_pPropertyArrayHelper = NULL;
103 76 : }
104 :
105 :
106 114 : sal_Int32 PropertyBagHelper::impl_findFreeHandle( const OUString& _rPropertyName )
107 : {
108 114 : ::comphelper::OPropertyArrayAggregationHelper& rPropInfo( impl_ts_getArrayHelper() );
109 :
110 : // check the preferred handle
111 114 : sal_Int32 nHandle = lcl_getPropertyInfos().getPreferredPropertyId( _rPropertyName );
112 114 : if ( ( nHandle != -1 ) && rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
113 0 : nHandle = -1;
114 :
115 : // search a free handle in <math>F_1009</math>
116 114 : if ( nHandle == -1 )
117 : {
118 114 : sal_Int32 nPrime = 1009;
119 114 : sal_Int32 nFactor = 11;
120 114 : sal_Int32 nNum = nFactor;
121 334 : while ( nNum != 1 )
122 : {
123 220 : if ( !rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nNum + NEW_HANDLE_BASE ) )
124 : {
125 : // handle not used, yet
126 114 : nHandle = nNum + NEW_HANDLE_BASE;
127 114 : break;
128 : }
129 106 : nNum = ( nNum * nFactor ) % nPrime;
130 : }
131 : }
132 :
133 : // search a free handle greater NEW_HANDLE_BASE
134 114 : if ( nHandle == -1 )
135 : {
136 0 : nHandle = NEW_HANDLE_BASE + 1009;
137 0 : while ( rPropInfo.fillPropertyMembersByHandle( NULL, NULL, nHandle ) )
138 0 : ++nHandle;
139 : }
140 :
141 114 : return nHandle;
142 : }
143 :
144 :
145 358503 : ::comphelper::OPropertyArrayAggregationHelper& PropertyBagHelper::impl_ts_getArrayHelper() const
146 : {
147 358503 : OPropertyArrayAggregationHelper* p = m_pPropertyArrayHelper;
148 358503 : if ( !p )
149 : {
150 1530 : ::osl::MutexGuard aGuard( m_rContext.getMutex() );
151 1530 : p = m_pPropertyArrayHelper;
152 1530 : if ( !p )
153 : {
154 : // our own fixed and our aggregate's properties
155 1530 : Sequence< Property > aFixedProps;
156 3060 : Sequence< Property > aAggregateProps;
157 1530 : m_rContext.describeFixedAndAggregateProperties( aFixedProps, aAggregateProps );
158 :
159 : // our dynamic properties
160 3060 : Sequence< Property > aDynamicProps;
161 1530 : m_aDynamicProperties.describeProperties( aDynamicProps );
162 :
163 : Sequence< Property > aOwnProps(
164 3060 : ::comphelper::concatSequences( aFixedProps, aDynamicProps ) );
165 :
166 1530 : p = new OPropertyArrayAggregationHelper( aOwnProps, aAggregateProps, &lcl_getPropertyInfos(), NEW_HANDLE_BASE );
167 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
168 3060 : const_cast< PropertyBagHelper* >( this )->m_pPropertyArrayHelper = p;
169 1530 : }
170 : } // if ( !p )
171 : else
172 : {
173 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
174 : }
175 358503 : return *p;
176 : }
177 :
178 :
179 152 : void PropertyBagHelper::addProperty( const OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue )
180 : {
181 152 : ::osl::MutexGuard aGuard( m_rContext.getMutex() );
182 152 : impl_nts_checkDisposed_throw();
183 :
184 :
185 : // check name sanity
186 152 : ::comphelper::OPropertyArrayAggregationHelper& aPropInfo( impl_ts_getArrayHelper() );
187 152 : if ( aPropInfo.hasPropertyByName( _rName ) )
188 38 : throw PropertyExistException( _rName, m_rContext.getPropertiesInterface() );
189 :
190 :
191 : // normalize the REMOVABLE attribute - the FormComponent service
192 : // requires that all dynamic properties are REMOVABLE
193 114 : _nAttributes |= PropertyAttribute::REMOVABLE;
194 :
195 :
196 : // find a free handle
197 114 : sal_Int32 nHandle = impl_findFreeHandle( _rName );
198 :
199 :
200 : // register the property, and invalidate our property meta data
201 114 : m_aDynamicProperties.addProperty( _rName, nHandle, _nAttributes, _rInitialValue );
202 152 : impl_nts_invalidatePropertySetInfo();
203 38 : }
204 :
205 :
206 114 : void PropertyBagHelper::removeProperty( const OUString& _rName )
207 : {
208 114 : ::osl::MutexGuard aGuard( m_rContext.getMutex() );
209 114 : impl_nts_checkDisposed_throw();
210 :
211 : // check whether it's removable at all
212 228 : Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
213 228 : Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
214 190 : Property aProperty( xPSI->getPropertyByName( _rName ) );
215 76 : if ( ( aProperty.Attributes & PropertyAttribute::REMOVABLE ) == 0 )
216 38 : throw NotRemoveableException( _rName, xMe );
217 :
218 38 : m_aDynamicProperties.removeProperty( _rName );
219 152 : impl_nts_invalidatePropertySetInfo();
220 38 : }
221 :
222 :
223 : namespace
224 : {
225 :
226 : struct SelectNameOfProperty : public ::std::unary_function< Property, OUString >
227 : {
228 1948 : const OUString& operator()( const Property& _rProp ) const { return _rProp.Name; }
229 : };
230 :
231 :
232 : struct SelectNameOfPropertyValue : public ::std::unary_function< PropertyValue, OUString >
233 : {
234 76 : const OUString& operator()( const PropertyValue& _rProp ) const { return _rProp.Name; }
235 : };
236 :
237 :
238 : struct SelectValueOfPropertyValue : public ::std::unary_function< PropertyValue, Any >
239 : {
240 76 : const Any& operator()( const PropertyValue& _rProp ) const { return _rProp.Value; }
241 : };
242 :
243 :
244 : struct PropertyValueLessByName : public ::std::binary_function< PropertyValue, PropertyValue, bool >
245 : {
246 0 : bool operator()( const PropertyValue& _lhs, const PropertyValue _rhs ) const
247 : {
248 0 : return _lhs.Name < _rhs.Name;
249 : }
250 : };
251 : }
252 :
253 :
254 38 : Sequence< PropertyValue > PropertyBagHelper::getPropertyValues()
255 : {
256 38 : ::osl::MutexGuard aGuard( m_rContext.getMutex() );
257 38 : impl_nts_checkDisposed_throw();
258 :
259 76 : Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
260 76 : Reference< XPropertySetInfo > xPSI( xMe->getPropertySetInfo(), UNO_QUERY_THROW );
261 :
262 76 : Sequence< Property > aProperties( xPSI->getProperties() );
263 76 : Sequence< OUString > aPropertyNames( aProperties.getLength() );
264 38 : ::std::transform( aProperties.getConstArray(), aProperties.getConstArray() + aProperties.getLength(),
265 76 : aPropertyNames.getArray(), SelectNameOfProperty() );
266 :
267 76 : Sequence< Any > aValues;
268 : try
269 : {
270 38 : aValues = xMe->getPropertyValues( aPropertyNames );
271 :
272 38 : if ( aValues.getLength() != aPropertyNames.getLength() )
273 0 : throw RuntimeException();
274 : }
275 0 : catch( const RuntimeException& ) { throw; }
276 0 : catch( const Exception& )
277 : {
278 : DBG_UNHANDLED_EXCEPTION();
279 : }
280 38 : Sequence< PropertyValue > aPropertyValues( aValues.getLength() );
281 38 : PropertyValue* pPropertyValue = aPropertyValues.getArray();
282 :
283 38 : const OUString* pName = aPropertyNames.getConstArray();
284 38 : const OUString* pNameEnd = aPropertyNames.getConstArray() + aPropertyNames.getLength();
285 38 : const Any* pValue = aValues.getConstArray();
286 1986 : for ( ; pName != pNameEnd; ++pName, ++pValue, ++pPropertyValue )
287 : {
288 1948 : pPropertyValue->Name = *pName;
289 1948 : pPropertyValue->Value = *pValue;
290 : }
291 :
292 76 : return aPropertyValues;
293 : }
294 :
295 :
296 114 : void PropertyBagHelper::setPropertyValues( const Sequence< PropertyValue >& _rProps )
297 : {
298 114 : ::osl::ClearableMutexGuard aGuard( m_rContext.getMutex() );
299 114 : impl_nts_checkDisposed_throw();
300 :
301 114 : sal_Int32 nPropertyValues = _rProps.getLength();
302 :
303 : // XMultiPropertySet::setPropertyValues expects its arguments to be sorted by name
304 : // while XPropertyAccess::setPropertyValues doesn't. So first of all, sort.
305 228 : Sequence< PropertyValue > aSortedProps( _rProps );
306 114 : ::std::sort( aSortedProps.getArray(), aSortedProps.getArray() + nPropertyValues, PropertyValueLessByName() );
307 :
308 : // also, XPropertyAccess::setPropertyValues is expected to throw an UnknownPropertyException
309 : // for unsupported properties, while XMultiPropertySet::setPropertyValues is expected to ignore
310 : // those. So, check for unsupported properties first.
311 114 : ::comphelper::OPropertyArrayAggregationHelper& rArrayHelper( impl_ts_getArrayHelper() );
312 380 : for ( const PropertyValue* pProperties = aSortedProps.getConstArray();
313 190 : pProperties != aSortedProps.getConstArray() + nPropertyValues;
314 : ++pProperties
315 : )
316 : {
317 114 : if ( !rArrayHelper.hasPropertyByName( pProperties->Name ) )
318 38 : throw UnknownPropertyException( pProperties->Name, m_rContext.getPropertiesInterface() );
319 : }
320 :
321 : // Now finally split into a Name and a Value sequence, and forward to
322 : // XMultiPropertySet::setPropertyValues
323 152 : Sequence< OUString > aNames( nPropertyValues );
324 76 : ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
325 152 : aNames.getArray(), SelectNameOfPropertyValue() );
326 :
327 152 : Sequence< Any > aValues( nPropertyValues );
328 76 : ::std::transform( aSortedProps.getConstArray(), aSortedProps.getConstArray() + nPropertyValues,
329 152 : aValues.getArray(), SelectValueOfPropertyValue() );
330 :
331 114 : Reference< XMultiPropertySet > xMe( m_rContext.getPropertiesInterface(), UNO_QUERY_THROW );
332 :
333 76 : aGuard.clear();
334 190 : xMe->setPropertyValues( aNames, aValues );
335 38 : }
336 :
337 :
338 : } // namespace frm
339 :
340 :
341 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|