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 <unotools/closeveto.hxx>
22 #include <com/sun/star/util/CloseVetoException.hpp>
23 #include <com/sun/star/util/XCloseable.hpp>
25 #include <cppuhelper/implbase.hxx>
26 #include <rtl/ref.hxx>
27 #include <comphelper/diagnose_ex.hxx>
32 using ::com::sun::star::uno::Reference
;
33 using ::com::sun::star::uno::XInterface
;
34 using ::com::sun::star::uno::UNO_QUERY
;
35 using ::com::sun::star::uno::Exception
;
36 using ::com::sun::star::util::XCloseable
;
37 using ::com::sun::star::util::XCloseListener
;
38 using ::com::sun::star::util::CloseVetoException
;
39 using ::com::sun::star::lang::EventObject
;
41 //= CloseListener_Impl
43 typedef ::cppu::WeakImplHelper
< XCloseListener
48 class CloseListener_Impl
: public CloseListener_Base
51 explicit CloseListener_Impl(bool const bHasOwnership
)
52 : m_bHasOwnership(bHasOwnership
)
57 virtual void SAL_CALL
queryClosing( const EventObject
& Source
, sal_Bool GetsOwnership
) override
;
58 virtual void SAL_CALL
notifyClosing( const EventObject
& Source
) override
;
61 virtual void SAL_CALL
disposing( const EventObject
& Source
) override
;
63 bool hasOwnership() const { return m_bHasOwnership
; }
66 virtual ~CloseListener_Impl() override
76 void SAL_CALL
CloseListener_Impl::queryClosing( const EventObject
&, sal_Bool i_deliverOwnership
)
78 if ( !m_bHasOwnership
)
79 m_bHasOwnership
= i_deliverOwnership
;
81 throw CloseVetoException();
84 void SAL_CALL
CloseListener_Impl::notifyClosing( const EventObject
& ) {}
86 void SAL_CALL
CloseListener_Impl::disposing( const EventObject
& ) {}
92 Reference
< XCloseable
> xCloseable
;
93 ::rtl::Reference
< CloseListener_Impl
> pListener
;
101 void lcl_init( CloseVeto_Data
& i_data
, const Reference
< XInterface
>& i_closeable
,
102 bool const hasOwnership
)
104 i_data
.xCloseable
.set( i_closeable
, UNO_QUERY
);
105 ENSURE_OR_RETURN_VOID( i_data
.xCloseable
.is(), "CloseVeto: the component is not closeable!" );
107 i_data
.pListener
= new CloseListener_Impl(hasOwnership
);
108 i_data
.xCloseable
->addCloseListener( i_data
.pListener
);
111 void lcl_deinit( CloseVeto_Data
const & i_data
)
113 if ( !i_data
.xCloseable
.is() )
116 i_data
.xCloseable
->removeCloseListener( i_data
.pListener
);
117 if ( i_data
.pListener
->hasOwnership() )
121 i_data
.xCloseable
->close( true );
123 catch( const CloseVetoException
& ) { }
124 catch( const Exception
& )
126 DBG_UNHANDLED_EXCEPTION("unotools");
133 CloseVeto::CloseVeto(const Reference
< XInterface
>& i_closeable
,
134 bool const hasOwnership
)
135 : m_xData(new CloseVeto_Data
)
137 lcl_init(*m_xData
, i_closeable
, hasOwnership
);
140 CloseVeto::~CloseVeto()
142 lcl_deinit(*m_xData
);
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */