1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*dde7d3faSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*dde7d3faSAndrew Rist * distributed with this work for additional information
6*dde7d3faSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*dde7d3faSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*dde7d3faSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*dde7d3faSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist * KIND, either express or implied. See the License for the
17*dde7d3faSAndrew Rist * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*dde7d3faSAndrew Rist *************************************************************/
21*dde7d3faSAndrew Rist
22*dde7d3faSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir
28cdf0e10cSrcweir #include <comphelper/property.hxx>
29cdf0e10cSrcweir #include <comphelper/sequence.hxx>
30cdf0e10cSrcweir #include <comphelper/types.hxx>
31cdf0e10cSrcweir #include <osl/diagnose.h>
32cdf0e10cSrcweir
33cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
34cdf0e10cSrcweir #include <rtl/strbuf.hxx>
35cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
36cdf0e10cSrcweir #include <osl/thread.h>
37cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
38cdf0e10cSrcweir #include <typeinfo>
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir #include <com/sun/star/beans/PropertyAttribute.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
42cdf0e10cSrcweir #include <com/sun/star/uno/genfunc.h>
43cdf0e10cSrcweir
44cdf0e10cSrcweir #include <algorithm>
45cdf0e10cSrcweir #include <boost/bind.hpp>
46cdf0e10cSrcweir
47cdf0e10cSrcweir //.........................................................................
48cdf0e10cSrcweir namespace comphelper
49cdf0e10cSrcweir {
50cdf0e10cSrcweir
51cdf0e10cSrcweir /** === begin UNO using === **/
52cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
53cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySet;
54cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySetInfo;
55cdf0e10cSrcweir using ::com::sun::star::beans::Property;
56cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
57cdf0e10cSrcweir using ::com::sun::star::uno::Exception;
58cdf0e10cSrcweir using ::com::sun::star::uno::Any;
59cdf0e10cSrcweir using ::com::sun::star::uno::Type;
60cdf0e10cSrcweir using ::com::sun::star::uno::cpp_queryInterface;
61cdf0e10cSrcweir using ::com::sun::star::uno::cpp_acquire;
62cdf0e10cSrcweir using ::com::sun::star::uno::cpp_release;
63cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
64cdf0e10cSrcweir using ::com::sun::star::lang::XServiceInfo;
65cdf0e10cSrcweir #endif
66cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY;
67cdf0e10cSrcweir /** === end UNO using === **/
68cdf0e10cSrcweir namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
69cdf0e10cSrcweir
70cdf0e10cSrcweir //------------------------------------------------------------------
copyProperties(const Reference<XPropertySet> & _rxSource,const Reference<XPropertySet> & _rxDest)71cdf0e10cSrcweir void copyProperties(const Reference<XPropertySet>& _rxSource,
72cdf0e10cSrcweir const Reference<XPropertySet>& _rxDest)
73cdf0e10cSrcweir {
74cdf0e10cSrcweir if (!_rxSource.is() || !_rxDest.is())
75cdf0e10cSrcweir {
76cdf0e10cSrcweir OSL_ENSURE(sal_False, "copyProperties: invalid arguments !");
77cdf0e10cSrcweir return;
78cdf0e10cSrcweir }
79cdf0e10cSrcweir
80cdf0e10cSrcweir Reference< XPropertySetInfo > xSourceProps = _rxSource->getPropertySetInfo();
81cdf0e10cSrcweir Reference< XPropertySetInfo > xDestProps = _rxDest->getPropertySetInfo();
82cdf0e10cSrcweir
83cdf0e10cSrcweir Sequence< Property > aSourceProps = xSourceProps->getProperties();
84cdf0e10cSrcweir const Property* pSourceProps = aSourceProps.getConstArray();
85cdf0e10cSrcweir Property aDestProp;
86cdf0e10cSrcweir for (sal_Int32 i=0; i<aSourceProps.getLength(); ++i, ++pSourceProps)
87cdf0e10cSrcweir {
88cdf0e10cSrcweir if ( xDestProps->hasPropertyByName(pSourceProps->Name) )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir try
91cdf0e10cSrcweir {
92cdf0e10cSrcweir aDestProp = xDestProps->getPropertyByName(pSourceProps->Name);
93cdf0e10cSrcweir if (0 == (aDestProp.Attributes & PropertyAttribute::READONLY) )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir const Any aSourceValue = _rxSource->getPropertyValue(pSourceProps->Name);
96cdf0e10cSrcweir if ( 0 != (aDestProp.Attributes & PropertyAttribute::MAYBEVOID) || aSourceValue.hasValue() )
97cdf0e10cSrcweir _rxDest->setPropertyValue(pSourceProps->Name, aSourceValue);
98cdf0e10cSrcweir }
99cdf0e10cSrcweir }
100cdf0e10cSrcweir catch (Exception&)
101cdf0e10cSrcweir {
102cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
103cdf0e10cSrcweir ::rtl::OStringBuffer aBuffer;
104cdf0e10cSrcweir aBuffer.append( "::comphelper::copyProperties: could not copy property '" );
105cdf0e10cSrcweir aBuffer.append( ::rtl::OString( pSourceProps->Name.getStr(), pSourceProps->Name.getLength(), RTL_TEXTENCODING_ASCII_US ) );
106cdf0e10cSrcweir aBuffer.append( "' to the destination set (a '" );
107cdf0e10cSrcweir
108cdf0e10cSrcweir Reference< XServiceInfo > xSI( _rxDest, UNO_QUERY );
109cdf0e10cSrcweir if ( xSI.is() )
110cdf0e10cSrcweir {
111cdf0e10cSrcweir aBuffer.append( ::rtl::OUStringToOString( xSI->getImplementationName(), osl_getThreadTextEncoding() ) );
112cdf0e10cSrcweir }
113cdf0e10cSrcweir else
114cdf0e10cSrcweir {
115cdf0e10cSrcweir aBuffer.append( typeid( *_rxDest.get() ).name() );
116cdf0e10cSrcweir }
117cdf0e10cSrcweir aBuffer.append( "' implementation).\n" );
118cdf0e10cSrcweir
119cdf0e10cSrcweir Any aException( ::cppu::getCaughtException() );
120cdf0e10cSrcweir aBuffer.append( "Caught an exception of type '" );
121cdf0e10cSrcweir ::rtl::OUString sExceptionType( aException.getValueTypeName() );
122cdf0e10cSrcweir aBuffer.append( ::rtl::OString( sExceptionType.getStr(), sExceptionType.getLength(), RTL_TEXTENCODING_ASCII_US ) );
123cdf0e10cSrcweir aBuffer.append( "'" );
124cdf0e10cSrcweir
125cdf0e10cSrcweir Exception aBaseException;
126cdf0e10cSrcweir if ( ( aException >>= aBaseException ) && aBaseException.Message.getLength() )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir aBuffer.append( ", saying '" );
129cdf0e10cSrcweir aBuffer.append( ::rtl::OString( aBaseException.Message.getStr(), aBaseException.Message.getLength(), osl_getThreadTextEncoding() ) );
130cdf0e10cSrcweir aBuffer.append( "'" );
131cdf0e10cSrcweir }
132cdf0e10cSrcweir aBuffer.append( "." );
133cdf0e10cSrcweir
134cdf0e10cSrcweir OSL_ENSURE( sal_False, aBuffer.getStr() );
135cdf0e10cSrcweir #endif
136cdf0e10cSrcweir }
137cdf0e10cSrcweir }
138cdf0e10cSrcweir }
139cdf0e10cSrcweir }
140cdf0e10cSrcweir
141cdf0e10cSrcweir //------------------------------------------------------------------
hasProperty(const rtl::OUString & _rName,const Reference<XPropertySet> & _rxSet)142cdf0e10cSrcweir sal_Bool hasProperty(const rtl::OUString& _rName, const Reference<XPropertySet>& _rxSet)
143cdf0e10cSrcweir {
144cdf0e10cSrcweir if (_rxSet.is())
145cdf0e10cSrcweir {
146cdf0e10cSrcweir // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
147cdf0e10cSrcweir return _rxSet->getPropertySetInfo()->hasPropertyByName(_rName);
148cdf0e10cSrcweir }
149cdf0e10cSrcweir return sal_False;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir
152cdf0e10cSrcweir //------------------------------------------------------------------
findProperty(Property & o_rProp,Sequence<Property> & i_seqProps,const::rtl::OUString & i_rPropName)153cdf0e10cSrcweir bool findProperty(Property& o_rProp,
154cdf0e10cSrcweir Sequence<Property>& i_seqProps,
155cdf0e10cSrcweir const ::rtl::OUString& i_rPropName)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir const Property* pAry(i_seqProps.getConstArray());
158cdf0e10cSrcweir const sal_Int32 nLen(i_seqProps.getLength());
159cdf0e10cSrcweir const Property* pRes(
160cdf0e10cSrcweir std::find_if(pAry,pAry+nLen,
161cdf0e10cSrcweir boost::bind(PropertyStringEqualFunctor(),
162cdf0e10cSrcweir _1,
163cdf0e10cSrcweir boost::cref(i_rPropName))));
164cdf0e10cSrcweir if( pRes == pAry+nLen )
165cdf0e10cSrcweir return false;
166cdf0e10cSrcweir
167cdf0e10cSrcweir o_rProp = *pRes;
168cdf0e10cSrcweir return true;
169cdf0e10cSrcweir }
170cdf0e10cSrcweir
171cdf0e10cSrcweir //------------------------------------------------------------------
RemoveProperty(Sequence<Property> & _rProps,const rtl::OUString & _rPropName)172cdf0e10cSrcweir void RemoveProperty(Sequence<Property>& _rProps, const rtl::OUString& _rPropName)
173cdf0e10cSrcweir {
174cdf0e10cSrcweir sal_Int32 nLen = _rProps.getLength();
175cdf0e10cSrcweir
176cdf0e10cSrcweir // binaere Suche
177cdf0e10cSrcweir const Property* pProperties = _rProps.getConstArray();
178cdf0e10cSrcweir const Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen, _rPropName,PropertyStringLessFunctor());
179cdf0e10cSrcweir
180cdf0e10cSrcweir // gefunden ?
181cdf0e10cSrcweir if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == _rPropName) )
182cdf0e10cSrcweir {
183cdf0e10cSrcweir OSL_ENSURE(pResult->Name.equals(_rPropName), "::RemoveProperty Properties nicht sortiert");
184cdf0e10cSrcweir removeElementAt(_rProps, pResult - pProperties);
185cdf0e10cSrcweir }
186cdf0e10cSrcweir }
187cdf0e10cSrcweir
188cdf0e10cSrcweir //------------------------------------------------------------------
ModifyPropertyAttributes(Sequence<Property> & seqProps,const::rtl::OUString & sPropName,sal_Int16 nAddAttrib,sal_Int16 nRemoveAttrib)189cdf0e10cSrcweir void ModifyPropertyAttributes(Sequence<Property>& seqProps, const ::rtl::OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
190cdf0e10cSrcweir {
191cdf0e10cSrcweir sal_Int32 nLen = seqProps.getLength();
192cdf0e10cSrcweir
193cdf0e10cSrcweir // binaere Suche
194cdf0e10cSrcweir Property* pProperties = seqProps.getArray();
195cdf0e10cSrcweir Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen,sPropName, PropertyStringLessFunctor());
196cdf0e10cSrcweir
197cdf0e10cSrcweir // gefunden ?
198cdf0e10cSrcweir if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == sPropName) )
199cdf0e10cSrcweir {
200cdf0e10cSrcweir pResult->Attributes |= nAddAttrib;
201cdf0e10cSrcweir pResult->Attributes &= ~nRemoveAttrib;
202cdf0e10cSrcweir }
203cdf0e10cSrcweir }
204cdf0e10cSrcweir
205cdf0e10cSrcweir //------------------------------------------------------------------
tryPropertyValue(Any & _rConvertedValue,Any & _rOldValue,const Any & _rValueToSet,const Any & _rCurrentValue,const Type & _rExpectedType)206cdf0e10cSrcweir sal_Bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType)
207cdf0e10cSrcweir {
208cdf0e10cSrcweir sal_Bool bModified(sal_False);
209cdf0e10cSrcweir if (_rCurrentValue.getValue() != _rValueToSet.getValue())
210cdf0e10cSrcweir {
211cdf0e10cSrcweir if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir _rConvertedValue = Any( NULL, _rExpectedType.getTypeLibType() );
214cdf0e10cSrcweir
215cdf0e10cSrcweir if ( !uno_type_assignData(
216cdf0e10cSrcweir const_cast< void* >( _rConvertedValue.getValue() ), _rConvertedValue.getValueType().getTypeLibType(),
217cdf0e10cSrcweir const_cast< void* >( _rValueToSet.getValue() ), _rValueToSet.getValueType().getTypeLibType(),
218cdf0e10cSrcweir reinterpret_cast< uno_QueryInterfaceFunc >(
219cdf0e10cSrcweir cpp_queryInterface),
220cdf0e10cSrcweir reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
221cdf0e10cSrcweir reinterpret_cast< uno_ReleaseFunc >(cpp_release)
222cdf0e10cSrcweir )
223cdf0e10cSrcweir )
224cdf0e10cSrcweir throw starlang::IllegalArgumentException();
225cdf0e10cSrcweir }
226cdf0e10cSrcweir else
227cdf0e10cSrcweir _rConvertedValue = _rValueToSet;
228cdf0e10cSrcweir
229cdf0e10cSrcweir if ( _rCurrentValue != _rConvertedValue )
230cdf0e10cSrcweir {
231cdf0e10cSrcweir _rOldValue = _rCurrentValue;
232cdf0e10cSrcweir bModified = sal_True;
233cdf0e10cSrcweir }
234cdf0e10cSrcweir }
235cdf0e10cSrcweir return bModified;
236cdf0e10cSrcweir }
237cdf0e10cSrcweir
238cdf0e10cSrcweir //.........................................................................
239cdf0e10cSrcweir }
240cdf0e10cSrcweir //.........................................................................
241cdf0e10cSrcweir
242