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 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_unotools.hxx" 30*cdf0e10cSrcweir #include <unotools/sharedunocomponent.hxx> 31*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp> 32*cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp> 33*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx> 34*cdf0e10cSrcweir #include <tools/debug.hxx> 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir //............................................................................ 37*cdf0e10cSrcweir namespace utl 38*cdf0e10cSrcweir { 39*cdf0e10cSrcweir //............................................................................ 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir using ::com::sun::star::uno::XInterface; 42*cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 43*cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 44*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 45*cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 46*cdf0e10cSrcweir using ::com::sun::star::lang::XComponent; 47*cdf0e10cSrcweir using ::com::sun::star::lang::EventObject; 48*cdf0e10cSrcweir using ::com::sun::star::util::XCloseable; 49*cdf0e10cSrcweir using ::com::sun::star::util::XCloseListener; 50*cdf0e10cSrcweir using ::com::sun::star::util::CloseVetoException; 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir //======================================================================== 53*cdf0e10cSrcweir //= DisposableComponent 54*cdf0e10cSrcweir //======================================================================== 55*cdf0e10cSrcweir //------------------------------------------------------------------------ 56*cdf0e10cSrcweir DisposableComponent::DisposableComponent( const Reference< XInterface >& _rxComponent ) 57*cdf0e10cSrcweir :m_xComponent( _rxComponent, UNO_QUERY ) 58*cdf0e10cSrcweir { 59*cdf0e10cSrcweir DBG_ASSERT( m_xComponent.is() || !_rxComponent.is(), "DisposableComponent::DisposableComponent: should be an XComponent!" ); 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir //------------------------------------------------------------------------ 63*cdf0e10cSrcweir DisposableComponent::~DisposableComponent() 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir if ( m_xComponent.is() ) 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir try 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir m_xComponent->dispose(); 70*cdf0e10cSrcweir } 71*cdf0e10cSrcweir catch( const Exception& ) 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir OSL_ENSURE( sal_False, "DisposableComponent::~DisposableComponent: caught an exception!" ); 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir m_xComponent.clear(); 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir } 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir //======================================================================== 80*cdf0e10cSrcweir //= CloseableComponentImpl 81*cdf0e10cSrcweir //======================================================================== 82*cdf0e10cSrcweir DBG_NAME( CloseableComponentImpl ) 83*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1 < XCloseListener 84*cdf0e10cSrcweir > CloseableComponentImpl_Base; 85*cdf0e10cSrcweir class CloseableComponentImpl : public CloseableComponentImpl_Base 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir private: 88*cdf0e10cSrcweir Reference< XCloseable > m_xCloseable; 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir public: 91*cdf0e10cSrcweir CloseableComponentImpl( const Reference< XInterface >& _rxComponent ); 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir /** closes the component 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir @nofail 96*cdf0e10cSrcweir */ 97*cdf0e10cSrcweir void nf_closeComponent(); 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir protected: 100*cdf0e10cSrcweir virtual ~CloseableComponentImpl(); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir // XCloseListener overridables 103*cdf0e10cSrcweir virtual void SAL_CALL queryClosing( const EventObject& Source, ::sal_Bool GetsOwnership ) throw (CloseVetoException, RuntimeException); 104*cdf0e10cSrcweir virtual void SAL_CALL notifyClosing( const EventObject& Source ) throw (RuntimeException); 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir // XEventListener overridables 107*cdf0e10cSrcweir virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException); 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir private: 110*cdf0e10cSrcweir /** starts or stops being a CloseListener at the component 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir Only to be called upon construction of the instance, or when the component 113*cdf0e10cSrcweir is to be closed. 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir @nofail 116*cdf0e10cSrcweir */ 117*cdf0e10cSrcweir void impl_nf_switchListening( bool _bListen ); 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir private: 121*cdf0e10cSrcweir CloseableComponentImpl(); // never implemented 122*cdf0e10cSrcweir CloseableComponentImpl( const CloseableComponentImpl& ); // never implemented 123*cdf0e10cSrcweir CloseableComponentImpl& operator=( const CloseableComponentImpl& ); // never implemented 124*cdf0e10cSrcweir }; 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir //------------------------------------------------------------------------ 127*cdf0e10cSrcweir CloseableComponentImpl::CloseableComponentImpl( const Reference< XInterface >& _rxComponent ) 128*cdf0e10cSrcweir :m_xCloseable( _rxComponent, UNO_QUERY ) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir DBG_CTOR( CloseableComponentImpl, NULL ); 131*cdf0e10cSrcweir DBG_ASSERT( m_xCloseable.is() || !_rxComponent.is(), "CloseableComponentImpl::CloseableComponentImpl: component is not an XCloseable!" ); 132*cdf0e10cSrcweir impl_nf_switchListening( true ); 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir //------------------------------------------------------------------------ 135*cdf0e10cSrcweir CloseableComponentImpl::~CloseableComponentImpl() 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir nf_closeComponent(); 138*cdf0e10cSrcweir DBG_DTOR( CloseableComponentImpl, NULL ); 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir //------------------------------------------------------------------------ 142*cdf0e10cSrcweir void CloseableComponentImpl::nf_closeComponent() 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir if ( !m_xCloseable.is() ) 145*cdf0e10cSrcweir // nothing to do 146*cdf0e10cSrcweir return; 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir // stop listening 149*cdf0e10cSrcweir impl_nf_switchListening( false ); 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir // close 152*cdf0e10cSrcweir try 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir m_xCloseable->close( sal_True ); 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir catch( const CloseVetoException& ) { /* fine */ } 157*cdf0e10cSrcweir catch( const Exception& ) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir OSL_ENSURE( sal_False, "CloseableComponentImpl::nf_closeComponent: caught an unexpected exception!" ); 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir // reset 163*cdf0e10cSrcweir m_xCloseable.clear(); 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir //------------------------------------------------------------------------ 167*cdf0e10cSrcweir void CloseableComponentImpl::impl_nf_switchListening( bool _bListen ) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir if ( !m_xCloseable.is() ) 170*cdf0e10cSrcweir return; 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir try 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir if ( _bListen ) 175*cdf0e10cSrcweir m_xCloseable->addCloseListener( this ); 176*cdf0e10cSrcweir else 177*cdf0e10cSrcweir m_xCloseable->removeCloseListener( this ); 178*cdf0e10cSrcweir } 179*cdf0e10cSrcweir catch( const Exception& ) 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir OSL_ENSURE( sal_False, "CloseableComponentImpl::impl_nf_switchListening: caught an exception!" ); 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir //-------------------------------------------------------------------- 186*cdf0e10cSrcweir void SAL_CALL CloseableComponentImpl::queryClosing( const EventObject& 187*cdf0e10cSrcweir #ifdef DBG_UTIL 188*cdf0e10cSrcweir Source 189*cdf0e10cSrcweir #endif 190*cdf0e10cSrcweir , ::sal_Bool /*GetsOwnership*/ ) throw (CloseVetoException, RuntimeException) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir // as long as we live, somebody wants to keep the object alive. So, veto the 193*cdf0e10cSrcweir // closing 194*cdf0e10cSrcweir DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::queryClosing: where did this come from?" ); 195*cdf0e10cSrcweir throw CloseVetoException(); 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir //-------------------------------------------------------------------- 199*cdf0e10cSrcweir void SAL_CALL CloseableComponentImpl::notifyClosing( const EventObject& 200*cdf0e10cSrcweir #ifdef DBG_UTIL 201*cdf0e10cSrcweir Source 202*cdf0e10cSrcweir #endif 203*cdf0e10cSrcweir ) throw (RuntimeException) 204*cdf0e10cSrcweir { 205*cdf0e10cSrcweir DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::notifyClosing: where did this come from?" ); 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir // this should be unreachable: As long as we're a CloseListener, we veto the closing. If we're going 208*cdf0e10cSrcweir // to close the component ourself, then we revoke ourself as listener *before* the close call. So, 209*cdf0e10cSrcweir // if this here fires, something went definately wrong. 210*cdf0e10cSrcweir DBG_ERROR( "CloseableComponentImpl::notifyClosing: unreachable!" ); 211*cdf0e10cSrcweir } 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir //-------------------------------------------------------------------- 214*cdf0e10cSrcweir void SAL_CALL CloseableComponentImpl::disposing( const EventObject& 215*cdf0e10cSrcweir #ifdef DBG_UTIL 216*cdf0e10cSrcweir Source 217*cdf0e10cSrcweir #endif 218*cdf0e10cSrcweir ) throw (RuntimeException) 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::disposing: where did this come from?" ); 221*cdf0e10cSrcweir DBG_ERROR( "CloseableComponentImpl::disposing: unreachable!" ); 222*cdf0e10cSrcweir // same reasoning for this assertion as in ->notifyClosing 223*cdf0e10cSrcweir } 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir //======================================================================== 226*cdf0e10cSrcweir //= CloseableComponentImpl 227*cdf0e10cSrcweir //======================================================================== 228*cdf0e10cSrcweir DBG_NAME( CloseableComponent ) 229*cdf0e10cSrcweir //------------------------------------------------------------------------ 230*cdf0e10cSrcweir CloseableComponent::CloseableComponent( const Reference< XInterface >& _rxComponent ) 231*cdf0e10cSrcweir :m_pImpl( new CloseableComponentImpl( _rxComponent ) ) 232*cdf0e10cSrcweir { 233*cdf0e10cSrcweir DBG_CTOR( CloseableComponent, NULL ); 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir //------------------------------------------------------------------------ 237*cdf0e10cSrcweir CloseableComponent::~CloseableComponent() 238*cdf0e10cSrcweir { 239*cdf0e10cSrcweir // close the component, deliver ownership to anybody who wants to veto the close 240*cdf0e10cSrcweir m_pImpl->nf_closeComponent(); 241*cdf0e10cSrcweir DBG_DTOR( CloseableComponent, NULL ); 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir //............................................................................ 245*cdf0e10cSrcweir } // namespace utl 246*cdf0e10cSrcweir //............................................................................ 247