Line data Source code
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 :
22 : #include <boost/noncopyable.hpp>
23 : #include <unotools/sharedunocomponent.hxx>
24 : #include <com/sun/star/lang/XComponent.hpp>
25 : #include <com/sun/star/util/XCloseable.hpp>
26 : #include <cppuhelper/implbase1.hxx>
27 : #include <tools/debug.hxx>
28 :
29 : namespace utl
30 : {
31 :
32 : using ::com::sun::star::uno::XInterface;
33 : using ::com::sun::star::uno::Reference;
34 : using ::com::sun::star::uno::Exception;
35 : using ::com::sun::star::uno::UNO_QUERY;
36 : using ::com::sun::star::uno::RuntimeException;
37 : using ::com::sun::star::lang::XComponent;
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 584 : DisposableComponent::DisposableComponent( const Reference< XInterface >& _rxComponent )
46 584 : :m_xComponent( _rxComponent, UNO_QUERY )
47 : {
48 : DBG_ASSERT( m_xComponent.is() || !_rxComponent.is(), "DisposableComponent::DisposableComponent: should be an XComponent!" );
49 584 : }
50 :
51 1164 : DisposableComponent::~DisposableComponent()
52 : {
53 582 : if ( m_xComponent.is() )
54 : {
55 : try
56 : {
57 320 : m_xComponent->dispose();
58 : }
59 0 : catch( const Exception& )
60 : {
61 : OSL_FAIL( "DisposableComponent::~DisposableComponent: caught an exception!" );
62 : }
63 320 : m_xComponent.clear();
64 : }
65 582 : }
66 :
67 : typedef ::cppu::WeakImplHelper1 < XCloseListener
68 : > CloseableComponentImpl_Base;
69 : class CloseableComponentImpl:
70 : public CloseableComponentImpl_Base, private boost::noncopyable
71 : {
72 : private:
73 : Reference< XCloseable > m_xCloseable;
74 :
75 : public:
76 : CloseableComponentImpl( const Reference< XInterface >& _rxComponent );
77 :
78 : /** closes the component
79 :
80 : @nofail
81 : */
82 : void nf_closeComponent();
83 :
84 : protected:
85 : virtual ~CloseableComponentImpl();
86 :
87 : // XCloseListener overridables
88 : virtual void SAL_CALL queryClosing( const EventObject& Source, sal_Bool GetsOwnership ) throw (CloseVetoException, RuntimeException, std::exception) SAL_OVERRIDE;
89 : virtual void SAL_CALL notifyClosing( const EventObject& Source ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
90 :
91 : // XEventListener overridables
92 : virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
93 :
94 : private:
95 : /** starts or stops being a CloseListener at the component
96 :
97 : Only to be called upon construction of the instance, or when the component
98 : is to be closed.
99 :
100 : @nofail
101 : */
102 : void impl_nf_switchListening( bool _bListen );
103 : };
104 :
105 18 : CloseableComponentImpl::CloseableComponentImpl( const Reference< XInterface >& _rxComponent )
106 18 : :m_xCloseable( _rxComponent, UNO_QUERY )
107 : {
108 : DBG_ASSERT( m_xCloseable.is() || !_rxComponent.is(), "CloseableComponentImpl::CloseableComponentImpl: component is not an XCloseable!" );
109 18 : impl_nf_switchListening( true );
110 18 : }
111 :
112 54 : CloseableComponentImpl::~CloseableComponentImpl()
113 : {
114 18 : nf_closeComponent();
115 36 : }
116 :
117 36 : void CloseableComponentImpl::nf_closeComponent()
118 : {
119 36 : if ( !m_xCloseable.is() )
120 : // nothing to do
121 54 : return;
122 :
123 : // stop listening
124 18 : impl_nf_switchListening( false );
125 :
126 : // close
127 : try
128 : {
129 18 : m_xCloseable->close( sal_True );
130 : }
131 0 : catch( const CloseVetoException& ) { /* fine */ }
132 0 : catch( const Exception& )
133 : {
134 : OSL_FAIL( "CloseableComponentImpl::nf_closeComponent: caught an unexpected exception!" );
135 : }
136 :
137 : // reset
138 18 : m_xCloseable.clear();
139 : }
140 :
141 36 : void CloseableComponentImpl::impl_nf_switchListening( bool _bListen )
142 : {
143 36 : if ( !m_xCloseable.is() )
144 36 : return;
145 :
146 : try
147 : {
148 36 : if ( _bListen )
149 18 : m_xCloseable->addCloseListener( this );
150 : else
151 18 : m_xCloseable->removeCloseListener( this );
152 : }
153 0 : catch( const Exception& )
154 : {
155 : OSL_FAIL( "CloseableComponentImpl::impl_nf_switchListening: caught an exception!" );
156 : }
157 : }
158 :
159 0 : void SAL_CALL CloseableComponentImpl::queryClosing( const EventObject&
160 : #ifdef DBG_UTIL
161 : Source
162 : #endif
163 : , sal_Bool /*GetsOwnership*/ ) throw (CloseVetoException, RuntimeException, std::exception)
164 : {
165 : // as long as we live, somebody wants to keep the object alive. So, veto the
166 : // closing
167 : DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::queryClosing: where did this come from?" );
168 0 : throw CloseVetoException();
169 : }
170 :
171 0 : void SAL_CALL CloseableComponentImpl::notifyClosing( const EventObject&
172 : #ifdef DBG_UTIL
173 : Source
174 : #endif
175 : ) throw (RuntimeException, std::exception)
176 : {
177 : DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::notifyClosing: where did this come from?" );
178 :
179 : // this should be unreachable: As long as we're a CloseListener, we veto the closing. If we're going
180 : // to close the component ourself, then we revoke ourself as listener *before* the close call. So,
181 : // if this here fires, something went definitely wrong.
182 : OSL_FAIL( "CloseableComponentImpl::notifyClosing: unreachable!" );
183 0 : }
184 :
185 0 : void SAL_CALL CloseableComponentImpl::disposing( const EventObject&
186 : #ifdef DBG_UTIL
187 : Source
188 : #endif
189 : ) throw (RuntimeException, std::exception)
190 : {
191 : DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::disposing: where did this come from?" );
192 : OSL_FAIL( "CloseableComponentImpl::disposing: unreachable!" );
193 : // same reasoning for this assertion as in ->notifyClosing
194 0 : }
195 :
196 18 : CloseableComponent::CloseableComponent( const Reference< XInterface >& _rxComponent )
197 18 : :m_pImpl( new CloseableComponentImpl( _rxComponent ) )
198 : {
199 18 : }
200 :
201 36 : CloseableComponent::~CloseableComponent()
202 : {
203 : // close the component, deliver ownership to anybody who wants to veto the close
204 18 : m_pImpl->nf_closeComponent();
205 18 : }
206 :
207 : } // namespace utl
208 :
209 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|