1*5900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5900e8ecSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5900e8ecSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5900e8ecSAndrew Rist  * distributed with this work for additional information
6*5900e8ecSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5900e8ecSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5900e8ecSAndrew Rist  * "License"); you may not use this file except in compliance
9*5900e8ecSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5900e8ecSAndrew Rist  *
11*5900e8ecSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5900e8ecSAndrew Rist  *
13*5900e8ecSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5900e8ecSAndrew Rist  * software distributed under the License is distributed on an
15*5900e8ecSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5900e8ecSAndrew Rist  * KIND, either express or implied.  See the License for the
17*5900e8ecSAndrew Rist  * specific language governing permissions and limitations
18*5900e8ecSAndrew Rist  * under the License.
19*5900e8ecSAndrew Rist  *
20*5900e8ecSAndrew Rist  *************************************************************/
21*5900e8ecSAndrew Rist 
22*5900e8ecSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svtools.hxx"
26cdf0e10cSrcweir #include <com/sun/star/embed/XComponentSupplier.hpp>
27cdf0e10cSrcweir #include <com/sun/star/embed/EmbedStates.hpp>
28cdf0e10cSrcweir #include <com/sun/star/embed/XVisualObject.hpp>
29cdf0e10cSrcweir #include <com/sun/star/embed/XEmbedPersist.hpp>
30cdf0e10cSrcweir #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
31cdf0e10cSrcweir #include <com/sun/star/datatransfer/XTransferable.hpp>
32cdf0e10cSrcweir #include <com/sun/star/embed/Aspects.hpp>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <svtools/embedtransfer.hxx>
35cdf0e10cSrcweir #include <tools/mapunit.hxx>
36cdf0e10cSrcweir #include <vcl/outdev.hxx>
37cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
38cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
39cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
40cdf0e10cSrcweir #include <unotools/tempfile.hxx>
41cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <svtools/embedhlp.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using namespace ::com::sun::star;
46cdf0e10cSrcweir 
SvEmbedTransferHelper(const uno::Reference<embed::XEmbeddedObject> & xObj,Graphic * pGraphic,sal_Int64 nAspect)47cdf0e10cSrcweir SvEmbedTransferHelper::SvEmbedTransferHelper( const uno::Reference< embed::XEmbeddedObject >& xObj,
48cdf0e10cSrcweir 												Graphic* pGraphic,
49cdf0e10cSrcweir 												sal_Int64 nAspect )
50cdf0e10cSrcweir : m_xObj( xObj )
51cdf0e10cSrcweir , m_pGraphic( pGraphic ? new Graphic( *pGraphic ) : NULL )
52cdf0e10cSrcweir , m_nAspect( nAspect )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     if( xObj.is() )
55cdf0e10cSrcweir     {
56cdf0e10cSrcweir         TransferableObjectDescriptor aObjDesc;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir         FillTransferableObjectDescriptor( aObjDesc, m_xObj, NULL, m_nAspect );
59cdf0e10cSrcweir         PrepareOLE( aObjDesc );
60cdf0e10cSrcweir     }
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir // -----------------------------------------------------------------------------
64cdf0e10cSrcweir 
~SvEmbedTransferHelper()65cdf0e10cSrcweir SvEmbedTransferHelper::~SvEmbedTransferHelper()
66cdf0e10cSrcweir {
67cdf0e10cSrcweir 	if ( m_pGraphic )
68cdf0e10cSrcweir 	{
69cdf0e10cSrcweir 		delete m_pGraphic;
70cdf0e10cSrcweir 		m_pGraphic = NULL;
71cdf0e10cSrcweir 	}
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir // -----------------------------------------------------------------------------
75cdf0e10cSrcweir 
AddSupportedFormats()76cdf0e10cSrcweir void SvEmbedTransferHelper::AddSupportedFormats()
77cdf0e10cSrcweir {
78cdf0e10cSrcweir 	AddFormat( SOT_FORMATSTR_ID_EMBED_SOURCE );
79cdf0e10cSrcweir 	AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
80cdf0e10cSrcweir 	AddFormat( FORMAT_GDIMETAFILE );
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir // -----------------------------------------------------------------------------
84cdf0e10cSrcweir 
GetData(const::com::sun::star::datatransfer::DataFlavor & rFlavor)85cdf0e10cSrcweir sal_Bool SvEmbedTransferHelper::GetData( const ::com::sun::star::datatransfer::DataFlavor& rFlavor )
86cdf0e10cSrcweir {
87cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 	if( m_xObj.is() )
90cdf0e10cSrcweir 	{
91cdf0e10cSrcweir         try
92cdf0e10cSrcweir         {
93cdf0e10cSrcweir             sal_uInt32 nFormat = SotExchange::GetFormat( rFlavor );
94cdf0e10cSrcweir             if( HasFormat( nFormat ) )
95cdf0e10cSrcweir             {
96cdf0e10cSrcweir                 if( nFormat == SOT_FORMATSTR_ID_OBJECTDESCRIPTOR )
97cdf0e10cSrcweir                 {
98cdf0e10cSrcweir                     TransferableObjectDescriptor aDesc;
99cdf0e10cSrcweir                     FillTransferableObjectDescriptor( aDesc, m_xObj, m_pGraphic, m_nAspect );
100cdf0e10cSrcweir                     bRet = SetTransferableObjectDescriptor( aDesc, rFlavor );
101cdf0e10cSrcweir                 }
102cdf0e10cSrcweir                 else if( nFormat == SOT_FORMATSTR_ID_EMBED_SOURCE )
103cdf0e10cSrcweir                 {
104cdf0e10cSrcweir                     try
105cdf0e10cSrcweir                     {
106cdf0e10cSrcweir 						// TODO/LATER: Propbably the graphic should be copied here as well
107cdf0e10cSrcweir 						// currently it is handled by the applications
108cdf0e10cSrcweir                         utl::TempFile aTmp;
109cdf0e10cSrcweir                         aTmp.EnableKillingFile( sal_True );
110cdf0e10cSrcweir                         uno::Reference < embed::XEmbedPersist > xPers( m_xObj, uno::UNO_QUERY );
111cdf0e10cSrcweir                         if ( xPers.is() )
112cdf0e10cSrcweir                         {
113cdf0e10cSrcweir                             uno::Reference < embed::XStorage > xStg = comphelper::OStorageHelper::GetTemporaryStorage();
114cdf0e10cSrcweir                             ::rtl::OUString aName = ::rtl::OUString::createFromAscii("Dummy");
115cdf0e10cSrcweir                             SvStream* pStream = NULL;
116cdf0e10cSrcweir                             sal_Bool bDeleteStream = sal_False;
117cdf0e10cSrcweir                             uno::Sequence < beans::PropertyValue > aEmpty;
118cdf0e10cSrcweir                             xPers->storeToEntry( xStg, aName, aEmpty, aEmpty );
119cdf0e10cSrcweir                             if ( xStg->isStreamElement( aName ) )
120cdf0e10cSrcweir                             {
121cdf0e10cSrcweir                                 uno::Reference < io::XStream > xStm = xStg->cloneStreamElement( aName );
122cdf0e10cSrcweir                                 pStream = utl::UcbStreamHelper::CreateStream( xStm );
123cdf0e10cSrcweir                                 bDeleteStream = sal_True;
124cdf0e10cSrcweir                             }
125cdf0e10cSrcweir                             else
126cdf0e10cSrcweir                             {
127cdf0e10cSrcweir                                 pStream = aTmp.GetStream( STREAM_STD_READWRITE );
128cdf0e10cSrcweir                                 uno::Reference < embed::XStorage > xStor = comphelper::OStorageHelper::GetStorageFromStream( new utl::OStreamWrapper( *pStream ) );
129cdf0e10cSrcweir                                 xStg->openStorageElement( aName, embed::ElementModes::READ )->copyToStorage( xStor );
130cdf0e10cSrcweir                             }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir                             ::com::sun::star::uno::Any                  aAny;
133cdf0e10cSrcweir                             const sal_uInt32                            nLen = pStream->Seek( STREAM_SEEK_TO_END );
134cdf0e10cSrcweir                             ::com::sun::star::uno::Sequence< sal_Int8 > aSeq( nLen );
135cdf0e10cSrcweir 
136cdf0e10cSrcweir                             pStream->Seek( STREAM_SEEK_TO_BEGIN );
137cdf0e10cSrcweir                             pStream->Read( aSeq.getArray(),  nLen );
138cdf0e10cSrcweir                             if ( bDeleteStream )
139cdf0e10cSrcweir                                 delete pStream;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir                             if( ( bRet = ( aSeq.getLength() > 0 ) ) == sal_True )
142cdf0e10cSrcweir                             {
143cdf0e10cSrcweir                                 aAny <<= aSeq;
144cdf0e10cSrcweir                                 SetAny( aAny, rFlavor );
145cdf0e10cSrcweir                             }
146cdf0e10cSrcweir                         }
147cdf0e10cSrcweir                         else
148cdf0e10cSrcweir                         {
149cdf0e10cSrcweir                             //TODO/LATER: how to handle objects without persistance?!
150cdf0e10cSrcweir                         }
151cdf0e10cSrcweir                     }
152cdf0e10cSrcweir                     catch ( uno::Exception& )
153cdf0e10cSrcweir                     {
154cdf0e10cSrcweir                     }
155cdf0e10cSrcweir                 }
156cdf0e10cSrcweir                 else if ( nFormat == FORMAT_GDIMETAFILE && m_pGraphic )
157cdf0e10cSrcweir 				{
158cdf0e10cSrcweir 					SvMemoryStream aMemStm( 65535, 65535 );
159cdf0e10cSrcweir                     aMemStm.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 					const GDIMetaFile& aMetaFile = m_pGraphic->GetGDIMetaFile();
162cdf0e10cSrcweir 					((GDIMetaFile*)(&aMetaFile))->Write( aMemStm );
163cdf0e10cSrcweir 					uno::Any aAny;
164cdf0e10cSrcweir 					aAny <<= uno::Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( aMemStm.GetData() ),
165cdf0e10cSrcweir 													aMemStm.Seek( STREAM_SEEK_TO_END ) );
166cdf0e10cSrcweir 					SetAny( aAny, rFlavor );
167cdf0e10cSrcweir 					bRet = sal_True;
168cdf0e10cSrcweir 				}
169cdf0e10cSrcweir 				else if ( m_xObj.is() && :: svt::EmbeddedObjectRef::TryRunningState( m_xObj ) )
170cdf0e10cSrcweir                 {
171cdf0e10cSrcweir                     uno::Reference< datatransfer::XTransferable > xTransferable( m_xObj->getComponent(), uno::UNO_QUERY );
172cdf0e10cSrcweir                     if ( xTransferable.is() )
173cdf0e10cSrcweir                     {
174cdf0e10cSrcweir                         uno::Any aAny = xTransferable->getTransferData( rFlavor );
175cdf0e10cSrcweir                         SetAny( aAny, rFlavor );
176cdf0e10cSrcweir                         bRet = sal_True;
177cdf0e10cSrcweir                     }
178cdf0e10cSrcweir                 }
179cdf0e10cSrcweir             }
180cdf0e10cSrcweir 		}
181cdf0e10cSrcweir 		catch( uno::Exception& )
182cdf0e10cSrcweir 		{
183cdf0e10cSrcweir 			// Error handling?
184cdf0e10cSrcweir 		}
185cdf0e10cSrcweir 	}
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	return bRet;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir // -----------------------------------------------------------------------------
191cdf0e10cSrcweir 
ObjectReleased()192cdf0e10cSrcweir void SvEmbedTransferHelper::ObjectReleased()
193cdf0e10cSrcweir {
194cdf0e10cSrcweir 	m_xObj = uno::Reference< embed::XEmbeddedObject >();
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
FillTransferableObjectDescriptor(TransferableObjectDescriptor & rDesc,const::com::sun::star::uno::Reference<::com::sun::star::embed::XEmbeddedObject> & xObj,Graphic * pGraphic,sal_Int64 nAspect)197cdf0e10cSrcweir void SvEmbedTransferHelper::FillTransferableObjectDescriptor( TransferableObjectDescriptor& rDesc,
198cdf0e10cSrcweir     const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XEmbeddedObject >& xObj,
199cdf0e10cSrcweir 	Graphic* pGraphic,
200cdf0e10cSrcweir 	sal_Int64 nAspect )
201cdf0e10cSrcweir {
202cdf0e10cSrcweir     //TODO/LATER: need TypeName to fill it into the Descriptor (will be shown in listbox)
203cdf0e10cSrcweir     ::com::sun::star::datatransfer::DataFlavor aFlavor;
204cdf0e10cSrcweir     SotExchange::GetFormatDataFlavor( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR, aFlavor );
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     rDesc.maClassName = SvGlobalName( xObj->getClassID() );
207cdf0e10cSrcweir     rDesc.maTypeName = aFlavor.HumanPresentableName;
208cdf0e10cSrcweir 
209cdf0e10cSrcweir     //TODO/LATER: the aspect size in the descriptor is wrong, unfortunately the stream
210cdf0e10cSrcweir 	// representation of the descriptor allows only 4 bytes for the aspect
211cdf0e10cSrcweir 	// so for internal transport something different should be found
212cdf0e10cSrcweir     rDesc.mnViewAspect = sal::static_int_cast<sal_uInt16>( nAspect );
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     //TODO/LATER: status needs to become sal_Int64
215cdf0e10cSrcweir     rDesc.mnOle2Misc = sal::static_int_cast<sal_Int32>(xObj->getStatus( rDesc.mnViewAspect ));
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 	Size aSize;
218cdf0e10cSrcweir 	MapMode aMapMode( MAP_100TH_MM );
219cdf0e10cSrcweir 	if ( nAspect == embed::Aspects::MSOLE_ICON )
220cdf0e10cSrcweir 	{
221cdf0e10cSrcweir 		if ( pGraphic )
222cdf0e10cSrcweir 		{
223cdf0e10cSrcweir 			aMapMode = pGraphic->GetPrefMapMode();
224cdf0e10cSrcweir 			aSize = pGraphic->GetPrefSize();
225cdf0e10cSrcweir 		}
226cdf0e10cSrcweir 		else
227cdf0e10cSrcweir 			aSize = Size( 2500, 2500 );
228cdf0e10cSrcweir 	}
229cdf0e10cSrcweir 	else
230cdf0e10cSrcweir 	{
231cdf0e10cSrcweir 		try
232cdf0e10cSrcweir 		{
233cdf0e10cSrcweir     		awt::Size aSz;
234cdf0e10cSrcweir 			aSz = xObj->getVisualAreaSize( rDesc.mnViewAspect );
235cdf0e10cSrcweir 			aSize = Size( aSz.Width, aSz.Height );
236cdf0e10cSrcweir 		}
237cdf0e10cSrcweir 		catch( embed::NoVisualAreaSizeException& )
238cdf0e10cSrcweir 		{
239cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Can not get visual area size!\n" );
240cdf0e10cSrcweir 			aSize = Size( 5000, 5000 );
241cdf0e10cSrcweir 		}
242cdf0e10cSrcweir 
243cdf0e10cSrcweir     	// TODO/LEAN: getMapUnit can switch object to running state
244cdf0e10cSrcweir     	aMapMode = MapMode( VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( rDesc.mnViewAspect ) ) );
245cdf0e10cSrcweir 	}
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     rDesc.maSize = OutputDevice::LogicToLogic( aSize, aMapMode, MapMode( MAP_100TH_MM ) );
248cdf0e10cSrcweir 	rDesc.maDragStartPos = Point();
249cdf0e10cSrcweir 	rDesc.maDisplayName = String();
250cdf0e10cSrcweir 	rDesc.mbCanLink = sal_False;
251cdf0e10cSrcweir }
252cdf0e10cSrcweir 
253