1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sharedunocomponent.cxx,v $
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 //............................................................................
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() )
72 m_xComponent
->dispose();
74 catch( const Exception
& )
76 OSL_ENSURE( sal_False
, "DisposableComponent::~DisposableComponent: caught an exception!" );
82 //========================================================================
83 //= CloseableComponentImpl
84 //========================================================================
85 DBG_NAME( CloseableComponentImpl
)
86 typedef ::cppu::WeakImplHelper1
< XCloseListener
87 > CloseableComponentImpl_Base
;
88 class CloseableComponentImpl
: public CloseableComponentImpl_Base
91 Reference
< XCloseable
> m_xCloseable
;
94 CloseableComponentImpl( const Reference
< XInterface
>& _rxComponent
);
96 /** closes the component
100 void nf_closeComponent();
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
);
113 /** starts or stops being a CloseListener at the component
115 Only to be called upon construction of the instance, or when the component
120 void impl_nf_switchListening( bool _bListen
);
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()
141 DBG_DTOR( CloseableComponentImpl
, NULL
);
144 //------------------------------------------------------------------------
145 void CloseableComponentImpl::nf_closeComponent()
147 if ( !m_xCloseable
.is() )
152 impl_nf_switchListening( false );
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!" );
166 m_xCloseable
.clear();
169 //------------------------------------------------------------------------
170 void CloseableComponentImpl::impl_nf_switchListening( bool _bListen
)
172 if ( !m_xCloseable
.is() )
178 m_xCloseable
->addCloseListener( this );
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
&
193 , ::sal_Bool
/*GetsOwnership*/ ) throw (CloseVetoException
, RuntimeException
)
195 // as long as we live, somebody wants to keep the object alive. So, veto the
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
&
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
&
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 //............................................................................
249 //............................................................................