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 "OPropertySet.hxx"
21 #include "ImplOPropertySet.hxx"
22 #include "ContainerHelper.hxx"
23 #include <cppuhelper/queryinterface.hxx>
24 #include <comphelper/servicehelper.hxx>
29 using namespace ::com::sun::star
;
31 using ::com::sun::star::style::XStyleSupplier
;
33 using ::com::sun::star::uno::Reference
;
34 using ::com::sun::star::uno::Sequence
;
35 using ::com::sun::star::uno::Any
;
36 using ::osl::MutexGuard
;
38 // needed for MS compiler
39 using ::cppu::OBroadcastHelper
;
40 using ::cppu::OPropertySetHelper
;
41 using ::cppu::OWeakObject
;
46 OPropertySet::OPropertySet( ::osl::Mutex
& par_rMutex
) :
47 OBroadcastHelper( par_rMutex
),
48 // the following causes a warning; there seems to be no way to avoid it
49 OPropertySetHelper( static_cast< OBroadcastHelper
& >( *this )),
50 m_rMutex( par_rMutex
),
51 m_pImplProperties( new impl::ImplOPropertySet() ),
52 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
56 OPropertySet::OPropertySet( const OPropertySet
& rOther
, ::osl::Mutex
& par_rMutex
) :
57 OBroadcastHelper( par_rMutex
),
58 // the following causes a warning; there seems to be no way to avoid it
59 OPropertySetHelper( static_cast< OBroadcastHelper
& >( *this )),
60 m_rMutex( par_rMutex
),
61 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
63 MutexGuard
aGuard( m_rMutex
);
64 if( rOther
.m_pImplProperties
.get())
65 m_pImplProperties
.reset( new impl::ImplOPropertySet( * rOther
.m_pImplProperties
.get()));
68 void OPropertySet::SetNewValuesExplicitlyEvenIfTheyEqualDefault()
70 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault
= true;
73 OPropertySet::~OPropertySet()
76 void OPropertySet::disposePropertySet()
78 m_pImplProperties
.reset( 0 );
81 Any SAL_CALL
OPropertySet::queryInterface( const uno::Type
& aType
)
82 throw (uno::RuntimeException
)
84 return ::cppu::queryInterface(
86 static_cast< lang::XTypeProvider
* >( this ),
87 static_cast< beans::XPropertySet
* >( this ),
88 static_cast< beans::XMultiPropertySet
* >( this ),
89 static_cast< beans::XFastPropertySet
* >( this ),
90 static_cast< beans::XPropertyState
* >( this ),
91 static_cast< beans::XMultiPropertyStates
* >( this ),
92 static_cast< XStyleSupplier
* >( this ) );
95 #define LCL_PROP_CPPUTYPE(t) (::getCppuType( reinterpret_cast< const Reference<t> *>(0)))
97 // // ____ XTypeProvider ____
98 Sequence
< uno::Type
> SAL_CALL
99 OPropertySet::getTypes()
100 throw (uno::RuntimeException
)
102 static Sequence
< uno::Type
> aTypeList
;
104 MutexGuard
aGuard( m_rMutex
);
106 if( aTypeList
.getLength() == 0 )
108 ::std::vector
< uno::Type
> aTypes
;
110 aTypes
.push_back( LCL_PROP_CPPUTYPE( lang::XTypeProvider
));
111 aTypes
.push_back( LCL_PROP_CPPUTYPE( beans::XPropertySet
));
112 aTypes
.push_back( LCL_PROP_CPPUTYPE( beans::XMultiPropertySet
));
113 aTypes
.push_back( LCL_PROP_CPPUTYPE( beans::XFastPropertySet
));
114 aTypes
.push_back( LCL_PROP_CPPUTYPE( beans::XPropertyState
));
115 aTypes
.push_back( LCL_PROP_CPPUTYPE( beans::XMultiPropertyStates
));
116 aTypes
.push_back( LCL_PROP_CPPUTYPE( XStyleSupplier
));
118 aTypeList
= ::chart::ContainerHelper::ContainerToSequence( aTypes
);
126 class theOPropertySetImplementationId
: public rtl::Static
< UnoTunnelIdInit
, theOPropertySetImplementationId
> {};
129 Sequence
< sal_Int8
> SAL_CALL
130 OPropertySet::getImplementationId()
131 throw (uno::RuntimeException
)
133 return theOPropertySetImplementationId::get().getSeq();
136 // ____ XPropertyState ____
137 beans::PropertyState SAL_CALL
138 OPropertySet::getPropertyState( const OUString
& PropertyName
)
139 throw (beans::UnknownPropertyException
,
140 uno::RuntimeException
)
142 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
144 return m_pImplProperties
->GetPropertyStateByHandle(
145 rPH
.getHandleByName( PropertyName
));
148 Sequence
< beans::PropertyState
> SAL_CALL
149 OPropertySet::getPropertyStates( const Sequence
< OUString
>& aPropertyName
)
150 throw (beans::UnknownPropertyException
,
151 uno::RuntimeException
)
153 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
155 sal_Int32
* pHandles
= new sal_Int32
[ aPropertyName
.getLength() ];
156 rPH
.fillHandles( pHandles
, aPropertyName
);
158 ::std::vector
< sal_Int32
> aHandles( pHandles
, pHandles
+ aPropertyName
.getLength());
161 return m_pImplProperties
->GetPropertyStatesByHandle( aHandles
);
165 OPropertySet::setPropertyToDefault( const OUString
& PropertyName
)
166 throw (beans::UnknownPropertyException
,
167 uno::RuntimeException
)
169 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
171 m_pImplProperties
->SetPropertyToDefault( rPH
.getHandleByName( PropertyName
));
172 firePropertyChangeEvent();
176 OPropertySet::getPropertyDefault( const OUString
& aPropertyName
)
177 throw (beans::UnknownPropertyException
,
178 lang::WrappedTargetException
,
179 uno::RuntimeException
)
181 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
183 return GetDefaultValue( rPH
.getHandleByName( aPropertyName
) );
187 // ____ XMultiPropertyStates ____
189 // Note: getPropertyStates() is already implemented in XPropertyState with the
193 OPropertySet::setAllPropertiesToDefault()
194 throw (uno::RuntimeException
)
196 m_pImplProperties
->SetAllPropertiesToDefault();
197 firePropertyChangeEvent();
201 OPropertySet::setPropertiesToDefault( const Sequence
< OUString
>& aPropertyNames
)
202 throw (beans::UnknownPropertyException
,
203 uno::RuntimeException
)
205 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
207 sal_Int32
* pHandles
= new sal_Int32
[ aPropertyNames
.getLength() ];
208 rPH
.fillHandles( pHandles
, aPropertyNames
);
210 ::std::vector
< sal_Int32
> aHandles( pHandles
, pHandles
+ aPropertyNames
.getLength());
213 m_pImplProperties
->SetPropertiesToDefault( aHandles
);
216 Sequence
< Any
> SAL_CALL
217 OPropertySet::getPropertyDefaults( const Sequence
< OUString
>& aPropertyNames
)
218 throw (beans::UnknownPropertyException
,
219 lang::WrappedTargetException
,
220 uno::RuntimeException
)
222 ::cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
223 const sal_Int32 nElements
= aPropertyNames
.getLength();
225 Sequence
< Any
> aResult( nElements
);
226 Any
* pResultArray
= aResult
.getArray();
229 for( ; nI
< nElements
; ++nI
)
231 pResultArray
[ nI
] = GetDefaultValue(
232 rPH
.getHandleByName( aPropertyNames
[ nI
] ));
238 sal_Bool SAL_CALL
OPropertySet::convertFastPropertyValue
239 ( Any
& rConvertedValue
,
243 throw (lang::IllegalArgumentException
)
245 getFastPropertyValue( rOldValue
, nHandle
);
246 //accept longs also for short values
249 if( (rOldValue
>>=nValue
) && !(rValue
>>=nValue
) )
251 sal_Int32 n32Value
= 0;
252 if( rValue
>>=n32Value
)
254 rConvertedValue
= uno::makeAny( static_cast<sal_Int16
>(n32Value
) );
258 sal_Int64 n64Value
= 0;
259 if( rValue
>>=n64Value
)
261 rConvertedValue
= uno::makeAny( static_cast<sal_Int16
>(n64Value
) );
266 rConvertedValue
= rValue
;
267 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault
&& rOldValue
== rConvertedValue
)
268 return sal_False
;//no change necessary
272 void SAL_CALL
OPropertySet::setFastPropertyValue_NoBroadcast
275 throw (uno::Exception
)
277 #if OSL_DEBUG_LEVEL > 0
278 if( rValue
.hasValue())
280 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
282 rPH
.fillPropertyMembersByHandle( &aName
, 0, nHandle
);
283 OSL_ENSURE( rValue
.isExtractableTo( rPH
.getPropertyByName( aName
).Type
),
284 "Property type is wrong" );
291 aDefault
= GetDefaultValue( nHandle
);
293 catch( const beans::UnknownPropertyException
& )
297 m_pImplProperties
->SetPropertyValueByHandle( nHandle
, rValue
);
298 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault
&& aDefault
.hasValue() && aDefault
== rValue
) //#i98893# don't export defaults to file
299 m_pImplProperties
->SetPropertyToDefault( nHandle
);
301 m_pImplProperties
->SetPropertyValueByHandle( nHandle
, rValue
);
304 void SAL_CALL
OPropertySet::getFastPropertyValue
306 sal_Int32 nHandle
) const
308 if( ! m_pImplProperties
->GetPropertyValueByHandle( rValue
, nHandle
))
310 // property was not set -> try style
311 uno::Reference
< beans::XFastPropertySet
> xStylePropSet( m_pImplProperties
->GetStyle(), uno::UNO_QUERY
);
312 if( xStylePropSet
.is() )
316 // check if the handle of the style points to the same property
317 // name as the handle in this property set
318 uno::Reference
< beans::XPropertySet
> xPropSet( xStylePropSet
, uno::UNO_QUERY
);
321 uno::Reference
< beans::XPropertySetInfo
> xInfo( xPropSet
->getPropertySetInfo(),
325 // for some reason the virtual method getInfoHelper() is
327 ::cppu::IPropertyArrayHelper
& rPH
=
328 const_cast< OPropertySet
* >( this )->getInfoHelper();
330 // find the Property with Handle nHandle in Style
331 Sequence
< beans::Property
> aProps( xInfo
->getProperties() );
332 sal_Int32 nI
= aProps
.getLength() - 1;
333 while( ( nI
>= 0 ) && nHandle
!= aProps
[ nI
].Handle
)
336 if( nI
>= 0 ) // => nHandle == aProps[nI].Handle
338 // check whether the handle in this property set is
339 // the same as the one in the style
340 beans::Property
aProp( rPH
.getPropertyByName( aProps
[ nI
].Name
) );
341 OSL_ENSURE( nHandle
== aProp
.Handle
,
342 "HandleCheck: Handles for same property differ!" );
344 if( nHandle
== aProp
.Handle
)
346 OSL_ENSURE( aProp
.Type
== aProps
[nI
].Type
,
347 "HandleCheck: Types differ!" );
348 OSL_ENSURE( aProp
.Attributes
== aProps
[nI
].Attributes
,
349 "HandleCheck: Attributes differ!" );
354 OSL_FAIL( "HandleCheck: Handle not found in Style" );
358 OSL_FAIL( "HandleCheck: Invalid XPropertySetInfo returned" );
361 OSL_FAIL( "HandleCheck: XPropertySet not supported" );
364 rValue
= xStylePropSet
->getFastPropertyValue( nHandle
);
368 // there is no style (or the style does not support XFastPropertySet)
369 // => take the default value
372 rValue
= GetDefaultValue( nHandle
);
374 catch( const beans::UnknownPropertyException
& )
382 void OPropertySet::firePropertyChangeEvent()
384 // nothing in base class
387 // ____ XStyleSupplier ____
388 Reference
< style::XStyle
> SAL_CALL
OPropertySet::getStyle()
389 throw (uno::RuntimeException
)
391 return m_pImplProperties
->GetStyle();
394 void SAL_CALL
OPropertySet::setStyle( const Reference
< style::XStyle
>& xStyle
)
395 throw (lang::IllegalArgumentException
,
396 uno::RuntimeException
)
398 if( ! m_pImplProperties
->SetStyle( xStyle
))
399 throw lang::IllegalArgumentException(
401 static_cast< beans::XPropertySet
* >( this ),
405 // ____ XMultiPropertySet ____
406 void SAL_CALL
OPropertySet::setPropertyValues(
407 const Sequence
< OUString
>& PropertyNames
, const Sequence
< Any
>& Values
)
408 throw(beans::PropertyVetoException
,
409 lang::IllegalArgumentException
,
410 lang::WrappedTargetException
,
411 uno::RuntimeException
)
413 ::cppu::OPropertySetHelper::setPropertyValues( PropertyNames
, Values
);
415 firePropertyChangeEvent();
418 // ____ XFastPropertySet ____
419 void SAL_CALL
OPropertySet::setFastPropertyValue( sal_Int32 nHandle
, const Any
& rValue
)
420 throw(beans::UnknownPropertyException
,
421 beans::PropertyVetoException
,
422 lang::IllegalArgumentException
,
423 lang::WrappedTargetException
, uno::RuntimeException
)
425 ::cppu::OPropertySetHelper::setFastPropertyValue( nHandle
, rValue
);
427 firePropertyChangeEvent();
431 } // namespace property
433 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */