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/closeveto.hxx>
21 :
22 : #include <com/sun/star/util/XCloseable.hpp>
23 :
24 : #include <cppuhelper/implbase1.hxx>
25 : #include <rtl/ref.hxx>
26 : #include <tools/diagnose_ex.h>
27 :
28 : namespace utl
29 : {
30 :
31 : using ::com::sun::star::uno::Reference;
32 : using ::com::sun::star::uno::XInterface;
33 : using ::com::sun::star::uno::UNO_QUERY;
34 : using ::com::sun::star::uno::UNO_QUERY_THROW;
35 : using ::com::sun::star::uno::UNO_SET_THROW;
36 : using ::com::sun::star::uno::Exception;
37 : using ::com::sun::star::uno::RuntimeException;
38 : using ::com::sun::star::uno::Any;
39 : using ::com::sun::star::uno::makeAny;
40 : using ::com::sun::star::uno::Sequence;
41 : using ::com::sun::star::uno::Type;
42 : using ::com::sun::star::util::XCloseable;
43 : using ::com::sun::star::util::XCloseListener;
44 : using ::com::sun::star::util::CloseVetoException;
45 : using ::com::sun::star::lang::EventObject;
46 :
47 : //= CloseListener_Impl
48 :
49 : typedef ::cppu::WeakImplHelper1 < XCloseListener
50 : > CloseListener_Base;
51 : class CloseListener_Impl : public CloseListener_Base
52 : {
53 : public:
54 9 : explicit CloseListener_Impl(bool const bHasOwnership)
55 9 : : m_bHasOwnership(bHasOwnership)
56 : {
57 9 : }
58 :
59 : // XCloseListener
60 : virtual void SAL_CALL queryClosing( const EventObject& Source, sal_Bool GetsOwnership ) throw (CloseVetoException, RuntimeException, std::exception) SAL_OVERRIDE;
61 : virtual void SAL_CALL notifyClosing( const EventObject& Source ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
62 :
63 : // XEventListener
64 : virtual void SAL_CALL disposing( const EventObject& Source) throw (RuntimeException, std::exception) SAL_OVERRIDE;
65 :
66 8 : bool hasOwnership() const { return m_bHasOwnership; }
67 :
68 : protected:
69 16 : virtual ~CloseListener_Impl()
70 8 : {
71 16 : }
72 :
73 : private:
74 : bool m_bHasOwnership;
75 : };
76 :
77 0 : void SAL_CALL CloseListener_Impl::queryClosing( const EventObject& i_source, sal_Bool i_deliverOwnership ) throw (CloseVetoException, RuntimeException, std::exception)
78 : {
79 : (void)i_source;
80 :
81 0 : if ( !m_bHasOwnership )
82 0 : m_bHasOwnership = i_deliverOwnership;
83 :
84 0 : throw CloseVetoException();
85 : }
86 :
87 0 : void SAL_CALL CloseListener_Impl::notifyClosing( const EventObject& i_source ) throw (RuntimeException, std::exception)
88 : {
89 : (void)i_source;
90 0 : }
91 :
92 0 : void SAL_CALL CloseListener_Impl::disposing( const EventObject& i_source ) throw (RuntimeException, std::exception)
93 : {
94 : (void)i_source;
95 0 : }
96 :
97 : //= CloseVeto_Data
98 :
99 17 : struct CloseVeto_Data
100 : {
101 : Reference< XCloseable > xCloseable;
102 : ::rtl::Reference< CloseListener_Impl > pListener;
103 : };
104 :
105 : //= operations
106 :
107 : namespace
108 : {
109 :
110 9 : void lcl_init( CloseVeto_Data& i_data, const Reference< XInterface >& i_closeable,
111 : bool const hasOwnership)
112 : {
113 9 : i_data.xCloseable.set( i_closeable, UNO_QUERY );
114 18 : ENSURE_OR_RETURN_VOID( i_data.xCloseable.is(), "CloseVeto: the component is not closeable!" );
115 :
116 9 : i_data.pListener = new CloseListener_Impl(hasOwnership);
117 9 : i_data.xCloseable->addCloseListener( i_data.pListener.get() );
118 : }
119 :
120 8 : void lcl_deinit( CloseVeto_Data& i_data )
121 : {
122 8 : if ( !i_data.xCloseable.is() )
123 8 : return;
124 :
125 8 : i_data.xCloseable->removeCloseListener( i_data.pListener.get() );
126 8 : if ( i_data.pListener->hasOwnership() )
127 : {
128 : try
129 : {
130 8 : i_data.xCloseable->close( sal_True );
131 : }
132 0 : catch( const CloseVetoException& ) { }
133 0 : catch( const Exception& )
134 : {
135 : DBG_UNHANDLED_EXCEPTION();
136 : }
137 : }
138 : }
139 : }
140 :
141 : //= CloseVeto
142 9 : CloseVeto::CloseVeto(const Reference< XInterface >& i_closeable,
143 : bool const hasOwnership)
144 9 : : m_xData(new CloseVeto_Data)
145 : {
146 9 : lcl_init(*m_xData, i_closeable, hasOwnership);
147 9 : }
148 :
149 16 : CloseVeto::~CloseVeto()
150 : {
151 8 : lcl_deinit(*m_xData);
152 8 : }
153 :
154 : } // namespace utl
155 :
156 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|