xref: /aoo41x/main/forms/source/xforms/unohelper.cxx (revision 24acc546)
1*24acc546SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*24acc546SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*24acc546SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*24acc546SAndrew Rist  * distributed with this work for additional information
6*24acc546SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*24acc546SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*24acc546SAndrew Rist  * "License"); you may not use this file except in compliance
9*24acc546SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*24acc546SAndrew Rist  *
11*24acc546SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*24acc546SAndrew Rist  *
13*24acc546SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*24acc546SAndrew Rist  * software distributed under the License is distributed on an
15*24acc546SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*24acc546SAndrew Rist  * KIND, either express or implied.  See the License for the
17*24acc546SAndrew Rist  * specific language governing permissions and limitations
18*24acc546SAndrew Rist  * under the License.
19*24acc546SAndrew Rist  *
20*24acc546SAndrew Rist  *************************************************************/
21*24acc546SAndrew Rist 
22*24acc546SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_forms.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "unohelper.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <osl/diagnose.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
32cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
33cdf0e10cSrcweir #include <com/sun/star/uno/XInterface.hpp>
34cdf0e10cSrcweir #include <com/sun/star/uno/Exception.hpp>
35cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36cdf0e10cSrcweir #include <com/sun/star/beans/Property.hpp>
37cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
38cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySetInfo.hpp>
39cdf0e10cSrcweir #include <com/sun/star/beans/PropertyAttribute.hpp>
40cdf0e10cSrcweir #include <unotools/processfactory.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 
43cdf0e10cSrcweir using com::sun::star::uno::Reference;
44cdf0e10cSrcweir using com::sun::star::uno::Sequence;
45cdf0e10cSrcweir using com::sun::star::uno::Exception;
46cdf0e10cSrcweir using com::sun::star::uno::XInterface;
47cdf0e10cSrcweir using com::sun::star::lang::XMultiServiceFactory;
48cdf0e10cSrcweir using com::sun::star::beans::Property;
49cdf0e10cSrcweir using com::sun::star::beans::XPropertySet;
50cdf0e10cSrcweir using com::sun::star::beans::XPropertySetInfo;
51cdf0e10cSrcweir using com::sun::star::beans::PropertyAttribute::READONLY;
52cdf0e10cSrcweir using rtl::OUString;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
createInstance(const OUString & sServiceName)55cdf0e10cSrcweir Reference<XInterface> xforms::createInstance( const OUString& sServiceName )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     Reference<XMultiServiceFactory> xFactory = utl::getProcessServiceFactory();
58cdf0e10cSrcweir     OSL_ENSURE( xFactory.is(), "can't get service factory" );
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     Reference<XInterface> xInstance = xFactory->createInstance( sServiceName );
61cdf0e10cSrcweir     OSL_ENSURE( xInstance.is(), "failed to create instance" );
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     return xInstance;
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
copy(const Reference<XPropertySet> & xFrom,Reference<XPropertySet> & xTo)66cdf0e10cSrcweir void xforms::copy( const Reference<XPropertySet>& xFrom,
67cdf0e10cSrcweir                    Reference<XPropertySet>& xTo )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     OSL_ENSURE( xFrom.is(), "no source" );
70cdf0e10cSrcweir     OSL_ENSURE( xTo.is(), "no target" );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     // get property names & infos, and iterate over target properties
73cdf0e10cSrcweir     Sequence<Property> aProperties =
74cdf0e10cSrcweir         xTo->getPropertySetInfo()->getProperties();
75cdf0e10cSrcweir     sal_Int32 nProperties = aProperties.getLength();
76cdf0e10cSrcweir     const Property* pProperties = aProperties.getConstArray();
77cdf0e10cSrcweir     Reference<XPropertySetInfo> xFromInfo = xFrom->getPropertySetInfo();
78cdf0e10cSrcweir     for( sal_Int32 n = 0; n < nProperties; n++ )
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         const OUString& rName = pProperties[n].Name;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         // if both set have the property, copy the value
83cdf0e10cSrcweir         // (catch and ignore exceptions, if any)
84cdf0e10cSrcweir         if( xFromInfo->hasPropertyByName( rName ) )
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             try
87cdf0e10cSrcweir             {
88cdf0e10cSrcweir                 Property aProperty = xFromInfo->getPropertyByName( rName );
89cdf0e10cSrcweir                 if ( ( aProperty.Attributes & READONLY ) == 0 )
90cdf0e10cSrcweir                     xTo->setPropertyValue(rName, xFrom->getPropertyValue( rName ));
91cdf0e10cSrcweir             }
92cdf0e10cSrcweir             catch( const Exception& )
93cdf0e10cSrcweir             {
94cdf0e10cSrcweir                 // ignore any errors; we'll copy as good as we can
95cdf0e10cSrcweir             }
96cdf0e10cSrcweir         }
97cdf0e10cSrcweir         // else: no property? then ignore.
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir }
100