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 <comphelper/property.hxx>
21 #include <comphelper/sequence.hxx>
22 #include <osl/diagnose.h>
23 #include <sal/log.hxx>
25 #if OSL_DEBUG_LEVEL > 0
26 #include <cppuhelper/exc_hlp.hxx>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 #include <rtl/ustrbuf.hxx>
40 using ::com::sun::star::uno::Reference
;
41 using ::com::sun::star::beans::XPropertySet
;
42 using ::com::sun::star::beans::XPropertySetInfo
;
43 using ::com::sun::star::beans::Property
;
44 using ::com::sun::star::uno::Sequence
;
45 using ::com::sun::star::uno::Exception
;
46 using ::com::sun::star::uno::Any
;
47 using ::com::sun::star::uno::Type
;
48 using ::com::sun::star::uno::cpp_queryInterface
;
49 using ::com::sun::star::uno::cpp_acquire
;
50 using ::com::sun::star::uno::cpp_release
;
51 #if OSL_DEBUG_LEVEL > 0
52 using ::com::sun::star::lang::XServiceInfo
;
54 using ::com::sun::star::uno::UNO_QUERY
;
56 namespace PropertyAttribute
= ::com::sun::star::beans::PropertyAttribute
;
59 void copyProperties(const Reference
<XPropertySet
>& _rxSource
,
60 const Reference
<XPropertySet
>& _rxDest
)
62 if (!_rxSource
.is() || !_rxDest
.is())
64 OSL_FAIL("copyProperties: invalid arguments !");
68 Reference
< XPropertySetInfo
> xSourceProps
= _rxSource
->getPropertySetInfo();
69 Reference
< XPropertySetInfo
> xDestProps
= _rxDest
->getPropertySetInfo();
71 Sequence
< Property
> aSourceProps
= xSourceProps
->getProperties();
73 for (const Property
& rSourceProp
: aSourceProps
)
75 if ( xDestProps
->hasPropertyByName(rSourceProp
.Name
) )
79 aDestProp
= xDestProps
->getPropertyByName(rSourceProp
.Name
);
80 if (0 == (aDestProp
.Attributes
& PropertyAttribute::READONLY
) )
82 const Any aSourceValue
= _rxSource
->getPropertyValue(rSourceProp
.Name
);
83 if ( 0 != (aDestProp
.Attributes
& PropertyAttribute::MAYBEVOID
) || aSourceValue
.hasValue() )
84 _rxDest
->setPropertyValue(rSourceProp
.Name
, aSourceValue
);
89 #if OSL_DEBUG_LEVEL > 0
90 OUStringBuffer aBuffer
;
91 aBuffer
.append( "::comphelper::copyProperties: could not copy property '" );
92 aBuffer
.append(rSourceProp
.Name
);
93 aBuffer
.append( "' to the destination set (a '" );
95 Reference
< XServiceInfo
> xSI( _rxDest
, UNO_QUERY
);
98 aBuffer
.append( xSI
->getImplementationName() );
102 aBuffer
.append( OUString::createFromAscii(typeid( *_rxDest
.get() ).name()) );
104 aBuffer
.append( "' implementation).\n" );
106 Any
aException( ::cppu::getCaughtException() );
107 aBuffer
.append( "Caught an exception of type '" );
108 aBuffer
.append( aException
.getValueTypeName() );
109 aBuffer
.append( "'" );
111 Exception aBaseException
;
112 if ( ( aException
>>= aBaseException
) && !aBaseException
.Message
.isEmpty() )
114 aBuffer
.append( ", saying '" );
115 aBuffer
.append( aBaseException
.Message
);
116 aBuffer
.append( "'" );
118 aBuffer
.append( "." );
120 SAL_WARN( "comphelper", aBuffer
.makeStringAndClear() );
128 bool hasProperty(const OUString
& _rName
, const Reference
<XPropertySet
>& _rxSet
)
132 // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
133 return _rxSet
->getPropertySetInfo()->hasPropertyByName(_rName
);
139 void RemoveProperty(Sequence
<Property
>& _rProps
, const OUString
& _rPropName
)
141 sal_Int32 nLen
= _rProps
.getLength();
144 const Property
* pProperties
= _rProps
.getConstArray();
145 Property
aNameProp(_rPropName
, 0, Type(), 0);
146 const Property
* pResult
= std::lower_bound(pProperties
, pProperties
+ nLen
, aNameProp
, PropertyCompareByName());
148 if ( pResult
!= _rProps
.end() && pResult
->Name
== _rPropName
)
150 OSL_ENSURE(pResult
->Name
== _rPropName
, "::RemoveProperty Properties not sorted");
151 removeElementAt(_rProps
, pResult
- pProperties
);
156 void ModifyPropertyAttributes(Sequence
<Property
>& seqProps
, const OUString
& sPropName
, sal_Int16 nAddAttrib
, sal_Int16 nRemoveAttrib
)
158 sal_Int32 nLen
= seqProps
.getLength();
161 Property
* pProperties
= seqProps
.getArray();
162 Property
aNameProp(sPropName
, 0, Type(), 0);
163 Property
* pResult
= std::lower_bound(pProperties
, pProperties
+ nLen
, aNameProp
, PropertyCompareByName());
165 if ( (pResult
!= seqProps
.end()) && (pResult
->Name
== sPropName
) )
167 pResult
->Attributes
|= nAddAttrib
;
168 pResult
->Attributes
&= ~nRemoveAttrib
;
173 bool tryPropertyValue(Any
& _rConvertedValue
, Any
& _rOldValue
, const Any
& _rValueToSet
, const Any
& _rCurrentValue
, const Type
& _rExpectedType
)
175 bool bModified(false);
176 if (_rCurrentValue
.getValue() != _rValueToSet
.getValue())
178 if ( _rValueToSet
.hasValue() && ( !_rExpectedType
.equals( _rValueToSet
.getValueType() ) ) )
180 _rConvertedValue
= Any( nullptr, _rExpectedType
.getTypeLibType() );
182 if ( !uno_type_assignData(
183 const_cast< void* >( _rConvertedValue
.getValue() ), _rConvertedValue
.getValueType().getTypeLibType(),
184 const_cast< void* >( _rValueToSet
.getValue() ), _rValueToSet
.getValueType().getTypeLibType(),
185 reinterpret_cast< uno_QueryInterfaceFunc
>(
187 reinterpret_cast< uno_AcquireFunc
>(cpp_acquire
),
188 reinterpret_cast< uno_ReleaseFunc
>(cpp_release
)
191 throw css::lang::IllegalArgumentException();
194 _rConvertedValue
= _rValueToSet
;
196 if ( _rCurrentValue
!= _rConvertedValue
)
198 _rOldValue
= _rCurrentValue
;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */