Branch data 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 <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>
25 : :
26 : : //............................................................................
27 : : namespace utl
28 : : {
29 : : //............................................................................
30 : :
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;
41 : :
42 : : //========================================================================
43 : : //= DisposableComponent
44 : : //========================================================================
45 : : //------------------------------------------------------------------------
46 : 490 : DisposableComponent::DisposableComponent( const Reference< XInterface >& _rxComponent )
47 : 490 : :m_xComponent( _rxComponent, UNO_QUERY )
48 : : {
49 : : DBG_ASSERT( m_xComponent.is() || !_rxComponent.is(), "DisposableComponent::DisposableComponent: should be an XComponent!" );
50 : 490 : }
51 : :
52 : : //------------------------------------------------------------------------
53 : 490 : DisposableComponent::~DisposableComponent()
54 : : {
55 [ + + ]: 490 : if ( m_xComponent.is() )
56 : : {
57 : : try
58 : : {
59 [ + - ][ + - ]: 282 : m_xComponent->dispose();
60 : : }
61 [ # # ]: 0 : catch( const Exception& )
62 : : {
63 : : OSL_FAIL( "DisposableComponent::~DisposableComponent: caught an exception!" );
64 : : }
65 : 282 : m_xComponent.clear();
66 : : }
67 [ # # ]: 490 : }
68 : :
69 : : //========================================================================
70 : : //= CloseableComponentImpl
71 : : //========================================================================
72 : : DBG_NAME( CloseableComponentImpl )
73 : : typedef ::cppu::WeakImplHelper1 < XCloseListener
74 : : > CloseableComponentImpl_Base;
75 : : class CloseableComponentImpl : public CloseableComponentImpl_Base
76 : : {
77 : : private:
78 : : Reference< XCloseable > m_xCloseable;
79 : :
80 : : public:
81 : : CloseableComponentImpl( const Reference< XInterface >& _rxComponent );
82 : :
83 : : /** closes the component
84 : :
85 : : @nofail
86 : : */
87 : : void nf_closeComponent();
88 : :
89 : : protected:
90 : : virtual ~CloseableComponentImpl();
91 : :
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);
95 : :
96 : : // XEventListener overridables
97 : : virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
98 : :
99 : : private:
100 : : /** starts or stops being a CloseListener at the component
101 : :
102 : : Only to be called upon construction of the instance, or when the component
103 : : is to be closed.
104 : :
105 : : @nofail
106 : : */
107 : : void impl_nf_switchListening( bool _bListen );
108 : :
109 : :
110 : : private:
111 : : CloseableComponentImpl(); // never implemented
112 : : CloseableComponentImpl( const CloseableComponentImpl& ); // never implemented
113 : : CloseableComponentImpl& operator=( const CloseableComponentImpl& ); // never implemented
114 : : };
115 : :
116 : : //------------------------------------------------------------------------
117 : 14 : CloseableComponentImpl::CloseableComponentImpl( const Reference< XInterface >& _rxComponent )
118 [ + - ]: 14 : :m_xCloseable( _rxComponent, UNO_QUERY )
119 : : {
120 : : DBG_CTOR( CloseableComponentImpl, NULL );
121 : : DBG_ASSERT( m_xCloseable.is() || !_rxComponent.is(), "CloseableComponentImpl::CloseableComponentImpl: component is not an XCloseable!" );
122 [ + - ]: 14 : impl_nf_switchListening( true );
123 : 14 : }
124 : : //------------------------------------------------------------------------
125 : 14 : CloseableComponentImpl::~CloseableComponentImpl()
126 : : {
127 [ + - ]: 14 : nf_closeComponent();
128 : : DBG_DTOR( CloseableComponentImpl, NULL );
129 [ - + ]: 28 : }
130 : :
131 : : //------------------------------------------------------------------------
132 : 28 : void CloseableComponentImpl::nf_closeComponent()
133 : : {
134 [ + + ]: 28 : if ( !m_xCloseable.is() )
135 : : // nothing to do
136 [ # # # ]: 28 : return;
137 : :
138 : : // stop listening
139 : 14 : impl_nf_switchListening( false );
140 : :
141 : : // close
142 : : try
143 : : {
144 [ + - ][ + - ]: 14 : m_xCloseable->close( sal_True );
145 : : }
146 : 0 : catch( const CloseVetoException& ) { /* fine */ }
147 : 0 : catch( const Exception& )
148 : : {
149 : : OSL_FAIL( "CloseableComponentImpl::nf_closeComponent: caught an unexpected exception!" );
150 : : }
151 : :
152 : : // reset
153 : 14 : m_xCloseable.clear();
154 : : }
155 : :
156 : : //------------------------------------------------------------------------
157 : 28 : void CloseableComponentImpl::impl_nf_switchListening( bool _bListen )
158 : : {
159 [ - + ]: 28 : if ( !m_xCloseable.is() )
160 : 28 : return;
161 : :
162 : : try
163 : : {
164 [ + + ]: 28 : if ( _bListen )
165 [ + - ][ + - ]: 14 : m_xCloseable->addCloseListener( this );
[ + - ]
166 : : else
167 [ + - ][ + - ]: 14 : m_xCloseable->removeCloseListener( this );
[ + - ][ # # ]
168 : : }
169 : 0 : catch( const Exception& )
170 : : {
171 : : OSL_FAIL( "CloseableComponentImpl::impl_nf_switchListening: caught an exception!" );
172 : : }
173 : : }
174 : :
175 : : //--------------------------------------------------------------------
176 : 0 : void SAL_CALL CloseableComponentImpl::queryClosing( const EventObject&
177 : : #ifdef DBG_UTIL
178 : : Source
179 : : #endif
180 : : , ::sal_Bool /*GetsOwnership*/ ) throw (CloseVetoException, RuntimeException)
181 : : {
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 [ # # ]: 0 : throw CloseVetoException();
186 : : }
187 : :
188 : : //--------------------------------------------------------------------
189 : 0 : void SAL_CALL CloseableComponentImpl::notifyClosing( const EventObject&
190 : : #ifdef DBG_UTIL
191 : : Source
192 : : #endif
193 : : ) throw (RuntimeException)
194 : : {
195 : : DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::notifyClosing: where did this come from?" );
196 : :
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!" );
201 : 0 : }
202 : :
203 : : //--------------------------------------------------------------------
204 : 0 : void SAL_CALL CloseableComponentImpl::disposing( const EventObject&
205 : : #ifdef DBG_UTIL
206 : : Source
207 : : #endif
208 : : ) throw (RuntimeException)
209 : : {
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
213 : 0 : }
214 : :
215 : : //========================================================================
216 : : //= CloseableComponentImpl
217 : : //========================================================================
218 : : DBG_NAME( CloseableComponent )
219 : : //------------------------------------------------------------------------
220 : 14 : CloseableComponent::CloseableComponent( const Reference< XInterface >& _rxComponent )
221 [ + - ]: 14 : :m_pImpl( new CloseableComponentImpl( _rxComponent ) )
222 : : {
223 : : DBG_CTOR( CloseableComponent, NULL );
224 : 14 : }
225 : :
226 : : //------------------------------------------------------------------------
227 : 14 : CloseableComponent::~CloseableComponent()
228 : : {
229 : : // close the component, deliver ownership to anybody who wants to veto the close
230 [ + - ]: 14 : m_pImpl->nf_closeComponent();
231 : : DBG_DTOR( CloseableComponent, NULL );
232 : 14 : }
233 : :
234 : : //............................................................................
235 : : } // namespace utl
236 : : //............................................................................
237 : :
238 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|