fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / embeddedobj / source / msole / olecomponent.cxx
blobedafa43b3fa00d9d37915cc9e09c6abc71ff7120
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/lang/IllegalArgumentException.hpp>
21 #include <com/sun/star/lang/DisposedException.hpp>
22 #include <com/sun/star/embed/WrongStateException.hpp>
23 #include <com/sun/star/embed/UnreachableStateException.hpp>
24 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/io/TempFile.hpp>
27 #include <com/sun/star/io/XTruncate.hpp>
28 #include <com/sun/star/awt/XRequestCallback.hpp>
30 #include <platform.h>
31 #include <cppuhelper/interfacecontainer.h>
32 #include <comphelper/mimeconfighelper.hxx>
33 #include <comphelper/processfactory.hxx>
34 #include <comphelper/storagehelper.hxx>
35 #include <osl/file.hxx>
36 #include <rtl/ref.hxx>
38 #include <olecomponent.hxx>
39 #include <olewrapclient.hxx>
40 #include <advisesink.hxx>
41 #include <oleembobj.hxx>
42 #include <mtnotification.hxx>
43 #include <boost/scoped_array.hpp>
45 using namespace ::com::sun::star;
46 using namespace ::comphelper;
47 #define MAX_ENUM_ELE 20
48 #define FORMATS_NUM 3
50 // ============ class ComSmart =====================
51 namespace {
53 template< class T > class ComSmart
55 T* m_pInterface;
57 void OwnRelease()
59 if ( m_pInterface )
61 T* pInterface = m_pInterface;
62 m_pInterface = NULL;
63 pInterface->Release();
67 public:
68 ComSmart()
69 : m_pInterface( NULL )
72 ComSmart( const ComSmart<T>& rObj )
73 : m_pInterface( rObj.m_pInterface )
75 if ( m_pInterface != NULL )
76 m_pInterface->AddRef();
79 ComSmart( T* pInterface )
80 : m_pInterface( pInterface )
82 if ( m_pInterface != NULL )
83 m_pInterface->AddRef();
86 ~ComSmart()
88 OwnRelease();
91 ComSmart& operator=( const ComSmart<T>& rObj )
93 OwnRelease();
95 m_pInterface = rObj.m_pInterface;
97 if ( m_pInterface != NULL )
98 m_pInterface->AddRef();
100 return *this;
103 ComSmart<T>& operator=( T* pInterface )
105 OwnRelease();
107 m_pInterface = pInterface;
109 if ( m_pInterface != NULL )
110 m_pInterface->AddRef();
112 return *this;
115 operator T*() const
117 return m_pInterface;
120 T& operator*() const
122 return *m_pInterface;
125 T** operator&()
127 OwnRelease();
129 m_pInterface = NULL;
131 return &m_pInterface;
134 T* operator->() const
136 return m_pInterface;
139 BOOL operator==( const ComSmart<T>& rObj ) const
141 return ( m_pInterface == rObj.m_pInterface );
144 BOOL operator!=( const ComSmart<T>& rObj ) const
146 return ( m_pInterface != rObj.m_pInterface );
149 BOOL operator==( const T* pInterface ) const
151 return ( m_pInterface == pInterface );
154 BOOL operator!=( const T* pInterface ) const
156 return ( m_pInterface != pInterface );
162 // ============ class ComSmart =====================
164 sal_Bool ConvertBufferToFormat( void* pBuf,
165 sal_uInt32 nBufSize,
166 const OUString& aFormatShortName,
167 uno::Any& aResult );
169 OUString GetNewTempFileURL_Impl( const uno::Reference< lang::XMultiServiceFactory >& xFactory ) throw( io::IOException );
171 typedef ::std::vector< FORMATETC* > FormatEtcList;
173 FORMATETC pFormatTemplates[FORMATS_NUM] = {
174 { CF_ENHMETAFILE, NULL, 0, -1, TYMED_ENHMF },
175 { CF_METAFILEPICT, NULL, 0, -1, TYMED_MFPICT },
176 { CF_BITMAP, NULL, 0, -1, TYMED_GDI } };
179 struct OleComponentNative_Impl {
180 ComSmart< IUnknown > m_pObj;
181 ComSmart< IOleObject > m_pOleObject;
182 ComSmart< IViewObject2 > m_pViewObject2;
183 ComSmart< IStorage > m_pIStorage;
184 FormatEtcList m_aFormatsList;
185 uno::Sequence< datatransfer::DataFlavor > m_aSupportedGraphFormats;
187 OleComponentNative_Impl()
189 // TODO: Extend format list
190 m_aSupportedGraphFormats.realloc( 5 );
192 m_aSupportedGraphFormats[0] = datatransfer::DataFlavor(
193 OUString( "application/x-openoffice-emf;windows_formatname=\"Image EMF\"" ),
194 OUString( "Windows Enhanced Metafile" ),
195 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
197 m_aSupportedGraphFormats[1] = datatransfer::DataFlavor(
198 OUString( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
199 OUString( "Windows Metafile" ),
200 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
202 m_aSupportedGraphFormats[2] = datatransfer::DataFlavor(
203 OUString( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ),
204 OUString( "Bitmap" ),
205 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
207 m_aSupportedGraphFormats[3] = datatransfer::DataFlavor(
208 OUString( "image/png" ),
209 OUString( "PNG" ),
210 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
212 m_aSupportedGraphFormats[0] = datatransfer::DataFlavor(
213 OUString( "application/x-openoffice-gdimetafile;windows_formatname=\"GDIMetaFile\"" ),
214 OUString( "GDIMetafile" ),
215 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
218 void AddSupportedFormat( const FORMATETC& aFormatEtc );
220 FORMATETC* GetSupportedFormatForAspect( sal_uInt32 nRequestedAspect );
222 sal_Bool ConvertDataForFlavor( const STGMEDIUM& aMedium,
223 const datatransfer::DataFlavor& aFlavor,
224 uno::Any& aResult );
226 sal_Bool GraphicalFlavor( const datatransfer::DataFlavor& aFlavor );
228 uno::Sequence< datatransfer::DataFlavor > GetFlavorsForAspects( sal_uInt32 nSupportedAspects );
232 DWORD GetAspectFromFlavor( const datatransfer::DataFlavor& aFlavor )
234 if ( aFlavor.MimeType.indexOf( ";Aspect=THUMBNAIL" ) != -1 )
235 return DVASPECT_THUMBNAIL;
236 else if ( aFlavor.MimeType.indexOf( ";Aspect=ICON" ) != -1 )
237 return DVASPECT_ICON;
238 else if ( aFlavor.MimeType.indexOf( ";Aspect=DOCPRINT" ) != -1 )
239 return DVASPECT_DOCPRINT;
240 else
241 return DVASPECT_CONTENT;
245 OUString GetFlavorSuffixFromAspect( DWORD nAsp )
247 OUString aResult;
249 if ( nAsp == DVASPECT_THUMBNAIL )
250 aResult = ";Aspect=THUMBNAIL";
251 else if ( nAsp == DVASPECT_ICON )
252 aResult = ";Aspect=ICON";
253 else if ( nAsp == DVASPECT_DOCPRINT )
254 aResult = ";Aspect=DOCPRINT";
256 // no suffix for DVASPECT_CONTENT
258 return aResult;
262 HRESULT OpenIStorageFromURL_Impl( const OUString& aURL, IStorage** ppIStorage )
264 OSL_ENSURE( ppIStorage, "The pointer must not be empty!" );
266 OUString aFilePath;
267 if ( !ppIStorage || ::osl::FileBase::getSystemPathFromFileURL( aURL, aFilePath ) != ::osl::FileBase::E_None )
268 throw uno::RuntimeException(); // TODO: something dangerous happened
270 return StgOpenStorage( reinterpret_cast<LPCWSTR>(aFilePath.getStr()),
271 NULL,
272 STGM_READWRITE | STGM_TRANSACTED, // | STGM_DELETEONRELEASE,
273 NULL,
275 ppIStorage );
279 sal_Bool OleComponentNative_Impl::ConvertDataForFlavor( const STGMEDIUM& aMedium,
280 const datatransfer::DataFlavor& aFlavor,
281 uno::Any& aResult )
283 sal_Bool bAnyIsReady = sal_False;
285 // try to convert data from Medium format to specified Flavor format
286 if ( aFlavor.DataType == cppu::UnoType<uno::Sequence< sal_Int8 >>::get() )
288 // first the GDI-metafile must be generated
290 boost::scoped_array<unsigned char> pBuf;
291 sal_uInt32 nBufSize = 0;
292 OUString aFormat;
294 if ( aMedium.tymed == TYMED_MFPICT ) // Win Metafile
296 aFormat = "image/x-wmf";
297 METAFILEPICT* pMF = ( METAFILEPICT* )GlobalLock( aMedium.hMetaFilePict );
298 if ( pMF )
300 nBufSize = GetMetaFileBitsEx( pMF->hMF, 0, NULL ) + 22;
301 pBuf.reset(new unsigned char[nBufSize]);
304 // TODO/LATER: the unit size must be calculated correctly
305 *( (long* )pBuf.get() ) = 0x9ac6cdd7L;
306 *( (short* )( pBuf.get()+6 )) = ( SHORT ) 0;
307 *( (short* )( pBuf.get()+8 )) = ( SHORT ) 0;
308 *( (short* )( pBuf.get()+10 )) = ( SHORT ) pMF->xExt;
309 *( (short* )( pBuf.get()+12 )) = ( SHORT ) pMF->yExt;
310 *( (short* )( pBuf.get()+14 )) = ( USHORT ) 2540;
313 if ( nBufSize && nBufSize == GetMetaFileBitsEx( pMF->hMF, nBufSize - 22, pBuf.get() + 22 ) )
315 if ( aFlavor.MimeType.matchAsciiL( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"", 57 ) )
317 aResult <<= uno::Sequence< sal_Int8 >( ( sal_Int8* )pBuf.get(), nBufSize );
318 bAnyIsReady = sal_True;
322 GlobalUnlock( aMedium.hMetaFilePict );
325 else if ( aMedium.tymed == TYMED_ENHMF ) // Enh Metafile
327 aFormat = "image/x-emf";
328 nBufSize = GetEnhMetaFileBits( aMedium.hEnhMetaFile, 0, NULL );
329 pBuf.reset(new unsigned char[nBufSize]);
330 if ( nBufSize && nBufSize == GetEnhMetaFileBits( aMedium.hEnhMetaFile, nBufSize, pBuf.get() ) )
332 if ( aFlavor.MimeType.matchAsciiL( "application/x-openoffice-emf;windows_formatname=\"Image EMF\"", 57 ) )
334 aResult <<= uno::Sequence< sal_Int8 >( ( sal_Int8* )pBuf.get(), nBufSize );
335 bAnyIsReady = sal_True;
339 else if ( aMedium.tymed == TYMED_GDI ) // Bitmap
341 aFormat = "image/x-MS-bmp";
342 nBufSize = GetBitmapBits( aMedium.hBitmap, 0, NULL );
343 pBuf.reset(new unsigned char[nBufSize]);
344 if ( nBufSize && nBufSize == sal::static_int_cast< ULONG >( GetBitmapBits( aMedium.hBitmap, nBufSize, pBuf.get() ) ) )
346 if ( aFlavor.MimeType.matchAsciiL( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"", 54 ) )
348 aResult <<= uno::Sequence< sal_Int8 >( ( sal_Int8* )pBuf.get(), nBufSize );
349 bAnyIsReady = sal_True;
354 if ( pBuf && !bAnyIsReady )
356 for ( sal_Int32 nInd = 0; nInd < m_aSupportedGraphFormats.getLength(); nInd++ )
357 if ( aFlavor.MimeType.match( m_aSupportedGraphFormats[nInd].MimeType )
358 && aFlavor.DataType == m_aSupportedGraphFormats[nInd].DataType
359 && aFlavor.DataType == cppu::UnoType<uno::Sequence< sal_Int8 >>::get() )
361 bAnyIsReady = ConvertBufferToFormat( ( void* )pBuf.get(), nBufSize, aFormat, aResult );
362 break;
367 return bAnyIsReady;
371 sal_Bool OleComponentNative_Impl::GraphicalFlavor( const datatransfer::DataFlavor& aFlavor )
373 // Actually all the required graphical formats must be supported
374 for ( sal_Int32 nInd = 0; nInd < m_aSupportedGraphFormats.getLength(); nInd++ )
375 if ( aFlavor.MimeType.match( m_aSupportedGraphFormats[nInd].MimeType )
376 && aFlavor.DataType == m_aSupportedGraphFormats[nInd].DataType )
377 return sal_True;
379 return sal_False;
383 sal_Bool GetClassIDFromSequence_Impl( uno::Sequence< sal_Int8 > aSeq, CLSID& aResult )
385 if ( aSeq.getLength() == 16 )
387 aResult.Data1 = ( ( ( ( ( ( sal_uInt8 )aSeq[0] << 8 ) + ( sal_uInt8 )aSeq[1] ) << 8 ) + ( sal_uInt8 )aSeq[2] ) << 8 ) + ( sal_uInt8 )aSeq[3];
388 aResult.Data2 = ( ( sal_uInt8 )aSeq[4] << 8 ) + ( sal_uInt8 )aSeq[5];
389 aResult.Data3 = ( ( sal_uInt8 )aSeq[6] << 8 ) + ( sal_uInt8 )aSeq[7];
390 for( int nInd = 0; nInd < 8; nInd++ )
391 aResult.Data4[nInd] = ( sal_uInt8 )aSeq[nInd+8];
393 return sal_True;
396 return sal_False;
400 OUString WinAccToVcl_Impl( const sal_Unicode* pStr )
402 OUString aResult;
404 if( pStr )
406 while ( *pStr )
408 if ( *pStr == '&' )
410 aResult += "~";
411 while( *( ++pStr ) == '&' );
413 else
415 aResult += OUString( pStr, 1 );
416 pStr++;
421 return aResult;
425 OleComponent::OleComponent( const uno::Reference< lang::XMultiServiceFactory >& xFactory, OleEmbeddedObject* pUnoOleObject )
426 : m_pInterfaceContainer( NULL )
427 , m_bDisposed( sal_False )
428 , m_bModified( sal_False )
429 , m_pNativeImpl( new OleComponentNative_Impl() )
430 , m_pUnoOleObject( pUnoOleObject )
431 , m_pOleWrapClientSite( NULL )
432 , m_pImplAdviseSink( NULL )
433 , m_nOLEMiscFlags( 0 )
434 , m_nAdvConn( 0 )
435 , m_xFactory( xFactory )
436 , m_bOleInitialized( sal_False )
437 , m_bWorkaroundActive( sal_False )
439 OSL_ENSURE( m_pUnoOleObject, "No owner object is provided!" );
441 HRESULT hr = OleInitialize( NULL );
442 OSL_ENSURE( hr == S_OK || hr == S_FALSE, "The ole can not be successfully initialized\n" );
443 if ( hr == S_OK || hr == S_FALSE )
444 m_bOleInitialized = sal_True;
446 m_pOleWrapClientSite = new OleWrapperClientSite( ( OleComponent* )this );
447 m_pOleWrapClientSite->AddRef();
449 m_pImplAdviseSink = new OleWrapperAdviseSink( ( OleComponent* )this );
450 m_pImplAdviseSink->AddRef();
455 OleComponent::~OleComponent()
457 OSL_ENSURE( !m_pOleWrapClientSite && !m_pImplAdviseSink && !m_pInterfaceContainer && !m_bOleInitialized,
458 "The object was not closed successfully! DISASTER is possible!" );
460 if ( m_pOleWrapClientSite || m_pImplAdviseSink || m_pInterfaceContainer || m_bOleInitialized )
462 ::osl::MutexGuard aGuard( m_aMutex );
463 m_refCount++;
464 try {
465 Dispose();
466 } catch( const uno::Exception& ) {}
469 for ( FormatEtcList::iterator aIter = m_pNativeImpl->m_aFormatsList.begin();
470 aIter != m_pNativeImpl->m_aFormatsList.end();
471 ++aIter )
473 delete (*aIter);
474 (*aIter) = NULL;
476 m_pNativeImpl->m_aFormatsList.clear();
478 delete m_pNativeImpl;
482 void OleComponentNative_Impl::AddSupportedFormat( const FORMATETC& aFormatEtc )
484 FORMATETC* pFormatToInsert = new FORMATETC( aFormatEtc );
485 m_aFormatsList.push_back( pFormatToInsert );
489 FORMATETC* OleComponentNative_Impl::GetSupportedFormatForAspect( sal_uInt32 nRequestedAspect )
491 for ( FormatEtcList::iterator aIter = m_aFormatsList.begin();
492 aIter != m_aFormatsList.end();
493 ++aIter )
494 if ( (*aIter) && (*aIter)->dwAspect == nRequestedAspect )
495 return (*aIter);
497 return NULL;
501 void OleComponent::Dispose()
503 // the mutex must be locked before this method is called
504 if ( m_bDisposed )
505 return;
507 CloseObject();
509 if ( m_pOleWrapClientSite )
511 m_pOleWrapClientSite->disconnectOleComponent();
512 m_pOleWrapClientSite->Release();
513 m_pOleWrapClientSite = NULL;
516 if ( m_pImplAdviseSink )
518 m_pImplAdviseSink->disconnectOleComponent();
519 m_pImplAdviseSink->Release();
520 m_pImplAdviseSink = NULL;
523 if ( m_pInterfaceContainer )
525 lang::EventObject aEvent( static_cast< ::cppu::OWeakObject* >( this ) );
526 m_pInterfaceContainer->disposeAndClear( aEvent );
528 delete m_pInterfaceContainer;
529 m_pInterfaceContainer = NULL;
532 if ( m_bOleInitialized )
534 // since the disposing can happen not only from main thread but also from a clipboard
535 // the deinitialization might lead to a disaster, SO7 does not deinitialize OLE at all
536 // so currently the same approach is selected as workaround
537 // OleUninitialize();
538 m_bOleInitialized = sal_False;
541 m_bDisposed = sal_True;
545 void OleComponent::disconnectEmbeddedObject()
547 // must not be called from destructor of UNO OLE object!!!
548 osl::MutexGuard aGuard( m_aMutex );
549 m_pUnoOleObject = NULL;
553 void OleComponent::CreateNewIStorage_Impl()
555 // TODO: in future a global memory could be used instead of file.
557 // write the stream to the temporary file
558 OUString aTempURL;
560 OSL_ENSURE( m_pUnoOleObject, "Unexpected object absence!" );
561 if ( m_pUnoOleObject )
562 aTempURL = m_pUnoOleObject->CreateTempURLEmpty_Impl();
563 else
564 aTempURL = GetNewTempFileURL_Impl( m_xFactory );
566 if ( !aTempURL.getLength() )
567 throw uno::RuntimeException(); // TODO
569 // open an IStorage based on the temporary file
570 OUString aTempFilePath;
571 if ( ::osl::FileBase::getSystemPathFromFileURL( aTempURL, aTempFilePath ) != ::osl::FileBase::E_None )
572 throw uno::RuntimeException(); // TODO: something dangerous happened
574 HRESULT hr = StgCreateDocfile( reinterpret_cast<LPCWSTR>(aTempFilePath.getStr()), STGM_CREATE | STGM_READWRITE | STGM_TRANSACTED | STGM_DELETEONRELEASE, 0, &m_pNativeImpl->m_pIStorage );
575 if ( FAILED( hr ) || !m_pNativeImpl->m_pIStorage )
576 throw io::IOException(); // TODO: transport error code?
580 uno::Sequence< datatransfer::DataFlavor > OleComponentNative_Impl::GetFlavorsForAspects( sal_uInt32 nSupportedAspects )
582 uno::Sequence< datatransfer::DataFlavor > aResult;
583 for ( sal_uInt32 nAsp = 1; nAsp <= 8; nAsp *= 2 )
584 if ( ( nSupportedAspects & nAsp ) == nAsp )
586 OUString aAspectSuffix = GetFlavorSuffixFromAspect( nAsp );
588 sal_Int32 nLength = aResult.getLength();
589 aResult.realloc( nLength + m_aSupportedGraphFormats.getLength() );
591 for ( sal_Int32 nInd = 0; nInd < m_aSupportedGraphFormats.getLength(); nInd++ )
593 aResult[nLength + nInd].MimeType = m_aSupportedGraphFormats[nInd].MimeType + aAspectSuffix;
594 aResult[nLength + nInd].HumanPresentableName = m_aSupportedGraphFormats[nInd].HumanPresentableName;
595 aResult[nLength + nInd].DataType = m_aSupportedGraphFormats[nInd].DataType;
599 return aResult;
603 void OleComponent::RetrieveObjectDataFlavors_Impl()
605 if ( !m_pNativeImpl->m_pOleObject )
606 throw embed::WrongStateException(); // TODO: the object is in wrong state
608 if ( !m_aDataFlavors.getLength() )
610 ComSmart< IDataObject > pDataObject;
611 HRESULT hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IDataObject, (void**)&pDataObject );
612 if ( SUCCEEDED( hr ) && pDataObject )
614 ComSmart< IEnumFORMATETC > pFormatEnum;
615 hr = pDataObject->EnumFormatEtc( DATADIR_GET, &pFormatEnum );
616 if ( SUCCEEDED( hr ) && pFormatEnum )
618 FORMATETC pElem[ MAX_ENUM_ELE ];
619 ULONG nNum = 0;
621 // if it is possible to retrieve at least one supported graphical format for an aspect
622 // this format can be converted to other supported formats
623 sal_uInt32 nSupportedAspects = 0;
626 HRESULT hr2 = pFormatEnum->Next( MAX_ENUM_ELE, pElem, &nNum );
627 if( hr2 == S_OK || hr2 == S_FALSE )
629 for( sal_uInt32 nInd = 0; nInd < FORMATS_NUM; nInd++ )
631 if ( pElem[nInd].cfFormat == pFormatTemplates[nInd].cfFormat
632 && pElem[nInd].tymed == pFormatTemplates[nInd].tymed )
633 nSupportedAspects |= pElem[nInd].dwAspect;
636 else
637 break;
639 while( nNum == MAX_ENUM_ELE );
641 m_aDataFlavors = m_pNativeImpl->GetFlavorsForAspects( nSupportedAspects );
645 if ( !m_aDataFlavors.getLength() )
647 // TODO:
648 // for any reason the object could not provide this information
649 // try to get access to the cached representation
655 sal_Bool OleComponent::InitializeObject_Impl()
656 // There will be no static objects!
658 if ( !m_pNativeImpl->m_pObj )
659 return sal_False;
661 // the linked object will be detected here
662 ComSmart< IOleLink > pOleLink;
663 HRESULT hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IOleLink, (void**)&pOleLink );
664 OSL_ENSURE( m_pUnoOleObject, "Unexpected object absence!" );
665 if ( m_pUnoOleObject )
666 m_pUnoOleObject->SetObjectIsLink_Impl( sal_Bool( pOleLink != NULL ) );
669 hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IViewObject2, (void**)&m_pNativeImpl->m_pViewObject2 );
670 if ( FAILED( hr ) || !m_pNativeImpl->m_pViewObject2 )
671 return sal_False;
673 // remove all the caches
674 IOleCache* pIOleCache = NULL;
675 if ( SUCCEEDED( m_pNativeImpl->m_pObj->QueryInterface( IID_IOleCache, (void**)&pIOleCache ) ) && pIOleCache )
677 IEnumSTATDATA* pEnumSD = NULL;
678 HRESULT hr2 = pIOleCache->EnumCache( &pEnumSD );
680 if ( SUCCEEDED( hr2 ) && pEnumSD )
682 pEnumSD->Reset();
683 STATDATA aSD;
684 DWORD nNum;
685 while( SUCCEEDED( pEnumSD->Next( 1, &aSD, &nNum ) ) && nNum == 1 )
686 hr2 = pIOleCache->Uncache( aSD.dwConnection );
689 // No IDataObject implementation, caching must be used instead
690 DWORD nConn;
691 FORMATETC aFormat = { 0, 0, DVASPECT_CONTENT, -1, TYMED_MFPICT };
692 hr2 = pIOleCache->Cache( &aFormat, ADVFCACHE_ONSAVE, &nConn );
694 pIOleCache->Release();
695 pIOleCache = NULL;
698 hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IOleObject, (void**)&m_pNativeImpl->m_pOleObject );
699 if ( FAILED( hr ) || !m_pNativeImpl->m_pOleObject )
700 return sal_False; // Static objects are not supported, they should be inserted as graphics
702 m_pNativeImpl->m_pOleObject->GetMiscStatus( DVASPECT_CONTENT, ( DWORD* )&m_nOLEMiscFlags );
703 // TODO: use other misc flags also
704 // the object should have drawable aspect even in case it supports only iconic representation
705 // if ( m_nOLEMiscFlags & OLEMISC_ONLYICONIC )
707 m_pNativeImpl->m_pOleObject->SetClientSite( m_pOleWrapClientSite );
709 // the only need in this registration is workaround for close notification
710 m_pNativeImpl->m_pOleObject->Advise( m_pImplAdviseSink, ( DWORD* )&m_nAdvConn );
711 m_pNativeImpl->m_pViewObject2->SetAdvise( DVASPECT_CONTENT, 0, m_pImplAdviseSink );
713 OleSetContainedObject( m_pNativeImpl->m_pOleObject, TRUE );
715 return sal_True;
719 void OleComponent::LoadEmbeddedObject( const OUString& aTempURL )
721 if ( !aTempURL.getLength() )
722 throw lang::IllegalArgumentException(); // TODO
724 if ( m_pNativeImpl->m_pIStorage )
725 throw io::IOException(); // TODO the object is already initialized or wrong initialization is done
727 // open an IStorage based on the temporary file
728 HRESULT hr = OpenIStorageFromURL_Impl( aTempURL, &m_pNativeImpl->m_pIStorage );
730 if ( FAILED( hr ) || !m_pNativeImpl->m_pIStorage )
731 throw io::IOException(); // TODO: transport error code?
733 hr = OleLoad( m_pNativeImpl->m_pIStorage, IID_IUnknown, NULL, (void**)&m_pNativeImpl->m_pObj );
734 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
736 throw uno::RuntimeException();
739 if ( !InitializeObject_Impl() )
740 throw uno::RuntimeException(); // TODO
744 void OleComponent::CreateObjectFromClipboard()
746 if ( m_pNativeImpl->m_pIStorage )
747 throw io::IOException(); // TODO:the object is already initialized
749 CreateNewIStorage_Impl();
750 if ( !m_pNativeImpl->m_pIStorage )
751 throw uno::RuntimeException(); // TODO
753 IDataObject * pDO = NULL;
754 HRESULT hr = OleGetClipboard( &pDO );
755 if( SUCCEEDED( hr ) && pDO )
757 hr = OleQueryCreateFromData( pDO );
758 if( S_OK == GetScode( hr ) )
760 hr = OleCreateFromData( pDO,
761 IID_IUnknown,
762 OLERENDER_DRAW, // OLERENDER_FORMAT
763 NULL, // &aFormat,
764 NULL,
765 m_pNativeImpl->m_pIStorage,
766 (void**)&m_pNativeImpl->m_pObj );
768 else
770 // Static objects are not supported
771 pDO->Release();
775 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
776 throw uno::RuntimeException();
778 if ( !InitializeObject_Impl() )
779 throw uno::RuntimeException(); // TODO
783 void OleComponent::CreateNewEmbeddedObject( const uno::Sequence< sal_Int8 >& aSeqCLSID )
785 CLSID aClsID;
787 if ( !GetClassIDFromSequence_Impl( aSeqCLSID, aClsID ) )
788 throw lang::IllegalArgumentException(); // TODO
790 if ( m_pNativeImpl->m_pIStorage )
791 throw io::IOException(); // TODO:the object is already initialized
793 CreateNewIStorage_Impl();
794 if ( !m_pNativeImpl->m_pIStorage )
795 throw uno::RuntimeException(); // TODO
797 // FORMATETC aFormat = { CF_METAFILEPICT, NULL, nAspect, -1, TYMED_MFPICT }; // for OLE..._DRAW should be NULL
799 HRESULT hr = OleCreate( aClsID,
800 IID_IUnknown,
801 OLERENDER_DRAW, // OLERENDER_FORMAT
802 NULL, // &aFormat,
803 NULL,
804 m_pNativeImpl->m_pIStorage,
805 (void**)&m_pNativeImpl->m_pObj );
807 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
808 throw uno::RuntimeException(); // TODO
810 if ( !InitializeObject_Impl() )
811 throw uno::RuntimeException(); // TODO
813 // TODO: getExtent???
817 void OleComponent::CreateObjectFromData( const uno::Reference< datatransfer::XTransferable >& )
818 // Static objects are not supported, they should be inserted as graphics
820 // TODO: May be this call is useless since there are no static objects
821 // and nonstatic objects will be created based on OLEstorage ( stream ).
822 // ???
824 // OleQueryCreateFromData...
828 void OleComponent::CreateObjectFromFile( const OUString& aFileURL )
830 if ( m_pNativeImpl->m_pIStorage )
831 throw io::IOException(); // TODO:the object is already initialized
833 CreateNewIStorage_Impl();
834 if ( !m_pNativeImpl->m_pIStorage )
835 throw uno::RuntimeException(); // TODO:
837 OUString aFilePath;
838 if ( ::osl::FileBase::getSystemPathFromFileURL( aFileURL, aFilePath ) != ::osl::FileBase::E_None )
839 throw uno::RuntimeException(); // TODO: something dangerous happened
841 HRESULT hr = OleCreateFromFile( CLSID_NULL,
842 reinterpret_cast<LPCWSTR>(aFilePath.getStr()),
843 IID_IUnknown,
844 OLERENDER_DRAW, // OLERENDER_FORMAT
845 NULL,
846 NULL,
847 m_pNativeImpl->m_pIStorage,
848 (void**)&m_pNativeImpl->m_pObj );
850 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
851 throw uno::RuntimeException(); // TODO
853 if ( !InitializeObject_Impl() )
854 throw uno::RuntimeException(); // TODO
858 void OleComponent::CreateLinkFromFile( const OUString& aFileURL )
860 if ( m_pNativeImpl->m_pIStorage )
861 throw io::IOException(); // TODO:the object is already initialized
863 CreateNewIStorage_Impl();
864 if ( !m_pNativeImpl->m_pIStorage )
865 throw uno::RuntimeException(); // TODO:
867 OUString aFilePath;
868 if ( ::osl::FileBase::getSystemPathFromFileURL( aFileURL, aFilePath ) != ::osl::FileBase::E_None )
869 throw uno::RuntimeException(); // TODO: something dangerous happened
871 HRESULT hr = OleCreateLinkToFile( reinterpret_cast<LPCWSTR>(aFilePath.getStr()),
872 IID_IUnknown,
873 OLERENDER_DRAW, // OLERENDER_FORMAT
874 NULL,
875 NULL,
876 m_pNativeImpl->m_pIStorage,
877 (void**)&m_pNativeImpl->m_pObj );
879 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
880 throw uno::RuntimeException(); // TODO
882 if ( !InitializeObject_Impl() )
883 throw uno::RuntimeException(); // TODO
887 void OleComponent::InitEmbeddedCopyOfLink( OleComponent* pOleLinkComponent )
889 if ( !pOleLinkComponent || !pOleLinkComponent->m_pNativeImpl->m_pObj )
890 throw lang::IllegalArgumentException(); // TODO
892 if ( m_pNativeImpl->m_pIStorage )
893 throw io::IOException(); // TODO:the object is already initialized
895 ComSmart< IDataObject > pDataObject;
896 HRESULT hr = pOleLinkComponent->m_pNativeImpl->m_pObj->QueryInterface( IID_IDataObject, (void**)&pDataObject );
897 if ( SUCCEEDED( hr ) && pDataObject && SUCCEEDED( OleQueryCreateFromData( pDataObject ) ) )
899 // the object must be already disconnected from the temporary URL
900 CreateNewIStorage_Impl();
901 if ( !m_pNativeImpl->m_pIStorage )
902 throw uno::RuntimeException(); // TODO:
904 hr = OleCreateFromData( pDataObject,
905 IID_IUnknown,
906 OLERENDER_DRAW,
907 NULL,
908 NULL,
909 m_pNativeImpl->m_pIStorage,
910 (void**)&m_pNativeImpl->m_pObj );
913 if ( !m_pNativeImpl->m_pObj )
915 ComSmart< IOleLink > pOleLink;
916 hr = pOleLinkComponent->m_pNativeImpl->m_pObj->QueryInterface( IID_IOleLink, (void**)&pOleLink );
917 if ( FAILED( hr ) || !pOleLink )
918 throw io::IOException(); // TODO: the object doesn't support IOleLink
920 ComSmart< IMoniker > pMoniker;
921 hr = pOleLink->GetSourceMoniker( &pMoniker );
922 if ( FAILED( hr ) || !pMoniker )
923 throw io::IOException(); // TODO: can not retrieve moniker
925 // In case of file moniker life is easy : )
926 DWORD aMonType = 0;
927 hr = pMoniker->IsSystemMoniker( &aMonType );
928 if ( SUCCEEDED( hr ) && aMonType == MKSYS_FILEMONIKER )
930 ComSmart< IMalloc > pMalloc;
931 CoGetMalloc( 1, &pMalloc ); // if fails there will be a memory leak
932 OSL_ENSURE( pMalloc, "CoGetMalloc() failed!" );
934 LPOLESTR pOleStr = NULL;
935 hr = pOleLink->GetSourceDisplayName( &pOleStr );
936 if ( SUCCEEDED( hr ) && pOleStr )
938 OUString aFilePath( ( sal_Unicode* )pOleStr );
939 if ( pMalloc )
940 pMalloc->Free( ( void* )pOleStr );
942 hr = OleCreateFromFile( CLSID_NULL,
943 reinterpret_cast<LPCWSTR>(aFilePath.getStr()),
944 IID_IUnknown,
945 OLERENDER_DRAW, // OLERENDER_FORMAT
946 NULL,
947 NULL,
948 m_pNativeImpl->m_pIStorage,
949 (void**)&m_pNativeImpl->m_pObj );
953 // in case of other moniker types the only way is to get storage
954 if ( !m_pNativeImpl->m_pObj )
956 ComSmart< IBindCtx > pBindCtx;
957 hr = CreateBindCtx( 0, ( LPBC FAR* )&pBindCtx );
958 if ( SUCCEEDED( hr ) && pBindCtx )
960 ComSmart< IStorage > pObjectStorage;
961 hr = pMoniker->BindToStorage( pBindCtx, NULL, IID_IStorage, (void**)&pObjectStorage );
962 if ( SUCCEEDED( hr ) && pObjectStorage )
964 hr = pObjectStorage->CopyTo( 0, NULL, NULL, m_pNativeImpl->m_pIStorage );
965 if ( SUCCEEDED( hr ) )
966 hr = OleLoad( m_pNativeImpl->m_pIStorage, IID_IUnknown, NULL, (void**)&m_pNativeImpl->m_pObj );
972 // If object could not be created the only way is to use graphical representation
973 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
974 throw uno::RuntimeException(); // TODO
976 if ( !InitializeObject_Impl() )
977 throw uno::RuntimeException(); // TODO
981 void OleComponent::RunObject()
983 OSL_ENSURE( m_pNativeImpl->m_pOleObject, "The pointer can not be set to NULL here!\n" );
984 if ( !m_pNativeImpl->m_pOleObject )
985 throw embed::WrongStateException(); // TODO: the object is in wrong state
987 if ( !OleIsRunning( m_pNativeImpl->m_pOleObject ) )
989 HRESULT hr = OleRun( m_pNativeImpl->m_pObj );
991 if ( FAILED( hr ) )
993 if ( hr == REGDB_E_CLASSNOTREG )
994 throw embed::UnreachableStateException(); // the object server is not installed
995 else
996 throw io::IOException();
1002 awt::Size OleComponent::CalculateWithFactor( const awt::Size& aSize,
1003 const awt::Size& aMultiplier,
1004 const awt::Size& aDivisor )
1006 awt::Size aResult;
1008 sal_Int64 nWidth = (sal_Int64)aSize.Width * (sal_Int64)aMultiplier.Width / (sal_Int64)aDivisor.Width;
1009 sal_Int64 nHeight = (sal_Int64)aSize.Height * (sal_Int64)aMultiplier.Height / (sal_Int64)aDivisor.Height;
1010 OSL_ENSURE( nWidth < SAL_MAX_INT32 && nWidth > SAL_MIN_INT32
1011 && nHeight < SAL_MAX_INT32 && nHeight > SAL_MIN_INT32,
1012 "Unacceptable result size!" );
1014 aResult.Width = (sal_Int32)nWidth;
1015 aResult.Height = (sal_Int32)nHeight;
1017 return aResult;
1021 void OleComponent::CloseObject()
1023 if ( m_pNativeImpl->m_pOleObject && OleIsRunning( m_pNativeImpl->m_pOleObject ) )
1024 m_pNativeImpl->m_pOleObject->Close( OLECLOSE_NOSAVE ); // must be saved before
1028 uno::Sequence< embed::VerbDescriptor > OleComponent::GetVerbList()
1030 if ( !m_pNativeImpl->m_pOleObject )
1031 throw embed::WrongStateException(); // TODO: the object is in wrong state
1033 if( !m_aVerbList.getLength() )
1035 ComSmart< IEnumOLEVERB > pEnum;
1036 if( SUCCEEDED( m_pNativeImpl->m_pOleObject->EnumVerbs( &pEnum ) ) )
1038 OLEVERB szEle[ MAX_ENUM_ELE ];
1039 ULONG nNum = 0;
1040 sal_Int32 nSeqSize = 0;
1044 HRESULT hr = pEnum->Next( MAX_ENUM_ELE, szEle, &nNum );
1045 if( hr == S_OK || hr == S_FALSE )
1047 m_aVerbList.realloc( nSeqSize += nNum );
1048 for( sal_uInt32 nInd = 0; nInd < nNum; nInd++ )
1050 m_aVerbList[nSeqSize-nNum+nInd].VerbID = szEle[ nInd ].lVerb;
1051 m_aVerbList[nSeqSize-nNum+nInd].VerbName = WinAccToVcl_Impl( reinterpret_cast<const sal_Unicode*>(szEle[ nInd ].lpszVerbName) );
1052 m_aVerbList[nSeqSize-nNum+nInd].VerbFlags = szEle[ nInd ].fuFlags;
1053 m_aVerbList[nSeqSize-nNum+nInd].VerbAttributes = szEle[ nInd ].grfAttribs;
1056 else
1057 break;
1059 while( nNum == MAX_ENUM_ELE );
1063 return m_aVerbList;
1067 void OleComponent::ExecuteVerb( sal_Int32 nVerbID )
1069 if ( !m_pNativeImpl->m_pOleObject )
1070 throw embed::WrongStateException(); // TODO
1072 HRESULT hr = OleRun( m_pNativeImpl->m_pOleObject );
1073 if ( FAILED( hr ) )
1074 throw io::IOException(); // TODO: a specific exception that transport error code can be thrown here
1076 // TODO: probably extents should be set here and stored in aRect
1077 // TODO: probably the parent window also should be set
1078 hr = m_pNativeImpl->m_pOleObject->DoVerb( nVerbID, NULL, m_pOleWrapClientSite, 0, NULL, NULL );
1080 if ( FAILED( hr ) )
1081 throw io::IOException(); // TODO
1083 // TODO/LATER: the real names should be used here
1084 m_pNativeImpl->m_pOleObject->SetHostNames( L"app name", L"untitled" );
1088 void OleComponent::SetHostName( const OUString&,
1089 const OUString& )
1091 if ( !m_pNativeImpl->m_pOleObject )
1092 throw embed::WrongStateException(); // TODO: the object is in wrong state
1094 // TODO: use aContName and aEmbDocName in m_pNativeImpl->m_pOleObject->SetHostNames()
1098 void OleComponent::SetExtent( const awt::Size& aVisAreaSize, sal_Int64 nAspect )
1100 if ( !m_pNativeImpl->m_pOleObject )
1101 throw embed::WrongStateException(); // TODO: the object is in wrong state
1103 DWORD nMSAspect = ( DWORD )nAspect; // first 32 bits are for MS aspects
1105 SIZEL aSize = { aVisAreaSize.Width, aVisAreaSize.Height };
1106 HRESULT hr = m_pNativeImpl->m_pOleObject->SetExtent( nMSAspect, &aSize );
1108 if ( FAILED( hr ) )
1110 // TODO/LATER: is it correct? In future user code probably should be ready for the exception.
1111 // if the object is running but not activated, RPC_E_SERVER_DIED error code is returned by OLE package
1112 // in this case just do nothing
1113 // Also Visio returns E_FAIL on resize if it is in running state
1114 // if ( hr != RPC_E_SERVER_DIED )
1115 throw io::IOException(); // TODO
1120 awt::Size OleComponent::GetExtent( sal_Int64 nAspect )
1122 if ( !m_pNativeImpl->m_pOleObject )
1123 throw embed::WrongStateException(); // TODO: the object is in wrong state
1125 DWORD nMSAspect = ( DWORD )nAspect; // first 32 bits are for MS aspects
1126 awt::Size aSize;
1127 sal_Bool bGotSize = sal_False;
1129 if ( nMSAspect == DVASPECT_CONTENT )
1131 // Try to get the size from the replacement image first
1132 ComSmart< IDataObject > pDataObject;
1133 HRESULT hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IDataObject, (void**)&pDataObject );
1134 if ( SUCCEEDED( hr ) || pDataObject )
1136 STGMEDIUM aMedium;
1137 FORMATETC aFormat = pFormatTemplates[1]; // use windows metafile format
1138 aFormat.dwAspect = nMSAspect;
1140 hr = pDataObject->GetData( &aFormat, &aMedium );
1141 if ( SUCCEEDED( hr ) && aMedium.tymed == TYMED_MFPICT ) // Win Metafile
1143 METAFILEPICT* pMF = ( METAFILEPICT* )GlobalLock( aMedium.hMetaFilePict );
1144 if ( pMF )
1146 // the object uses 0.01 mm as unit, so the metafile size should be converted to object unit
1147 sal_Int64 nMult = 1;
1148 sal_Int64 nDiv = 1;
1149 switch( pMF->mm )
1151 case MM_HIENGLISH:
1152 nMult = 254;
1153 nDiv = 100;
1154 break;
1156 case MM_LOENGLISH:
1157 nMult = 254;
1158 nDiv = 10;
1159 break;
1161 case MM_LOMETRIC:
1162 nMult = 10;
1163 break;
1165 case MM_TWIPS:
1166 nMult = 254;
1167 nDiv = 144;
1168 break;
1170 case MM_ISOTROPIC:
1171 case MM_ANISOTROPIC:
1172 case MM_HIMETRIC:
1173 // do nothing
1174 break;
1177 sal_Int64 nX = ( (sal_Int64)abs( pMF->xExt ) ) * nMult / nDiv;
1178 sal_Int64 nY = ( (sal_Int64)abs( pMF->yExt ) ) * nMult / nDiv;
1179 if ( nX < SAL_MAX_INT32 && nY < SAL_MAX_INT32 )
1181 aSize.Width = ( sal_Int32 )nX;
1182 aSize.Height = ( sal_Int32 )nY;
1183 bGotSize = sal_True;
1185 else
1186 OSL_FAIL( "Unexpected size is provided!" );
1189 // i113605, to release storage medium
1190 if ( SUCCEEDED( hr ) )
1191 ::ReleaseStgMedium(&aMedium);
1195 if ( !bGotSize )
1196 throw lang::IllegalArgumentException();
1198 return aSize;
1202 awt::Size OleComponent::GetCachedExtent( sal_Int64 nAspect )
1204 if ( !m_pNativeImpl->m_pOleObject )
1205 throw embed::WrongStateException(); // TODO: the object is in wrong state
1207 DWORD nMSAspect = ( DWORD )nAspect; // first 32 bits are for MS aspects
1208 SIZEL aSize;
1210 HRESULT hr = m_pNativeImpl->m_pViewObject2->GetExtent( nMSAspect, -1, NULL, &aSize );
1212 if ( FAILED( hr ) )
1214 // TODO/LATER: is it correct?
1215 // if there is no appropriate cache for the aspect, OLE_E_BLANK error code is returned
1216 // if ( hr == OLE_E_BLANK )
1217 // throw lang::IllegalArgumentException();
1218 //else
1219 // throw io::IOException(); // TODO
1221 throw lang::IllegalArgumentException();
1224 return awt::Size( aSize.cx, aSize.cy );
1228 awt::Size OleComponent::GetReccomendedExtent( sal_Int64 nAspect )
1230 if ( !m_pNativeImpl->m_pOleObject )
1231 throw embed::WrongStateException(); // TODO: the object is in wrong state
1233 DWORD nMSAspect = ( DWORD )nAspect; // first 32 bits are for MS aspects
1234 SIZEL aSize;
1235 HRESULT hr = m_pNativeImpl->m_pOleObject->GetExtent( nMSAspect, &aSize );
1236 if ( FAILED( hr ) )
1237 throw lang::IllegalArgumentException();
1239 return awt::Size( aSize.cx, aSize.cy );
1243 sal_Int64 OleComponent::GetMiscStatus( sal_Int64 nAspect )
1245 if ( !m_pNativeImpl->m_pOleObject )
1246 throw embed::WrongStateException(); // TODO: the object is in wrong state
1248 sal_uInt32 nResult;
1249 m_pNativeImpl->m_pOleObject->GetMiscStatus( ( DWORD )nAspect, ( DWORD* )&nResult );
1250 return ( sal_Int64 )nResult; // first 32 bits are for MS flags
1254 uno::Sequence< sal_Int8 > OleComponent::GetCLSID()
1256 if ( !m_pNativeImpl->m_pOleObject )
1257 throw embed::WrongStateException(); // TODO: the object is in wrong state
1259 GUID aCLSID;
1260 HRESULT hr = m_pNativeImpl->m_pOleObject->GetUserClassID( &aCLSID );
1261 if ( FAILED( hr ) )
1262 throw io::IOException(); // TODO:
1264 return MimeConfigurationHelper::GetSequenceClassID( aCLSID.Data1, aCLSID.Data2, aCLSID.Data3,
1265 aCLSID.Data4[0], aCLSID.Data4[1],
1266 aCLSID.Data4[2], aCLSID.Data4[3],
1267 aCLSID.Data4[4], aCLSID.Data4[5],
1268 aCLSID.Data4[6], aCLSID.Data4[7] );
1272 sal_Bool OleComponent::IsDirty()
1274 if ( !m_pNativeImpl->m_pOleObject )
1275 throw embed::WrongStateException(); // TODO: the object is in wrong state
1277 if ( IsWorkaroundActive() )
1278 return sal_True;
1280 ComSmart< IPersistStorage > pPersistStorage;
1281 HRESULT hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IPersistStorage, (void**)&pPersistStorage );
1282 if ( FAILED( hr ) || !pPersistStorage )
1283 throw io::IOException(); // TODO
1285 hr = pPersistStorage->IsDirty();
1286 return ( hr != S_FALSE );
1290 void OleComponent::StoreOwnTmpIfNecessary()
1292 if ( !m_pNativeImpl->m_pOleObject )
1293 throw embed::WrongStateException(); // TODO: the object is in wrong state
1295 ComSmart< IPersistStorage > pPersistStorage;
1296 HRESULT hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IPersistStorage, (void**)&pPersistStorage );
1297 if ( FAILED( hr ) || !pPersistStorage )
1298 throw io::IOException(); // TODO
1300 if ( m_bWorkaroundActive || pPersistStorage->IsDirty() != S_FALSE )
1302 hr = OleSave( pPersistStorage, m_pNativeImpl->m_pIStorage, TRUE );
1303 if ( FAILED( hr ) )
1305 // Till now was required only for AcrobatReader7.0.8
1306 GUID aCLSID;
1307 hr = m_pNativeImpl->m_pOleObject->GetUserClassID( &aCLSID );
1308 if ( FAILED( hr ) )
1309 throw io::IOException(); // TODO
1311 hr = WriteClassStg( m_pNativeImpl->m_pIStorage, aCLSID );
1312 if ( FAILED( hr ) )
1313 throw io::IOException(); // TODO
1315 // the result of the following call is not checked because some objects, for example AcrobatReader7.0.8
1316 // return error even in case the saving was done correctly
1317 hr = pPersistStorage->Save( m_pNativeImpl->m_pIStorage, TRUE );
1319 // another workaround for AcrobatReader7.0.8 object, this object might think that it is not changed
1320 // when it has been created from file, although it must be saved
1321 m_bWorkaroundActive = sal_True;
1324 hr = m_pNativeImpl->m_pIStorage->Commit( STGC_DEFAULT );
1325 if ( FAILED( hr ) )
1326 throw io::IOException(); // TODO
1328 hr = pPersistStorage->SaveCompleted( NULL );
1329 if ( FAILED( hr ) && hr != E_UNEXPECTED )
1330 throw io::IOException(); // TODO
1336 sal_Bool OleComponent::SaveObject_Impl()
1338 sal_Bool bResult = sal_False;
1339 OleEmbeddedObject* pLockObject = NULL;
1342 osl::MutexGuard aGuard( m_aMutex );
1343 if ( m_pUnoOleObject )
1345 pLockObject = m_pUnoOleObject;
1346 pLockObject->acquire();
1350 if ( pLockObject )
1352 bResult = pLockObject->SaveObject_Impl();
1353 pLockObject->release();
1356 return bResult;
1360 sal_Bool OleComponent::OnShowWindow_Impl( bool bShow )
1362 sal_Bool bResult = sal_False;
1363 OleEmbeddedObject* pLockObject = NULL;
1366 osl::MutexGuard aGuard( m_aMutex );
1368 if ( m_pUnoOleObject )
1370 pLockObject = m_pUnoOleObject;
1371 pLockObject->acquire();
1375 if ( pLockObject )
1377 bResult = pLockObject->OnShowWindow_Impl( bShow );
1378 pLockObject->release();
1381 return bResult;
1385 void OleComponent::OnViewChange_Impl( sal_uInt32 dwAspect )
1387 // TODO: check if it is enough or may be saving notifications are required for Visio2000
1388 ::rtl::Reference< OleEmbeddedObject > xLockObject;
1391 osl::MutexGuard aGuard( m_aMutex );
1392 if ( m_pUnoOleObject )
1393 xLockObject = m_pUnoOleObject;
1396 if ( xLockObject.is() )
1398 uno::Reference < awt::XRequestCallback > xRequestCallback(
1399 m_xFactory->createInstance(
1400 OUString("com.sun.star.awt.AsyncCallback")),
1401 uno::UNO_QUERY );
1402 xRequestCallback->addCallback( new MainThreadNotificationRequest( xLockObject, OLECOMP_ONVIEWCHANGE, dwAspect ), uno::Any() );
1407 void OleComponent::OnClose_Impl()
1409 ::rtl::Reference< OleEmbeddedObject > xLockObject;
1412 osl::MutexGuard aGuard( m_aMutex );
1413 if ( m_pUnoOleObject )
1414 xLockObject = m_pUnoOleObject;
1417 if ( xLockObject.is() )
1419 uno::Reference < awt::XRequestCallback > xRequestCallback(
1420 m_xFactory->createInstance(
1421 OUString("com.sun.star.awt.AsyncCallback")),
1422 uno::UNO_QUERY );
1423 xRequestCallback->addCallback( new MainThreadNotificationRequest( xLockObject, OLECOMP_ONCLOSE ), uno::Any() );
1427 // XCloseable
1429 void SAL_CALL OleComponent::close( sal_Bool bDeliverOwnership )
1430 throw ( util::CloseVetoException,
1431 uno::RuntimeException )
1433 ::osl::MutexGuard aGuard( m_aMutex );
1434 if ( m_bDisposed )
1435 throw lang::DisposedException(); // TODO
1437 uno::Reference< uno::XInterface > xSelfHold( static_cast< ::cppu::OWeakObject* >( this ) );
1438 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >( this ) );
1440 if ( m_pInterfaceContainer )
1442 ::cppu::OInterfaceContainerHelper* pContainer =
1443 m_pInterfaceContainer->getContainer( cppu::UnoType<util::XCloseListener>::get());
1444 if ( pContainer != NULL )
1446 ::cppu::OInterfaceIteratorHelper pIterator( *pContainer );
1447 while ( pIterator.hasMoreElements() )
1451 ( (util::XCloseListener* )pIterator.next() )->queryClosing( aSource, bDeliverOwnership );
1453 catch( const uno::RuntimeException& )
1455 pIterator.remove();
1460 pContainer = m_pInterfaceContainer->getContainer(
1461 cppu::UnoType<util::XCloseListener>::get());
1462 if ( pContainer != NULL )
1464 ::cppu::OInterfaceIteratorHelper pCloseIterator( *pContainer );
1465 while ( pCloseIterator.hasMoreElements() )
1469 ( (util::XCloseListener* )pCloseIterator.next() )->notifyClosing( aSource );
1471 catch( const uno::RuntimeException& )
1473 pCloseIterator.remove();
1479 Dispose();
1483 void SAL_CALL OleComponent::addCloseListener( const uno::Reference< util::XCloseListener >& xListener )
1484 throw ( uno::RuntimeException )
1486 ::osl::MutexGuard aGuard( m_aMutex );
1487 if ( m_bDisposed )
1488 throw lang::DisposedException(); // TODO
1490 if ( !m_pInterfaceContainer )
1491 m_pInterfaceContainer = new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex );
1493 m_pInterfaceContainer->addInterface( cppu::UnoType<util::XCloseListener>::get(), xListener );
1497 void SAL_CALL OleComponent::removeCloseListener( const uno::Reference< util::XCloseListener >& xListener )
1498 throw ( uno::RuntimeException )
1500 ::osl::MutexGuard aGuard( m_aMutex );
1501 if ( m_bDisposed )
1502 throw lang::DisposedException(); // TODO
1504 if ( m_pInterfaceContainer )
1505 m_pInterfaceContainer->removeInterface( cppu::UnoType<util::XCloseListener>::get(),
1506 xListener );
1509 // XTransferable
1511 uno::Any SAL_CALL OleComponent::getTransferData( const datatransfer::DataFlavor& aFlavor )
1512 throw ( datatransfer::UnsupportedFlavorException,
1513 io::IOException,
1514 uno::RuntimeException )
1516 ::osl::MutexGuard aGuard( m_aMutex );
1517 if ( m_bDisposed )
1518 throw lang::DisposedException(); // TODO
1520 if ( !m_pNativeImpl->m_pOleObject )
1521 throw embed::WrongStateException(); // TODO: the object is in wrong state
1523 uno::Any aResult;
1524 sal_Bool bSupportedFlavor = sal_False;
1526 if ( m_pNativeImpl->GraphicalFlavor( aFlavor ) )
1528 DWORD nRequestedAspect = GetAspectFromFlavor( aFlavor );
1529 // if own icon is set and icon aspect is requested the own icon can be returned directly
1531 ComSmart< IDataObject > pDataObject;
1532 HRESULT hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IDataObject, (void**)&pDataObject );
1533 if ( FAILED( hr ) || !pDataObject )
1534 throw io::IOException(); // TODO: transport error code
1536 // The following optimization does not make much sense currently just because
1537 // only one aspect is supported, and only three formats for the aspect are supported
1538 // and moreover it is not guarantied that the once returned format will be supported further
1539 // example - i52106
1540 // TODO/LATER: bring the optimization back when other aspects are supported
1542 // FORMATETC* pFormatEtc = m_pNativeImpl->GetSupportedFormatForAspect( nRequestedAspect );
1543 // if ( pFormatEtc )
1544 // {
1545 // STGMEDIUM aMedium;
1546 // hr = pDataObject->GetData( pFormatEtc, &aMedium );
1547 // if ( SUCCEEDED( hr ) )
1548 // bSupportedFlavor = m_pNativeImpl->ConvertDataForFlavor( aMedium, aFlavor, aResult );
1549 // }
1550 // else
1552 // the supported format of the application is still not found, find one
1553 for ( sal_Int32 nInd = 0; nInd < FORMATS_NUM; nInd++ )
1555 STGMEDIUM aMedium;
1556 FORMATETC aFormat = pFormatTemplates[nInd];
1557 aFormat.dwAspect = nRequestedAspect;
1559 hr = pDataObject->GetData( &aFormat, &aMedium );
1560 if ( SUCCEEDED( hr ) )
1562 bSupportedFlavor = m_pNativeImpl->ConvertDataForFlavor( aMedium, aFlavor, aResult );
1563 ::ReleaseStgMedium(&aMedium); // i113605, to release storage medium
1564 if ( bSupportedFlavor )
1566 // TODO/LATER: bring the optimization back when other aspects are supported
1567 // m_pNativeImpl->AddSupportedFormat( aFormat );
1568 break;
1574 // If the replacement could not be retrieved, the cached representaion should be used
1575 // currently it is not necessary to retrieve it here, so it is implemented in the object itself
1577 // TODO: Investigate if there is already the format name
1578 // and whether this format is really required
1579 else if ( aFlavor.DataType == cppu::UnoType<io::XInputStream>::get()
1580 && aFlavor.MimeType == "application/x-openoffice-contentstream" )
1582 // allow to retrieve stream-representation of the object persistence
1583 bSupportedFlavor = sal_True;
1584 uno::Reference < io::XStream > xTempFileStream(
1585 io::TempFile::create(comphelper::getComponentContext(m_xFactory)),
1586 uno::UNO_QUERY_THROW );
1588 uno::Reference< io::XOutputStream > xTempOutStream = xTempFileStream->getOutputStream();
1589 uno::Reference< io::XInputStream > xTempInStream = xTempFileStream->getInputStream();
1590 if ( xTempOutStream.is() && xTempInStream.is() )
1592 OSL_ENSURE( m_pUnoOleObject, "Unexpected object absence!" );
1593 if ( !m_pUnoOleObject )
1594 throw uno::RuntimeException();
1596 m_pUnoOleObject->StoreObjectToStream( xTempOutStream );
1598 xTempOutStream->closeOutput();
1599 xTempOutStream = uno::Reference< io::XOutputStream >();
1601 else
1602 throw io::IOException(); // TODO:
1604 aResult <<= xTempInStream;
1607 if ( !bSupportedFlavor )
1608 throw datatransfer::UnsupportedFlavorException();
1610 return aResult;
1614 uno::Sequence< datatransfer::DataFlavor > SAL_CALL OleComponent::getTransferDataFlavors()
1615 throw ( uno::RuntimeException )
1617 ::osl::MutexGuard aGuard( m_aMutex );
1618 if ( m_bDisposed )
1619 throw lang::DisposedException(); // TODO
1621 if ( !m_pNativeImpl->m_pOleObject )
1622 throw embed::WrongStateException(); // TODO: the object is in wrong state
1624 RetrieveObjectDataFlavors_Impl();
1626 return m_aDataFlavors;
1630 sal_Bool SAL_CALL OleComponent::isDataFlavorSupported( const datatransfer::DataFlavor& aFlavor )
1631 throw ( uno::RuntimeException )
1633 ::osl::MutexGuard aGuard( m_aMutex );
1634 if ( m_bDisposed )
1635 throw lang::DisposedException(); // TODO
1637 if ( !m_pNativeImpl->m_pOleObject )
1638 throw embed::WrongStateException(); // TODO: the object is in wrong state
1640 if ( !m_aDataFlavors.getLength() )
1642 RetrieveObjectDataFlavors_Impl();
1645 for ( sal_Int32 nInd = 0; nInd < m_aDataFlavors.getLength(); nInd++ )
1646 if ( m_aDataFlavors[nInd].MimeType.equals( aFlavor.MimeType ) && m_aDataFlavors[nInd].DataType == aFlavor.DataType )
1647 return sal_True;
1649 return sal_False;
1652 void SAL_CALL OleComponent::dispose() throw (::com::sun::star::uno::RuntimeException)
1656 close( sal_True );
1658 catch ( const uno::Exception& )
1663 void SAL_CALL OleComponent::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
1664 throw ( uno::RuntimeException )
1666 ::osl::MutexGuard aGuard( m_aMutex );
1667 if ( m_bDisposed )
1668 throw lang::DisposedException(); // TODO
1670 if ( !m_pInterfaceContainer )
1671 m_pInterfaceContainer = new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex );
1673 m_pInterfaceContainer->addInterface( cppu::UnoType<lang::XEventListener>::get(), xListener );
1677 void SAL_CALL OleComponent::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
1678 throw ( uno::RuntimeException )
1680 ::osl::MutexGuard aGuard( m_aMutex );
1681 if ( m_bDisposed )
1682 throw lang::DisposedException(); // TODO
1684 if ( m_pInterfaceContainer )
1685 m_pInterfaceContainer->removeInterface( cppu::UnoType<lang::XEventListener>::get(),
1686 xListener );
1689 sal_Int64 SAL_CALL OleComponent::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier ) throw(::com::sun::star::uno::RuntimeException)
1693 uno::Sequence < sal_Int8 > aCLSID = GetCLSID();
1694 if ( MimeConfigurationHelper::ClassIDsEqual( aIdentifier, aCLSID ) )
1695 return (sal_Int64) (IUnknown*) m_pNativeImpl->m_pObj;
1697 // compatibility hack for old versions: CLSID was used in wrong order (SvGlobalName order)
1698 sal_Int32 nLength = aIdentifier.getLength();
1699 if ( nLength == 16 )
1701 for ( sal_Int32 n=8; n<16; n++ )
1702 if ( aIdentifier[n] != aCLSID[n] )
1703 return 0;
1704 if ( aIdentifier[7] == aCLSID[6] &&
1705 aIdentifier[6] == aCLSID[7] &&
1706 aIdentifier[5] == aCLSID[4] &&
1707 aIdentifier[4] == aCLSID[5] &&
1708 aIdentifier[3] == aCLSID[0] &&
1709 aIdentifier[2] == aCLSID[1] &&
1710 aIdentifier[1] == aCLSID[2] &&
1711 aIdentifier[0] == aCLSID[3] )
1712 return (sal_Int64) (IUnknown*) m_pNativeImpl->m_pObj;
1715 catch ( const uno::Exception& )
1719 return 0;
1722 sal_Bool SAL_CALL OleComponent::isModified() throw (::com::sun::star::uno::RuntimeException)
1724 return m_bModified;
1727 void SAL_CALL OleComponent::setModified( sal_Bool bModified )
1728 throw (::com::sun::star::beans::PropertyVetoException, ::com::sun::star::uno::RuntimeException)
1730 m_bModified = bModified;
1732 if ( bModified && m_pInterfaceContainer )
1734 ::cppu::OInterfaceContainerHelper* pContainer =
1735 m_pInterfaceContainer->getContainer( cppu::UnoType<util::XModifyListener>::get());
1736 if ( pContainer != NULL )
1738 ::cppu::OInterfaceIteratorHelper pIterator( *pContainer );
1739 while ( pIterator.hasMoreElements() )
1743 lang::EventObject aEvent( (util::XModifiable*) this );
1744 ((util::XModifyListener*)pIterator.next())->modified( aEvent );
1746 catch( const uno::RuntimeException& )
1748 pIterator.remove();
1755 void SAL_CALL OleComponent::addModifyListener( const com::sun::star::uno::Reference < com::sun::star::util::XModifyListener >& xListener ) throw(::com::sun::star::uno::RuntimeException)
1757 ::osl::MutexGuard aGuard( m_aMutex );
1758 if ( m_bDisposed )
1759 throw lang::DisposedException(); // TODO
1761 if ( !m_pInterfaceContainer )
1762 m_pInterfaceContainer = new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex );
1764 m_pInterfaceContainer->addInterface( cppu::UnoType<util::XModifyListener>::get(), xListener );
1767 void SAL_CALL OleComponent::removeModifyListener( const com::sun::star::uno::Reference < com::sun::star::util::XModifyListener >& xListener) throw(::com::sun::star::uno::RuntimeException)
1769 ::osl::MutexGuard aGuard( m_aMutex );
1770 if ( m_bDisposed )
1771 throw lang::DisposedException(); // TODO
1773 if ( m_pInterfaceContainer )
1774 m_pInterfaceContainer->removeInterface( cppu::UnoType<util::XModifyListener>::get(),
1775 xListener );
1778 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */