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