loplugin:indentation (clang-cl)
[LibreOffice.git] / embeddedobj / source / msole / olecomponent.cxx
blobcbcc1f6e79d76beb38fa286a8b29046ec96aabfe
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 <sal/config.h>
22 #include <com/sun/star/datatransfer/UnsupportedFlavorException.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <com/sun/star/embed/WrongStateException.hpp>
26 #include <com/sun/star/embed/UnreachableStateException.hpp>
27 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/io/TempFile.hpp>
30 #include <com/sun/star/io/XTruncate.hpp>
31 #include <com/sun/star/io/IOException.hpp>
32 #include <com/sun/star/awt/XRequestCallback.hpp>
34 #include "platform.h"
35 #include <cppuhelper/interfacecontainer.h>
36 #include <comphelper/mimeconfighelper.hxx>
37 #include <comphelper/processfactory.hxx>
38 #include <osl/file.hxx>
39 #include <rtl/ref.hxx>
40 #include <o3tl/char16_t2wchar_t.hxx>
41 #include <vcl/threadex.hxx>
43 #include "graphconvert.hxx"
44 #include "olecomponent.hxx"
45 #include "olepersist.hxx"
46 #include "olewrapclient.hxx"
47 #include "advisesink.hxx"
48 #include <oleembobj.hxx>
49 #include "mtnotification.hxx"
50 #include <memory>
51 #include <string>
53 using namespace ::com::sun::star;
54 using namespace ::comphelper;
55 #define MAX_ENUM_ELE 20
56 #define FORMATS_NUM 3
58 // ============ class ComSmart =====================
59 namespace {
61 template< class T > class ComSmart
63 T* m_pInterface;
65 void OwnRelease()
67 if ( m_pInterface )
69 T* pInterface = m_pInterface;
70 m_pInterface = nullptr;
71 pInterface->Release();
75 public:
76 ComSmart()
77 : m_pInterface( nullptr )
80 ComSmart( const ComSmart<T>& rObj )
81 : m_pInterface( rObj.m_pInterface )
83 if ( m_pInterface != NULL )
84 m_pInterface->AddRef();
87 ComSmart( T* pInterface )
88 : m_pInterface( pInterface )
90 if ( m_pInterface != NULL )
91 m_pInterface->AddRef();
94 ~ComSmart()
96 OwnRelease();
99 ComSmart& operator=( const ComSmart<T>& rObj )
101 if(this == &rObj)
102 return *this;
104 OwnRelease();
106 m_pInterface = rObj.m_pInterface;
108 if ( m_pInterface != NULL )
109 m_pInterface->AddRef();
111 return *this;
114 ComSmart<T>& operator=( T* pInterface )
116 OwnRelease();
118 m_pInterface = pInterface;
120 if ( m_pInterface != NULL )
121 m_pInterface->AddRef();
123 return *this;
126 operator T*() const
128 return m_pInterface;
131 T& operator*() const
133 return *m_pInterface;
136 T** operator&()
138 OwnRelease();
140 m_pInterface = nullptr;
142 return &m_pInterface;
145 T* operator->() const
147 return m_pInterface;
150 bool operator==( const ComSmart<T>& rObj ) const
152 return ( m_pInterface == rObj.m_pInterface );
155 bool operator!=( const ComSmart<T>& rObj ) const
157 return ( m_pInterface != rObj.m_pInterface );
160 bool operator==( const T* pInterface ) const
162 return ( m_pInterface == pInterface );
165 bool operator!=( const T* pInterface ) const
167 return ( m_pInterface != pInterface );
173 // ============ class ComSmart =====================
175 typedef std::vector< FORMATETC* > FormatEtcList;
177 FORMATETC const pFormatTemplates[FORMATS_NUM] = {
178 { CF_ENHMETAFILE, nullptr, 0, -1, TYMED_ENHMF },
179 { CF_METAFILEPICT, nullptr, 0, -1, TYMED_MFPICT },
180 { CF_BITMAP, nullptr, 0, -1, TYMED_GDI } };
183 struct OleComponentNative_Impl {
184 ComSmart< IUnknown > m_pObj;
185 ComSmart< IOleObject > m_pOleObject;
186 ComSmart< IViewObject2 > m_pViewObject2;
187 ComSmart< IStorage > m_pIStorage;
188 FormatEtcList m_aFormatsList;
189 uno::Sequence< datatransfer::DataFlavor > m_aSupportedGraphFormats;
191 OleComponentNative_Impl()
193 // TODO: Extend format list
194 m_aSupportedGraphFormats.realloc( 5 );
196 m_aSupportedGraphFormats[0] = datatransfer::DataFlavor(
197 "application/x-openoffice-emf;windows_formatname=\"Image EMF\"",
198 "Windows Enhanced Metafile",
199 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
201 m_aSupportedGraphFormats[1] = datatransfer::DataFlavor(
202 "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"",
203 "Windows Metafile",
204 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
206 m_aSupportedGraphFormats[2] = datatransfer::DataFlavor(
207 "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"",
208 "Bitmap",
209 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
211 m_aSupportedGraphFormats[3] = datatransfer::DataFlavor(
212 "image/png",
213 "PNG",
214 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
216 m_aSupportedGraphFormats[0] = datatransfer::DataFlavor(
217 "application/x-openoffice-gdimetafile;windows_formatname=\"GDIMetaFile\"",
218 "GDIMetafile",
219 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
222 bool ConvertDataForFlavor( const STGMEDIUM& aMedium,
223 const datatransfer::DataFlavor& aFlavor,
224 uno::Any& aResult );
226 bool GraphicalFlavor( const datatransfer::DataFlavor& aFlavor );
228 uno::Sequence< datatransfer::DataFlavor > GetFlavorsForAspects( sal_uInt32 nSupportedAspects );
232 static 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 static 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 static 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( o3tl::toW(aFilePath.getStr()),
271 nullptr,
272 STGM_READWRITE | STGM_TRANSACTED, // | STGM_DELETEONRELEASE,
273 nullptr,
275 ppIStorage );
279 bool OleComponentNative_Impl::ConvertDataForFlavor( const STGMEDIUM& aMedium,
280 const datatransfer::DataFlavor& aFlavor,
281 uno::Any& aResult )
283 bool bAnyIsReady = 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 std::unique_ptr<sal_Int8[]> 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 = static_cast<METAFILEPICT*>(GlobalLock( aMedium.hMetaFilePict ));
298 if ( pMF )
300 nBufSize = GetMetaFileBitsEx( pMF->hMF, 0, nullptr ) + 22;
301 pBuf.reset(new sal_Int8[nBufSize]);
304 // TODO/LATER: the unit size must be calculated correctly
305 *reinterpret_cast<long*>( pBuf.get() ) = 0x9ac6cdd7L;
306 *reinterpret_cast<short*>( pBuf.get()+6 ) = SHORT(0);
307 *reinterpret_cast<short*>( pBuf.get()+8 ) = SHORT(0);
308 *reinterpret_cast<short*>( pBuf.get()+10 ) = static_cast<SHORT>(pMF->xExt);
309 *reinterpret_cast<short*>( pBuf.get()+12 ) = static_cast<SHORT>(pMF->yExt);
310 *reinterpret_cast<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 >( pBuf.get(), nBufSize );
318 bAnyIsReady = 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, nullptr );
329 pBuf.reset(new sal_Int8[nBufSize]);
330 if ( nBufSize && nBufSize == GetEnhMetaFileBits( aMedium.hEnhMetaFile, nBufSize, reinterpret_cast<LPBYTE>(pBuf.get()) ) )
332 if ( aFlavor.MimeType.matchAsciiL( "application/x-openoffice-emf;windows_formatname=\"Image EMF\"", 57 ) )
334 aResult <<= uno::Sequence< sal_Int8 >( pBuf.get(), nBufSize );
335 bAnyIsReady = true;
339 else if ( aMedium.tymed == TYMED_GDI ) // Bitmap
341 aFormat = "image/x-MS-bmp";
343 // Find out size of buffer: deprecated GetBitmapBits does not have a mode to return
344 // required buffer size
345 BITMAP aBmp;
346 GetObjectW(aMedium.hBitmap, sizeof(aBmp), &aBmp);
347 nBufSize = aBmp.bmWidthBytes * aBmp.bmHeight;
349 pBuf.reset(new sal_Int8[nBufSize]);
350 if ( nBufSize && nBufSize == sal::static_int_cast< ULONG >( GetBitmapBits( aMedium.hBitmap, nBufSize, pBuf.get() ) ) )
352 if ( aFlavor.MimeType.matchAsciiL( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"", 54 ) )
354 aResult <<= uno::Sequence< sal_Int8 >( pBuf.get(), nBufSize );
355 bAnyIsReady = true;
360 if ( pBuf && !bAnyIsReady )
362 for ( auto const & supportedFormat : std::as_const(m_aSupportedGraphFormats) )
363 if ( aFlavor.MimeType.match( supportedFormat.MimeType )
364 && aFlavor.DataType == supportedFormat.DataType
365 && aFlavor.DataType == cppu::UnoType<uno::Sequence< sal_Int8 >>::get() )
367 bAnyIsReady = ConvertBufferToFormat( pBuf.get(), nBufSize, aFormat, aResult );
368 break;
373 return bAnyIsReady;
377 bool OleComponentNative_Impl::GraphicalFlavor( const datatransfer::DataFlavor& aFlavor )
379 // Actually all the required graphical formats must be supported
380 for ( auto const & supportedFormat : std::as_const(m_aSupportedGraphFormats) )
381 if ( aFlavor.MimeType.match( supportedFormat.MimeType )
382 && aFlavor.DataType == supportedFormat.DataType )
383 return true;
385 return false;
389 static bool GetClassIDFromSequence_Impl( uno::Sequence< sal_Int8 > const & aSeq, CLSID& aResult )
391 if ( aSeq.getLength() == 16 )
393 aResult.Data1 = ( ( ( ( ( static_cast<sal_uInt8>(aSeq[0]) << 8 ) + static_cast<sal_uInt8>(aSeq[1]) ) << 8 ) + static_cast<sal_uInt8>(aSeq[2]) ) << 8 ) + static_cast<sal_uInt8>(aSeq[3]);
394 aResult.Data2 = ( static_cast<sal_uInt8>(aSeq[4]) << 8 ) + static_cast<sal_uInt8>(aSeq[5]);
395 aResult.Data3 = ( static_cast<sal_uInt8>(aSeq[6]) << 8 ) + static_cast<sal_uInt8>(aSeq[7]);
396 for( int nInd = 0; nInd < 8; nInd++ )
397 aResult.Data4[nInd] = static_cast<sal_uInt8>(aSeq[nInd+8]);
399 return true;
402 return false;
406 static OUString WinAccToVcl_Impl( const sal_Unicode* pStr )
408 OUString aResult;
410 if( pStr )
412 while ( *pStr )
414 if ( *pStr == '&' )
416 aResult += "~";
417 while( *( ++pStr ) == '&' );
419 else
421 aResult += OUStringChar( *pStr );
422 pStr++;
427 return aResult;
431 OleComponent::OleComponent( const uno::Reference< uno::XComponentContext >& xContext, OleEmbeddedObject* pUnoOleObject )
432 : m_pInterfaceContainer( nullptr )
433 , m_bDisposed( false )
434 , m_bModified( false )
435 , m_pNativeImpl( new OleComponentNative_Impl() )
436 , m_pUnoOleObject( pUnoOleObject )
437 , m_pOleWrapClientSite( nullptr )
438 , m_pImplAdviseSink( nullptr )
439 , m_nOLEMiscFlags( 0 )
440 , m_nAdvConn( 0 )
441 , m_xContext( xContext )
442 , m_bOleInitialized( false )
443 , m_bWorkaroundActive( false )
445 OSL_ENSURE( m_pUnoOleObject, "No owner object is provided!" );
447 HRESULT hr = OleInitialize( nullptr );
448 OSL_ENSURE( hr == S_OK || hr == S_FALSE, "The ole can not be successfully initialized" );
449 if ( hr == S_OK || hr == S_FALSE )
450 m_bOleInitialized = true;
452 m_pOleWrapClientSite = new OleWrapperClientSite( this );
453 m_pOleWrapClientSite->AddRef();
455 m_pImplAdviseSink = new OleWrapperAdviseSink( this );
456 m_pImplAdviseSink->AddRef();
461 OleComponent::~OleComponent()
463 OSL_ENSURE( !m_pOleWrapClientSite && !m_pImplAdviseSink && !m_pInterfaceContainer && !m_bOleInitialized,
464 "The object was not closed successfully! DISASTER is possible!" );
466 if ( m_pOleWrapClientSite || m_pImplAdviseSink || m_pInterfaceContainer || m_bOleInitialized )
468 osl_atomic_increment(&m_refCount);
469 try {
470 Dispose();
471 } catch( const uno::Exception& ) {}
474 for (auto const& format : m_pNativeImpl->m_aFormatsList)
476 delete format;
478 m_pNativeImpl->m_aFormatsList.clear();
480 delete m_pNativeImpl;
483 void OleComponent::Dispose()
485 if ( m_bDisposed )
486 return;
488 // Call CloseObject() without m_aMutex locked, since it will call
489 // IOleObject::Close(), which can call event listeners, which can run on a
490 // different thread.
491 CloseObject();
493 osl::MutexGuard aGuard(m_aMutex);
494 if ( m_pOleWrapClientSite )
496 m_pOleWrapClientSite->disconnectOleComponent();
497 m_pOleWrapClientSite->Release();
498 m_pOleWrapClientSite = nullptr;
501 if ( m_pImplAdviseSink )
503 m_pImplAdviseSink->disconnectOleComponent();
504 m_pImplAdviseSink->Release();
505 m_pImplAdviseSink = nullptr;
508 if ( m_pInterfaceContainer )
510 lang::EventObject aEvent( static_cast< ::cppu::OWeakObject* >( this ) );
511 m_pInterfaceContainer->disposeAndClear( aEvent );
513 delete m_pInterfaceContainer;
514 m_pInterfaceContainer = nullptr;
517 if ( m_bOleInitialized )
519 // since the disposing can happen not only from main thread but also from a clipboard
520 // the deinitialization might lead to a disaster, SO7 does not deinitialize OLE at all
521 // so currently the same approach is selected as workaround
522 // OleUninitialize();
523 m_bOleInitialized = false;
526 m_bDisposed = true;
530 void OleComponent::disconnectEmbeddedObject()
532 // must not be called from destructor of UNO OLE object!!!
533 osl::MutexGuard aGuard( m_aMutex );
534 m_pUnoOleObject = nullptr;
538 void OleComponent::CreateNewIStorage_Impl()
540 // TODO: in future a global memory could be used instead of file.
542 // write the stream to the temporary file
543 OUString aTempURL;
545 OSL_ENSURE( m_pUnoOleObject, "Unexpected object absence!" );
546 if ( m_pUnoOleObject )
547 aTempURL = m_pUnoOleObject->CreateTempURLEmpty_Impl();
548 else
549 aTempURL = GetNewTempFileURL_Impl( m_xContext );
551 if ( !aTempURL.getLength() )
552 throw uno::RuntimeException(); // TODO
554 // open an IStorage based on the temporary file
555 OUString aTempFilePath;
556 if ( ::osl::FileBase::getSystemPathFromFileURL( aTempURL, aTempFilePath ) != ::osl::FileBase::E_None )
557 throw uno::RuntimeException(); // TODO: something dangerous happened
559 HRESULT hr = StgCreateDocfile( o3tl::toW(aTempFilePath.getStr()), STGM_CREATE | STGM_READWRITE | STGM_TRANSACTED | STGM_DELETEONRELEASE, 0, &m_pNativeImpl->m_pIStorage );
560 if ( FAILED( hr ) || !m_pNativeImpl->m_pIStorage )
561 throw io::IOException(); // TODO: transport error code?
565 uno::Sequence< datatransfer::DataFlavor > OleComponentNative_Impl::GetFlavorsForAspects( sal_uInt32 nSupportedAspects )
567 uno::Sequence< datatransfer::DataFlavor > aResult;
568 for ( sal_uInt32 nAsp = 1; nAsp <= 8; nAsp *= 2 )
569 if ( ( nSupportedAspects & nAsp ) == nAsp )
571 OUString aAspectSuffix = GetFlavorSuffixFromAspect( nAsp );
573 sal_Int32 nLength = aResult.getLength();
574 aResult.realloc( nLength + m_aSupportedGraphFormats.getLength() );
576 for ( sal_Int32 nInd = 0; nInd < m_aSupportedGraphFormats.getLength(); nInd++ )
578 aResult[nLength + nInd].MimeType = m_aSupportedGraphFormats[nInd].MimeType + aAspectSuffix;
579 aResult[nLength + nInd].HumanPresentableName = m_aSupportedGraphFormats[nInd].HumanPresentableName;
580 aResult[nLength + nInd].DataType = m_aSupportedGraphFormats[nInd].DataType;
584 return aResult;
588 void OleComponent::RetrieveObjectDataFlavors_Impl()
590 if ( !m_pNativeImpl->m_pOleObject )
591 throw embed::WrongStateException(); // TODO: the object is in wrong state
593 if ( !m_aDataFlavors.getLength() )
595 ComSmart< IDataObject > pDataObject;
596 HRESULT hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pDataObject) );
597 if ( SUCCEEDED( hr ) && pDataObject )
599 ComSmart< IEnumFORMATETC > pFormatEnum;
600 hr = pDataObject->EnumFormatEtc( DATADIR_GET, &pFormatEnum );
601 if ( SUCCEEDED( hr ) && pFormatEnum )
603 FORMATETC pElem[ MAX_ENUM_ELE ];
604 ULONG nNum = 0;
606 // if it is possible to retrieve at least one supported graphical format for an aspect
607 // this format can be converted to other supported formats
608 sal_uInt32 nSupportedAspects = 0;
611 HRESULT hr2 = pFormatEnum->Next( MAX_ENUM_ELE, pElem, &nNum );
612 if( hr2 == S_OK || hr2 == S_FALSE )
614 for( sal_uInt32 nInd = 0; nInd < FORMATS_NUM; nInd++ )
616 if ( pElem[nInd].cfFormat == pFormatTemplates[nInd].cfFormat
617 && pElem[nInd].tymed == pFormatTemplates[nInd].tymed )
618 nSupportedAspects |= pElem[nInd].dwAspect;
621 else
622 break;
624 while( nNum == MAX_ENUM_ELE );
626 m_aDataFlavors = m_pNativeImpl->GetFlavorsForAspects( nSupportedAspects );
630 if ( !m_aDataFlavors.getLength() )
632 // TODO:
633 // for any reason the object could not provide this information
634 // try to get access to the cached representation
640 bool OleComponent::InitializeObject_Impl()
641 // There will be no static objects!
643 if ( !m_pNativeImpl->m_pObj )
644 return false;
646 // the linked object will be detected here
647 ComSmart< IOleLink > pOleLink;
648 HRESULT hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IOleLink, reinterpret_cast<void**>(&pOleLink) );
649 OSL_ENSURE( m_pUnoOleObject, "Unexpected object absence!" );
650 if ( m_pUnoOleObject )
651 m_pUnoOleObject->SetObjectIsLink_Impl( pOleLink != nullptr );
654 hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IViewObject2, reinterpret_cast<void**>(&m_pNativeImpl->m_pViewObject2) );
655 if ( FAILED( hr ) || !m_pNativeImpl->m_pViewObject2 )
656 return false;
658 // remove all the caches
659 IOleCache* pIOleCache = nullptr;
660 if ( SUCCEEDED( m_pNativeImpl->m_pObj->QueryInterface( IID_IOleCache, reinterpret_cast<void**>(&pIOleCache) ) ) && pIOleCache )
662 IEnumSTATDATA* pEnumSD = nullptr;
663 HRESULT hr2 = pIOleCache->EnumCache( &pEnumSD );
665 if ( SUCCEEDED( hr2 ) && pEnumSD )
667 pEnumSD->Reset();
668 STATDATA aSD;
669 DWORD nNum;
670 while( SUCCEEDED( pEnumSD->Next( 1, &aSD, &nNum ) ) && nNum == 1 )
671 hr2 = pIOleCache->Uncache( aSD.dwConnection );
674 // No IDataObject implementation, caching must be used instead
675 DWORD nConn;
676 FORMATETC aFormat = { 0, nullptr, DVASPECT_CONTENT, -1, TYMED_MFPICT };
677 hr2 = pIOleCache->Cache( &aFormat, ADVFCACHE_ONSAVE, &nConn );
679 pIOleCache->Release();
680 pIOleCache = nullptr;
683 hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&m_pNativeImpl->m_pOleObject) );
684 if ( FAILED( hr ) || !m_pNativeImpl->m_pOleObject )
685 return false; // Static objects are not supported, they should be inserted as graphics
687 m_pNativeImpl->m_pOleObject->GetMiscStatus( DVASPECT_CONTENT, reinterpret_cast<DWORD*>(&m_nOLEMiscFlags) );
688 // TODO: use other misc flags also
689 // the object should have drawable aspect even in case it supports only iconic representation
690 // if ( m_nOLEMiscFlags & OLEMISC_ONLYICONIC )
692 m_pNativeImpl->m_pOleObject->SetClientSite( m_pOleWrapClientSite );
694 // the only need in this registration is workaround for close notification
695 m_pNativeImpl->m_pOleObject->Advise( m_pImplAdviseSink, reinterpret_cast<DWORD*>(&m_nAdvConn) );
696 m_pNativeImpl->m_pViewObject2->SetAdvise( DVASPECT_CONTENT, 0, m_pImplAdviseSink );
698 OleSetContainedObject( m_pNativeImpl->m_pOleObject, TRUE );
700 return true;
703 namespace
705 HRESULT OleLoadSeh(LPSTORAGE pIStorage, LPVOID* ppObj)
707 HRESULT hr = E_FAIL;
708 __try {
709 hr = OleLoad(pIStorage, IID_IUnknown, nullptr, ppObj);
710 } __except( EXCEPTION_EXECUTE_HANDLER ) {
711 return E_FAIL;
713 return hr;
717 void OleComponent::LoadEmbeddedObject( const OUString& aTempURL )
719 if ( !aTempURL.getLength() )
720 throw lang::IllegalArgumentException(); // TODO
722 if ( m_pNativeImpl->m_pIStorage )
723 throw io::IOException(); // TODO the object is already initialized or wrong initialization is done
725 // open an IStorage based on the temporary file
726 HRESULT hr = OpenIStorageFromURL_Impl( aTempURL, &m_pNativeImpl->m_pIStorage );
728 if ( FAILED( hr ) || !m_pNativeImpl->m_pIStorage )
729 throw io::IOException(); // TODO: transport error code?
731 hr = OleLoadSeh(m_pNativeImpl->m_pIStorage, reinterpret_cast<void**>(&m_pNativeImpl->m_pObj));
732 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
734 throw uno::RuntimeException();
737 if ( !InitializeObject_Impl() )
738 throw uno::RuntimeException(); // TODO
742 void OleComponent::CreateObjectFromClipboard()
744 if ( m_pNativeImpl->m_pIStorage )
745 throw io::IOException(); // TODO:the object is already initialized
747 CreateNewIStorage_Impl();
748 if ( !m_pNativeImpl->m_pIStorage )
749 throw uno::RuntimeException(); // TODO
751 IDataObject * pDO = nullptr;
752 HRESULT hr = OleGetClipboard( &pDO );
753 if( SUCCEEDED( hr ) && pDO )
755 hr = OleQueryCreateFromData( pDO );
756 if( S_OK == GetScode( hr ) )
758 hr = OleCreateFromData( pDO,
759 IID_IUnknown,
760 OLERENDER_DRAW, // OLERENDER_FORMAT
761 nullptr, // &aFormat,
762 nullptr,
763 m_pNativeImpl->m_pIStorage,
764 reinterpret_cast<void**>(&m_pNativeImpl->m_pObj) );
766 else
768 // Static objects are not supported
769 pDO->Release();
773 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
774 throw uno::RuntimeException();
776 if ( !InitializeObject_Impl() )
777 throw uno::RuntimeException(); // TODO
781 void OleComponent::CreateNewEmbeddedObject( const uno::Sequence< sal_Int8 >& aSeqCLSID )
783 CLSID aClsID;
785 if ( !GetClassIDFromSequence_Impl( aSeqCLSID, aClsID ) )
786 throw lang::IllegalArgumentException(); // TODO
788 if ( m_pNativeImpl->m_pIStorage )
789 throw io::IOException(); // TODO:the object is already initialized
791 CreateNewIStorage_Impl();
792 if ( !m_pNativeImpl->m_pIStorage )
793 throw uno::RuntimeException(); // TODO
795 // FORMATETC aFormat = { CF_METAFILEPICT, NULL, nAspect, -1, TYMED_MFPICT }; // for OLE..._DRAW should be NULL
797 HRESULT hr = OleCreate( aClsID,
798 IID_IUnknown,
799 OLERENDER_DRAW, // OLERENDER_FORMAT
800 nullptr, // &aFormat,
801 nullptr,
802 m_pNativeImpl->m_pIStorage,
803 reinterpret_cast<void**>(&m_pNativeImpl->m_pObj) );
805 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
806 throw uno::RuntimeException(); // TODO
808 if ( !InitializeObject_Impl() )
809 throw uno::RuntimeException(); // TODO
811 // TODO: getExtent???
815 void OleComponent::CreateObjectFromData( const uno::Reference< datatransfer::XTransferable >& )
816 // Static objects are not supported, they should be inserted as graphics
818 // TODO: May be this call is useless since there are no static objects
819 // and nonstatic objects will be created based on OLEstorage ( stream ).
820 // ???
822 // OleQueryCreateFromData...
826 void OleComponent::CreateObjectFromFile( const OUString& aFileURL )
828 if ( m_pNativeImpl->m_pIStorage )
829 throw io::IOException(); // TODO:the object is already initialized
831 CreateNewIStorage_Impl();
832 if ( !m_pNativeImpl->m_pIStorage )
833 throw uno::RuntimeException(); // TODO:
835 OUString aFilePath;
836 if ( ::osl::FileBase::getSystemPathFromFileURL( aFileURL, aFilePath ) != ::osl::FileBase::E_None )
837 throw uno::RuntimeException(); // TODO: something dangerous happened
839 HRESULT hr = OleCreateFromFile( CLSID_NULL,
840 o3tl::toW(aFilePath.getStr()),
841 IID_IUnknown,
842 OLERENDER_DRAW, // OLERENDER_FORMAT
843 nullptr,
844 nullptr,
845 m_pNativeImpl->m_pIStorage,
846 reinterpret_cast<void**>(&m_pNativeImpl->m_pObj) );
848 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
849 throw uno::RuntimeException(); // TODO
851 if ( !InitializeObject_Impl() )
852 throw uno::RuntimeException(); // TODO
856 void OleComponent::CreateLinkFromFile( const OUString& aFileURL )
858 if ( m_pNativeImpl->m_pIStorage )
859 throw io::IOException(); // TODO:the object is already initialized
861 CreateNewIStorage_Impl();
862 if ( !m_pNativeImpl->m_pIStorage )
863 throw uno::RuntimeException(); // TODO:
865 OUString aFilePath;
866 if ( ::osl::FileBase::getSystemPathFromFileURL( aFileURL, aFilePath ) != ::osl::FileBase::E_None )
867 throw uno::RuntimeException(); // TODO: something dangerous happened
869 HRESULT hr = OleCreateLinkToFile( o3tl::toW(aFilePath.getStr()),
870 IID_IUnknown,
871 OLERENDER_DRAW, // OLERENDER_FORMAT
872 nullptr,
873 nullptr,
874 m_pNativeImpl->m_pIStorage,
875 reinterpret_cast<void**>(&m_pNativeImpl->m_pObj) );
877 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
878 throw uno::RuntimeException(); // TODO
880 if ( !InitializeObject_Impl() )
881 throw uno::RuntimeException(); // TODO
885 void OleComponent::InitEmbeddedCopyOfLink( rtl::Reference<OleComponent> const & pOleLinkComponent )
887 if ( !pOleLinkComponent || !pOleLinkComponent->m_pNativeImpl->m_pObj )
888 throw lang::IllegalArgumentException(); // TODO
890 if ( m_pNativeImpl->m_pIStorage )
891 throw io::IOException(); // TODO:the object is already initialized
893 ComSmart< IDataObject > pDataObject;
894 HRESULT hr = pOleLinkComponent->m_pNativeImpl->m_pObj->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pDataObject) );
895 if ( SUCCEEDED( hr ) && pDataObject && SUCCEEDED( OleQueryCreateFromData( pDataObject ) ) )
897 // the object must be already disconnected from the temporary URL
898 CreateNewIStorage_Impl();
899 if ( !m_pNativeImpl->m_pIStorage )
900 throw uno::RuntimeException(); // TODO:
902 hr = OleCreateFromData( pDataObject,
903 IID_IUnknown,
904 OLERENDER_DRAW,
905 nullptr,
906 nullptr,
907 m_pNativeImpl->m_pIStorage,
908 reinterpret_cast<void**>(&m_pNativeImpl->m_pObj) );
911 if ( !m_pNativeImpl->m_pObj )
913 ComSmart< IOleLink > pOleLink;
914 hr = pOleLinkComponent->m_pNativeImpl->m_pObj->QueryInterface( IID_IOleLink, reinterpret_cast<void**>(&pOleLink) );
915 if ( FAILED( hr ) || !pOleLink )
916 throw io::IOException(); // TODO: the object doesn't support IOleLink
918 ComSmart< IMoniker > pMoniker;
919 hr = pOleLink->GetSourceMoniker( &pMoniker );
920 if ( FAILED( hr ) || !pMoniker )
921 throw io::IOException(); // TODO: can not retrieve moniker
923 // In case of file moniker life is easy : )
924 DWORD aMonType = 0;
925 hr = pMoniker->IsSystemMoniker( &aMonType );
926 if ( SUCCEEDED( hr ) && aMonType == MKSYS_FILEMONIKER )
928 ComSmart< IMalloc > pMalloc;
929 hr = CoGetMalloc( 1, &pMalloc ); // if fails there will be a memory leak
930 OSL_ENSURE(SUCCEEDED(hr) && pMalloc, "CoGetMalloc() failed!");
932 LPOLESTR pOleStr = nullptr;
933 hr = pOleLink->GetSourceDisplayName( &pOleStr );
934 if ( SUCCEEDED( hr ) && pOleStr )
936 std::wstring aFilePath( pOleStr );
937 if ( pMalloc )
938 pMalloc->Free( pOleStr );
940 hr = OleCreateFromFile( CLSID_NULL,
941 aFilePath.c_str(),
942 IID_IUnknown,
943 OLERENDER_DRAW, // OLERENDER_FORMAT
944 nullptr,
945 nullptr,
946 m_pNativeImpl->m_pIStorage,
947 reinterpret_cast<void**>(&m_pNativeImpl->m_pObj) );
951 // in case of other moniker types the only way is to get storage
952 if ( !m_pNativeImpl->m_pObj )
954 ComSmart< IBindCtx > pBindCtx;
955 hr = CreateBindCtx( 0, &pBindCtx );
956 if ( SUCCEEDED( hr ) && pBindCtx )
958 ComSmart< IStorage > pObjectStorage;
959 hr = pMoniker->BindToStorage( pBindCtx, nullptr, IID_IStorage, reinterpret_cast<void**>(&pObjectStorage) );
960 if ( SUCCEEDED( hr ) && pObjectStorage )
962 hr = pObjectStorage->CopyTo( 0, nullptr, nullptr, m_pNativeImpl->m_pIStorage );
963 if ( SUCCEEDED( hr ) )
964 hr = OleLoadSeh(m_pNativeImpl->m_pIStorage, reinterpret_cast<void**>(&m_pNativeImpl->m_pObj));
970 // If object could not be created the only way is to use graphical representation
971 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
972 throw uno::RuntimeException(); // TODO
974 if ( !InitializeObject_Impl() )
975 throw uno::RuntimeException(); // TODO
979 void OleComponent::RunObject()
981 OSL_ENSURE( m_pNativeImpl->m_pOleObject, "The pointer can not be set to NULL here!" );
982 if ( !m_pNativeImpl->m_pOleObject )
983 throw embed::WrongStateException(); // TODO: the object is in wrong state
985 if ( !OleIsRunning( m_pNativeImpl->m_pOleObject ) )
987 HRESULT hr = OleRun( m_pNativeImpl->m_pObj );
989 if ( FAILED( hr ) )
991 if ( hr == REGDB_E_CLASSNOTREG )
992 throw embed::UnreachableStateException(); // the object server is not installed
993 else
994 throw io::IOException();
1000 awt::Size OleComponent::CalculateWithFactor( const awt::Size& aSize,
1001 const awt::Size& aMultiplier,
1002 const awt::Size& aDivisor )
1004 awt::Size aResult;
1006 sal_Int64 nWidth = static_cast<sal_Int64>(aSize.Width) * static_cast<sal_Int64>(aMultiplier.Width) / static_cast<sal_Int64>(aDivisor.Width);
1007 sal_Int64 nHeight = static_cast<sal_Int64>(aSize.Height) * static_cast<sal_Int64>(aMultiplier.Height) / static_cast<sal_Int64>(aDivisor.Height);
1008 OSL_ENSURE( nWidth < SAL_MAX_INT32 && nWidth > SAL_MIN_INT32
1009 && nHeight < SAL_MAX_INT32 && nHeight > SAL_MIN_INT32,
1010 "Unacceptable result size!" );
1012 aResult.Width = static_cast<sal_Int32>(nWidth);
1013 aResult.Height = static_cast<sal_Int32>(nHeight);
1015 return aResult;
1019 void OleComponent::CloseObject()
1021 if ( m_pNativeImpl->m_pOleObject && OleIsRunning( m_pNativeImpl->m_pOleObject ) )
1022 m_pNativeImpl->m_pOleObject->Close( OLECLOSE_NOSAVE ); // must be saved before
1026 uno::Sequence< embed::VerbDescriptor > OleComponent::GetVerbList()
1028 if ( !m_pNativeImpl->m_pOleObject )
1029 throw embed::WrongStateException(); // TODO: the object is in wrong state
1031 if( !m_aVerbList.getLength() )
1033 ComSmart< IEnumOLEVERB > pEnum;
1034 if( SUCCEEDED( m_pNativeImpl->m_pOleObject->EnumVerbs( &pEnum ) ) )
1036 OLEVERB szEle[ MAX_ENUM_ELE ];
1037 ULONG nNum = 0;
1038 sal_Int32 nSeqSize = 0;
1042 HRESULT hr = pEnum->Next( MAX_ENUM_ELE, szEle, &nNum );
1043 if( hr == S_OK || hr == S_FALSE )
1045 m_aVerbList.realloc( nSeqSize += nNum );
1046 for( sal_uInt32 nInd = 0; nInd < nNum; nInd++ )
1048 m_aVerbList[nSeqSize-nNum+nInd].VerbID = szEle[ nInd ].lVerb;
1049 m_aVerbList[nSeqSize-nNum+nInd].VerbName = WinAccToVcl_Impl( o3tl::toU(szEle[ nInd ].lpszVerbName) );
1050 m_aVerbList[nSeqSize-nNum+nInd].VerbFlags = szEle[ nInd ].fuFlags;
1051 m_aVerbList[nSeqSize-nNum+nInd].VerbAttributes = szEle[ nInd ].grfAttribs;
1054 else
1055 break;
1057 while( nNum == MAX_ENUM_ELE );
1061 return m_aVerbList;
1065 void OleComponent::ExecuteVerb( sal_Int32 nVerbID )
1067 if ( !m_pNativeImpl->m_pOleObject )
1068 throw embed::WrongStateException(); // TODO
1070 HRESULT hr = OleRun( m_pNativeImpl->m_pOleObject );
1071 if ( FAILED( hr ) )
1072 throw io::IOException(); // TODO: a specific exception that transport error code can be thrown here
1074 // TODO: probably extents should be set here and stored in aRect
1075 // TODO: probably the parent window also should be set
1076 hr = m_pNativeImpl->m_pOleObject->DoVerb( nVerbID, nullptr, m_pOleWrapClientSite, 0, nullptr, nullptr );
1078 if ( FAILED( hr ) )
1079 throw io::IOException(); // TODO
1083 void OleComponent::SetHostName( const OUString& aEmbDocName )
1085 if ( !m_pNativeImpl->m_pOleObject )
1086 throw embed::WrongStateException(); // TODO: the object is in wrong state
1088 m_pNativeImpl->m_pOleObject->SetHostNames( L"app name", o3tl::toW( aEmbDocName.getStr() ) );
1092 void OleComponent::SetExtent( const awt::Size& aVisAreaSize, sal_Int64 nAspect )
1094 if ( !m_pNativeImpl->m_pOleObject )
1095 throw embed::WrongStateException(); // TODO: the object is in wrong state
1097 DWORD nMSAspect = static_cast<DWORD>(nAspect); // first 32 bits are for MS aspects
1099 SIZEL aSize = { aVisAreaSize.Width, aVisAreaSize.Height };
1100 HRESULT hr = m_pNativeImpl->m_pOleObject->SetExtent( nMSAspect, &aSize );
1102 if ( FAILED( hr ) )
1104 // TODO/LATER: is it correct? In future user code probably should be ready for the exception.
1105 // if the object is running but not activated, RPC_E_SERVER_DIED error code is returned by OLE package
1106 // in this case just do nothing
1107 // Also Visio returns E_FAIL on resize if it is in running state
1108 // if ( hr != RPC_E_SERVER_DIED )
1109 throw io::IOException(); // TODO
1114 awt::Size OleComponent::GetExtent( sal_Int64 nAspect )
1116 if ( !m_pNativeImpl->m_pOleObject )
1117 throw embed::WrongStateException(); // TODO: the object is in wrong state
1119 DWORD nMSAspect = static_cast<DWORD>(nAspect); // first 32 bits are for MS aspects
1120 awt::Size aSize;
1121 bool bGotSize = false;
1123 if ( nMSAspect == DVASPECT_CONTENT )
1125 // Try to get the size from the replacement image first
1126 ComSmart< IDataObject > pDataObject;
1127 HRESULT hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pDataObject) );
1128 if ( SUCCEEDED( hr ) || pDataObject )
1130 STGMEDIUM aMedium;
1131 FORMATETC aFormat = pFormatTemplates[1]; // use windows metafile format
1132 aFormat.dwAspect = nMSAspect;
1134 hr = pDataObject->GetData( &aFormat, &aMedium );
1136 if (hr == RPC_E_WRONG_THREAD)
1138 // Assume that the OLE object was loaded on the main thread.
1139 vcl::solarthread::syncExecute([this, &hr, &pDataObject, &aFormat, &aMedium]() {
1140 // Make sure that the current state is embed::EmbedStates::RUNNING.
1141 RunObject();
1142 // Now try again on the correct thread.
1143 hr = pDataObject->GetData(&aFormat, &aMedium);
1147 if ( SUCCEEDED( hr ) && aMedium.tymed == TYMED_MFPICT ) // Win Metafile
1149 METAFILEPICT* pMF = static_cast<METAFILEPICT*>(GlobalLock( aMedium.hMetaFilePict ));
1150 if ( pMF )
1152 // the object uses 0.01 mm as unit, so the metafile size should be converted to object unit
1153 sal_Int64 nMult = 1;
1154 sal_Int64 nDiv = 1;
1155 switch( pMF->mm )
1157 case MM_HIENGLISH:
1158 nMult = 254;
1159 nDiv = 100;
1160 break;
1162 case MM_LOENGLISH:
1163 nMult = 254;
1164 nDiv = 10;
1165 break;
1167 case MM_LOMETRIC:
1168 nMult = 10;
1169 break;
1171 case MM_TWIPS:
1172 nMult = 254;
1173 nDiv = 144;
1174 break;
1176 case MM_ISOTROPIC:
1177 case MM_ANISOTROPIC:
1178 case MM_HIMETRIC:
1179 // do nothing
1180 break;
1183 sal_Int64 nX = static_cast<sal_Int64>(abs( pMF->xExt )) * nMult / nDiv;
1184 sal_Int64 nY = static_cast<sal_Int64>(abs( pMF->yExt )) * nMult / nDiv;
1185 if ( nX < SAL_MAX_INT32 && nY < SAL_MAX_INT32 )
1187 aSize.Width = static_cast<sal_Int32>(nX);
1188 aSize.Height = static_cast<sal_Int32>(nY);
1189 bGotSize = true;
1191 else
1192 OSL_FAIL( "Unexpected size is provided!" );
1195 else if (!SUCCEEDED(hr))
1197 SAL_WARN("embeddedobj.ole", " OleComponent::GetExtent: GetData() failed");
1199 // i113605, to release storage medium
1200 if ( SUCCEEDED( hr ) )
1201 ::ReleaseStgMedium(&aMedium);
1205 if ( !bGotSize )
1206 throw lang::IllegalArgumentException();
1208 return aSize;
1212 awt::Size OleComponent::GetCachedExtent( sal_Int64 nAspect )
1214 if ( !m_pNativeImpl->m_pOleObject )
1215 throw embed::WrongStateException(); // TODO: the object is in wrong state
1217 DWORD nMSAspect = static_cast<DWORD>(nAspect); // first 32 bits are for MS aspects
1218 SIZEL aSize;
1220 HRESULT hr = m_pNativeImpl->m_pViewObject2->GetExtent( nMSAspect, -1, nullptr, &aSize );
1222 if ( FAILED( hr ) )
1224 // TODO/LATER: is it correct?
1225 // if there is no appropriate cache for the aspect, OLE_E_BLANK error code is returned
1226 // if ( hr == OLE_E_BLANK )
1227 // throw lang::IllegalArgumentException();
1228 //else
1229 // throw io::IOException(); // TODO
1231 SAL_WARN("embeddedobj.ole", " OleComponent::GetCachedExtent: GetExtent() failed");
1232 throw lang::IllegalArgumentException();
1235 return awt::Size( aSize.cx, aSize.cy );
1239 awt::Size OleComponent::GetRecommendedExtent( sal_Int64 nAspect )
1241 if ( !m_pNativeImpl->m_pOleObject )
1242 throw embed::WrongStateException(); // TODO: the object is in wrong state
1244 DWORD nMSAspect = static_cast<DWORD>(nAspect); // first 32 bits are for MS aspects
1245 SIZEL aSize;
1246 HRESULT hr = m_pNativeImpl->m_pOleObject->GetExtent( nMSAspect, &aSize );
1247 if ( FAILED( hr ) )
1249 SAL_WARN("embeddedobj.ole", " OleComponent::GetRecommendedExtent: GetExtent() failed");
1250 throw lang::IllegalArgumentException();
1253 return awt::Size( aSize.cx, aSize.cy );
1257 sal_Int64 OleComponent::GetMiscStatus( sal_Int64 nAspect )
1259 if ( !m_pNativeImpl->m_pOleObject )
1260 throw embed::WrongStateException(); // TODO: the object is in wrong state
1262 DWORD nResult;
1263 m_pNativeImpl->m_pOleObject->GetMiscStatus( static_cast<DWORD>(nAspect), &nResult );
1264 return static_cast<sal_Int64>(nResult); // first 32 bits are for MS flags
1268 uno::Sequence< sal_Int8 > OleComponent::GetCLSID()
1270 if ( !m_pNativeImpl->m_pOleObject )
1271 throw embed::WrongStateException(); // TODO: the object is in wrong state
1273 GUID aCLSID;
1274 HRESULT hr = m_pNativeImpl->m_pOleObject->GetUserClassID( &aCLSID );
1275 if ( FAILED( hr ) )
1276 throw io::IOException(); // TODO:
1278 return MimeConfigurationHelper::GetSequenceClassID( aCLSID.Data1, aCLSID.Data2, aCLSID.Data3,
1279 aCLSID.Data4[0], aCLSID.Data4[1],
1280 aCLSID.Data4[2], aCLSID.Data4[3],
1281 aCLSID.Data4[4], aCLSID.Data4[5],
1282 aCLSID.Data4[6], aCLSID.Data4[7] );
1286 bool OleComponent::IsDirty()
1288 if ( !m_pNativeImpl->m_pOleObject )
1289 throw embed::WrongStateException(); // TODO: the object is in wrong state
1291 if ( IsWorkaroundActive() )
1292 return true;
1294 ComSmart< IPersistStorage > pPersistStorage;
1295 HRESULT hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IPersistStorage, reinterpret_cast<void**>(&pPersistStorage) );
1296 if ( FAILED( hr ) || !pPersistStorage )
1297 throw io::IOException(); // TODO
1299 hr = pPersistStorage->IsDirty();
1300 return ( hr != S_FALSE );
1304 void OleComponent::StoreOwnTmpIfNecessary()
1306 if ( !m_pNativeImpl->m_pOleObject )
1307 throw embed::WrongStateException(); // TODO: the object is in wrong state
1309 ComSmart< IPersistStorage > pPersistStorage;
1310 HRESULT hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IPersistStorage, reinterpret_cast<void**>(&pPersistStorage) );
1311 if ( FAILED( hr ) || !pPersistStorage )
1312 throw io::IOException(); // TODO
1314 if ( m_bWorkaroundActive || pPersistStorage->IsDirty() != S_FALSE )
1316 hr = OleSave( pPersistStorage, m_pNativeImpl->m_pIStorage, TRUE );
1317 if ( FAILED( hr ) )
1319 // Till now was required only for AcrobatReader7.0.8
1320 GUID aCLSID;
1321 hr = m_pNativeImpl->m_pOleObject->GetUserClassID( &aCLSID );
1322 if ( FAILED( hr ) )
1324 SAL_WARN("embeddedobj.ole", "OleComponent::StoreOwnTmpIfNecessary: GetUserClassID() failed");
1325 throw io::IOException(); // TODO
1328 hr = WriteClassStg( m_pNativeImpl->m_pIStorage, aCLSID );
1329 if ( FAILED( hr ) )
1330 throw io::IOException(); // TODO
1332 // the result of the following call is not checked because some objects, for example AcrobatReader7.0.8
1333 // return error even in case the saving was done correctly
1334 hr = pPersistStorage->Save( m_pNativeImpl->m_pIStorage, TRUE );
1336 // another workaround for AcrobatReader7.0.8 object, this object might think that it is not changed
1337 // when it has been created from file, although it must be saved
1338 m_bWorkaroundActive = true;
1341 hr = m_pNativeImpl->m_pIStorage->Commit( STGC_DEFAULT );
1342 if ( FAILED( hr ) )
1343 throw io::IOException(); // TODO
1345 hr = pPersistStorage->SaveCompleted( nullptr );
1346 if ( FAILED( hr ) && hr != E_UNEXPECTED )
1347 throw io::IOException(); // TODO
1353 bool OleComponent::SaveObject_Impl()
1355 bool bResult = false;
1356 OleEmbeddedObject* pLockObject = nullptr;
1359 osl::MutexGuard aGuard( m_aMutex );
1360 if ( m_pUnoOleObject )
1362 pLockObject = m_pUnoOleObject;
1363 pLockObject->acquire();
1367 if ( pLockObject )
1369 bResult = pLockObject->SaveObject_Impl();
1370 pLockObject->release();
1373 return bResult;
1377 bool OleComponent::OnShowWindow_Impl( bool bShow )
1379 bool bResult = false;
1380 OleEmbeddedObject* pLockObject = nullptr;
1383 osl::MutexGuard aGuard( m_aMutex );
1385 if ( m_pUnoOleObject )
1387 pLockObject = m_pUnoOleObject;
1388 pLockObject->acquire();
1392 if ( pLockObject )
1394 bResult = pLockObject->OnShowWindow_Impl( bShow );
1395 pLockObject->release();
1398 return bResult;
1402 void OleComponent::OnViewChange_Impl( sal_uInt32 dwAspect )
1404 // TODO: check if it is enough or may be saving notifications are required for Visio2000
1405 ::rtl::Reference< OleEmbeddedObject > xLockObject;
1408 osl::MutexGuard aGuard( m_aMutex );
1409 if ( m_pUnoOleObject )
1410 xLockObject = m_pUnoOleObject;
1413 if ( xLockObject.is() )
1415 uno::Reference < awt::XRequestCallback > xRequestCallback(
1416 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.awt.AsyncCallback", m_xContext),
1417 uno::UNO_QUERY );
1418 xRequestCallback->addCallback( new MainThreadNotificationRequest( xLockObject, OLECOMP_ONVIEWCHANGE, dwAspect ), uno::Any() );
1423 void OleComponent::OnClose_Impl()
1425 ::rtl::Reference< OleEmbeddedObject > xLockObject;
1428 osl::MutexGuard aGuard( m_aMutex );
1429 if ( m_pUnoOleObject )
1430 xLockObject = m_pUnoOleObject;
1433 if ( xLockObject.is() )
1435 uno::Reference < awt::XRequestCallback > xRequestCallback(
1436 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.awt.AsyncCallback", m_xContext),
1437 uno::UNO_QUERY );
1438 xRequestCallback->addCallback( new MainThreadNotificationRequest( xLockObject, OLECOMP_ONCLOSE ), uno::Any() );
1442 // XCloseable
1444 void SAL_CALL OleComponent::close( sal_Bool bDeliverOwnership )
1446 uno::Reference< uno::XInterface > xSelfHold;
1448 osl::MutexGuard aGuard(m_aMutex);
1449 if (m_bDisposed)
1450 throw lang::DisposedException(); // TODO
1452 xSelfHold.set(static_cast<::cppu::OWeakObject*>(this));
1453 lang::EventObject aSource(static_cast<::cppu::OWeakObject*>(this));
1455 if (m_pInterfaceContainer)
1457 ::cppu::OInterfaceContainerHelper* pContainer
1458 = m_pInterfaceContainer->getContainer(cppu::UnoType<util::XCloseListener>::get());
1459 if (pContainer != nullptr)
1461 ::cppu::OInterfaceIteratorHelper pIterator(*pContainer);
1462 while (pIterator.hasMoreElements())
1466 static_cast<util::XCloseListener*>(pIterator.next())
1467 ->queryClosing(aSource, bDeliverOwnership);
1469 catch (const uno::RuntimeException&)
1471 pIterator.remove();
1476 pContainer
1477 = m_pInterfaceContainer->getContainer(cppu::UnoType<util::XCloseListener>::get());
1478 if (pContainer != nullptr)
1480 ::cppu::OInterfaceIteratorHelper pCloseIterator(*pContainer);
1481 while (pCloseIterator.hasMoreElements())
1485 static_cast<util::XCloseListener*>(pCloseIterator.next())
1486 ->notifyClosing(aSource);
1488 catch (const uno::RuntimeException&)
1490 pCloseIterator.remove();
1497 Dispose();
1501 void SAL_CALL OleComponent::addCloseListener( const uno::Reference< util::XCloseListener >& xListener )
1503 ::osl::MutexGuard aGuard( m_aMutex );
1504 if ( m_bDisposed )
1505 throw lang::DisposedException(); // TODO
1507 if ( !m_pInterfaceContainer )
1508 m_pInterfaceContainer = new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex );
1510 m_pInterfaceContainer->addInterface( cppu::UnoType<util::XCloseListener>::get(), xListener );
1514 void SAL_CALL OleComponent::removeCloseListener( const uno::Reference< util::XCloseListener >& xListener )
1516 ::osl::MutexGuard aGuard( m_aMutex );
1517 if ( m_bDisposed )
1518 throw lang::DisposedException(); // TODO
1520 if ( m_pInterfaceContainer )
1521 m_pInterfaceContainer->removeInterface( cppu::UnoType<util::XCloseListener>::get(),
1522 xListener );
1525 // XTransferable
1527 uno::Any SAL_CALL OleComponent::getTransferData( const datatransfer::DataFlavor& aFlavor )
1529 ::osl::MutexGuard aGuard( m_aMutex );
1530 if ( m_bDisposed )
1531 throw lang::DisposedException(); // TODO
1533 if ( !m_pNativeImpl->m_pOleObject )
1534 throw embed::WrongStateException(); // TODO: the object is in wrong state
1536 uno::Any aResult;
1537 bool bSupportedFlavor = false;
1539 if ( m_pNativeImpl->GraphicalFlavor( aFlavor ) )
1541 DWORD nRequestedAspect = GetAspectFromFlavor( aFlavor );
1542 // if own icon is set and icon aspect is requested the own icon can be returned directly
1544 ComSmart< IDataObject > pDataObject;
1545 HRESULT hr = m_pNativeImpl->m_pObj->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pDataObject) );
1546 if ( FAILED( hr ) || !pDataObject )
1547 throw io::IOException(); // TODO: transport error code
1549 // The following optimization does not make much sense currently just because
1550 // only one aspect is supported, and only three formats for the aspect are supported
1551 // and moreover it is not guaranteed that the once returned format will be supported further
1552 // example - i52106
1553 // TODO/LATER: bring the optimization back when other aspects are supported
1555 // FORMATETC* pFormatEtc = m_pNativeImpl->GetSupportedFormatForAspect( nRequestedAspect );
1556 // if ( pFormatEtc )
1557 // {
1558 // STGMEDIUM aMedium;
1559 // hr = pDataObject->GetData( pFormatEtc, &aMedium );
1560 // if ( SUCCEEDED( hr ) )
1561 // bSupportedFlavor = m_pNativeImpl->ConvertDataForFlavor( aMedium, aFlavor, aResult );
1562 // }
1563 // else
1565 // the supported format of the application is still not found, find one
1566 for ( sal_Int32 nInd = 0; nInd < FORMATS_NUM; nInd++ )
1568 STGMEDIUM aMedium;
1569 FORMATETC aFormat = pFormatTemplates[nInd];
1570 aFormat.dwAspect = nRequestedAspect;
1572 hr = pDataObject->GetData( &aFormat, &aMedium );
1573 if ( SUCCEEDED( hr ) )
1575 bSupportedFlavor = m_pNativeImpl->ConvertDataForFlavor( aMedium, aFlavor, aResult );
1576 ::ReleaseStgMedium(&aMedium); // i113605, to release storage medium
1577 if ( bSupportedFlavor )
1579 // TODO/LATER: bring the optimization back when other aspects are supported
1580 // m_pNativeImpl->AddSupportedFormat( aFormat );
1581 break;
1587 // If the replacement could not be retrieved, the cached representation should be used
1588 // currently it is not necessary to retrieve it here, so it is implemented in the object itself
1590 // TODO: Investigate if there is already the format name
1591 // and whether this format is really required
1592 else if ( aFlavor.DataType == cppu::UnoType<io::XInputStream>::get()
1593 && aFlavor.MimeType == "application/x-openoffice-contentstream" )
1595 // allow to retrieve stream-representation of the object persistence
1596 bSupportedFlavor = true;
1597 uno::Reference < io::XStream > xTempFileStream(
1598 io::TempFile::create(m_xContext),
1599 uno::UNO_QUERY_THROW );
1601 uno::Reference< io::XOutputStream > xTempOutStream = xTempFileStream->getOutputStream();
1602 uno::Reference< io::XInputStream > xTempInStream = xTempFileStream->getInputStream();
1603 if ( !(xTempOutStream.is() && xTempInStream.is()) )
1604 throw io::IOException(); // TODO:
1606 OSL_ENSURE( m_pUnoOleObject, "Unexpected object absence!" );
1607 if ( !m_pUnoOleObject )
1608 throw uno::RuntimeException();
1610 m_pUnoOleObject->StoreObjectToStream( xTempOutStream );
1612 xTempOutStream->closeOutput();
1613 xTempOutStream.clear();
1615 aResult <<= xTempInStream;
1618 if ( !bSupportedFlavor )
1619 throw datatransfer::UnsupportedFlavorException();
1621 return aResult;
1625 uno::Sequence< datatransfer::DataFlavor > SAL_CALL OleComponent::getTransferDataFlavors()
1627 ::osl::MutexGuard aGuard( m_aMutex );
1628 if ( m_bDisposed )
1629 throw lang::DisposedException(); // TODO
1631 if ( !m_pNativeImpl->m_pOleObject )
1632 throw embed::WrongStateException(); // TODO: the object is in wrong state
1634 RetrieveObjectDataFlavors_Impl();
1636 return m_aDataFlavors;
1640 sal_Bool SAL_CALL OleComponent::isDataFlavorSupported( const datatransfer::DataFlavor& aFlavor )
1642 ::osl::MutexGuard aGuard( m_aMutex );
1643 if ( m_bDisposed )
1644 throw lang::DisposedException(); // TODO
1646 if ( !m_pNativeImpl->m_pOleObject )
1647 throw embed::WrongStateException(); // TODO: the object is in wrong state
1649 if ( !m_aDataFlavors.getLength() )
1651 RetrieveObjectDataFlavors_Impl();
1654 for ( auto const & supportedFormat : std::as_const(m_aDataFlavors) )
1655 if ( supportedFormat.MimeType.equals( aFlavor.MimeType ) && supportedFormat.DataType == aFlavor.DataType )
1656 return true;
1658 return false;
1661 void SAL_CALL OleComponent::dispose()
1665 close( true );
1667 catch ( const uno::Exception& )
1672 void SAL_CALL OleComponent::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
1674 ::osl::MutexGuard aGuard( m_aMutex );
1675 if ( m_bDisposed )
1676 throw lang::DisposedException(); // TODO
1678 if ( !m_pInterfaceContainer )
1679 m_pInterfaceContainer = new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex );
1681 m_pInterfaceContainer->addInterface( cppu::UnoType<lang::XEventListener>::get(), xListener );
1685 void SAL_CALL OleComponent::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
1687 ::osl::MutexGuard aGuard( m_aMutex );
1688 if ( m_bDisposed )
1689 throw lang::DisposedException(); // TODO
1691 if ( m_pInterfaceContainer )
1692 m_pInterfaceContainer->removeInterface( cppu::UnoType<lang::XEventListener>::get(),
1693 xListener );
1696 sal_Int64 SAL_CALL OleComponent::getSomething( const css::uno::Sequence< sal_Int8 >& aIdentifier )
1700 uno::Sequence < sal_Int8 > aCLSID = GetCLSID();
1701 if ( MimeConfigurationHelper::ClassIDsEqual( aIdentifier, aCLSID ) )
1702 return reinterpret_cast<sal_Int64>(static_cast<IUnknown*>(m_pNativeImpl->m_pObj));
1704 // compatibility hack for old versions: CLSID was used in wrong order (SvGlobalName order)
1705 sal_Int32 nLength = aIdentifier.getLength();
1706 if ( nLength == 16 )
1708 for ( sal_Int32 n=8; n<16; n++ )
1709 if ( aIdentifier[n] != aCLSID[n] )
1710 return 0;
1711 if ( aIdentifier[7] == aCLSID[6] &&
1712 aIdentifier[6] == aCLSID[7] &&
1713 aIdentifier[5] == aCLSID[4] &&
1714 aIdentifier[4] == aCLSID[5] &&
1715 aIdentifier[3] == aCLSID[0] &&
1716 aIdentifier[2] == aCLSID[1] &&
1717 aIdentifier[1] == aCLSID[2] &&
1718 aIdentifier[0] == aCLSID[3] )
1719 return reinterpret_cast<sal_Int64>(static_cast<IUnknown*>(m_pNativeImpl->m_pObj));
1722 catch ( const uno::Exception& )
1726 return 0;
1729 sal_Bool SAL_CALL OleComponent::isModified()
1731 return m_bModified;
1734 void SAL_CALL OleComponent::setModified( sal_Bool bModified )
1736 m_bModified = bModified;
1738 if ( bModified && m_pInterfaceContainer )
1740 ::cppu::OInterfaceContainerHelper* pContainer =
1741 m_pInterfaceContainer->getContainer( cppu::UnoType<util::XModifyListener>::get());
1742 if ( pContainer != nullptr )
1744 ::cppu::OInterfaceIteratorHelper pIterator( *pContainer );
1745 while ( pIterator.hasMoreElements() )
1749 lang::EventObject aEvent( static_cast<util::XModifiable*>(this) );
1750 static_cast<util::XModifyListener*>(pIterator.next())->modified( aEvent );
1752 catch( const uno::RuntimeException& )
1754 pIterator.remove();
1761 void SAL_CALL OleComponent::addModifyListener( const css::uno::Reference < css::util::XModifyListener >& xListener )
1763 ::osl::MutexGuard aGuard( m_aMutex );
1764 if ( m_bDisposed )
1765 throw lang::DisposedException(); // TODO
1767 if ( !m_pInterfaceContainer )
1768 m_pInterfaceContainer = new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex );
1770 m_pInterfaceContainer->addInterface( cppu::UnoType<util::XModifyListener>::get(), xListener );
1773 void SAL_CALL OleComponent::removeModifyListener( const css::uno::Reference < css::util::XModifyListener >& xListener)
1775 ::osl::MutexGuard aGuard( m_aMutex );
1776 if ( m_bDisposed )
1777 throw lang::DisposedException(); // TODO
1779 if ( m_pInterfaceContainer )
1780 m_pInterfaceContainer->removeInterface( cppu::UnoType<util::XModifyListener>::get(),
1781 xListener );
1784 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */