merge the formfield patch from ooo-build
[ooovba.git] / comphelper / source / property / property.cxx
blobf21666c0afb1a44c94225eeb57fae37f9dbdf12d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: property.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
35 #include <comphelper/property.hxx>
36 #include <comphelper/sequence.hxx>
37 #include <comphelper/types.hxx>
38 #include <osl/diagnose.h>
40 #if OSL_DEBUG_LEVEL > 0
41 #ifndef _RTL_STRBUF_HXX_
42 #include <rtl/strbuf.hxx>
43 #endif
44 #ifndef _CPPUHELPER_EXC_HLP_HXX_
45 #include <cppuhelper/exc_hlp.hxx>
46 #endif
47 #ifndef _OSL_THREAD_H_
48 #include <osl/thread.h>
49 #endif
50 #endif
51 #include <com/sun/star/beans/PropertyAttribute.hpp>
52 #include <com/sun/star/lang/IllegalArgumentException.hpp>
53 #include <com/sun/star/uno/genfunc.h>
55 #include <algorithm>
56 #include <boost/bind.hpp>
58 //.........................................................................
59 namespace comphelper
62 /** === begin UNO using === **/
63 using ::com::sun::star::uno::Reference;
64 using ::com::sun::star::beans::XPropertySet;
65 using ::com::sun::star::beans::XPropertySetInfo;
66 using ::com::sun::star::beans::Property;
67 using ::com::sun::star::uno::Sequence;
68 using ::com::sun::star::uno::Exception;
69 using ::com::sun::star::uno::Any;
70 using ::com::sun::star::uno::Type;
71 using ::com::sun::star::uno::cpp_queryInterface;
72 using ::com::sun::star::uno::cpp_acquire;
73 using ::com::sun::star::uno::cpp_release;
74 /** === end UNO using === **/
75 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
77 //------------------------------------------------------------------
78 void copyProperties(const Reference<XPropertySet>& _rxSource,
79 const Reference<XPropertySet>& _rxDest)
81 if (!_rxSource.is() || !_rxDest.is())
83 OSL_ENSURE(sal_False, "copyProperties: invalid arguments !");
84 return;
87 Reference< XPropertySetInfo > xSourceProps = _rxSource->getPropertySetInfo();
88 Reference< XPropertySetInfo > xDestProps = _rxDest->getPropertySetInfo();
90 Sequence< Property > aSourceProps = xSourceProps->getProperties();
91 const Property* pSourceProps = aSourceProps.getConstArray();
92 Property aDestProp;
93 for (sal_Int32 i=0; i<aSourceProps.getLength(); ++i, ++pSourceProps)
95 if ( xDestProps->hasPropertyByName(pSourceProps->Name) )
97 try
99 aDestProp = xDestProps->getPropertyByName(pSourceProps->Name);
100 if (0 == (aDestProp.Attributes & PropertyAttribute::READONLY) )
102 const Any aSourceValue = _rxSource->getPropertyValue(pSourceProps->Name);
103 if ( 0 != (aDestProp.Attributes & PropertyAttribute::MAYBEVOID) || aSourceValue.hasValue() )
104 _rxDest->setPropertyValue(pSourceProps->Name, aSourceValue);
107 catch (Exception&)
109 #if OSL_DEBUG_LEVEL > 0
110 ::rtl::OStringBuffer aBuffer;
111 aBuffer.append( "::comphelper::copyProperties: could not copy property '" );
112 aBuffer.append( ::rtl::OString( pSourceProps->Name.getStr(), pSourceProps->Name.getLength(), RTL_TEXTENCODING_ASCII_US ) );
113 aBuffer.append( "' to the destination set.\n" );
115 Any aException( ::cppu::getCaughtException() );
116 aBuffer.append( "Caught an exception of type '" );
117 ::rtl::OUString sExceptionType( aException.getValueTypeName() );
118 aBuffer.append( ::rtl::OString( sExceptionType.getStr(), sExceptionType.getLength(), RTL_TEXTENCODING_ASCII_US ) );
119 aBuffer.append( "'" );
121 Exception aBaseException;
122 if ( ( aException >>= aBaseException ) && aBaseException.Message.getLength() )
124 aBuffer.append( ", saying '" );
125 aBuffer.append( ::rtl::OString( aBaseException.Message.getStr(), aBaseException.Message.getLength(), osl_getThreadTextEncoding() ) );
126 aBuffer.append( "'" );
128 aBuffer.append( "." );
130 OSL_ENSURE( sal_False, aBuffer.getStr() );
131 #endif
137 //------------------------------------------------------------------
138 sal_Bool hasProperty(const rtl::OUString& _rName, const Reference<XPropertySet>& _rxSet)
140 if (_rxSet.is())
142 // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
143 return _rxSet->getPropertySetInfo()->hasPropertyByName(_rName);
145 return sal_False;
148 //------------------------------------------------------------------
149 bool findProperty(Property& o_rProp,
150 Sequence<Property>& i_seqProps,
151 const ::rtl::OUString& i_rPropName)
153 const Property* pAry(i_seqProps.getConstArray());
154 const sal_Int32 nLen(i_seqProps.getLength());
155 const Property* pRes(
156 std::find_if(pAry,pAry+nLen,
157 boost::bind(PropertyStringEqualFunctor(),
159 boost::cref(i_rPropName))));
160 if( pRes == pAry+nLen )
161 return false;
163 o_rProp = *pRes;
164 return true;
167 //------------------------------------------------------------------
168 void RemoveProperty(Sequence<Property>& _rProps, const rtl::OUString& _rPropName)
170 sal_Int32 nLen = _rProps.getLength();
172 // binaere Suche
173 const Property* pProperties = _rProps.getConstArray();
174 const Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen, _rPropName,PropertyStringLessFunctor());
176 // gefunden ?
177 if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == _rPropName) )
179 OSL_ENSURE(pResult->Name.equals(_rPropName), "::RemoveProperty Properties nicht sortiert");
180 removeElementAt(_rProps, pResult - pProperties);
184 //------------------------------------------------------------------
185 void ModifyPropertyAttributes(Sequence<Property>& seqProps, const ::rtl::OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
187 sal_Int32 nLen = seqProps.getLength();
189 // binaere Suche
190 Property* pProperties = seqProps.getArray();
191 Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen,sPropName, PropertyStringLessFunctor());
193 // gefunden ?
194 if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == sPropName) )
196 pResult->Attributes |= nAddAttrib;
197 pResult->Attributes &= ~nRemoveAttrib;
201 //------------------------------------------------------------------
202 sal_Bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType)
204 sal_Bool bModified(sal_False);
205 if (_rCurrentValue.getValue() != _rValueToSet.getValue())
207 if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) )
209 _rConvertedValue = Any( NULL, _rExpectedType.getTypeLibType() );
211 if ( !uno_type_assignData(
212 const_cast< void* >( _rConvertedValue.getValue() ), _rConvertedValue.getValueType().getTypeLibType(),
213 const_cast< void* >( _rValueToSet.getValue() ), _rValueToSet.getValueType().getTypeLibType(),
214 reinterpret_cast< uno_QueryInterfaceFunc >(
215 cpp_queryInterface),
216 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
217 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
220 throw starlang::IllegalArgumentException();
222 else
223 _rConvertedValue = _rValueToSet;
225 if ( _rCurrentValue != _rConvertedValue )
227 _rOldValue = _rCurrentValue;
228 bModified = sal_True;
231 return bModified;
234 //.........................................................................
236 //.........................................................................