LCOV - code coverage report
Current view: top level - writerfilter/source/dmapper - OLEHandler.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 110 113 97.3 %
Date: 2014-04-11 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          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 <editeng/unoprnms.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         169 : OLEHandler::OLEHandler() :
      49             : LoggedProperties(dmapper_logger, "OLEHandler"),
      50             : m_nDxaOrig(0),
      51             : m_nDyaOrig(0),
      52         169 :     m_nWrapMode(1)
      53             : {
      54         169 : }
      55             : 
      56             : 
      57         338 : OLEHandler::~OLEHandler()
      58             : {
      59         338 : }
      60             : 
      61             : 
      62         390 : void OLEHandler::lcl_attribute(Id rName, Value & rVal)
      63             : {
      64         390 :     OUString sStringValue = rVal.getString();
      65             :     (void)rName;
      66         390 :     switch( rName )
      67             :     {
      68             :         case NS_ooxml::LN_CT_OLEObject_Type:
      69          22 :             m_sObjectType = sStringValue;
      70          22 :         break;
      71             :         case NS_ooxml::LN_CT_OLEObject_ProgID:
      72          22 :             m_sProgId = sStringValue;
      73          22 :         break;
      74             :         case NS_ooxml::LN_CT_OLEObject_ShapeID:
      75          22 :             m_sShapeId = sStringValue;
      76          22 :         break;
      77             :         case NS_ooxml::LN_CT_OLEObject_DrawAspect:
      78          22 :             m_sDrawAspect = sStringValue;
      79          22 :         break;
      80             :         case NS_ooxml::LN_CT_OLEObject_ObjectID:
      81          22 :             m_sObjectId = sStringValue;
      82          22 :         break;
      83             :         case NS_ooxml::LN_CT_OLEObject_r_id:
      84          22 :             m_sr_id = sStringValue;
      85          22 :         break;
      86             :         case NS_ooxml::LN_inputstream:
      87          22 :             rVal.getAny() >>= m_xInputStream;
      88          22 :         break;
      89             :         case NS_ooxml::LN_CT_Object_dxaOrig:
      90          34 :             m_nDxaOrig = rVal.getInt();
      91          34 :         break;
      92             :         case NS_ooxml::LN_CT_Object_dyaOrig:
      93          34 :             m_nDyaOrig = rVal.getInt();
      94          34 :         break;
      95             :         case NS_ooxml::LN_shape:
      96             :         {
      97         168 :             uno::Reference< drawing::XShape > xTempShape;
      98         168 :             rVal.getAny() >>= xTempShape;
      99         168 :             if( xTempShape.is() )
     100             :             {
     101         168 :                 m_xShape.set( xTempShape );
     102         168 :                 uno::Reference< beans::XPropertySet > xShapeProps( xTempShape, uno::UNO_QUERY );
     103         168 :                 PropertyNameSupplier& rNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
     104             : 
     105             :                 try
     106             :                 {
     107         168 :                     m_aShapeSize = xTempShape->getSize();
     108         168 :                     m_aShapePosition = xTempShape->getPosition();
     109             : 
     110         138 :                     xShapeProps->getPropertyValue( rNameSupplier.GetName( PROP_BITMAP ) ) >>= m_xReplacement;
     111             :                 }
     112          30 :                 catch( const uno::Exception& e )
     113             :                 {
     114             :                     SAL_WARN("writerfilter", "Exception in OLE Handler: " << e.Message);
     115         168 :                 }
     116             :                 // No need to set the wrapping here as it's either set in oox or will be set later
     117         168 :             }
     118             :         }
     119         168 :         break;
     120             :         default:
     121             :             OSL_FAIL( "unknown attribute");
     122         390 :     }
     123         390 : }
     124             : 
     125             : 
     126          52 : void OLEHandler::lcl_sprm(Sprm & rSprm)
     127             : {
     128          52 :     sal_uInt32 nSprmId = rSprm.getId();
     129          52 :     switch( nSprmId )
     130             :     {
     131             :         case NS_ooxml::LN_OLEObject_OLEObject:
     132             :         {
     133          22 :             writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
     134          22 :             if( pProperties.get())
     135             :             {
     136          22 :                 pProperties->resolve(*this);
     137          22 :             }
     138             :         }
     139          22 :         break;
     140             :         case NS_ooxml::LN_wrap_wrap:
     141             :         {
     142          10 :             writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
     143          10 :             if ( pProperties.get( ) )
     144             :             {
     145          10 :                 WrapHandlerPtr pHandler( new WrapHandler );
     146          10 :                 pProperties->resolve( *pHandler );
     147             : 
     148          10 :                 m_nWrapMode = pHandler->getWrapMode( );
     149             : 
     150             :                 try
     151             :                 {
     152          10 :                     uno::Reference< beans::XPropertySet > xShapeProps( m_xShape, uno::UNO_QUERY_THROW );
     153          10 :                     PropertyNameSupplier& rNameSupplier = PropertyNameSupplier::GetPropertyNameSupplier();
     154             : 
     155          10 :                     xShapeProps->setPropertyValue(
     156             :                         rNameSupplier.GetName( PROP_SURROUND ),
     157          10 :                         uno::makeAny( m_nWrapMode ) );
     158             :                 }
     159           0 :                 catch( const uno::Exception& e )
     160             :                 {
     161             :                     SAL_WARN("writerfilter", "Exception in OLE Handler: " << e.Message);
     162          10 :                 }
     163          10 :             }
     164             :         }
     165          10 :         break;
     166             :         default:
     167             :         {
     168             :             OSL_FAIL( "unknown attribute");
     169             :         }
     170             :     }
     171          52 : }
     172             : 
     173             : 
     174          22 : void OLEHandler::saveInteropProperties( uno::Reference< text::XTextDocument > xTextDocument, const OUString& sObjectName )
     175             : {
     176          22 :     const OUString sGrabBagPropName = UNO_NAME_MISC_OBJ_INTEROPGRABBAG;
     177          44 :     const OUString sEmbeddingsPropName = "EmbeddedObjects";
     178             : 
     179             :     // get interop grab bag from document
     180          44 :     uno::Reference< beans::XPropertySet > xDocProps( xTextDocument, uno::UNO_QUERY );
     181          44 :     uno::Sequence< beans::PropertyValue > aGrabBag;
     182          22 :     xDocProps->getPropertyValue( sGrabBagPropName ) >>= aGrabBag;
     183             : 
     184             :     // get EmbeddedObjects property inside grab bag
     185          22 :     sal_Int32 i = 0;
     186          22 :     sal_Int32 nBagLength = aGrabBag.getLength();
     187          44 :     uno::Sequence< beans::PropertyValue > objectsList;
     188          48 :     for( ; i < nBagLength; ++i )
     189          35 :         if ( aGrabBag[i].Name == sEmbeddingsPropName )
     190             :         {
     191           9 :             aGrabBag[i].Value >>= objectsList;
     192           9 :             break;
     193             :         }
     194             : 
     195             :     // save ProgID of current object
     196          22 :     sal_Int32 length = objectsList.getLength();
     197          22 :     objectsList.realloc( length + 1 );
     198          22 :     objectsList[length].Name = sObjectName;
     199          22 :     objectsList[length].Value = uno::Any( m_sProgId );
     200             : 
     201             :     // put objects list back into the grab bag
     202          22 :     if( i == nBagLength )
     203             :     {
     204          13 :         aGrabBag.realloc( nBagLength + 1 );
     205          13 :         aGrabBag[nBagLength].Name = sEmbeddingsPropName;
     206          13 :         aGrabBag[nBagLength].Value = uno::Any( objectsList );
     207             :     }
     208             :     else
     209           9 :         aGrabBag[i].Value = uno::Any( objectsList );
     210             : 
     211             :     // put grab bag back into the document
     212          44 :     xDocProps->setPropertyValue( sGrabBagPropName, uno::Any( aGrabBag ) );
     213          22 : }
     214             : 
     215             : 
     216          22 : OUString OLEHandler::copyOLEOStream( uno::Reference< text::XTextDocument > xTextDocument )
     217             : {
     218          22 :     OUString sRet;
     219          22 :     if( !m_xInputStream.is( ) )
     220           0 :         return sRet;
     221             :     try
     222             :     {
     223          22 :         uno::Reference < lang::XMultiServiceFactory > xFactory(xTextDocument, uno::UNO_QUERY_THROW);
     224             :         uno::Reference< document::XEmbeddedObjectResolver > xEmbeddedResolver(
     225          44 :             xFactory->createInstance("com.sun.star.document.ImportEmbeddedObjectResolver"), uno::UNO_QUERY_THROW );
     226             :         //hack to work with the ImportEmbeddedObjectResolver
     227             :         static sal_Int32 nObjectCount = 100;
     228          44 :         uno::Reference< container::XNameAccess > xNA( xEmbeddedResolver, uno::UNO_QUERY_THROW );
     229          44 :         OUString aURL("Obj");
     230          22 :         aURL += OUString::number( nObjectCount++ );
     231          44 :         uno::Reference < io::XOutputStream > xOLEStream;
     232          22 :         if( (xNA->getByName( aURL ) >>= xOLEStream) && xOLEStream.is() )
     233             :         {
     234          22 :             const sal_Int32 nReadRequest = 0x1000;
     235          22 :             uno::Sequence< sal_Int8 > aData;
     236             : 
     237             :             while( true )
     238             :             {
     239         288 :                 sal_Int32 nRead = m_xInputStream->readBytes( aData, nReadRequest );
     240         288 :                 xOLEStream->writeBytes( aData );
     241         288 :                 if( nRead < nReadRequest )
     242             :                 {
     243          22 :                     xOLEStream->closeOutput();
     244          22 :                     break;
     245             :                 }
     246             :             }
     247             : 
     248          22 :             saveInteropProperties( xTextDocument, aURL );
     249             : 
     250          22 :             static const OUString sProtocol("vnd.sun.star.EmbeddedObject:");
     251          44 :             OUString aPersistName( xEmbeddedResolver->resolveEmbeddedObjectURL( aURL ) );
     252          44 :             sRet = aPersistName.copy( sProtocol.getLength() );
     253             : 
     254             :         }
     255          44 :         uno::Reference< lang::XComponent > xComp( xEmbeddedResolver, uno::UNO_QUERY_THROW );
     256          44 :         xComp->dispose();
     257             :     }
     258           0 :     catch( const uno::Exception& )
     259             :     {
     260             :         OSL_FAIL("exception in OLEHandler::createOLEObject");
     261             :     }
     262          22 :     return sRet;
     263             : }
     264             : 
     265             : } //namespace dmapper
     266             : } //namespace writerfilter
     267             : 
     268             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10