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