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