1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <cppuhelper/propshlp.hxx>
23 #include <tools/diagnose_ex.h>
24 #include <sal/log.hxx>
29 using namespace ::com::sun::star
;
30 using ::com::sun::star::uno::Reference
;
31 using ::com::sun::star::uno::Sequence
;
32 using ::com::sun::star::uno::Any
;
34 WrappedPropertySet::WrappedPropertySet()
37 WrappedPropertySet::~WrappedPropertySet()
39 clearWrappedPropertySet();
42 Reference
< beans::XPropertyState
> WrappedPropertySet::getInnerPropertyState()
44 return Reference
< beans::XPropertyState
>( getInnerPropertySet(), uno::UNO_QUERY
);
47 void WrappedPropertySet::clearWrappedPropertySet()
49 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
51 m_pPropertyArrayHelper
.reset();
52 m_pWrappedPropertyMap
.reset();
58 Reference
< beans::XPropertySetInfo
> SAL_CALL
WrappedPropertySet::getPropertySetInfo( )
60 Reference
< beans::XPropertySetInfo
> xInfo
= m_xInfo
;
63 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
67 xInfo
= ::cppu::OPropertySetHelper::createPropertySetInfo( getInfoHelper() );
68 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
74 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
79 void SAL_CALL
WrappedPropertySet::setPropertyValue( const OUString
& rPropertyName
, const Any
& rValue
)
83 sal_Int32 nHandle
= getInfoHelper().getHandleByName( rPropertyName
);
84 const WrappedProperty
* pWrappedProperty
= getWrappedProperty( nHandle
);
85 Reference
< beans::XPropertySet
> xInnerPropertySet( getInnerPropertySet() );
86 if( pWrappedProperty
)
87 pWrappedProperty
->setPropertyValue( rValue
, xInnerPropertySet
);
88 else if( xInnerPropertySet
.is() )
89 xInnerPropertySet
->setPropertyValue( rPropertyName
, rValue
);
92 SAL_WARN("chart2.tools", "found no inner property set to map to");
95 catch( const beans::UnknownPropertyException
& )
99 catch( const beans::PropertyVetoException
& )
103 catch( const lang::IllegalArgumentException
& )
107 catch( const lang::WrappedTargetException
& )
111 catch( const uno::RuntimeException
& )
115 catch( const uno::Exception
& ex
)
117 css::uno::Any anyEx
= cppu::getCaughtException();
118 OSL_FAIL("invalid exception caught in WrappedPropertySet::setPropertyValue");
119 throw lang::WrappedTargetException( ex
.Message
, nullptr, anyEx
);
122 Any SAL_CALL
WrappedPropertySet::getPropertyValue( const OUString
& rPropertyName
)
128 sal_Int32 nHandle
= getInfoHelper().getHandleByName( rPropertyName
);
129 const WrappedProperty
* pWrappedProperty
= getWrappedProperty( nHandle
);
130 Reference
< beans::XPropertySet
> xInnerPropertySet( getInnerPropertySet() );
131 if( pWrappedProperty
)
132 aRet
= pWrappedProperty
->getPropertyValue( xInnerPropertySet
);
133 else if( xInnerPropertySet
.is() )
134 aRet
= xInnerPropertySet
->getPropertyValue( rPropertyName
);
137 SAL_WARN("chart2.tools", "found no inner property set to map to");
140 catch( const beans::UnknownPropertyException
& )
144 catch( const lang::WrappedTargetException
& )
148 catch( const uno::RuntimeException
& )
152 catch( const uno::Exception
& ex
)
154 css::uno::Any anyEx
= cppu::getCaughtException();
155 OSL_FAIL("invalid exception caught in WrappedPropertySet::setPropertyValue");
156 throw lang::WrappedTargetException( ex
.Message
, nullptr, anyEx
);
162 void SAL_CALL
WrappedPropertySet::addPropertyChangeListener( const OUString
& rPropertyName
, const Reference
< beans::XPropertyChangeListener
>& xListener
)
164 Reference
< beans::XPropertySet
> xInnerPropertySet( getInnerPropertySet() );
165 if( xInnerPropertySet
.is() )
167 const WrappedProperty
* pWrappedProperty
= getWrappedProperty( rPropertyName
);
168 if( pWrappedProperty
)
169 xInnerPropertySet
->addPropertyChangeListener( pWrappedProperty
->getInnerName(), xListener
);
171 xInnerPropertySet
->addPropertyChangeListener( rPropertyName
, xListener
);
174 void SAL_CALL
WrappedPropertySet::removePropertyChangeListener( const OUString
& rPropertyName
, const Reference
< beans::XPropertyChangeListener
>& aListener
)
176 Reference
< beans::XPropertySet
> xInnerPropertySet( getInnerPropertySet() );
177 if( xInnerPropertySet
.is() )
179 const WrappedProperty
* pWrappedProperty
= getWrappedProperty( rPropertyName
);
180 if( pWrappedProperty
)
181 xInnerPropertySet
->removePropertyChangeListener( pWrappedProperty
->getInnerName(), aListener
);
183 xInnerPropertySet
->removePropertyChangeListener( rPropertyName
, aListener
);
186 void SAL_CALL
WrappedPropertySet::addVetoableChangeListener( const OUString
& rPropertyName
, const Reference
< beans::XVetoableChangeListener
>& aListener
)
188 Reference
< beans::XPropertySet
> xInnerPropertySet( getInnerPropertySet() );
189 if( xInnerPropertySet
.is() )
191 const WrappedProperty
* pWrappedProperty
= getWrappedProperty( rPropertyName
);
192 if( pWrappedProperty
)
193 xInnerPropertySet
->addVetoableChangeListener( pWrappedProperty
->getInnerName(), aListener
);
195 xInnerPropertySet
->addVetoableChangeListener( rPropertyName
, aListener
);
198 void SAL_CALL
WrappedPropertySet::removeVetoableChangeListener( const OUString
& rPropertyName
, const Reference
< beans::XVetoableChangeListener
>& aListener
)
200 Reference
< beans::XPropertySet
> xInnerPropertySet( getInnerPropertySet() );
201 if( xInnerPropertySet
.is() )
203 const WrappedProperty
* pWrappedProperty
= getWrappedProperty( rPropertyName
);
204 if( pWrappedProperty
)
205 xInnerPropertySet
->removeVetoableChangeListener( pWrappedProperty
->getInnerName(), aListener
);
207 xInnerPropertySet
->removeVetoableChangeListener( rPropertyName
, aListener
);
212 void SAL_CALL
WrappedPropertySet::setPropertyValues( const Sequence
< OUString
>& rNameSeq
, const Sequence
< Any
>& rValueSeq
)
214 bool bUnknownProperty
= false;
215 sal_Int32 nMinCount
= std::min( rValueSeq
.getLength(), rNameSeq
.getLength() );
216 for(sal_Int32 nN
=0; nN
<nMinCount
; nN
++)
218 OUString
aPropertyName( rNameSeq
[nN
] );
221 setPropertyValue( aPropertyName
, rValueSeq
[nN
] );
223 catch( const beans::UnknownPropertyException
& )
225 DBG_UNHANDLED_EXCEPTION("chart2");
226 bUnknownProperty
= true;
229 //todo: store unknown properties elsewhere
230 OSL_ENSURE(!bUnknownProperty
,"unknown property");
231 // if( bUnknownProperty )
232 // throw beans::UnknownPropertyException();
234 Sequence
< Any
> SAL_CALL
WrappedPropertySet::getPropertyValues( const Sequence
< OUString
>& rNameSeq
)
236 Sequence
< Any
> aRetSeq
;
237 if( rNameSeq
.hasElements() )
239 aRetSeq
.realloc( rNameSeq
.getLength() );
240 for(sal_Int32 nN
=0; nN
<rNameSeq
.getLength(); nN
++)
244 OUString
aPropertyName( rNameSeq
[nN
] );
245 aRetSeq
[nN
] = getPropertyValue( aPropertyName
);
247 catch( const beans::UnknownPropertyException
& )
249 DBG_UNHANDLED_EXCEPTION("chart2");
251 catch( const lang::WrappedTargetException
& )
253 DBG_UNHANDLED_EXCEPTION("chart2");
259 void SAL_CALL
WrappedPropertySet::addPropertiesChangeListener( const Sequence
< OUString
>& /* rNameSeq */, const Reference
< beans::XPropertiesChangeListener
>& /* xListener */ )
261 OSL_FAIL("not implemented yet");
264 void SAL_CALL
WrappedPropertySet::removePropertiesChangeListener( const Reference
< beans::XPropertiesChangeListener
>& /* xListener */ )
266 OSL_FAIL("not implemented yet");
269 void SAL_CALL
WrappedPropertySet::firePropertiesChangeEvent( const Sequence
< OUString
>& /* rNameSeq */, const Reference
< beans::XPropertiesChangeListener
>& /* xListener */ )
271 OSL_FAIL("not implemented yet");
276 beans::PropertyState SAL_CALL
WrappedPropertySet::getPropertyState( const OUString
& rPropertyName
)
278 beans::PropertyState
aState( beans::PropertyState_DIRECT_VALUE
);
280 Reference
< beans::XPropertyState
> xInnerPropertyState( getInnerPropertyState() );
281 if( xInnerPropertyState
.is() )
283 const WrappedProperty
* pWrappedProperty
= getWrappedProperty( rPropertyName
);
284 if( pWrappedProperty
)
285 aState
= pWrappedProperty
->getPropertyState( xInnerPropertyState
);
287 aState
= xInnerPropertyState
->getPropertyState( rPropertyName
);
292 const WrappedProperty
* WrappedPropertySet::getWrappedProperty( const OUString
& rOuterName
)
294 sal_Int32 nHandle
= getInfoHelper().getHandleByName( rOuterName
);
295 return getWrappedProperty( nHandle
);
298 const WrappedProperty
* WrappedPropertySet::getWrappedProperty( sal_Int32 nHandle
)
300 tWrappedPropertyMap::const_iterator
aFound( getWrappedPropertyMap().find( nHandle
) );
301 if( aFound
!= getWrappedPropertyMap().end() )
302 return (*aFound
).second
.get();
306 Sequence
< beans::PropertyState
> SAL_CALL
WrappedPropertySet::getPropertyStates( const Sequence
< OUString
>& rNameSeq
)
308 Sequence
< beans::PropertyState
> aRetSeq
;
309 if( rNameSeq
.hasElements() )
311 aRetSeq
.realloc( rNameSeq
.getLength() );
312 for(sal_Int32 nN
=0; nN
<rNameSeq
.getLength(); nN
++)
314 OUString
aPropertyName( rNameSeq
[nN
] );
315 aRetSeq
[nN
] = getPropertyState( aPropertyName
);
321 void SAL_CALL
WrappedPropertySet::setPropertyToDefault( const OUString
& rPropertyName
)
323 Reference
< beans::XPropertyState
> xInnerPropertyState( getInnerPropertyState() );
324 if( xInnerPropertyState
.is() )
326 const WrappedProperty
* pWrappedProperty
= getWrappedProperty( rPropertyName
);
327 if( pWrappedProperty
)
328 pWrappedProperty
->setPropertyToDefault( xInnerPropertyState
);
330 xInnerPropertyState
->setPropertyToDefault( rPropertyName
);
333 Any SAL_CALL
WrappedPropertySet::getPropertyDefault( const OUString
& rPropertyName
)
336 Reference
< beans::XPropertyState
> xInnerPropertyState( getInnerPropertyState() );
337 if( xInnerPropertyState
.is() )
339 const WrappedProperty
* pWrappedProperty
= getWrappedProperty( rPropertyName
);
340 if( pWrappedProperty
)
341 aRet
= pWrappedProperty
->getPropertyDefault(xInnerPropertyState
);
343 aRet
= xInnerPropertyState
->getPropertyDefault( rPropertyName
);
348 //XMultiPropertyStates
349 void SAL_CALL
WrappedPropertySet::setAllPropertiesToDefault( )
351 const Sequence
< beans::Property
>& rPropSeq
= getPropertySequence();
352 for(sal_Int32 nN
=0; nN
<rPropSeq
.getLength(); nN
++)
354 OUString
aPropertyName( rPropSeq
[nN
].Name
);
355 setPropertyToDefault( aPropertyName
);
358 void SAL_CALL
WrappedPropertySet::setPropertiesToDefault( const Sequence
< OUString
>& rNameSeq
)
360 for(sal_Int32 nN
=0; nN
<rNameSeq
.getLength(); nN
++)
362 OUString
aPropertyName( rNameSeq
[nN
] );
363 setPropertyToDefault( aPropertyName
);
366 Sequence
< Any
> SAL_CALL
WrappedPropertySet::getPropertyDefaults( const Sequence
< OUString
>& rNameSeq
)
368 Sequence
< Any
> aRetSeq
;
369 if( rNameSeq
.hasElements() )
371 aRetSeq
.realloc( rNameSeq
.getLength() );
372 for(sal_Int32 nN
=0; nN
<rNameSeq
.getLength(); nN
++)
374 OUString
aPropertyName( rNameSeq
[nN
] );
375 aRetSeq
[nN
] = getPropertyDefault( aPropertyName
);
381 ::cppu::IPropertyArrayHelper
& WrappedPropertySet::getInfoHelper()
383 ::cppu::OPropertyArrayHelper
* p
= m_pPropertyArrayHelper
.get();
386 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
387 p
= m_pPropertyArrayHelper
.get();
390 p
= new ::cppu::OPropertyArrayHelper( getPropertySequence(), true );
391 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
392 m_pPropertyArrayHelper
.reset(p
);
397 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
399 return *m_pPropertyArrayHelper
;
402 tWrappedPropertyMap
& WrappedPropertySet::getWrappedPropertyMap()
404 tWrappedPropertyMap
* p
= m_pWrappedPropertyMap
.get();
407 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
408 p
= m_pWrappedPropertyMap
.get();
411 std::vector
< std::unique_ptr
<WrappedProperty
> > aPropList( createWrappedProperties() );
412 p
= new tWrappedPropertyMap
;
414 for (auto & elem
: aPropList
)
416 sal_Int32 nHandle
= getInfoHelper().getHandleByName( elem
->getOuterName() );
420 OSL_FAIL( "missing property in property list" );
422 else if( p
->find( nHandle
) != p
->end() )
424 //duplicate Wrapped property
425 OSL_FAIL( "duplicate Wrapped property" );
428 (*p
)[ nHandle
] = std::move(elem
);
431 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
432 m_pWrappedPropertyMap
.reset(p
);
437 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
439 return *m_pWrappedPropertyMap
;
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */