1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir #if defined(_MSC_VER) && (_MSC_VER > 1310) 28*cdf0e10cSrcweir #pragma warning(disable : 4917 4555) 29*cdf0e10cSrcweir #endif 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "embeddoc.hxx" 32*cdf0e10cSrcweir #include <com/sun/star/uno/Any.h> 33*cdf0e10cSrcweir #include <com/sun/star/uno/Exception.hpp> 34*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp> 35*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp> 36*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp> 37*cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp> 38*cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp> 39*cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp> 40*cdf0e10cSrcweir #include <com/sun/star/frame/XLoadable.hpp> 41*cdf0e10cSrcweir #include <com/sun/star/util/XModifiable.hpp> 42*cdf0e10cSrcweir #include <com/sun/star/frame/XStorable.hpp> 43*cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp> 44*cdf0e10cSrcweir #include <com/sun/star/util/XUrlTransformer.hpp> 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir #include <osl/mutex.hxx> 48*cdf0e10cSrcweir #include <osl/diagnose.h> 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir #include <string.h> 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir #define EXT_STREAM_LENGTH 16 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir using namespace ::com::sun::star; 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir extern ::rtl::OUString getStorageTypeFromGUID_Impl( GUID* guid ); 57*cdf0e10cSrcweir extern ::rtl::OUString getServiceNameFromGUID_Impl( GUID* ); 58*cdf0e10cSrcweir extern ::rtl::OUString getFilterNameFromGUID_Impl( GUID* ); 59*cdf0e10cSrcweir // extern CLIPFORMAT getClipFormatFromGUID_Impl( GUID* ); 60*cdf0e10cSrcweir ::rtl::OUString getTestFileURLFromGUID_Impl( GUID* guid ); 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir const ::rtl::OUString aOfficeEmbedStreamName( RTL_CONSTASCII_USTRINGPARAM ( "package_stream" ) ); 63*cdf0e10cSrcweir const ::rtl::OUString aExtentStreamName( RTL_CONSTASCII_USTRINGPARAM ( "properties_stream" ) ); 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir uno::Reference< io::XInputStream > createTempXInStreamFromIStream( 66*cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xFactory, 67*cdf0e10cSrcweir IStream *pStream ) 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir uno::Reference< io::XInputStream > xResult; 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir if ( !pStream ) 72*cdf0e10cSrcweir return xResult; 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir const ::rtl::OUString aServiceName ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.io.TempFile" ) ); 75*cdf0e10cSrcweir uno::Reference < io::XOutputStream > xTempOut = uno::Reference < io::XOutputStream > ( 76*cdf0e10cSrcweir xFactory->createInstance ( aServiceName ), 77*cdf0e10cSrcweir uno::UNO_QUERY ); 78*cdf0e10cSrcweir if ( xTempOut.is() ) 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir ULARGE_INTEGER nNewPos; 81*cdf0e10cSrcweir LARGE_INTEGER aZero = { 0L, 0L }; 82*cdf0e10cSrcweir HRESULT hr = pStream->Seek( aZero, STREAM_SEEK_SET, &nNewPos ); 83*cdf0e10cSrcweir if ( FAILED( hr ) ) return xResult; 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir STATSTG aStat; 86*cdf0e10cSrcweir hr = pStream->Stat( &aStat, STATFLAG_NONAME ); 87*cdf0e10cSrcweir if ( FAILED( hr ) ) return xResult; 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir sal_uInt32 nSize = (sal_uInt32)aStat.cbSize.QuadPart; 90*cdf0e10cSrcweir sal_uInt32 nCopied = 0; 91*cdf0e10cSrcweir uno::Sequence< sal_Int8 > aBuffer( nConstBufferSize ); 92*cdf0e10cSrcweir try 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir sal_uInt32 nRead = 0; 95*cdf0e10cSrcweir do 96*cdf0e10cSrcweir { 97*cdf0e10cSrcweir pStream->Read( (void*)aBuffer.getArray(), nConstBufferSize, &nRead ); 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir if ( nRead < nConstBufferSize ) 100*cdf0e10cSrcweir aBuffer.realloc( nRead ); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir xTempOut->writeBytes( aBuffer ); 103*cdf0e10cSrcweir nCopied += nRead; 104*cdf0e10cSrcweir } while( nRead == nConstBufferSize ); 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir if ( nCopied == nSize ) 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir uno::Reference < io::XSeekable > xTempSeek ( xTempOut, uno::UNO_QUERY ); 109*cdf0e10cSrcweir if ( xTempSeek.is() ) 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir xTempSeek->seek ( 0 ); 112*cdf0e10cSrcweir xResult = uno::Reference< io::XInputStream >( xTempOut, uno::UNO_QUERY ); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir catch( uno::Exception& ) 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir return xResult; 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir HRESULT copyXTempOutToIStream( uno::Reference< io::XOutputStream > xTempOut, IStream* pStream ) 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir if ( !xTempOut.is() || !pStream ) 127*cdf0e10cSrcweir return E_FAIL; 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir uno::Reference < io::XSeekable > xTempSeek ( xTempOut, uno::UNO_QUERY ); 130*cdf0e10cSrcweir if ( !xTempSeek.is() ) 131*cdf0e10cSrcweir return E_FAIL; 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir xTempSeek->seek ( 0 ); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir uno::Reference< io::XInputStream > xTempIn ( xTempOut, uno::UNO_QUERY ); 136*cdf0e10cSrcweir if ( !xTempSeek.is() ) 137*cdf0e10cSrcweir return E_FAIL; 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir // Seek to zero and truncate the stream 140*cdf0e10cSrcweir ULARGE_INTEGER nNewPos; 141*cdf0e10cSrcweir LARGE_INTEGER aZero = { 0L, 0L }; 142*cdf0e10cSrcweir HRESULT hr = pStream->Seek( aZero, STREAM_SEEK_SET, &nNewPos ); 143*cdf0e10cSrcweir if ( FAILED( hr ) ) return E_FAIL; 144*cdf0e10cSrcweir ULARGE_INTEGER aUZero = { 0L, 0L }; 145*cdf0e10cSrcweir hr = pStream->SetSize( aUZero ); 146*cdf0e10cSrcweir if ( FAILED( hr ) ) return E_FAIL; 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir uno::Sequence< sal_Int8 > aBuffer( nConstBufferSize ); 149*cdf0e10cSrcweir sal_uInt32 nReadBytes = 0; 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir do 152*cdf0e10cSrcweir { 153*cdf0e10cSrcweir try { 154*cdf0e10cSrcweir nReadBytes = xTempIn->readBytes( aBuffer, nConstBufferSize ); 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir catch( uno::Exception& ) 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir return E_FAIL; 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir sal_uInt32 nWritten = 0; 162*cdf0e10cSrcweir HRESULT hr = pStream->Write( (void*)aBuffer.getArray(), nReadBytes, &nWritten ); 163*cdf0e10cSrcweir if ( !SUCCEEDED( hr ) || nWritten != nReadBytes ) 164*cdf0e10cSrcweir return E_FAIL; 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir } while( nReadBytes == nConstBufferSize ); 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir return S_OK; 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir //=============================================================================== 173*cdf0e10cSrcweir // EmbedDocument_Impl 174*cdf0e10cSrcweir //=============================================================================== 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir EmbedDocument_Impl::EmbedDocument_Impl( const uno::Reference< lang::XMultiServiceFactory >& xFactory, const GUID* guid ) 177*cdf0e10cSrcweir : m_refCount( 0L ) 178*cdf0e10cSrcweir , m_xFactory( xFactory ) 179*cdf0e10cSrcweir , m_guid( *guid ) 180*cdf0e10cSrcweir , m_bIsDirty( sal_False ) 181*cdf0e10cSrcweir , m_nAdviseNum( 0 ) 182*cdf0e10cSrcweir , m_bIsInVerbHandling( sal_False ) 183*cdf0e10cSrcweir //, m_bLoadedFromFile( sal_False ) 184*cdf0e10cSrcweir { 185*cdf0e10cSrcweir m_xOwnAccess = new EmbeddedDocumentInstanceAccess_Impl( this ); 186*cdf0e10cSrcweir m_pDocHolder = new DocumentHolder( xFactory, m_xOwnAccess ); 187*cdf0e10cSrcweir m_pDocHolder->acquire(); 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir EmbedDocument_Impl::~EmbedDocument_Impl() 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir m_pDocHolder->FreeOffice(); 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir if ( m_pDocHolder->HasFrame() && m_pDocHolder->IsLink() ) 195*cdf0e10cSrcweir { 196*cdf0e10cSrcweir // a link with frame should be only disconnected, not closed 197*cdf0e10cSrcweir m_pDocHolder->DisconnectFrameDocument( sal_True ); 198*cdf0e10cSrcweir } 199*cdf0e10cSrcweir else 200*cdf0e10cSrcweir { 201*cdf0e10cSrcweir m_pDocHolder->CloseDocument(); 202*cdf0e10cSrcweir m_pDocHolder->CloseFrame(); 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir m_pDocHolder->release(); 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > EmbedDocument_Impl::fillArgsForLoading_Impl( uno::Reference< io::XInputStream > xStream, DWORD /*nStreamMode*/, LPCOLESTR pFilePath ) 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aArgs( 3 ); 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir sal_Int32 nInd = 0; // must not be bigger than the preset size 213*cdf0e10cSrcweir aArgs[nInd].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "FilterName" ) ); 214*cdf0e10cSrcweir aArgs[nInd++].Value <<= getFilterNameFromGUID_Impl( &m_guid ); 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir if ( xStream.is() ) 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir aArgs[nInd].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "InputStream" ) ); 219*cdf0e10cSrcweir aArgs[nInd++].Value <<= xStream; 220*cdf0e10cSrcweir aArgs[nInd].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "URL" ) ); 221*cdf0e10cSrcweir aArgs[nInd++].Value <<= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "private:stream" ) ); 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir else 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir aArgs[nInd].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "URL" ) ); 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir rtl::OUString sDocUrl; 228*cdf0e10cSrcweir if ( pFilePath ) 229*cdf0e10cSrcweir { 230*cdf0e10cSrcweir ::rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.util.URLTransformer" ) ); 231*cdf0e10cSrcweir uno::Reference< util::XURLTransformer > aTransformer( m_xFactory->createInstance( aServiceName ), 232*cdf0e10cSrcweir uno::UNO_QUERY ); 233*cdf0e10cSrcweir if ( aTransformer.is() ) 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir util::URL aURL; 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir aURL.Complete = ::rtl::OUString( reinterpret_cast<const sal_Unicode*>(pFilePath) ); 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir if ( aTransformer->parseSmart( aURL, ::rtl::OUString() ) ) 240*cdf0e10cSrcweir sDocUrl = aURL.Complete; 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir aArgs[nInd++].Value <<= sDocUrl; 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir aArgs.realloc( nInd ); 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir // aArgs[].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ReadOnly" ) ); 250*cdf0e10cSrcweir // aArgs[].Value <<= sal_False; //( ( nStreamMode & ( STGM_READWRITE | STGM_WRITE ) ) ? sal_True : sal_False ); 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir return aArgs; 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > EmbedDocument_Impl::fillArgsForStoring_Impl( uno::Reference< io::XOutputStream > xStream) 256*cdf0e10cSrcweir { 257*cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aArgs( xStream.is() ? 2 : 1 ); 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir aArgs[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "FilterName" ) ); 260*cdf0e10cSrcweir aArgs[0].Value <<= getFilterNameFromGUID_Impl( &m_guid ); 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir if ( xStream.is() ) 263*cdf0e10cSrcweir { 264*cdf0e10cSrcweir aArgs[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "OutputStream" ) ); 265*cdf0e10cSrcweir aArgs[1].Value <<= xStream; 266*cdf0e10cSrcweir } 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir return aArgs; 269*cdf0e10cSrcweir } 270*cdf0e10cSrcweir 271*cdf0e10cSrcweir HRESULT EmbedDocument_Impl::SaveTo_Impl( IStorage* pStg ) 272*cdf0e10cSrcweir { 273*cdf0e10cSrcweir if ( !pStg || pStg == m_pMasterStorage ) 274*cdf0e10cSrcweir return E_FAIL; 275*cdf0e10cSrcweir 276*cdf0e10cSrcweir // for saveto operation the master storage 277*cdf0e10cSrcweir // should not enter NoScribble mode 278*cdf0e10cSrcweir CComPtr< IStream > pOrigOwn = m_pOwnStream; 279*cdf0e10cSrcweir CComPtr< IStream > pOrigExt = m_pExtStream; 280*cdf0e10cSrcweir HRESULT hr = Save( pStg, sal_False ); 281*cdf0e10cSrcweir pStg->Commit( STGC_ONLYIFCURRENT ); 282*cdf0e10cSrcweir m_pOwnStream = pOrigOwn; 283*cdf0e10cSrcweir m_pExtStream = pOrigExt; 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir return hr; 286*cdf0e10cSrcweir } 287*cdf0e10cSrcweir 288*cdf0e10cSrcweir //------------------------------------------------------------------------------- 289*cdf0e10cSrcweir // IUnknown 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::QueryInterface( REFIID riid, void FAR* FAR* ppv ) 292*cdf0e10cSrcweir { 293*cdf0e10cSrcweir if(IsEqualIID(riid, IID_IUnknown)) 294*cdf0e10cSrcweir { 295*cdf0e10cSrcweir AddRef(); 296*cdf0e10cSrcweir *ppv = (IUnknown*) (IPersistStorage*) this; 297*cdf0e10cSrcweir return S_OK; 298*cdf0e10cSrcweir } 299*cdf0e10cSrcweir else if (IsEqualIID(riid, IID_IPersist)) 300*cdf0e10cSrcweir { 301*cdf0e10cSrcweir AddRef(); 302*cdf0e10cSrcweir *ppv = (IPersist*) (IPersistStorage*) this; 303*cdf0e10cSrcweir return S_OK; 304*cdf0e10cSrcweir } 305*cdf0e10cSrcweir else if (IsEqualIID(riid, IID_IExternalConnection)) 306*cdf0e10cSrcweir { 307*cdf0e10cSrcweir AddRef(); 308*cdf0e10cSrcweir *ppv = (IExternalConnection*) this; 309*cdf0e10cSrcweir return S_OK; 310*cdf0e10cSrcweir } 311*cdf0e10cSrcweir else if (IsEqualIID(riid, IID_IPersistStorage)) 312*cdf0e10cSrcweir { 313*cdf0e10cSrcweir AddRef(); 314*cdf0e10cSrcweir *ppv = (IPersistStorage*) this; 315*cdf0e10cSrcweir return S_OK; 316*cdf0e10cSrcweir } 317*cdf0e10cSrcweir else if (IsEqualIID(riid, IID_IDataObject)) 318*cdf0e10cSrcweir { 319*cdf0e10cSrcweir AddRef(); 320*cdf0e10cSrcweir *ppv = (IDataObject*) this; 321*cdf0e10cSrcweir return S_OK; 322*cdf0e10cSrcweir } 323*cdf0e10cSrcweir else if (IsEqualIID(riid, IID_IOleObject)) 324*cdf0e10cSrcweir { 325*cdf0e10cSrcweir AddRef(); 326*cdf0e10cSrcweir *ppv = (IOleObject*) this; 327*cdf0e10cSrcweir return S_OK; 328*cdf0e10cSrcweir } 329*cdf0e10cSrcweir else if (IsEqualIID(riid, IID_IOleWindow)) 330*cdf0e10cSrcweir { 331*cdf0e10cSrcweir AddRef(); 332*cdf0e10cSrcweir *ppv = (IOleWindow*) this; 333*cdf0e10cSrcweir return S_OK; 334*cdf0e10cSrcweir } 335*cdf0e10cSrcweir else if (IsEqualIID(riid, IID_IOleInPlaceObject)) 336*cdf0e10cSrcweir { 337*cdf0e10cSrcweir AddRef(); 338*cdf0e10cSrcweir *ppv = (IOleInPlaceObject*) this; 339*cdf0e10cSrcweir return S_OK; 340*cdf0e10cSrcweir } 341*cdf0e10cSrcweir else if (IsEqualIID(riid, IID_IPersistFile)) 342*cdf0e10cSrcweir { 343*cdf0e10cSrcweir AddRef(); 344*cdf0e10cSrcweir *ppv = (IPersistFile*) this; 345*cdf0e10cSrcweir return S_OK; 346*cdf0e10cSrcweir } 347*cdf0e10cSrcweir else if (IsEqualIID(riid, IID_IDispatch)) 348*cdf0e10cSrcweir { 349*cdf0e10cSrcweir AddRef(); 350*cdf0e10cSrcweir *ppv = (IDispatch*) this; 351*cdf0e10cSrcweir return S_OK; 352*cdf0e10cSrcweir } 353*cdf0e10cSrcweir 354*cdf0e10cSrcweir *ppv = NULL; 355*cdf0e10cSrcweir return ResultFromScode(E_NOINTERFACE); 356*cdf0e10cSrcweir } 357*cdf0e10cSrcweir 358*cdf0e10cSrcweir STDMETHODIMP_(ULONG) EmbedDocument_Impl::AddRef() 359*cdf0e10cSrcweir { 360*cdf0e10cSrcweir return osl_incrementInterlockedCount( &m_refCount); 361*cdf0e10cSrcweir } 362*cdf0e10cSrcweir 363*cdf0e10cSrcweir STDMETHODIMP_(ULONG) EmbedDocument_Impl::Release() 364*cdf0e10cSrcweir { 365*cdf0e10cSrcweir // if there is a time when the last reference is destructed, that means that only internal pointers are alive 366*cdf0e10cSrcweir // after the following call either the refcount is increased or the pointers are empty 367*cdf0e10cSrcweir if ( m_refCount == 1 ) 368*cdf0e10cSrcweir m_xOwnAccess->ClearEmbedDocument(); 369*cdf0e10cSrcweir 370*cdf0e10cSrcweir sal_Int32 nCount = osl_decrementInterlockedCount( &m_refCount ); 371*cdf0e10cSrcweir if ( nCount == 0 ) 372*cdf0e10cSrcweir delete this; 373*cdf0e10cSrcweir return nCount; 374*cdf0e10cSrcweir } 375*cdf0e10cSrcweir 376*cdf0e10cSrcweir //------------------------------------------------------------------------------- 377*cdf0e10cSrcweir // IPersist 378*cdf0e10cSrcweir 379*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::GetClassID( CLSID* pClassId ) 380*cdf0e10cSrcweir { 381*cdf0e10cSrcweir *pClassId = *&m_guid; 382*cdf0e10cSrcweir return S_OK; 383*cdf0e10cSrcweir } 384*cdf0e10cSrcweir 385*cdf0e10cSrcweir //------------------------------------------------------------------------------- 386*cdf0e10cSrcweir // IPersistStorage 387*cdf0e10cSrcweir 388*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::IsDirty() 389*cdf0e10cSrcweir { 390*cdf0e10cSrcweir // the link modified state is controlled by the document 391*cdf0e10cSrcweir if ( m_bIsDirty && !m_aFileName.getLength() ) 392*cdf0e10cSrcweir return S_OK; 393*cdf0e10cSrcweir 394*cdf0e10cSrcweir uno::Reference< util::XModifiable > xMod( m_pDocHolder->GetDocument(), uno::UNO_QUERY ); 395*cdf0e10cSrcweir if ( xMod.is() ) 396*cdf0e10cSrcweir return xMod->isModified() ? S_OK : S_FALSE; 397*cdf0e10cSrcweir return S_FALSE; 398*cdf0e10cSrcweir } 399*cdf0e10cSrcweir 400*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::InitNew( IStorage *pStg ) 401*cdf0e10cSrcweir { 402*cdf0e10cSrcweir HRESULT hr = CO_E_ALREADYINITIALIZED; 403*cdf0e10cSrcweir 404*cdf0e10cSrcweir if ( !m_pDocHolder->GetDocument().is() ) 405*cdf0e10cSrcweir { 406*cdf0e10cSrcweir 407*cdf0e10cSrcweir STATSTG aStat; 408*cdf0e10cSrcweir hr = pStg->Stat( &aStat, STATFLAG_NONAME ); 409*cdf0e10cSrcweir if ( FAILED( hr ) ) return E_FAIL; 410*cdf0e10cSrcweir 411*cdf0e10cSrcweir DWORD nStreamMode = aStat.grfMode; 412*cdf0e10cSrcweir 413*cdf0e10cSrcweir hr = E_FAIL; 414*cdf0e10cSrcweir if ( m_xFactory.is() && pStg ) 415*cdf0e10cSrcweir { 416*cdf0e10cSrcweir uno::Reference< frame::XModel > aDocument( 417*cdf0e10cSrcweir m_xFactory->createInstance( getServiceNameFromGUID_Impl( &m_guid ) ), 418*cdf0e10cSrcweir uno::UNO_QUERY ); 419*cdf0e10cSrcweir if ( aDocument.is() ) 420*cdf0e10cSrcweir { 421*cdf0e10cSrcweir m_pDocHolder->SetDocument( aDocument ); 422*cdf0e10cSrcweir 423*cdf0e10cSrcweir uno::Reference< frame::XLoadable > xLoadable( m_pDocHolder->GetDocument(), uno::UNO_QUERY ); 424*cdf0e10cSrcweir if( xLoadable.is() ) 425*cdf0e10cSrcweir { 426*cdf0e10cSrcweir try 427*cdf0e10cSrcweir { 428*cdf0e10cSrcweir xLoadable->initNew(); 429*cdf0e10cSrcweir // xLoadable->load( fillArgsForLoading_Impl( uno::Reference< io::XInputStream >(), nStreamMode ) ); 430*cdf0e10cSrcweir hr = S_OK; 431*cdf0e10cSrcweir } 432*cdf0e10cSrcweir catch( uno::Exception& ) 433*cdf0e10cSrcweir { 434*cdf0e10cSrcweir } 435*cdf0e10cSrcweir } 436*cdf0e10cSrcweir 437*cdf0e10cSrcweir if ( hr == S_OK ) 438*cdf0e10cSrcweir { 439*cdf0e10cSrcweir ::rtl::OUString aCurType = getStorageTypeFromGUID_Impl( &m_guid ); // ??? 440*cdf0e10cSrcweir CLIPFORMAT cf = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" ); 441*cdf0e10cSrcweir hr = WriteFmtUserTypeStg( pStg, 442*cdf0e10cSrcweir cf, // ??? 443*cdf0e10cSrcweir reinterpret_cast<LPWSTR>(( sal_Unicode* )aCurType.getStr()) ); 444*cdf0e10cSrcweir 445*cdf0e10cSrcweir if ( hr == S_OK ) 446*cdf0e10cSrcweir { 447*cdf0e10cSrcweir hr = pStg->CreateStream( reinterpret_cast<LPCWSTR>(aOfficeEmbedStreamName.getStr()), 448*cdf0e10cSrcweir STGM_CREATE | ( nStreamMode & 0x73 ), 449*cdf0e10cSrcweir 0, 450*cdf0e10cSrcweir 0, 451*cdf0e10cSrcweir &m_pOwnStream ); 452*cdf0e10cSrcweir 453*cdf0e10cSrcweir if ( hr == S_OK && m_pOwnStream ) 454*cdf0e10cSrcweir { 455*cdf0e10cSrcweir hr = pStg->CreateStream( reinterpret_cast<LPCWSTR>(aExtentStreamName.getStr()), 456*cdf0e10cSrcweir STGM_CREATE | ( nStreamMode & 0x73 ), 457*cdf0e10cSrcweir 0, 458*cdf0e10cSrcweir 0, 459*cdf0e10cSrcweir &m_pExtStream ); 460*cdf0e10cSrcweir 461*cdf0e10cSrcweir if ( hr == S_OK && m_pExtStream ) 462*cdf0e10cSrcweir { 463*cdf0e10cSrcweir 464*cdf0e10cSrcweir m_pMasterStorage = pStg; 465*cdf0e10cSrcweir m_bIsDirty = sal_True; 466*cdf0e10cSrcweir } 467*cdf0e10cSrcweir else 468*cdf0e10cSrcweir hr = E_FAIL; 469*cdf0e10cSrcweir } 470*cdf0e10cSrcweir else 471*cdf0e10cSrcweir hr = E_FAIL; 472*cdf0e10cSrcweir } 473*cdf0e10cSrcweir else 474*cdf0e10cSrcweir hr = E_FAIL; 475*cdf0e10cSrcweir } 476*cdf0e10cSrcweir 477*cdf0e10cSrcweir if ( hr != S_OK ) 478*cdf0e10cSrcweir m_pDocHolder->CloseDocument(); 479*cdf0e10cSrcweir } 480*cdf0e10cSrcweir } 481*cdf0e10cSrcweir } 482*cdf0e10cSrcweir 483*cdf0e10cSrcweir return hr; 484*cdf0e10cSrcweir } 485*cdf0e10cSrcweir 486*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::Load( IStorage *pStg ) 487*cdf0e10cSrcweir { 488*cdf0e10cSrcweir if ( m_pDocHolder->GetDocument().is() ) 489*cdf0e10cSrcweir return CO_E_ALREADYINITIALIZED; 490*cdf0e10cSrcweir 491*cdf0e10cSrcweir if ( !m_xFactory.is() || !pStg ) 492*cdf0e10cSrcweir return E_FAIL; 493*cdf0e10cSrcweir 494*cdf0e10cSrcweir HRESULT hr = E_FAIL; 495*cdf0e10cSrcweir 496*cdf0e10cSrcweir STATSTG aStat; 497*cdf0e10cSrcweir hr = pStg->Stat( &aStat, STATFLAG_NONAME ); 498*cdf0e10cSrcweir if ( FAILED( hr ) ) return E_FAIL; 499*cdf0e10cSrcweir 500*cdf0e10cSrcweir DWORD nStreamMode = aStat.grfMode; 501*cdf0e10cSrcweir hr = pStg->OpenStream( reinterpret_cast<LPCWSTR>(aOfficeEmbedStreamName.getStr()), 502*cdf0e10cSrcweir 0, 503*cdf0e10cSrcweir nStreamMode & 0x73, 504*cdf0e10cSrcweir 0, 505*cdf0e10cSrcweir &m_pOwnStream ); 506*cdf0e10cSrcweir if ( !m_pOwnStream ) hr = E_FAIL; 507*cdf0e10cSrcweir 508*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 509*cdf0e10cSrcweir { 510*cdf0e10cSrcweir hr = pStg->OpenStream( reinterpret_cast<LPCWSTR>(aExtentStreamName.getStr()), 511*cdf0e10cSrcweir 0, 512*cdf0e10cSrcweir nStreamMode & 0x73, 513*cdf0e10cSrcweir 0, 514*cdf0e10cSrcweir &m_pExtStream ); 515*cdf0e10cSrcweir if ( !m_pExtStream ) hr = E_FAIL; 516*cdf0e10cSrcweir } 517*cdf0e10cSrcweir 518*cdf0e10cSrcweir // RECTL aRectToSet; 519*cdf0e10cSrcweir SIZEL aSizeToSet; 520*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 521*cdf0e10cSrcweir { 522*cdf0e10cSrcweir ULARGE_INTEGER nNewPos; 523*cdf0e10cSrcweir LARGE_INTEGER aZero = { 0L, 0L }; 524*cdf0e10cSrcweir hr = m_pExtStream->Seek( aZero, STREAM_SEEK_SET, &nNewPos ); 525*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 526*cdf0e10cSrcweir { 527*cdf0e10cSrcweir sal_uInt32 nRead; 528*cdf0e10cSrcweir sal_Int8 aInf[EXT_STREAM_LENGTH]; 529*cdf0e10cSrcweir hr = m_pExtStream->Read( (void*)aInf, EXT_STREAM_LENGTH, &nRead ); 530*cdf0e10cSrcweir if ( nRead != EXT_STREAM_LENGTH ) hr = E_FAIL; 531*cdf0e10cSrcweir 532*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 533*cdf0e10cSrcweir { 534*cdf0e10cSrcweir // aRectToSet.left = *((sal_Int32*)aInf); 535*cdf0e10cSrcweir // aRectToSet.top = *((sal_Int32*)&aInf[4]); 536*cdf0e10cSrcweir // aRectToSet.right = *((sal_Int32*)&aInf[8]); 537*cdf0e10cSrcweir // aRectToSet.bottom = *((sal_Int32*)&aInf[12]); 538*cdf0e10cSrcweir aSizeToSet.cx = *((sal_Int32*)&aInf[8]) - *((sal_Int32*)aInf); 539*cdf0e10cSrcweir aSizeToSet.cy = *((sal_Int32*)&aInf[12]) - *((sal_Int32*)&aInf[4]); 540*cdf0e10cSrcweir } 541*cdf0e10cSrcweir } 542*cdf0e10cSrcweir } 543*cdf0e10cSrcweir 544*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 545*cdf0e10cSrcweir { 546*cdf0e10cSrcweir hr = E_FAIL; 547*cdf0e10cSrcweir 548*cdf0e10cSrcweir uno::Reference < io::XInputStream > xTempIn = createTempXInStreamFromIStream( m_xFactory, m_pOwnStream ); 549*cdf0e10cSrcweir if ( xTempIn.is() ) 550*cdf0e10cSrcweir { 551*cdf0e10cSrcweir uno::Reference< frame::XModel > aDocument( 552*cdf0e10cSrcweir m_xFactory->createInstance( getServiceNameFromGUID_Impl( &m_guid ) ), 553*cdf0e10cSrcweir uno::UNO_QUERY ); 554*cdf0e10cSrcweir if ( aDocument.is() ) 555*cdf0e10cSrcweir { 556*cdf0e10cSrcweir m_pDocHolder->SetDocument( aDocument ); 557*cdf0e10cSrcweir 558*cdf0e10cSrcweir uno::Reference< frame::XLoadable > xLoadable( m_pDocHolder->GetDocument(), uno::UNO_QUERY ); 559*cdf0e10cSrcweir if( xLoadable.is() ) 560*cdf0e10cSrcweir { 561*cdf0e10cSrcweir try 562*cdf0e10cSrcweir { 563*cdf0e10cSrcweir xLoadable->load( fillArgsForLoading_Impl( xTempIn, nStreamMode ) ); 564*cdf0e10cSrcweir m_pMasterStorage = pStg; 565*cdf0e10cSrcweir hr = m_pDocHolder->SetExtent( &aSizeToSet ); 566*cdf0e10cSrcweir // hr = m_pDocHolder->SetVisArea( &aRectToSet ); 567*cdf0e10cSrcweir } 568*cdf0e10cSrcweir catch( uno::Exception& ) 569*cdf0e10cSrcweir { 570*cdf0e10cSrcweir } 571*cdf0e10cSrcweir } 572*cdf0e10cSrcweir 573*cdf0e10cSrcweir if ( FAILED( hr ) ) 574*cdf0e10cSrcweir m_pDocHolder->CloseDocument(); 575*cdf0e10cSrcweir } 576*cdf0e10cSrcweir } 577*cdf0e10cSrcweir } 578*cdf0e10cSrcweir 579*cdf0e10cSrcweir if ( FAILED( hr ) ) 580*cdf0e10cSrcweir { 581*cdf0e10cSrcweir m_pOwnStream = CComPtr< IStream >(); 582*cdf0e10cSrcweir m_pExtStream = CComPtr< IStream >(); 583*cdf0e10cSrcweir hr = pStg->DestroyElement( reinterpret_cast<LPCWSTR>(aOfficeEmbedStreamName.getStr()) ); 584*cdf0e10cSrcweir hr = pStg->DestroyElement( reinterpret_cast<LPCWSTR>(aExtentStreamName.getStr()) ); 585*cdf0e10cSrcweir 586*cdf0e10cSrcweir OSL_ENSURE( SUCCEEDED( hr ), "Can not destroy created stream!\n" ); 587*cdf0e10cSrcweir if ( FAILED( hr ) ) 588*cdf0e10cSrcweir hr = E_FAIL; 589*cdf0e10cSrcweir } 590*cdf0e10cSrcweir 591*cdf0e10cSrcweir return hr; 592*cdf0e10cSrcweir } 593*cdf0e10cSrcweir 594*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::Save( IStorage *pStgSave, BOOL fSameAsLoad ) 595*cdf0e10cSrcweir { 596*cdf0e10cSrcweir if ( !m_pDocHolder->GetDocument().is() || !m_xFactory.is() || !pStgSave || !m_pOwnStream || !m_pExtStream ) 597*cdf0e10cSrcweir return E_FAIL; 598*cdf0e10cSrcweir 599*cdf0e10cSrcweir CComPtr< IStream > pTargetStream; 600*cdf0e10cSrcweir CComPtr< IStream > pNewExtStream; 601*cdf0e10cSrcweir 602*cdf0e10cSrcweir if ( !fSameAsLoad && pStgSave != m_pMasterStorage ) 603*cdf0e10cSrcweir { 604*cdf0e10cSrcweir OSL_ENSURE( m_pMasterStorage, "How could the document be initialized without storage!??\n" ); 605*cdf0e10cSrcweir HRESULT hr = m_pMasterStorage->CopyTo( NULL, NULL, NULL, pStgSave ); 606*cdf0e10cSrcweir if ( FAILED( hr ) ) return E_FAIL; 607*cdf0e10cSrcweir 608*cdf0e10cSrcweir STATSTG aStat; 609*cdf0e10cSrcweir hr = pStgSave->Stat( &aStat, STATFLAG_NONAME ); 610*cdf0e10cSrcweir if ( FAILED( hr ) ) return E_FAIL; 611*cdf0e10cSrcweir 612*cdf0e10cSrcweir DWORD nStreamMode = aStat.grfMode; 613*cdf0e10cSrcweir hr = pStgSave->CreateStream( reinterpret_cast<LPCWSTR>(aOfficeEmbedStreamName.getStr()), 614*cdf0e10cSrcweir STGM_CREATE | ( nStreamMode & 0x73 ), 615*cdf0e10cSrcweir 0, 616*cdf0e10cSrcweir 0, 617*cdf0e10cSrcweir &pTargetStream ); 618*cdf0e10cSrcweir if ( FAILED( hr ) || !pTargetStream ) return E_FAIL; 619*cdf0e10cSrcweir 620*cdf0e10cSrcweir hr = pStgSave->CreateStream( reinterpret_cast<LPCWSTR>(aExtentStreamName.getStr()), 621*cdf0e10cSrcweir STGM_CREATE | ( nStreamMode & 0x73 ), 622*cdf0e10cSrcweir 0, 623*cdf0e10cSrcweir 0, 624*cdf0e10cSrcweir &pNewExtStream ); 625*cdf0e10cSrcweir if ( FAILED( hr ) || !pNewExtStream ) return E_FAIL; 626*cdf0e10cSrcweir } 627*cdf0e10cSrcweir else 628*cdf0e10cSrcweir { 629*cdf0e10cSrcweir pTargetStream = m_pOwnStream; 630*cdf0e10cSrcweir pNewExtStream = m_pExtStream; 631*cdf0e10cSrcweir } 632*cdf0e10cSrcweir 633*cdf0e10cSrcweir HRESULT hr = E_FAIL; 634*cdf0e10cSrcweir 635*cdf0e10cSrcweir const ::rtl::OUString aServiceName ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.io.TempFile" ) ); 636*cdf0e10cSrcweir uno::Reference < io::XOutputStream > xTempOut = uno::Reference < io::XOutputStream > ( 637*cdf0e10cSrcweir m_xFactory->createInstance ( aServiceName ), 638*cdf0e10cSrcweir uno::UNO_QUERY ); 639*cdf0e10cSrcweir 640*cdf0e10cSrcweir if ( xTempOut.is() ) 641*cdf0e10cSrcweir { 642*cdf0e10cSrcweir uno::Reference< frame::XStorable > xStorable( m_pDocHolder->GetDocument(), uno::UNO_QUERY ); 643*cdf0e10cSrcweir if( xStorable.is() ) 644*cdf0e10cSrcweir { 645*cdf0e10cSrcweir try 646*cdf0e10cSrcweir { 647*cdf0e10cSrcweir xStorable->storeToURL( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "private:stream" ) ), 648*cdf0e10cSrcweir fillArgsForStoring_Impl( xTempOut ) ); 649*cdf0e10cSrcweir hr = copyXTempOutToIStream( xTempOut, pTargetStream ); 650*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 651*cdf0e10cSrcweir { 652*cdf0e10cSrcweir // no need to truncate the stream, the size of the stream is always the same 653*cdf0e10cSrcweir ULARGE_INTEGER nNewPos; 654*cdf0e10cSrcweir LARGE_INTEGER aZero = { 0L, 0L }; 655*cdf0e10cSrcweir hr = pNewExtStream->Seek( aZero, STREAM_SEEK_SET, &nNewPos ); 656*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 657*cdf0e10cSrcweir { 658*cdf0e10cSrcweir SIZEL aSize; 659*cdf0e10cSrcweir hr = m_pDocHolder->GetExtent( &aSize ); 660*cdf0e10cSrcweir 661*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 662*cdf0e10cSrcweir { 663*cdf0e10cSrcweir sal_uInt32 nWritten; 664*cdf0e10cSrcweir sal_Int8 aInf[EXT_STREAM_LENGTH]; 665*cdf0e10cSrcweir *((sal_Int32*)aInf) = 0; 666*cdf0e10cSrcweir *((sal_Int32*)&aInf[4]) = 0; 667*cdf0e10cSrcweir *((sal_Int32*)&aInf[8]) = aSize.cx; 668*cdf0e10cSrcweir *((sal_Int32*)&aInf[12]) = aSize.cy; 669*cdf0e10cSrcweir 670*cdf0e10cSrcweir hr = pNewExtStream->Write( (void*)aInf, EXT_STREAM_LENGTH, &nWritten ); 671*cdf0e10cSrcweir if ( nWritten != EXT_STREAM_LENGTH ) hr = E_FAIL; 672*cdf0e10cSrcweir 673*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 674*cdf0e10cSrcweir { 675*cdf0e10cSrcweir m_pOwnStream = CComPtr< IStream >(); 676*cdf0e10cSrcweir m_pExtStream = CComPtr< IStream >(); 677*cdf0e10cSrcweir if ( fSameAsLoad || pStgSave == m_pMasterStorage ) 678*cdf0e10cSrcweir { 679*cdf0e10cSrcweir uno::Reference< util::XModifiable > xMod( m_pDocHolder->GetDocument(), uno::UNO_QUERY ); 680*cdf0e10cSrcweir if ( xMod.is() ) 681*cdf0e10cSrcweir xMod->setModified( sal_False ); 682*cdf0e10cSrcweir m_bIsDirty = sal_False; 683*cdf0e10cSrcweir } 684*cdf0e10cSrcweir } 685*cdf0e10cSrcweir } 686*cdf0e10cSrcweir } 687*cdf0e10cSrcweir } 688*cdf0e10cSrcweir } 689*cdf0e10cSrcweir catch( uno::Exception& ) 690*cdf0e10cSrcweir { 691*cdf0e10cSrcweir } 692*cdf0e10cSrcweir } 693*cdf0e10cSrcweir } 694*cdf0e10cSrcweir 695*cdf0e10cSrcweir return hr; 696*cdf0e10cSrcweir } 697*cdf0e10cSrcweir 698*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::SaveCompleted( IStorage *pStgNew ) 699*cdf0e10cSrcweir { 700*cdf0e10cSrcweir // m_pOwnStream == NULL && m_pMasterStorage != NULL means the object is in NoScribble mode 701*cdf0e10cSrcweir // m_pOwnStream == NULL && m_pMasterStorage == NULL means the object is in HandsOff mode 702*cdf0e10cSrcweir 703*cdf0e10cSrcweir if ( m_pOwnStream || m_pExtStream ) 704*cdf0e10cSrcweir return E_UNEXPECTED; 705*cdf0e10cSrcweir 706*cdf0e10cSrcweir if ( !m_pMasterStorage && !pStgNew ) 707*cdf0e10cSrcweir return E_INVALIDARG; 708*cdf0e10cSrcweir 709*cdf0e10cSrcweir if ( pStgNew ) 710*cdf0e10cSrcweir m_pMasterStorage = pStgNew; 711*cdf0e10cSrcweir 712*cdf0e10cSrcweir STATSTG aStat; 713*cdf0e10cSrcweir HRESULT hr = m_pMasterStorage->Stat( &aStat, STATFLAG_NONAME ); 714*cdf0e10cSrcweir if ( FAILED( hr ) ) return E_OUTOFMEMORY; 715*cdf0e10cSrcweir 716*cdf0e10cSrcweir DWORD nStreamMode = aStat.grfMode; 717*cdf0e10cSrcweir hr = m_pMasterStorage->OpenStream( reinterpret_cast<LPCWSTR>(aOfficeEmbedStreamName.getStr()), 718*cdf0e10cSrcweir 0, 719*cdf0e10cSrcweir nStreamMode & 0x73, 720*cdf0e10cSrcweir 0, 721*cdf0e10cSrcweir &m_pOwnStream ); 722*cdf0e10cSrcweir if ( FAILED( hr ) || !m_pOwnStream ) return E_OUTOFMEMORY; 723*cdf0e10cSrcweir 724*cdf0e10cSrcweir hr = m_pMasterStorage->OpenStream( reinterpret_cast<LPCWSTR>(aExtentStreamName.getStr()), 725*cdf0e10cSrcweir 0, 726*cdf0e10cSrcweir nStreamMode & 0x73, 727*cdf0e10cSrcweir 0, 728*cdf0e10cSrcweir &m_pExtStream ); 729*cdf0e10cSrcweir if ( FAILED( hr ) || !m_pExtStream ) return E_OUTOFMEMORY; 730*cdf0e10cSrcweir 731*cdf0e10cSrcweir sal_Bool bModified = sal_False; 732*cdf0e10cSrcweir uno::Reference< util::XModifiable > xMod( m_pDocHolder->GetDocument(), uno::UNO_QUERY ); 733*cdf0e10cSrcweir if ( xMod.is() ) 734*cdf0e10cSrcweir bModified = xMod->isModified(); 735*cdf0e10cSrcweir 736*cdf0e10cSrcweir for ( AdviseSinkHashMapIterator iAdvise = m_aAdviseHashMap.begin(); iAdvise != m_aAdviseHashMap.end(); iAdvise++ ) 737*cdf0e10cSrcweir { 738*cdf0e10cSrcweir if ( iAdvise->second ) 739*cdf0e10cSrcweir iAdvise->second->OnSave(); 740*cdf0e10cSrcweir } 741*cdf0e10cSrcweir 742*cdf0e10cSrcweir if ( xMod.is() ) 743*cdf0e10cSrcweir bModified = xMod->isModified(); 744*cdf0e10cSrcweir 745*cdf0e10cSrcweir return S_OK; 746*cdf0e10cSrcweir } 747*cdf0e10cSrcweir 748*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::HandsOffStorage() 749*cdf0e10cSrcweir { 750*cdf0e10cSrcweir m_pMasterStorage = CComPtr< IStorage >(); 751*cdf0e10cSrcweir m_pOwnStream = CComPtr< IStream >(); 752*cdf0e10cSrcweir m_pExtStream = CComPtr< IStream >(); 753*cdf0e10cSrcweir 754*cdf0e10cSrcweir return S_OK; 755*cdf0e10cSrcweir } 756*cdf0e10cSrcweir 757*cdf0e10cSrcweir //------------------------------------------------------------------------------- 758*cdf0e10cSrcweir // IPersistFile 759*cdf0e10cSrcweir 760*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::Load( LPCOLESTR pszFileName, DWORD /*dwMode*/ ) 761*cdf0e10cSrcweir { 762*cdf0e10cSrcweir if ( m_pDocHolder->GetDocument().is() ) 763*cdf0e10cSrcweir return CO_E_ALREADYINITIALIZED; 764*cdf0e10cSrcweir 765*cdf0e10cSrcweir if ( !m_xFactory.is() ) 766*cdf0e10cSrcweir return E_FAIL; 767*cdf0e10cSrcweir 768*cdf0e10cSrcweir DWORD nStreamMode = STGM_CREATE | STGM_READWRITE | STGM_DELETEONRELEASE | STGM_SHARE_EXCLUSIVE; 769*cdf0e10cSrcweir HRESULT hr = StgCreateDocfile( NULL, 770*cdf0e10cSrcweir nStreamMode , 771*cdf0e10cSrcweir 0, 772*cdf0e10cSrcweir &m_pMasterStorage ); 773*cdf0e10cSrcweir 774*cdf0e10cSrcweir if ( FAILED( hr ) || !m_pMasterStorage ) return E_FAIL; 775*cdf0e10cSrcweir 776*cdf0e10cSrcweir ::rtl::OUString aCurType = getServiceNameFromGUID_Impl( &m_guid ); // ??? 777*cdf0e10cSrcweir CLIPFORMAT cf = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" ); 778*cdf0e10cSrcweir hr = WriteFmtUserTypeStg( m_pMasterStorage, 779*cdf0e10cSrcweir cf, // ??? 780*cdf0e10cSrcweir reinterpret_cast<LPWSTR>(( sal_Unicode* )aCurType.getStr()) ); 781*cdf0e10cSrcweir if ( FAILED( hr ) ) return E_FAIL; 782*cdf0e10cSrcweir 783*cdf0e10cSrcweir hr = m_pMasterStorage->SetClass( m_guid ); 784*cdf0e10cSrcweir if ( FAILED( hr ) ) return E_FAIL; 785*cdf0e10cSrcweir 786*cdf0e10cSrcweir hr = m_pMasterStorage->CreateStream( reinterpret_cast<LPCWSTR>(aOfficeEmbedStreamName.getStr()), 787*cdf0e10cSrcweir STGM_CREATE | ( nStreamMode & 0x73 ), 788*cdf0e10cSrcweir 0, 789*cdf0e10cSrcweir 0, 790*cdf0e10cSrcweir &m_pOwnStream ); 791*cdf0e10cSrcweir if ( FAILED( hr ) || !m_pOwnStream ) return E_FAIL; 792*cdf0e10cSrcweir 793*cdf0e10cSrcweir hr = m_pMasterStorage->CreateStream( reinterpret_cast<LPCWSTR>(aExtentStreamName.getStr()), 794*cdf0e10cSrcweir STGM_CREATE | ( nStreamMode & 0x73 ), 795*cdf0e10cSrcweir 0, 796*cdf0e10cSrcweir 0, 797*cdf0e10cSrcweir &m_pExtStream ); 798*cdf0e10cSrcweir if ( FAILED( hr ) || !m_pExtStream ) return E_FAIL; 799*cdf0e10cSrcweir 800*cdf0e10cSrcweir 801*cdf0e10cSrcweir uno::Reference< frame::XModel > aDocument( 802*cdf0e10cSrcweir m_xFactory->createInstance( getServiceNameFromGUID_Impl( &m_guid ) ), 803*cdf0e10cSrcweir uno::UNO_QUERY ); 804*cdf0e10cSrcweir if ( aDocument.is() ) 805*cdf0e10cSrcweir { 806*cdf0e10cSrcweir m_pDocHolder->SetDocument( aDocument, sal_True ); 807*cdf0e10cSrcweir 808*cdf0e10cSrcweir uno::Reference< frame::XLoadable > xLoadable( m_pDocHolder->GetDocument(), uno::UNO_QUERY ); 809*cdf0e10cSrcweir if( xLoadable.is() ) 810*cdf0e10cSrcweir { 811*cdf0e10cSrcweir try 812*cdf0e10cSrcweir { 813*cdf0e10cSrcweir xLoadable->load( fillArgsForLoading_Impl( uno::Reference< io::XInputStream >(), 814*cdf0e10cSrcweir STGM_READWRITE, 815*cdf0e10cSrcweir pszFileName ) ); 816*cdf0e10cSrcweir hr = S_OK; 817*cdf0e10cSrcweir 818*cdf0e10cSrcweir m_aFileName = ::rtl::OUString( reinterpret_cast<const sal_Unicode*>(pszFileName) ); 819*cdf0e10cSrcweir } 820*cdf0e10cSrcweir catch( uno::Exception& ) 821*cdf0e10cSrcweir { 822*cdf0e10cSrcweir } 823*cdf0e10cSrcweir } 824*cdf0e10cSrcweir 825*cdf0e10cSrcweir if ( hr == S_OK ) 826*cdf0e10cSrcweir { 827*cdf0e10cSrcweir ::rtl::OUString aCurType = getServiceNameFromGUID_Impl( &m_guid ); // ??? 828*cdf0e10cSrcweir CLIPFORMAT cf = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" ); 829*cdf0e10cSrcweir hr = WriteFmtUserTypeStg( m_pMasterStorage, 830*cdf0e10cSrcweir cf, // ??? 831*cdf0e10cSrcweir reinterpret_cast<LPWSTR>(( sal_Unicode* )aCurType.getStr()) ); 832*cdf0e10cSrcweir 833*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 834*cdf0e10cSrcweir { 835*cdf0e10cSrcweir // no need to truncate the stream, the size of the stream is always the same 836*cdf0e10cSrcweir ULARGE_INTEGER nNewPos; 837*cdf0e10cSrcweir LARGE_INTEGER aZero = { 0L, 0L }; 838*cdf0e10cSrcweir hr = m_pExtStream->Seek( aZero, STREAM_SEEK_SET, &nNewPos ); 839*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 840*cdf0e10cSrcweir { 841*cdf0e10cSrcweir SIZEL aSize; 842*cdf0e10cSrcweir hr = m_pDocHolder->GetExtent( &aSize ); 843*cdf0e10cSrcweir 844*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 845*cdf0e10cSrcweir { 846*cdf0e10cSrcweir sal_uInt32 nWritten; 847*cdf0e10cSrcweir sal_Int8 aInf[EXT_STREAM_LENGTH]; 848*cdf0e10cSrcweir *((sal_Int32*)aInf) = 0; 849*cdf0e10cSrcweir *((sal_Int32*)&aInf[4]) = 0; 850*cdf0e10cSrcweir *((sal_Int32*)&aInf[8]) = aSize.cx; 851*cdf0e10cSrcweir *((sal_Int32*)&aInf[12]) = aSize.cy; 852*cdf0e10cSrcweir 853*cdf0e10cSrcweir hr = m_pExtStream->Write( (void*)aInf, EXT_STREAM_LENGTH, &nWritten ); 854*cdf0e10cSrcweir if ( nWritten != EXT_STREAM_LENGTH ) hr = E_FAIL; 855*cdf0e10cSrcweir } 856*cdf0e10cSrcweir } 857*cdf0e10cSrcweir } 858*cdf0e10cSrcweir 859*cdf0e10cSrcweir if ( SUCCEEDED( hr ) ) 860*cdf0e10cSrcweir m_bIsDirty = sal_True; 861*cdf0e10cSrcweir else 862*cdf0e10cSrcweir hr = E_FAIL; 863*cdf0e10cSrcweir } 864*cdf0e10cSrcweir 865*cdf0e10cSrcweir if ( FAILED( hr ) ) 866*cdf0e10cSrcweir { 867*cdf0e10cSrcweir m_pDocHolder->CloseDocument(); 868*cdf0e10cSrcweir m_pOwnStream = NULL; 869*cdf0e10cSrcweir m_pExtStream = NULL; 870*cdf0e10cSrcweir m_pMasterStorage = NULL; 871*cdf0e10cSrcweir } 872*cdf0e10cSrcweir } 873*cdf0e10cSrcweir 874*cdf0e10cSrcweir return hr; 875*cdf0e10cSrcweir } 876*cdf0e10cSrcweir 877*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::Save( LPCOLESTR pszFileName, BOOL fRemember ) 878*cdf0e10cSrcweir { 879*cdf0e10cSrcweir if ( !m_pDocHolder->GetDocument().is() || !m_xFactory.is() ) 880*cdf0e10cSrcweir return E_FAIL; 881*cdf0e10cSrcweir 882*cdf0e10cSrcweir HRESULT hr = E_FAIL; 883*cdf0e10cSrcweir 884*cdf0e10cSrcweir // TODO/LATER: currently there is no hands off state implemented 885*cdf0e10cSrcweir try 886*cdf0e10cSrcweir { 887*cdf0e10cSrcweir uno::Reference< frame::XStorable > xStorable( m_pDocHolder->GetDocument(), uno::UNO_QUERY_THROW ); 888*cdf0e10cSrcweir 889*cdf0e10cSrcweir if ( !pszFileName ) 890*cdf0e10cSrcweir xStorable->store(); 891*cdf0e10cSrcweir else 892*cdf0e10cSrcweir { 893*cdf0e10cSrcweir util::URL aURL; 894*cdf0e10cSrcweir aURL.Complete = ::rtl::OUString( reinterpret_cast<const sal_Unicode*>( pszFileName ) ); 895*cdf0e10cSrcweir 896*cdf0e10cSrcweir ::rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.util.URLTransformer" ) ); 897*cdf0e10cSrcweir uno::Reference< util::XURLTransformer > aTransformer( m_xFactory->createInstance( aServiceName ), 898*cdf0e10cSrcweir uno::UNO_QUERY_THROW ); 899*cdf0e10cSrcweir 900*cdf0e10cSrcweir if ( aTransformer->parseSmart( aURL, ::rtl::OUString() ) && aURL.Complete.getLength() ) 901*cdf0e10cSrcweir { 902*cdf0e10cSrcweir if ( fRemember ) 903*cdf0e10cSrcweir { 904*cdf0e10cSrcweir xStorable->storeAsURL( aURL.Complete, fillArgsForStoring_Impl( uno::Reference< io::XOutputStream >() ) ); 905*cdf0e10cSrcweir m_aFileName = aURL.Complete; 906*cdf0e10cSrcweir } 907*cdf0e10cSrcweir else 908*cdf0e10cSrcweir xStorable->storeToURL( aURL.Complete, fillArgsForStoring_Impl( uno::Reference< io::XOutputStream >() ) ); 909*cdf0e10cSrcweir } 910*cdf0e10cSrcweir } 911*cdf0e10cSrcweir 912*cdf0e10cSrcweir hr = S_OK; 913*cdf0e10cSrcweir } 914*cdf0e10cSrcweir catch( uno::Exception& ) 915*cdf0e10cSrcweir { 916*cdf0e10cSrcweir } 917*cdf0e10cSrcweir 918*cdf0e10cSrcweir return hr; 919*cdf0e10cSrcweir } 920*cdf0e10cSrcweir 921*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::SaveCompleted( LPCOLESTR pszFileName ) 922*cdf0e10cSrcweir { 923*cdf0e10cSrcweir // the different file name would mean error here 924*cdf0e10cSrcweir m_aFileName = ::rtl::OUString( reinterpret_cast<const sal_Unicode*>(pszFileName) ); 925*cdf0e10cSrcweir return S_OK; 926*cdf0e10cSrcweir } 927*cdf0e10cSrcweir 928*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::GetCurFile( LPOLESTR *ppszFileName ) 929*cdf0e10cSrcweir { 930*cdf0e10cSrcweir CComPtr<IMalloc> pMalloc; 931*cdf0e10cSrcweir 932*cdf0e10cSrcweir HRESULT hr = CoGetMalloc( 1, &pMalloc ); 933*cdf0e10cSrcweir if ( FAILED( hr ) || !pMalloc ) return E_FAIL; 934*cdf0e10cSrcweir 935*cdf0e10cSrcweir *ppszFileName = (LPOLESTR)( pMalloc->Alloc( sizeof( sal_Unicode ) * ( m_aFileName.getLength() + 1 ) ) ); 936*cdf0e10cSrcweir wcsncpy( *ppszFileName, reinterpret_cast<LPCWSTR>(m_aFileName.getStr()), m_aFileName.getLength() + 1 ); 937*cdf0e10cSrcweir 938*cdf0e10cSrcweir return m_aFileName.getLength() ? S_OK : S_FALSE; 939*cdf0e10cSrcweir } 940*cdf0e10cSrcweir 941*cdf0e10cSrcweir // =============================================== 942*cdf0e10cSrcweir 943*cdf0e10cSrcweir LockedEmbedDocument_Impl EmbeddedDocumentInstanceAccess_Impl::GetEmbedDocument() 944*cdf0e10cSrcweir { 945*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 946*cdf0e10cSrcweir return LockedEmbedDocument_Impl( m_pEmbedDocument ); 947*cdf0e10cSrcweir } 948*cdf0e10cSrcweir 949*cdf0e10cSrcweir void EmbeddedDocumentInstanceAccess_Impl::ClearEmbedDocument() 950*cdf0e10cSrcweir { 951*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 952*cdf0e10cSrcweir m_pEmbedDocument = NULL; 953*cdf0e10cSrcweir } 954*cdf0e10cSrcweir 955*cdf0e10cSrcweir // =============================================== 956*cdf0e10cSrcweir 957*cdf0e10cSrcweir LockedEmbedDocument_Impl::LockedEmbedDocument_Impl() 958*cdf0e10cSrcweir : m_pEmbedDocument( NULL ) 959*cdf0e10cSrcweir {} 960*cdf0e10cSrcweir 961*cdf0e10cSrcweir LockedEmbedDocument_Impl::LockedEmbedDocument_Impl( EmbedDocument_Impl* pEmbedDocument ) 962*cdf0e10cSrcweir : m_pEmbedDocument( pEmbedDocument ) 963*cdf0e10cSrcweir { 964*cdf0e10cSrcweir if ( m_pEmbedDocument ) 965*cdf0e10cSrcweir m_pEmbedDocument->AddRef(); 966*cdf0e10cSrcweir } 967*cdf0e10cSrcweir 968*cdf0e10cSrcweir LockedEmbedDocument_Impl::LockedEmbedDocument_Impl( const LockedEmbedDocument_Impl& aDocLock ) 969*cdf0e10cSrcweir : m_pEmbedDocument( aDocLock.m_pEmbedDocument ) 970*cdf0e10cSrcweir { 971*cdf0e10cSrcweir if ( m_pEmbedDocument ) 972*cdf0e10cSrcweir m_pEmbedDocument->AddRef(); 973*cdf0e10cSrcweir } 974*cdf0e10cSrcweir 975*cdf0e10cSrcweir LockedEmbedDocument_Impl& LockedEmbedDocument_Impl::operator=( const LockedEmbedDocument_Impl& aDocLock ) 976*cdf0e10cSrcweir { 977*cdf0e10cSrcweir if ( m_pEmbedDocument ) 978*cdf0e10cSrcweir m_pEmbedDocument->Release(); 979*cdf0e10cSrcweir 980*cdf0e10cSrcweir m_pEmbedDocument = aDocLock.m_pEmbedDocument; 981*cdf0e10cSrcweir if ( m_pEmbedDocument ) 982*cdf0e10cSrcweir m_pEmbedDocument->AddRef(); 983*cdf0e10cSrcweir 984*cdf0e10cSrcweir return *this; 985*cdf0e10cSrcweir } 986*cdf0e10cSrcweir 987*cdf0e10cSrcweir LockedEmbedDocument_Impl::~LockedEmbedDocument_Impl() 988*cdf0e10cSrcweir { 989*cdf0e10cSrcweir if ( m_pEmbedDocument ) 990*cdf0e10cSrcweir m_pEmbedDocument->Release(); 991*cdf0e10cSrcweir } 992*cdf0e10cSrcweir 993*cdf0e10cSrcweir void LockedEmbedDocument_Impl::ExecuteMethod( sal_Int16 nId ) 994*cdf0e10cSrcweir { 995*cdf0e10cSrcweir if ( m_pEmbedDocument ) 996*cdf0e10cSrcweir { 997*cdf0e10cSrcweir if ( nId == OLESERV_SAVEOBJECT ) 998*cdf0e10cSrcweir m_pEmbedDocument->SaveObject(); 999*cdf0e10cSrcweir else if ( nId == OLESERV_CLOSE ) 1000*cdf0e10cSrcweir m_pEmbedDocument->Close( 0 ); 1001*cdf0e10cSrcweir else if ( nId == OLESERV_NOTIFY ) 1002*cdf0e10cSrcweir m_pEmbedDocument->notify(); 1003*cdf0e10cSrcweir else if ( nId == OLESERV_NOTIFYCLOSING ) 1004*cdf0e10cSrcweir m_pEmbedDocument->OLENotifyClosing(); 1005*cdf0e10cSrcweir else if ( nId == OLESERV_SHOWOBJECT ) 1006*cdf0e10cSrcweir m_pEmbedDocument->ShowObject(); 1007*cdf0e10cSrcweir else if ( nId == OLESERV_DEACTIVATE ) 1008*cdf0e10cSrcweir m_pEmbedDocument->Deactivate(); 1009*cdf0e10cSrcweir } 1010*cdf0e10cSrcweir } 1011*cdf0e10cSrcweir 1012*cdf0e10cSrcweir // Fix strange warnings about some 1013*cdf0e10cSrcweir // ATL::CAxHostWindow::QueryInterface|AddRef|Releae functions. 1014*cdf0e10cSrcweir // warning C4505: 'xxx' : unreferenced local function has been removed 1015*cdf0e10cSrcweir #if defined(_MSC_VER) 1016*cdf0e10cSrcweir #pragma warning(disable: 4505) 1017*cdf0e10cSrcweir #endif 1018*cdf0e10cSrcweir 1019