LCOV - code coverage report
Current view: top level - svtools/source/misc - embedtransfer.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 116 0.0 %
Date: 2015-06-13 12:38:46 Functions: 0 8 0.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             : 
      20             : #include <com/sun/star/embed/XComponentSupplier.hpp>
      21             : #include <com/sun/star/embed/EmbedStates.hpp>
      22             : #include <com/sun/star/embed/XVisualObject.hpp>
      23             : #include <com/sun/star/embed/XEmbedPersist.hpp>
      24             : #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
      25             : #include <com/sun/star/datatransfer/XTransferable.hpp>
      26             : #include <com/sun/star/embed/Aspects.hpp>
      27             : #include <sot/exchange.hxx>
      28             : #include <svtools/embedtransfer.hxx>
      29             : #include <tools/mapunit.hxx>
      30             : #include <vcl/outdev.hxx>
      31             : #include <comphelper/storagehelper.hxx>
      32             : #include <unotools/ucbstreamhelper.hxx>
      33             : #include <unotools/streamwrap.hxx>
      34             : #include <unotools/tempfile.hxx>
      35             : #include <toolkit/helper/vclunohelper.hxx>
      36             : 
      37             : #include <svtools/embedhlp.hxx>
      38             : 
      39             : using namespace ::com::sun::star;
      40             : 
      41           0 : SvEmbedTransferHelper::SvEmbedTransferHelper( const uno::Reference< embed::XEmbeddedObject >& xObj,
      42             :                                               const Graphic* pGraphic,
      43             :                                                 sal_Int64 nAspect )
      44             : : m_xObj( xObj )
      45           0 : , m_pGraphic( pGraphic ? new Graphic( *pGraphic ) : NULL )
      46           0 : , m_nAspect( nAspect )
      47             : {
      48           0 :     if( xObj.is() )
      49             :     {
      50           0 :         TransferableObjectDescriptor aObjDesc;
      51             : 
      52           0 :         FillTransferableObjectDescriptor( aObjDesc, m_xObj, NULL, m_nAspect );
      53           0 :         PrepareOLE( aObjDesc );
      54             :     }
      55           0 : }
      56             : 
      57             : 
      58             : 
      59           0 : SvEmbedTransferHelper::~SvEmbedTransferHelper()
      60             : {
      61           0 :     if ( m_pGraphic )
      62             :     {
      63           0 :         delete m_pGraphic;
      64           0 :         m_pGraphic = NULL;
      65             :     }
      66           0 : }
      67             : 
      68           0 : void SvEmbedTransferHelper::SetParentShellID( const OUString& rShellID )
      69             : {
      70           0 :     maParentShellID = rShellID;
      71           0 : }
      72             : 
      73             : 
      74           0 : void SvEmbedTransferHelper::AddSupportedFormats()
      75             : {
      76           0 :     AddFormat( SotClipboardFormatId::EMBED_SOURCE );
      77           0 :     AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
      78           0 :     AddFormat( SotClipboardFormatId::GDIMETAFILE );
      79           0 :     AddFormat( SotClipboardFormatId::BITMAP );
      80           0 : }
      81             : 
      82             : 
      83             : 
      84           0 : bool SvEmbedTransferHelper::GetData( const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc )
      85             : {
      86           0 :     bool bRet = false;
      87             : 
      88           0 :     if( m_xObj.is() )
      89             :     {
      90             :         try
      91             :         {
      92           0 :             SotClipboardFormatId nFormat = SotExchange::GetFormat( rFlavor );
      93           0 :             if( HasFormat( nFormat ) )
      94             :             {
      95           0 :                 if( nFormat == SotClipboardFormatId::OBJECTDESCRIPTOR )
      96             :                 {
      97           0 :                     TransferableObjectDescriptor aDesc;
      98           0 :                     FillTransferableObjectDescriptor( aDesc, m_xObj, m_pGraphic, m_nAspect );
      99           0 :                     bRet = SetTransferableObjectDescriptor( aDesc, rFlavor );
     100             :                 }
     101           0 :                 else if( nFormat == SotClipboardFormatId::EMBED_SOURCE )
     102             :                 {
     103             :                     try
     104             :                     {
     105             :                         // TODO/LATER: Propbably the graphic should be copied here as well
     106             :                         // currently it is handled by the applications
     107           0 :                         utl::TempFile aTmp;
     108           0 :                         aTmp.EnableKillingFile( true );
     109           0 :                         uno::Reference < embed::XEmbedPersist > xPers( m_xObj, uno::UNO_QUERY );
     110           0 :                         if ( xPers.is() )
     111             :                         {
     112           0 :                             uno::Reference < embed::XStorage > xStg = comphelper::OStorageHelper::GetTemporaryStorage();
     113           0 :                             OUString aName( "Dummy" );
     114           0 :                             SvStream* pStream = NULL;
     115           0 :                             bool bDeleteStream = false;
     116           0 :                             uno::Sequence < beans::PropertyValue > aEmpty;
     117           0 :                             uno::Sequence<beans::PropertyValue> aObjArgs(2);
     118           0 :                             aObjArgs[0].Name = "SourceShellID";
     119           0 :                             aObjArgs[0].Value <<= maParentShellID;
     120           0 :                             aObjArgs[1].Name = "DestinationShellID";
     121           0 :                             aObjArgs[1].Value <<= rDestDoc;
     122           0 :                             xPers->storeToEntry(xStg, aName, aEmpty, aObjArgs);
     123           0 :                             if ( xStg->isStreamElement( aName ) )
     124             :                             {
     125           0 :                                 uno::Reference < io::XStream > xStm = xStg->cloneStreamElement( aName );
     126           0 :                                 pStream = utl::UcbStreamHelper::CreateStream( xStm );
     127           0 :                                 bDeleteStream = true;
     128             :                             }
     129             :                             else
     130             :                             {
     131           0 :                                 pStream = aTmp.GetStream( STREAM_STD_READWRITE );
     132           0 :                                 uno::Reference < embed::XStorage > xStor = comphelper::OStorageHelper::GetStorageFromStream( new utl::OStreamWrapper( *pStream ) );
     133           0 :                                 xStg->openStorageElement( aName, embed::ElementModes::READ )->copyToStorage( xStor );
     134             :                             }
     135             : 
     136           0 :                             ::com::sun::star::uno::Any                  aAny;
     137           0 :                             const sal_uInt32                            nLen = pStream->Seek( STREAM_SEEK_TO_END );
     138           0 :                             ::com::sun::star::uno::Sequence< sal_Int8 > aSeq( nLen );
     139             : 
     140           0 :                             pStream->Seek( STREAM_SEEK_TO_BEGIN );
     141           0 :                             pStream->Read( aSeq.getArray(),  nLen );
     142           0 :                             if ( bDeleteStream )
     143           0 :                                 delete pStream;
     144             : 
     145           0 :                             if( ( bRet = ( aSeq.getLength() > 0 ) ) )
     146             :                             {
     147           0 :                                 aAny <<= aSeq;
     148           0 :                                 SetAny( aAny, rFlavor );
     149           0 :                             }
     150             :                         }
     151             :                         else
     152             :                         {
     153             :                             //TODO/LATER: how to handle objects without persistence?!
     154           0 :                         }
     155             :                     }
     156           0 :                     catch ( uno::Exception& )
     157             :                     {
     158             :                     }
     159             :                 }
     160           0 :                 else if ( nFormat == SotClipboardFormatId::GDIMETAFILE && m_pGraphic )
     161             :                 {
     162           0 :                     SvMemoryStream aMemStm( 65535, 65535 );
     163           0 :                     aMemStm.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
     164             : 
     165           0 :                     const GDIMetaFile& aMetaFile = m_pGraphic->GetGDIMetaFile();
     166           0 :                     const_cast<GDIMetaFile*>(&aMetaFile)->Write( aMemStm );
     167           0 :                     uno::Any aAny;
     168           0 :                     aAny <<= uno::Sequence< sal_Int8 >( static_cast< const sal_Int8* >( aMemStm.GetData() ),
     169           0 :                                                     aMemStm.Seek( STREAM_SEEK_TO_END ) );
     170           0 :                     SetAny( aAny, rFlavor );
     171           0 :                     bRet = true;
     172             :                 }
     173           0 :                 else if ( ( nFormat == SotClipboardFormatId::BITMAP || nFormat == SotClipboardFormatId::PNG ) && m_pGraphic )
     174             :                 {
     175           0 :                     bRet = SetBitmapEx( m_pGraphic->GetBitmapEx(), rFlavor );
     176             :                 }
     177           0 :                 else if ( m_xObj.is() && :: svt::EmbeddedObjectRef::TryRunningState( m_xObj ) )
     178             :                 {
     179           0 :                     uno::Reference< datatransfer::XTransferable > xTransferable( m_xObj->getComponent(), uno::UNO_QUERY );
     180           0 :                     if ( xTransferable.is() )
     181             :                     {
     182           0 :                         uno::Any aAny = xTransferable->getTransferData( rFlavor );
     183           0 :                         SetAny( aAny, rFlavor );
     184           0 :                         bRet = true;
     185           0 :                     }
     186             :                 }
     187             :             }
     188             :         }
     189           0 :         catch( uno::Exception& )
     190             :         {
     191             :             // Error handling?
     192             :         }
     193             :     }
     194             : 
     195           0 :     return bRet;
     196             : }
     197             : 
     198             : 
     199             : 
     200           0 : void SvEmbedTransferHelper::ObjectReleased()
     201             : {
     202           0 :     m_xObj = uno::Reference< embed::XEmbeddedObject >();
     203           0 : }
     204             : 
     205           0 : void SvEmbedTransferHelper::FillTransferableObjectDescriptor( TransferableObjectDescriptor& rDesc,
     206             :     const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XEmbeddedObject >& xObj,
     207             :     const Graphic* pGraphic,
     208             :     sal_Int64 nAspect )
     209             : {
     210             :     //TODO/LATER: need TypeName to fill it into the Descriptor (will be shown in listbox)
     211           0 :     ::com::sun::star::datatransfer::DataFlavor aFlavor;
     212           0 :     SotExchange::GetFormatDataFlavor( SotClipboardFormatId::OBJECTDESCRIPTOR, aFlavor );
     213             : 
     214           0 :     rDesc.maClassName = SvGlobalName( xObj->getClassID() );
     215           0 :     rDesc.maTypeName = aFlavor.HumanPresentableName;
     216             : 
     217             :     //TODO/LATER: the aspect size in the descriptor is wrong, unfortunately the stream
     218             :     // representation of the descriptor allows only 4 bytes for the aspect
     219             :     // so for internal transport something different should be found
     220           0 :     rDesc.mnViewAspect = sal::static_int_cast<sal_uInt16>( nAspect );
     221             : 
     222             :     //TODO/LATER: status needs to become sal_Int64
     223           0 :     rDesc.mnOle2Misc = sal::static_int_cast<sal_Int32>(xObj->getStatus( rDesc.mnViewAspect ));
     224             : 
     225           0 :     Size aSize;
     226           0 :     MapMode aMapMode( MAP_100TH_MM );
     227           0 :     if ( nAspect == embed::Aspects::MSOLE_ICON )
     228             :     {
     229           0 :         if ( pGraphic )
     230             :         {
     231           0 :             aMapMode = pGraphic->GetPrefMapMode();
     232           0 :             aSize = pGraphic->GetPrefSize();
     233             :         }
     234             :         else
     235           0 :             aSize = Size( 2500, 2500 );
     236             :     }
     237             :     else
     238             :     {
     239             :         try
     240             :         {
     241           0 :             awt::Size aSz;
     242           0 :             aSz = xObj->getVisualAreaSize( rDesc.mnViewAspect );
     243           0 :             aSize = Size( aSz.Width, aSz.Height );
     244             :         }
     245           0 :         catch( embed::NoVisualAreaSizeException& )
     246             :         {
     247             :             OSL_FAIL( "Can not get visual area size!\n" );
     248           0 :             aSize = Size( 5000, 5000 );
     249             :         }
     250             : 
     251             :         // TODO/LEAN: getMapUnit can switch object to running state
     252           0 :         aMapMode = MapMode( VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( rDesc.mnViewAspect ) ) );
     253             :     }
     254             : 
     255           0 :     rDesc.maSize = OutputDevice::LogicToLogic( aSize, aMapMode, MapMode( MAP_100TH_MM ) );
     256           0 :     rDesc.maDragStartPos = Point();
     257           0 :     rDesc.maDisplayName.clear();
     258           0 :     rDesc.mbCanLink = false;
     259           0 : }
     260             : 
     261             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11