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