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