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 <comphelper/types.hxx>
23 #include <osl/diagnose.h>
25 #if OSL_DEBUG_LEVEL > 0
26 #include <rtl/strbuf.hxx>
27 #include <cppuhelper/exc_hlp.hxx>
28 #include <osl/thread.h>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
34 #include <com/sun/star/uno/genfunc.h>
37 #include <boost/bind.hpp>
39 //.........................................................................
43 /** === begin UNO using === **/
44 using ::com::sun::star::uno::Reference
;
45 using ::com::sun::star::beans::XPropertySet
;
46 using ::com::sun::star::beans::XPropertySetInfo
;
47 using ::com::sun::star::beans::Property
;
48 using ::com::sun::star::uno::Sequence
;
49 using ::com::sun::star::uno::Exception
;
50 using ::com::sun::star::uno::Any
;
51 using ::com::sun::star::uno::Type
;
52 using ::com::sun::star::uno::cpp_queryInterface
;
53 using ::com::sun::star::uno::cpp_acquire
;
54 using ::com::sun::star::uno::cpp_release
;
55 #if OSL_DEBUG_LEVEL > 0
56 using ::com::sun::star::lang::XServiceInfo
;
58 using ::com::sun::star::uno::UNO_QUERY
;
59 /** === end UNO using === **/
60 namespace PropertyAttribute
= ::com::sun::star::beans::PropertyAttribute
;
62 //------------------------------------------------------------------
63 void copyProperties(const Reference
<XPropertySet
>& _rxSource
,
64 const Reference
<XPropertySet
>& _rxDest
)
66 if (!_rxSource
.is() || !_rxDest
.is())
68 OSL_FAIL("copyProperties: invalid arguments !");
72 Reference
< XPropertySetInfo
> xSourceProps
= _rxSource
->getPropertySetInfo();
73 Reference
< XPropertySetInfo
> xDestProps
= _rxDest
->getPropertySetInfo();
75 Sequence
< Property
> aSourceProps
= xSourceProps
->getProperties();
76 const Property
* pSourceProps
= aSourceProps
.getConstArray();
78 for (sal_Int32 i
=0; i
<aSourceProps
.getLength(); ++i
, ++pSourceProps
)
80 if ( xDestProps
->hasPropertyByName(pSourceProps
->Name
) )
84 aDestProp
= xDestProps
->getPropertyByName(pSourceProps
->Name
);
85 if (0 == (aDestProp
.Attributes
& PropertyAttribute::READONLY
) )
87 const Any aSourceValue
= _rxSource
->getPropertyValue(pSourceProps
->Name
);
88 if ( 0 != (aDestProp
.Attributes
& PropertyAttribute::MAYBEVOID
) || aSourceValue
.hasValue() )
89 _rxDest
->setPropertyValue(pSourceProps
->Name
, aSourceValue
);
94 #if OSL_DEBUG_LEVEL > 0
95 ::rtl::OStringBuffer aBuffer
;
96 aBuffer
.append( "::comphelper::copyProperties: could not copy property '" );
97 aBuffer
.append( ::rtl::OString( pSourceProps
->Name
.getStr(), pSourceProps
->Name
.getLength(), RTL_TEXTENCODING_ASCII_US
) );
98 aBuffer
.append( "' to the destination set (a '" );
100 Reference
< XServiceInfo
> xSI( _rxDest
, UNO_QUERY
);
103 aBuffer
.append( ::rtl::OUStringToOString( xSI
->getImplementationName(), osl_getThreadTextEncoding() ) );
107 aBuffer
.append( typeid( *_rxDest
.get() ).name() );
109 aBuffer
.append( "' implementation).\n" );
111 Any
aException( ::cppu::getCaughtException() );
112 aBuffer
.append( "Caught an exception of type '" );
113 ::rtl::OUString
sExceptionType( aException
.getValueTypeName() );
114 aBuffer
.append( ::rtl::OString( sExceptionType
.getStr(), sExceptionType
.getLength(), RTL_TEXTENCODING_ASCII_US
) );
115 aBuffer
.append( "'" );
117 Exception aBaseException
;
118 if ( ( aException
>>= aBaseException
) && !aBaseException
.Message
.isEmpty() )
120 aBuffer
.append( ", saying '" );
121 aBuffer
.append( ::rtl::OString( aBaseException
.Message
.getStr(), aBaseException
.Message
.getLength(), osl_getThreadTextEncoding() ) );
122 aBuffer
.append( "'" );
124 aBuffer
.append( "." );
126 OSL_FAIL( aBuffer
.getStr() );
133 //------------------------------------------------------------------
134 sal_Bool
hasProperty(const rtl::OUString
& _rName
, const Reference
<XPropertySet
>& _rxSet
)
138 // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
139 return _rxSet
->getPropertySetInfo()->hasPropertyByName(_rName
);
144 //------------------------------------------------------------------
145 void RemoveProperty(Sequence
<Property
>& _rProps
, const rtl::OUString
& _rPropName
)
147 sal_Int32 nLen
= _rProps
.getLength();
150 const Property
* pProperties
= _rProps
.getConstArray();
151 Property
aNameProp(_rPropName
, 0, Type(), 0);
152 const Property
* pResult
= ::std::lower_bound(pProperties
, pProperties
+ nLen
, aNameProp
, PropertyCompareByName());
155 if ( pResult
&& (pResult
!= pProperties
+ nLen
) && (pResult
->Name
== _rPropName
) )
157 OSL_ENSURE(pResult
->Name
.equals(_rPropName
), "::RemoveProperty Properties nicht sortiert");
158 removeElementAt(_rProps
, pResult
- pProperties
);
162 //------------------------------------------------------------------
163 void ModifyPropertyAttributes(Sequence
<Property
>& seqProps
, const ::rtl::OUString
& sPropName
, sal_Int16 nAddAttrib
, sal_Int16 nRemoveAttrib
)
165 sal_Int32 nLen
= seqProps
.getLength();
168 Property
* pProperties
= seqProps
.getArray();
169 Property
aNameProp(sPropName
, 0, Type(), 0);
170 Property
* pResult
= ::std::lower_bound(pProperties
, pProperties
+ nLen
, aNameProp
, PropertyCompareByName());
173 if ( pResult
&& (pResult
!= pProperties
+ nLen
) && (pResult
->Name
== sPropName
) )
175 pResult
->Attributes
|= nAddAttrib
;
176 pResult
->Attributes
&= ~nRemoveAttrib
;
180 //------------------------------------------------------------------
181 sal_Bool
tryPropertyValue(Any
& _rConvertedValue
, Any
& _rOldValue
, const Any
& _rValueToSet
, const Any
& _rCurrentValue
, const Type
& _rExpectedType
)
183 sal_Bool
bModified(sal_False
);
184 if (_rCurrentValue
.getValue() != _rValueToSet
.getValue())
186 if ( _rValueToSet
.hasValue() && ( !_rExpectedType
.equals( _rValueToSet
.getValueType() ) ) )
188 _rConvertedValue
= Any( NULL
, _rExpectedType
.getTypeLibType() );
190 if ( !uno_type_assignData(
191 const_cast< void* >( _rConvertedValue
.getValue() ), _rConvertedValue
.getValueType().getTypeLibType(),
192 const_cast< void* >( _rValueToSet
.getValue() ), _rValueToSet
.getValueType().getTypeLibType(),
193 reinterpret_cast< uno_QueryInterfaceFunc
>(
195 reinterpret_cast< uno_AcquireFunc
>(cpp_acquire
),
196 reinterpret_cast< uno_ReleaseFunc
>(cpp_release
)
199 throw starlang::IllegalArgumentException();
202 _rConvertedValue
= _rValueToSet
;
204 if ( _rCurrentValue
!= _rConvertedValue
)
206 _rOldValue
= _rCurrentValue
;
207 bModified
= sal_True
;
213 //.........................................................................
215 //.........................................................................
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */