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 :
21 : #include "unohelper.hxx"
22 :
23 : #include <osl/diagnose.h>
24 :
25 : #include <com/sun/star/uno/Reference.hxx>
26 : #include <com/sun/star/uno/Sequence.hxx>
27 : #include <com/sun/star/uno/XInterface.hpp>
28 : #include <com/sun/star/uno/Exception.hpp>
29 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 : #include <com/sun/star/beans/Property.hpp>
31 : #include <com/sun/star/beans/XPropertySet.hpp>
32 : #include <com/sun/star/beans/XPropertySetInfo.hpp>
33 : #include <com/sun/star/beans/PropertyAttribute.hpp>
34 : #include <comphelper/processfactory.hxx>
35 :
36 :
37 : using com::sun::star::uno::Reference;
38 : using com::sun::star::uno::Sequence;
39 : using com::sun::star::uno::Exception;
40 : using com::sun::star::uno::XInterface;
41 : using com::sun::star::lang::XMultiServiceFactory;
42 : using com::sun::star::beans::Property;
43 : using com::sun::star::beans::XPropertySet;
44 : using com::sun::star::beans::XPropertySetInfo;
45 : using com::sun::star::beans::PropertyAttribute::READONLY;
46 : using rtl::OUString;
47 :
48 :
49 0 : Reference<XInterface> xforms::createInstance( const OUString& sServiceName )
50 : {
51 0 : Reference<XMultiServiceFactory> xFactory = comphelper::getProcessServiceFactory();
52 : OSL_ENSURE( xFactory.is(), "can't get service factory" );
53 :
54 0 : Reference<XInterface> xInstance = xFactory->createInstance( sServiceName );
55 : OSL_ENSURE( xInstance.is(), "failed to create instance" );
56 :
57 0 : return xInstance;
58 : }
59 :
60 0 : void xforms::copy( const Reference<XPropertySet>& xFrom,
61 : Reference<XPropertySet>& xTo )
62 : {
63 : OSL_ENSURE( xFrom.is(), "no source" );
64 : OSL_ENSURE( xTo.is(), "no target" );
65 :
66 : // get property names & infos, and iterate over target properties
67 : Sequence<Property> aProperties =
68 0 : xTo->getPropertySetInfo()->getProperties();
69 0 : sal_Int32 nProperties = aProperties.getLength();
70 0 : const Property* pProperties = aProperties.getConstArray();
71 0 : Reference<XPropertySetInfo> xFromInfo = xFrom->getPropertySetInfo();
72 0 : for( sal_Int32 n = 0; n < nProperties; n++ )
73 : {
74 0 : const OUString& rName = pProperties[n].Name;
75 :
76 : // if both set have the property, copy the value
77 : // (catch and ignore exceptions, if any)
78 0 : if( xFromInfo->hasPropertyByName( rName ) )
79 : {
80 : try
81 : {
82 0 : Property aProperty = xFromInfo->getPropertyByName( rName );
83 0 : if ( ( aProperty.Attributes & READONLY ) == 0 )
84 0 : xTo->setPropertyValue(rName, xFrom->getPropertyValue( rName ));
85 : }
86 0 : catch( const Exception& )
87 : {
88 : // ignore any errors; we'll copy as good as we can
89 : }
90 : }
91 : // else: no property? then ignore.
92 0 : }
93 0 : }
94 :
95 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|