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 <PropertyMap.hxx>
21 #include "GraphicHelpers.hxx"
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>
40 #include "dmapperLoggers.hxx"
42 namespace writerfilter
{
45 using namespace ::com::sun::star
;
48 OLEHandler::OLEHandler() :
49 LoggedProperties(dmapper_logger
, "OLEHandler"),
57 OLEHandler::~OLEHandler()
62 void OLEHandler::lcl_attribute(Id rName
, Value
& rVal
)
64 OUString sStringValue
= rVal
.getString();
68 case NS_ooxml::LN_CT_OLEObject_Type
:
69 m_sObjectType
= sStringValue
;
71 case NS_ooxml::LN_CT_OLEObject_ProgID
:
72 m_sProgId
= sStringValue
;
74 case NS_ooxml::LN_CT_OLEObject_ShapeID
:
75 m_sShapeId
= sStringValue
;
77 case NS_ooxml::LN_CT_OLEObject_DrawAspect
:
78 m_sDrawAspect
= sStringValue
;
80 case NS_ooxml::LN_CT_OLEObject_ObjectID
:
81 m_sObjectId
= sStringValue
;
83 case NS_ooxml::LN_CT_OLEObject_r_id
:
84 m_sr_id
= sStringValue
;
86 case NS_ooxml::LN_inputstream
:
87 rVal
.getAny() >>= m_xInputStream
;
89 case NS_ooxml::LN_CT_Object_dxaOrig
:
90 m_nDxaOrig
= rVal
.getInt();
92 case NS_ooxml::LN_CT_Object_dyaOrig
:
93 m_nDyaOrig
= rVal
.getInt();
95 case NS_ooxml::LN_shape
:
97 uno::Reference
< drawing::XShape
> xTempShape
;
98 rVal
.getAny() >>= xTempShape
;
101 m_xShape
.set( xTempShape
);
102 uno::Reference
< beans::XPropertySet
> xShapeProps( xTempShape
, uno::UNO_QUERY
);
103 PropertyNameSupplier
& rNameSupplier
= PropertyNameSupplier::GetPropertyNameSupplier();
107 m_aShapeSize
= xTempShape
->getSize();
108 m_aShapePosition
= xTempShape
->getPosition();
110 xShapeProps
->getPropertyValue( rNameSupplier
.GetName( PROP_BITMAP
) ) >>= m_xReplacement
;
112 catch( const uno::Exception
& e
)
114 SAL_WARN("writerfilter", "Exception in OLE Handler: " << e
.Message
);
116 // No need to set the wrapping here as it's either set in oox or will be set later
121 OSL_FAIL( "unknown attribute");
126 void OLEHandler::lcl_sprm(Sprm
& rSprm
)
128 sal_uInt32 nSprmId
= rSprm
.getId();
131 case NS_ooxml::LN_OLEObject_OLEObject
:
133 writerfilter::Reference
<Properties
>::Pointer_t pProperties
= rSprm
.getProps();
134 if( pProperties
.get())
136 pProperties
->resolve(*this);
140 case NS_ooxml::LN_wrap_wrap
:
142 writerfilter::Reference
<Properties
>::Pointer_t pProperties
= rSprm
.getProps();
143 if ( pProperties
.get( ) )
145 WrapHandlerPtr
pHandler( new WrapHandler
);
146 pProperties
->resolve( *pHandler
);
148 m_nWrapMode
= pHandler
->getWrapMode( );
152 uno::Reference
< beans::XPropertySet
> xShapeProps( m_xShape
, uno::UNO_QUERY_THROW
);
153 PropertyNameSupplier
& rNameSupplier
= PropertyNameSupplier::GetPropertyNameSupplier();
155 xShapeProps
->setPropertyValue(
156 rNameSupplier
.GetName( PROP_SURROUND
),
157 uno::makeAny( m_nWrapMode
) );
159 catch( const uno::Exception
& e
)
161 SAL_WARN("writerfilter", "Exception in OLE Handler: " << e
.Message
);
168 OSL_FAIL( "unknown attribute");
174 OUString
OLEHandler::copyOLEOStream( uno::Reference
< text::XTextDocument
> xTextDocument
)
177 if( !m_xInputStream
.is( ) )
181 uno::Reference
< lang::XMultiServiceFactory
> xFactory(xTextDocument
, uno::UNO_QUERY_THROW
);
182 uno::Reference
< document::XEmbeddedObjectResolver
> xEmbeddedResolver(
183 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 uno::Reference
< container::XNameAccess
> xNA( xEmbeddedResolver
, uno::UNO_QUERY_THROW
);
187 OUString
aURL("Obj");
188 aURL
+= OUString::valueOf( nObjectCount
++ );
189 uno::Reference
< io::XOutputStream
> xOLEStream
;
190 if( (xNA
->getByName( aURL
) >>= xOLEStream
) && xOLEStream
.is() )
192 const sal_Int32 nReadRequest
= 0x1000;
193 uno::Sequence
< sal_Int8
> aData
;
197 sal_Int32 nRead
= m_xInputStream
->readBytes( aData
, nReadRequest
);
198 xOLEStream
->writeBytes( aData
);
199 if( nRead
< nReadRequest
)
201 xOLEStream
->closeOutput();
206 static const OUString
sProtocol("vnd.sun.star.EmbeddedObject:");
207 OUString
aPersistName( xEmbeddedResolver
->resolveEmbeddedObjectURL( aURL
) );
208 sRet
= aPersistName
.copy( sProtocol
.getLength() );
211 uno::Reference
< lang::XComponent
> xComp( xEmbeddedResolver
, uno::UNO_QUERY_THROW
);
214 catch( const uno::Exception
& )
216 OSL_FAIL("exception in OLEHandler::createOLEObject");
221 } //namespace dmapper
222 } //namespace writerfilter
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */