1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
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
;
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() )
57 m_xComponent
->dispose();
59 catch( const Exception
& )
61 OSL_FAIL( "DisposableComponent::~DisposableComponent: caught an exception!" );
67 typedef ::cppu::WeakImplHelper1
< XCloseListener
68 > CloseableComponentImpl_Base
;
69 class CloseableComponentImpl
:
70 public CloseableComponentImpl_Base
, private boost::noncopyable
73 Reference
< XCloseable
> m_xCloseable
;
76 CloseableComponentImpl( const Reference
< XInterface
>& _rxComponent
);
78 /** closes the component
82 void nf_closeComponent();
85 virtual ~CloseableComponentImpl();
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
;
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
;
95 /** starts or stops being a CloseListener at the component
97 Only to be called upon construction of the instance, or when the component
102 void impl_nf_switchListening( bool _bListen
);
105 CloseableComponentImpl::CloseableComponentImpl( const Reference
< XInterface
>& _rxComponent
)
106 :m_xCloseable( _rxComponent
, UNO_QUERY
)
108 DBG_ASSERT( m_xCloseable
.is() || !_rxComponent
.is(), "CloseableComponentImpl::CloseableComponentImpl: component is not an XCloseable!" );
109 impl_nf_switchListening( true );
112 CloseableComponentImpl::~CloseableComponentImpl()
117 void CloseableComponentImpl::nf_closeComponent()
119 if ( !m_xCloseable
.is() )
124 impl_nf_switchListening( false );
129 m_xCloseable
->close( sal_True
);
131 catch( const CloseVetoException
& ) { /* fine */ }
132 catch( const Exception
& )
134 OSL_FAIL( "CloseableComponentImpl::nf_closeComponent: caught an unexpected exception!" );
138 m_xCloseable
.clear();
141 void CloseableComponentImpl::impl_nf_switchListening( bool _bListen
)
143 if ( !m_xCloseable
.is() )
149 m_xCloseable
->addCloseListener( this );
151 m_xCloseable
->removeCloseListener( this );
153 catch( const Exception
& )
155 OSL_FAIL( "CloseableComponentImpl::impl_nf_switchListening: caught an exception!" );
159 void SAL_CALL
CloseableComponentImpl::queryClosing( const EventObject
&
163 , sal_Bool
/*GetsOwnership*/ ) throw (CloseVetoException
, RuntimeException
, std::exception
)
165 // as long as we live, somebody wants to keep the object alive. So, veto the
167 DBG_ASSERT( Source
.Source
== m_xCloseable
, "CloseableComponentImpl::queryClosing: where did this come from?" );
168 throw CloseVetoException();
171 void SAL_CALL
CloseableComponentImpl::notifyClosing( const EventObject
&
175 ) throw (RuntimeException
, std::exception
)
177 DBG_ASSERT( Source
.Source
== m_xCloseable
, "CloseableComponentImpl::notifyClosing: where did this come from?" );
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!" );
185 void SAL_CALL
CloseableComponentImpl::disposing( const EventObject
&
189 ) throw (RuntimeException
, std::exception
)
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
196 CloseableComponent::CloseableComponent( const Reference
< XInterface
>& _rxComponent
)
197 :m_pImpl( new CloseableComponentImpl( _rxComponent
) )
201 CloseableComponent::~CloseableComponent()
203 // close the component, deliver ownership to anybody who wants to veto the close
204 m_pImpl
->nf_closeComponent();
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */