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 :
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 : 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 : // -----------------------------------------------------------------------------
69 :
70 0 : void SvEmbedTransferHelper::AddSupportedFormats()
71 : {
72 0 : AddFormat( SOT_FORMATSTR_ID_EMBED_SOURCE );
73 0 : AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
74 0 : AddFormat( FORMAT_GDIMETAFILE );
75 0 : }
76 :
77 : // -----------------------------------------------------------------------------
78 :
79 0 : sal_Bool SvEmbedTransferHelper::GetData( const ::com::sun::star::datatransfer::DataFlavor& rFlavor )
80 : {
81 0 : sal_Bool bRet = sal_False;
82 :
83 0 : if( m_xObj.is() )
84 : {
85 : try
86 : {
87 0 : sal_uInt32 nFormat = SotExchange::GetFormat( rFlavor );
88 0 : if( HasFormat( nFormat ) )
89 : {
90 0 : if( nFormat == SOT_FORMATSTR_ID_OBJECTDESCRIPTOR )
91 : {
92 0 : TransferableObjectDescriptor aDesc;
93 0 : FillTransferableObjectDescriptor( aDesc, m_xObj, m_pGraphic, m_nAspect );
94 0 : bRet = SetTransferableObjectDescriptor( aDesc, rFlavor );
95 : }
96 0 : else if( nFormat == SOT_FORMATSTR_ID_EMBED_SOURCE )
97 : {
98 : try
99 : {
100 : // TODO/LATER: Propbably the graphic should be copied here as well
101 : // currently it is handled by the applications
102 0 : utl::TempFile aTmp;
103 0 : aTmp.EnableKillingFile( sal_True );
104 0 : uno::Reference < embed::XEmbedPersist > xPers( m_xObj, uno::UNO_QUERY );
105 0 : if ( xPers.is() )
106 : {
107 0 : uno::Reference < embed::XStorage > xStg = comphelper::OStorageHelper::GetTemporaryStorage();
108 0 : ::rtl::OUString aName( RTL_CONSTASCII_USTRINGPARAM( "Dummy" ));
109 0 : SvStream* pStream = NULL;
110 0 : sal_Bool bDeleteStream = sal_False;
111 0 : uno::Sequence < beans::PropertyValue > aEmpty;
112 0 : xPers->storeToEntry( xStg, aName, aEmpty, aEmpty );
113 0 : if ( xStg->isStreamElement( aName ) )
114 : {
115 0 : uno::Reference < io::XStream > xStm = xStg->cloneStreamElement( aName );
116 0 : pStream = utl::UcbStreamHelper::CreateStream( xStm );
117 0 : bDeleteStream = sal_True;
118 : }
119 : else
120 : {
121 0 : pStream = aTmp.GetStream( STREAM_STD_READWRITE );
122 0 : uno::Reference < embed::XStorage > xStor = comphelper::OStorageHelper::GetStorageFromStream( new utl::OStreamWrapper( *pStream ) );
123 0 : xStg->openStorageElement( aName, embed::ElementModes::READ )->copyToStorage( xStor );
124 : }
125 :
126 0 : ::com::sun::star::uno::Any aAny;
127 0 : const sal_uInt32 nLen = pStream->Seek( STREAM_SEEK_TO_END );
128 0 : ::com::sun::star::uno::Sequence< sal_Int8 > aSeq( nLen );
129 :
130 0 : pStream->Seek( STREAM_SEEK_TO_BEGIN );
131 0 : pStream->Read( aSeq.getArray(), nLen );
132 0 : if ( bDeleteStream )
133 0 : delete pStream;
134 :
135 0 : if( ( bRet = ( aSeq.getLength() > 0 ) ) == sal_True )
136 : {
137 0 : aAny <<= aSeq;
138 0 : SetAny( aAny, rFlavor );
139 0 : }
140 : }
141 : else
142 : {
143 : //TODO/LATER: how to handle objects without persistance?!
144 0 : }
145 : }
146 0 : catch ( uno::Exception& )
147 : {
148 : }
149 : }
150 0 : else if ( nFormat == FORMAT_GDIMETAFILE && m_pGraphic )
151 : {
152 0 : SvMemoryStream aMemStm( 65535, 65535 );
153 0 : aMemStm.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
154 :
155 0 : const GDIMetaFile& aMetaFile = m_pGraphic->GetGDIMetaFile();
156 0 : ((GDIMetaFile*)(&aMetaFile))->Write( aMemStm );
157 0 : uno::Any aAny;
158 0 : aAny <<= uno::Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( aMemStm.GetData() ),
159 0 : aMemStm.Seek( STREAM_SEEK_TO_END ) );
160 0 : SetAny( aAny, rFlavor );
161 0 : bRet = sal_True;
162 : }
163 0 : else if ( m_xObj.is() && :: svt::EmbeddedObjectRef::TryRunningState( m_xObj ) )
164 : {
165 0 : uno::Reference< datatransfer::XTransferable > xTransferable( m_xObj->getComponent(), uno::UNO_QUERY );
166 0 : if ( xTransferable.is() )
167 : {
168 0 : uno::Any aAny = xTransferable->getTransferData( rFlavor );
169 0 : SetAny( aAny, rFlavor );
170 0 : bRet = sal_True;
171 0 : }
172 : }
173 : }
174 : }
175 0 : catch( uno::Exception& )
176 : {
177 : // Error handling?
178 : }
179 : }
180 :
181 0 : return bRet;
182 : }
183 :
184 : // -----------------------------------------------------------------------------
185 :
186 0 : void SvEmbedTransferHelper::ObjectReleased()
187 : {
188 0 : m_xObj = uno::Reference< embed::XEmbeddedObject >();
189 0 : }
190 :
191 0 : void SvEmbedTransferHelper::FillTransferableObjectDescriptor( TransferableObjectDescriptor& rDesc,
192 : const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XEmbeddedObject >& xObj,
193 : Graphic* pGraphic,
194 : sal_Int64 nAspect )
195 : {
196 : //TODO/LATER: need TypeName to fill it into the Descriptor (will be shown in listbox)
197 0 : ::com::sun::star::datatransfer::DataFlavor aFlavor;
198 0 : SotExchange::GetFormatDataFlavor( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR, aFlavor );
199 :
200 0 : rDesc.maClassName = SvGlobalName( xObj->getClassID() );
201 0 : rDesc.maTypeName = aFlavor.HumanPresentableName;
202 :
203 : //TODO/LATER: the aspect size in the descriptor is wrong, unfortunately the stream
204 : // representation of the descriptor allows only 4 bytes for the aspect
205 : // so for internal transport something different should be found
206 0 : rDesc.mnViewAspect = sal::static_int_cast<sal_uInt16>( nAspect );
207 :
208 : //TODO/LATER: status needs to become sal_Int64
209 0 : rDesc.mnOle2Misc = sal::static_int_cast<sal_Int32>(xObj->getStatus( rDesc.mnViewAspect ));
210 :
211 0 : Size aSize;
212 0 : MapMode aMapMode( MAP_100TH_MM );
213 0 : if ( nAspect == embed::Aspects::MSOLE_ICON )
214 : {
215 0 : if ( pGraphic )
216 : {
217 0 : aMapMode = pGraphic->GetPrefMapMode();
218 0 : aSize = pGraphic->GetPrefSize();
219 : }
220 : else
221 0 : aSize = Size( 2500, 2500 );
222 : }
223 : else
224 : {
225 : try
226 : {
227 0 : awt::Size aSz;
228 0 : aSz = xObj->getVisualAreaSize( rDesc.mnViewAspect );
229 0 : aSize = Size( aSz.Width, aSz.Height );
230 : }
231 0 : catch( embed::NoVisualAreaSizeException& )
232 : {
233 : OSL_FAIL( "Can not get visual area size!\n" );
234 0 : aSize = Size( 5000, 5000 );
235 : }
236 :
237 : // TODO/LEAN: getMapUnit can switch object to running state
238 0 : aMapMode = MapMode( VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( rDesc.mnViewAspect ) ) );
239 : }
240 :
241 0 : rDesc.maSize = OutputDevice::LogicToLogic( aSize, aMapMode, MapMode( MAP_100TH_MM ) );
242 0 : rDesc.maDragStartPos = Point();
243 0 : rDesc.maDisplayName = String();
244 0 : rDesc.mbCanLink = sal_False;
245 0 : }
246 :
247 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|