1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*ca5ec200SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*ca5ec200SAndrew Rist * distributed with this work for additional information
6*ca5ec200SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*ca5ec200SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist * with the License. You may obtain a copy of the License at
10*ca5ec200SAndrew Rist *
11*ca5ec200SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist *
13*ca5ec200SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist * KIND, either express or implied. See the License for the
17*ca5ec200SAndrew Rist * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist * under the License.
19*ca5ec200SAndrew Rist *
20*ca5ec200SAndrew Rist *************************************************************/
21*ca5ec200SAndrew Rist
22*ca5ec200SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "oox/ole/oleobjecthelper.hxx"
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include <com/sun/star/awt/Rectangle.hpp>
27cdf0e10cSrcweir #include <com/sun/star/awt/Size.hpp>
28cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
29cdf0e10cSrcweir #include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
30cdf0e10cSrcweir #include <com/sun/star/embed/Aspects.hpp>
31cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
32cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34cdf0e10cSrcweir #include "oox/helper/propertymap.hxx"
35cdf0e10cSrcweir
36cdf0e10cSrcweir namespace oox {
37cdf0e10cSrcweir namespace ole {
38cdf0e10cSrcweir
39cdf0e10cSrcweir // ============================================================================
40cdf0e10cSrcweir
41cdf0e10cSrcweir using namespace ::com::sun::star::awt;
42cdf0e10cSrcweir using namespace ::com::sun::star::container;
43cdf0e10cSrcweir using namespace ::com::sun::star::embed;
44cdf0e10cSrcweir using namespace ::com::sun::star::io;
45cdf0e10cSrcweir using namespace ::com::sun::star::lang;
46cdf0e10cSrcweir using namespace ::com::sun::star::uno;
47cdf0e10cSrcweir
48cdf0e10cSrcweir using ::rtl::OUString;
49cdf0e10cSrcweir
50cdf0e10cSrcweir // ============================================================================
51cdf0e10cSrcweir
OleObjectInfo()52cdf0e10cSrcweir OleObjectInfo::OleObjectInfo() :
53cdf0e10cSrcweir mbLinked( false ),
54cdf0e10cSrcweir mbShowAsIcon( false ),
55cdf0e10cSrcweir mbAutoUpdate( false )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir }
58cdf0e10cSrcweir
59cdf0e10cSrcweir // ============================================================================
60cdf0e10cSrcweir
OleObjectHelper(const Reference<XMultiServiceFactory> & rxModelFactory)61cdf0e10cSrcweir OleObjectHelper::OleObjectHelper( const Reference< XMultiServiceFactory >& rxModelFactory ) :
62cdf0e10cSrcweir maEmbeddedObjScheme( CREATE_OUSTRING( "vnd.sun.star.EmbeddedObject:" ) ),
63cdf0e10cSrcweir mnObjectId( 100 )
64cdf0e10cSrcweir {
65cdf0e10cSrcweir if( rxModelFactory.is() ) try
66cdf0e10cSrcweir {
67cdf0e10cSrcweir mxResolver.set( rxModelFactory->createInstance( CREATE_OUSTRING( "com.sun.star.document.ImportEmbeddedObjectResolver" ) ), UNO_QUERY );
68cdf0e10cSrcweir }
69cdf0e10cSrcweir catch( Exception& )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir }
72cdf0e10cSrcweir }
73cdf0e10cSrcweir
~OleObjectHelper()74cdf0e10cSrcweir OleObjectHelper::~OleObjectHelper()
75cdf0e10cSrcweir {
76cdf0e10cSrcweir try
77cdf0e10cSrcweir {
78cdf0e10cSrcweir Reference< XComponent > xResolverComp( mxResolver, UNO_QUERY_THROW );
79cdf0e10cSrcweir xResolverComp->dispose();
80cdf0e10cSrcweir }
81cdf0e10cSrcweir catch( Exception& )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir }
84cdf0e10cSrcweir }
85cdf0e10cSrcweir
importOleObject(PropertyMap & rPropMap,const OleObjectInfo & rOleObject,const Size & rObjSize)86cdf0e10cSrcweir bool OleObjectHelper::importOleObject( PropertyMap& rPropMap, const OleObjectInfo& rOleObject, const Size& rObjSize )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir bool bRet = false;
89cdf0e10cSrcweir
90cdf0e10cSrcweir if( rOleObject.mbLinked )
91cdf0e10cSrcweir {
92cdf0e10cSrcweir // linked OLE object - set target URL
93cdf0e10cSrcweir if( rOleObject.maTargetLink.getLength() > 0 )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir rPropMap[ PROP_LinkURL ] <<= rOleObject.maTargetLink;
96cdf0e10cSrcweir bRet = true;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir }
99cdf0e10cSrcweir else
100cdf0e10cSrcweir {
101cdf0e10cSrcweir // embedded OLE object - import the embedded data
102cdf0e10cSrcweir if( rOleObject.maEmbeddedData.hasElements() && mxResolver.is() ) try
103cdf0e10cSrcweir {
104cdf0e10cSrcweir OUString aObjectId = CREATE_OUSTRING( "Obj" ) + OUString::valueOf( mnObjectId++ );
105cdf0e10cSrcweir
106cdf0e10cSrcweir Reference< XNameAccess > xResolverNA( mxResolver, UNO_QUERY_THROW );
107cdf0e10cSrcweir Reference< XOutputStream > xOutStrm( xResolverNA->getByName( aObjectId ), UNO_QUERY_THROW );
108cdf0e10cSrcweir xOutStrm->writeBytes( rOleObject.maEmbeddedData );
109cdf0e10cSrcweir xOutStrm->closeOutput();
110cdf0e10cSrcweir
111cdf0e10cSrcweir OUString aUrl = mxResolver->resolveEmbeddedObjectURL( aObjectId );
112cdf0e10cSrcweir OSL_ENSURE( aUrl.match( maEmbeddedObjScheme ), "OleObjectHelper::importOleObject - unexpected URL scheme" );
113cdf0e10cSrcweir OUString aPersistName = aUrl.copy( maEmbeddedObjScheme.getLength() );
114cdf0e10cSrcweir if( aPersistName.getLength() > 0 )
115cdf0e10cSrcweir {
116cdf0e10cSrcweir rPropMap[ PROP_PersistName ] <<= aPersistName;
117cdf0e10cSrcweir bRet = true;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir }
120cdf0e10cSrcweir catch( Exception& )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir }
123cdf0e10cSrcweir }
124cdf0e10cSrcweir
125cdf0e10cSrcweir if( bRet )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir rPropMap[ PROP_Aspect ] <<= (rOleObject.mbShowAsIcon ? Aspects::MSOLE_ICON : Aspects::MSOLE_CONTENT);
128cdf0e10cSrcweir rPropMap[ PROP_VisualArea ] <<= Rectangle( 0, 0, rObjSize.Width, rObjSize.Height );
129cdf0e10cSrcweir }
130cdf0e10cSrcweir return bRet;
131cdf0e10cSrcweir }
132cdf0e10cSrcweir
133cdf0e10cSrcweir // ============================================================================
134cdf0e10cSrcweir
135cdf0e10cSrcweir } // namespace ole
136cdf0e10cSrcweir } // namespace oox
137