update dev300-m58
[ooovba.git] / unotools / source / misc / sharedunocomponent.cxx
blob9a167709555614528cd044aca02f89089fcb1651
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sharedunocomponent.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_unotools.hxx"
33 #include <unotools/sharedunocomponent.hxx>
34 #include <com/sun/star/lang/XComponent.hpp>
35 #include <com/sun/star/util/XCloseable.hpp>
36 #include <cppuhelper/implbase1.hxx>
37 #include <tools/debug.hxx>
39 //............................................................................
40 namespace utl
42 //............................................................................
44 using ::com::sun::star::uno::XInterface;
45 using ::com::sun::star::uno::Reference;
46 using ::com::sun::star::uno::Exception;
47 using ::com::sun::star::uno::UNO_QUERY;
48 using ::com::sun::star::uno::RuntimeException;
49 using ::com::sun::star::lang::XComponent;
50 using ::com::sun::star::lang::EventObject;
51 using ::com::sun::star::util::XCloseable;
52 using ::com::sun::star::util::XCloseListener;
53 using ::com::sun::star::util::CloseVetoException;
55 //========================================================================
56 //= DisposableComponent
57 //========================================================================
58 //------------------------------------------------------------------------
59 DisposableComponent::DisposableComponent( const Reference< XInterface >& _rxComponent )
60 :m_xComponent( _rxComponent, UNO_QUERY )
62 DBG_ASSERT( m_xComponent.is() || !_rxComponent.is(), "DisposableComponent::DisposableComponent: should be an XComponent!" );
65 //------------------------------------------------------------------------
66 DisposableComponent::~DisposableComponent()
68 if ( m_xComponent.is() )
70 try
72 m_xComponent->dispose();
74 catch( const Exception& )
76 OSL_ENSURE( sal_False, "DisposableComponent::~DisposableComponent: caught an exception!" );
78 m_xComponent.clear();
82 //========================================================================
83 //= CloseableComponentImpl
84 //========================================================================
85 DBG_NAME( CloseableComponentImpl )
86 typedef ::cppu::WeakImplHelper1 < XCloseListener
87 > CloseableComponentImpl_Base;
88 class CloseableComponentImpl : public CloseableComponentImpl_Base
90 private:
91 Reference< XCloseable > m_xCloseable;
93 public:
94 CloseableComponentImpl( const Reference< XInterface >& _rxComponent );
96 /** closes the component
98 @nofail
100 void nf_closeComponent();
102 protected:
103 virtual ~CloseableComponentImpl();
105 // XCloseListener overridables
106 virtual void SAL_CALL queryClosing( const EventObject& Source, ::sal_Bool GetsOwnership ) throw (CloseVetoException, RuntimeException);
107 virtual void SAL_CALL notifyClosing( const EventObject& Source ) throw (RuntimeException);
109 // XEventListener overridables
110 virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
112 private:
113 /** starts or stops being a CloseListener at the component
115 Only to be called upon construction of the instance, or when the component
116 is to be closed.
118 @nofail
120 void impl_nf_switchListening( bool _bListen );
123 private:
124 CloseableComponentImpl(); // never implemented
125 CloseableComponentImpl( const CloseableComponentImpl& ); // never implemented
126 CloseableComponentImpl& operator=( const CloseableComponentImpl& ); // never implemented
129 //------------------------------------------------------------------------
130 CloseableComponentImpl::CloseableComponentImpl( const Reference< XInterface >& _rxComponent )
131 :m_xCloseable( _rxComponent, UNO_QUERY )
133 DBG_CTOR( CloseableComponentImpl, NULL );
134 DBG_ASSERT( m_xCloseable.is() || !_rxComponent.is(), "CloseableComponentImpl::CloseableComponentImpl: component is not an XCloseable!" );
135 impl_nf_switchListening( true );
137 //------------------------------------------------------------------------
138 CloseableComponentImpl::~CloseableComponentImpl()
140 nf_closeComponent();
141 DBG_DTOR( CloseableComponentImpl, NULL );
144 //------------------------------------------------------------------------
145 void CloseableComponentImpl::nf_closeComponent()
147 if ( !m_xCloseable.is() )
148 // nothing to do
149 return;
151 // stop listening
152 impl_nf_switchListening( false );
154 // close
157 m_xCloseable->close( sal_True );
159 catch( const CloseVetoException& ) { /* fine */ }
160 catch( const Exception& )
162 OSL_ENSURE( sal_False, "CloseableComponentImpl::nf_closeComponent: caught an unexpected exception!" );
165 // reset
166 m_xCloseable.clear();
169 //------------------------------------------------------------------------
170 void CloseableComponentImpl::impl_nf_switchListening( bool _bListen )
172 if ( !m_xCloseable.is() )
173 return;
177 if ( _bListen )
178 m_xCloseable->addCloseListener( this );
179 else
180 m_xCloseable->removeCloseListener( this );
182 catch( const Exception& )
184 OSL_ENSURE( sal_False, "CloseableComponentImpl::impl_nf_switchListening: caught an exception!" );
188 //--------------------------------------------------------------------
189 void SAL_CALL CloseableComponentImpl::queryClosing( const EventObject&
190 #ifdef DBG_UTIL
191 Source
192 #endif
193 , ::sal_Bool /*GetsOwnership*/ ) throw (CloseVetoException, RuntimeException)
195 // as long as we live, somebody wants to keep the object alive. So, veto the
196 // closing
197 DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::queryClosing: where did this come from?" );
198 throw CloseVetoException();
201 //--------------------------------------------------------------------
202 void SAL_CALL CloseableComponentImpl::notifyClosing( const EventObject&
203 #ifdef DBG_UTIL
204 Source
205 #endif
206 ) throw (RuntimeException)
208 DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::notifyClosing: where did this come from?" );
210 // this should be unreachable: As long as we're a CloseListener, we veto the closing. If we're going
211 // to close the component ourself, then we revoke ourself as listener *before* the close call. So,
212 // if this here fires, something went definately wrong.
213 DBG_ERROR( "CloseableComponentImpl::notifyClosing: unreachable!" );
216 //--------------------------------------------------------------------
217 void SAL_CALL CloseableComponentImpl::disposing( const EventObject&
218 #ifdef DBG_UTIL
219 Source
220 #endif
221 ) throw (RuntimeException)
223 DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::disposing: where did this come from?" );
224 DBG_ERROR( "CloseableComponentImpl::disposing: unreachable!" );
225 // same reasoning for this assertion as in ->notifyClosing
228 //========================================================================
229 //= CloseableComponentImpl
230 //========================================================================
231 DBG_NAME( CloseableComponent )
232 //------------------------------------------------------------------------
233 CloseableComponent::CloseableComponent( const Reference< XInterface >& _rxComponent )
234 :m_pImpl( new CloseableComponentImpl( _rxComponent ) )
236 DBG_CTOR( CloseableComponent, NULL );
239 //------------------------------------------------------------------------
240 CloseableComponent::~CloseableComponent()
242 // close the component, deliver ownership to anybody who wants to veto the close
243 m_pImpl->nf_closeComponent();
244 DBG_DTOR( CloseableComponent, NULL );
247 //............................................................................
248 } // namespace utl
249 //............................................................................