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 "GraphicHelpers.hxx"
23 #include <oox/ole/oleobjecthelper.hxx>
24 #include <ooxml/resourceids.hxx>
25 #include <rtl/ustring.hxx>
26 #include <sal/log.hxx>
27 #include <osl/diagnose.h>
28 #include <comphelper/diagnose_ex.hxx>
29 #include <unotools/mediadescriptor.hxx>
30 #include <officecfg/Office/Common.hxx>
31 #include <com/sun/star/container/XNameAccess.hpp>
32 #include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
33 #include <com/sun/star/document/XEmbeddedObjectSupplier.hpp>
34 #include <com/sun/star/document/XFilter.hpp>
35 #include <com/sun/star/document/XImporter.hpp>
36 #include <com/sun/star/drawing/XShape.hpp>
37 #include <com/sun/star/graphic/XGraphic.hpp>
38 #include <com/sun/star/io/XOutputStream.hpp>
39 #include <com/sun/star/lang/XComponent.hpp>
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 #include <com/sun/star/lang/XServiceInfo.hpp>
42 #include <com/sun/star/text/XTextDocument.hpp>
43 #include <com/sun/star/text/WrapTextMode.hpp>
44 #include <com/sun/star/uno/XComponentContext.hpp>
46 namespace writerfilter::dmapper
{
48 using namespace ::com::sun::star
;
51 OLEHandler::OLEHandler(DomainMapper
& rDomainMapper
) :
52 LoggedProperties("OLEHandler"),
53 m_nWrapMode(text::WrapTextMode_THROUGHT
),
54 m_rDomainMapper(rDomainMapper
)
59 OLEHandler::~OLEHandler()
64 void OLEHandler::lcl_attribute(Id rName
, Value
& rVal
)
66 OUString sStringValue
= rVal
.getString();
69 case NS_ooxml::LN_CT_OLEObject_Type
:
71 case NS_ooxml::LN_CT_OLEObject_ProgID
:
72 m_sProgId
= sStringValue
;
74 case NS_ooxml::LN_CT_OLEObject_ShapeID
:
76 case NS_ooxml::LN_CT_OLEObject_DrawAspect
:
77 m_sDrawAspect
= sStringValue
;
79 case NS_ooxml::LN_CT_OLEObject_ObjectID
:
81 case NS_ooxml::LN_CT_OLEObject_r_id
:
83 case NS_ooxml::LN_inputstream
:
84 rVal
.getAny() >>= m_xInputStream
;
86 case NS_ooxml::LN_CT_Object_dxaOrig
:
87 m_sVisAreaWidth
= sStringValue
;
89 case NS_ooxml::LN_CT_Object_dyaOrig
:
90 m_sVisAreaHeight
= sStringValue
;
92 case NS_ooxml::LN_shape
:
94 uno::Reference
< drawing::XShape
> xTempShape
;
95 rVal
.getAny() >>= xTempShape
;
97 // Control shape is handled on a different code path
98 uno::Reference
< lang::XServiceInfo
> xSInfo( xTempShape
, uno::UNO_QUERY_THROW
);
99 if(xSInfo
->supportsService("com.sun.star.drawing.ControlShape"))
101 m_rDomainMapper
.hasControls(true);
105 if( xTempShape
.is() )
107 m_xShape
.set( xTempShape
);
109 // No need to set the wrapping here as it's either set in oox or will be set later
111 // Shapes in the header or footer should be in the background, since the default is WrapTextMode_THROUGH.
112 if (m_rDomainMapper
.IsInHeaderFooter())
116 uno::Reference
<beans::XPropertySet
> xShapeProps(m_xShape
, uno::UNO_QUERY
);
117 xShapeProps
->setPropertyValue("Opaque", uno::Any(false));
119 catch( const uno::Exception
& )
121 TOOLS_WARN_EXCEPTION("writerfilter", "Exception in OLE Handler");
128 OSL_FAIL( "unknown attribute");
132 css::awt::Size
OLEHandler::getSize() const
135 return css::awt::Size();
136 return m_xShape
->getSize();
139 css::uno::Reference
<css::graphic::XGraphic
> OLEHandler::getReplacement() const
143 uno::Reference
<beans::XPropertySet
> xShapeProps(m_xShape
, uno::UNO_QUERY
);
144 css::uno::Reference
<css::graphic::XGraphic
> xReplacement
;
145 xShapeProps
->getPropertyValue(getPropertyName(PROP_BITMAP
)) >>= xReplacement
;
149 void OLEHandler::lcl_sprm(Sprm
& rSprm
)
151 sal_uInt32 nSprmId
= rSprm
.getId();
154 case NS_ooxml::LN_OLEObject_OLEObject
:
156 writerfilter::Reference
<Properties
>::Pointer_t pProperties
= rSprm
.getProps();
159 pProperties
->resolve(*this);
163 case NS_ooxml::LN_wrap_wrap
:
165 writerfilter::Reference
<Properties
>::Pointer_t pProperties
= rSprm
.getProps();
168 tools::SvRef
<WrapHandler
> pHandler( new WrapHandler
);
169 pProperties
->resolve( *pHandler
);
171 m_nWrapMode
= pHandler
->getWrapMode( );
175 uno::Reference
< beans::XPropertySet
> xShapeProps( m_xShape
, uno::UNO_QUERY_THROW
);
177 xShapeProps
->setPropertyValue(
178 getPropertyName( PROP_SURROUND
),
179 uno::Any( static_cast<sal_Int32
>(m_nWrapMode
) ) );
181 // Through shapes in the header or footer(that spill into the body) should be in the background.
182 // It is just assumed that all shapes will spill into the body.
183 if( m_rDomainMapper
.IsInHeaderFooter() )
184 xShapeProps
->setPropertyValue("Opaque", uno::Any(m_nWrapMode
!= text::WrapTextMode_THROUGH
));
186 catch( const uno::Exception
& )
188 TOOLS_WARN_EXCEPTION("writerfilter", "Exception in OLE Handler");
195 OSL_FAIL( "unknown attribute");
200 void OLEHandler::importStream(const uno::Reference
<uno::XComponentContext
>& xComponentContext
, const uno::Reference
<text::XTextDocument
>& xTextDocument
, const uno::Reference
<text::XTextContent
>& xOLE
)
202 OUString aFilterService
;
203 if (m_sProgId
== "Word.Document.12")
204 aFilterService
= "com.sun.star.comp.Writer.WriterFilter";
205 else if (m_sProgId
== "Excel.Sheet.12")
206 aFilterService
= "com.sun.star.comp.oox.xls.ExcelFilter";
207 else if (m_sProgId
== "Equation.3")
208 aFilterService
= "com.sun.star.comp.Math.MathTypeFilter";
210 SAL_WARN("writerfilter", "OLEHandler::importStream: unhandled m_sProgId: " << m_sProgId
);
212 if (!m_xInputStream
.is() || aFilterService
.isEmpty())
215 // Create the filter service.
216 uno::Reference
<uno::XInterface
> xInterface
= xComponentContext
->getServiceManager()->createInstanceWithContext(aFilterService
, xComponentContext
);
218 // Set target document.
219 uno::Reference
<document::XImporter
> xImporter(xInterface
, uno::UNO_QUERY
);
220 uno::Reference
<document::XEmbeddedObjectSupplier
> xSupplier(xOLE
, uno::UNO_QUERY
);
221 uno::Reference
<lang::XComponent
> xEmbeddedObject
= xSupplier
->getEmbeddedObject();
222 if (!xEmbeddedObject
.is())
224 xImporter
->setTargetDocument( xEmbeddedObject
);
226 // Import the input stream.
227 utl::MediaDescriptor aMediaDescriptor
;
228 aMediaDescriptor
["InputStream"] <<= m_xInputStream
;
229 uno::Reference
<document::XFilter
> xFilter(xInterface
, uno::UNO_QUERY
);
230 xFilter
->filter(aMediaDescriptor
.getAsConstPropertyValueList());
232 // Now that the data is imported, update the (typically) changed stream name.
233 uno::Reference
<beans::XPropertySet
> xPropertySet(xOLE
, uno::UNO_QUERY
);
234 ::oox::ole::SaveInteropProperties(xTextDocument
,
235 xPropertySet
->getPropertyValue("StreamName").get
<OUString
>(), &m_aURL
,
239 OUString
OLEHandler::getCLSID() const
243 // See officecfg/registry/data/org/openoffice/Office/Embedding.xcu.
244 if (m_sProgId
== "Word.Document.12")
246 if (officecfg::Office::Common::Filter::Microsoft::Import::WinWordToWriter::get())
247 aRet
= "8BC6B165-B1B2-4EDD-aa47-dae2ee689dd6";
249 else if (m_sProgId
== "Excel.Sheet.12")
251 if (officecfg::Office::Common::Filter::Microsoft::Import::ExcelToCalc::get())
252 aRet
= "47BBB4CB-CE4C-4E80-A591-42D9AE74950F";
254 else if (m_sProgId
== "Equation.3")
256 if (officecfg::Office::Common::Filter::Microsoft::Import::MathTypeToMath::get())
257 aRet
= "078B7ABA-54FC-457F-8551-6147E776A997";
260 SAL_WARN("writerfilter", "OLEHandler::getCLSID: unhandled m_sProgId: " << m_sProgId
);
265 OUString
const & OLEHandler::GetDrawAspect() const
267 return m_sDrawAspect
;
270 OUString
const & OLEHandler::GetVisAreaWidth() const
272 return m_sVisAreaWidth
;
275 OUString
const & OLEHandler::GetVisAreaHeight() const
277 return m_sVisAreaHeight
;
280 OUString
OLEHandler::copyOLEOStream(
281 uno::Reference
<text::XTextDocument
> const& xTextDocument
)
284 if( !m_xInputStream
.is( ) )
288 uno::Reference
< lang::XMultiServiceFactory
> xFactory(xTextDocument
, uno::UNO_QUERY_THROW
);
289 uno::Reference
< document::XEmbeddedObjectResolver
> xEmbeddedResolver(
290 xFactory
->createInstance("com.sun.star.document.ImportEmbeddedObjectResolver"), uno::UNO_QUERY_THROW
);
291 //hack to work with the ImportEmbeddedObjectResolver
292 static sal_Int32 nObjectCount
= 100;
293 uno::Reference
< container::XNameAccess
> xNA( xEmbeddedResolver
, uno::UNO_QUERY_THROW
);
294 OUString aURL
= "Obj" + OUString::number( nObjectCount
++ );
295 uno::Reference
< io::XOutputStream
> xOLEStream
;
296 if( (xNA
->getByName( aURL
) >>= xOLEStream
) && xOLEStream
.is() )
298 const sal_Int32 nReadRequest
= 0x1000;
299 uno::Sequence
< sal_Int8
> aData
;
303 sal_Int32 nRead
= m_xInputStream
->readBytes( aData
, nReadRequest
);
304 xOLEStream
->writeBytes( aData
);
305 if( nRead
< nReadRequest
)
307 xOLEStream
->closeOutput();
312 ::oox::ole::SaveInteropProperties(xTextDocument
, aURL
, nullptr, m_sProgId
);
314 OUString
aPersistName( xEmbeddedResolver
->resolveEmbeddedObjectURL( aURL
) );
315 sRet
= aPersistName
.copy( strlen("vnd.sun.star.EmbeddedObject:") );
318 uno::Reference
< lang::XComponent
> xComp( xEmbeddedResolver
, uno::UNO_QUERY_THROW
);
322 catch( const uno::Exception
& )
324 TOOLS_WARN_EXCEPTION( "writerfilter", "OLEHandler::createOLEObject");
329 } //namespace writerfilter::dmapper
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */