Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / unotools / source / misc / sharedunocomponent.cxx
blob1b724a2b94f6e11abb8fd26d1b0965ace6606220
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 <sal/config.h>
21 #include <osl/diagnose.h>
23 #include <unotools/sharedunocomponent.hxx>
24 #include <com/sun/star/lang/XComponent.hpp>
25 #include <com/sun/star/util/CloseVetoException.hpp>
26 #include <com/sun/star/util/XCloseable.hpp>
27 #include <cppuhelper/implbase.hxx>
28 #include <tools/debug.hxx>
29 #include <comphelper/diagnose_ex.hxx>
31 namespace utl
34 using ::com::sun::star::uno::XInterface;
35 using ::com::sun::star::uno::Reference;
36 using ::com::sun::star::uno::Exception;
37 using ::com::sun::star::uno::UNO_QUERY;
38 using ::com::sun::star::lang::EventObject;
39 using ::com::sun::star::util::XCloseable;
40 using ::com::sun::star::util::XCloseListener;
41 using ::com::sun::star::util::CloseVetoException;
43 //= DisposableComponent
45 DisposableComponent::DisposableComponent( const Reference< XInterface >& _rxComponent )
46 :m_xComponent( _rxComponent, UNO_QUERY )
48 DBG_ASSERT( m_xComponent.is() || !_rxComponent.is(), "DisposableComponent::DisposableComponent: should be an XComponent!" );
51 DisposableComponent::~DisposableComponent()
53 if ( m_xComponent.is() )
55 try
57 m_xComponent->dispose();
59 catch( const Exception& )
61 TOOLS_WARN_EXCEPTION( "unotools", "DisposableComponent::~DisposableComponent" );
63 m_xComponent.clear();
67 typedef ::cppu::WeakImplHelper < XCloseListener
68 > CloseableComponentImpl_Base;
69 class CloseableComponentImpl : public CloseableComponentImpl_Base
71 private:
72 Reference< XCloseable > m_xCloseable;
74 CloseableComponentImpl(const CloseableComponentImpl&) = delete;
75 CloseableComponentImpl& operator=(const CloseableComponentImpl&) = delete;
77 public:
78 explicit CloseableComponentImpl( const Reference< XInterface >& _rxComponent );
80 /** closes the component
82 @nofail
84 void nf_closeComponent();
86 protected:
87 virtual ~CloseableComponentImpl() override;
89 // XCloseListener overridables
90 virtual void SAL_CALL queryClosing( const EventObject& Source, sal_Bool GetsOwnership ) override;
91 virtual void SAL_CALL notifyClosing( const EventObject& Source ) override;
93 // XEventListener overridables
94 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
96 private:
97 /** starts or stops being a CloseListener at the component
99 Only to be called upon construction of the instance, or when the component
100 is to be closed.
102 @nofail
104 void impl_nf_switchListening( bool _bListen );
107 CloseableComponentImpl::CloseableComponentImpl( const Reference< XInterface >& _rxComponent )
108 :m_xCloseable( _rxComponent, UNO_QUERY )
110 DBG_ASSERT( m_xCloseable.is() || !_rxComponent.is(), "CloseableComponentImpl::CloseableComponentImpl: component is not an XCloseable!" );
111 impl_nf_switchListening( true );
114 CloseableComponentImpl::~CloseableComponentImpl()
116 nf_closeComponent();
119 void CloseableComponentImpl::nf_closeComponent()
121 if ( !m_xCloseable.is() )
122 // nothing to do
123 return;
125 // stop listening
126 impl_nf_switchListening( false );
128 // close
131 m_xCloseable->close( true );
133 catch( const CloseVetoException& ) { /* fine */ }
134 catch( const Exception& )
136 TOOLS_WARN_EXCEPTION( "unotools", "CloseableComponentImpl::nf_closeComponent: caught an unexpected exception!" );
139 // reset
140 m_xCloseable.clear();
143 void CloseableComponentImpl::impl_nf_switchListening( bool _bListen )
145 if ( !m_xCloseable.is() )
146 return;
150 if ( _bListen )
151 m_xCloseable->addCloseListener( this );
152 else
153 m_xCloseable->removeCloseListener( this );
155 catch( const Exception& )
157 TOOLS_WARN_EXCEPTION( "unotools", "CloseableComponentImpl::impl_nf_switchListening" );
161 void SAL_CALL CloseableComponentImpl::queryClosing( const EventObject& Source, sal_Bool /*GetsOwnership*/ )
163 // as long as we live, somebody wants to keep the object alive. So, veto the
164 // closing
165 DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::queryClosing: where did this come from?" );
166 throw CloseVetoException();
169 void SAL_CALL CloseableComponentImpl::notifyClosing( const EventObject& Source )
171 DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::notifyClosing: where did this come from?" );
173 // this should be unreachable: As long as we're a CloseListener, we veto the closing. If we're going
174 // to close the component ourself, then we revoke ourself as listener *before* the close call. So,
175 // if this here fires, something went definitely wrong.
176 OSL_FAIL( "CloseableComponentImpl::notifyClosing: unreachable!" );
179 void SAL_CALL CloseableComponentImpl::disposing( const EventObject& Source )
181 DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::disposing: where did this come from?" );
182 OSL_FAIL( "CloseableComponentImpl::disposing: unreachable!" );
183 // same reasoning for this assertion as in ->notifyClosing
186 CloseableComponent::CloseableComponent( const Reference< XInterface >& _rxComponent )
187 :m_pImpl( new CloseableComponentImpl( _rxComponent ) )
191 CloseableComponent::~CloseableComponent()
193 // close the component, deliver ownership to anybody who wants to veto the close
194 m_pImpl->nf_closeComponent();
197 } // namespace utl
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */