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 : #include <OLEHandler.hxx>
20 : #include <PropertyMap.hxx>
21 : #include "GraphicHelpers.hxx"
22 :
23 : #include <doctok/resourceids.hxx>
24 : #include <ooxml/resourceids.hxx>
25 : #include <rtl/ustring.hxx>
26 : #include <com/sun/star/beans/PropertyValue.hpp>
27 : #include <com/sun/star/container/XNameAccess.hpp>
28 : #include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
29 : #include <com/sun/star/document/XStorageBasedDocument.hpp>
30 : #include <com/sun/star/drawing/XShape.hpp>
31 : #include <com/sun/star/embed/XEmbeddedObject.hpp>
32 : #include <com/sun/star/embed/XEmbedObjectCreator.hpp>
33 : #include <com/sun/star/graphic/XGraphic.hpp>
34 : #include <com/sun/star/io/XStream.hpp>
35 : #include <com/sun/star/lang/XComponent.hpp>
36 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 : #include <com/sun/star/text/XTextDocument.hpp>
38 : #include <com/sun/star/uno/XComponentContext.hpp>
39 :
40 : #include "dmapperLoggers.hxx"
41 :
42 : namespace writerfilter {
43 : namespace dmapper {
44 :
45 : using namespace ::com::sun::star;
46 :
47 :
48 47 : OLEHandler::OLEHandler() :
49 : LoggedProperties(dmapper_logger, "OLEHandler"),
50 : m_nDxaOrig(0),
51 : m_nDyaOrig(0),
52 47 : m_nWrapMode(1)
53 : {
54 47 : }
55 :
56 :
57 94 : OLEHandler::~OLEHandler()
58 : {
59 94 : }
60 :
61 :
62 89 : void OLEHandler::lcl_attribute(Id rName, Value & rVal)
63 : {
64 89 : OUString sStringValue = rVal.getString();
65 : (void)rName;
66 89 : switch( rName )
67 : {
68 : case NS_ooxml::LN_CT_OLEObject_Type:
69 5 : m_sObjectType = sStringValue;
70 5 : break;
71 : case NS_ooxml::LN_CT_OLEObject_ProgID:
72 5 : m_sProgId = sStringValue;
73 5 : break;
74 : case NS_ooxml::LN_CT_OLEObject_ShapeID:
75 5 : m_sShapeId = sStringValue;
76 5 : break;
77 : case NS_ooxml::LN_CT_OLEObject_DrawAspect:
78 5 : m_sDrawAspect = sStringValue;
79 5 : break;
80 : case NS_ooxml::LN_CT_OLEObject_ObjectID:
81 5 : m_sObjectId = sStringValue;
82 5 : break;
83 : case NS_ooxml::LN_CT_OLEObject_r_id:
84 5 : m_sr_id = sStringValue;
85 5 : break;
86 : case NS_ooxml::LN_inputstream:
87 5 : rVal.getAny() >>= m_xInputStream;
88 5 : break;
89 : case NS_ooxml::LN_CT_Object_dxaOrig:
90 4 : m_nDxaOrig = rVal.getInt();
91 4 : break;
92 : case NS_ooxml::LN_CT_Object_dyaOrig:
93 4 : m_nDyaOrig = rVal.getInt();
94 4 : break;
95 : case NS_ooxml::LN_shape:
96 : {
97 46 : uno::Reference< drawing::XShape > xTempShape;
98 46 : rVal.getAny() >>= xTempShape;
99 46 : if( xTempShape.is() )
100 : {
101 46 : m_xShape.set( xTempShape );
102 46 : uno::Reference< beans::XPropertySet > xShapeProps( xTempShape, uno::UNO_QUERY );
103 46 : PropertyNameSupplier& rNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
104 :
105 : try
106 : {
107 46 : m_aShapeSize = xTempShape->getSize();
108 46 : m_aShapePosition = xTempShape->getPosition();
109 :
110 25 : xShapeProps->getPropertyValue( rNameSupplier.GetName( PROP_BITMAP ) ) >>= m_xReplacement;
111 : }
112 21 : catch( const uno::Exception& e )
113 : {
114 : SAL_WARN("writerfilter", "Exception in OLE Handler: " << e.Message);
115 46 : }
116 : // No need to set the wrapping here as it's either set in oox or will be set later
117 46 : }
118 : }
119 46 : break;
120 : default:
121 : OSL_FAIL( "unknown attribute");
122 89 : }
123 89 : }
124 :
125 :
126 18 : void OLEHandler::lcl_sprm(Sprm & rSprm)
127 : {
128 18 : sal_uInt32 nSprmId = rSprm.getId();
129 18 : switch( nSprmId )
130 : {
131 : case NS_ooxml::LN_OLEObject_OLEObject:
132 : {
133 5 : writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
134 5 : if( pProperties.get())
135 : {
136 5 : pProperties->resolve(*this);
137 5 : }
138 : }
139 5 : break;
140 : case NS_ooxml::LN_wrap_wrap:
141 : {
142 13 : writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
143 13 : if ( pProperties.get( ) )
144 : {
145 13 : WrapHandlerPtr pHandler( new WrapHandler );
146 13 : pProperties->resolve( *pHandler );
147 :
148 13 : m_nWrapMode = pHandler->getWrapMode( );
149 :
150 : try
151 : {
152 13 : uno::Reference< beans::XPropertySet > xShapeProps( m_xShape, uno::UNO_QUERY_THROW );
153 13 : PropertyNameSupplier& rNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
154 :
155 13 : xShapeProps->setPropertyValue(
156 13 : rNameSupplier.GetName( PROP_SURROUND ),
157 26 : uno::makeAny( m_nWrapMode ) );
158 : }
159 0 : catch( const uno::Exception& e )
160 : {
161 : SAL_WARN("writerfilter", "Exception in OLE Handler: " << e.Message);
162 13 : }
163 13 : }
164 : }
165 13 : break;
166 : default:
167 : {
168 : OSL_FAIL( "unknown attribute");
169 : }
170 : }
171 18 : }
172 :
173 :
174 5 : OUString OLEHandler::copyOLEOStream( uno::Reference< text::XTextDocument > xTextDocument )
175 : {
176 5 : OUString sRet;
177 5 : if( !m_xInputStream.is( ) )
178 0 : return sRet;
179 : try
180 : {
181 5 : uno::Reference < lang::XMultiServiceFactory > xFactory(xTextDocument, uno::UNO_QUERY_THROW);
182 : uno::Reference< document::XEmbeddedObjectResolver > xEmbeddedResolver(
183 10 : xFactory->createInstance("com.sun.star.document.ImportEmbeddedObjectResolver"), uno::UNO_QUERY_THROW );
184 : //hack to work with the ImportEmbeddedObjectResolver
185 : static sal_Int32 nObjectCount = 100;
186 10 : uno::Reference< container::XNameAccess > xNA( xEmbeddedResolver, uno::UNO_QUERY_THROW );
187 10 : OUString aURL("Obj");
188 5 : aURL += OUString::valueOf( nObjectCount++ );
189 10 : uno::Reference < io::XOutputStream > xOLEStream;
190 5 : if( (xNA->getByName( aURL ) >>= xOLEStream) && xOLEStream.is() )
191 : {
192 5 : const sal_Int32 nReadRequest = 0x1000;
193 5 : uno::Sequence< sal_Int8 > aData;
194 :
195 : while( true )
196 : {
197 13 : sal_Int32 nRead = m_xInputStream->readBytes( aData, nReadRequest );
198 13 : xOLEStream->writeBytes( aData );
199 13 : if( nRead < nReadRequest )
200 : {
201 5 : xOLEStream->closeOutput();
202 5 : break;
203 : }
204 : }
205 :
206 5 : static const OUString sProtocol("vnd.sun.star.EmbeddedObject:");
207 10 : OUString aPersistName( xEmbeddedResolver->resolveEmbeddedObjectURL( aURL ) );
208 10 : sRet = aPersistName.copy( sProtocol.getLength() );
209 :
210 : }
211 10 : uno::Reference< lang::XComponent > xComp( xEmbeddedResolver, uno::UNO_QUERY_THROW );
212 10 : xComp->dispose();
213 : }
214 0 : catch( const uno::Exception& )
215 : {
216 : OSL_FAIL("exception in OLEHandler::createOLEObject");
217 : }
218 5 : return sRet;
219 : }
220 :
221 : } //namespace dmapper
222 24 : } //namespace writerfilter
223 :
224 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|