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 3916 : void copyProperties(const Reference<XPropertySet>& _rxSource,
63 : const Reference<XPropertySet>& _rxDest)
64 : {
65 3916 : if (!_rxSource.is() || !_rxDest.is())
66 : {
67 : OSL_FAIL("copyProperties: invalid arguments !");
68 3916 : return;
69 : }
70 :
71 3916 : Reference< XPropertySetInfo > xSourceProps = _rxSource->getPropertySetInfo();
72 7832 : Reference< XPropertySetInfo > xDestProps = _rxDest->getPropertySetInfo();
73 :
74 7832 : Sequence< Property > aSourceProps = xSourceProps->getProperties();
75 3916 : const Property* pSourceProps = aSourceProps.getConstArray();
76 7832 : Property aDestProp;
77 247950 : for (sal_Int32 i=0; i<aSourceProps.getLength(); ++i, ++pSourceProps)
78 : {
79 244034 : if ( xDestProps->hasPropertyByName(pSourceProps->Name) )
80 : {
81 : try
82 : {
83 149002 : aDestProp = xDestProps->getPropertyByName(pSourceProps->Name);
84 149002 : if (0 == (aDestProp.Attributes & PropertyAttribute::READONLY) )
85 : {
86 140458 : const Any aSourceValue = _rxSource->getPropertyValue(pSourceProps->Name);
87 140458 : if ( 0 != (aDestProp.Attributes & PropertyAttribute::MAYBEVOID) || aSourceValue.hasValue() )
88 140458 : _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 3916 : }
130 : }
131 :
132 :
133 11467 : bool hasProperty(const OUString& _rName, const Reference<XPropertySet>& _rxSet)
134 : {
135 11467 : if (_rxSet.is())
136 : {
137 : // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
138 11467 : return _rxSet->getPropertySetInfo()->hasPropertyByName(_rName);
139 : }
140 0 : return false;
141 : }
142 :
143 :
144 1798 : void RemoveProperty(Sequence<Property>& _rProps, const OUString& _rPropName)
145 : {
146 1798 : sal_Int32 nLen = _rProps.getLength();
147 :
148 : // binaere Suche
149 1798 : const Property* pProperties = _rProps.getConstArray();
150 1798 : Property aNameProp(_rPropName, 0, Type(), 0);
151 1798 : const Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen, aNameProp, PropertyCompareByName());
152 :
153 : // gefunden ?
154 1798 : if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == _rPropName) )
155 : {
156 : OSL_ENSURE(pResult->Name.equals(_rPropName), "::RemoveProperty Properties nicht sortiert");
157 1789 : removeElementAt(_rProps, pResult - pProperties);
158 1798 : }
159 1798 : }
160 :
161 :
162 32 : void ModifyPropertyAttributes(Sequence<Property>& seqProps, const OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
163 : {
164 32 : sal_Int32 nLen = seqProps.getLength();
165 :
166 : // binaere Suche
167 32 : Property* pProperties = seqProps.getArray();
168 32 : Property aNameProp(sPropName, 0, Type(), 0);
169 32 : Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen, aNameProp, PropertyCompareByName());
170 :
171 : // gefunden ?
172 32 : if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == sPropName) )
173 : {
174 32 : pResult->Attributes |= nAddAttrib;
175 32 : pResult->Attributes &= ~nRemoveAttrib;
176 32 : }
177 32 : }
178 :
179 :
180 543 : bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType)
181 : {
182 543 : bool bModified(false);
183 543 : if (_rCurrentValue.getValue() != _rValueToSet.getValue())
184 : {
185 543 : if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) )
186 : {
187 31 : _rConvertedValue = Any( NULL, _rExpectedType.getTypeLibType() );
188 :
189 31 : if ( !uno_type_assignData(
190 62 : const_cast< void* >( _rConvertedValue.getValue() ), _rConvertedValue.getValueType().getTypeLibType(),
191 62 : 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 93 : )
197 : )
198 0 : throw css::lang::IllegalArgumentException();
199 : }
200 : else
201 512 : _rConvertedValue = _rValueToSet;
202 :
203 543 : if ( _rCurrentValue != _rConvertedValue )
204 : {
205 421 : _rOldValue = _rCurrentValue;
206 421 : bModified = true;
207 : }
208 : }
209 543 : return bModified;
210 : }
211 :
212 :
213 2853 : }
214 :
215 :
216 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|