LibreOffice Module embeddedobj (master) 1
olecomponent.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21
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>
33
34#include "platform.h"
40#include <osl/file.hxx>
41#include <rtl/ref.hxx>
44#include <systools/win32/comtools.hxx>
45#include <vcl/threadex.hxx>
46
47#include "graphconvert.hxx"
48#include "olecomponent.hxx"
49#include "olepersist.hxx"
50#include "olewrapclient.hxx"
51#include "advisesink.hxx"
52#include <oleembobj.hxx>
53#include "mtnotification.hxx"
54#include <memory>
55#include <string>
56
57using namespace ::com::sun::star;
58using namespace ::comphelper;
59#define MAX_ENUM_ELE 20
60#define FORMATS_NUM 3
61
62typedef std::vector< FORMATETC* > FormatEtcList;
63
64FORMATETC const pFormatTemplates[FORMATS_NUM] = {
65 { CF_ENHMETAFILE, nullptr, 0, -1, TYMED_ENHMF },
66 { CF_METAFILEPICT, nullptr, 0, -1, TYMED_MFPICT },
67 { CF_BITMAP, nullptr, 0, -1, TYMED_GDI } };
68
69
71 sal::systools::COMReference< IUnknown > m_pObj;
72 sal::systools::COMReference< IOleObject > m_pOleObject;
73 sal::systools::COMReference< IViewObject2 > m_pViewObject2;
74 sal::systools::COMReference< IStorage > m_pIStorage;
76 uno::Sequence< datatransfer::DataFlavor > m_aSupportedGraphFormats;
77
79 {
80 // TODO: Extend format list
82
83 datatransfer::DataFlavor(
84 "application/x-openoffice-emf;windows_formatname=\"Image EMF\"",
85 "Windows Enhanced Metafile",
86 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() ),
87
88 datatransfer::DataFlavor(
89 "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"",
90 "Windows Metafile",
91 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() ),
92
93 datatransfer::DataFlavor(
94 "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"",
95 "Bitmap",
96 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() ),
97
98 datatransfer::DataFlavor(
99 "image/png",
100 "PNG",
101 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() ),
102
103 datatransfer::DataFlavor(
104 "application/x-openoffice-gdimetafile;windows_formatname=\"GDIMetaFile\"",
105 "GDIMetafile",
106 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() )
107 };
108 }
109
110 bool ConvertDataForFlavor( const STGMEDIUM& aMedium,
111 const datatransfer::DataFlavor& aFlavor,
112 uno::Any& aResult );
113
114 bool GraphicalFlavor( const datatransfer::DataFlavor& aFlavor );
115
116 uno::Sequence< datatransfer::DataFlavor > GetFlavorsForAspects( sal_uInt32 nSupportedAspects );
117};
118
119
120static DWORD GetAspectFromFlavor( const datatransfer::DataFlavor& aFlavor )
121{
122 if ( aFlavor.MimeType.indexOf( ";Aspect=THUMBNAIL" ) != -1 )
123 return DVASPECT_THUMBNAIL;
124 else if ( aFlavor.MimeType.indexOf( ";Aspect=ICON" ) != -1 )
125 return DVASPECT_ICON;
126 else if ( aFlavor.MimeType.indexOf( ";Aspect=DOCPRINT" ) != -1 )
127 return DVASPECT_DOCPRINT;
128 else
129 return DVASPECT_CONTENT;
130}
131
132
133static OUString GetFlavorSuffixFromAspect( DWORD nAsp )
134{
135 OUString aResult;
136
137 if ( nAsp == DVASPECT_THUMBNAIL )
138 aResult = ";Aspect=THUMBNAIL";
139 else if ( nAsp == DVASPECT_ICON )
140 aResult = ";Aspect=ICON";
141 else if ( nAsp == DVASPECT_DOCPRINT )
142 aResult = ";Aspect=DOCPRINT";
143
144 // no suffix for DVASPECT_CONTENT
145
146 return aResult;
147}
148
149
150static HRESULT OpenIStorageFromURL_Impl( const OUString& aURL, IStorage** ppIStorage )
151{
152 OSL_ENSURE( ppIStorage, "The pointer must not be empty!" );
153
154 OUString aFilePath;
155 if ( !ppIStorage || ::osl::FileBase::getSystemPathFromFileURL( aURL, aFilePath ) != ::osl::FileBase::E_None )
156 throw uno::RuntimeException(); // TODO: something dangerous happened
157
158 return StgOpenStorage( o3tl::toW(aFilePath.getStr()),
159 nullptr,
160 STGM_READWRITE | STGM_TRANSACTED, // | STGM_DELETEONRELEASE,
161 nullptr,
162 0,
163 ppIStorage );
164}
165
166
168 const datatransfer::DataFlavor& aFlavor,
169 uno::Any& aResult )
170{
171 bool bAnyIsReady = false;
172
173 // try to convert data from Medium format to specified Flavor format
174 if ( aFlavor.DataType == cppu::UnoType<uno::Sequence< sal_Int8 >>::get() )
175 {
176 // first the GDI-metafile must be generated
177
178 std::unique_ptr<sal_Int8[]> pBuf;
179 sal_uInt32 nBufSize = 0;
180 OUString aFormat;
181
182 if ( aMedium.tymed == TYMED_MFPICT ) // Win Metafile
183 {
184 aFormat = "image/x-wmf";
185 METAFILEPICT* pMF = static_cast<METAFILEPICT*>(GlobalLock( aMedium.hMetaFilePict ));
186 if ( pMF )
187 {
188 nBufSize = GetMetaFileBitsEx( pMF->hMF, 0, nullptr ) + 22;
189 pBuf.reset(new sal_Int8[nBufSize]);
190
191
192 // TODO/LATER: the unit size must be calculated correctly
193 *reinterpret_cast<long*>( pBuf.get() ) = 0x9ac6cdd7L;
194 *reinterpret_cast<short*>( pBuf.get()+6 ) = SHORT(0);
195 *reinterpret_cast<short*>( pBuf.get()+8 ) = SHORT(0);
196 *reinterpret_cast<short*>( pBuf.get()+10 ) = static_cast<SHORT>(pMF->xExt);
197 *reinterpret_cast<short*>( pBuf.get()+12 ) = static_cast<SHORT>(pMF->yExt);
198 *reinterpret_cast<short*>( pBuf.get()+14 ) = USHORT(2540);
199
200
201 if ( nBufSize && nBufSize == GetMetaFileBitsEx( pMF->hMF, nBufSize - 22, pBuf.get() + 22 ) )
202 {
203 if ( aFlavor.MimeType.matchAsciiL( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"", 57 ) )
204 {
205 aResult <<= uno::Sequence< sal_Int8 >( pBuf.get(), nBufSize );
206 bAnyIsReady = true;
207 }
208 }
209
210 GlobalUnlock( aMedium.hMetaFilePict );
211 }
212 }
213 else if ( aMedium.tymed == TYMED_ENHMF ) // Enh Metafile
214 {
215 aFormat = "image/x-emf";
216 nBufSize = GetEnhMetaFileBits( aMedium.hEnhMetaFile, 0, nullptr );
217 pBuf.reset(new sal_Int8[nBufSize]);
218 if ( nBufSize && nBufSize == GetEnhMetaFileBits( aMedium.hEnhMetaFile, nBufSize, reinterpret_cast<LPBYTE>(pBuf.get()) ) )
219 {
220 if ( aFlavor.MimeType.matchAsciiL( "application/x-openoffice-emf;windows_formatname=\"Image EMF\"", 57 ) )
221 {
222 aResult <<= uno::Sequence< sal_Int8 >( pBuf.get(), nBufSize );
223 bAnyIsReady = true;
224 }
225 }
226 }
227 else if ( aMedium.tymed == TYMED_GDI ) // Bitmap
228 {
229 aFormat = "image/x-MS-bmp";
230
231 // Find out size of buffer: deprecated GetBitmapBits does not have a mode to return
232 // required buffer size
233 BITMAP aBmp;
234 GetObjectW(aMedium.hBitmap, sizeof(aBmp), &aBmp);
235 nBufSize = aBmp.bmWidthBytes * aBmp.bmHeight;
236
237 pBuf.reset(new sal_Int8[nBufSize]);
238 if ( nBufSize && nBufSize == sal::static_int_cast< ULONG >( GetBitmapBits( aMedium.hBitmap, nBufSize, pBuf.get() ) ) )
239 {
240 if ( aFlavor.MimeType.matchAsciiL( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"", 54 ) )
241 {
242 aResult <<= uno::Sequence< sal_Int8 >( pBuf.get(), nBufSize );
243 bAnyIsReady = true;
244 }
245 }
246 }
247
248 if ( pBuf && !bAnyIsReady )
249 {
250 for ( auto const & supportedFormat : std::as_const(m_aSupportedGraphFormats) )
251 if ( aFlavor.MimeType.match( supportedFormat.MimeType )
252 && aFlavor.DataType == supportedFormat.DataType
253 && aFlavor.DataType == cppu::UnoType<uno::Sequence< sal_Int8 >>::get() )
254 {
255 bAnyIsReady = ConvertBufferToFormat( pBuf.get(), nBufSize, aFormat, aResult );
256 break;
257 }
258 }
259 }
260
261 return bAnyIsReady;
262}
263
264
265bool OleComponentNative_Impl::GraphicalFlavor( const datatransfer::DataFlavor& aFlavor )
266{
267 // Actually all the required graphical formats must be supported
268 for ( auto const & supportedFormat : std::as_const(m_aSupportedGraphFormats) )
269 if ( aFlavor.MimeType.match( supportedFormat.MimeType )
270 && aFlavor.DataType == supportedFormat.DataType )
271 return true;
272
273 return false;
274}
275
276
277static bool GetClassIDFromSequence_Impl( uno::Sequence< sal_Int8 > const & aSeq, CLSID& aResult )
278{
279 if ( aSeq.getLength() == 16 )
280 {
281 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]);
282 aResult.Data2 = ( static_cast<sal_uInt8>(aSeq[4]) << 8 ) + static_cast<sal_uInt8>(aSeq[5]);
283 aResult.Data3 = ( static_cast<sal_uInt8>(aSeq[6]) << 8 ) + static_cast<sal_uInt8>(aSeq[7]);
284 for( int nInd = 0; nInd < 8; nInd++ )
285 aResult.Data4[nInd] = static_cast<sal_uInt8>(aSeq[nInd+8]);
286
287 return true;
288 }
289
290 return false;
291}
292
293
294static OUString WinAccToVcl_Impl( const sal_Unicode* pStr )
295{
296 OUString aResult;
297
298 if( pStr )
299 {
300 while ( *pStr )
301 {
302 if ( *pStr == '&' )
303 {
304 aResult += "~";
305 while( *( ++pStr ) == '&' );
306 }
307 else
308 {
309 aResult += OUStringChar( *pStr );
310 pStr++;
311 }
312 }
313 }
314
315 return aResult;
316}
317
318
319OleComponent::OleComponent( const uno::Reference< uno::XComponentContext >& xContext, OleEmbeddedObject* pUnoOleObject )
320: m_pInterfaceContainer( nullptr )
321, m_bDisposed( false )
322, m_bModified( false )
323, m_pNativeImpl( new OleComponentNative_Impl() )
324, m_pUnoOleObject( pUnoOleObject )
325, m_pOleWrapClientSite( nullptr )
326, m_pImplAdviseSink( nullptr )
327, m_nOLEMiscFlags( 0 )
328, m_nAdvConn( 0 )
329, m_xContext( xContext )
330, m_bOleInitialized( false )
331, m_bWorkaroundActive( false )
332{
333 OSL_ENSURE( m_pUnoOleObject, "No owner object is provided!" );
334
335 HRESULT hr = OleInitialize( nullptr );
336 if ( hr == S_OK || hr == S_FALSE )
337 m_bOleInitialized = true;
338 else
339 {
340 SAL_WARN("embeddedobj.ole", "OleComponent ctor: OleInitialize() failed with 0x"
341 << OUString::number(static_cast<sal_uInt32>(hr), 16) << ": "
342 << WindowsErrorStringFromHRESULT(hr));
343 }
344
346 m_pOleWrapClientSite->AddRef();
347
349 m_pImplAdviseSink->AddRef();
350
351}
352
353
355{
357 "The object was not closed successfully! DISASTER is possible!" );
358
360 {
361 osl_atomic_increment(&m_refCount);
362 try {
363 Dispose();
364 } catch( const uno::Exception& ) {}
365 }
366
367 for (auto const& format : m_pNativeImpl->m_aFormatsList)
368 {
369 delete format;
370 }
372
373 delete m_pNativeImpl;
374}
375
377{
378 if ( m_bDisposed )
379 return;
380
381 // Call CloseObject() without m_aMutex locked, since it will call
382 // IOleObject::Close(), which can call event listeners, which can run on a
383 // different thread.
384 CloseObject();
385
386 osl::MutexGuard aGuard(m_aMutex);
388 {
390 m_pOleWrapClientSite->Release();
391 m_pOleWrapClientSite = nullptr;
392 }
393
394 if ( m_pImplAdviseSink )
395 {
397 m_pImplAdviseSink->Release();
398 m_pImplAdviseSink = nullptr;
399 }
400
402 {
403 lang::EventObject aEvent( static_cast< ::cppu::OWeakObject* >( this ) );
405
407 m_pInterfaceContainer = nullptr;
408 }
409
410 if ( m_bOleInitialized )
411 {
412 // since the disposing can happen not only from main thread but also from a clipboard
413 // the deinitialization might lead to a disaster, SO7 does not deinitialize OLE at all
414 // so currently the same approach is selected as workaround
415 // OleUninitialize();
416 m_bOleInitialized = false;
417 }
418
419 m_bDisposed = true;
420}
421
422
424{
425 // must not be called from destructor of UNO OLE object!!!
426 osl::MutexGuard aGuard( m_aMutex );
427 m_pUnoOleObject = nullptr;
428}
429
430
432{
433 // TODO: in future a global memory could be used instead of file.
434
435 // write the stream to the temporary file
436 OUString aTempURL;
437
438 OSL_ENSURE( m_pUnoOleObject, "Unexpected object absence!" );
439 if ( m_pUnoOleObject )
440 aTempURL = m_pUnoOleObject->CreateTempURLEmpty_Impl();
441 else
443
444 if ( !aTempURL.getLength() )
445 throw uno::RuntimeException(); // TODO
446
447 // open an IStorage based on the temporary file
448 OUString aTempFilePath;
449 if ( ::osl::FileBase::getSystemPathFromFileURL( aTempURL, aTempFilePath ) != ::osl::FileBase::E_None )
450 throw uno::RuntimeException(); // TODO: something dangerous happened
451
452 HRESULT hr = StgCreateDocfile( o3tl::toW(aTempFilePath.getStr()), STGM_CREATE | STGM_READWRITE | STGM_TRANSACTED | STGM_DELETEONRELEASE, 0, &m_pNativeImpl->m_pIStorage );
453 if ( FAILED( hr ) || !m_pNativeImpl->m_pIStorage )
454 throw io::IOException(); // TODO: transport error code?
455}
456
457
458uno::Sequence< datatransfer::DataFlavor > OleComponentNative_Impl::GetFlavorsForAspects( sal_uInt32 nSupportedAspects )
459{
460 uno::Sequence< datatransfer::DataFlavor > aResult;
461 for ( sal_uInt32 nAsp = 1; nAsp <= 8; nAsp *= 2 )
462 if ( ( nSupportedAspects & nAsp ) == nAsp )
463 {
464 OUString aAspectSuffix = GetFlavorSuffixFromAspect( nAsp );
465
466 sal_Int32 nLength = aResult.getLength();
467 aResult.realloc( nLength + m_aSupportedGraphFormats.getLength() );
468 auto pResult = aResult.getArray();
469
470 for ( sal_Int32 nInd = 0; nInd < m_aSupportedGraphFormats.getLength(); nInd++ )
471 {
472 pResult[nLength + nInd].MimeType = m_aSupportedGraphFormats[nInd].MimeType + aAspectSuffix;
473 pResult[nLength + nInd].HumanPresentableName = m_aSupportedGraphFormats[nInd].HumanPresentableName;
474 pResult[nLength + nInd].DataType = m_aSupportedGraphFormats[nInd].DataType;
475 }
476 }
477
478 return aResult;
479}
480
481
483{
485 throw embed::WrongStateException(); // TODO: the object is in wrong state
486
487 if ( !m_aDataFlavors.getLength() )
488 {
489 sal::systools::COMReference< IDataObject > pDataObject(m_pNativeImpl->m_pObj, sal::systools::COM_QUERY);
490 if ( pDataObject )
491 {
492 sal::systools::COMReference< IEnumFORMATETC > pFormatEnum;
493 HRESULT hr = pDataObject->EnumFormatEtc( DATADIR_GET, &pFormatEnum );
494 if ( SUCCEEDED( hr ) && pFormatEnum )
495 {
496 FORMATETC pElem[ MAX_ENUM_ELE ];
497 ULONG nNum = 0;
498
499 // if it is possible to retrieve at least one supported graphical format for an aspect
500 // this format can be converted to other supported formats
501 sal_uInt32 nSupportedAspects = 0;
502 do
503 {
504 HRESULT hr2 = pFormatEnum->Next( MAX_ENUM_ELE, pElem, &nNum );
505 if( hr2 == S_OK || hr2 == S_FALSE )
506 {
507 for( sal_uInt32 nInd = 0; nInd < FORMATS_NUM; nInd++ )
508 {
509 if ( pElem[nInd].cfFormat == pFormatTemplates[nInd].cfFormat
510 && pElem[nInd].tymed == pFormatTemplates[nInd].tymed )
511 nSupportedAspects |= pElem[nInd].dwAspect;
512 }
513 }
514 else
515 break;
516 }
517 while( nNum == MAX_ENUM_ELE );
518
519 m_aDataFlavors = m_pNativeImpl->GetFlavorsForAspects( nSupportedAspects );
520 }
521 }
522
523 if ( !m_aDataFlavors.getLength() )
524 {
525 // TODO:
526 // for any reason the object could not provide this information
527 // try to get access to the cached representation
528 }
529 }
530}
531
532
534// There will be no static objects!
535{
536 if ( !m_pNativeImpl->m_pObj )
537 return false;
538
539 // the linked object will be detected here
540 OSL_ENSURE( m_pUnoOleObject, "Unexpected object absence!" );
541 if ( m_pUnoOleObject )
542 m_pUnoOleObject->SetObjectIsLink_Impl( m_pNativeImpl->m_pObj.QueryInterface<IOleLink>(sal::systools::COM_QUERY).is() );
543
544 if ( !m_pNativeImpl->m_pViewObject2.set(m_pNativeImpl->m_pObj, sal::systools::COM_QUERY) )
545 return false;
546
547 // remove all the caches
548 if ( sal::systools::COMReference< IOleCache > pIOleCache{ m_pNativeImpl->m_pObj, sal::systools::COM_QUERY } )
549 {
550 IEnumSTATDATA* pEnumSD = nullptr;
551 HRESULT hr2 = pIOleCache->EnumCache( &pEnumSD );
552
553 if ( SUCCEEDED( hr2 ) && pEnumSD )
554 {
555 pEnumSD->Reset();
556 STATDATA aSD;
557 DWORD nNum;
558 while( SUCCEEDED( pEnumSD->Next( 1, &aSD, &nNum ) ) && nNum == 1 )
559 hr2 = pIOleCache->Uncache( aSD.dwConnection );
560 }
561
562 // No IDataObject implementation, caching must be used instead
563 DWORD nConn;
564 FORMATETC aFormat = { 0, nullptr, DVASPECT_CONTENT, -1, TYMED_MFPICT };
565 hr2 = pIOleCache->Cache( &aFormat, ADVFCACHE_ONSAVE, &nConn );
566 }
567
568 if ( !m_pNativeImpl->m_pOleObject.set(m_pNativeImpl->m_pObj, sal::systools::COM_QUERY) )
569 return false; // Static objects are not supported, they should be inserted as graphics
570
571 m_pNativeImpl->m_pOleObject->GetMiscStatus( DVASPECT_CONTENT, reinterpret_cast<DWORD*>(&m_nOLEMiscFlags) );
572 // TODO: use other misc flags also
573 // the object should have drawable aspect even in case it supports only iconic representation
574 // if ( m_nOLEMiscFlags & OLEMISC_ONLYICONIC )
575
577
578 // the only need in this registration is workaround for close notification
579 m_pNativeImpl->m_pOleObject->Advise( m_pImplAdviseSink, reinterpret_cast<DWORD*>(&m_nAdvConn) );
580 m_pNativeImpl->m_pViewObject2->SetAdvise( DVASPECT_CONTENT, 0, m_pImplAdviseSink );
581
582 OleSetContainedObject( m_pNativeImpl->m_pOleObject, TRUE );
583
584 return true;
585}
586
587namespace
588{
589 HRESULT OleLoadSeh(LPSTORAGE pIStorage, LPVOID* ppObj)
590 {
591 HRESULT hr = E_FAIL;
592 // tdf#119039: there is a nasty bug in OleLoad, that may call an unpaired
593 // IUnknown::Release on pIStorage on STG_E_FILENOTFOUND: see
594 // https://developercommunity.visualstudio.com/t/10144795
595 // Workaround it here to avoid crash in smart COM pointer destructor that
596 // would try to release already released object. Since we don't know if
597 // the bug appears each time STG_E_FILENOTFOUND is returned, this might
598 // potentially leak the storage object.
599 if (pIStorage)
600 pIStorage->AddRef();
601
602 __try {
603 hr = OleLoad(pIStorage, IID_IUnknown, nullptr, ppObj);
604 } __except( EXCEPTION_EXECUTE_HANDLER ) {
605 hr = E_FAIL;
606 }
607 if (pIStorage && hr != STG_E_FILENOTFOUND)
608 pIStorage->Release();
609
610 return hr;
611 }
612}
613
614void OleComponent::LoadEmbeddedObject( const OUString& aTempURL )
615{
616 if ( !aTempURL.getLength() )
617 throw lang::IllegalArgumentException(); // TODO
618
620 throw io::IOException(); // TODO the object is already initialized or wrong initialization is done
621
622 // open an IStorage based on the temporary file
623 HRESULT hr = OpenIStorageFromURL_Impl( aTempURL, &m_pNativeImpl->m_pIStorage );
624
625 if ( FAILED( hr ) || !m_pNativeImpl->m_pIStorage )
626 throw io::IOException(); // TODO: transport error code?
627
628 hr = OleLoadSeh(m_pNativeImpl->m_pIStorage, reinterpret_cast<void**>(&m_pNativeImpl->m_pObj));
629 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
630 {
631 throw uno::RuntimeException();
632 }
633
634 if ( !InitializeObject_Impl() )
635 throw uno::RuntimeException(); // TODO
636}
637
638
640{
642 throw io::IOException(); // TODO:the object is already initialized
643
646 throw uno::RuntimeException(); // TODO
647
648 IDataObject * pDO = nullptr;
649 HRESULT hr = OleGetClipboard( &pDO );
650 if( SUCCEEDED( hr ) && pDO )
651 {
652 hr = OleQueryCreateFromData( pDO );
653 if( S_OK == GetScode( hr ) )
654 {
655 hr = OleCreateFromData( pDO,
656 IID_IUnknown,
657 OLERENDER_DRAW, // OLERENDER_FORMAT
658 nullptr, // &aFormat,
659 nullptr,
661 reinterpret_cast<void**>(&m_pNativeImpl->m_pObj) );
662 }
663 else
664 {
665 // Static objects are not supported
666 pDO->Release();
667 }
668 }
669
670 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
671 throw uno::RuntimeException();
672
673 if ( !InitializeObject_Impl() )
674 throw uno::RuntimeException(); // TODO
675}
676
677
678void OleComponent::CreateNewEmbeddedObject( const uno::Sequence< sal_Int8 >& aSeqCLSID )
679{
680 CLSID aClsID;
681
682 if ( !GetClassIDFromSequence_Impl( aSeqCLSID, aClsID ) )
683 throw lang::IllegalArgumentException(); // TODO
684
686 throw io::IOException(); // TODO:the object is already initialized
687
690 throw uno::RuntimeException(); // TODO
691
692 // FORMATETC aFormat = { CF_METAFILEPICT, NULL, nAspect, -1, TYMED_MFPICT }; // for OLE..._DRAW should be NULL
693
694 HRESULT hr = OleCreate( aClsID,
695 IID_IUnknown,
696 OLERENDER_DRAW, // OLERENDER_FORMAT
697 nullptr, // &aFormat,
698 nullptr,
700 reinterpret_cast<void**>(&m_pNativeImpl->m_pObj) );
701
702 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
703 throw uno::RuntimeException(); // TODO
704
705 if ( !InitializeObject_Impl() )
706 throw uno::RuntimeException(); // TODO
707
708 // TODO: getExtent???
709}
710
711
712void OleComponent::CreateObjectFromData( const uno::Reference< datatransfer::XTransferable >& )
713// Static objects are not supported, they should be inserted as graphics
714{
715 // TODO: May be this call is useless since there are no static objects
716 // and nonstatic objects will be created based on OLEstorage ( stream ).
717 // ???
718
719 // OleQueryCreateFromData...
720}
721
722
723void OleComponent::CreateObjectFromFile( const OUString& aFileURL )
724{
726 throw io::IOException(); // TODO:the object is already initialized
727
730 throw uno::RuntimeException(); // TODO:
731
732 OUString aFilePath;
733 if ( ::osl::FileBase::getSystemPathFromFileURL( aFileURL, aFilePath ) != ::osl::FileBase::E_None )
734 throw uno::RuntimeException(); // TODO: something dangerous happened
735
736 HRESULT hr = OleCreateFromFile( CLSID_NULL,
737 o3tl::toW(aFilePath.getStr()),
738 IID_IUnknown,
739 OLERENDER_DRAW, // OLERENDER_FORMAT
740 nullptr,
741 nullptr,
743 reinterpret_cast<void**>(&m_pNativeImpl->m_pObj) );
744
745 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
746 throw uno::RuntimeException(); // TODO
747
748 if ( !InitializeObject_Impl() )
749 throw uno::RuntimeException(); // TODO
750}
751
752
753void OleComponent::CreateLinkFromFile( const OUString& aFileURL )
754{
756 throw io::IOException(); // TODO:the object is already initialized
757
760 throw uno::RuntimeException(); // TODO:
761
762 OUString aFilePath;
763 if ( ::osl::FileBase::getSystemPathFromFileURL( aFileURL, aFilePath ) != ::osl::FileBase::E_None )
764 throw uno::RuntimeException(); // TODO: something dangerous happened
765
766 HRESULT hr = OleCreateLinkToFile( o3tl::toW(aFilePath.getStr()),
767 IID_IUnknown,
768 OLERENDER_DRAW, // OLERENDER_FORMAT
769 nullptr,
770 nullptr,
772 reinterpret_cast<void**>(&m_pNativeImpl->m_pObj) );
773
774 if ( FAILED( hr ) || !m_pNativeImpl->m_pObj )
775 throw uno::RuntimeException(); // TODO
776
777 if ( !InitializeObject_Impl() )
778 throw uno::RuntimeException(); // TODO
779}
780
781
783{
784 if ( !pOleLinkComponent || !pOleLinkComponent->m_pNativeImpl->m_pObj )
785 throw lang::IllegalArgumentException(); // TODO
786
788 throw io::IOException(); // TODO:the object is already initialized
789
790 sal::systools::COMReference< IDataObject > pDataObject(pOleLinkComponent->m_pNativeImpl->m_pObj, sal::systools::COM_QUERY);
791 if ( pDataObject && SUCCEEDED( OleQueryCreateFromData( pDataObject ) ) )
792 {
793 // the object must be already disconnected from the temporary URL
796 throw uno::RuntimeException(); // TODO:
797
798 OleCreateFromData( pDataObject,
799 IID_IUnknown,
800 OLERENDER_DRAW,
801 nullptr,
802 nullptr,
804 reinterpret_cast<void**>(&m_pNativeImpl->m_pObj) );
805 }
806
807 if ( !m_pNativeImpl->m_pObj )
808 {
809 sal::systools::COMReference< IOleLink > pOleLink(m_pNativeImpl->m_pObj, sal::systools::COM_QUERY);
810 if ( !pOleLink )
811 throw io::IOException(); // TODO: the object doesn't support IOleLink
812
813 sal::systools::COMReference< IMoniker > pMoniker;
814 HRESULT hr = pOleLink->GetSourceMoniker( &pMoniker );
815 if ( FAILED( hr ) || !pMoniker )
816 throw io::IOException(); // TODO: can not retrieve moniker
817
818 // In case of file moniker life is easy : )
819 DWORD aMonType = 0;
820 hr = pMoniker->IsSystemMoniker( &aMonType );
821 if ( SUCCEEDED( hr ) && aMonType == MKSYS_FILEMONIKER )
822 {
823 sal::systools::COMReference< IMalloc > pMalloc;
824 hr = CoGetMalloc( 1, &pMalloc ); // if fails there will be a memory leak
825 OSL_ENSURE(SUCCEEDED(hr) && pMalloc, "CoGetMalloc() failed!");
826
827 LPOLESTR pOleStr = nullptr;
828 hr = pOleLink->GetSourceDisplayName( &pOleStr );
829 if ( SUCCEEDED( hr ) && pOleStr )
830 {
831 std::wstring aFilePath( pOleStr );
832 if ( pMalloc )
833 pMalloc->Free( pOleStr );
834
835 hr = OleCreateFromFile( CLSID_NULL,
836 aFilePath.c_str(),
837 IID_IUnknown,
838 OLERENDER_DRAW, // OLERENDER_FORMAT
839 nullptr,
840 nullptr,
842 reinterpret_cast<void**>(&m_pNativeImpl->m_pObj) );
843 }
844 }
845
846 // in case of other moniker types the only way is to get storage
847 if ( !m_pNativeImpl->m_pObj )
848 {
849 sal::systools::COMReference< IBindCtx > pBindCtx;
850 hr = CreateBindCtx( 0, &pBindCtx );
851 if ( SUCCEEDED( hr ) && pBindCtx )
852 {
853 sal::systools::COMReference< IStorage > pObjectStorage;
854 hr = pMoniker->BindToStorage( pBindCtx, nullptr, IID_IStorage, reinterpret_cast<void**>(&pObjectStorage) );
855 if ( SUCCEEDED( hr ) && pObjectStorage )
856 {
857 hr = pObjectStorage->CopyTo( 0, nullptr, nullptr, m_pNativeImpl->m_pIStorage );
858 if ( SUCCEEDED( hr ) )
859 hr = OleLoadSeh(m_pNativeImpl->m_pIStorage, reinterpret_cast<void**>(&m_pNativeImpl->m_pObj));
860 }
861 }
862 }
863 }
864
865 // If object could not be created the only way is to use graphical representation
866 if ( !m_pNativeImpl->m_pObj )
867 throw uno::RuntimeException(); // TODO
868
869 if ( !InitializeObject_Impl() )
870 throw uno::RuntimeException(); // TODO
871}
872
873
875{
876 OSL_ENSURE( m_pNativeImpl->m_pOleObject, "The pointer can not be set to NULL here!" );
878 throw embed::WrongStateException(); // TODO: the object is in wrong state
879
880 if ( !OleIsRunning( m_pNativeImpl->m_pOleObject ) )
881 {
882 HRESULT hr = OleRun( m_pNativeImpl->m_pObj );
883
884 if ( FAILED( hr ) )
885 {
886 if ( hr == REGDB_E_CLASSNOTREG )
887 throw embed::UnreachableStateException(); // the object server is not installed
888 else
889 throw io::IOException();
890 }
891 }
892}
893
894
895awt::Size OleComponent::CalculateWithFactor( const awt::Size& aSize,
896 const awt::Size& aMultiplier,
897 const awt::Size& aDivisor )
898{
899 awt::Size aResult;
900
901 sal_Int64 nWidth = static_cast<sal_Int64>(aSize.Width) * static_cast<sal_Int64>(aMultiplier.Width) / static_cast<sal_Int64>(aDivisor.Width);
902 sal_Int64 nHeight = static_cast<sal_Int64>(aSize.Height) * static_cast<sal_Int64>(aMultiplier.Height) / static_cast<sal_Int64>(aDivisor.Height);
903 OSL_ENSURE( nWidth < SAL_MAX_INT32 && nWidth > SAL_MIN_INT32
904 && nHeight < SAL_MAX_INT32 && nHeight > SAL_MIN_INT32,
905 "Unacceptable result size!" );
906
907 aResult.Width = static_cast<sal_Int32>(nWidth);
908 aResult.Height = static_cast<sal_Int32>(nHeight);
909
910 return aResult;
911}
912
913
915{
916 if ( m_pNativeImpl->m_pOleObject && OleIsRunning( m_pNativeImpl->m_pOleObject ) )
917 m_pNativeImpl->m_pOleObject->Close( OLECLOSE_NOSAVE ); // must be saved before
918}
919
920
921uno::Sequence< embed::VerbDescriptor > OleComponent::GetVerbList()
922{
924 throw embed::WrongStateException(); // TODO: the object is in wrong state
925
926 if( !m_aVerbList.getLength() )
927 {
928 sal::systools::COMReference< IEnumOLEVERB > pEnum;
929 if( SUCCEEDED( m_pNativeImpl->m_pOleObject->EnumVerbs( &pEnum ) ) )
930 {
931 OLEVERB szEle[ MAX_ENUM_ELE ];
932 ULONG nNum = 0;
933 sal_Int32 nSeqSize = 0;
934
935 do
936 {
937 HRESULT hr = pEnum->Next( MAX_ENUM_ELE, szEle, &nNum );
938 if( hr == S_OK || hr == S_FALSE )
939 {
940 m_aVerbList.realloc( nSeqSize += nNum );
941 auto pVerbList = m_aVerbList.getArray();
942 for( sal_uInt32 nInd = 0; nInd < nNum; nInd++ )
943 {
944 pVerbList[nSeqSize-nNum+nInd].VerbID = szEle[ nInd ].lVerb;
945 pVerbList[nSeqSize-nNum+nInd].VerbName = WinAccToVcl_Impl( o3tl::toU(szEle[ nInd ].lpszVerbName) );
946 pVerbList[nSeqSize-nNum+nInd].VerbFlags = szEle[ nInd ].fuFlags;
947 pVerbList[nSeqSize-nNum+nInd].VerbAttributes = szEle[ nInd ].grfAttribs;
948 }
949 }
950 else
951 break;
952 }
953 while( nNum == MAX_ENUM_ELE );
954 }
955 }
956
957 return m_aVerbList;
958}
959
960
961void OleComponent::ExecuteVerb( sal_Int32 nVerbID )
962{
964 throw embed::WrongStateException(); // TODO
965
966 HRESULT hr = OleRun( m_pNativeImpl->m_pOleObject );
967 if ( FAILED( hr ) )
968 throw io::IOException(); // TODO: a specific exception that transport error code can be thrown here
969
970 // TODO: probably extents should be set here and stored in aRect
971 // TODO: probably the parent window also should be set
972 hr = m_pNativeImpl->m_pOleObject->DoVerb( nVerbID, nullptr, m_pOleWrapClientSite, 0, nullptr, nullptr );
973
974 if ( FAILED( hr ) )
975 throw io::IOException(); // TODO
976}
977
978
979void OleComponent::SetHostName( const OUString& aEmbDocName )
980{
982 throw embed::WrongStateException(); // TODO: the object is in wrong state
983
984 m_pNativeImpl->m_pOleObject->SetHostNames( L"app name", o3tl::toW( aEmbDocName.getStr() ) );
985}
986
987
988void OleComponent::SetExtent( const awt::Size& aVisAreaSize, sal_Int64 nAspect )
989{
991 throw embed::WrongStateException(); // TODO: the object is in wrong state
992
993 DWORD nMSAspect = static_cast<DWORD>(nAspect); // first 32 bits are for MS aspects
994
995 SIZEL aSize = { aVisAreaSize.Width, aVisAreaSize.Height };
996 HRESULT hr = m_pNativeImpl->m_pOleObject->SetExtent( nMSAspect, &aSize );
997
998 if ( FAILED( hr ) )
999 {
1000 // TODO/LATER: is it correct? In future user code probably should be ready for the exception.
1001 // if the object is running but not activated, RPC_E_SERVER_DIED error code is returned by OLE package
1002 // in this case just do nothing
1003 // Also Visio returns E_FAIL on resize if it is in running state
1004 // if ( hr != RPC_E_SERVER_DIED )
1005 throw io::IOException(); // TODO
1006 }
1007}
1008
1009
1010awt::Size OleComponent::GetExtent( sal_Int64 nAspect )
1011{
1013 throw embed::WrongStateException(); // TODO: the object is in wrong state
1014
1015 DWORD nMSAspect = static_cast<DWORD>(nAspect); // first 32 bits are for MS aspects
1016 awt::Size aSize;
1017 bool bGotSize = false;
1018
1019 if ( nMSAspect == DVASPECT_CONTENT )
1020 {
1021 // Try to get the size from the replacement image first
1022 sal::systools::COMReference< IDataObject > pDataObject(m_pNativeImpl->m_pObj, sal::systools::COM_QUERY);
1023 if ( pDataObject )
1024 {
1025 STGMEDIUM aMedium;
1026 FORMATETC aFormat = pFormatTemplates[1]; // use windows metafile format
1027 aFormat.dwAspect = nMSAspect;
1028
1029 HRESULT hr = pDataObject->GetData( &aFormat, &aMedium );
1030
1031 if (hr == RPC_E_WRONG_THREAD)
1032 {
1033 // Assume that the OLE object was loaded on the main thread.
1034 vcl::solarthread::syncExecute([this, &hr, &pDataObject, &aFormat, &aMedium]() {
1035 // Make sure that the current state is embed::EmbedStates::RUNNING.
1036 RunObject();
1037 // Now try again on the correct thread.
1038 hr = pDataObject->GetData(&aFormat, &aMedium);
1039 });
1040 }
1041
1042 if ( SUCCEEDED( hr ) && aMedium.tymed == TYMED_MFPICT ) // Win Metafile
1043 {
1044 METAFILEPICT* pMF = static_cast<METAFILEPICT*>(GlobalLock( aMedium.hMetaFilePict ));
1045 if ( pMF )
1046 {
1047 // the object uses 0.01 mm as unit, so the metafile size should be converted to object unit
1049 switch( pMF->mm )
1050 {
1051 case MM_HIENGLISH:
1052 eFrom = o3tl::Length::in1000;
1053 break;
1054
1055 case MM_LOENGLISH:
1056 eFrom = o3tl::Length::in100;
1057 break;
1058
1059 case MM_LOMETRIC:
1060 eFrom = o3tl::Length::mm10;
1061 break;
1062
1063 case MM_TWIPS:
1064 eFrom = o3tl::Length::twip;
1065 break;
1066
1067 case MM_ISOTROPIC:
1068 case MM_ANISOTROPIC:
1069 case MM_HIMETRIC:
1070 // do nothing
1071 break;
1072 }
1073
1074 sal_Int64 nX = o3tl::convert(abs( pMF->xExt ), eFrom, o3tl::Length::mm100);
1075 sal_Int64 nY = o3tl::convert(abs( pMF->yExt ), eFrom, o3tl::Length::mm100);
1076 if ( nX < SAL_MAX_INT32 && nY < SAL_MAX_INT32 )
1077 {
1078 aSize.Width = static_cast<sal_Int32>(nX);
1079 aSize.Height = static_cast<sal_Int32>(nY);
1080 bGotSize = true;
1081 }
1082 else
1083 OSL_FAIL( "Unexpected size is provided!" );
1084 }
1085 }
1086 else if (!SUCCEEDED(hr))
1087 {
1088 SAL_WARN("embeddedobj.ole", " OleComponent::GetExtent: GetData() failed");
1089 }
1090 // i113605, to release storage medium
1091 if ( SUCCEEDED( hr ) )
1092 ::ReleaseStgMedium(&aMedium);
1093 }
1094 }
1095
1096 if ( !bGotSize )
1097 throw lang::IllegalArgumentException();
1098
1099 return aSize;
1100}
1101
1102
1103awt::Size OleComponent::GetCachedExtent( sal_Int64 nAspect )
1104{
1106 throw embed::WrongStateException(); // TODO: the object is in wrong state
1107
1108 DWORD nMSAspect = static_cast<DWORD>(nAspect); // first 32 bits are for MS aspects
1109 SIZEL aSize;
1110
1111 HRESULT hr = m_pNativeImpl->m_pViewObject2->GetExtent( nMSAspect, -1, nullptr, &aSize );
1112
1113 if ( FAILED( hr ) )
1114 {
1115 // TODO/LATER: is it correct?
1116 // if there is no appropriate cache for the aspect, OLE_E_BLANK error code is returned
1117 // if ( hr == OLE_E_BLANK )
1118 // throw lang::IllegalArgumentException();
1119 //else
1120 // throw io::IOException(); // TODO
1121
1122 SAL_WARN("embeddedobj.ole", " OleComponent::GetCachedExtent: GetExtent() failed");
1123 throw lang::IllegalArgumentException();
1124 }
1125
1126 return awt::Size( aSize.cx, aSize.cy );
1127}
1128
1129
1130awt::Size OleComponent::GetRecommendedExtent( sal_Int64 nAspect )
1131{
1133 throw embed::WrongStateException(); // TODO: the object is in wrong state
1134
1135 DWORD nMSAspect = static_cast<DWORD>(nAspect); // first 32 bits are for MS aspects
1136 SIZEL aSize;
1137 HRESULT hr = m_pNativeImpl->m_pOleObject->GetExtent( nMSAspect, &aSize );
1138 if ( FAILED( hr ) )
1139 {
1140 SAL_WARN("embeddedobj.ole", " OleComponent::GetRecommendedExtent: GetExtent() failed");
1141 throw lang::IllegalArgumentException();
1142 }
1143
1144 return awt::Size( aSize.cx, aSize.cy );
1145}
1146
1147
1148sal_Int64 OleComponent::GetMiscStatus( sal_Int64 nAspect )
1149{
1151 throw embed::WrongStateException(); // TODO: the object is in wrong state
1152
1153 DWORD nResult;
1154 m_pNativeImpl->m_pOleObject->GetMiscStatus( static_cast<DWORD>(nAspect), &nResult );
1155 return static_cast<sal_Int64>(nResult); // first 32 bits are for MS flags
1156}
1157
1158
1159uno::Sequence< sal_Int8 > OleComponent::GetCLSID()
1160{
1162 throw embed::WrongStateException(); // TODO: the object is in wrong state
1163
1164 GUID aCLSID;
1165 HRESULT hr = m_pNativeImpl->m_pOleObject->GetUserClassID( &aCLSID );
1166 if ( FAILED( hr ) )
1167 throw io::IOException(); // TODO:
1168
1169 return MimeConfigurationHelper::GetSequenceClassID( aCLSID.Data1, aCLSID.Data2, aCLSID.Data3,
1170 aCLSID.Data4[0], aCLSID.Data4[1],
1171 aCLSID.Data4[2], aCLSID.Data4[3],
1172 aCLSID.Data4[4], aCLSID.Data4[5],
1173 aCLSID.Data4[6], aCLSID.Data4[7] );
1174}
1175
1176
1178{
1180 throw embed::WrongStateException(); // TODO: the object is in wrong state
1181
1182 if ( IsWorkaroundActive() )
1183 return true;
1184
1185 sal::systools::COMReference< IPersistStorage > pPersistStorage(m_pNativeImpl->m_pObj, sal::systools::COM_QUERY);
1186 if ( !pPersistStorage )
1187 throw io::IOException(); // TODO
1188
1189 HRESULT hr = pPersistStorage->IsDirty();
1190 return ( hr != S_FALSE );
1191}
1192
1193
1195{
1197 throw embed::WrongStateException(); // TODO: the object is in wrong state
1198
1199 sal::systools::COMReference< IPersistStorage > pPersistStorage(m_pNativeImpl->m_pObj, sal::systools::COM_QUERY);
1200 if ( !pPersistStorage )
1201 throw io::IOException(); // TODO
1202
1203 if ( m_bWorkaroundActive || pPersistStorage->IsDirty() != S_FALSE )
1204 {
1205 HRESULT hr = OleSave( pPersistStorage, m_pNativeImpl->m_pIStorage, TRUE );
1206 if ( FAILED( hr ) )
1207 {
1208 // Till now was required only for AcrobatReader7.0.8
1209 GUID aCLSID;
1210 hr = m_pNativeImpl->m_pOleObject->GetUserClassID( &aCLSID );
1211 if ( FAILED( hr ) )
1212 {
1213 SAL_WARN("embeddedobj.ole", "OleComponent::StoreOwnTmpIfNecessary: GetUserClassID() failed");
1214 throw io::IOException(); // TODO
1215 }
1216
1217 hr = WriteClassStg( m_pNativeImpl->m_pIStorage, aCLSID );
1218 if ( FAILED( hr ) )
1219 throw io::IOException(); // TODO
1220
1221 // the result of the following call is not checked because some objects, for example AcrobatReader7.0.8
1222 // return error even in case the saving was done correctly
1223 hr = pPersistStorage->Save( m_pNativeImpl->m_pIStorage, TRUE );
1224
1225 // another workaround for AcrobatReader7.0.8 object, this object might think that it is not changed
1226 // when it has been created from file, although it must be saved
1227 m_bWorkaroundActive = true;
1228 }
1229
1230 hr = m_pNativeImpl->m_pIStorage->Commit( STGC_DEFAULT );
1231 if ( FAILED( hr ) )
1232 throw io::IOException(); // TODO
1233
1234 hr = pPersistStorage->SaveCompleted( nullptr );
1235 if ( FAILED( hr ) && hr != E_UNEXPECTED )
1236 throw io::IOException(); // TODO
1237
1238 }
1239}
1240
1241
1243{
1244 bool bResult = false;
1245 OleEmbeddedObject* pLockObject = nullptr;
1246
1247 {
1248 osl::MutexGuard aGuard( m_aMutex );
1249 if ( m_pUnoOleObject )
1250 {
1251 pLockObject = m_pUnoOleObject;
1252 pLockObject->acquire();
1253 }
1254 }
1255
1256 if ( pLockObject )
1257 {
1258 bResult = pLockObject->SaveObject_Impl();
1259 pLockObject->release();
1260 }
1261
1262 return bResult;
1263}
1264
1265
1267{
1268 bool bResult = false;
1269 OleEmbeddedObject* pLockObject = nullptr;
1270
1271 {
1272 osl::MutexGuard aGuard( m_aMutex );
1273
1274 if ( m_pUnoOleObject )
1275 {
1276 pLockObject = m_pUnoOleObject;
1277 pLockObject->acquire();
1278 }
1279 }
1280
1281 if ( pLockObject )
1282 {
1283 bResult = pLockObject->OnShowWindow_Impl( bShow );
1284 pLockObject->release();
1285 }
1286
1287 return bResult;
1288}
1289
1290
1291void OleComponent::OnViewChange_Impl( sal_uInt32 dwAspect )
1292{
1293 // TODO: check if it is enough or may be saving notifications are required for Visio2000
1295
1296 {
1297 osl::MutexGuard aGuard( m_aMutex );
1298 if ( m_pUnoOleObject )
1299 xLockObject = m_pUnoOleObject;
1300 }
1301
1302 if ( xLockObject.is() )
1303 {
1304 uno::Reference < awt::XRequestCallback > xRequestCallback(
1305 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.awt.AsyncCallback", m_xContext),
1306 uno::UNO_QUERY );
1307 xRequestCallback->addCallback( new MainThreadNotificationRequest( xLockObject, OLECOMP_ONVIEWCHANGE, dwAspect ), uno::Any() );
1308 }
1309}
1310
1311
1313{
1315
1316 {
1317 osl::MutexGuard aGuard( m_aMutex );
1318 if ( m_pUnoOleObject )
1319 xLockObject = m_pUnoOleObject;
1320 }
1321
1322 if ( xLockObject.is() )
1323 {
1324 uno::Reference < awt::XRequestCallback > xRequestCallback(
1325 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.awt.AsyncCallback", m_xContext),
1326 uno::UNO_QUERY );
1327 xRequestCallback->addCallback( new MainThreadNotificationRequest( xLockObject, OLECOMP_ONCLOSE ), uno::Any() );
1328 }
1329}
1330
1331// XCloseable
1332
1333void SAL_CALL OleComponent::close( sal_Bool bDeliverOwnership )
1334{
1335 uno::Reference< uno::XInterface > xSelfHold;
1336 {
1337 osl::MutexGuard aGuard(m_aMutex);
1338 if (m_bDisposed)
1339 throw lang::DisposedException(); // TODO
1340
1341 xSelfHold.set(static_cast<::cppu::OWeakObject*>(this));
1342 lang::EventObject aSource(static_cast<::cppu::OWeakObject*>(this));
1343
1345 {
1348 if (pContainer != nullptr)
1349 {
1350 comphelper::OInterfaceIteratorHelper2 pIterator(*pContainer);
1351 while (pIterator.hasMoreElements())
1352 {
1353 try
1354 {
1355 static_cast<util::XCloseListener*>(pIterator.next())
1356 ->queryClosing(aSource, bDeliverOwnership);
1357 }
1358 catch (const uno::RuntimeException&)
1359 {
1360 pIterator.remove();
1361 }
1362 }
1363 }
1364
1365 pContainer
1367 if (pContainer != nullptr)
1368 {
1369 comphelper::OInterfaceIteratorHelper2 pCloseIterator(*pContainer);
1370 while (pCloseIterator.hasMoreElements())
1371 {
1372 try
1373 {
1374 static_cast<util::XCloseListener*>(pCloseIterator.next())
1375 ->notifyClosing(aSource);
1376 }
1377 catch (const uno::RuntimeException&)
1378 {
1379 pCloseIterator.remove();
1380 }
1381 }
1382 }
1383 }
1384 }
1385
1386 Dispose();
1387}
1388
1389
1390void SAL_CALL OleComponent::addCloseListener( const uno::Reference< util::XCloseListener >& xListener )
1391{
1392 ::osl::MutexGuard aGuard( m_aMutex );
1393 if ( m_bDisposed )
1394 throw lang::DisposedException(); // TODO
1395
1396 if ( !m_pInterfaceContainer )
1398
1400}
1401
1402
1403void SAL_CALL OleComponent::removeCloseListener( const uno::Reference< util::XCloseListener >& xListener )
1404{
1405 ::osl::MutexGuard aGuard( m_aMutex );
1406 if ( m_bDisposed )
1407 throw lang::DisposedException(); // TODO
1408
1411 xListener );
1412}
1413
1414// XTransferable
1415
1416uno::Any SAL_CALL OleComponent::getTransferData( const datatransfer::DataFlavor& aFlavor )
1417{
1418 ::osl::MutexGuard aGuard( m_aMutex );
1419 if ( m_bDisposed )
1420 throw lang::DisposedException(); // TODO
1421
1423 throw embed::WrongStateException(); // TODO: the object is in wrong state
1424
1425 uno::Any aResult;
1426 bool bSupportedFlavor = false;
1427
1428 if ( m_pNativeImpl->GraphicalFlavor( aFlavor ) )
1429 {
1430 DWORD nRequestedAspect = GetAspectFromFlavor( aFlavor );
1431 // if own icon is set and icon aspect is requested the own icon can be returned directly
1432
1433 sal::systools::COMReference< IDataObject > pDataObject(m_pNativeImpl->m_pObj, sal::systools::COM_QUERY);
1434 if ( !pDataObject )
1435 throw io::IOException(); // TODO: transport error code
1436
1437 // The following optimization does not make much sense currently just because
1438 // only one aspect is supported, and only three formats for the aspect are supported
1439 // and moreover it is not guaranteed that the once returned format will be supported further
1440 // example - i52106
1441 // TODO/LATER: bring the optimization back when other aspects are supported
1442
1443 // FORMATETC* pFormatEtc = m_pNativeImpl->GetSupportedFormatForAspect( nRequestedAspect );
1444 // if ( pFormatEtc )
1445 // {
1446 // STGMEDIUM aMedium;
1447 // hr = pDataObject->GetData( pFormatEtc, &aMedium );
1448 // if ( SUCCEEDED( hr ) )
1449 // bSupportedFlavor = m_pNativeImpl->ConvertDataForFlavor( aMedium, aFlavor, aResult );
1450 // }
1451 // else
1452 {
1453 // the supported format of the application is still not found, find one
1454 for ( sal_Int32 nInd = 0; nInd < FORMATS_NUM; nInd++ )
1455 {
1456 STGMEDIUM aMedium;
1457 FORMATETC aFormat = pFormatTemplates[nInd];
1458 aFormat.dwAspect = nRequestedAspect;
1459
1460 HRESULT hr = pDataObject->GetData( &aFormat, &aMedium );
1461 if ( SUCCEEDED( hr ) )
1462 {
1463 bSupportedFlavor = m_pNativeImpl->ConvertDataForFlavor( aMedium, aFlavor, aResult );
1464 ::ReleaseStgMedium(&aMedium); // i113605, to release storage medium
1465 if ( bSupportedFlavor )
1466 {
1467 // TODO/LATER: bring the optimization back when other aspects are supported
1468 // m_pNativeImpl->AddSupportedFormat( aFormat );
1469 break;
1470 }
1471 }
1472 }
1473 }
1474
1475 // If the replacement could not be retrieved, the cached representation should be used
1476 // currently it is not necessary to retrieve it here, so it is implemented in the object itself
1477 }
1478 // TODO: Investigate if there is already the format name
1479 // and whether this format is really required
1480 else if ( aFlavor.DataType == cppu::UnoType<io::XInputStream>::get()
1481 && aFlavor.MimeType == "application/x-openoffice-contentstream" )
1482 {
1483 // allow to retrieve stream-representation of the object persistence
1484 bSupportedFlavor = true;
1485 uno::Reference < io::XStream > xTempFileStream(
1486 io::TempFile::create(m_xContext),
1487 uno::UNO_QUERY_THROW );
1488
1489 uno::Reference< io::XOutputStream > xTempOutStream = xTempFileStream->getOutputStream();
1490 uno::Reference< io::XInputStream > xTempInStream = xTempFileStream->getInputStream();
1491 if ( !(xTempOutStream.is() && xTempInStream.is()) )
1492 throw io::IOException(); // TODO:
1493
1494 OSL_ENSURE( m_pUnoOleObject, "Unexpected object absence!" );
1495 if ( !m_pUnoOleObject )
1496 throw uno::RuntimeException();
1497
1498 m_pUnoOleObject->StoreObjectToStream( xTempOutStream );
1499
1500 xTempOutStream->closeOutput();
1501 xTempOutStream.clear();
1502
1503 aResult <<= xTempInStream;
1504 }
1505
1506 if ( !bSupportedFlavor )
1507 throw datatransfer::UnsupportedFlavorException();
1508
1509 return aResult;
1510}
1511
1512
1513uno::Sequence< datatransfer::DataFlavor > SAL_CALL OleComponent::getTransferDataFlavors()
1514{
1515 ::osl::MutexGuard aGuard( m_aMutex );
1516 if ( m_bDisposed )
1517 throw lang::DisposedException(); // TODO
1518
1520 throw embed::WrongStateException(); // TODO: the object is in wrong state
1521
1523
1524 return m_aDataFlavors;
1525}
1526
1527
1528sal_Bool SAL_CALL OleComponent::isDataFlavorSupported( const datatransfer::DataFlavor& aFlavor )
1529{
1530 ::osl::MutexGuard aGuard( m_aMutex );
1531 if ( m_bDisposed )
1532 throw lang::DisposedException(); // TODO
1533
1535 throw embed::WrongStateException(); // TODO: the object is in wrong state
1536
1537 if ( !m_aDataFlavors.getLength() )
1538 {
1540 }
1541
1542 for ( auto const & supportedFormat : std::as_const(m_aDataFlavors) )
1543 if ( supportedFormat.MimeType.equals( aFlavor.MimeType ) && supportedFormat.DataType == aFlavor.DataType )
1544 return true;
1545
1546 return false;
1547}
1548
1550{
1551 try
1552 {
1553 close( true );
1554 }
1555 catch ( const uno::Exception& )
1556 {
1557 }
1558}
1559
1560void SAL_CALL OleComponent::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
1561{
1562 ::osl::MutexGuard aGuard( m_aMutex );
1563 if ( m_bDisposed )
1564 throw lang::DisposedException(); // TODO
1565
1566 if ( !m_pInterfaceContainer )
1568
1570}
1571
1572
1573void SAL_CALL OleComponent::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
1574{
1575 ::osl::MutexGuard aGuard( m_aMutex );
1576 if ( m_bDisposed )
1577 throw lang::DisposedException(); // TODO
1578
1581 xListener );
1582}
1583
1584sal_Int64 SAL_CALL OleComponent::getSomething( const css::uno::Sequence< sal_Int8 >& aIdentifier )
1585{
1586 try
1587 {
1588 uno::Sequence < sal_Int8 > aCLSID = GetCLSID();
1589 if ( MimeConfigurationHelper::ClassIDsEqual( aIdentifier, aCLSID ) )
1590 return comphelper::getSomething_cast(static_cast<IUnknown*>(m_pNativeImpl->m_pObj));
1591
1592 // compatibility hack for old versions: CLSID was used in wrong order (SvGlobalName order)
1593 sal_Int32 nLength = aIdentifier.getLength();
1594 if ( nLength == 16 )
1595 {
1596 for ( sal_Int32 n=8; n<16; n++ )
1597 if ( aIdentifier[n] != aCLSID[n] )
1598 return 0;
1599 if ( aIdentifier[7] == aCLSID[6] &&
1600 aIdentifier[6] == aCLSID[7] &&
1601 aIdentifier[5] == aCLSID[4] &&
1602 aIdentifier[4] == aCLSID[5] &&
1603 aIdentifier[3] == aCLSID[0] &&
1604 aIdentifier[2] == aCLSID[1] &&
1605 aIdentifier[1] == aCLSID[2] &&
1606 aIdentifier[0] == aCLSID[3] )
1607 return reinterpret_cast<sal_Int64>(static_cast<IUnknown*>(m_pNativeImpl->m_pObj));
1608 }
1609 }
1610 catch ( const uno::Exception& )
1611 {
1612 }
1613
1614 return 0;
1615}
1616
1618{
1619 return m_bModified;
1620}
1621
1622void SAL_CALL OleComponent::setModified( sal_Bool bModified )
1623{
1624 m_bModified = bModified;
1625
1626 if ( bModified && m_pInterfaceContainer )
1627 {
1630 if ( pContainer != nullptr )
1631 {
1632 comphelper::OInterfaceIteratorHelper2 pIterator( *pContainer );
1633 while ( pIterator.hasMoreElements() )
1634 {
1635 try
1636 {
1637 lang::EventObject aEvent( static_cast<util::XModifiable*>(this) );
1638 static_cast<util::XModifyListener*>(pIterator.next())->modified( aEvent );
1639 }
1640 catch( const uno::RuntimeException& )
1641 {
1642 pIterator.remove();
1643 }
1644 }
1645 }
1646 }
1647}
1648
1649void SAL_CALL OleComponent::addModifyListener( const css::uno::Reference < css::util::XModifyListener >& xListener )
1650{
1651 ::osl::MutexGuard aGuard( m_aMutex );
1652 if ( m_bDisposed )
1653 throw lang::DisposedException(); // TODO
1654
1655 if ( !m_pInterfaceContainer )
1657
1659}
1660
1661void SAL_CALL OleComponent::removeModifyListener( const css::uno::Reference < css::util::XModifyListener >& xListener)
1662{
1663 ::osl::MutexGuard aGuard( m_aMutex );
1664 if ( m_bDisposed )
1665 throw lang::DisposedException(); // TODO
1666
1669 xListener );
1670}
1671
1672/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
AnyEventRef aEvent
void RetrieveObjectDataFlavors_Impl()
virtual void SAL_CALL close(sal_Bool DeliverOwnership) override
sal_Int64 GetMiscStatus(sal_Int64 nAspect)
virtual css::uno::Any SAL_CALL getTransferData(const css::datatransfer::DataFlavor &aFlavor) override
void SetExtent(const css::awt::Size &aVisAreaSize, sal_Int64 nAspect)
css::uno::Sequence< sal_Int8 > GetCLSID()
virtual void SAL_CALL removeCloseListener(const css::uno::Reference< css::util::XCloseListener > &Listener) override
bool m_bOleInitialized
void CreateNewIStorage_Impl()
void SetHostName(const OUString &aEmbDocName)
OleComponent(const css::uno::Reference< css::uno::XComponentContext > &xContext, OleEmbeddedObject *pOleObj)
css::uno::Sequence< css::datatransfer::DataFlavor > m_aDataFlavors
void disconnectEmbeddedObject()
css::uno::Sequence< css::embed::VerbDescriptor > m_aVerbList
virtual void SAL_CALL dispose() override
static void CreateObjectFromData(const css::uno::Reference< css::datatransfer::XTransferable > &xTransfer)
virtual void SAL_CALL setModified(sal_Bool bModified) override
OleEmbeddedObject * m_pUnoOleObject
sal_Int32 m_nAdvConn
bool SaveObject_Impl()
void StoreOwnTmpIfNecessary()
void CreateObjectFromFile(const OUString &aFileName)
bool m_bWorkaroundActive
OleWrapperClientSite * m_pOleWrapClientSite
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
virtual sal_Int64 SAL_CALL getSomething(const css::uno::Sequence< sal_Int8 > &aIdentifier) override
void LoadEmbeddedObject(const OUString &aTempURL)
virtual sal_Bool SAL_CALL isModified() override
void InitEmbeddedCopyOfLink(rtl::Reference< OleComponent > const &pOleLinkComponent)
css::awt::Size GetExtent(sal_Int64 nAspect)
OleWrapperAdviseSink * m_pImplAdviseSink
virtual void SAL_CALL addModifyListener(const css::uno::Reference< css::util::XModifyListener > &xListener) override
css::awt::Size GetCachedExtent(sal_Int64 nAspect)
sal_Int32 m_nOLEMiscFlags
static css::awt::Size CalculateWithFactor(const css::awt::Size &aSize, const css::awt::Size &aMultiplier, const css::awt::Size &aDivisor)
bool IsWorkaroundActive() const
css::awt::Size GetRecommendedExtent(sal_Int64 nAspect)
void CreateLinkFromFile(const OUString &aFileName)
bool OnShowWindow_Impl(bool bShow)
virtual ~OleComponent() override
void CreateNewEmbeddedObject(const css::uno::Sequence< sal_Int8 > &aSeqCLSID)
virtual sal_Bool SAL_CALL isDataFlavorSupported(const css::datatransfer::DataFlavor &aFlavor) override
virtual css::uno::Sequence< css::datatransfer::DataFlavor > SAL_CALL getTransferDataFlavors() override
void CreateObjectFromClipboard()
bool InitializeObject_Impl()
comphelper::OMultiTypeInterfaceContainerHelper2 * m_pInterfaceContainer
OleComponentNative_Impl * m_pNativeImpl
void ExecuteVerb(sal_Int32 nVerbID)
virtual void SAL_CALL removeModifyListener(const css::uno::Reference< css::util::XModifyListener > &xListener) override
virtual void SAL_CALL addCloseListener(const css::uno::Reference< css::util::XCloseListener > &Listener) override
css::uno::Sequence< css::embed::VerbDescriptor > GetVerbList()
void CloseObject()
css::uno::Reference< css::uno::XComponentContext > m_xContext
::osl::Mutex m_aMutex
void OnViewChange_Impl(sal_uInt32 dwAspect)
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
Represents an OLE object that has native data and we try to let an external application handle that d...
Definition: oleembobj.hxx:120
void disconnectOleComponent()
Definition: advisesink.cxx:70
constexpr tools::Long Width() const
css::uno::XInterface * next()
OInterfaceContainerHelper2 * getContainer(const css::uno::Type &rKey) const
sal_Int32 removeInterface(const css::uno::Type &rKey, const css::uno::Reference< css::uno::XInterface > &rxIFace)
sal_Int32 addInterface(const css::uno::Type &rKey, const css::uno::Reference< css::uno::XInterface > &r)
void disposeAndClear(const css::lang::EventObject &rEvt)
URL aURL
ULONG m_refCount
bool m_bDisposed
bool ConvertBufferToFormat(void *pBuf, sal_uInt32 nBufSize, const OUString &aMimeType, uno::Any &aResult)
#define TRUE
sal_Int64 n
Sequence< sal_Int8 > aSeq
#define SAL_WARN(area, stream)
#define OLECOMP_ONCLOSE
#define OLECOMP_ONVIEWCHANGE
sal_Int64 getSomething_cast(void *p)
MM_HIMETRIC
MM_LOENGLISH
MM_HIENGLISH
MM_ISOTROPIC
MM_TWIPS
MM_ANISOTROPIC
MM_LOMETRIC
constexpr Point convert(const Point &rPoint, o3tl::Length eFrom, o3tl::Length eTo)
auto syncExecute(FuncT const &func) -> decltype(func())
SwNodeOffset abs(const SwNodeOffset &a)
std::vector< FORMATETC * > FormatEtcList
static HRESULT OpenIStorageFromURL_Impl(const OUString &aURL, IStorage **ppIStorage)
static DWORD GetAspectFromFlavor(const datatransfer::DataFlavor &aFlavor)
static bool GetClassIDFromSequence_Impl(uno::Sequence< sal_Int8 > const &aSeq, CLSID &aResult)
static OUString WinAccToVcl_Impl(const sal_Unicode *pStr)
FORMATETC const pFormatTemplates[FORMATS_NUM]
static OUString GetFlavorSuffixFromAspect(DWORD nAsp)
#define FORMATS_NUM
#define MAX_ENUM_ELE
OUString GetNewTempFileURL_Impl(const uno::Reference< uno::XComponentContext > &xContext)
Definition: olepersist.cxx:85
return hr
sal::systools::COMReference< IOleObject > m_pOleObject
sal::systools::COMReference< IUnknown > m_pObj
sal::systools::COMReference< IViewObject2 > m_pViewObject2
uno::Sequence< datatransfer::DataFlavor > m_aSupportedGraphFormats
bool GraphicalFlavor(const datatransfer::DataFlavor &aFlavor)
FormatEtcList m_aFormatsList
bool ConvertDataForFlavor(const STGMEDIUM &aMedium, const datatransfer::DataFlavor &aFlavor, uno::Any &aResult)
uno::Sequence< datatransfer::DataFlavor > GetFlavorsForAspects(sal_uInt32 nSupportedAspects)
sal::systools::COMReference< IStorage > m_pIStorage
unsigned char sal_uInt8
#define SAL_MAX_INT32
unsigned char sal_Bool
#define SAL_MIN_INT32
sal_uInt16 sal_Unicode
signed char sal_Int8
sal_Int32 nLength