Bump version to 4.3-4
[LibreOffice.git] / comphelper / source / property / opropertybag.cxx
blob3ce5b48f191b4bf5b2e7968d44b089141b87383e
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 .
21 #include "opropertybag.hxx"
22 #include "comphelper_module.hxx"
23 #include "comphelper_services.hxx"
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/beans/NamedValue.hpp>
27 #include <com/sun/star/beans/Property.hpp>
29 #include <comphelper/namedvaluecollection.hxx>
30 #include <cppuhelper/supportsservice.hxx>
32 #include <cppuhelper/exc_hlp.hxx>
33 #include <osl/thread.h>
35 #include <algorithm>
36 #include <functional>
37 #include <iterator>
41 using namespace ::com::sun::star;
43 void createRegistryInfo_OPropertyBag()
45 static ::comphelper::module::OAutoRegistration< ::comphelper::OPropertyBag > aAutoRegistration;
49 namespace comphelper
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;
60 //= OPropertyBag
63 OPropertyBag::OPropertyBag()
64 :OPropertyBag_PBase( GetBroadcastHelper(), this )
65 ,::cppu::IEventNotificationHook()
66 ,m_bAutoAddProperties( false )
67 ,m_NotifyListeners(m_aMutex)
68 ,m_isModified(false)
74 OPropertyBag::~OPropertyBag()
79 IMPLEMENT_FORWARD_XINTERFACE2( OPropertyBag, OPropertyBag_Base, OPropertyBag_PBase )
80 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyBag, OPropertyBag_Base, OPropertyBag_PBase )
83 Sequence< OUString > OPropertyBag::getSupportedServiceNames_static() throw( RuntimeException )
85 Sequence< OUString > aServices(1);
86 aServices[0] = "com.sun.star.beans.PropertyBag";
87 return aServices;
91 void SAL_CALL OPropertyBag::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException, std::exception)
93 Sequence< Type > aTypes;
94 bool AllowEmptyPropertyName(false);
95 bool AutomaticAddition(false);
97 if (_rArguments.getLength() == 3
98 && (_rArguments[0] >>= aTypes)
99 && (_rArguments[1] >>= AllowEmptyPropertyName)
100 && (_rArguments[2] >>= AutomaticAddition))
102 ::std::copy(
103 aTypes.getConstArray(),
104 aTypes.getConstArray() + aTypes.getLength(),
105 ::std::insert_iterator< TypeBag >( m_aAllowedTypes, m_aAllowedTypes.begin() )
107 m_bAutoAddProperties = AutomaticAddition;
109 } else {
110 ::comphelper::NamedValueCollection aArguments( _rArguments );
112 if ( aArguments.get_ensureType( "AllowedTypes", aTypes ) )
113 ::std::copy(
114 aTypes.getConstArray(),
115 aTypes.getConstArray() + aTypes.getLength(),
116 ::std::insert_iterator< TypeBag >( m_aAllowedTypes, m_aAllowedTypes.begin() )
119 aArguments.get_ensureType( "AutomaticAddition", m_bAutoAddProperties );
120 aArguments.get_ensureType( "AllowEmptyPropertyName",
121 AllowEmptyPropertyName );
123 if (AllowEmptyPropertyName) {
124 m_aDynamicProperties.setAllowEmptyPropertyName(
125 AllowEmptyPropertyName);
130 OUString OPropertyBag::getImplementationName_static() throw( RuntimeException )
132 return OUString( "com.sun.star.comp.comphelper.OPropertyBag" );
136 Reference< XInterface > SAL_CALL OPropertyBag::Create( SAL_UNUSED_PARAMETER const Reference< XComponentContext >& )
138 return *new OPropertyBag;
142 OUString SAL_CALL OPropertyBag::getImplementationName() throw (RuntimeException, std::exception)
144 return getImplementationName_static();
147 sal_Bool SAL_CALL OPropertyBag::supportsService( const OUString& rServiceName ) throw (RuntimeException, std::exception)
149 return cppu::supportsService(this, rServiceName);
153 Sequence< OUString > SAL_CALL OPropertyBag::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
155 return getSupportedServiceNames_static();
159 void OPropertyBag::fireEvents(
160 sal_Int32 * /*pnHandles*/,
161 sal_Int32 nCount,
162 sal_Bool bVetoable,
163 bool bIgnoreRuntimeExceptionsWhileFiring)
165 if (nCount && !bVetoable) {
166 setModifiedImpl(true, bIgnoreRuntimeExceptionsWhileFiring);
170 void OPropertyBag::setModifiedImpl(bool bModified,
171 bool bIgnoreRuntimeExceptionsWhileFiring)
173 { // do not lock mutex while notifying (#i93514#) to prevent deadlock
174 ::osl::MutexGuard aGuard( m_aMutex );
175 m_isModified = bModified;
177 if (bModified) {
178 try {
179 Reference<XInterface> xThis(*this);
180 EventObject event(xThis);
181 m_NotifyListeners.notifyEach(
182 &XModifyListener::modified, event);
183 } catch (RuntimeException &) {
184 if (!bIgnoreRuntimeExceptionsWhileFiring) {
185 throw;
187 } catch (Exception &) {
188 // ignore
194 sal_Bool SAL_CALL OPropertyBag::isModified()
195 throw (RuntimeException, std::exception)
197 ::osl::MutexGuard aGuard( m_aMutex );
198 return m_isModified;
201 void SAL_CALL OPropertyBag::setModified( sal_Bool bModified )
202 throw (PropertyVetoException, RuntimeException, std::exception)
204 setModifiedImpl(bModified, false);
207 void SAL_CALL OPropertyBag::addModifyListener(
208 const Reference< XModifyListener > & xListener)
209 throw (RuntimeException, std::exception)
211 m_NotifyListeners.addInterface(xListener);
214 void SAL_CALL OPropertyBag::removeModifyListener(
215 const Reference< XModifyListener > & xListener)
216 throw (RuntimeException, std::exception)
218 m_NotifyListeners.removeInterface(xListener);
222 Reference< XPropertySetInfo > SAL_CALL OPropertyBag::getPropertySetInfo( ) throw(RuntimeException, std::exception)
224 return createPropertySetInfo( getInfoHelper() );
228 sal_Bool SAL_CALL OPropertyBag::has( const Any& /*aElement*/ ) throw (RuntimeException, std::exception)
230 // XSet is only a workaround for addProperty not being able to add default-void properties.
231 // So, everything of XSet except insert is implemented empty
232 return sal_False;
236 void SAL_CALL OPropertyBag::insert( const Any& _element ) throw (IllegalArgumentException, ElementExistException, RuntimeException, std::exception)
238 // This is a workaround for addProperty not being able to add default-void properties.
239 // If we ever have a smarter XPropertyContainer::addProperty interface, we can remove this, ehm, well, hack.
240 Property aProperty;
241 if ( !( _element >>= aProperty ) )
242 throw IllegalArgumentException( OUString(), *this, 1 );
244 ::osl::ClearableMutexGuard g( m_aMutex );
246 // check whether the type is allowed, everything else will be checked
247 // by m_aDynamicProperties
248 if ( !m_aAllowedTypes.empty()
249 && m_aAllowedTypes.find( aProperty.Type ) == m_aAllowedTypes.end()
251 throw IllegalTypeException( OUString(), *this );
253 m_aDynamicProperties.addVoidProperty( aProperty.Name, aProperty.Type, findFreeHandle(), aProperty.Attributes );
255 // our property info is dirty
256 m_pArrayHelper.reset();
258 g.clear();
259 setModified(sal_True);
263 void SAL_CALL OPropertyBag::remove( const Any& /*aElement*/ ) throw (IllegalArgumentException, NoSuchElementException, RuntimeException, std::exception)
265 // XSet is only a workaround for addProperty not being able to add default-void properties.
266 // So, everything of XSet except insert is implemented empty
267 throw NoSuchElementException( OUString(), *this );
272 Reference< XEnumeration > SAL_CALL OPropertyBag::createEnumeration( ) throw (RuntimeException, std::exception)
274 // XSet is only a workaround for addProperty not being able to add default-void properties.
275 // So, everything of XSet except insert is implemented empty
276 return NULL;
280 Type SAL_CALL OPropertyBag::getElementType( ) throw (RuntimeException, std::exception)
282 // XSet is only a workaround for addProperty not being able to add default-void properties.
283 // So, everything of XSet except insert is implemented empty
284 return Type();
288 sal_Bool SAL_CALL OPropertyBag::hasElements( ) throw (RuntimeException, std::exception)
290 // XSet is only a workaround for addProperty not being able to add default-void properties.
291 // So, everything of XSet except insert is implemented empty
292 return sal_False;
296 void SAL_CALL OPropertyBag::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
298 m_aDynamicProperties.getFastPropertyValue( _nHandle, _rValue );
302 sal_Bool SAL_CALL OPropertyBag::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue ) throw (IllegalArgumentException)
304 return m_aDynamicProperties.convertFastPropertyValue( _nHandle, _rValue, _rConvertedValue, _rOldValue );
308 void SAL_CALL OPropertyBag::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw (Exception, std::exception)
310 m_aDynamicProperties.setFastPropertyValue( nHandle, rValue );
314 ::cppu::IPropertyArrayHelper& SAL_CALL OPropertyBag::getInfoHelper()
316 if ( !m_pArrayHelper.get() )
318 Sequence< Property > aProperties;
319 m_aDynamicProperties.describeProperties( aProperties );
320 m_pArrayHelper.reset( new ::cppu::OPropertyArrayHelper( aProperties ) );
322 return *m_pArrayHelper;
327 sal_Int32 OPropertyBag::findFreeHandle() const
329 const sal_Int32 nPrime = 1009;
330 const sal_Int32 nSeed = 11;
332 sal_Int32 nCheck = nSeed;
333 while ( m_aDynamicProperties.hasPropertyByHandle( nCheck ) && ( nCheck != 1 ) )
335 nCheck = ( nCheck * nSeed ) % nPrime;
338 if ( nCheck == 1 )
339 { // uh ... we already have 1008 handles used up
340 // -> simply count upwards
341 while ( m_aDynamicProperties.hasPropertyByHandle( nCheck ) )
342 ++nCheck;
345 return nCheck;
349 void SAL_CALL OPropertyBag::addProperty( const OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue ) throw (PropertyExistException, IllegalTypeException, IllegalArgumentException, RuntimeException, std::exception)
351 ::osl::ClearableMutexGuard g( m_aMutex );
353 // check whether the type is allowed, everything else will be checked
354 // by m_aDynamicProperties
355 Type aPropertyType = _rInitialValue.getValueType();
356 if ( _rInitialValue.hasValue()
357 && !m_aAllowedTypes.empty()
358 && m_aAllowedTypes.find( aPropertyType ) == m_aAllowedTypes.end()
360 throw IllegalTypeException( OUString(), *this );
362 m_aDynamicProperties.addProperty( _rName, findFreeHandle(), _nAttributes, _rInitialValue );
364 // our property info is dirty
365 m_pArrayHelper.reset();
367 g.clear();
368 setModified(sal_True);
372 void SAL_CALL OPropertyBag::removeProperty( const OUString& _rName ) throw (UnknownPropertyException, NotRemoveableException, RuntimeException, std::exception)
374 ::osl::ClearableMutexGuard g( m_aMutex );
376 m_aDynamicProperties.removeProperty( _rName );
378 // our property info is dirty
379 m_pArrayHelper.reset();
381 g.clear();
382 setModified(sal_True);
386 namespace
388 struct ComparePropertyValueByName : public ::std::binary_function< PropertyValue, PropertyValue, bool >
390 bool operator()( const PropertyValue& _rLHS, const PropertyValue& _rRHS )
392 return _rLHS.Name < _rRHS.Name;
396 template< typename CLASS >
397 struct TransformPropertyToName : public ::std::unary_function< CLASS, OUString >
399 const OUString& operator()( const CLASS& _rProp )
401 return _rProp.Name;
405 struct ExtractPropertyValue : public ::std::unary_function< PropertyValue, Any >
407 const Any& operator()( const PropertyValue& _rProp )
409 return _rProp.Value;
415 Sequence< PropertyValue > SAL_CALL OPropertyBag::getPropertyValues( ) throw (RuntimeException, std::exception)
417 ::osl::MutexGuard aGuard( m_aMutex );
419 // all registered properties
420 Sequence< Property > aProperties;
421 m_aDynamicProperties.describeProperties( aProperties );
423 // their names
424 Sequence< OUString > aNames( aProperties.getLength() );
425 ::std::transform(
426 aProperties.getConstArray(),
427 aProperties.getConstArray() + aProperties.getLength(),
428 aNames.getArray(),
429 TransformPropertyToName< Property >()
432 // their values
433 Sequence< Any > aValues;
436 aValues = OPropertyBag_PBase::getPropertyValues( aNames );
437 if ( aValues.getLength() != aNames.getLength() )
438 throw RuntimeException();
440 catch( const RuntimeException& )
442 throw;
444 catch( const Exception& )
446 // ignore
449 // merge names and values, and retrieve the state/handle
450 ::cppu::IPropertyArrayHelper& rPropInfo = getInfoHelper();
452 Sequence< PropertyValue > aPropertyValues( aNames.getLength() );
453 const OUString* pName = aNames.getConstArray();
454 const OUString* pNamesEnd = aNames.getConstArray() + aNames.getLength();
455 const Any* pValue = aValues.getArray();
456 PropertyValue* pPropertyValue = aPropertyValues.getArray();
458 for ( ; pName != pNamesEnd; ++pName, ++pValue, ++pPropertyValue )
460 pPropertyValue->Name = *pName;
461 pPropertyValue->Handle = rPropInfo.getHandleByName( *pName );
462 pPropertyValue->Value = *pValue;
463 pPropertyValue->State = getPropertyStateByHandle( pPropertyValue->Handle );
466 return aPropertyValues;
470 void OPropertyBag::impl_setPropertyValues_throw( const Sequence< PropertyValue >& _rProps )
472 // sort (the XMultiPropertySet interface requires this)
473 Sequence< PropertyValue > aProperties( _rProps );
474 ::std::sort(
475 aProperties.getArray(),
476 aProperties.getArray() + aProperties.getLength(),
477 ComparePropertyValueByName()
480 // a sequence of names
481 Sequence< OUString > aNames( aProperties.getLength() );
482 ::std::transform(
483 aProperties.getConstArray(),
484 aProperties.getConstArray() + aProperties.getLength(),
485 aNames.getArray(),
486 TransformPropertyToName< PropertyValue >()
491 // check for unknown properties
492 // we cannot simply rely on the XMultiPropertySet::setPropertyValues
493 // implementation of our base class, since it does not throw
494 // an UnknownPropertyException. More precise, XMultiPropertySet::setPropertyValues
495 // does not allow to throw this exception, while XPropertyAccess::setPropertyValues
496 // requires it
497 sal_Int32 nCount = aNames.getLength();
499 Sequence< sal_Int32 > aHandles( nCount );
500 sal_Int32* pHandle = aHandles.getArray();
501 const PropertyValue* pProperty = aProperties.getConstArray();
502 for ( const OUString* pName = aNames.getConstArray();
503 pName != aNames.getConstArray() + aNames.getLength();
504 ++pName, ++pHandle, ++pProperty
507 ::cppu::IPropertyArrayHelper& rPropInfo = getInfoHelper();
508 *pHandle = rPropInfo.getHandleByName( *pName );
509 if ( *pHandle != -1 )
510 continue;
512 // there's a property requested which we do not know
513 if ( m_bAutoAddProperties )
515 // add the property
516 sal_Int16 nAttributes = PropertyAttribute::BOUND | PropertyAttribute::REMOVABLE | PropertyAttribute::MAYBEDEFAULT;
517 addProperty( *pName, nAttributes, pProperty->Value );
518 continue;
521 // no way out
522 throw UnknownPropertyException( *pName, *this );
525 // a sequence of values
526 Sequence< Any > aValues( aProperties.getLength() );
527 ::std::transform(
528 aProperties.getConstArray(),
529 aProperties.getConstArray() + aProperties.getLength(),
530 aValues.getArray(),
531 ExtractPropertyValue()
534 setFastPropertyValues( nCount, aHandles.getArray(), aValues.getConstArray(), nCount );
536 catch( const PropertyVetoException& ) { throw; }
537 catch( const IllegalArgumentException& ) { throw; }
538 catch( const WrappedTargetException& ) { throw; }
539 catch( const RuntimeException& ) { throw; }
540 catch( const UnknownPropertyException& ) { throw; }
541 catch( const Exception& )
543 throw WrappedTargetException( OUString(), *this, ::cppu::getCaughtException() );
548 void SAL_CALL OPropertyBag::setPropertyValues( const Sequence< PropertyValue >& _rProps ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
550 ::osl::MutexGuard aGuard( m_aMutex );
551 impl_setPropertyValues_throw( _rProps );
555 PropertyState OPropertyBag::getPropertyStateByHandle( sal_Int32 _nHandle )
557 // for properties which do not support the MAYBEDEFAULT attribute, don't rely on the base class, but
558 // assume they're always in DIRECT state.
559 // (Note that this probably would belong into the base class. However, this would mean we would need
560 // to check all existent usages of the base class, where MAYBEDEFAULT is *not* set, but
561 // a default is nonetheless supplied/used. This is hard to accomplish reliably, in the
562 // current phase.
563 // #i78593# / 2007-07-07 / frank.schoenheit@sun.com
565 ::cppu::IPropertyArrayHelper& rPropInfo = getInfoHelper();
566 sal_Int16 nAttributes(0);
567 OSL_VERIFY( rPropInfo.fillPropertyMembersByHandle( NULL, &nAttributes, _nHandle ) );
568 if ( ( nAttributes & PropertyAttribute::MAYBEDEFAULT ) == 0 )
569 return PropertyState_DIRECT_VALUE;
571 return OPropertyBag_PBase::getPropertyStateByHandle( _nHandle );
575 Any OPropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle ) const
577 Any aDefault;
578 m_aDynamicProperties.getPropertyDefaultByHandle( _nHandle, aDefault );
579 return aDefault;
583 } // namespace comphelper
586 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */