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 :
21 : #include "opropertybag.hxx"
22 : #include "comphelper_module.hxx"
23 : #include "comphelper_services.hxx"
24 :
25 : #include <com/sun/star/beans/PropertyAttribute.hpp>
26 : #include <com/sun/star/beans/NamedValue.hpp>
27 : #include <com/sun/star/beans/Property.hpp>
28 :
29 : #include <comphelper/namedvaluecollection.hxx>
30 : #include <cppuhelper/supportsservice.hxx>
31 :
32 : #include <cppuhelper/exc_hlp.hxx>
33 : #include <osl/thread.h>
34 :
35 : #include <algorithm>
36 : #include <functional>
37 : #include <iterator>
38 :
39 :
40 :
41 : using namespace ::com::sun::star;
42 :
43 191 : void createRegistryInfo_OPropertyBag()
44 : {
45 191 : static ::comphelper::module::OAutoRegistration< ::comphelper::OPropertyBag > aAutoRegistration;
46 191 : }
47 :
48 :
49 : namespace comphelper
50 : {
51 :
52 :
53 : using namespace ::com::sun::star::uno;
54 : using namespace ::com::sun::star::lang;
55 : using namespace ::com::sun::star::beans;
56 : using namespace ::com::sun::star::util;
57 : using namespace ::com::sun::star::container;
58 :
59 2961 : OPropertyBag::OPropertyBag()
60 2961 : :OPropertyBag_PBase( GetBroadcastHelper(), this )
61 : ,::cppu::IEventNotificationHook()
62 : ,m_bAutoAddProperties( false )
63 : ,m_NotifyListeners(m_aMutex)
64 5922 : ,m_isModified(false)
65 :
66 : {
67 2961 : }
68 :
69 :
70 5894 : OPropertyBag::~OPropertyBag()
71 : {
72 5894 : }
73 :
74 :
75 176154 : IMPLEMENT_FORWARD_XINTERFACE2( OPropertyBag, OPropertyBag_Base, OPropertyBag_PBase )
76 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyBag, OPropertyBag_Base, OPropertyBag_PBase )
77 :
78 :
79 192 : Sequence< OUString > OPropertyBag::getSupportedServiceNames_static() throw( RuntimeException )
80 : {
81 192 : Sequence< OUString > aServices(1);
82 192 : aServices[0] = "com.sun.star.beans.PropertyBag";
83 192 : return aServices;
84 : }
85 :
86 :
87 2960 : void SAL_CALL OPropertyBag::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException, std::exception)
88 : {
89 2960 : Sequence< Type > aTypes;
90 2960 : bool AllowEmptyPropertyName(false);
91 2960 : bool AutomaticAddition(false);
92 :
93 5920 : if (_rArguments.getLength() == 3
94 2949 : && (_rArguments[0] >>= aTypes)
95 2949 : && (_rArguments[1] >>= AllowEmptyPropertyName)
96 5909 : && (_rArguments[2] >>= AutomaticAddition))
97 : {
98 : ::std::copy(
99 : aTypes.getConstArray(),
100 2949 : aTypes.getConstArray() + aTypes.getLength(),
101 : ::std::insert_iterator< TypeBag >( m_aAllowedTypes, m_aAllowedTypes.begin() )
102 5898 : );
103 2949 : m_bAutoAddProperties = AutomaticAddition;
104 :
105 : } else {
106 11 : ::comphelper::NamedValueCollection aArguments( _rArguments );
107 :
108 11 : if ( aArguments.get_ensureType( "AllowedTypes", aTypes ) )
109 : ::std::copy(
110 : aTypes.getConstArray(),
111 0 : aTypes.getConstArray() + aTypes.getLength(),
112 : ::std::insert_iterator< TypeBag >( m_aAllowedTypes, m_aAllowedTypes.begin() )
113 0 : );
114 :
115 11 : aArguments.get_ensureType( "AutomaticAddition", m_bAutoAddProperties );
116 : aArguments.get_ensureType( "AllowEmptyPropertyName",
117 11 : AllowEmptyPropertyName );
118 : }
119 2960 : if (AllowEmptyPropertyName) {
120 : m_aDynamicProperties.setAllowEmptyPropertyName(
121 2838 : AllowEmptyPropertyName);
122 2960 : }
123 2960 : }
124 :
125 :
126 192 : OUString OPropertyBag::getImplementationName_static() throw( RuntimeException )
127 : {
128 192 : return OUString( "com.sun.star.comp.comphelper.OPropertyBag" );
129 : }
130 :
131 :
132 2961 : Reference< XInterface > SAL_CALL OPropertyBag::Create( SAL_UNUSED_PARAMETER const Reference< XComponentContext >& )
133 : {
134 2961 : return *new OPropertyBag;
135 : }
136 :
137 :
138 1 : OUString SAL_CALL OPropertyBag::getImplementationName() throw (RuntimeException, std::exception)
139 : {
140 1 : return getImplementationName_static();
141 : }
142 :
143 0 : sal_Bool SAL_CALL OPropertyBag::supportsService( const OUString& rServiceName ) throw (RuntimeException, std::exception)
144 : {
145 0 : return cppu::supportsService(this, rServiceName);
146 : }
147 :
148 :
149 1 : Sequence< OUString > SAL_CALL OPropertyBag::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
150 : {
151 1 : return getSupportedServiceNames_static();
152 : }
153 :
154 :
155 38 : void OPropertyBag::fireEvents(
156 : sal_Int32 * /*pnHandles*/,
157 : sal_Int32 nCount,
158 : sal_Bool bVetoable,
159 : bool bIgnoreRuntimeExceptionsWhileFiring)
160 : {
161 38 : if (nCount && !bVetoable) {
162 12 : setModifiedImpl(true, bIgnoreRuntimeExceptionsWhileFiring);
163 : }
164 38 : }
165 :
166 19291 : void OPropertyBag::setModifiedImpl(bool bModified,
167 : bool bIgnoreRuntimeExceptionsWhileFiring)
168 : {
169 : { // do not lock mutex while notifying (#i93514#) to prevent deadlock
170 19291 : ::osl::MutexGuard aGuard( m_aMutex );
171 19291 : m_isModified = bModified;
172 : }
173 19291 : if (bModified) {
174 : try {
175 19291 : Reference<XInterface> xThis(*this);
176 38582 : EventObject event(xThis);
177 : m_NotifyListeners.notifyEach(
178 38582 : &XModifyListener::modified, event);
179 0 : } catch (RuntimeException &) {
180 0 : if (!bIgnoreRuntimeExceptionsWhileFiring) {
181 0 : throw;
182 : }
183 0 : } catch (Exception &) {
184 : // ignore
185 : }
186 : }
187 19291 : }
188 :
189 :
190 0 : sal_Bool SAL_CALL OPropertyBag::isModified()
191 : throw (RuntimeException, std::exception)
192 : {
193 0 : ::osl::MutexGuard aGuard( m_aMutex );
194 0 : return m_isModified;
195 : }
196 :
197 19279 : void SAL_CALL OPropertyBag::setModified( sal_Bool bModified )
198 : throw (PropertyVetoException, RuntimeException, std::exception)
199 : {
200 19279 : setModifiedImpl(bModified, false);
201 19279 : }
202 :
203 2133 : void SAL_CALL OPropertyBag::addModifyListener(
204 : const Reference< XModifyListener > & xListener)
205 : throw (RuntimeException, std::exception)
206 : {
207 2133 : m_NotifyListeners.addInterface(xListener);
208 2133 : }
209 :
210 1 : void SAL_CALL OPropertyBag::removeModifyListener(
211 : const Reference< XModifyListener > & xListener)
212 : throw (RuntimeException, std::exception)
213 : {
214 1 : m_NotifyListeners.removeInterface(xListener);
215 1 : }
216 :
217 :
218 1488 : Reference< XPropertySetInfo > SAL_CALL OPropertyBag::getPropertySetInfo( ) throw(RuntimeException, std::exception)
219 : {
220 1488 : return createPropertySetInfo( getInfoHelper() );
221 : }
222 :
223 :
224 0 : sal_Bool SAL_CALL OPropertyBag::has( const Any& /*aElement*/ ) throw (RuntimeException, std::exception)
225 : {
226 : // XSet is only a workaround for addProperty not being able to add default-void properties.
227 : // So, everything of XSet except insert is implemented empty
228 0 : return sal_False;
229 : }
230 :
231 :
232 333 : void SAL_CALL OPropertyBag::insert( const Any& _element ) throw (IllegalArgumentException, ElementExistException, RuntimeException, std::exception)
233 : {
234 : // This is a workaround for addProperty not being able to add default-void properties.
235 : // If we ever have a smarter XPropertyContainer::addProperty interface, we can remove this, ehm, well, hack.
236 333 : Property aProperty;
237 333 : if ( !( _element >>= aProperty ) )
238 0 : throw IllegalArgumentException( OUString(), *this, 1 );
239 :
240 666 : ::osl::ClearableMutexGuard g( m_aMutex );
241 :
242 : // check whether the type is allowed, everything else will be checked
243 : // by m_aDynamicProperties
244 999 : if ( !m_aAllowedTypes.empty()
245 1332 : && m_aAllowedTypes.find( aProperty.Type ) == m_aAllowedTypes.end()
246 : )
247 0 : throw IllegalArgumentException( OUString(), *this, 1 );
248 :
249 333 : m_aDynamicProperties.addVoidProperty( aProperty.Name, aProperty.Type, findFreeHandle(), aProperty.Attributes );
250 :
251 : // our property info is dirty
252 333 : m_pArrayHelper.reset();
253 :
254 333 : g.clear();
255 666 : setModified(sal_True);
256 333 : }
257 :
258 :
259 0 : void SAL_CALL OPropertyBag::remove( const Any& /*aElement*/ ) throw (IllegalArgumentException, NoSuchElementException, RuntimeException, std::exception)
260 : {
261 : // XSet is only a workaround for addProperty not being able to add default-void properties.
262 : // So, everything of XSet except insert is implemented empty
263 0 : throw NoSuchElementException( OUString(), *this );
264 : }
265 :
266 :
267 :
268 0 : Reference< XEnumeration > SAL_CALL OPropertyBag::createEnumeration( ) throw (RuntimeException, std::exception)
269 : {
270 : // XSet is only a workaround for addProperty not being able to add default-void properties.
271 : // So, everything of XSet except insert is implemented empty
272 0 : return NULL;
273 : }
274 :
275 :
276 0 : Type SAL_CALL OPropertyBag::getElementType( ) throw (RuntimeException, std::exception)
277 : {
278 : // XSet is only a workaround for addProperty not being able to add default-void properties.
279 : // So, everything of XSet except insert is implemented empty
280 0 : return Type();
281 : }
282 :
283 :
284 0 : sal_Bool SAL_CALL OPropertyBag::hasElements( ) throw (RuntimeException, std::exception)
285 : {
286 : // XSet is only a workaround for addProperty not being able to add default-void properties.
287 : // So, everything of XSet except insert is implemented empty
288 0 : return sal_False;
289 : }
290 :
291 :
292 51321 : void SAL_CALL OPropertyBag::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
293 : {
294 51321 : m_aDynamicProperties.getFastPropertyValue( _nHandle, _rValue );
295 51321 : }
296 :
297 857 : sal_Bool SAL_CALL OPropertyBag::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue ) throw (IllegalArgumentException, UnknownPropertyException)
298 : {
299 857 : return m_aDynamicProperties.convertFastPropertyValue( _nHandle, _rValue, _rConvertedValue, _rOldValue );
300 : }
301 :
302 21 : void SAL_CALL OPropertyBag::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw (Exception, std::exception)
303 : {
304 21 : m_aDynamicProperties.setFastPropertyValue( nHandle, rValue );
305 21 : }
306 :
307 :
308 62097 : ::cppu::IPropertyArrayHelper& SAL_CALL OPropertyBag::getInfoHelper()
309 : {
310 62097 : if ( !m_pArrayHelper.get() )
311 : {
312 2026 : Sequence< Property > aProperties;
313 2026 : m_aDynamicProperties.describeProperties( aProperties );
314 2026 : m_pArrayHelper.reset( new ::cppu::OPropertyArrayHelper( aProperties ) );
315 : }
316 62097 : return *m_pArrayHelper;
317 :
318 : }
319 :
320 :
321 19393 : sal_Int32 OPropertyBag::findFreeHandle() const
322 : {
323 19393 : const sal_Int32 nPrime = 1009;
324 19393 : const sal_Int32 nSeed = 11;
325 :
326 19393 : sal_Int32 nCheck = nSeed;
327 229372 : while ( m_aDynamicProperties.hasPropertyByHandle( nCheck ) && ( nCheck != 1 ) )
328 : {
329 190586 : nCheck = ( nCheck * nSeed ) % nPrime;
330 : }
331 :
332 19393 : if ( nCheck == 1 )
333 : { // uh ... we already have 1008 handles used up
334 : // -> simply count upwards
335 0 : while ( m_aDynamicProperties.hasPropertyByHandle( nCheck ) )
336 0 : ++nCheck;
337 : }
338 :
339 19393 : return nCheck;
340 : }
341 :
342 :
343 19061 : void SAL_CALL OPropertyBag::addProperty( const OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue ) throw (PropertyExistException, IllegalTypeException, IllegalArgumentException, RuntimeException, std::exception)
344 : {
345 19061 : ::osl::ClearableMutexGuard g( m_aMutex );
346 :
347 : // check whether the type is allowed, everything else will be checked
348 : // by m_aDynamicProperties
349 38122 : Type aPropertyType = _rInitialValue.getValueType();
350 57213 : if ( _rInitialValue.hasValue()
351 19061 : && !m_aAllowedTypes.empty()
352 95245 : && m_aAllowedTypes.find( aPropertyType ) == m_aAllowedTypes.end()
353 : )
354 1 : throw IllegalTypeException( OUString(), *this );
355 :
356 19060 : m_aDynamicProperties.addProperty( _rName, findFreeHandle(), _nAttributes, _rInitialValue );
357 :
358 : // our property info is dirty
359 18943 : m_pArrayHelper.reset();
360 :
361 18943 : g.clear();
362 38004 : setModified(sal_True);
363 18943 : }
364 :
365 :
366 5 : void SAL_CALL OPropertyBag::removeProperty( const OUString& _rName ) throw (UnknownPropertyException, NotRemoveableException, RuntimeException, std::exception)
367 : {
368 5 : ::osl::ClearableMutexGuard g( m_aMutex );
369 :
370 5 : m_aDynamicProperties.removeProperty( _rName );
371 :
372 : // our property info is dirty
373 3 : m_pArrayHelper.reset();
374 :
375 3 : g.clear();
376 5 : setModified(sal_True);
377 3 : }
378 :
379 :
380 : namespace
381 : {
382 : struct ComparePropertyValueByName : public ::std::binary_function< PropertyValue, PropertyValue, bool >
383 : {
384 354 : bool operator()( const PropertyValue& _rLHS, const PropertyValue& _rRHS )
385 : {
386 354 : return _rLHS.Name < _rRHS.Name;
387 : }
388 : };
389 :
390 : template< typename CLASS >
391 : struct TransformPropertyToName : public ::std::unary_function< CLASS, OUString >
392 : {
393 26578 : const OUString& operator()( const CLASS& _rProp )
394 : {
395 26578 : return _rProp.Name;
396 : }
397 : };
398 :
399 : struct ExtractPropertyValue : public ::std::unary_function< PropertyValue, Any >
400 : {
401 135 : const Any& operator()( const PropertyValue& _rProp )
402 : {
403 135 : return _rProp.Value;
404 : }
405 : };
406 : }
407 :
408 :
409 2584 : Sequence< PropertyValue > SAL_CALL OPropertyBag::getPropertyValues( ) throw (RuntimeException, std::exception)
410 : {
411 2584 : ::osl::MutexGuard aGuard( m_aMutex );
412 :
413 : // all registered properties
414 5168 : Sequence< Property > aProperties;
415 2584 : m_aDynamicProperties.describeProperties( aProperties );
416 :
417 : // their names
418 5168 : Sequence< OUString > aNames( aProperties.getLength() );
419 : ::std::transform(
420 : aProperties.getConstArray(),
421 2584 : aProperties.getConstArray() + aProperties.getLength(),
422 : aNames.getArray(),
423 : TransformPropertyToName< Property >()
424 5168 : );
425 :
426 : // their values
427 5168 : Sequence< Any > aValues;
428 : try
429 : {
430 2584 : aValues = OPropertyBag_PBase::getPropertyValues( aNames );
431 2584 : if ( aValues.getLength() != aNames.getLength() )
432 0 : throw RuntimeException();
433 : }
434 0 : catch( const RuntimeException& )
435 : {
436 0 : throw;
437 : }
438 0 : catch( const Exception& )
439 : {
440 : // ignore
441 : }
442 :
443 : // merge names and values, and retrieve the state/handle
444 2584 : ::cppu::IPropertyArrayHelper& rPropInfo = getInfoHelper();
445 :
446 2584 : Sequence< PropertyValue > aPropertyValues( aNames.getLength() );
447 2584 : const OUString* pName = aNames.getConstArray();
448 2584 : const OUString* pNamesEnd = aNames.getConstArray() + aNames.getLength();
449 2584 : const Any* pValue = aValues.getArray();
450 2584 : PropertyValue* pPropertyValue = aPropertyValues.getArray();
451 :
452 29024 : for ( ; pName != pNamesEnd; ++pName, ++pValue, ++pPropertyValue )
453 : {
454 26440 : pPropertyValue->Name = *pName;
455 26440 : pPropertyValue->Handle = rPropInfo.getHandleByName( *pName );
456 26440 : pPropertyValue->Value = *pValue;
457 26440 : pPropertyValue->State = getPropertyStateByHandle( pPropertyValue->Handle );
458 : }
459 :
460 5168 : return aPropertyValues;
461 : }
462 :
463 :
464 19 : void OPropertyBag::impl_setPropertyValues_throw( const Sequence< PropertyValue >& _rProps )
465 : {
466 : // sort (the XMultiPropertySet interface requires this)
467 19 : Sequence< PropertyValue > aProperties( _rProps );
468 : ::std::sort(
469 : aProperties.getArray(),
470 19 : aProperties.getArray() + aProperties.getLength(),
471 : ComparePropertyValueByName()
472 19 : );
473 :
474 : // a sequence of names
475 38 : Sequence< OUString > aNames( aProperties.getLength() );
476 : ::std::transform(
477 : aProperties.getConstArray(),
478 19 : aProperties.getConstArray() + aProperties.getLength(),
479 : aNames.getArray(),
480 : TransformPropertyToName< PropertyValue >()
481 38 : );
482 :
483 : try
484 : {
485 : // check for unknown properties
486 : // we cannot simply rely on the XMultiPropertySet::setPropertyValues
487 : // implementation of our base class, since it does not throw
488 : // an UnknownPropertyException. More precise, XMultiPropertySet::setPropertyValues
489 : // does not allow to throw this exception, while XPropertyAccess::setPropertyValues
490 : // requires it
491 19 : sal_Int32 nCount = aNames.getLength();
492 :
493 19 : Sequence< sal_Int32 > aHandles( nCount );
494 19 : sal_Int32* pHandle = aHandles.getArray();
495 19 : const PropertyValue* pProperty = aProperties.getConstArray();
496 310 : for ( const OUString* pName = aNames.getConstArray();
497 155 : pName != aNames.getConstArray() + aNames.getLength();
498 : ++pName, ++pHandle, ++pProperty
499 : )
500 : {
501 137 : ::cppu::IPropertyArrayHelper& rPropInfo = getInfoHelper();
502 137 : *pHandle = rPropInfo.getHandleByName( *pName );
503 137 : if ( *pHandle != -1 )
504 135 : continue;
505 :
506 : // there's a property requested which we do not know
507 2 : if ( m_bAutoAddProperties )
508 : {
509 : // add the property
510 1 : sal_Int16 nAttributes = PropertyAttribute::BOUND | PropertyAttribute::REMOVABLE | PropertyAttribute::MAYBEDEFAULT;
511 1 : addProperty( *pName, nAttributes, pProperty->Value );
512 1 : continue;
513 : }
514 :
515 : // no way out
516 1 : throw UnknownPropertyException( *pName, *this );
517 : }
518 :
519 : // a sequence of values
520 36 : Sequence< Any > aValues( aProperties.getLength() );
521 : ::std::transform(
522 : aProperties.getConstArray(),
523 18 : aProperties.getConstArray() + aProperties.getLength(),
524 : aValues.getArray(),
525 : ExtractPropertyValue()
526 36 : );
527 :
528 37 : setFastPropertyValues( nCount, aHandles.getArray(), aValues.getConstArray(), nCount );
529 : }
530 0 : catch( const PropertyVetoException& ) { throw; }
531 0 : catch( const IllegalArgumentException& ) { throw; }
532 0 : catch( const WrappedTargetException& ) { throw; }
533 0 : catch( const RuntimeException& ) { throw; }
534 2 : catch( const UnknownPropertyException& ) { throw; }
535 0 : catch( const Exception& )
536 : {
537 0 : throw WrappedTargetException( OUString(), *this, ::cppu::getCaughtException() );
538 19 : }
539 18 : }
540 :
541 :
542 19 : void SAL_CALL OPropertyBag::setPropertyValues( const Sequence< PropertyValue >& _rProps ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
543 : {
544 19 : ::osl::MutexGuard aGuard( m_aMutex );
545 20 : impl_setPropertyValues_throw( _rProps );
546 18 : }
547 :
548 :
549 30472 : PropertyState OPropertyBag::getPropertyStateByHandle( sal_Int32 _nHandle )
550 : {
551 : // for properties which do not support the MAYBEDEFAULT attribute, don't rely on the base class, but
552 : // assume they're always in DIRECT state.
553 : // (Note that this probably would belong into the base class. However, this would mean we would need
554 : // to check all existent usages of the base class, where MAYBEDEFAULT is *not* set, but
555 : // a default is nonetheless supplied/used. This is hard to accomplish reliably, in the
556 : // current phase.
557 : // #i78593# / 2007-07-07 / frank.schoenheit@sun.com
558 :
559 30472 : ::cppu::IPropertyArrayHelper& rPropInfo = getInfoHelper();
560 30472 : sal_Int16 nAttributes(0);
561 30472 : OSL_VERIFY( rPropInfo.fillPropertyMembersByHandle( NULL, &nAttributes, _nHandle ) );
562 30472 : if ( ( nAttributes & PropertyAttribute::MAYBEDEFAULT ) == 0 )
563 15045 : return PropertyState_DIRECT_VALUE;
564 :
565 15427 : return OPropertyBag_PBase::getPropertyStateByHandle( _nHandle );
566 : }
567 :
568 :
569 16531 : Any OPropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle ) const
570 : {
571 16531 : Any aDefault;
572 16531 : m_aDynamicProperties.getPropertyDefaultByHandle( _nHandle, aDefault );
573 16531 : return aDefault;
574 : }
575 :
576 :
577 : } // namespace comphelper
578 :
579 :
580 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|