1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 
31 #include "WrappedPropertySet.hxx"
32 #include "macros.hxx"
33 
34 // header for define DELETEZ
35 #include <tools/solar.h>
36 
37 #include <tools/debug.hxx>
38 
39 //.............................................................................
40 namespace chart
41 {
42 //.............................................................................
43 
44 using namespace ::com::sun::star;
45 using ::com::sun::star::uno::Reference;
46 using ::com::sun::star::uno::Sequence;
47 using ::com::sun::star::uno::Any;
48 using ::rtl::OUString;
49 
50 WrappedPropertySet::WrappedPropertySet()
51                     : MutexContainer()
52                     , m_xInfo(0)
53                     , m_pPropertyArrayHelper(0)
54                     , m_pWrappedPropertyMap(0)
55 {
56 }
57 WrappedPropertySet::~WrappedPropertySet()
58 {
59     clearWrappedPropertySet();
60 }
61 
62 Reference< beans::XPropertyState > WrappedPropertySet::getInnerPropertyState()
63 {
64     return Reference< beans::XPropertyState >( getInnerPropertySet(), uno::UNO_QUERY );
65 }
66 
67 void WrappedPropertySet::clearWrappedPropertySet()
68 {
69     ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
70 
71     //delete all wrapped properties
72     if(m_pWrappedPropertyMap)
73     {
74         for( tWrappedPropertyMap::iterator aIt = m_pWrappedPropertyMap->begin()
75             ; aIt!= m_pWrappedPropertyMap->end(); aIt++ )
76         {
77             const WrappedProperty* pWrappedProperty = (*aIt).second;
78             DELETEZ(pWrappedProperty);
79         }
80     }
81 
82     DELETEZ(m_pPropertyArrayHelper);
83     DELETEZ(m_pWrappedPropertyMap);
84 
85     m_xInfo = NULL;
86 }
87 
88 //XPropertySet
89 Reference< beans::XPropertySetInfo > SAL_CALL WrappedPropertySet::getPropertySetInfo(  )
90                                     throw (uno::RuntimeException)
91 {
92     Reference< beans::XPropertySetInfo > xInfo = m_xInfo;
93     if( !xInfo.is() )
94     {
95         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
96         xInfo = m_xInfo;
97         if( !xInfo.is() )
98         {
99             xInfo = ::cppu::OPropertySetHelper::createPropertySetInfo( getInfoHelper() );
100             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
101             m_xInfo = xInfo;
102         }
103     }
104     else
105     {
106         OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
107     }
108     return m_xInfo;
109 }
110 
111 void SAL_CALL WrappedPropertySet::setPropertyValue( const OUString& rPropertyName, const Any& rValue )
112                                     throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
113 {
114     try
115     {
116         sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
117         const WrappedProperty* pWrappedProperty = getWrappedProperty( nHandle );
118         Reference< beans::XPropertySet > xInnerPropertySet( this->getInnerPropertySet() );
119         if( pWrappedProperty )
120             pWrappedProperty->setPropertyValue( rValue, xInnerPropertySet );
121         else if( xInnerPropertySet.is() )
122             xInnerPropertySet->setPropertyValue( rPropertyName, rValue );
123         else
124         {
125 #if OSL_DEBUG_LEVEL > 1
126             DBG_ERROR("found no inner property set to map to");
127 #endif
128         }
129     }
130     catch( beans::UnknownPropertyException& ex )
131     {
132         throw ex;
133     }
134     catch( beans::PropertyVetoException& ex )
135     {
136         throw ex;
137     }
138     catch( lang::IllegalArgumentException& ex )
139     {
140         throw ex;
141     }
142     catch( lang::WrappedTargetException& ex )
143     {
144         throw ex;
145     }
146     catch( uno::RuntimeException& ex )
147     {
148         throw ex;
149     }
150     catch( uno::Exception& ex )
151     {
152         OSL_ENSURE(false,"invalid exception caught in WrappedPropertySet::setPropertyValue");
153         lang::WrappedTargetException aWrappedException;
154         aWrappedException.TargetException = uno::makeAny( ex );
155         throw aWrappedException;
156     }
157 }
158 Any SAL_CALL WrappedPropertySet::getPropertyValue( const OUString& rPropertyName )
159                                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
160 {
161     Any aRet;
162 
163     try
164     {
165         sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
166         const WrappedProperty* pWrappedProperty = getWrappedProperty( nHandle );
167         Reference< beans::XPropertySet > xInnerPropertySet( this->getInnerPropertySet() );
168         if( pWrappedProperty )
169             aRet = pWrappedProperty->getPropertyValue( xInnerPropertySet );
170         else if( xInnerPropertySet.is() )
171             aRet = xInnerPropertySet->getPropertyValue( rPropertyName );
172         else
173         {
174 #if OSL_DEBUG_LEVEL > 1
175             DBG_ERROR("found no inner property set to map to");
176 #endif
177         }
178     }
179     catch( beans::UnknownPropertyException& ex )
180     {
181         throw ex;
182     }
183     catch( lang::WrappedTargetException& ex )
184     {
185         throw ex;
186     }
187     catch( uno::RuntimeException& ex )
188     {
189         throw ex;
190     }
191     catch( uno::Exception& ex )
192     {
193         OSL_ENSURE(false,"invalid exception caught in WrappedPropertySet::setPropertyValue");
194         lang::WrappedTargetException aWrappedException;
195         aWrappedException.TargetException = uno::makeAny( ex );
196         throw aWrappedException;
197     }
198 
199     return aRet;
200 }
201 
202 void SAL_CALL WrappedPropertySet::addPropertyChangeListener( const OUString& rPropertyName, const Reference< beans::XPropertyChangeListener >& xListener )
203                                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
204 {
205     Reference< beans::XPropertySet > xInnerPropertySet( this->getInnerPropertySet() );
206     if( xInnerPropertySet.is() )
207     {
208         const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
209         if( pWrappedProperty )
210             xInnerPropertySet->addPropertyChangeListener( pWrappedProperty->getInnerName(), xListener );
211         else
212             xInnerPropertySet->addPropertyChangeListener( rPropertyName, xListener );
213     }
214 //     m_aBoundListenerContainer.addInterface( (sal_Int32)nHandle, xListener );
215 }
216 void SAL_CALL WrappedPropertySet::removePropertyChangeListener( const OUString& rPropertyName, const Reference< beans::XPropertyChangeListener >& aListener )
217                                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
218 {
219     Reference< beans::XPropertySet > xInnerPropertySet( this->getInnerPropertySet() );
220     if( xInnerPropertySet.is() )
221     {
222         const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
223         if( pWrappedProperty )
224             xInnerPropertySet->removePropertyChangeListener( pWrappedProperty->getInnerName(), aListener );
225         else
226             xInnerPropertySet->removePropertyChangeListener( rPropertyName, aListener );
227     }
228 }
229 void SAL_CALL WrappedPropertySet::addVetoableChangeListener( const OUString& rPropertyName, const Reference< beans::XVetoableChangeListener >& aListener )
230                                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
231 {
232     Reference< beans::XPropertySet > xInnerPropertySet( this->getInnerPropertySet() );
233     if( xInnerPropertySet.is() )
234     {
235         const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
236         if( pWrappedProperty )
237             xInnerPropertySet->addVetoableChangeListener( pWrappedProperty->getInnerName(), aListener );
238         else
239             xInnerPropertySet->addVetoableChangeListener( rPropertyName, aListener );
240     }
241 }
242 void SAL_CALL WrappedPropertySet::removeVetoableChangeListener( const OUString& rPropertyName, const Reference< beans::XVetoableChangeListener >& aListener )
243                                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
244 {
245     Reference< beans::XPropertySet > xInnerPropertySet( this->getInnerPropertySet() );
246     if( xInnerPropertySet.is() )
247     {
248         const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
249         if( pWrappedProperty )
250             xInnerPropertySet->removeVetoableChangeListener( pWrappedProperty->getInnerName(), aListener );
251         else
252             xInnerPropertySet->removeVetoableChangeListener( rPropertyName, aListener );
253     }
254 }
255 
256 //XMultiPropertySet
257 void SAL_CALL WrappedPropertySet::setPropertyValues( const Sequence< OUString >& rNameSeq, const Sequence< Any >& rValueSeq )
258                                     throw (beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
259 {
260     bool bUnknownProperty = false;
261     sal_Int32 nMinCount = std::min( rValueSeq.getLength(), rNameSeq.getLength() );
262     for(sal_Int32 nN=0; nN<nMinCount; nN++)
263     {
264         ::rtl::OUString aPropertyName( rNameSeq[nN] );
265         try
266         {
267             this->setPropertyValue( aPropertyName, rValueSeq[nN] );
268         }
269         catch( beans::UnknownPropertyException& ex )
270         {
271             ASSERT_EXCEPTION( ex );
272             bUnknownProperty = true;
273         }
274     }
275     //todo: store unknown properties elsewhere
276 //    if( bUnknownProperty )
277 //        throw beans::UnknownPropertyException();
278 }
279 Sequence< Any > SAL_CALL WrappedPropertySet::getPropertyValues( const Sequence< OUString >& rNameSeq )
280                                     throw (uno::RuntimeException)
281 {
282     Sequence< Any > aRetSeq;
283     if( rNameSeq.getLength() )
284     {
285         aRetSeq.realloc( rNameSeq.getLength() );
286         for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
287         {
288             try
289             {
290                 ::rtl::OUString aPropertyName( rNameSeq[nN] );
291                 aRetSeq[nN] = this->getPropertyValue( aPropertyName );
292             }
293             catch( beans::UnknownPropertyException& ex )
294             {
295                 ASSERT_EXCEPTION( ex );
296             }
297             catch( lang::WrappedTargetException& ex )
298             {
299                 ASSERT_EXCEPTION( ex );
300             }
301         }
302     }
303     return aRetSeq;
304 }
305 void SAL_CALL WrappedPropertySet::addPropertiesChangeListener( const Sequence< OUString >& /* rNameSeq */, const Reference< beans::XPropertiesChangeListener >& /* xListener */ )
306                                     throw (uno::RuntimeException)
307 {
308     OSL_ENSURE(false,"not implemented yet");
309     //todo
310 }
311 void SAL_CALL WrappedPropertySet::removePropertiesChangeListener( const Reference< beans::XPropertiesChangeListener >& /* xListener */ )
312                                     throw (uno::RuntimeException)
313 {
314     OSL_ENSURE(false,"not implemented yet");
315     //todo
316 }
317 void SAL_CALL WrappedPropertySet::firePropertiesChangeEvent( const Sequence< OUString >& /* rNameSeq */, const Reference< beans::XPropertiesChangeListener >& /* xListener */ )
318                                     throw (uno::RuntimeException)
319 {
320     OSL_ENSURE(false,"not implemented yet");
321     //todo
322 }
323 
324 //XPropertyState
325 beans::PropertyState SAL_CALL WrappedPropertySet::getPropertyState( const OUString& rPropertyName )
326                                     throw (beans::UnknownPropertyException, uno::RuntimeException)
327 {
328     beans::PropertyState aState( beans::PropertyState_DIRECT_VALUE );
329 
330     Reference< beans::XPropertyState > xInnerPropertyState( this->getInnerPropertyState() );
331     if( xInnerPropertyState.is() )
332     {
333         const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
334         if( pWrappedProperty )
335             aState = pWrappedProperty->getPropertyState( xInnerPropertyState );
336         else
337             aState = xInnerPropertyState->getPropertyState( rPropertyName );
338     }
339     return aState;
340 }
341 
342 const WrappedProperty* WrappedPropertySet::getWrappedProperty( const ::rtl::OUString& rOuterName )
343 {
344     sal_Int32 nHandle = getInfoHelper().getHandleByName( rOuterName );
345     return getWrappedProperty( nHandle );
346 }
347 
348 const WrappedProperty* WrappedPropertySet::getWrappedProperty( sal_Int32 nHandle )
349 {
350     tWrappedPropertyMap::const_iterator aFound( getWrappedPropertyMap().find( nHandle ) );
351     if( aFound != getWrappedPropertyMap().end() )
352         return (*aFound).second;
353     return 0;
354 }
355 
356 Sequence< beans::PropertyState > SAL_CALL WrappedPropertySet::getPropertyStates( const Sequence< OUString >& rNameSeq )
357                                     throw (beans::UnknownPropertyException, uno::RuntimeException)
358 {
359     Sequence< beans::PropertyState > aRetSeq;
360     if( rNameSeq.getLength() )
361     {
362         aRetSeq.realloc( rNameSeq.getLength() );
363         for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
364         {
365             ::rtl::OUString aPropertyName( rNameSeq[nN] );
366             aRetSeq[nN] = this->getPropertyState( aPropertyName );
367         }
368     }
369     return aRetSeq;
370 }
371 
372 void SAL_CALL WrappedPropertySet::setPropertyToDefault( const OUString& rPropertyName )
373                                     throw (beans::UnknownPropertyException, uno::RuntimeException)
374 {
375     Reference< beans::XPropertyState > xInnerPropertyState( this->getInnerPropertyState() );
376     if( xInnerPropertyState.is() )
377     {
378         const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
379         if( pWrappedProperty )
380             pWrappedProperty->setPropertyToDefault( xInnerPropertyState );
381         else
382             xInnerPropertyState->setPropertyToDefault( rPropertyName );
383     }
384 }
385 Any SAL_CALL WrappedPropertySet::getPropertyDefault( const OUString& rPropertyName )
386                                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
387 {
388     Any aRet;
389     Reference< beans::XPropertyState > xInnerPropertyState( this->getInnerPropertyState() );
390     if( xInnerPropertyState.is() )
391     {
392         const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
393         if( pWrappedProperty )
394             aRet = pWrappedProperty->getPropertyDefault(xInnerPropertyState);
395         else
396             aRet = xInnerPropertyState->getPropertyDefault( rPropertyName );
397     }
398     return aRet;
399 }
400 
401 //XMultiPropertyStates
402 void SAL_CALL WrappedPropertySet::setAllPropertiesToDefault(  )
403                                     throw (uno::RuntimeException)
404 {
405     const Sequence< beans::Property >&  rPropSeq = getPropertySequence();
406     for(sal_Int32 nN=0; nN<rPropSeq.getLength(); nN++)
407     {
408         ::rtl::OUString aPropertyName( rPropSeq[nN].Name );
409         this->setPropertyToDefault( aPropertyName );
410     }
411 }
412 void SAL_CALL WrappedPropertySet::setPropertiesToDefault( const Sequence< OUString >& rNameSeq )
413                                     throw (beans::UnknownPropertyException, uno::RuntimeException)
414 {
415     for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
416     {
417         ::rtl::OUString aPropertyName( rNameSeq[nN] );
418         this->setPropertyToDefault( aPropertyName );
419     }
420 }
421 Sequence< Any > SAL_CALL WrappedPropertySet::getPropertyDefaults( const Sequence< OUString >& rNameSeq )
422                                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
423 {
424     Sequence< Any > aRetSeq;
425     if( rNameSeq.getLength() )
426     {
427         aRetSeq.realloc( rNameSeq.getLength() );
428         for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
429         {
430             ::rtl::OUString aPropertyName( rNameSeq[nN] );
431             aRetSeq[nN] = this->getPropertyDefault( aPropertyName );
432         }
433     }
434     return aRetSeq;
435 }
436 
437 //-----------------------------------------------------------------------------
438 //-----------------------------------------------------------------------------
439 
440 ::cppu::IPropertyArrayHelper& WrappedPropertySet::getInfoHelper()
441 {
442     ::cppu::OPropertyArrayHelper* p = m_pPropertyArrayHelper;
443     if(!p)
444     {
445         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
446         p = m_pPropertyArrayHelper;
447         if(!p)
448         {
449             p = new ::cppu::OPropertyArrayHelper( getPropertySequence(), sal_True );
450             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
451             m_pPropertyArrayHelper = p;
452         }
453     }
454     else
455     {
456         OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
457     }
458     return *m_pPropertyArrayHelper;
459 }
460 
461 //-----------------------------------------------------------------------------
462 
463 tWrappedPropertyMap& WrappedPropertySet::getWrappedPropertyMap()
464 {
465     tWrappedPropertyMap* p = m_pWrappedPropertyMap;
466     if(!p)
467     {
468         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
469         p = m_pWrappedPropertyMap;
470         if(!p)
471         {
472             std::vector< WrappedProperty* > aPropList( createWrappedProperties() );
473             p = new tWrappedPropertyMap();
474 
475             for( std::vector< WrappedProperty* >::const_iterator aIt = aPropList.begin(); aIt!=aPropList.end(); ++aIt )
476             {
477                 WrappedProperty* pProperty = *aIt;
478                 if(pProperty)
479                 {
480                     sal_Int32 nHandle = getInfoHelper().getHandleByName( pProperty->getOuterName() );
481 
482                     if( nHandle == -1 )
483                     {
484                         OSL_ENSURE( false, "missing property in property list" );
485                         delete pProperty;//we are owner or the created WrappedProperties
486                     }
487                     else if( p->find( nHandle ) != p->end() )
488                     {
489                         //duplicate Wrapped property
490                         OSL_ENSURE( false, "duplicate Wrapped property" );
491                         delete pProperty;//we are owner or the created WrappedProperties
492                     }
493                     else
494                         (*p)[ nHandle ] = pProperty;
495                 }
496             }
497 
498             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
499             m_pWrappedPropertyMap = p;
500         }
501     }
502     else
503     {
504         OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
505     }
506     return *m_pWrappedPropertyMap;
507 }
508 
509 //.............................................................................
510 } //namespace chart
511 //.............................................................................
512