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