1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "OLEHandler.hxx"
20 #include "DomainMapper.hxx"
21 #include "PropertyMap.hxx"
22 #include "GraphicHelpers.hxx"
24 #include <editeng/unoprnms.hxx>
25 #include <oox/ole/oleobjecthelper.hxx>
26 #include <ooxml/resourceids.hxx>
27 #include <rtl/ustring.hxx>
28 #include <sal/log.hxx>
29 #include <osl/diagnose.h>
30 #include <unotools/mediadescriptor.hxx>
31 #include <officecfg/Office/Common.hxx>
32 #include <com/sun/star/beans/PropertyValue.hpp>
33 #include <com/sun/star/container/XNameAccess.hpp>
34 #include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
35 #include <com/sun/star/document/XEmbeddedObjectSupplier.hpp>
36 #include <com/sun/star/document/XFilter.hpp>
37 #include <com/sun/star/document/XImporter.hpp>
38 #include <com/sun/star/document/XStorageBasedDocument.hpp>
39 #include <com/sun/star/drawing/XShape.hpp>
40 #include <com/sun/star/embed/XEmbeddedObject.hpp>
41 #include <com/sun/star/embed/XEmbedObjectCreator.hpp>
42 #include <com/sun/star/graphic/XGraphic.hpp>
43 #include <com/sun/star/io/XStream.hpp>
44 #include <com/sun/star/lang/XComponent.hpp>
45 #include <com/sun/star/lang/XInitialization.hpp>
46 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47 #include <com/sun/star/lang/XServiceInfo.hpp>
48 #include <com/sun/star/text/XTextDocument.hpp>
49 #include <com/sun/star/text/WrapTextMode.hpp>
50 #include <com/sun/star/uno/XComponentContext.hpp>
52 namespace writerfilter
{
55 using namespace ::com::sun::star
;
58 OLEHandler::OLEHandler(DomainMapper
& rDomainMapper
) :
59 LoggedProperties("OLEHandler"),
60 m_nWrapMode(text::WrapTextMode_THROUGHT
),
61 m_rDomainMapper(rDomainMapper
)
66 OLEHandler::~OLEHandler()
71 void OLEHandler::lcl_attribute(Id rName
, Value
& rVal
)
73 OUString sStringValue
= rVal
.getString();
76 case NS_ooxml::LN_CT_OLEObject_Type
:
77 m_sObjectType
= sStringValue
;
79 case NS_ooxml::LN_CT_OLEObject_ProgID
:
80 m_sProgId
= sStringValue
;
82 case NS_ooxml::LN_CT_OLEObject_ShapeID
:
83 m_sShapeId
= sStringValue
;
85 case NS_ooxml::LN_CT_OLEObject_DrawAspect
:
86 m_sDrawAspect
= sStringValue
;
88 case NS_ooxml::LN_CT_OLEObject_ObjectID
:
89 m_sObjectId
= sStringValue
;
91 case NS_ooxml::LN_CT_OLEObject_r_id
:
92 m_sr_id
= sStringValue
;
94 case NS_ooxml::LN_inputstream
:
95 rVal
.getAny() >>= m_xInputStream
;
97 case NS_ooxml::LN_CT_Object_dxaOrig
:
98 m_sVisAreaWidth
= sStringValue
;
100 case NS_ooxml::LN_CT_Object_dyaOrig
:
101 m_sVisAreaHeight
= sStringValue
;
103 case NS_ooxml::LN_shape
:
105 uno::Reference
< drawing::XShape
> xTempShape
;
106 rVal
.getAny() >>= xTempShape
;
108 // Control shape is handled on a different code path
109 uno::Reference
< lang::XServiceInfo
> xSInfo( xTempShape
, uno::UNO_QUERY_THROW
);
110 if(xSInfo
->supportsService("com.sun.star.drawing.ControlShape"))
112 m_rDomainMapper
.hasControls(true);
116 if( xTempShape
.is() )
118 m_xShape
.set( xTempShape
);
119 uno::Reference
< beans::XPropertySet
> xShapeProps( xTempShape
, uno::UNO_QUERY
);
123 // Shapes in the header or footer should be in the background, since the default is WrapTextMode_THROUGH.
124 if (m_rDomainMapper
.IsInHeaderFooter())
125 xShapeProps
->setPropertyValue("Opaque", uno::makeAny(false));
127 m_aShapeSize
= xTempShape
->getSize();
128 m_aShapePosition
= xTempShape
->getPosition();
130 xShapeProps
->getPropertyValue( getPropertyName( PROP_BITMAP
) ) >>= m_xReplacement
;
132 catch( const uno::Exception
& e
)
134 SAL_WARN("writerfilter", "Exception in OLE Handler: " << e
);
136 // No need to set the wrapping here as it's either set in oox or will be set later
141 OSL_FAIL( "unknown attribute");
146 void OLEHandler::lcl_sprm(Sprm
& rSprm
)
148 sal_uInt32 nSprmId
= rSprm
.getId();
151 case NS_ooxml::LN_OLEObject_OLEObject
:
153 writerfilter::Reference
<Properties
>::Pointer_t pProperties
= rSprm
.getProps();
154 if( pProperties
.get())
156 pProperties
->resolve(*this);
160 case NS_ooxml::LN_wrap_wrap
:
162 writerfilter::Reference
<Properties
>::Pointer_t pProperties
= rSprm
.getProps();
163 if ( pProperties
.get( ) )
165 tools::SvRef
<WrapHandler
> pHandler( new WrapHandler
);
166 pProperties
->resolve( *pHandler
);
168 m_nWrapMode
= pHandler
->getWrapMode( );
172 uno::Reference
< beans::XPropertySet
> xShapeProps( m_xShape
, uno::UNO_QUERY_THROW
);
174 xShapeProps
->setPropertyValue(
175 getPropertyName( PROP_SURROUND
),
176 uno::makeAny( static_cast<sal_Int32
>(m_nWrapMode
) ) );
178 // Through shapes in the header or footer(that spill into the body) should be in the background.
179 // It is just assumed that all shapes will spill into the body.
180 if( m_rDomainMapper
.IsInHeaderFooter() )
181 xShapeProps
->setPropertyValue("Opaque", uno::makeAny(m_nWrapMode
!= text::WrapTextMode_THROUGH
));
183 catch( const uno::Exception
& e
)
185 SAL_WARN("writerfilter", "Exception in OLE Handler: " << e
);
192 OSL_FAIL( "unknown attribute");
197 void OLEHandler::importStream(const uno::Reference
<uno::XComponentContext
>& xComponentContext
, const uno::Reference
<text::XTextDocument
>& xTextDocument
, const uno::Reference
<text::XTextContent
>& xOLE
)
199 OUString aFilterService
;
200 if (m_sProgId
== "Word.Document.12")
201 aFilterService
= "com.sun.star.comp.Writer.WriterFilter";
202 else if (m_sProgId
== "Excel.Sheet.12")
203 aFilterService
= "com.sun.star.comp.oox.xls.ExcelFilter";
204 else if (m_sProgId
== "Equation.3")
205 aFilterService
= "com.sun.star.comp.Math.MathTypeFilter";
207 SAL_WARN("writerfilter", "OLEHandler::importStream: unhandled m_sProgId: " << m_sProgId
);
209 if (!m_xInputStream
.is() || aFilterService
.isEmpty())
212 // Create the filter service.
213 uno::Reference
<uno::XInterface
> xInterface
= xComponentContext
->getServiceManager()->createInstanceWithContext(aFilterService
, xComponentContext
);
215 // Set target document.
216 uno::Reference
<document::XImporter
> xImporter(xInterface
, uno::UNO_QUERY
);
217 uno::Reference
<document::XEmbeddedObjectSupplier
> xSupplier(xOLE
, uno::UNO_QUERY
);
218 uno::Reference
<lang::XComponent
> xEmbeddedObject(xSupplier
->getEmbeddedObject(), uno::UNO_QUERY
);
219 if (!xEmbeddedObject
.is())
221 xImporter
->setTargetDocument( xEmbeddedObject
);
223 // Import the input stream.
224 utl::MediaDescriptor aMediaDescriptor
;
225 aMediaDescriptor
["InputStream"] <<= m_xInputStream
;
226 uno::Reference
<document::XFilter
> xFilter(xInterface
, uno::UNO_QUERY
);
227 xFilter
->filter(aMediaDescriptor
.getAsConstPropertyValueList());
229 // Now that the data is imported, update the (typically) changed stream name.
230 uno::Reference
<beans::XPropertySet
> xPropertySet(xOLE
, uno::UNO_QUERY
);
231 ::oox::ole::SaveInteropProperties(xTextDocument
,
232 xPropertySet
->getPropertyValue("StreamName").get
<OUString
>(), &m_aURL
,
233 m_sProgId
, m_sDrawAspect
);
236 OUString
OLEHandler::getCLSID(const uno::Reference
<uno::XComponentContext
>& xComponentContext
) const
240 // See officecfg/registry/data/org/openoffice/Office/Embedding.xcu.
241 if (m_sProgId
== "Word.Document.12")
243 if (officecfg::Office::Common::Filter::Microsoft::Import::WinWordToWriter::get(xComponentContext
))
244 aRet
= "8BC6B165-B1B2-4EDD-aa47-dae2ee689dd6";
246 else if (m_sProgId
== "Excel.Sheet.12")
248 if (officecfg::Office::Common::Filter::Microsoft::Import::ExcelToCalc::get(xComponentContext
))
249 aRet
= "47BBB4CB-CE4C-4E80-A591-42D9AE74950F";
251 else if (m_sProgId
== "Equation.3")
253 if (officecfg::Office::Common::Filter::Microsoft::Import::MathTypeToMath::get(xComponentContext
))
254 aRet
= "078B7ABA-54FC-457F-8551-6147E776A997";
257 SAL_WARN("writerfilter", "OLEHandler::getCLSID: unhandled m_sProgId: " << m_sProgId
);
262 OUString
const & OLEHandler::GetDrawAspect() const
264 return m_sDrawAspect
;
267 OUString
const & OLEHandler::GetVisAreaWidth() const
269 return m_sVisAreaWidth
;
272 OUString
const & OLEHandler::GetVisAreaHeight() const
274 return m_sVisAreaHeight
;
277 OUString
OLEHandler::copyOLEOStream(
278 uno::Reference
<text::XTextDocument
> const& xTextDocument
)
281 if( !m_xInputStream
.is( ) )
285 uno::Reference
< lang::XMultiServiceFactory
> xFactory(xTextDocument
, uno::UNO_QUERY_THROW
);
286 uno::Reference
< document::XEmbeddedObjectResolver
> xEmbeddedResolver(
287 xFactory
->createInstance("com.sun.star.document.ImportEmbeddedObjectResolver"), uno::UNO_QUERY_THROW
);
288 //hack to work with the ImportEmbeddedObjectResolver
289 static sal_Int32 nObjectCount
= 100;
290 uno::Reference
< container::XNameAccess
> xNA( xEmbeddedResolver
, uno::UNO_QUERY_THROW
);
291 OUString
aURL("Obj");
292 aURL
+= OUString::number( nObjectCount
++ );
293 uno::Reference
< io::XOutputStream
> xOLEStream
;
294 if( (xNA
->getByName( aURL
) >>= xOLEStream
) && xOLEStream
.is() )
296 const sal_Int32 nReadRequest
= 0x1000;
297 uno::Sequence
< sal_Int8
> aData
;
301 sal_Int32 nRead
= m_xInputStream
->readBytes( aData
, nReadRequest
);
302 xOLEStream
->writeBytes( aData
);
303 if( nRead
< nReadRequest
)
305 xOLEStream
->closeOutput();
310 ::oox::ole::SaveInteropProperties(xTextDocument
, aURL
, nullptr, m_sProgId
, m_sDrawAspect
);
312 OUString
aPersistName( xEmbeddedResolver
->resolveEmbeddedObjectURL( aURL
) );
313 sRet
= aPersistName
.copy( strlen("vnd.sun.star.EmbeddedObject:") );
316 uno::Reference
< lang::XComponent
> xComp( xEmbeddedResolver
, uno::UNO_QUERY_THROW
);
320 catch( const uno::Exception
& )
322 OSL_FAIL("exception in OLEHandler::createOLEObject");
327 } //namespace dmapper
328 } //namespace writerfilter
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */