bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / property / property.cxx
blobf22561c96373bf4704b0b0f533139a3d4ab0c406
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
30 #include <typeinfo>
31 #endif
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
34 #include <com/sun/star/uno/genfunc.h>
36 #include <algorithm>
37 #include <boost/bind.hpp>
40 namespace comphelper
43 using ::com::sun::star::uno::Reference;
44 using ::com::sun::star::beans::XPropertySet;
45 using ::com::sun::star::beans::XPropertySetInfo;
46 using ::com::sun::star::beans::Property;
47 using ::com::sun::star::uno::Sequence;
48 using ::com::sun::star::uno::Exception;
49 using ::com::sun::star::uno::Any;
50 using ::com::sun::star::uno::Type;
51 using ::com::sun::star::uno::cpp_queryInterface;
52 using ::com::sun::star::uno::cpp_acquire;
53 using ::com::sun::star::uno::cpp_release;
54 #if OSL_DEBUG_LEVEL > 0
55 using ::com::sun::star::lang::XServiceInfo;
56 #endif
57 using ::com::sun::star::uno::UNO_QUERY;
59 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
62 void copyProperties(const Reference<XPropertySet>& _rxSource,
63 const Reference<XPropertySet>& _rxDest)
65 if (!_rxSource.is() || !_rxDest.is())
67 OSL_FAIL("copyProperties: invalid arguments !");
68 return;
71 Reference< XPropertySetInfo > xSourceProps = _rxSource->getPropertySetInfo();
72 Reference< XPropertySetInfo > xDestProps = _rxDest->getPropertySetInfo();
74 Sequence< Property > aSourceProps = xSourceProps->getProperties();
75 const Property* pSourceProps = aSourceProps.getConstArray();
76 Property aDestProp;
77 for (sal_Int32 i=0; i<aSourceProps.getLength(); ++i, ++pSourceProps)
79 if ( xDestProps->hasPropertyByName(pSourceProps->Name) )
81 try
83 aDestProp = xDestProps->getPropertyByName(pSourceProps->Name);
84 if (0 == (aDestProp.Attributes & PropertyAttribute::READONLY) )
86 const Any aSourceValue = _rxSource->getPropertyValue(pSourceProps->Name);
87 if ( 0 != (aDestProp.Attributes & PropertyAttribute::MAYBEVOID) || aSourceValue.hasValue() )
88 _rxDest->setPropertyValue(pSourceProps->Name, aSourceValue);
91 catch (Exception&)
93 #if OSL_DEBUG_LEVEL > 0
94 OStringBuffer aBuffer;
95 aBuffer.append( "::comphelper::copyProperties: could not copy property '" );
96 aBuffer.append( OString( pSourceProps->Name.getStr(), pSourceProps->Name.getLength(), RTL_TEXTENCODING_ASCII_US ) );
97 aBuffer.append( "' to the destination set (a '" );
99 Reference< XServiceInfo > xSI( _rxDest, UNO_QUERY );
100 if ( xSI.is() )
102 aBuffer.append( OUStringToOString( xSI->getImplementationName(), osl_getThreadTextEncoding() ) );
104 else
106 aBuffer.append( typeid( *_rxDest.get() ).name() );
108 aBuffer.append( "' implementation).\n" );
110 Any aException( ::cppu::getCaughtException() );
111 aBuffer.append( "Caught an exception of type '" );
112 OUString sExceptionType( aException.getValueTypeName() );
113 aBuffer.append( OString( sExceptionType.getStr(), sExceptionType.getLength(), RTL_TEXTENCODING_ASCII_US ) );
114 aBuffer.append( "'" );
116 Exception aBaseException;
117 if ( ( aException >>= aBaseException ) && !aBaseException.Message.isEmpty() )
119 aBuffer.append( ", saying '" );
120 aBuffer.append( OString( aBaseException.Message.getStr(), aBaseException.Message.getLength(), osl_getThreadTextEncoding() ) );
121 aBuffer.append( "'" );
123 aBuffer.append( "." );
125 OSL_FAIL( aBuffer.getStr() );
126 #endif
133 bool hasProperty(const OUString& _rName, const Reference<XPropertySet>& _rxSet)
135 if (_rxSet.is())
137 // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
138 return _rxSet->getPropertySetInfo()->hasPropertyByName(_rName);
140 return false;
144 void RemoveProperty(Sequence<Property>& _rProps, const OUString& _rPropName)
146 sal_Int32 nLen = _rProps.getLength();
148 // binaere Suche
149 const Property* pProperties = _rProps.getConstArray();
150 Property aNameProp(_rPropName, 0, Type(), 0);
151 const Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen, aNameProp, PropertyCompareByName());
153 // gefunden ?
154 if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == _rPropName) )
156 OSL_ENSURE(pResult->Name.equals(_rPropName), "::RemoveProperty Properties nicht sortiert");
157 removeElementAt(_rProps, pResult - pProperties);
162 void ModifyPropertyAttributes(Sequence<Property>& seqProps, const OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
164 sal_Int32 nLen = seqProps.getLength();
166 // binaere Suche
167 Property* pProperties = seqProps.getArray();
168 Property aNameProp(sPropName, 0, Type(), 0);
169 Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen, aNameProp, PropertyCompareByName());
171 // gefunden ?
172 if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == sPropName) )
174 pResult->Attributes |= nAddAttrib;
175 pResult->Attributes &= ~nRemoveAttrib;
180 bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType)
182 bool bModified(false);
183 if (_rCurrentValue.getValue() != _rValueToSet.getValue())
185 if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) )
187 _rConvertedValue = Any( NULL, _rExpectedType.getTypeLibType() );
189 if ( !uno_type_assignData(
190 const_cast< void* >( _rConvertedValue.getValue() ), _rConvertedValue.getValueType().getTypeLibType(),
191 const_cast< void* >( _rValueToSet.getValue() ), _rValueToSet.getValueType().getTypeLibType(),
192 reinterpret_cast< uno_QueryInterfaceFunc >(
193 cpp_queryInterface),
194 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
195 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
198 throw css::lang::IllegalArgumentException();
200 else
201 _rConvertedValue = _rValueToSet;
203 if ( _rCurrentValue != _rConvertedValue )
205 _rOldValue = _rCurrentValue;
206 bModified = true;
209 return bModified;
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */