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