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 0 : CloseListener_Impl()
55 0 : :m_bHasOwnership( false )
56 : {
57 0 : }
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 0 : bool hasOwnership() const { return m_bHasOwnership; }
67 :
68 : protected:
69 0 : virtual ~CloseListener_Impl()
70 0 : {
71 0 : }
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 0 : struct CloseVeto_Data
100 : {
101 : Reference< XCloseable > xCloseable;
102 : ::rtl::Reference< CloseListener_Impl > pListener;
103 : };
104 :
105 : //= operations
106 :
107 : namespace
108 : {
109 :
110 0 : void lcl_init( CloseVeto_Data& i_data, const Reference< XInterface >& i_closeable )
111 : {
112 0 : i_data.xCloseable.set( i_closeable, UNO_QUERY );
113 0 : ENSURE_OR_RETURN_VOID( i_data.xCloseable.is(), "CloseVeto: the component is not closeable!" );
114 :
115 0 : i_data.pListener = new CloseListener_Impl;
116 0 : i_data.xCloseable->addCloseListener( i_data.pListener.get() );
117 : }
118 :
119 0 : void lcl_deinit( CloseVeto_Data& i_data )
120 : {
121 0 : if ( !i_data.xCloseable.is() )
122 0 : return;
123 :
124 0 : i_data.xCloseable->removeCloseListener( i_data.pListener.get() );
125 0 : if ( i_data.pListener->hasOwnership() )
126 : {
127 : try
128 : {
129 0 : i_data.xCloseable->close( sal_True );
130 : }
131 0 : catch( const CloseVetoException& ) { }
132 0 : catch( const Exception& )
133 : {
134 : DBG_UNHANDLED_EXCEPTION();
135 : }
136 : }
137 : }
138 : }
139 :
140 : //= CloseVeto
141 :
142 0 : CloseVeto::CloseVeto( const Reference< XInterface >& i_closeable )
143 0 : :m_pData( new CloseVeto_Data )
144 : {
145 0 : lcl_init( *m_pData, i_closeable );
146 0 : }
147 :
148 0 : CloseVeto::~CloseVeto()
149 : {
150 0 : lcl_deinit( *m_pData );
151 0 : }
152 :
153 : } // namespace utl
154 :
155 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|