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 <cppuhelper/queryinterface.hxx>
27 using namespace ::com::sun::star
;
29 using ::com::sun::star::uno::Reference
;
30 using ::com::sun::star::uno::Sequence
;
31 using ::com::sun::star::uno::Any
;
32 using ::osl::MutexGuard
;
34 // needed for MS compiler
35 using ::cppu::OBroadcastHelper
;
36 using ::cppu::OPropertySetHelper
;
41 OPropertySet::OPropertySet( ::osl::Mutex
& par_rMutex
) :
42 OBroadcastHelper( par_rMutex
),
43 // the following causes a warning; there seems to be no way to avoid it
44 OPropertySetHelper( static_cast< OBroadcastHelper
& >( *this )),
45 m_rMutex( par_rMutex
),
46 m_pImplProperties( new impl::ImplOPropertySet() ),
47 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
51 OPropertySet::OPropertySet( const OPropertySet
& rOther
, ::osl::Mutex
& par_rMutex
) :
52 OBroadcastHelper( par_rMutex
),
53 // the following causes a warning; there seems to be no way to avoid it
54 OPropertySetHelper( static_cast< OBroadcastHelper
& >( *this )),
55 m_rMutex( par_rMutex
),
56 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
58 MutexGuard
aGuard( m_rMutex
);
59 if (rOther
.m_pImplProperties
)
60 m_pImplProperties
.reset(new impl::ImplOPropertySet(*rOther
.m_pImplProperties
));
63 void OPropertySet::SetNewValuesExplicitlyEvenIfTheyEqualDefault()
65 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault
= true;
68 OPropertySet::~OPropertySet()
71 Any SAL_CALL
OPropertySet::queryInterface( const uno::Type
& aType
)
73 return ::cppu::queryInterface(
75 static_cast< lang::XTypeProvider
* >( this ),
76 static_cast< beans::XPropertySet
* >( this ),
77 static_cast< beans::XMultiPropertySet
* >( this ),
78 static_cast< beans::XFastPropertySet
* >( this ),
79 static_cast< beans::XPropertyState
* >( this ),
80 static_cast< beans::XMultiPropertyStates
* >( this ),
81 static_cast< style::XStyleSupplier
* >( this ) );
84 // ____ XTypeProvider ____
85 Sequence
< uno::Type
> SAL_CALL
86 OPropertySet::getTypes()
88 static const Sequence
< uno::Type
> aTypeList
{
89 cppu::UnoType
<lang::XTypeProvider
>::get(),
90 cppu::UnoType
<beans::XPropertySet
>::get(),
91 cppu::UnoType
<beans::XMultiPropertySet
>::get(),
92 cppu::UnoType
<beans::XFastPropertySet
>::get(),
93 cppu::UnoType
<beans::XPropertyState
>::get(),
94 cppu::UnoType
<beans::XMultiPropertyStates
>::get(),
95 cppu::UnoType
<style::XStyleSupplier
>::get() };
100 Sequence
< sal_Int8
> SAL_CALL
101 OPropertySet::getImplementationId()
103 return css::uno::Sequence
<sal_Int8
>();
106 // ____ XPropertyState ____
107 beans::PropertyState SAL_CALL
108 OPropertySet::getPropertyState( const OUString
& PropertyName
)
110 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
112 return m_pImplProperties
->GetPropertyStateByHandle(
113 rPH
.getHandleByName( PropertyName
));
116 Sequence
< beans::PropertyState
> SAL_CALL
117 OPropertySet::getPropertyStates( const Sequence
< OUString
>& aPropertyName
)
119 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
121 std::unique_ptr
<sal_Int32
[]> pHandles(new sal_Int32
[ aPropertyName
.getLength() ]);
122 rPH
.fillHandles( pHandles
.get(), aPropertyName
);
124 std::vector
< sal_Int32
> aHandles( pHandles
.get(), pHandles
.get() + aPropertyName
.getLength());
127 return m_pImplProperties
->GetPropertyStatesByHandle( aHandles
);
131 OPropertySet::setPropertyToDefault( const OUString
& PropertyName
)
133 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
135 m_pImplProperties
->SetPropertyToDefault( rPH
.getHandleByName( PropertyName
));
136 firePropertyChangeEvent();
140 OPropertySet::getPropertyDefault( const OUString
& aPropertyName
)
142 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
144 return GetDefaultValue( rPH
.getHandleByName( aPropertyName
) );
147 // ____ XMultiPropertyStates ____
149 // Note: getPropertyStates() is already implemented in XPropertyState with the
153 OPropertySet::setAllPropertiesToDefault()
155 m_pImplProperties
->SetAllPropertiesToDefault();
156 firePropertyChangeEvent();
160 OPropertySet::setPropertiesToDefault( const Sequence
< OUString
>& aPropertyNames
)
162 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
164 std::unique_ptr
<sal_Int32
[]> pHandles(new sal_Int32
[ aPropertyNames
.getLength() ]);
165 rPH
.fillHandles( pHandles
.get(), aPropertyNames
);
167 std::vector
< sal_Int32
> aHandles( pHandles
.get(), pHandles
.get() + aPropertyNames
.getLength());
170 m_pImplProperties
->SetPropertiesToDefault( aHandles
);
173 Sequence
< Any
> SAL_CALL
174 OPropertySet::getPropertyDefaults( const Sequence
< OUString
>& aPropertyNames
)
176 ::cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
177 const sal_Int32 nElements
= aPropertyNames
.getLength();
179 Sequence
< Any
> aResult( nElements
);
180 Any
* pResultArray
= aResult
.getArray();
183 for( ; nI
< nElements
; ++nI
)
185 pResultArray
[ nI
] = GetDefaultValue(
186 rPH
.getHandleByName( aPropertyNames
[ nI
] ));
192 sal_Bool SAL_CALL
OPropertySet::convertFastPropertyValue
193 ( Any
& rConvertedValue
,
198 getFastPropertyValue( rOldValue
, nHandle
);
199 //accept longs also for short values
202 if( (rOldValue
>>=nValue
) && !(rValue
>>=nValue
) )
204 sal_Int32 n32Value
= 0;
205 if( rValue
>>=n32Value
)
207 rConvertedValue
<<= static_cast<sal_Int16
>(n32Value
);
211 sal_Int64 n64Value
= 0;
212 if( rValue
>>=n64Value
)
214 rConvertedValue
<<= static_cast<sal_Int16
>(n64Value
);
219 rConvertedValue
= rValue
;
220 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault
&& rOldValue
== rConvertedValue
)
221 return false;//no change necessary
225 void SAL_CALL
OPropertySet::setFastPropertyValue_NoBroadcast
229 #if OSL_DEBUG_LEVEL > 0
230 if( rValue
.hasValue())
232 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
234 rPH
.fillPropertyMembersByHandle( &aName
, nullptr, nHandle
);
235 OSL_ENSURE( rValue
.isExtractableTo( rPH
.getPropertyByName( aName
).Type
),
236 "Property type is wrong" );
243 aDefault
= GetDefaultValue( nHandle
);
245 catch( const beans::UnknownPropertyException
& )
249 m_pImplProperties
->SetPropertyValueByHandle( nHandle
, rValue
);
250 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault
&& aDefault
.hasValue() && aDefault
== rValue
) //#i98893# don't export defaults to file
251 m_pImplProperties
->SetPropertyToDefault( nHandle
);
253 m_pImplProperties
->SetPropertyValueByHandle( nHandle
, rValue
);
256 void SAL_CALL
OPropertySet::getFastPropertyValue
258 sal_Int32 nHandle
) const
260 if( ! m_pImplProperties
->GetPropertyValueByHandle( rValue
, nHandle
))
262 // property was not set -> try style
263 uno::Reference
< beans::XFastPropertySet
> xStylePropSet( m_pImplProperties
->GetStyle(), uno::UNO_QUERY
);
264 if( xStylePropSet
.is() )
268 // check if the handle of the style points to the same property
269 // name as the handle in this property set
270 uno::Reference
< beans::XPropertySet
> xPropSet( xStylePropSet
, uno::UNO_QUERY
);
273 uno::Reference
< beans::XPropertySetInfo
> xInfo
= xPropSet
->getPropertySetInfo();
276 // for some reason the virtual method getInfoHelper() is
278 ::cppu::IPropertyArrayHelper
& rPH
=
279 const_cast< OPropertySet
* >( this )->getInfoHelper();
281 // find the Property with Handle nHandle in Style
282 Sequence
< beans::Property
> aProps( xInfo
->getProperties() );
283 sal_Int32 nI
= aProps
.getLength() - 1;
284 while( ( nI
>= 0 ) && nHandle
!= aProps
[ nI
].Handle
)
287 if( nI
>= 0 ) // => nHandle == aProps[nI].Handle
289 // check whether the handle in this property set is
290 // the same as the one in the style
291 beans::Property
aProp( rPH
.getPropertyByName( aProps
[ nI
].Name
) );
292 OSL_ENSURE( nHandle
== aProp
.Handle
,
293 "HandleCheck: Handles for same property differ!" );
295 if( nHandle
== aProp
.Handle
)
297 OSL_ENSURE( aProp
.Type
== aProps
[nI
].Type
,
298 "HandleCheck: Types differ!" );
299 OSL_ENSURE( aProp
.Attributes
== aProps
[nI
].Attributes
,
300 "HandleCheck: Attributes differ!" );
305 OSL_FAIL( "HandleCheck: Handle not found in Style" );
309 OSL_FAIL( "HandleCheck: Invalid XPropertySetInfo returned" );
312 OSL_FAIL( "HandleCheck: XPropertySet not supported" );
315 rValue
= xStylePropSet
->getFastPropertyValue( nHandle
);
319 // there is no style (or the style does not support XFastPropertySet)
320 // => take the default value
323 rValue
= GetDefaultValue( nHandle
);
325 catch( const beans::UnknownPropertyException
& )
333 void OPropertySet::firePropertyChangeEvent()
335 // nothing in base class
338 // ____ XStyleSupplier ____
339 Reference
< style::XStyle
> SAL_CALL
OPropertySet::getStyle()
341 return m_pImplProperties
->GetStyle();
344 void SAL_CALL
OPropertySet::setStyle( const Reference
< style::XStyle
>& xStyle
)
346 if( ! m_pImplProperties
->SetStyle( xStyle
))
347 throw lang::IllegalArgumentException(
349 static_cast< beans::XPropertySet
* >( this ),
353 // ____ XMultiPropertySet ____
354 void SAL_CALL
OPropertySet::setPropertyValues(
355 const Sequence
< OUString
>& PropertyNames
, const Sequence
< Any
>& Values
)
357 ::cppu::OPropertySetHelper::setPropertyValues( PropertyNames
, Values
);
359 firePropertyChangeEvent();
362 // ____ XFastPropertySet ____
363 void SAL_CALL
OPropertySet::setFastPropertyValue( sal_Int32 nHandle
, const Any
& rValue
)
365 ::cppu::OPropertySetHelper::setFastPropertyValue( nHandle
, rValue
);
367 firePropertyChangeEvent();
370 } // namespace property
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */