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 0 : void createRegistryInfo_OPropertyBag()
44 : {
45 0 : static ::comphelper::module::OAutoRegistration< ::comphelper::OPropertyBag > aAutoRegistration;
46 0 : }
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 0 : OPropertyBag::OPropertyBag()
64 0 : :OPropertyBag_PBase( GetBroadcastHelper(), this )
65 : ,::cppu::IEventNotificationHook()
66 : ,m_bAutoAddProperties( false )
67 : ,m_NotifyListeners(m_aMutex)
68 0 : ,m_isModified(false)
69 :
70 : {
71 0 : }
72 :
73 :
74 0 : OPropertyBag::~OPropertyBag()
75 : {
76 0 : }
77 :
78 :
79 0 : IMPLEMENT_FORWARD_XINTERFACE2( OPropertyBag, OPropertyBag_Base, OPropertyBag_PBase )
80 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyBag, OPropertyBag_Base, OPropertyBag_PBase )
81 :
82 :
83 0 : Sequence< OUString > OPropertyBag::getSupportedServiceNames_static() throw( RuntimeException )
84 : {
85 0 : Sequence< OUString > aServices(1);
86 0 : aServices[0] = "com.sun.star.beans.PropertyBag";
87 0 : return aServices;
88 : }
89 :
90 :
91 0 : void SAL_CALL OPropertyBag::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException, std::exception)
92 : {
93 0 : Sequence< Type > aTypes;
94 0 : bool AllowEmptyPropertyName(false);
95 0 : bool AutomaticAddition(false);
96 :
97 0 : if (_rArguments.getLength() == 3
98 0 : && (_rArguments[0] >>= aTypes)
99 0 : && (_rArguments[1] >>= AllowEmptyPropertyName)
100 0 : && (_rArguments[2] >>= AutomaticAddition))
101 : {
102 : ::std::copy(
103 : aTypes.getConstArray(),
104 0 : aTypes.getConstArray() + aTypes.getLength(),
105 : ::std::insert_iterator< TypeBag >( m_aAllowedTypes, m_aAllowedTypes.begin() )
106 0 : );
107 0 : m_bAutoAddProperties = AutomaticAddition;
108 :
109 : } else {
110 0 : ::comphelper::NamedValueCollection aArguments( _rArguments );
111 :
112 0 : 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 0 : aArguments.get_ensureType( "AutomaticAddition", m_bAutoAddProperties );
120 : aArguments.get_ensureType( "AllowEmptyPropertyName",
121 0 : AllowEmptyPropertyName );
122 : }
123 0 : if (AllowEmptyPropertyName) {
124 : m_aDynamicProperties.setAllowEmptyPropertyName(
125 0 : AllowEmptyPropertyName);
126 0 : }
127 0 : }
128 :
129 :
130 0 : OUString OPropertyBag::getImplementationName_static() throw( RuntimeException )
131 : {
132 0 : return OUString( "com.sun.star.comp.comphelper.OPropertyBag" );
133 : }
134 :
135 :
136 0 : Reference< XInterface > SAL_CALL OPropertyBag::Create( SAL_UNUSED_PARAMETER const Reference< XComponentContext >& )
137 : {
138 0 : 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 0 : void OPropertyBag::fireEvents(
160 : sal_Int32 * /*pnHandles*/,
161 : sal_Int32 nCount,
162 : sal_Bool bVetoable,
163 : bool bIgnoreRuntimeExceptionsWhileFiring)
164 : {
165 0 : if (nCount && !bVetoable) {
166 0 : setModifiedImpl(true, bIgnoreRuntimeExceptionsWhileFiring);
167 : }
168 0 : }
169 :
170 0 : void OPropertyBag::setModifiedImpl(bool bModified,
171 : bool bIgnoreRuntimeExceptionsWhileFiring)
172 : {
173 : { // do not lock mutex while notifying (#i93514#) to prevent deadlock
174 0 : ::osl::MutexGuard aGuard( m_aMutex );
175 0 : m_isModified = bModified;
176 : }
177 0 : if (bModified) {
178 : try {
179 0 : Reference<XInterface> xThis(*this);
180 0 : EventObject event(xThis);
181 : m_NotifyListeners.notifyEach(
182 0 : &XModifyListener::modified, event);
183 0 : } catch (RuntimeException &) {
184 0 : if (!bIgnoreRuntimeExceptionsWhileFiring) {
185 0 : throw;
186 : }
187 0 : } catch (Exception &) {
188 : // ignore
189 : }
190 : }
191 0 : }
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 0 : void SAL_CALL OPropertyBag::setModified( sal_Bool bModified )
202 : throw (PropertyVetoException, RuntimeException, std::exception)
203 : {
204 0 : setModifiedImpl(bModified, false);
205 0 : }
206 :
207 0 : void SAL_CALL OPropertyBag::addModifyListener(
208 : const Reference< XModifyListener > & xListener)
209 : throw (RuntimeException, std::exception)
210 : {
211 0 : m_NotifyListeners.addInterface(xListener);
212 0 : }
213 :
214 0 : void SAL_CALL OPropertyBag::removeModifyListener(
215 : const Reference< XModifyListener > & xListener)
216 : throw (RuntimeException, std::exception)
217 : {
218 0 : m_NotifyListeners.removeInterface(xListener);
219 0 : }
220 :
221 :
222 0 : Reference< XPropertySetInfo > SAL_CALL OPropertyBag::getPropertySetInfo( ) throw(RuntimeException, std::exception)
223 : {
224 0 : 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 0 : 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 0 : Property aProperty;
241 0 : if ( !( _element >>= aProperty ) )
242 0 : throw IllegalArgumentException( OUString(), *this, 1 );
243 :
244 0 : ::osl::ClearableMutexGuard g( m_aMutex );
245 :
246 : // check whether the type is allowed, everything else will be checked
247 : // by m_aDynamicProperties
248 0 : if ( !m_aAllowedTypes.empty()
249 0 : && m_aAllowedTypes.find( aProperty.Type ) == m_aAllowedTypes.end()
250 : )
251 0 : throw IllegalTypeException( OUString(), *this );
252 :
253 0 : m_aDynamicProperties.addVoidProperty( aProperty.Name, aProperty.Type, findFreeHandle(), aProperty.Attributes );
254 :
255 : // our property info is dirty
256 0 : m_pArrayHelper.reset();
257 :
258 0 : g.clear();
259 0 : setModified(sal_True);
260 0 : }
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 0 : void SAL_CALL OPropertyBag::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
297 : {
298 0 : m_aDynamicProperties.getFastPropertyValue( _nHandle, _rValue );
299 0 : }
300 :
301 :
302 0 : sal_Bool SAL_CALL OPropertyBag::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue ) throw (IllegalArgumentException)
303 : {
304 0 : return m_aDynamicProperties.convertFastPropertyValue( _nHandle, _rValue, _rConvertedValue, _rOldValue );
305 : }
306 :
307 :
308 0 : void SAL_CALL OPropertyBag::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw (Exception, std::exception)
309 : {
310 0 : m_aDynamicProperties.setFastPropertyValue( nHandle, rValue );
311 0 : }
312 :
313 :
314 0 : ::cppu::IPropertyArrayHelper& SAL_CALL OPropertyBag::getInfoHelper()
315 : {
316 0 : if ( !m_pArrayHelper.get() )
317 : {
318 0 : Sequence< Property > aProperties;
319 0 : m_aDynamicProperties.describeProperties( aProperties );
320 0 : m_pArrayHelper.reset( new ::cppu::OPropertyArrayHelper( aProperties ) );
321 : }
322 0 : return *m_pArrayHelper;
323 :
324 : }
325 :
326 :
327 0 : sal_Int32 OPropertyBag::findFreeHandle() const
328 : {
329 0 : const sal_Int32 nPrime = 1009;
330 0 : const sal_Int32 nSeed = 11;
331 :
332 0 : sal_Int32 nCheck = nSeed;
333 0 : while ( m_aDynamicProperties.hasPropertyByHandle( nCheck ) && ( nCheck != 1 ) )
334 : {
335 0 : nCheck = ( nCheck * nSeed ) % nPrime;
336 : }
337 :
338 0 : if ( nCheck == 1 )
339 : { // uh ... we already have 1008 handles used up
340 : // -> simply count upwards
341 0 : while ( m_aDynamicProperties.hasPropertyByHandle( nCheck ) )
342 0 : ++nCheck;
343 : }
344 :
345 0 : return nCheck;
346 : }
347 :
348 :
349 0 : void SAL_CALL OPropertyBag::addProperty( const OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue ) throw (PropertyExistException, IllegalTypeException, IllegalArgumentException, RuntimeException, std::exception)
350 : {
351 0 : ::osl::ClearableMutexGuard g( m_aMutex );
352 :
353 : // check whether the type is allowed, everything else will be checked
354 : // by m_aDynamicProperties
355 0 : Type aPropertyType = _rInitialValue.getValueType();
356 0 : if ( _rInitialValue.hasValue()
357 0 : && !m_aAllowedTypes.empty()
358 0 : && m_aAllowedTypes.find( aPropertyType ) == m_aAllowedTypes.end()
359 : )
360 0 : throw IllegalTypeException( OUString(), *this );
361 :
362 0 : m_aDynamicProperties.addProperty( _rName, findFreeHandle(), _nAttributes, _rInitialValue );
363 :
364 : // our property info is dirty
365 0 : m_pArrayHelper.reset();
366 :
367 0 : g.clear();
368 0 : setModified(sal_True);
369 0 : }
370 :
371 :
372 0 : void SAL_CALL OPropertyBag::removeProperty( const OUString& _rName ) throw (UnknownPropertyException, NotRemoveableException, RuntimeException, std::exception)
373 : {
374 0 : ::osl::ClearableMutexGuard g( m_aMutex );
375 :
376 0 : m_aDynamicProperties.removeProperty( _rName );
377 :
378 : // our property info is dirty
379 0 : m_pArrayHelper.reset();
380 :
381 0 : g.clear();
382 0 : setModified(sal_True);
383 0 : }
384 :
385 :
386 : namespace
387 : {
388 : struct ComparePropertyValueByName : public ::std::binary_function< PropertyValue, PropertyValue, bool >
389 : {
390 0 : bool operator()( const PropertyValue& _rLHS, const PropertyValue& _rRHS )
391 : {
392 0 : return _rLHS.Name < _rRHS.Name;
393 : }
394 : };
395 :
396 : template< typename CLASS >
397 : struct TransformPropertyToName : public ::std::unary_function< CLASS, OUString >
398 : {
399 0 : const OUString& operator()( const CLASS& _rProp )
400 : {
401 0 : return _rProp.Name;
402 : }
403 : };
404 :
405 : struct ExtractPropertyValue : public ::std::unary_function< PropertyValue, Any >
406 : {
407 0 : const Any& operator()( const PropertyValue& _rProp )
408 : {
409 0 : return _rProp.Value;
410 : }
411 : };
412 : }
413 :
414 :
415 0 : Sequence< PropertyValue > SAL_CALL OPropertyBag::getPropertyValues( ) throw (RuntimeException, std::exception)
416 : {
417 0 : ::osl::MutexGuard aGuard( m_aMutex );
418 :
419 : // all registered properties
420 0 : Sequence< Property > aProperties;
421 0 : m_aDynamicProperties.describeProperties( aProperties );
422 :
423 : // their names
424 0 : Sequence< OUString > aNames( aProperties.getLength() );
425 : ::std::transform(
426 : aProperties.getConstArray(),
427 0 : aProperties.getConstArray() + aProperties.getLength(),
428 : aNames.getArray(),
429 : TransformPropertyToName< Property >()
430 0 : );
431 :
432 : // their values
433 0 : Sequence< Any > aValues;
434 : try
435 : {
436 0 : aValues = OPropertyBag_PBase::getPropertyValues( aNames );
437 0 : if ( aValues.getLength() != aNames.getLength() )
438 0 : throw RuntimeException();
439 : }
440 0 : catch( const RuntimeException& )
441 : {
442 0 : throw;
443 : }
444 0 : catch( const Exception& )
445 : {
446 : // ignore
447 : }
448 :
449 : // merge names and values, and retrieve the state/handle
450 0 : ::cppu::IPropertyArrayHelper& rPropInfo = getInfoHelper();
451 :
452 0 : Sequence< PropertyValue > aPropertyValues( aNames.getLength() );
453 0 : const OUString* pName = aNames.getConstArray();
454 0 : const OUString* pNamesEnd = aNames.getConstArray() + aNames.getLength();
455 0 : const Any* pValue = aValues.getArray();
456 0 : PropertyValue* pPropertyValue = aPropertyValues.getArray();
457 :
458 0 : for ( ; pName != pNamesEnd; ++pName, ++pValue, ++pPropertyValue )
459 : {
460 0 : pPropertyValue->Name = *pName;
461 0 : pPropertyValue->Handle = rPropInfo.getHandleByName( *pName );
462 0 : pPropertyValue->Value = *pValue;
463 0 : pPropertyValue->State = getPropertyStateByHandle( pPropertyValue->Handle );
464 : }
465 :
466 0 : return aPropertyValues;
467 : }
468 :
469 :
470 0 : void OPropertyBag::impl_setPropertyValues_throw( const Sequence< PropertyValue >& _rProps )
471 : {
472 : // sort (the XMultiPropertySet interface requires this)
473 0 : Sequence< PropertyValue > aProperties( _rProps );
474 : ::std::sort(
475 : aProperties.getArray(),
476 0 : aProperties.getArray() + aProperties.getLength(),
477 : ComparePropertyValueByName()
478 0 : );
479 :
480 : // a sequence of names
481 0 : Sequence< OUString > aNames( aProperties.getLength() );
482 : ::std::transform(
483 : aProperties.getConstArray(),
484 0 : aProperties.getConstArray() + aProperties.getLength(),
485 : aNames.getArray(),
486 : TransformPropertyToName< PropertyValue >()
487 0 : );
488 :
489 : try
490 : {
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 0 : sal_Int32 nCount = aNames.getLength();
498 :
499 0 : Sequence< sal_Int32 > aHandles( nCount );
500 0 : sal_Int32* pHandle = aHandles.getArray();
501 0 : const PropertyValue* pProperty = aProperties.getConstArray();
502 0 : for ( const OUString* pName = aNames.getConstArray();
503 0 : pName != aNames.getConstArray() + aNames.getLength();
504 : ++pName, ++pHandle, ++pProperty
505 : )
506 : {
507 0 : ::cppu::IPropertyArrayHelper& rPropInfo = getInfoHelper();
508 0 : *pHandle = rPropInfo.getHandleByName( *pName );
509 0 : if ( *pHandle != -1 )
510 0 : continue;
511 :
512 : // there's a property requested which we do not know
513 0 : if ( m_bAutoAddProperties )
514 : {
515 : // add the property
516 0 : sal_Int16 nAttributes = PropertyAttribute::BOUND | PropertyAttribute::REMOVABLE | PropertyAttribute::MAYBEDEFAULT;
517 0 : addProperty( *pName, nAttributes, pProperty->Value );
518 0 : continue;
519 : }
520 :
521 : // no way out
522 0 : throw UnknownPropertyException( *pName, *this );
523 : }
524 :
525 : // a sequence of values
526 0 : Sequence< Any > aValues( aProperties.getLength() );
527 : ::std::transform(
528 : aProperties.getConstArray(),
529 0 : aProperties.getConstArray() + aProperties.getLength(),
530 : aValues.getArray(),
531 : ExtractPropertyValue()
532 0 : );
533 :
534 0 : setFastPropertyValues( nCount, aHandles.getArray(), aValues.getConstArray(), nCount );
535 : }
536 0 : catch( const PropertyVetoException& ) { throw; }
537 0 : catch( const IllegalArgumentException& ) { throw; }
538 0 : catch( const WrappedTargetException& ) { throw; }
539 0 : catch( const RuntimeException& ) { throw; }
540 0 : catch( const UnknownPropertyException& ) { throw; }
541 0 : catch( const Exception& )
542 : {
543 0 : throw WrappedTargetException( OUString(), *this, ::cppu::getCaughtException() );
544 0 : }
545 0 : }
546 :
547 :
548 0 : void SAL_CALL OPropertyBag::setPropertyValues( const Sequence< PropertyValue >& _rProps ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
549 : {
550 0 : ::osl::MutexGuard aGuard( m_aMutex );
551 0 : impl_setPropertyValues_throw( _rProps );
552 0 : }
553 :
554 :
555 0 : PropertyState OPropertyBag::getPropertyStateByHandle( sal_Int32 _nHandle )
556 : {
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
564 :
565 0 : ::cppu::IPropertyArrayHelper& rPropInfo = getInfoHelper();
566 0 : sal_Int16 nAttributes(0);
567 0 : OSL_VERIFY( rPropInfo.fillPropertyMembersByHandle( NULL, &nAttributes, _nHandle ) );
568 0 : if ( ( nAttributes & PropertyAttribute::MAYBEDEFAULT ) == 0 )
569 0 : return PropertyState_DIRECT_VALUE;
570 :
571 0 : return OPropertyBag_PBase::getPropertyStateByHandle( _nHandle );
572 : }
573 :
574 :
575 0 : Any OPropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle ) const
576 : {
577 0 : Any aDefault;
578 0 : m_aDynamicProperties.getPropertyDefaultByHandle( _nHandle, aDefault );
579 0 : return aDefault;
580 : }
581 :
582 :
583 : } // namespace comphelper
584 :
585 :
586 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|