LibreOffice Module unotools (master) 1
sharedunocomponent.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21#include <osl/diagnose.h>
22
24#include <com/sun/star/lang/XComponent.hpp>
25#include <com/sun/star/util/CloseVetoException.hpp>
26#include <com/sun/star/util/XCloseable.hpp>
28#include <tools/debug.hxx>
30
31namespace utl
32{
33
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;
42
43 //= DisposableComponent
44
45 DisposableComponent::DisposableComponent( const Reference< XInterface >& _rxComponent )
46 :m_xComponent( _rxComponent, UNO_QUERY )
47 {
48 DBG_ASSERT( m_xComponent.is() || !_rxComponent.is(), "DisposableComponent::DisposableComponent: should be an XComponent!" );
49 }
50
51 DisposableComponent::~DisposableComponent()
52 {
53 if ( m_xComponent.is() )
54 {
55 try
56 {
57 m_xComponent->dispose();
58 }
59 catch( const Exception& )
60 {
61 TOOLS_WARN_EXCEPTION( "unotools", "DisposableComponent::~DisposableComponent" );
62 }
63 m_xComponent.clear();
64 }
65 }
66
67 typedef ::cppu::WeakImplHelper < XCloseListener
70 {
71 private:
72 Reference< XCloseable > m_xCloseable;
73
76
77 public:
78 explicit CloseableComponentImpl( const Reference< XInterface >& _rxComponent );
79
84 void nf_closeComponent();
85
86 protected:
87 virtual ~CloseableComponentImpl() override;
88
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;
92
93 // XEventListener overridables
94 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
95
96 private:
104 void impl_nf_switchListening( bool _bListen );
105 };
106
107 CloseableComponentImpl::CloseableComponentImpl( const Reference< XInterface >& _rxComponent )
108 :m_xCloseable( _rxComponent, UNO_QUERY )
109 {
110 DBG_ASSERT( m_xCloseable.is() || !_rxComponent.is(), "CloseableComponentImpl::CloseableComponentImpl: component is not an XCloseable!" );
112 }
113
115 {
117 }
118
120 {
121 if ( !m_xCloseable.is() )
122 // nothing to do
123 return;
124
125 // stop listening
127
128 // close
129 try
130 {
131 m_xCloseable->close( true );
132 }
133 catch( const CloseVetoException& ) { /* fine */ }
134 catch( const Exception& )
135 {
136 TOOLS_WARN_EXCEPTION( "unotools", "CloseableComponentImpl::nf_closeComponent: caught an unexpected exception!" );
137 }
138
139 // reset
140 m_xCloseable.clear();
141 }
142
144 {
145 if ( !m_xCloseable.is() )
146 return;
147
148 try
149 {
150 if ( _bListen )
151 m_xCloseable->addCloseListener( this );
152 else
153 m_xCloseable->removeCloseListener( this );
154 }
155 catch( const Exception& )
156 {
157 TOOLS_WARN_EXCEPTION( "unotools", "CloseableComponentImpl::impl_nf_switchListening" );
158 }
159 }
160
161 void SAL_CALL CloseableComponentImpl::queryClosing( const EventObject& Source, sal_Bool /*GetsOwnership*/ )
162 {
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();
167 }
168
169 void SAL_CALL CloseableComponentImpl::notifyClosing( const EventObject& Source )
170 {
171 DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::notifyClosing: where did this come from?" );
172
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!" );
177 }
178
179 void SAL_CALL CloseableComponentImpl::disposing( const EventObject& Source )
180 {
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
184 }
185
186 CloseableComponent::CloseableComponent( const Reference< XInterface >& _rxComponent )
187 :m_pImpl( new CloseableComponentImpl( _rxComponent ) )
188 {
189 }
190
192 {
193 // close the component, deliver ownership to anybody who wants to veto the close
194 m_pImpl->nf_closeComponent();
195 }
196
197} // namespace utl
198
199/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
void impl_nf_switchListening(bool _bListen)
starts or stops being a CloseListener at the component
virtual void SAL_CALL notifyClosing(const EventObject &Source) override
virtual void SAL_CALL queryClosing(const EventObject &Source, sal_Bool GetsOwnership) override
virtual ~CloseableComponentImpl() override
CloseableComponentImpl & operator=(const CloseableComponentImpl &)=delete
void nf_closeComponent()
closes the component
Reference< XCloseable > m_xCloseable
CloseableComponentImpl(const CloseableComponentImpl &)=delete
~CloseableComponent()
destroys resources associated with this instance, and disposes the component
CloseableComponent(const css::uno::Reference< css::uno::XInterface > &_rxComponent)
constructs a ->CloseableComponent instance
::rtl::Reference< CloseableComponentImpl > m_pImpl
Our IMPL class.
DisposableComponent(const css::uno::Reference< css::uno::XInterface > &_rxComponent)
constructs a ->DisposableComponent instance
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
@ Exception
::cppu::WeakImplHelper< XCloseListener > CloseableComponentImpl_Base
unsigned char sal_Bool