update emoji autocorrect entries from po-files
[LibreOffice.git] / writerfilter / source / dmapper / OLEHandler.cxx
blobf72d8ef15409a1937c2c01e4d2d90639682faafa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <ooxml/resourceids.hxx>
26 #include <rtl/ustring.hxx>
27 #include <osl/diagnose.h>
28 #include <unotools/mediadescriptor.hxx>
29 #include <officecfg/Office/Common.hxx>
30 #include <com/sun/star/beans/PropertyValue.hpp>
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/document/XStorageBasedDocument.hpp>
37 #include <com/sun/star/drawing/XShape.hpp>
38 #include <com/sun/star/embed/XEmbeddedObject.hpp>
39 #include <com/sun/star/embed/XEmbedObjectCreator.hpp>
40 #include <com/sun/star/graphic/XGraphic.hpp>
41 #include <com/sun/star/io/XStream.hpp>
42 #include <com/sun/star/lang/XComponent.hpp>
43 #include <com/sun/star/lang/XInitialization.hpp>
44 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
45 #include <com/sun/star/text/XTextDocument.hpp>
46 #include <com/sun/star/uno/XComponentContext.hpp>
48 namespace writerfilter {
49 namespace dmapper {
51 using namespace ::com::sun::star;
54 OLEHandler::OLEHandler(DomainMapper& rDomainMapper) :
55 LoggedProperties("OLEHandler"),
56 m_nDxaOrig(0),
57 m_nDyaOrig(0),
58 m_nWrapMode(1),
59 m_rDomainMapper(rDomainMapper)
64 OLEHandler::~OLEHandler()
69 void OLEHandler::lcl_attribute(Id rName, Value & rVal)
71 OUString sStringValue = rVal.getString();
72 (void)rName;
73 switch( rName )
75 case NS_ooxml::LN_CT_OLEObject_Type:
76 m_sObjectType = sStringValue;
77 break;
78 case NS_ooxml::LN_CT_OLEObject_ProgID:
79 m_sProgId = sStringValue;
80 break;
81 case NS_ooxml::LN_CT_OLEObject_ShapeID:
82 m_sShapeId = sStringValue;
83 break;
84 case NS_ooxml::LN_CT_OLEObject_DrawAspect:
85 m_sDrawAspect = sStringValue;
86 break;
87 case NS_ooxml::LN_CT_OLEObject_ObjectID:
88 m_sObjectId = sStringValue;
89 break;
90 case NS_ooxml::LN_CT_OLEObject_r_id:
91 m_sr_id = sStringValue;
92 break;
93 case NS_ooxml::LN_inputstream:
94 rVal.getAny() >>= m_xInputStream;
95 break;
96 case NS_ooxml::LN_CT_Object_dxaOrig:
97 m_nDxaOrig = rVal.getInt();
98 break;
99 case NS_ooxml::LN_CT_Object_dyaOrig:
100 m_nDyaOrig = rVal.getInt();
101 break;
102 case NS_ooxml::LN_shape:
104 uno::Reference< drawing::XShape > xTempShape;
105 rVal.getAny() >>= xTempShape;
106 if( xTempShape.is() )
108 m_xShape.set( xTempShape );
109 uno::Reference< beans::XPropertySet > xShapeProps( xTempShape, uno::UNO_QUERY );
110 PropertyNameSupplier& rNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
114 // Shapes in the header or footer should be in the background.
115 if (m_rDomainMapper.IsInHeaderFooter())
116 xShapeProps->setPropertyValue("Opaque", uno::makeAny(false));
118 m_aShapeSize = xTempShape->getSize();
119 m_aShapePosition = xTempShape->getPosition();
121 xShapeProps->getPropertyValue( rNameSupplier.GetName( PROP_BITMAP ) ) >>= m_xReplacement;
123 catch( const uno::Exception& e )
125 SAL_WARN("writerfilter", "Exception in OLE Handler: " << e.Message);
127 // No need to set the wrapping here as it's either set in oox or will be set later
130 break;
131 default:
132 OSL_FAIL( "unknown attribute");
137 void OLEHandler::lcl_sprm(Sprm & rSprm)
139 sal_uInt32 nSprmId = rSprm.getId();
140 switch( nSprmId )
142 case NS_ooxml::LN_OLEObject_OLEObject:
144 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
145 if( pProperties.get())
147 pProperties->resolve(*this);
150 break;
151 case NS_ooxml::LN_wrap_wrap:
153 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
154 if ( pProperties.get( ) )
156 WrapHandlerPtr pHandler( new WrapHandler );
157 pProperties->resolve( *pHandler );
159 m_nWrapMode = pHandler->getWrapMode( );
163 uno::Reference< beans::XPropertySet > xShapeProps( m_xShape, uno::UNO_QUERY_THROW );
164 PropertyNameSupplier& rNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
166 xShapeProps->setPropertyValue(
167 rNameSupplier.GetName( PROP_SURROUND ),
168 uno::makeAny( m_nWrapMode ) );
170 catch( const uno::Exception& e )
172 SAL_WARN("writerfilter", "Exception in OLE Handler: " << e.Message);
176 break;
177 default:
179 OSL_FAIL( "unknown attribute");
185 void OLEHandler::saveInteropProperties(uno::Reference<text::XTextDocument> const& xTextDocument, const OUString& sObjectName, const OUString& sOldObjectName)
187 static const char sEmbeddingsPropName[] = "EmbeddedObjects";
189 // get interop grab bag from document
190 uno::Reference< beans::XPropertySet > xDocProps( xTextDocument, uno::UNO_QUERY );
191 comphelper::SequenceAsHashMap aGrabBag(xDocProps->getPropertyValue(UNO_NAME_MISC_OBJ_INTEROPGRABBAG));
193 // get EmbeddedObjects property inside grab bag
194 comphelper::SequenceAsHashMap objectsList;
195 if (aGrabBag.find(sEmbeddingsPropName) != aGrabBag.end())
196 objectsList << aGrabBag[sEmbeddingsPropName];
198 uno::Sequence< beans::PropertyValue > aGrabBagAttribute(2);
199 aGrabBagAttribute[0].Name = "ProgID";
200 aGrabBagAttribute[0].Value = uno::Any( m_sProgId );
201 aGrabBagAttribute[1].Name = "DrawAspect";
202 aGrabBagAttribute[1].Value = uno::Any( m_sDrawAspect );
204 // If we got an "old name", erase that first.
205 if (!sOldObjectName.isEmpty())
207 comphelper::SequenceAsHashMap::iterator it = objectsList.find(sOldObjectName);
208 if (it != objectsList.end())
209 objectsList.erase(it);
212 objectsList[sObjectName] = uno::Any( aGrabBagAttribute );
214 // put objects list back into the grab bag
215 aGrabBag[sEmbeddingsPropName] = uno::Any(objectsList.getAsConstPropertyValueList());
217 // put grab bag back into the document
218 xDocProps->setPropertyValue(UNO_NAME_MISC_OBJ_INTEROPGRABBAG, uno::Any(aGrabBag.getAsConstPropertyValueList()));
221 void OLEHandler::importStream(uno::Reference<uno::XComponentContext> xComponentContext, uno::Reference<text::XTextDocument> xTextDocument, uno::Reference<text::XTextContent> xOLE)
223 OUString aFilterService;
224 if (m_sProgId == "Word.Document.12")
225 aFilterService = "com.sun.star.comp.Writer.WriterFilter";
226 else if (m_sProgId == "Equation.3")
227 aFilterService = "com.sun.star.comp.Math.MathTypeFilter";
228 else
229 SAL_WARN("writerfilter", "OLEHandler::importStream: unhandled m_sProgId: " << m_sProgId);
231 if (!m_xInputStream.is() || aFilterService.isEmpty())
232 return;
234 // Create the filter service.
235 uno::Reference<uno::XInterface> xInterface = xComponentContext->getServiceManager()->createInstanceWithContext(aFilterService, xComponentContext);
237 // Set target document.
238 uno::Reference<document::XImporter> xImporter(xInterface, uno::UNO_QUERY);
239 uno::Reference<document::XEmbeddedObjectSupplier> xSupplier(xOLE, uno::UNO_QUERY);
240 uno::Reference<lang::XComponent> xEmbeddedObject(xSupplier->getEmbeddedObject(), uno::UNO_QUERY);
241 xImporter->setTargetDocument( xEmbeddedObject );
243 // Import the input stream.
244 utl::MediaDescriptor aMediaDescriptor;
245 aMediaDescriptor["InputStream"] <<= m_xInputStream;
246 uno::Reference<document::XFilter> xFilter(xInterface, uno::UNO_QUERY);
247 xFilter->filter(aMediaDescriptor.getAsConstPropertyValueList());
249 // Now that the data is imported, update the (typically) changed stream name.
250 uno::Reference<beans::XPropertySet> xPropertySet(xOLE, uno::UNO_QUERY);
251 saveInteropProperties(xTextDocument, xPropertySet->getPropertyValue("StreamName").get<OUString>(), m_aURL);
254 OUString OLEHandler::getCLSID(uno::Reference<uno::XComponentContext> xComponentContext) const
256 OUString aRet;
258 // See officecfg/registry/data/org/openoffice/Office/Embedding.xcu.
259 if (m_sProgId == "Word.Document.12")
261 if (officecfg::Office::Common::Filter::Microsoft::Import::WinWordToWriter::get(xComponentContext))
262 aRet = "8BC6B165-B1B2-4EDD-aa47-dae2ee689dd6";
264 else if (m_sProgId == "Equation.3")
266 if (officecfg::Office::Common::Filter::Microsoft::Import::MathTypeToMath::get(xComponentContext))
267 aRet = "078B7ABA-54FC-457F-8551-6147E776A997";
269 else
270 SAL_WARN("writerfilter", "OLEHandler::getCLSID: unhandled m_sProgId: " << m_sProgId);
272 return aRet;
275 OUString OLEHandler::copyOLEOStream(
276 uno::Reference<text::XTextDocument> const& xTextDocument)
278 OUString sRet;
279 if( !m_xInputStream.is( ) )
280 return sRet;
283 uno::Reference < lang::XMultiServiceFactory > xFactory(xTextDocument, uno::UNO_QUERY_THROW);
284 uno::Reference< document::XEmbeddedObjectResolver > xEmbeddedResolver(
285 xFactory->createInstance("com.sun.star.document.ImportEmbeddedObjectResolver"), uno::UNO_QUERY_THROW );
286 //hack to work with the ImportEmbeddedObjectResolver
287 static sal_Int32 nObjectCount = 100;
288 uno::Reference< container::XNameAccess > xNA( xEmbeddedResolver, uno::UNO_QUERY_THROW );
289 OUString aURL("Obj");
290 aURL += OUString::number( nObjectCount++ );
291 uno::Reference < io::XOutputStream > xOLEStream;
292 if( (xNA->getByName( aURL ) >>= xOLEStream) && xOLEStream.is() )
294 const sal_Int32 nReadRequest = 0x1000;
295 uno::Sequence< sal_Int8 > aData;
297 while( true )
299 sal_Int32 nRead = m_xInputStream->readBytes( aData, nReadRequest );
300 xOLEStream->writeBytes( aData );
301 if( nRead < nReadRequest )
303 xOLEStream->closeOutput();
304 break;
308 saveInteropProperties( xTextDocument, aURL );
310 static const char sProtocol[] = "vnd.sun.star.EmbeddedObject:";
311 OUString aPersistName( xEmbeddedResolver->resolveEmbeddedObjectURL( aURL ) );
312 sRet = aPersistName.copy( strlen(sProtocol) );
315 uno::Reference< lang::XComponent > xComp( xEmbeddedResolver, uno::UNO_QUERY_THROW );
316 xComp->dispose();
317 m_aURL = aURL;
319 catch( const uno::Exception& )
321 OSL_FAIL("exception in OLEHandler::createOLEObject");
323 return sRet;
326 } //namespace dmapper
327 } //namespace writerfilter
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */