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