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 : 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;
58 :
59 : namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
60 :
61 :
62 3813 : void copyProperties(const Reference<XPropertySet>& _rxSource,
63 : const Reference<XPropertySet>& _rxDest)
64 : {
65 3813 : if (!_rxSource.is() || !_rxDest.is())
66 : {
67 : OSL_FAIL("copyProperties: invalid arguments !");
68 3813 : return;
69 : }
70 :
71 3813 : Reference< XPropertySetInfo > xSourceProps = _rxSource->getPropertySetInfo();
72 7626 : Reference< XPropertySetInfo > xDestProps = _rxDest->getPropertySetInfo();
73 :
74 7626 : Sequence< Property > aSourceProps = xSourceProps->getProperties();
75 3813 : const Property* pSourceProps = aSourceProps.getConstArray();
76 7626 : Property aDestProp;
77 153942 : for (sal_Int32 i=0; i<aSourceProps.getLength(); ++i, ++pSourceProps)
78 : {
79 150129 : if ( xDestProps->hasPropertyByName(pSourceProps->Name) )
80 : {
81 : try
82 : {
83 107570 : aDestProp = xDestProps->getPropertyByName(pSourceProps->Name);
84 107570 : if (0 == (aDestProp.Attributes & PropertyAttribute::READONLY) )
85 : {
86 89338 : const Any aSourceValue = _rxSource->getPropertyValue(pSourceProps->Name);
87 89338 : if ( 0 != (aDestProp.Attributes & PropertyAttribute::MAYBEVOID) || aSourceValue.hasValue() )
88 89338 : _rxDest->setPropertyValue(pSourceProps->Name, aSourceValue);
89 : }
90 : }
91 0 : catch (Exception&)
92 : {
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 '" );
98 :
99 : Reference< XServiceInfo > xSI( _rxDest, UNO_QUERY );
100 : if ( xSI.is() )
101 : {
102 : aBuffer.append( OUStringToOString( xSI->getImplementationName(), osl_getThreadTextEncoding() ) );
103 : }
104 : else
105 : {
106 : aBuffer.append( typeid( *_rxDest.get() ).name() );
107 : }
108 : aBuffer.append( "' implementation).\n" );
109 :
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( "'" );
115 :
116 : Exception aBaseException;
117 : if ( ( aException >>= aBaseException ) && !aBaseException.Message.isEmpty() )
118 : {
119 : aBuffer.append( ", saying '" );
120 : aBuffer.append( OString( aBaseException.Message.getStr(), aBaseException.Message.getLength(), osl_getThreadTextEncoding() ) );
121 : aBuffer.append( "'" );
122 : }
123 : aBuffer.append( "." );
124 :
125 : OSL_FAIL( aBuffer.getStr() );
126 : #endif
127 : }
128 : }
129 3813 : }
130 : }
131 :
132 :
133 21899 : bool hasProperty(const OUString& _rName, const Reference<XPropertySet>& _rxSet)
134 : {
135 21899 : if (_rxSet.is())
136 : {
137 : // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
138 21899 : return _rxSet->getPropertySetInfo()->hasPropertyByName(_rName);
139 : }
140 0 : return false;
141 : }
142 :
143 :
144 3338 : void RemoveProperty(Sequence<Property>& _rProps, const OUString& _rPropName)
145 : {
146 3338 : sal_Int32 nLen = _rProps.getLength();
147 :
148 : // binaere Suche
149 3338 : const Property* pProperties = _rProps.getConstArray();
150 3338 : Property aNameProp(_rPropName, 0, Type(), 0);
151 3338 : const Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen, aNameProp, PropertyCompareByName());
152 :
153 : // gefunden ?
154 3338 : if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == _rPropName) )
155 : {
156 : OSL_ENSURE(pResult->Name.equals(_rPropName), "::RemoveProperty Properties nicht sortiert");
157 3306 : removeElementAt(_rProps, pResult - pProperties);
158 3338 : }
159 3338 : }
160 :
161 :
162 60 : void ModifyPropertyAttributes(Sequence<Property>& seqProps, const OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
163 : {
164 60 : sal_Int32 nLen = seqProps.getLength();
165 :
166 : // binaere Suche
167 60 : Property* pProperties = seqProps.getArray();
168 60 : Property aNameProp(sPropName, 0, Type(), 0);
169 60 : Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen, aNameProp, PropertyCompareByName());
170 :
171 : // gefunden ?
172 60 : if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == sPropName) )
173 : {
174 60 : pResult->Attributes |= nAddAttrib;
175 60 : pResult->Attributes &= ~nRemoveAttrib;
176 60 : }
177 60 : }
178 :
179 :
180 1045 : bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType)
181 : {
182 1045 : bool bModified(false);
183 1045 : if (_rCurrentValue.getValue() != _rValueToSet.getValue())
184 : {
185 1045 : if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) )
186 : {
187 62 : _rConvertedValue = Any( NULL, _rExpectedType.getTypeLibType() );
188 :
189 62 : if ( !uno_type_assignData(
190 124 : const_cast< void* >( _rConvertedValue.getValue() ), _rConvertedValue.getValueType().getTypeLibType(),
191 124 : 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)
196 186 : )
197 : )
198 0 : throw css::lang::IllegalArgumentException();
199 : }
200 : else
201 983 : _rConvertedValue = _rValueToSet;
202 :
203 1045 : if ( _rCurrentValue != _rConvertedValue )
204 : {
205 815 : _rOldValue = _rCurrentValue;
206 815 : bModified = true;
207 : }
208 : }
209 1045 : return bModified;
210 : }
211 :
212 :
213 3315 : }
214 :
215 :
216 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|