Update ooo320-m1
[ooovba.git] / writerfilter / source / dmapper / OLEHandler.cxx
blob491a1c39c8bc9338160adc0f6a822807de7c6ba2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OLEHandler.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <OLEHandler.hxx>
31 #include <PropertyMap.hxx>
32 #include "GraphicHelpers.hxx"
34 #include <doctok/resourceids.hxx>
35 #include <ooxml/resourceids.hxx>
36 #include <com/sun/star/beans/PropertyValue.hpp>
37 #include <com/sun/star/container/XNameAccess.hpp>
38 #include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
39 #include <com/sun/star/document/XStorageBasedDocument.hpp>
40 #include <com/sun/star/drawing/XShape.hpp>
41 #include <com/sun/star/embed/XEmbeddedObject.hpp>
42 #include <com/sun/star/embed/XEmbedObjectCreator.hpp>
43 #include <com/sun/star/graphic/XGraphic.hpp>
44 #include <com/sun/star/io/XStream.hpp>
45 #include <com/sun/star/lang/XComponent.hpp>
46 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47 #include <com/sun/star/text/XTextDocument.hpp>
48 #include <com/sun/star/uno/XComponentContext.hpp>
50 namespace writerfilter {
51 namespace dmapper {
53 using namespace ::com::sun::star;
54 /*-- 23.04.2008 10:46:14---------------------------------------------------
56 -----------------------------------------------------------------------*/
57 OLEHandler::OLEHandler() :
58 m_nDxaOrig(0),
59 m_nDyaOrig(0),
60 m_nWrapMode(0)
63 /*-- 23.04.2008 10:46:14---------------------------------------------------
65 -----------------------------------------------------------------------*/
66 OLEHandler::~OLEHandler()
69 /*-- 23.04.2008 10:46:14---------------------------------------------------
71 -----------------------------------------------------------------------*/
72 void OLEHandler::attribute(Id rName, Value & rVal)
74 rtl::OUString sStringValue = rVal.getString();
75 (void)rName;
76 /* WRITERFILTERSTATUS: table: OLEHandler_attributedata */
77 switch( rName )
79 /* WRITERFILTERSTATUS: done: 1, planned: 0, spent: 0 */
80 case NS_ooxml::LN_CT_OLEObject_Type:
81 m_sObjectType = sStringValue;
82 break;
83 case NS_ooxml::LN_CT_OLEObject_ProgID:
84 m_sProgId = sStringValue;
85 break;
86 case NS_ooxml::LN_CT_OLEObject_ShapeID:
87 m_sShapeId = sStringValue;
88 break;
89 case NS_ooxml::LN_CT_OLEObject_DrawAspect:
90 m_sDrawAspect = sStringValue;
91 break;
92 case NS_ooxml::LN_CT_OLEObject_ObjectID:
93 m_sObjectId = sStringValue;
94 break;
95 case NS_ooxml::LN_CT_OLEObject_r_id:
96 m_sr_id = sStringValue;
97 break;
98 case NS_ooxml::LN_inputstream:
99 rVal.getAny() >>= m_xInputStream;
100 break;
101 case NS_ooxml::LN_CT_Object_dxaOrig:
102 m_nDxaOrig = rVal.getInt();
103 break;
104 case NS_ooxml::LN_CT_Object_dyaOrig:
105 m_nDyaOrig = rVal.getInt();
106 break;
107 case NS_ooxml::LN_shape:
108 /* WRITERFILTERSTATUS: done: 0, planned: 0.5, spent: 0 */
110 uno::Reference< drawing::XShape > xTempShape;
111 rVal.getAny() >>= xTempShape;
112 if( xTempShape.is() )
114 m_xShape.set( xTempShape );
118 m_aShapeSize = xTempShape->getSize();
119 m_aShapePosition = xTempShape->getPosition();
121 uno::Reference< beans::XPropertySet > xShapeProps( xTempShape, uno::UNO_QUERY_THROW );
122 PropertyNameSupplier& rNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
124 xShapeProps->getPropertyValue( rNameSupplier.GetName( PROP_BITMAP ) ) >>= m_xReplacement;
126 xShapeProps->setPropertyValue(
127 rNameSupplier.GetName( PROP_SURROUND ),
128 uno::makeAny( m_nWrapMode ) );
130 catch( const uno::Exception& e )
132 #if DEBUG
133 clog << "Exception in OLE Handler: ";
134 clog << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr( ) << endl;
135 #endif
139 break;
140 default:
141 OSL_ENSURE( false, "unknown attribute");
144 /*-- 23.04.2008 10:46:14---------------------------------------------------
146 -----------------------------------------------------------------------*/
147 void OLEHandler::sprm(Sprm & rSprm)
149 sal_uInt32 nSprmId = rSprm.getId();
150 switch( nSprmId )
152 case NS_ooxml::LN_OLEObject_OLEObject:
154 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
155 if( pProperties.get())
157 pProperties->resolve(*this);
160 break;
161 case NS_ooxml::LN_wrap_wrap:
163 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
164 if ( pProperties.get( ) )
166 WrapHandlerPtr pHandler( new WrapHandler );
167 pProperties->resolve( *pHandler );
169 m_nWrapMode = pHandler->getWrapMode( );
173 uno::Reference< beans::XPropertySet > xShapeProps( m_xShape, uno::UNO_QUERY_THROW );
174 PropertyNameSupplier& rNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
176 xShapeProps->setPropertyValue(
177 rNameSupplier.GetName( PROP_SURROUND ),
178 uno::makeAny( m_nWrapMode ) );
180 catch( const uno::Exception& e )
182 #if DEBUG
183 clog << "Exception in OLE Handler: ";
184 clog << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr( ) << endl;
185 #endif
189 break;
190 default:
192 OSL_ENSURE( false, "unknown attribute");
196 /*-- 23.04.2008 11:15:19---------------------------------------------------
198 -----------------------------------------------------------------------*/
199 ::rtl::OUString OLEHandler::copyOLEOStream( uno::Reference< text::XTextDocument > xTextDocument )
201 ::rtl::OUString sRet;
202 if( !m_xInputStream.is( ) )
203 return sRet;
206 uno::Reference < lang::XMultiServiceFactory > xFactory(xTextDocument, uno::UNO_QUERY_THROW);
207 uno::Reference< document::XEmbeddedObjectResolver > xEmbeddedResolver(
208 xFactory->createInstance(
209 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.document.ImportEmbeddedObjectResolver" ))), uno::UNO_QUERY_THROW );
210 //hack to work with the ImportEmbeddedObjectResolver
211 static sal_Int32 nObjectCount = 100;
212 uno::Reference< container::XNameAccess > xNA( xEmbeddedResolver, uno::UNO_QUERY_THROW );
213 ::rtl::OUString aURL(RTL_CONSTASCII_USTRINGPARAM("Obj" ));
214 aURL += ::rtl::OUString::valueOf( nObjectCount++ );
215 uno::Reference < io::XOutputStream > xOLEStream;
216 if( (xNA->getByName( aURL ) >>= xOLEStream) && xOLEStream.is() )
218 const sal_Int32 nReadRequest = 0x1000;
219 uno::Sequence< sal_Int8 > aData;
221 while( true )
223 sal_Int32 nRead = m_xInputStream->readBytes( aData, nReadRequest );
224 xOLEStream->writeBytes( aData );
225 if( nRead < nReadRequest )
227 xOLEStream->closeOutput();
228 break;
232 static const ::rtl::OUString sProtocol = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.star.EmbeddedObject:" ));
233 ::rtl::OUString aPersistName( xEmbeddedResolver->resolveEmbeddedObjectURL( aURL ) );
234 sRet = aPersistName.copy( sProtocol.getLength() );
237 uno::Reference< lang::XComponent > xComp( xEmbeddedResolver, uno::UNO_QUERY_THROW );
238 xComp->dispose();
240 catch( const uno::Exception& rEx)
242 (void)rEx;
243 OSL_ENSURE(false, "exception in OLEHandler::createOLEObject");
245 return sRet;
248 } //namespace dmapper
249 } //namespace writerfilter