xref: /trunk/main/chart2/source/tools/OPropertySet.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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_charttools.hxx"
26 #include "OPropertySet.hxx"
27 #include "ImplOPropertySet.hxx"
28 #include "ContainerHelper.hxx"
29 #include <rtl/uuid.h>
30 #include <cppuhelper/queryinterface.hxx>
31 
32 #include <vector>
33 #include <algorithm>
34 
35 using namespace ::com::sun::star;
36 
37 using ::com::sun::star::style::XStyleSupplier;
38 // using ::com::sun::star::beans::XFastPropertyState;
39 
40 using ::com::sun::star::uno::Reference;
41 using ::com::sun::star::uno::Sequence;
42 using ::com::sun::star::uno::Any;
43 using ::rtl::OUString;
44 using ::osl::MutexGuard;
45 
46 // needed for MS compiler
47 using ::cppu::OBroadcastHelper;
48 using ::cppu::OPropertySetHelper;
49 using ::cppu::OWeakObject;
50 
51 namespace property
52 {
53 
OPropertySet(::osl::Mutex & par_rMutex)54 OPropertySet::OPropertySet( ::osl::Mutex & par_rMutex ) :
55         OBroadcastHelper( par_rMutex ),
56         // the following causes a warning; there seems to be no way to avoid it
57         OPropertySetHelper( static_cast< OBroadcastHelper & >( *this )),
58         m_rMutex( par_rMutex ),
59         m_pImplProperties( new impl::ImplOPropertySet() ),
60         m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
61 {
62 }
63 
OPropertySet(const OPropertySet & rOther,::osl::Mutex & par_rMutex)64 OPropertySet::OPropertySet( const OPropertySet & rOther, ::osl::Mutex & par_rMutex ) :
65         OBroadcastHelper( par_rMutex ),
66         // the following causes a warning; there seems to be no way to avoid it
67         OPropertySetHelper( static_cast< OBroadcastHelper & >( *this )),
68         m_rMutex( par_rMutex ),
69         m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
70 {
71     // /--
72     MutexGuard aGuard( m_rMutex );
73     if( rOther.m_pImplProperties.get())
74         m_pImplProperties.reset( new impl::ImplOPropertySet( * rOther.m_pImplProperties.get()));
75     // \--
76 }
77 
SetNewValuesExplicitlyEvenIfTheyEqualDefault()78 void OPropertySet::SetNewValuesExplicitlyEvenIfTheyEqualDefault()
79 {
80     m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault = true;
81 }
82 
~OPropertySet()83 OPropertySet::~OPropertySet()
84 {}
85 
disposePropertySet()86 void OPropertySet::disposePropertySet()
87 {
88     m_pImplProperties.reset( 0 );
89 }
90 
queryInterface(const uno::Type & aType)91 Any SAL_CALL OPropertySet::queryInterface( const uno::Type& aType )
92 {
93     return ::cppu::queryInterface(
94         aType,
95 //         static_cast< uno::XInterface * >(
96 //             static_cast< uno::XWeak * >( this )),
97 //         static_cast< uno::XWeak * >( this ),
98 //         static_cast< lang::XServiceInfo * >( this ),
99         static_cast< lang::XTypeProvider * >( this ),
100         static_cast< beans::XPropertySet * >( this ),
101         static_cast< beans::XMultiPropertySet * >( this ),
102         static_cast< beans::XFastPropertySet * >( this ),
103         static_cast< beans::XPropertyState * >( this ),
104         static_cast< beans::XMultiPropertyStates * >( this ),
105         static_cast< XStyleSupplier * >( this ) );
106 //         static_cast< XFastPropertyState * >( this ) );
107 }
108 
109 // void SAL_CALL OPropertySet::acquire() throw ()
110 // {
111 //     OWeakObject::acquire();
112 // }
113 
114 // void SAL_CALL OPropertySet::release() throw ()
115 // {
116 //     OWeakObject::release();
117 // }
118 
119 
120 // ____ XServiceInfo ____
121 // OUString SAL_CALL
122 //     OPropertySet::getImplementationName()
123 //     throw (uno::RuntimeException)
124 // {
125 //     return OUString( RTL_CONSTASCII_USTRINGPARAM( "property::OPropertySet" ));
126 // }
127 
128 // sal_Bool SAL_CALL
129 //     OPropertySet::supportsService( const OUString& ServiceName )
130 //     throw (uno::RuntimeException)
131 // {
132 //     return ( 0 == ServiceName.reverseCompareToAsciiL(
133 //                  RTL_CONSTASCII_STRINGPARAM( "com.sun.star.beans.PropertySet" )));
134 // }
135 
136 // Sequence< OUString > SAL_CALL
137 //     OPropertySet::getSupportedServiceNames()
138 //     throw (uno::RuntimeException)
139 // {
140 //     Sequence< OUString > aServiceNames( 1 );
141 //     aServiceNames[ 0 ] = OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.beans.PropertySet" ));
142 //     return aServiceNames;
143 // }
144 
145 #define LCL_PROP_CPPUTYPE(t) (::getCppuType( reinterpret_cast< const Reference<t> *>(0)))
146 
147 // // ____ XTypeProvider ____
148 Sequence< uno::Type > SAL_CALL
getTypes()149     OPropertySet::getTypes()
150 {
151     static Sequence< uno::Type > aTypeList;
152 
153     // /--
154     MutexGuard aGuard( m_rMutex );
155 
156     if( aTypeList.getLength() == 0 )
157     {
158         ::std::vector< uno::Type > aTypes;
159 
160 //         aTypes.push_back( LCL_PROP_CPPUTYPE( uno::XWeak ));
161 //         aTypes.push_back( LCL_PROP_CPPUTYPE( lang::XServiceInfo ));
162         aTypes.push_back( LCL_PROP_CPPUTYPE( lang::XTypeProvider ));
163         aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XPropertySet ));
164         aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XMultiPropertySet ));
165         aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XFastPropertySet ));
166         aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XPropertyState ));
167         aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XMultiPropertyStates ));
168         aTypes.push_back( LCL_PROP_CPPUTYPE( XStyleSupplier ));
169 //         aTypes.push_back( LCL_PROP_CPPUTYPE( XFastPropertyState ));
170 
171         aTypeList = ::chart::ContainerHelper::ContainerToSequence( aTypes );
172     }
173 
174     return aTypeList;
175     // \--
176 }
177 
178 Sequence< sal_Int8 > SAL_CALL
getImplementationId()179     OPropertySet::getImplementationId()
180 {
181     static uno::Sequence< sal_Int8 > aId;
182     if( aId.getLength() == 0 )
183     {
184         aId.realloc( 16 );
185         rtl_createUuid( (sal_uInt8 *)aId.getArray(), 0, sal_True );
186     }
187     return aId;
188 }
189 
190 
191 // ____ XPropertyState ____
192 beans::PropertyState SAL_CALL
getPropertyState(const OUString & PropertyName)193     OPropertySet::getPropertyState( const OUString& PropertyName )
194 {
195     cppu::IPropertyArrayHelper & rPH = getInfoHelper();
196 
197     return m_pImplProperties->GetPropertyStateByHandle(
198         rPH.getHandleByName( PropertyName ));
199 }
200 
201 Sequence< beans::PropertyState > SAL_CALL
getPropertyStates(const Sequence<OUString> & aPropertyName)202     OPropertySet::getPropertyStates( const Sequence< OUString >& aPropertyName )
203 {
204     cppu::IPropertyArrayHelper & rPH = getInfoHelper();
205 
206     sal_Int32 * pHandles = new sal_Int32[ aPropertyName.getLength() ];
207     rPH.fillHandles( pHandles, aPropertyName );
208 
209     ::std::vector< sal_Int32 > aHandles( pHandles, pHandles + aPropertyName.getLength());
210     delete[] pHandles;
211 
212     return m_pImplProperties->GetPropertyStatesByHandle( aHandles );
213 }
214 
215 void SAL_CALL
setPropertyToDefault(const OUString & PropertyName)216     OPropertySet::setPropertyToDefault( const OUString& PropertyName )
217 {
218     cppu::IPropertyArrayHelper & rPH = getInfoHelper();
219 
220     m_pImplProperties->SetPropertyToDefault( rPH.getHandleByName( PropertyName ));
221     firePropertyChangeEvent();
222 }
223 
224 Any SAL_CALL
getPropertyDefault(const OUString & aPropertyName)225     OPropertySet::getPropertyDefault( const OUString& aPropertyName )
226 {
227     cppu::IPropertyArrayHelper & rPH = getInfoHelper();
228 
229     return GetDefaultValue( rPH.getHandleByName( aPropertyName ) );
230 }
231 
232 
233 // ____ XMultiPropertyStates ____
234 
235 // Note: getPropertyStates() is already implemented in XPropertyState with the
236 // same signature
237 
238 void SAL_CALL
setAllPropertiesToDefault()239     OPropertySet::setAllPropertiesToDefault()
240 {
241     m_pImplProperties->SetAllPropertiesToDefault();
242     firePropertyChangeEvent();
243 }
244 
245 void SAL_CALL
setPropertiesToDefault(const Sequence<OUString> & aPropertyNames)246     OPropertySet::setPropertiesToDefault( const Sequence< OUString >& aPropertyNames )
247 {
248     cppu::IPropertyArrayHelper & rPH = getInfoHelper();
249 
250     sal_Int32 * pHandles = new sal_Int32[ aPropertyNames.getLength() ];
251     rPH.fillHandles( pHandles, aPropertyNames );
252 
253     ::std::vector< sal_Int32 > aHandles( pHandles, pHandles + aPropertyNames.getLength());
254     delete[] pHandles;
255 
256     m_pImplProperties->SetPropertiesToDefault( aHandles );
257 }
258 
259 Sequence< Any > SAL_CALL
getPropertyDefaults(const Sequence<OUString> & aPropertyNames)260     OPropertySet::getPropertyDefaults( const Sequence< OUString >& aPropertyNames )
261 {
262     ::cppu::IPropertyArrayHelper & rPH = getInfoHelper();
263     const sal_Int32 nElements = aPropertyNames.getLength();
264 
265     Sequence< Any > aResult( nElements );
266     Any * pResultArray = aResult.getArray();
267     sal_Int32 nI = 0;
268 
269     for( ; nI < nElements; ++nI )
270     {
271         pResultArray[ nI ] = GetDefaultValue(
272             rPH.getHandleByName( aPropertyNames[ nI ] ));
273     }
274 
275     return aResult;
276 }
277 
convertFastPropertyValue(Any & rConvertedValue,Any & rOldValue,sal_Int32 nHandle,const Any & rValue)278 sal_Bool SAL_CALL OPropertySet::convertFastPropertyValue
279     ( Any & rConvertedValue,
280       Any & rOldValue,
281       sal_Int32 nHandle,
282       const Any& rValue )
283 {
284     getFastPropertyValue( rOldValue, nHandle );
285     //accept longs also for short values
286     {
287         sal_Int16 nValue;
288         if( (rOldValue>>=nValue) && !(rValue>>=nValue) )
289         {
290             sal_Int32 n32Value = 0;
291             if( rValue>>=n32Value )
292             {
293                 rConvertedValue = uno::makeAny( static_cast<sal_Int16>(n32Value) );
294                 return sal_True;
295             }
296 
297             sal_Int64 n64Value = 0;
298             if( rValue>>=n64Value )
299             {
300                 rConvertedValue = uno::makeAny( static_cast<sal_Int16>(n64Value) );
301                 return sal_True;
302             }
303         }
304     }
305     rConvertedValue = rValue;
306     if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault && rOldValue == rConvertedValue )
307         return sal_False;//no change necessary
308     return sal_True;
309 }
310 
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any & rValue)311 void SAL_CALL OPropertySet::setFastPropertyValue_NoBroadcast
312     ( sal_Int32 nHandle,
313       const Any& rValue )
314 {
315 #if OSL_DEBUG_LEVEL > 0
316     if( rValue.hasValue())
317     {
318         cppu::IPropertyArrayHelper & rPH = getInfoHelper();
319         OUString aName;
320         rPH.fillPropertyMembersByHandle( &aName, 0, nHandle );
321         OSL_ENSURE( rValue.isExtractableTo( rPH.getPropertyByName( aName ).Type ),
322                     "Property type is wrong" );
323     }
324 #endif
325 
326     Any aDefault;
327     try
328     {
329         aDefault = GetDefaultValue( nHandle );
330     }
331     catch( beans::UnknownPropertyException ex )
332     {
333         aDefault.clear();
334     }
335     m_pImplProperties->SetPropertyValueByHandle( nHandle, rValue );
336     if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault && aDefault.hasValue() && aDefault == rValue ) //#i98893# don't export defaults to file
337         m_pImplProperties->SetPropertyToDefault( nHandle );
338     else
339         m_pImplProperties->SetPropertyValueByHandle( nHandle, rValue );
340 }
341 
getFastPropertyValue(Any & rValue,sal_Int32 nHandle) const342 void SAL_CALL OPropertySet::getFastPropertyValue
343     ( Any& rValue,
344       sal_Int32 nHandle ) const
345 {
346     if( ! m_pImplProperties->GetPropertyValueByHandle( rValue, nHandle ))
347     {
348 //         OSL_TRACE( "OPropertySet: asking style for property" );
349         // property was not set -> try style
350         uno::Reference< beans::XFastPropertySet > xStylePropSet( m_pImplProperties->GetStyle(), uno::UNO_QUERY );
351         if( xStylePropSet.is() )
352         {
353 #ifdef DBG_UTIL
354             {
355                 // check if the handle of the style points to the same property
356                 // name as the handle in this property set
357                 uno::Reference< beans::XPropertySet > xPropSet( xStylePropSet, uno::UNO_QUERY );
358                 if( xPropSet.is())
359                 {
360                     uno::Reference< beans::XPropertySetInfo > xInfo( xPropSet->getPropertySetInfo(),
361                                                                      uno::UNO_QUERY );
362                     if( xInfo.is() )
363                     {
364                         // for some reason the virtual method getInfoHelper() is
365                         // not const
366                         ::cppu::IPropertyArrayHelper & rPH =
367                               const_cast< OPropertySet * >( this )->getInfoHelper();
368 
369                         // find the Property with Handle nHandle in Style
370                         Sequence< beans::Property > aProps( xInfo->getProperties() );
371                         sal_Int32 nI = aProps.getLength() - 1;
372                         while( ( nI >= 0 ) && nHandle != aProps[ nI ].Handle )
373                             --nI;
374 
375                         if( nI >= 0 ) // => nHandle == aProps[nI].Handle
376                         {
377                             // check whether the handle in this property set is
378                             // the same as the one in the style
379                             beans::Property aProp( rPH.getPropertyByName( aProps[ nI ].Name ) );
380                             OSL_ENSURE( nHandle == aProp.Handle,
381                                         "HandleCheck: Handles for same property differ!" );
382 
383                             if( nHandle == aProp.Handle )
384                             {
385                                 OSL_ENSURE( aProp.Type == aProps[nI].Type,
386                                             "HandleCheck: Types differ!" );
387                                 OSL_ENSURE( aProp.Attributes == aProps[nI].Attributes,
388                                             "HandleCheck: Attributes differ!" );
389                             }
390                         }
391                         else
392                         {
393                             OSL_ENSURE( false,  "HandleCheck: Handle not found in Style" );
394                         }
395                     }
396                     else
397                         OSL_ENSURE( false, "HandleCheck: Invalid XPropertySetInfo returned" );
398                 }
399                 else
400                     OSL_ENSURE( false, "HandleCheck: XPropertySet not supported" );
401             }
402 #endif
403             rValue = xStylePropSet->getFastPropertyValue( nHandle );
404         }
405         else
406         {
407 //             OSL_TRACE( "OPropertySet: no style => getting default for property" );
408             // there is no style (or the style does not support XFastPropertySet)
409             // => take the default value
410             try
411             {
412                 rValue = GetDefaultValue( nHandle );
413             }
414             catch( beans::UnknownPropertyException ex )
415             {
416                 rValue.clear();
417             }
418         }
419     }
420 }
421 
firePropertyChangeEvent()422 void OPropertySet::firePropertyChangeEvent()
423 {
424     // nothing in base class
425 }
426 
427 // ____ XStyleSupplier ____
getStyle()428 Reference< style::XStyle > SAL_CALL OPropertySet::getStyle()
429 {
430     return m_pImplProperties->GetStyle();
431 }
432 
setStyle(const Reference<style::XStyle> & xStyle)433 void SAL_CALL OPropertySet::setStyle( const Reference< style::XStyle >& xStyle )
434 {
435     if( ! m_pImplProperties->SetStyle( xStyle ))
436         throw lang::IllegalArgumentException(
437             OUString( RTL_CONSTASCII_USTRINGPARAM( "Empty Style" )),
438             static_cast< beans::XPropertySet * >( this ),
439             0 );
440 }
441 
442 // ____ XFastPropertyState ____
443 // beans::PropertyState OPropertySet::SAL_CALL getFastPropertyState( sal_Int32 nHandle )
444 //     throw (beans::UnknownPropertyException,
445 //            uno::RuntimeException)
446 // {
447 //     return m_pImplProperties->GetPropertyStateByHandle( nHandle );
448 // }
449 
450 // uno::Sequence< beans::PropertyState > OPropertySet::SAL_CALL getFastPropertyStates(
451 //     const uno::Sequence< sal_Int32 >& aHandles )
452 //     throw (beans::UnknownPropertyException,
453 //            uno::RuntimeException)
454 // {
455 //     ::std::vector< sal_Int32 > aHandleVec(
456 //         aHandles.getConstArray(),
457 //         aHandles.getConstArray() + aHandles.getLength() );
458 
459 //     return m_pImplProperties->GetPropertyStatesByHandle( aHandleVec );
460 // }
461 
462 // void OPropertySet::SAL_CALL setFastPropertyToDefault( sal_Int32 nHandle )
463 //     throw (beans::UnknownPropertyException,
464 //            uno::RuntimeException)
465 // {
466 //     m_pImplProperties->SetPropertyToDefault( nHandle );
467 // }
468 
469 // uno::Any OPropertySet::SAL_CALL getFastPropertyDefault( sal_Int32 nHandle )
470 //     throw (beans::UnknownPropertyException,
471 //            lang::WrappedTargetException,
472 //            uno::RuntimeException)
473 // {
474 //     return GetDefaultValue( nHandle );
475 // }
476 
477 // ____ XMultiPropertySet ____
setPropertyValues(const Sequence<OUString> & PropertyNames,const Sequence<Any> & Values)478 void SAL_CALL OPropertySet::setPropertyValues(
479     const Sequence< OUString >& PropertyNames, const Sequence< Any >& Values )
480 {
481     ::cppu::OPropertySetHelper::setPropertyValues( PropertyNames, Values );
482 
483     firePropertyChangeEvent();
484 }
485 
486 // ____ XFastPropertySet ____
setFastPropertyValue(sal_Int32 nHandle,const Any & rValue)487 void SAL_CALL OPropertySet::setFastPropertyValue( sal_Int32 nHandle, const Any& rValue )
488 {
489     ::cppu::OPropertySetHelper::setFastPropertyValue( nHandle, rValue );
490 
491     firePropertyChangeEvent();
492 }
493 
494 
495 } //  namespace property
496