bump product version to 5.0.4.1
[LibreOffice.git] / chart2 / source / tools / WrappedPropertySet.cxx
blob4ef8e87382790446e0b2c71a0817e2ec4b0c445e
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 .
20 #include "WrappedPropertySet.hxx"
21 #include "macros.hxx"
23 #include <tools/solar.h>
25 namespace chart
28 using namespace ::com::sun::star;
29 using ::com::sun::star::uno::Reference;
30 using ::com::sun::star::uno::Sequence;
31 using ::com::sun::star::uno::Any;
33 WrappedPropertySet::WrappedPropertySet()
34 : MutexContainer()
35 , m_xInfo(0)
36 , m_pPropertyArrayHelper(0)
37 , m_pWrappedPropertyMap(0)
40 WrappedPropertySet::~WrappedPropertySet()
42 clearWrappedPropertySet();
45 Reference< beans::XPropertyState > WrappedPropertySet::getInnerPropertyState()
47 return Reference< beans::XPropertyState >( getInnerPropertySet(), uno::UNO_QUERY );
50 void WrappedPropertySet::clearWrappedPropertySet()
52 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
54 //delete all wrapped properties
55 if(m_pWrappedPropertyMap)
57 for( tWrappedPropertyMap::iterator aIt = m_pWrappedPropertyMap->begin()
58 ; aIt!= m_pWrappedPropertyMap->end(); ++aIt )
60 const WrappedProperty* pWrappedProperty = (*aIt).second;
61 DELETEZ(pWrappedProperty);
65 DELETEZ(m_pPropertyArrayHelper);
66 DELETEZ(m_pWrappedPropertyMap);
68 m_xInfo = NULL;
71 //XPropertySet
72 Reference< beans::XPropertySetInfo > SAL_CALL WrappedPropertySet::getPropertySetInfo( )
73 throw (uno::RuntimeException, std::exception)
75 Reference< beans::XPropertySetInfo > xInfo = m_xInfo;
76 if( !xInfo.is() )
78 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
79 xInfo = m_xInfo;
80 if( !xInfo.is() )
82 xInfo = ::cppu::OPropertySetHelper::createPropertySetInfo( getInfoHelper() );
83 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
84 m_xInfo = xInfo;
87 else
89 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
91 return m_xInfo;
94 void SAL_CALL WrappedPropertySet::setPropertyValue( const OUString& rPropertyName, const Any& rValue )
95 throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
97 try
99 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
100 const WrappedProperty* pWrappedProperty = getWrappedProperty( nHandle );
101 Reference< beans::XPropertySet > xInnerPropertySet( this->getInnerPropertySet() );
102 if( pWrappedProperty )
103 pWrappedProperty->setPropertyValue( rValue, xInnerPropertySet );
104 else if( xInnerPropertySet.is() )
105 xInnerPropertySet->setPropertyValue( rPropertyName, rValue );
106 else
108 #if OSL_DEBUG_LEVEL > 1
109 OSL_FAIL("found no inner property set to map to");
110 #endif
113 catch( const beans::UnknownPropertyException& )
115 throw;
117 catch( const beans::PropertyVetoException& )
119 throw;
121 catch( const lang::IllegalArgumentException& )
123 throw;
125 catch( const lang::WrappedTargetException& )
127 throw;
129 catch( const uno::RuntimeException& )
131 throw;
133 catch( const uno::Exception& ex )
135 OSL_FAIL("invalid exception caught in WrappedPropertySet::setPropertyValue");
136 lang::WrappedTargetException aWrappedException;
137 aWrappedException.TargetException = uno::makeAny( ex );
138 throw aWrappedException;
141 Any SAL_CALL WrappedPropertySet::getPropertyValue( const OUString& rPropertyName )
142 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
144 Any aRet;
148 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
149 const WrappedProperty* pWrappedProperty = getWrappedProperty( nHandle );
150 Reference< beans::XPropertySet > xInnerPropertySet( this->getInnerPropertySet() );
151 if( pWrappedProperty )
152 aRet = pWrappedProperty->getPropertyValue( xInnerPropertySet );
153 else if( xInnerPropertySet.is() )
154 aRet = xInnerPropertySet->getPropertyValue( rPropertyName );
155 else
157 #if OSL_DEBUG_LEVEL > 1
158 OSL_FAIL("found no inner property set to map to");
159 #endif
162 catch( const beans::UnknownPropertyException& )
164 throw;
166 catch( const lang::WrappedTargetException& )
168 throw;
170 catch( const uno::RuntimeException& )
172 throw;
174 catch( const uno::Exception& ex )
176 OSL_FAIL("invalid exception caught in WrappedPropertySet::setPropertyValue");
177 lang::WrappedTargetException aWrappedException;
178 aWrappedException.TargetException = uno::makeAny( ex );
179 throw aWrappedException;
182 return aRet;
185 void SAL_CALL WrappedPropertySet::addPropertyChangeListener( const OUString& rPropertyName, const Reference< beans::XPropertyChangeListener >& xListener )
186 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
188 Reference< beans::XPropertySet > xInnerPropertySet( this->getInnerPropertySet() );
189 if( xInnerPropertySet.is() )
191 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
192 if( pWrappedProperty )
193 xInnerPropertySet->addPropertyChangeListener( pWrappedProperty->getInnerName(), xListener );
194 else
195 xInnerPropertySet->addPropertyChangeListener( rPropertyName, xListener );
198 void SAL_CALL WrappedPropertySet::removePropertyChangeListener( const OUString& rPropertyName, const Reference< beans::XPropertyChangeListener >& aListener )
199 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
201 Reference< beans::XPropertySet > xInnerPropertySet( this->getInnerPropertySet() );
202 if( xInnerPropertySet.is() )
204 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
205 if( pWrappedProperty )
206 xInnerPropertySet->removePropertyChangeListener( pWrappedProperty->getInnerName(), aListener );
207 else
208 xInnerPropertySet->removePropertyChangeListener( rPropertyName, aListener );
211 void SAL_CALL WrappedPropertySet::addVetoableChangeListener( const OUString& rPropertyName, const Reference< beans::XVetoableChangeListener >& aListener )
212 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
214 Reference< beans::XPropertySet > xInnerPropertySet( this->getInnerPropertySet() );
215 if( xInnerPropertySet.is() )
217 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
218 if( pWrappedProperty )
219 xInnerPropertySet->addVetoableChangeListener( pWrappedProperty->getInnerName(), aListener );
220 else
221 xInnerPropertySet->addVetoableChangeListener( rPropertyName, aListener );
224 void SAL_CALL WrappedPropertySet::removeVetoableChangeListener( const OUString& rPropertyName, const Reference< beans::XVetoableChangeListener >& aListener )
225 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
227 Reference< beans::XPropertySet > xInnerPropertySet( this->getInnerPropertySet() );
228 if( xInnerPropertySet.is() )
230 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
231 if( pWrappedProperty )
232 xInnerPropertySet->removeVetoableChangeListener( pWrappedProperty->getInnerName(), aListener );
233 else
234 xInnerPropertySet->removeVetoableChangeListener( rPropertyName, aListener );
238 //XMultiPropertySet
239 void SAL_CALL WrappedPropertySet::setPropertyValues( const Sequence< OUString >& rNameSeq, const Sequence< Any >& rValueSeq )
240 throw (beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
242 bool bUnknownProperty = false;
243 sal_Int32 nMinCount = std::min( rValueSeq.getLength(), rNameSeq.getLength() );
244 for(sal_Int32 nN=0; nN<nMinCount; nN++)
246 OUString aPropertyName( rNameSeq[nN] );
249 this->setPropertyValue( aPropertyName, rValueSeq[nN] );
251 catch( const beans::UnknownPropertyException& ex )
253 ASSERT_EXCEPTION( ex );
254 bUnknownProperty = true;
257 //todo: store unknown properties elsewhere
258 OSL_ENSURE(!bUnknownProperty,"unknown property");
259 (void)bUnknownProperty;
260 // if( bUnknownProperty )
261 // throw beans::UnknownPropertyException();
263 Sequence< Any > SAL_CALL WrappedPropertySet::getPropertyValues( const Sequence< OUString >& rNameSeq )
264 throw (uno::RuntimeException, std::exception)
266 Sequence< Any > aRetSeq;
267 if( rNameSeq.getLength() )
269 aRetSeq.realloc( rNameSeq.getLength() );
270 for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
274 OUString aPropertyName( rNameSeq[nN] );
275 aRetSeq[nN] = this->getPropertyValue( aPropertyName );
277 catch( const beans::UnknownPropertyException& ex )
279 ASSERT_EXCEPTION( ex );
281 catch( const lang::WrappedTargetException& ex )
283 ASSERT_EXCEPTION( ex );
287 return aRetSeq;
289 void SAL_CALL WrappedPropertySet::addPropertiesChangeListener( const Sequence< OUString >& /* rNameSeq */, const Reference< beans::XPropertiesChangeListener >& /* xListener */ )
290 throw (uno::RuntimeException, std::exception)
292 OSL_FAIL("not implemented yet");
293 //todo
295 void SAL_CALL WrappedPropertySet::removePropertiesChangeListener( const Reference< beans::XPropertiesChangeListener >& /* xListener */ )
296 throw (uno::RuntimeException, std::exception)
298 OSL_FAIL("not implemented yet");
299 //todo
301 void SAL_CALL WrappedPropertySet::firePropertiesChangeEvent( const Sequence< OUString >& /* rNameSeq */, const Reference< beans::XPropertiesChangeListener >& /* xListener */ )
302 throw (uno::RuntimeException, std::exception)
304 OSL_FAIL("not implemented yet");
305 //todo
308 //XPropertyState
309 beans::PropertyState SAL_CALL WrappedPropertySet::getPropertyState( const OUString& rPropertyName )
310 throw (beans::UnknownPropertyException, uno::RuntimeException, std::exception)
312 beans::PropertyState aState( beans::PropertyState_DIRECT_VALUE );
314 Reference< beans::XPropertyState > xInnerPropertyState( this->getInnerPropertyState() );
315 if( xInnerPropertyState.is() )
317 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
318 if( pWrappedProperty )
319 aState = pWrappedProperty->getPropertyState( xInnerPropertyState );
320 else
321 aState = xInnerPropertyState->getPropertyState( rPropertyName );
323 return aState;
326 const WrappedProperty* WrappedPropertySet::getWrappedProperty( const OUString& rOuterName )
328 sal_Int32 nHandle = getInfoHelper().getHandleByName( rOuterName );
329 return getWrappedProperty( nHandle );
332 const WrappedProperty* WrappedPropertySet::getWrappedProperty( sal_Int32 nHandle )
334 tWrappedPropertyMap::const_iterator aFound( getWrappedPropertyMap().find( nHandle ) );
335 if( aFound != getWrappedPropertyMap().end() )
336 return (*aFound).second;
337 return 0;
340 Sequence< beans::PropertyState > SAL_CALL WrappedPropertySet::getPropertyStates( const Sequence< OUString >& rNameSeq )
341 throw (beans::UnknownPropertyException, uno::RuntimeException, std::exception)
343 Sequence< beans::PropertyState > aRetSeq;
344 if( rNameSeq.getLength() )
346 aRetSeq.realloc( rNameSeq.getLength() );
347 for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
349 OUString aPropertyName( rNameSeq[nN] );
350 aRetSeq[nN] = this->getPropertyState( aPropertyName );
353 return aRetSeq;
356 void SAL_CALL WrappedPropertySet::setPropertyToDefault( const OUString& rPropertyName )
357 throw (beans::UnknownPropertyException, uno::RuntimeException, std::exception)
359 Reference< beans::XPropertyState > xInnerPropertyState( this->getInnerPropertyState() );
360 if( xInnerPropertyState.is() )
362 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
363 if( pWrappedProperty )
364 pWrappedProperty->setPropertyToDefault( xInnerPropertyState );
365 else
366 xInnerPropertyState->setPropertyToDefault( rPropertyName );
369 Any SAL_CALL WrappedPropertySet::getPropertyDefault( const OUString& rPropertyName )
370 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
372 Any aRet;
373 Reference< beans::XPropertyState > xInnerPropertyState( this->getInnerPropertyState() );
374 if( xInnerPropertyState.is() )
376 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
377 if( pWrappedProperty )
378 aRet = pWrappedProperty->getPropertyDefault(xInnerPropertyState);
379 else
380 aRet = xInnerPropertyState->getPropertyDefault( rPropertyName );
382 return aRet;
385 //XMultiPropertyStates
386 void SAL_CALL WrappedPropertySet::setAllPropertiesToDefault( )
387 throw (uno::RuntimeException, std::exception)
389 const Sequence< beans::Property >& rPropSeq = getPropertySequence();
390 for(sal_Int32 nN=0; nN<rPropSeq.getLength(); nN++)
392 OUString aPropertyName( rPropSeq[nN].Name );
393 this->setPropertyToDefault( aPropertyName );
396 void SAL_CALL WrappedPropertySet::setPropertiesToDefault( const Sequence< OUString >& rNameSeq )
397 throw (beans::UnknownPropertyException, uno::RuntimeException, std::exception)
399 for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
401 OUString aPropertyName( rNameSeq[nN] );
402 this->setPropertyToDefault( aPropertyName );
405 Sequence< Any > SAL_CALL WrappedPropertySet::getPropertyDefaults( const Sequence< OUString >& rNameSeq )
406 throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
408 Sequence< Any > aRetSeq;
409 if( rNameSeq.getLength() )
411 aRetSeq.realloc( rNameSeq.getLength() );
412 for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
414 OUString aPropertyName( rNameSeq[nN] );
415 aRetSeq[nN] = this->getPropertyDefault( aPropertyName );
418 return aRetSeq;
421 ::cppu::IPropertyArrayHelper& WrappedPropertySet::getInfoHelper()
423 ::cppu::OPropertyArrayHelper* p = m_pPropertyArrayHelper;
424 if(!p)
426 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
427 p = m_pPropertyArrayHelper;
428 if(!p)
430 p = new ::cppu::OPropertyArrayHelper( getPropertySequence(), sal_True );
431 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
432 m_pPropertyArrayHelper = p;
435 else
437 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
439 return *m_pPropertyArrayHelper;
442 tWrappedPropertyMap& WrappedPropertySet::getWrappedPropertyMap()
444 tWrappedPropertyMap* p = m_pWrappedPropertyMap;
445 if(!p)
447 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
448 p = m_pWrappedPropertyMap;
449 if(!p)
451 std::vector< WrappedProperty* > aPropList( createWrappedProperties() );
452 p = new tWrappedPropertyMap();
454 for( std::vector< WrappedProperty* >::const_iterator aIt = aPropList.begin(); aIt!=aPropList.end(); ++aIt )
456 WrappedProperty* pProperty = *aIt;
457 if(pProperty)
459 sal_Int32 nHandle = getInfoHelper().getHandleByName( pProperty->getOuterName() );
461 if( nHandle == -1 )
463 OSL_FAIL( "missing property in property list" );
464 delete pProperty;//we are owner or the created WrappedProperties
466 else if( p->find( nHandle ) != p->end() )
468 //duplicate Wrapped property
469 OSL_FAIL( "duplicate Wrapped property" );
470 delete pProperty;//we are owner or the created WrappedProperties
472 else
473 (*p)[ nHandle ] = pProperty;
477 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
478 m_pWrappedPropertyMap = p;
481 else
483 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
485 return *m_pWrappedPropertyMap;
488 } //namespace chart
490 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */