Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include <comphelper/property.hxx>
21 : : #include <comphelper/sequence.hxx>
22 : : #include <comphelper/types.hxx>
23 : : #include <osl/diagnose.h>
24 : :
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>
35 : :
36 : : #include <algorithm>
37 : : #include <boost/bind.hpp>
38 : :
39 : : //.........................................................................
40 : : namespace comphelper
41 : : {
42 : :
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;
57 : : #endif
58 : : using ::com::sun::star::uno::UNO_QUERY;
59 : : /** === end UNO using === **/
60 : : namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
61 : :
62 : : //------------------------------------------------------------------
63 : 5317 : void copyProperties(const Reference<XPropertySet>& _rxSource,
64 : : const Reference<XPropertySet>& _rxDest)
65 : : {
66 [ + - ][ - + ]: 5317 : if (!_rxSource.is() || !_rxDest.is())
[ + - ]
67 : : {
68 : : OSL_FAIL("copyProperties: invalid arguments !");
69 : 5317 : return;
70 : : }
71 : :
72 [ + - ][ + - ]: 5317 : Reference< XPropertySetInfo > xSourceProps = _rxSource->getPropertySetInfo();
73 [ + - ][ + - ]: 5317 : Reference< XPropertySetInfo > xDestProps = _rxDest->getPropertySetInfo();
74 : :
75 [ + - ][ + - ]: 5317 : Sequence< Property > aSourceProps = xSourceProps->getProperties();
76 : 5317 : const Property* pSourceProps = aSourceProps.getConstArray();
77 : 5317 : Property aDestProp;
78 [ + + ]: 307610 : for (sal_Int32 i=0; i<aSourceProps.getLength(); ++i, ++pSourceProps)
79 : : {
80 [ + - ][ + - ]: 302293 : if ( xDestProps->hasPropertyByName(pSourceProps->Name) )
[ + + ]
81 : : {
82 : : try
83 : : {
84 [ + - ][ + - ]: 189630 : aDestProp = xDestProps->getPropertyByName(pSourceProps->Name);
85 [ + + ]: 189630 : if (0 == (aDestProp.Attributes & PropertyAttribute::READONLY) )
86 : : {
87 [ + - ][ + - ]: 173918 : const Any aSourceValue = _rxSource->getPropertyValue(pSourceProps->Name);
88 [ + + ][ + - ]: 173918 : if ( 0 != (aDestProp.Attributes & PropertyAttribute::MAYBEVOID) || aSourceValue.hasValue() )
[ + - ]
89 [ + - ][ + - ]: 173918 : _rxDest->setPropertyValue(pSourceProps->Name, aSourceValue);
[ # # ]
90 : : }
91 : : }
92 [ # # ]: 0 : catch (Exception&)
93 : : {
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 '" );
99 : :
100 : : Reference< XServiceInfo > xSI( _rxDest, UNO_QUERY );
101 : : if ( xSI.is() )
102 : : {
103 : : aBuffer.append( ::rtl::OUStringToOString( xSI->getImplementationName(), osl_getThreadTextEncoding() ) );
104 : : }
105 : : else
106 : : {
107 : : aBuffer.append( typeid( *_rxDest.get() ).name() );
108 : : }
109 : : aBuffer.append( "' implementation).\n" );
110 : :
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( "'" );
116 : :
117 : : Exception aBaseException;
118 : : if ( ( aException >>= aBaseException ) && !aBaseException.Message.isEmpty() )
119 : : {
120 : : aBuffer.append( ", saying '" );
121 : : aBuffer.append( ::rtl::OString( aBaseException.Message.getStr(), aBaseException.Message.getLength(), osl_getThreadTextEncoding() ) );
122 : : aBuffer.append( "'" );
123 : : }
124 : : aBuffer.append( "." );
125 : :
126 : : OSL_FAIL( aBuffer.getStr() );
127 : : #endif
128 : : }
129 : : }
130 [ + - ]: 5317 : }
131 : : }
132 : :
133 : : //------------------------------------------------------------------
134 : 20806 : sal_Bool hasProperty(const rtl::OUString& _rName, const Reference<XPropertySet>& _rxSet)
135 : : {
136 [ + - ]: 20806 : if (_rxSet.is())
137 : : {
138 : : // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
139 [ + - ][ + - ]: 20806 : return _rxSet->getPropertySetInfo()->hasPropertyByName(_rName);
140 : : }
141 : 20806 : return sal_False;
142 : : }
143 : :
144 : : //------------------------------------------------------------------
145 : 2971 : void RemoveProperty(Sequence<Property>& _rProps, const rtl::OUString& _rPropName)
146 : : {
147 : 2971 : sal_Int32 nLen = _rProps.getLength();
148 : :
149 : : // binaere Suche
150 : 2971 : const Property* pProperties = _rProps.getConstArray();
151 : 2971 : Property aNameProp(_rPropName, 0, Type(), 0);
152 [ + - ]: 2971 : const Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen, aNameProp, PropertyCompareByName());
153 : :
154 : : // gefunden ?
155 [ + - ][ + - ]: 2971 : if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == _rPropName) )
[ + + ][ + + ]
156 : : {
157 : : OSL_ENSURE(pResult->Name.equals(_rPropName), "::RemoveProperty Properties nicht sortiert");
158 [ + - ]: 2938 : removeElementAt(_rProps, pResult - pProperties);
159 : 2971 : }
160 : 2971 : }
161 : :
162 : : //------------------------------------------------------------------
163 : 60 : void ModifyPropertyAttributes(Sequence<Property>& seqProps, const ::rtl::OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
164 : : {
165 : 60 : sal_Int32 nLen = seqProps.getLength();
166 : :
167 : : // binaere Suche
168 [ + - ]: 60 : Property* pProperties = seqProps.getArray();
169 : 60 : Property aNameProp(sPropName, 0, Type(), 0);
170 [ + - ]: 60 : Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen, aNameProp, PropertyCompareByName());
171 : :
172 : : // gefunden ?
173 [ + - ][ + - ]: 60 : if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == sPropName) )
[ + - ][ + - ]
174 : : {
175 : 60 : pResult->Attributes |= nAddAttrib;
176 : 60 : pResult->Attributes &= ~nRemoveAttrib;
177 : 60 : }
178 : 60 : }
179 : :
180 : : //------------------------------------------------------------------
181 : 778 : sal_Bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType)
182 : : {
183 : 778 : sal_Bool bModified(sal_False);
184 [ + - ]: 778 : if (_rCurrentValue.getValue() != _rValueToSet.getValue())
185 : : {
186 [ + + ][ + + ]: 778 : if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) )
[ + + ]
187 : : {
188 : 62 : _rConvertedValue = Any( NULL, _rExpectedType.getTypeLibType() );
189 : :
190 [ - + ]: 62 : if ( !uno_type_assignData(
191 : 124 : const_cast< void* >( _rConvertedValue.getValue() ), _rConvertedValue.getValueType().getTypeLibType(),
192 : 124 : const_cast< void* >( _rValueToSet.getValue() ), _rValueToSet.getValueType().getTypeLibType(),
193 : : reinterpret_cast< uno_QueryInterfaceFunc >(
194 : : cpp_queryInterface),
195 : : reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
196 : : reinterpret_cast< uno_ReleaseFunc >(cpp_release)
197 : 186 : )
198 : : )
199 [ # # ]: 0 : throw starlang::IllegalArgumentException();
200 : : }
201 : : else
202 : 716 : _rConvertedValue = _rValueToSet;
203 : :
204 [ + + ]: 778 : if ( _rCurrentValue != _rConvertedValue )
205 : : {
206 : 556 : _rOldValue = _rCurrentValue;
207 : 556 : bModified = sal_True;
208 : : }
209 : : }
210 : 778 : return bModified;
211 : : }
212 : :
213 : : //.........................................................................
214 [ + - ][ + - ]: 3699 : }
215 : : //.........................................................................
216 : :
217 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|