Files
libreoffice/embeddedobj/source/msole/olemisc.cxx

504 lines
17 KiB
C++
Raw Normal View History

2003-11-13 16:01:50 +00:00
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2003-11-13 16:01:50 +00:00
*
* Copyright 2008 by Sun Microsystems, Inc.
2003-11-13 16:01:50 +00:00
*
* OpenOffice.org - a multi-platform office productivity suite
2003-11-13 16:01:50 +00:00
*
* $RCSfile: olemisc.cxx,v $
* $Revision: 1.26 $
2003-11-13 16:01:50 +00:00
*
* This file is part of OpenOffice.org.
2003-11-13 16:01:50 +00:00
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
2003-11-13 16:01:50 +00:00
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
2003-11-13 16:01:50 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
2003-11-13 16:01:50 +00:00
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_embeddedobj.hxx"
2003-11-13 16:01:50 +00:00
#include <com/sun/star/embed/EmbedUpdateModes.hpp>
#include <com/sun/star/embed/EmbedStates.hpp>
#include <com/sun/star/lang/XComponent.hpp>
2003-11-13 16:01:50 +00:00
#include <com/sun/star/lang/DisposedException.hpp>
#include <cppuhelper/interfacecontainer.h>
#include <oleembobj.hxx>
#include <olecomponent.hxx>
#include "ownview.hxx"
2003-11-13 16:01:50 +00:00
using namespace ::com::sun::star;
sal_Bool KillFile_Impl( const ::rtl::OUString& aURL, const uno::Reference< lang::XMultiServiceFactory >& xFactory );
2003-11-13 16:01:50 +00:00
//------------------------------------------------------
OleEmbeddedObject::OleEmbeddedObject( const uno::Reference< lang::XMultiServiceFactory >& xFactory,
const uno::Sequence< sal_Int8 >& aClassID,
const ::rtl::OUString& aClassName )
: m_pOleComponent( NULL )
, m_pInterfaceContainer( NULL )
, m_bReadOnly( sal_False )
, m_bDisposed( sal_False )
, m_nObjectState( -1 )
, m_nTargetState( -1 )
, m_nUpdateMode ( embed::EmbedUpdateModes::ALWAYS_UPDATE )
2003-11-13 16:01:50 +00:00
, m_xFactory( xFactory )
, m_aClassID( aClassID )
, m_aClassName( aClassName )
, m_bWaitSaveCompleted( sal_False )
, m_bNewVisReplInStream( sal_True )
, m_bStoreLoaded( sal_False )
, m_bVisReplInitialized( sal_False )
, m_bVisReplInStream( sal_False )
, m_bStoreVisRepl( sal_False )
2003-11-13 16:01:50 +00:00
, m_bIsLink( sal_False )
, m_bHasCachedSize( sal_False )
, m_nCachedAspect( 0 )
, m_bHasSizeToSet( sal_False )
, m_nAspectToSet( 0 )
, m_bGotStatus( sal_False )
, m_nStatus( 0 )
, m_nStatusAspect( 0 )
, m_pOwnView( NULL )
, m_bFromClipboard( sal_False )
2003-11-13 16:01:50 +00:00
{
}
//------------------------------------------------------
// In case of loading from persistent entry the classID of the object
// will be retrieved from the entry, during construction it is unknown
OleEmbeddedObject::OleEmbeddedObject( const uno::Reference< lang::XMultiServiceFactory >& xFactory, sal_Bool bLink )
2003-11-13 16:01:50 +00:00
: m_pOleComponent( NULL )
, m_pInterfaceContainer( NULL )
, m_bReadOnly( sal_False )
, m_bDisposed( sal_False )
, m_nObjectState( -1 )
, m_nTargetState( -1 )
, m_nUpdateMode( embed::EmbedUpdateModes::ALWAYS_UPDATE )
2003-11-13 16:01:50 +00:00
, m_xFactory( xFactory )
, m_bWaitSaveCompleted( sal_False )
, m_bNewVisReplInStream( sal_True )
, m_bStoreLoaded( sal_False )
, m_bVisReplInitialized( sal_False )
, m_bVisReplInStream( sal_False )
, m_bStoreVisRepl( sal_False )
, m_bIsLink( bLink )
, m_bHasCachedSize( sal_False )
, m_nCachedAspect( 0 )
, m_bHasSizeToSet( sal_False )
, m_nAspectToSet( 0 )
, m_bGotStatus( sal_False )
, m_nStatus( 0 )
, m_nStatusAspect( 0 )
, m_pOwnView( NULL )
, m_bFromClipboard( sal_False )
2003-11-13 16:01:50 +00:00
{
}
//------------------------------------------------------
// this constructor let object be initialized from clipboard
OleEmbeddedObject::OleEmbeddedObject( const uno::Reference< lang::XMultiServiceFactory >& xFactory )
: m_pOleComponent( NULL )
, m_pInterfaceContainer( NULL )
, m_bReadOnly( sal_False )
, m_bDisposed( sal_False )
, m_nObjectState( -1 )
, m_nTargetState( -1 )
, m_nUpdateMode( embed::EmbedUpdateModes::ALWAYS_UPDATE )
, m_xFactory( xFactory )
, m_bWaitSaveCompleted( sal_False )
, m_bNewVisReplInStream( sal_True )
, m_bStoreLoaded( sal_False )
, m_bVisReplInitialized( sal_False )
, m_bVisReplInStream( sal_False )
, m_bStoreVisRepl( sal_False )
, m_bIsLink( sal_False )
, m_bHasCachedSize( sal_False )
, m_nCachedAspect( 0 )
, m_bHasSizeToSet( sal_False )
, m_nAspectToSet( 0 )
, m_bGotStatus( sal_False )
, m_nStatus( 0 )
, m_nStatusAspect( 0 )
, m_pOwnView( NULL )
, m_bFromClipboard( sal_True )
{
}
2003-11-13 16:01:50 +00:00
//------------------------------------------------------
OleEmbeddedObject::~OleEmbeddedObject()
{
OSL_ENSURE( !m_pInterfaceContainer && !m_pOleComponent && !m_xObjectStream.is(),
"The object is not closed! DISASTER is possible!" );
2003-11-17 15:19:26 +00:00
if ( m_pOleComponent || m_pInterfaceContainer || m_xObjectStream.is() )
2003-11-17 15:19:26 +00:00
{
// the component must be cleaned during closing
m_refCount++; // to avoid crash
try {
Dispose();
} catch( uno::Exception& ) {}
}
if ( m_aTempURL.getLength() )
KillFile_Impl( m_aTempURL, m_xFactory );
2003-11-17 15:19:26 +00:00
}
//------------------------------------------------------
void OleEmbeddedObject::MakeEventListenerNotification_Impl( const ::rtl::OUString& aEventName )
{
if ( m_pInterfaceContainer )
{
::cppu::OInterfaceContainerHelper* pContainer =
m_pInterfaceContainer->getContainer(
::getCppuType( ( const uno::Reference< document::XEventListener >*) NULL ) );
if ( pContainer != NULL )
{
document::EventObject aEvent( static_cast< ::cppu::OWeakObject* >( this ), aEventName );
::cppu::OInterfaceIteratorHelper pIterator(*pContainer);
while (pIterator.hasMoreElements())
{
try
{
((document::XEventListener*)pIterator.next())->notifyEvent( aEvent );
}
catch( uno::RuntimeException& )
{
}
}
}
}
}
//----------------------------------------------
void OleEmbeddedObject::StateChangeNotification_Impl( sal_Bool bBeforeChange, sal_Int32 nOldState, sal_Int32 nNewState )
{
if ( m_pInterfaceContainer )
{
::cppu::OInterfaceContainerHelper* pContainer = m_pInterfaceContainer->getContainer(
::getCppuType( ( const uno::Reference< embed::XStateChangeListener >*) NULL ) );
if ( pContainer != NULL )
{
lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >( this ) );
::cppu::OInterfaceIteratorHelper pIterator(*pContainer);
while (pIterator.hasMoreElements())
{
if ( bBeforeChange )
{
try
{
((embed::XStateChangeListener*)pIterator.next())->changingState( aSource, nOldState, nNewState );
}
catch( uno::Exception& )
{
// even if the listener complains ignore it for now
}
}
else
{
try
{
((embed::XStateChangeListener*)pIterator.next())->stateChanged( aSource, nOldState, nNewState );
}
catch( uno::Exception& )
{
// if anything happened it is problem of listener, ignore it
}
}
}
}
}
}
2003-11-17 15:19:26 +00:00
//------------------------------------------------------
2003-11-26 09:27:48 +00:00
void OleEmbeddedObject::GetRidOfComponent()
2003-11-17 15:19:26 +00:00
{
2003-12-15 14:37:44 +00:00
#ifdef WNT
2003-11-13 16:01:50 +00:00
if ( m_pOleComponent )
{
if ( m_nObjectState != -1 && m_nObjectState != embed::EmbedStates::LOADED )
2003-11-17 15:19:26 +00:00
SaveObject_Impl();
2003-11-13 16:01:50 +00:00
2003-11-17 15:19:26 +00:00
m_pOleComponent->removeCloseListener( m_xClosePreventer );
2003-12-15 14:37:44 +00:00
try
{
m_pOleComponent->close( sal_False );
}
catch( uno::Exception& )
{
// TODO: there should be a special listener to wait for component closing
// and to notify object, may be object itself can be such a listener
m_pOleComponent->addCloseListener( m_xClosePreventer );
throw;
}
2003-11-13 16:01:50 +00:00
m_pOleComponent->disconnectEmbeddedObject();
m_pOleComponent->release();
m_pOleComponent = NULL;
}
2003-12-15 14:37:44 +00:00
#endif
2003-11-26 09:27:48 +00:00
}
//------------------------------------------------------
void OleEmbeddedObject::Dispose()
{
if ( m_pInterfaceContainer )
{
lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >( this ) );
m_pInterfaceContainer->disposeAndClear( aSource );
delete m_pInterfaceContainer;
m_pInterfaceContainer = NULL;
}
if ( m_pOwnView )
{
m_pOwnView->Close();
m_pOwnView->release();
m_pOwnView = NULL;
}
2003-11-26 09:27:48 +00:00
if ( m_pOleComponent )
2003-12-15 14:59:22 +00:00
try {
GetRidOfComponent();
} catch( uno::Exception& )
{
m_bDisposed = true;
throw; // TODO: there should be a special listener that will close object when
// component is finally closed
}
2003-11-17 15:19:26 +00:00
if ( m_xObjectStream.is() )
{
uno::Reference< lang::XComponent > xComp( m_xObjectStream, uno::UNO_QUERY );
OSL_ENSURE( xComp.is(), "Storage stream doesn't support XComponent!\n" );
if ( xComp.is() )
{
try {
xComp->dispose();
} catch( uno::Exception& ) {}
}
m_xObjectStream = uno::Reference< io::XStream >();
}
m_xParentStorage = uno::Reference< embed::XStorage >();
2003-12-15 14:59:22 +00:00
m_bDisposed = true;
2003-11-13 16:01:50 +00:00
}
//------------------------------------------------------
uno::Sequence< sal_Int8 > SAL_CALL OleEmbeddedObject::getClassID()
throw ( uno::RuntimeException )
{
::osl::MutexGuard aGuard( m_aMutex );
if ( m_bDisposed )
throw lang::DisposedException(); // TODO
return m_aClassID;
}
//------------------------------------------------------
::rtl::OUString SAL_CALL OleEmbeddedObject::getClassName()
throw ( uno::RuntimeException )
{
::osl::MutexGuard aGuard( m_aMutex );
if ( m_bDisposed )
throw lang::DisposedException(); // TODO
return m_aClassName;
}
//------------------------------------------------------
void SAL_CALL OleEmbeddedObject::setClassInfo(
const uno::Sequence< sal_Int8 >& /*aClassID*/, const ::rtl::OUString& /*aClassName*/ )
2003-11-13 16:01:50 +00:00
throw ( lang::NoSupportException,
uno::RuntimeException )
{
// the object class info can not be changed explicitly
throw lang::NoSupportException(); //TODO:
}
//------------------------------------------------------
2003-11-18 08:03:56 +00:00
uno::Reference< util::XCloseable > SAL_CALL OleEmbeddedObject::getComponent()
2003-11-13 16:01:50 +00:00
throw ( uno::RuntimeException )
{
::osl::MutexGuard aGuard( m_aMutex );
if ( m_bDisposed )
throw lang::DisposedException(); // TODO
if ( m_nObjectState == -1 ) // || m_nObjectState == embed::EmbedStates::LOADED )
2003-11-13 16:01:50 +00:00
{
2003-12-15 14:37:44 +00:00
// the object is still not running
throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The object is not loaded!\n" ),
uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
2003-11-13 16:01:50 +00:00
}
// if ( m_bWaitSaveCompleted )
// throw embed::WrongStateException(
// ::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
// uno::Reference< uno::XInterface >( reinterpret_cast< ::cppu::OWeakObject* >(this) ) );
2003-11-13 16:01:50 +00:00
2003-11-18 08:03:56 +00:00
if ( !m_pOleComponent )
{
// TODO/LATER: Is it correct???
return uno::Reference< util::XCloseable >();
// throw uno::RuntimeException(); // TODO
}
2003-11-18 08:03:56 +00:00
return uno::Reference< util::XCloseable >( static_cast< ::cppu::OWeakObject* >( m_pOleComponent ), uno::UNO_QUERY );
2003-11-13 16:01:50 +00:00
}
//----------------------------------------------
void SAL_CALL OleEmbeddedObject::addStateChangeListener( const uno::Reference< embed::XStateChangeListener >& xListener )
throw ( uno::RuntimeException )
{
::osl::MutexGuard aGuard( m_aMutex );
if ( m_bDisposed )
throw lang::DisposedException(); // TODO
if ( !m_pInterfaceContainer )
m_pInterfaceContainer = new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex );
m_pInterfaceContainer->addInterface( ::getCppuType( (const uno::Reference< embed::XStateChangeListener >*)0 ),
xListener );
}
//----------------------------------------------
void SAL_CALL OleEmbeddedObject::removeStateChangeListener(
const uno::Reference< embed::XStateChangeListener >& xListener )
throw (uno::RuntimeException)
{
::osl::MutexGuard aGuard( m_aMutex );
if ( m_pInterfaceContainer )
m_pInterfaceContainer->removeInterface( ::getCppuType( (const uno::Reference< embed::XStateChangeListener >*)0 ),
xListener );
}
2003-11-17 15:19:26 +00:00
//----------------------------------------------
void SAL_CALL OleEmbeddedObject::close( sal_Bool bDeliverOwnership )
throw ( util::CloseVetoException,
uno::RuntimeException )
2003-11-13 16:01:50 +00:00
{
::osl::MutexGuard aGuard( m_aMutex );
if ( m_bDisposed )
throw lang::DisposedException(); // TODO
2003-11-17 15:19:26 +00:00
uno::Reference< uno::XInterface > xSelfHold( static_cast< ::cppu::OWeakObject* >( this ) );
lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >( this ) );
2003-11-13 16:01:50 +00:00
2003-11-17 15:19:26 +00:00
if ( m_pInterfaceContainer )
2003-11-13 16:01:50 +00:00
{
2003-11-17 15:19:26 +00:00
::cppu::OInterfaceContainerHelper* pContainer =
m_pInterfaceContainer->getContainer( ::getCppuType( ( const uno::Reference< util::XCloseListener >*) NULL ) );
if ( pContainer != NULL )
2003-11-13 16:01:50 +00:00
{
2003-11-17 15:19:26 +00:00
::cppu::OInterfaceIteratorHelper pIterator(*pContainer);
while (pIterator.hasMoreElements())
{
try
{
((util::XCloseListener*)pIterator.next())->queryClosing( aSource, bDeliverOwnership );
}
catch( uno::RuntimeException& )
{
pIterator.remove();
}
}
2003-11-13 16:01:50 +00:00
}
2003-11-17 15:19:26 +00:00
pContainer = m_pInterfaceContainer->getContainer(
::getCppuType( ( const uno::Reference< util::XCloseListener >*) NULL ) );
if ( pContainer != NULL )
{
::cppu::OInterfaceIteratorHelper pCloseIterator(*pContainer);
while (pCloseIterator.hasMoreElements())
{
try
{
((util::XCloseListener*)pCloseIterator.next())->notifyClosing( aSource );
}
catch( uno::RuntimeException& )
{
pCloseIterator.remove();
}
}
}
2003-11-13 16:01:50 +00:00
}
2003-11-17 15:19:26 +00:00
Dispose();
2003-11-13 16:01:50 +00:00
}
2003-11-17 15:19:26 +00:00
//----------------------------------------------
void SAL_CALL OleEmbeddedObject::addCloseListener( const uno::Reference< util::XCloseListener >& xListener )
throw ( uno::RuntimeException )
2003-11-13 16:01:50 +00:00
{
::osl::MutexGuard aGuard( m_aMutex );
if ( m_bDisposed )
throw lang::DisposedException(); // TODO
if ( !m_pInterfaceContainer )
m_pInterfaceContainer = new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex );
2003-11-17 15:19:26 +00:00
m_pInterfaceContainer->addInterface( ::getCppuType( (const uno::Reference< util::XCloseListener >*)0 ), xListener );
2003-11-13 16:01:50 +00:00
}
2003-11-17 15:19:26 +00:00
//----------------------------------------------
void SAL_CALL OleEmbeddedObject::removeCloseListener( const uno::Reference< util::XCloseListener >& xListener )
throw (uno::RuntimeException)
2003-11-13 16:01:50 +00:00
{
::osl::MutexGuard aGuard( m_aMutex );
if ( m_bDisposed )
throw lang::DisposedException(); // TODO
if ( m_pInterfaceContainer )
2003-11-17 15:19:26 +00:00
m_pInterfaceContainer->removeInterface( ::getCppuType( (const uno::Reference< util::XCloseListener >*)0 ),
2003-11-13 16:01:50 +00:00
xListener );
}
//------------------------------------------------------
void SAL_CALL OleEmbeddedObject::addEventListener( const uno::Reference< document::XEventListener >& xListener )
throw ( uno::RuntimeException )
{
::osl::MutexGuard aGuard( m_aMutex );
if ( m_bDisposed )
throw lang::DisposedException(); // TODO
if ( !m_pInterfaceContainer )
m_pInterfaceContainer = new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex );
m_pInterfaceContainer->addInterface( ::getCppuType( (const uno::Reference< document::XEventListener >*)0 ), xListener );
}
//------------------------------------------------------
void SAL_CALL OleEmbeddedObject::removeEventListener(
const uno::Reference< document::XEventListener >& xListener )
throw ( uno::RuntimeException )
{
::osl::MutexGuard aGuard( m_aMutex );
if ( m_bDisposed )
throw lang::DisposedException(); // TODO
if ( m_pInterfaceContainer )
m_pInterfaceContainer->removeInterface( ::getCppuType( (const uno::Reference< document::XEventListener >*)0 ),
xListener );
}