Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / unotools / source / misc / sharedunocomponent.cxx
blob836fac0270ca0fc954adeef332b5f603e26f2789
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <unotools/sharedunocomponent.hxx>
21 #include <com/sun/star/lang/XComponent.hpp>
22 #include <com/sun/star/util/XCloseable.hpp>
23 #include <cppuhelper/implbase1.hxx>
24 #include <tools/debug.hxx>
26 //............................................................................
27 namespace utl
29 //............................................................................
31 using ::com::sun::star::uno::XInterface;
32 using ::com::sun::star::uno::Reference;
33 using ::com::sun::star::uno::Exception;
34 using ::com::sun::star::uno::UNO_QUERY;
35 using ::com::sun::star::uno::RuntimeException;
36 using ::com::sun::star::lang::XComponent;
37 using ::com::sun::star::lang::EventObject;
38 using ::com::sun::star::util::XCloseable;
39 using ::com::sun::star::util::XCloseListener;
40 using ::com::sun::star::util::CloseVetoException;
42 //========================================================================
43 //= DisposableComponent
44 //========================================================================
45 //------------------------------------------------------------------------
46 DisposableComponent::DisposableComponent( const Reference< XInterface >& _rxComponent )
47 :m_xComponent( _rxComponent, UNO_QUERY )
49 DBG_ASSERT( m_xComponent.is() || !_rxComponent.is(), "DisposableComponent::DisposableComponent: should be an XComponent!" );
52 //------------------------------------------------------------------------
53 DisposableComponent::~DisposableComponent()
55 if ( m_xComponent.is() )
57 try
59 m_xComponent->dispose();
61 catch( const Exception& )
63 OSL_FAIL( "DisposableComponent::~DisposableComponent: caught an exception!" );
65 m_xComponent.clear();
69 //========================================================================
70 //= CloseableComponentImpl
71 //========================================================================
72 DBG_NAME( CloseableComponentImpl )
73 typedef ::cppu::WeakImplHelper1 < XCloseListener
74 > CloseableComponentImpl_Base;
75 class CloseableComponentImpl : public CloseableComponentImpl_Base
77 private:
78 Reference< XCloseable > m_xCloseable;
80 public:
81 CloseableComponentImpl( const Reference< XInterface >& _rxComponent );
83 /** closes the component
85 @nofail
87 void nf_closeComponent();
89 protected:
90 virtual ~CloseableComponentImpl();
92 // XCloseListener overridables
93 virtual void SAL_CALL queryClosing( const EventObject& Source, ::sal_Bool GetsOwnership ) throw (CloseVetoException, RuntimeException);
94 virtual void SAL_CALL notifyClosing( const EventObject& Source ) throw (RuntimeException);
96 // XEventListener overridables
97 virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
99 private:
100 /** starts or stops being a CloseListener at the component
102 Only to be called upon construction of the instance, or when the component
103 is to be closed.
105 @nofail
107 void impl_nf_switchListening( bool _bListen );
110 private:
111 CloseableComponentImpl(); // never implemented
112 CloseableComponentImpl( const CloseableComponentImpl& ); // never implemented
113 CloseableComponentImpl& operator=( const CloseableComponentImpl& ); // never implemented
116 //------------------------------------------------------------------------
117 CloseableComponentImpl::CloseableComponentImpl( const Reference< XInterface >& _rxComponent )
118 :m_xCloseable( _rxComponent, UNO_QUERY )
120 DBG_CTOR( CloseableComponentImpl, NULL );
121 DBG_ASSERT( m_xCloseable.is() || !_rxComponent.is(), "CloseableComponentImpl::CloseableComponentImpl: component is not an XCloseable!" );
122 impl_nf_switchListening( true );
124 //------------------------------------------------------------------------
125 CloseableComponentImpl::~CloseableComponentImpl()
127 nf_closeComponent();
128 DBG_DTOR( CloseableComponentImpl, NULL );
131 //------------------------------------------------------------------------
132 void CloseableComponentImpl::nf_closeComponent()
134 if ( !m_xCloseable.is() )
135 // nothing to do
136 return;
138 // stop listening
139 impl_nf_switchListening( false );
141 // close
144 m_xCloseable->close( sal_True );
146 catch( const CloseVetoException& ) { /* fine */ }
147 catch( const Exception& )
149 OSL_FAIL( "CloseableComponentImpl::nf_closeComponent: caught an unexpected exception!" );
152 // reset
153 m_xCloseable.clear();
156 //------------------------------------------------------------------------
157 void CloseableComponentImpl::impl_nf_switchListening( bool _bListen )
159 if ( !m_xCloseable.is() )
160 return;
164 if ( _bListen )
165 m_xCloseable->addCloseListener( this );
166 else
167 m_xCloseable->removeCloseListener( this );
169 catch( const Exception& )
171 OSL_FAIL( "CloseableComponentImpl::impl_nf_switchListening: caught an exception!" );
175 //--------------------------------------------------------------------
176 void SAL_CALL CloseableComponentImpl::queryClosing( const EventObject&
177 #ifdef DBG_UTIL
178 Source
179 #endif
180 , ::sal_Bool /*GetsOwnership*/ ) throw (CloseVetoException, RuntimeException)
182 // as long as we live, somebody wants to keep the object alive. So, veto the
183 // closing
184 DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::queryClosing: where did this come from?" );
185 throw CloseVetoException();
188 //--------------------------------------------------------------------
189 void SAL_CALL CloseableComponentImpl::notifyClosing( const EventObject&
190 #ifdef DBG_UTIL
191 Source
192 #endif
193 ) throw (RuntimeException)
195 DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::notifyClosing: where did this come from?" );
197 // this should be unreachable: As long as we're a CloseListener, we veto the closing. If we're going
198 // to close the component ourself, then we revoke ourself as listener *before* the close call. So,
199 // if this here fires, something went definately wrong.
200 OSL_FAIL( "CloseableComponentImpl::notifyClosing: unreachable!" );
203 //--------------------------------------------------------------------
204 void SAL_CALL CloseableComponentImpl::disposing( const EventObject&
205 #ifdef DBG_UTIL
206 Source
207 #endif
208 ) throw (RuntimeException)
210 DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::disposing: where did this come from?" );
211 OSL_FAIL( "CloseableComponentImpl::disposing: unreachable!" );
212 // same reasoning for this assertion as in ->notifyClosing
215 //========================================================================
216 //= CloseableComponentImpl
217 //========================================================================
218 DBG_NAME( CloseableComponent )
219 //------------------------------------------------------------------------
220 CloseableComponent::CloseableComponent( const Reference< XInterface >& _rxComponent )
221 :m_pImpl( new CloseableComponentImpl( _rxComponent ) )
223 DBG_CTOR( CloseableComponent, NULL );
226 //------------------------------------------------------------------------
227 CloseableComponent::~CloseableComponent()
229 // close the component, deliver ownership to anybody who wants to veto the close
230 m_pImpl->nf_closeComponent();
231 DBG_DTOR( CloseableComponent, NULL );
234 //............................................................................
235 } // namespace utl
236 //............................................................................
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */