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 "oox/ole/oleobjecthelper.hxx"
21 :
22 : #include <com/sun/star/awt/Rectangle.hpp>
23 : #include <com/sun/star/awt/Size.hpp>
24 : #include <com/sun/star/container/XNameAccess.hpp>
25 : #include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
26 : #include <com/sun/star/embed/Aspects.hpp>
27 : #include <com/sun/star/io/XOutputStream.hpp>
28 : #include <com/sun/star/lang/XComponent.hpp>
29 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 : #include "oox/helper/propertymap.hxx"
31 :
32 : namespace oox {
33 : namespace ole {
34 :
35 : using namespace ::com::sun::star;
36 : using namespace ::com::sun::star::container;
37 : using namespace ::com::sun::star::embed;
38 : using namespace ::com::sun::star::io;
39 : using namespace ::com::sun::star::lang;
40 : using namespace ::com::sun::star::uno;
41 :
42 116270 : OleObjectInfo::OleObjectInfo() :
43 : mbLinked( false ),
44 : mbShowAsIcon( false ),
45 116270 : mbAutoUpdate( false )
46 : {
47 116270 : }
48 :
49 2 : OleObjectHelper::OleObjectHelper( const Reference< XMultiServiceFactory >& rxModelFactory ) :
50 : maEmbeddedObjScheme( "vnd.sun.star.EmbeddedObject:" ),
51 2 : mnObjectId( 100 )
52 : {
53 2 : if( rxModelFactory.is() ) try
54 : {
55 2 : mxResolver.set( rxModelFactory->createInstance( "com.sun.star.document.ImportEmbeddedObjectResolver" ), UNO_QUERY );
56 : }
57 0 : catch(const Exception& )
58 : {
59 : }
60 2 : }
61 :
62 4 : OleObjectHelper::~OleObjectHelper()
63 : {
64 : try
65 : {
66 2 : Reference< XComponent > xResolverComp( mxResolver, UNO_QUERY_THROW );
67 2 : xResolverComp->dispose();
68 : }
69 0 : catch(const Exception& )
70 : {
71 : }
72 2 : }
73 :
74 4 : bool OleObjectHelper::importOleObject( PropertyMap& rPropMap, const OleObjectInfo& rOleObject, const awt::Size& rObjSize )
75 : {
76 4 : bool bRet = false;
77 :
78 4 : if( rOleObject.mbLinked )
79 : {
80 : // linked OLE object - set target URL
81 0 : if( !rOleObject.maTargetLink.isEmpty() )
82 : {
83 0 : rPropMap.setProperty( PROP_LinkURL, rOleObject.maTargetLink);
84 0 : bRet = true;
85 : }
86 : }
87 : else
88 : {
89 : // embedded OLE object - import the embedded data
90 4 : if( rOleObject.maEmbeddedData.hasElements() && mxResolver.is() ) try
91 : {
92 4 : OUString aObjectId = "Obj" + OUString::number( mnObjectId++ );
93 :
94 8 : Reference< XNameAccess > xResolverNA( mxResolver, UNO_QUERY_THROW );
95 8 : Reference< XOutputStream > xOutStrm( xResolverNA->getByName( aObjectId ), UNO_QUERY_THROW );
96 4 : xOutStrm->writeBytes( rOleObject.maEmbeddedData );
97 4 : xOutStrm->closeOutput();
98 :
99 8 : OUString aUrl = mxResolver->resolveEmbeddedObjectURL( aObjectId );
100 : OSL_ENSURE( aUrl.match( maEmbeddedObjScheme ), "OleObjectHelper::importOleObject - unexpected URL scheme" );
101 8 : OUString aPersistName = aUrl.copy( maEmbeddedObjScheme.getLength() );
102 4 : if( !aPersistName.isEmpty() )
103 : {
104 4 : rPropMap.setProperty( PROP_PersistName, aPersistName);
105 4 : bRet = true;
106 4 : }
107 : }
108 0 : catch(const Exception& )
109 : {
110 : }
111 : }
112 :
113 4 : if( bRet )
114 : {
115 4 : rPropMap.setProperty( PROP_Aspect, (rOleObject.mbShowAsIcon ? Aspects::MSOLE_ICON : Aspects::MSOLE_CONTENT));
116 4 : rPropMap.setProperty( PROP_VisualArea, awt::Rectangle( 0, 0, rObjSize.Width, rObjSize.Height ));
117 : }
118 4 : return bRet;
119 : }
120 :
121 : } // namespace ole
122 : } // namespace oox
123 :
124 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|