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