LibreOffice Module svtools (master) 1
embedtransfer.cxx
Go to the documentation of this file.
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/XEmbeddedObject.hpp>
21#include <com/sun/star/embed/XEmbedPersist.hpp>
22#include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
23#include <com/sun/star/datatransfer/XTransferable.hpp>
24#include <com/sun/star/embed/Aspects.hpp>
25#include <osl/diagnose.h>
26#include <sot/exchange.hxx>
28#include <tools/mapunit.hxx>
29#include <vcl/outdev.hxx>
31#include <vcl/gdimtf.hxx>
37#include <unotools/tempfile.hxx>
39
40#include <svtools/embedhlp.hxx>
41
42using namespace ::com::sun::star;
43
44SvEmbedTransferHelper::SvEmbedTransferHelper( const uno::Reference< embed::XEmbeddedObject >& xObj,
45 const Graphic* pGraphic,
46 sal_Int64 nAspect )
47: m_xObj( xObj )
48, m_nAspect( nAspect )
49{
50 if (pGraphic)
51 m_oGraphic.emplace(*pGraphic);
52 if( xObj.is() )
53 {
55
57 PrepareOLE( aObjDesc );
58 }
59}
60
61
63{
64}
65
66void SvEmbedTransferHelper::SetParentShellID( const OUString& rShellID )
67{
68 maParentShellID = rShellID;
69}
70
71
73{
74 AddFormat( SotClipboardFormatId::EMBED_SOURCE );
75 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
76 AddFormat( SotClipboardFormatId::GDIMETAFILE );
77 AddFormat( SotClipboardFormatId::BITMAP );
78}
79
80
81bool SvEmbedTransferHelper::GetData( const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc )
82{
83 bool bRet = false;
84
85 if( m_xObj.is() )
86 {
87 try
88 {
90 if( HasFormat( nFormat ) )
91 {
92 if( nFormat == SotClipboardFormatId::OBJECTDESCRIPTOR )
93 {
96 bRet = SetTransferableObjectDescriptor( aDesc );
97 }
98 else if( nFormat == SotClipboardFormatId::EMBED_SOURCE )
99 {
100 try
101 {
102 // TODO/LATER: Probably the graphic should be copied here as well
103 // currently it is handled by the applications
105 uno::Reference < embed::XEmbedPersist > xPers( m_xObj, uno::UNO_QUERY );
106 if ( xPers.is() )
107 {
108 uno::Reference < embed::XStorage > xStg = comphelper::OStorageHelper::GetTemporaryStorage();
109 OUString aName( "Dummy" );
110 SvStream* pStream = nullptr;
111 bool bDeleteStream = false;
112 uno::Sequence < beans::PropertyValue > aEmpty;
113 uno::Sequence<beans::PropertyValue> aObjArgs( comphelper::InitPropertySequence({
114 { "SourceShellID", uno::Any(maParentShellID) },
115 { "DestinationShellID", uno::Any(rDestDoc) }
116 }));
117 xPers->storeToEntry(xStg, aName, aEmpty, aObjArgs);
118 if ( xStg->isStreamElement( aName ) )
119 {
120 uno::Reference < io::XStream > xStm = xStg->cloneStreamElement( aName );
121 pStream = utl::UcbStreamHelper::CreateStream( xStm ).release();
122 bDeleteStream = true;
123 }
124 else
125 {
126 pStream = aTmp.GetStream( StreamMode::STD_READWRITE );
127 uno::Reference < embed::XStorage > xStor = comphelper::OStorageHelper::GetStorageFromStream( new utl::OStreamWrapper( *pStream ) );
128 xStg->openStorageElement( aName, embed::ElementModes::READ )->copyToStorage( xStor );
129 }
130
131 const sal_uInt32 nLen = pStream->TellEnd();
132 css::uno::Sequence< sal_Int8 > aSeq( nLen );
133
134 pStream->Seek( STREAM_SEEK_TO_BEGIN );
135 pStream->ReadBytes(aSeq.getArray(), nLen);
136 if ( bDeleteStream )
137 delete pStream;
138
139 bRet = aSeq.hasElements();
140 if( bRet )
141 {
142 SetAny( uno::Any(aSeq) );
143 }
144 }
145 else
146 {
147 //TODO/LATER: how to handle objects without persistence?!
148 }
149 }
150 catch ( uno::Exception& )
151 {
152 }
153 }
154 else if ( nFormat == SotClipboardFormatId::GDIMETAFILE && m_oGraphic )
155 {
156 SvMemoryStream aMemStm( 65535, 65535 );
158
159 const GDIMetaFile& aMetaFile = m_oGraphic->GetGDIMetaFile();
160 SvmWriter aWriter( aMemStm );
161 aWriter.Write( aMetaFile );
162 uno::Any aAny;
163 aAny <<= uno::Sequence< sal_Int8 >( static_cast< const sal_Int8* >( aMemStm.GetData() ),
164 aMemStm.TellEnd() );
165 SetAny( aAny );
166 bRet = true;
167 }
168 else if ( ( nFormat == SotClipboardFormatId::BITMAP || nFormat == SotClipboardFormatId::PNG ) && m_oGraphic )
169 {
170 bRet = SetBitmapEx( m_oGraphic->GetBitmapEx(), rFlavor );
171 }
173 {
174 uno::Reference< datatransfer::XTransferable > xTransferable( m_xObj->getComponent(), uno::UNO_QUERY );
175 if ( xTransferable.is() )
176 {
177 uno::Any aAny = xTransferable->getTransferData( rFlavor );
178 SetAny( aAny );
179 bRet = true;
180 }
181 }
182 }
183 }
184 catch( uno::Exception& )
185 {
186 // Error handling?
187 }
188 }
189
190 return bRet;
191}
192
193
195{
196 m_xObj.clear();
197}
198
200 const css::uno::Reference< css::embed::XEmbeddedObject >& xObj,
201 const Graphic* pGraphic,
202 sal_Int64 nAspect )
203{
204 //TODO/LATER: need TypeName to fill it into the Descriptor (will be shown in listbox)
205 css::datatransfer::DataFlavor aFlavor;
206 SotExchange::GetFormatDataFlavor( SotClipboardFormatId::OBJECTDESCRIPTOR, aFlavor );
207
208 rDesc.maClassName = SvGlobalName( xObj->getClassID() );
209 rDesc.maTypeName = aFlavor.HumanPresentableName;
210
211 //TODO/LATER: the aspect size in the descriptor is wrong, unfortunately the stream
212 // representation of the descriptor allows only 4 bytes for the aspect
213 // so for internal transport something different should be found
214 rDesc.mnViewAspect = sal::static_int_cast<sal_uInt16>( nAspect );
215
216 Size aSize;
217 MapMode aMapMode( MapUnit::Map100thMM );
218 if ( nAspect == embed::Aspects::MSOLE_ICON )
219 {
220 if ( pGraphic )
221 {
222 aMapMode = pGraphic->GetPrefMapMode();
223 aSize = pGraphic->GetPrefSize();
224 }
225 else
226 aSize = Size( 2500, 2500 );
227 }
228 else
229 {
230 try
231 {
232 awt::Size aSz = xObj->getVisualAreaSize( rDesc.mnViewAspect );
233 aSize = Size( aSz.Width, aSz.Height );
234 }
235 catch( embed::NoVisualAreaSizeException& )
236 {
237 OSL_FAIL( "Can not get visual area size!" );
238 aSize = Size( 5000, 5000 );
239 }
240
241 // TODO/LEAN: getMapUnit can switch object to running state
242 aMapMode = MapMode( VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( rDesc.mnViewAspect ) ) );
243 }
244
245 rDesc.maSize = OutputDevice::LogicToLogic( aSize, aMapMode, MapMode( MapUnit::Map100thMM ) );
246 rDesc.maDragStartPos = Point();
247 rDesc.maDisplayName.clear();
248}
249
250/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Size GetPrefSize() const
MapMode GetPrefMapMode() const
SAL_WARN_UNUSED_RESULT Point LogicToLogic(const Point &rPtSource, const MapMode *pMapModeSource, const MapMode *pMapModeDest) const
static bool GetFormatDataFlavor(SotClipboardFormatId nFormat, css::datatransfer::DataFlavor &rFlavor)
static SotClipboardFormatId GetFormat(const css::datatransfer::DataFlavor &rFlavor)
static void FillTransferableObjectDescriptor(TransferableObjectDescriptor &rDesc, const css::uno::Reference< css::embed::XEmbeddedObject > &xObj, const Graphic *pGraphic, sal_Int64 nAspect)
virtual void AddSupportedFormats() override
css::uno::Reference< css::embed::XEmbeddedObject > m_xObj
virtual void ObjectReleased() override
virtual bool GetData(const css::datatransfer::DataFlavor &rFlavor, const OUString &rDestDoc) override
virtual ~SvEmbedTransferHelper() override
void SetParentShellID(const OUString &rShellID)
SvEmbedTransferHelper(const css::uno::Reference< css::embed::XEmbeddedObject > &xObj, const Graphic *pGraphic, sal_Int64 nAspect)
std::optional< Graphic > m_oGraphic
const void * GetData()
virtual sal_uInt64 TellEnd() override
virtual sal_uInt64 TellEnd()
void SetVersion(sal_Int32 n)
sal_uInt64 Seek(sal_uInt64 nPos)
std::size_t ReadBytes(void *pData, std::size_t nSize)
SvStream & Write(const GDIMetaFile &rMetaFile)
bool SetTransferableObjectDescriptor(const TransferableObjectDescriptor &rDesc)
bool SetAny(const css::uno::Any &rAny)
bool SetBitmapEx(const BitmapEx &rBitmap, const css::datatransfer::DataFlavor &rFlavor)
bool HasFormat(SotClipboardFormatId nFormat)
void PrepareOLE(const TransferableObjectDescriptor &rObjDesc)
void AddFormat(SotClipboardFormatId nFormat)
static MapUnit UnoEmbed2VCLMapUnit(sal_Int32 nUnoEmbedMapUnit)
static css::uno::Reference< css::embed::XStorage > GetStorageFromStream(const css::uno::Reference< css::io::XStream > &xStream, sal_Int32 nStorageMode=css::embed::ElementModes::READWRITE, const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >())
static css::uno::Reference< css::embed::XStorage > GetTemporaryStorage(const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >())
static bool TryRunningState(const css::uno::Reference< css::embed::XEmbeddedObject > &)
Definition: embedhlp.cxx:849
SvStream * GetStream(StreamMode eMode)
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
#define SOFFICE_FILEFORMAT_CURRENT
SotClipboardFormatId
OUString aName
Sequence< sal_Int8 > aSeq
css::uno::Sequence< css::beans::PropertyValue > InitPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
#define STREAM_SEEK_TO_BEGIN
signed char sal_Int8