25#include <com/sun/star/uno/Exception.hpp>
26#include <com/sun/star/lang/XMultiServiceFactory.hpp>
27#include <com/sun/star/lang/XComponent.hpp>
28#include <com/sun/star/io/TempFile.hpp>
29#include <com/sun/star/io/XInputStream.hpp>
30#include <com/sun/star/io/XOutputStream.hpp>
31#include <com/sun/star/io/XSeekable.hpp>
32#include <com/sun/star/frame/XModel.hpp>
33#include <com/sun/star/frame/XLoadable.hpp>
34#include <com/sun/star/util/XModifiable.hpp>
35#include <com/sun/star/frame/XStorable.hpp>
36#include <com/sun/star/util/URLTransformer.hpp>
37#include <com/sun/star/util/XURLTransformer.hpp>
41#include <osl/mutex.hxx>
42#include <osl/diagnose.h>
49#define EXT_STREAM_LENGTH 4
63 uno::Reference< lang::XMultiServiceFactory >
const & xFactory,
66 uno::Reference< io::XInputStream > xResult;
72 uno::UNO_QUERY_THROW );
73 ULARGE_INTEGER nNewPos;
74 LARGE_INTEGER
const aZero = { 0, 0 };
75 HRESULT
hr = pStream->Seek( aZero, STREAM_SEEK_SET, &nNewPos );
76 if ( FAILED(
hr ) )
return xResult;
79 hr = pStream->Stat( &aStat, STATFLAG_NONAME );
80 if ( FAILED(
hr ) )
return xResult;
82 sal_uInt32 nSize =
static_cast<sal_uInt32
>(aStat.cbSize.QuadPart);
83 sal_uInt32 nCopied = 0;
95 xTempOut->writeBytes(
aBuffer );
99 if ( nCopied == nSize )
101 uno::Reference < io::XSeekable > xTempSeek ( xTempOut, uno::UNO_QUERY );
102 if ( xTempSeek.is() )
104 xTempSeek->seek ( 0 );
105 xResult.set( xTempOut, uno::UNO_QUERY );
109 catch(
const uno::Exception& )
118 if ( !xTempOut.is() || !pStream )
121 uno::Reference < io::XSeekable > xTempSeek ( xTempOut, uno::UNO_QUERY );
122 if ( !xTempSeek.is() )
125 xTempSeek->seek ( 0 );
127 uno::Reference< io::XInputStream > xTempIn ( xTempOut, uno::UNO_QUERY );
128 if ( !xTempSeek.is() )
132 ULARGE_INTEGER nNewPos;
133 LARGE_INTEGER
const aZero = { 0, 0 };
134 HRESULT
hr = pStream->Seek( aZero, STREAM_SEEK_SET, &nNewPos );
135 if ( FAILED(
hr ) )
return E_FAIL;
136 ULARGE_INTEGER
const aUZero = { 0, 0 };
137 hr = pStream->SetSize( aUZero );
138 if ( FAILED(
hr ) )
return E_FAIL;
141 sal_uInt32 nReadBytes = 0;
148 catch(
const uno::Exception& )
153 sal_uInt32 nWritten = 0;
154 hr = pStream->Write(
aBuffer.getArray(), nReadBytes, &nWritten );
155 if ( !SUCCEEDED(
hr ) || nWritten != nReadBytes )
173, m_bIsInVerbHandling( false )
198 uno::Sequence< beans::PropertyValue > aArgs(
xStream.is() ? 3 : 2 );
199 auto pArgs = aArgs.getArray();
200 pArgs[0].Name =
"FilterName";
205 pArgs[1].Name =
"InputStream";
207 pArgs[2].Name =
"URL";
208 pArgs[2].Value <<= OUString(
"private:stream" );
212 pArgs[1].Name =
"URL";
220 aURL.Complete = o3tl::toU(pFilePath);
222 if ( aTransformer->parseSmart(
aURL, OUString() ) )
223 sDocUrl =
aURL.Complete;
226 pArgs[1].Value <<= sDocUrl;
237 uno::Sequence< beans::PropertyValue > aArgs(
xStream.is() ? 2 : 1 );
238 auto pArgs = aArgs.getArray();
239 pArgs[0].Name =
"FilterName";
244 pArgs[1].Name =
"OutputStream";
260 HRESULT
hr =
Save( pStg,
false );
261 pStg->Commit( STGC_ONLYIFCURRENT );
273 if(IsEqualIID(riid, IID_IUnknown))
276 *ppv =
static_cast<IUnknown*
>(
static_cast<IPersistStorage*
>(
this));
279 else if (IsEqualIID(riid, IID_IPersist))
282 *ppv =
static_cast<IPersist*
>(
static_cast<IPersistStorage*
>(
this));
285 else if (IsEqualIID(riid, IID_IExternalConnection))
288 *ppv =
static_cast<IExternalConnection*
>(
this);
291 else if (IsEqualIID(riid, IID_IPersistStorage))
294 *ppv =
static_cast<IPersistStorage*
>(
this);
297 else if (IsEqualIID(riid, IID_IDataObject))
300 *ppv =
static_cast<IDataObject*
>(
this);
303 else if (IsEqualIID(riid, IID_IOleObject))
306 *ppv =
static_cast<IOleObject*
>(
this);
309 else if (IsEqualIID(riid, IID_IOleWindow))
312 *ppv =
static_cast<IOleWindow*
>(
this);
315 else if (IsEqualIID(riid, IID_IOleInPlaceObject))
318 *ppv =
static_cast<IOleInPlaceObject*
>(
this);
321 else if (IsEqualIID(riid, IID_IPersistFile))
324 *ppv =
static_cast<IPersistFile*
>(
this);
327 else if (IsEqualIID(riid, IID_IDispatch))
330 *ppv =
static_cast<IDispatch*
>(
this);
335 return ResultFromScode(E_NOINTERFACE);
343COM_DECLSPEC_NOTHROW
STDMETHODIMP_(ULONG) EmbedDocument_Impl::Release()
374 uno::Reference< util::XModifiable > xMod(
m_pDocHolder->GetDocument(), uno::UNO_QUERY );
376 return xMod->isModified() ? S_OK : S_FALSE;
382 HRESULT
hr = CO_E_ALREADYINITIALIZED;
388 hr = pStg->Stat( &aStat, STATFLAG_NONAME );
389 if ( FAILED(
hr ) )
return E_FAIL;
391 DWORD nStreamMode = aStat.grfMode;
396 uno::Reference< frame::XModel >
aDocument(
403 uno::Reference< frame::XLoadable > xLoadable(
m_pDocHolder->GetDocument(), uno::UNO_QUERY );
408 xLoadable->initNew();
412 catch(
const uno::Exception& )
420 CLIPFORMAT cf =
static_cast<CLIPFORMAT
>(RegisterClipboardFormatW( L
"Embedded Object" ));
421 hr = WriteFmtUserTypeStg( pStg,
423 const_cast<wchar_t *
>(aCurType) );
428 STGM_CREATE | ( nStreamMode & 0x73 ),
436 STGM_CREATE | ( nStreamMode & 0x73 ),
469 return CO_E_ALREADYINITIALIZED;
477 hr = pStg->Stat( &aStat, STATFLAG_NONAME );
478 if ( FAILED(
hr ) )
return E_FAIL;
480 DWORD nStreamMode = aStat.grfMode;
488 if ( SUCCEEDED(
hr ) )
500 if ( SUCCEEDED(
hr ) )
502 ULARGE_INTEGER nNewPos;
503 LARGE_INTEGER
const aZero = { 0, 0 };
505 if ( SUCCEEDED(
hr ) )
510 if ( nRead !=
sizeof aInf )
hr = E_FAIL;
512 if ( SUCCEEDED(
hr ) )
518 aSizeToSet.cx = aInf[2] - aInf[0];
519 aSizeToSet.cy = aInf[3] - aInf[1];
524 if ( SUCCEEDED(
hr ) )
531 uno::Reference< frame::XModel >
aDocument(
538 uno::Reference< frame::XLoadable > xLoadable(
m_pDocHolder->GetDocument(), uno::UNO_QUERY );
548 catch(
const uno::Exception& )
566 OSL_ENSURE( SUCCEEDED(
hr ),
"Can not destroy created stream!" );
579 CComPtr< IStream > pTargetStream;
580 CComPtr< IStream > pNewExtStream;
584 OSL_ENSURE(
m_pMasterStorage,
"How could the document be initialized without storage!??" );
586 if ( FAILED(
hr ) )
return E_FAIL;
589 hr = pStgSave->Stat( &aStat, STATFLAG_NONAME );
590 if ( FAILED(
hr ) )
return E_FAIL;
592 DWORD nStreamMode = aStat.grfMode;
594 STGM_CREATE | ( nStreamMode & 0x73 ),
598 if ( FAILED(
hr ) || !pTargetStream )
return E_FAIL;
601 STGM_CREATE | ( nStreamMode & 0x73 ),
605 if ( FAILED(
hr ) || !pNewExtStream )
return E_FAIL;
616 uno::UNO_QUERY_THROW );
618 uno::Reference< frame::XStorable > xStorable(
m_pDocHolder->GetDocument(), uno::UNO_QUERY );
623 xStorable->storeToURL(
"private:stream",
626 if ( SUCCEEDED(
hr ) )
629 ULARGE_INTEGER nNewPos;
630 LARGE_INTEGER
const aZero = { 0, 0 };
631 hr = pNewExtStream->Seek( aZero, STREAM_SEEK_SET, &nNewPos );
632 if ( SUCCEEDED(
hr ) )
637 if ( SUCCEEDED(
hr ) )
642 hr = pNewExtStream->Write( aInf,
sizeof aInf, &nWritten );
643 if ( nWritten !=
sizeof aInf )
hr = E_FAIL;
645 if ( SUCCEEDED(
hr ) )
651 uno::Reference< util::XModifiable > xMod(
m_pDocHolder->GetDocument(), uno::UNO_QUERY );
653 xMod->setModified(
false );
661 catch(
const uno::Exception& )
685 if ( FAILED(
hr ) )
return E_OUTOFMEMORY;
687 DWORD nStreamMode = aStat.grfMode;
705 advise.second->OnSave();
726 return CO_E_ALREADYINITIALIZED;
731 DWORD nStreamMode = STGM_CREATE | STGM_READWRITE | STGM_DELETEONRELEASE | STGM_SHARE_EXCLUSIVE;
732 HRESULT
hr = StgCreateDocfile(
nullptr,
740 CLIPFORMAT cf =
static_cast<CLIPFORMAT
>(RegisterClipboardFormatW( L
"Embedded Object" ));
743 const_cast<LPOLESTR
>( o3tl::toW(aCurType.data())) );
744 if ( FAILED(
hr ) )
return E_FAIL;
747 if ( FAILED(
hr ) )
return E_FAIL;
750 STGM_CREATE | ( nStreamMode & 0x73 ),
757 STGM_CREATE | ( nStreamMode & 0x73 ),
764 uno::Reference< frame::XModel >
aDocument(
771 uno::Reference< frame::XLoadable > xLoadable(
m_pDocHolder->GetDocument(), uno::UNO_QUERY );
783 catch(
const uno::Exception& )
791 cf =
static_cast<CLIPFORMAT
>(RegisterClipboardFormatW( L
"Embedded Object" ));
794 const_cast<LPOLESTR
>( o3tl::toW(aCurType.data())) );
796 if ( SUCCEEDED(
hr ) )
799 ULARGE_INTEGER nNewPos;
800 LARGE_INTEGER
const aZero = { 0, 0 };
802 if ( SUCCEEDED(
hr ) )
807 if ( SUCCEEDED(
hr ) )
813 if ( nWritten !=
sizeof aInf )
hr = E_FAIL;
818 if ( SUCCEEDED(
hr ) )
846 uno::Reference< frame::XStorable > xStorable(
m_pDocHolder->GetDocument(), uno::UNO_QUERY_THROW );
853 aURL.Complete = o3tl::toU( pszFileName );
857 if ( aTransformer->parseSmart(
aURL, OUString() ) &&
aURL.Complete.getLength() )
871 catch(
const uno::Exception& )
887 CComPtr<IMalloc> pMalloc;
889 HRESULT
hr = CoGetMalloc( 1, &pMalloc );
890 if ( FAILED(
hr ) || !pMalloc )
return E_FAIL;
892 *ppszFileName =
static_cast<LPOLESTR
>( pMalloc->Alloc(
sizeof(
sal_Unicode ) * (
m_aFileName.getLength() + 1 ) ) );
901 ::osl::MutexGuard aGuard(
m_aMutex );
907 ::osl::MutexGuard aGuard(
m_aMutex );
913: m_pEmbedDocument( nullptr )
917: m_pEmbedDocument( pEmbedDocument )
924: m_pEmbedDocument( aDocLock.m_pEmbedDocument )
HRESULT OLENotifyClosing()
STDMETHOD() InitNew(IStorage *pStg) override
virtual ~EmbedDocument_Impl()
CComPtr< IStream > m_pOwnStream
STDMETHOD() GetClassID(CLSID *pClassID) override
STDMETHOD() Save(IStorage *pStgSave, BOOL fSameAsLoad) override
STDMETHOD() QueryInterface(REFIID riid, LPVOID *ppvObj) override
STDMETHOD() Load(IStorage *pStr) override
STDMETHOD() IsDirty() override
void notify(bool bDataChanged=true)
HRESULT SaveTo_Impl(IStorage *pStg)
oslInterlockedCount m_refCount
STDMETHOD() GetCurFile(LPOLESTR *ppszFileName) override
AdviseSinkHashMap m_aAdviseHashMap
rtl::Reference< DocumentHolder > m_pDocHolder
::rtl::Reference< EmbeddedDocumentInstanceAccess_Impl > m_xOwnAccess
css::uno::Sequence< css::beans::PropertyValue > fillArgsForStoring_Impl(css::uno::Reference< css::io::XOutputStream > const &xStream)
STDMETHOD() SaveCompleted(IStorage *pStgNew) override
EmbedDocument_Impl(const css::uno::Reference< css::lang::XMultiServiceFactory > &smgr, const GUID *guid)
CComPtr< IStream > m_pExtStream
css::uno::Sequence< css::beans::PropertyValue > fillArgsForLoading_Impl(css::uno::Reference< css::io::XInputStream > const &xStream, DWORD nStreamMode, LPCOLESTR pFilePath=nullptr)
STDMETHOD() Close(DWORD dwSaveOption) override
STDMETHOD() HandsOffStorage(void) override
css::uno::Reference< css::lang::XMultiServiceFactory > m_xFactory
CComPtr< IStorage > m_pMasterStorage
void ClearEmbedDocument()
LockedEmbedDocument_Impl GetEmbedDocument()
EmbedDocument_Impl * m_pEmbedDocument
static HRESULT copyXTempOutToIStream(uno::Reference< io::XOutputStream > const &xTempOut, IStream *pStream)
const wchar_t aOfficeEmbedStreamName[]
static uno::Reference< io::XInputStream > createTempXInStreamFromIStream(uno::Reference< lang::XMultiServiceFactory > const &xFactory, IStream *pStream)
const wchar_t aExtentStreamName[]
#define EXT_STREAM_LENGTH
COM_DECLSPEC_NOTHROW STDMETHODIMP_(ULONG) EmbedDocument_Impl
#define OLESERV_SAVEOBJECT
#define OLESERV_NOTIFYCLOSING
#define OLESERV_DEACTIVATE
#define OLESERV_SHOWOBJECT
Reference< XSingleServiceFactory > xFactory
wchar_t const * getStorageTypeFromGUID_Impl(GUID const *guid)
std::u16string_view getServiceNameFromGUID_Impl(GUID const *guid)
OUString getFilterNameFromGUID_Impl(GUID const *guid)
const sal_Int32 nConstBufferSize
Reference< XComponentContext > getComponentContext(Reference< XMultiServiceFactory > const &factory)
const wchar_t *typedef BOOL
EmbedDocument_Impl * m_pEmbedDocument
LockedEmbedDocument_Impl & operator=(const LockedEmbedDocument_Impl &aDocLock)
LockedEmbedDocument_Impl()
void ExecuteMethod(sal_Int16 nId)
~LockedEmbedDocument_Impl()
std::unique_ptr< char[]> aBuffer