xref: /trunk/main/dbaccess/source/ui/app/closeveto.cxx (revision cdf0e10c)
1 /*************************************************************************
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * Copyright 2000, 2010 Oracle and/or its affiliates.
5  *
6  * OpenOffice.org - a multi-platform office productivity suite
7  *
8  * This file is part of OpenOffice.org.
9  *
10  * OpenOffice.org is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License version 3
12  * only, as published by the Free Software Foundation.
13  *
14  * OpenOffice.org is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License version 3 for more details
18  * (a copy is included in the LICENSE file that accompanied this code).
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with OpenOffice.org.  If not, see
22  * <http://www.openoffice.org/license.html>
23  * for a copy of the LGPLv3 License.
24  *
25  ************************************************************************/
26 
27 #include "precompiled_dbaccess.hxx"
28 
29 #include "closeveto.hxx"
30 
31 /** === begin UNO includes === **/
32 #include <com/sun/star/util/XCloseable.hpp>
33 /** === end UNO includes === **/
34 
35 #include <cppuhelper/implbase1.hxx>
36 #include <rtl/ref.hxx>
37 #include <tools/diagnose_ex.h>
38 
39 //......................................................................................................................
40 namespace dbaui
41 {
42 //......................................................................................................................
43 
44 	/** === begin UNO using === **/
45 	using ::com::sun::star::uno::Reference;
46 	using ::com::sun::star::uno::XInterface;
47 	using ::com::sun::star::uno::UNO_QUERY;
48 	using ::com::sun::star::uno::UNO_QUERY_THROW;
49 	using ::com::sun::star::uno::UNO_SET_THROW;
50 	using ::com::sun::star::uno::Exception;
51 	using ::com::sun::star::uno::RuntimeException;
52 	using ::com::sun::star::uno::Any;
53 	using ::com::sun::star::uno::makeAny;
54 	using ::com::sun::star::uno::Sequence;
55 	using ::com::sun::star::uno::Type;
56     using ::com::sun::star::util::XCloseable;
57     using ::com::sun::star::util::XCloseListener;
58     using ::com::sun::star::util::CloseVetoException;
59     using ::com::sun::star::lang::EventObject;
60 	/** === end UNO using === **/
61 
62 	//==================================================================================================================
63 	//= CloseListener_Impl
64 	//==================================================================================================================
65     typedef ::cppu::WeakImplHelper1 <   XCloseListener
66                                     >   CloseListener_Base;
67     class DBACCESS_DLLPRIVATE CloseListener_Impl : public CloseListener_Base
68     {
69     public:
70         CloseListener_Impl()
71             :m_bHasOwnership( false )
72         {
73         }
74 
75         // XCloseListener
76         virtual void SAL_CALL queryClosing( const EventObject& Source, ::sal_Bool GetsOwnership ) throw (CloseVetoException, RuntimeException);
77         virtual void SAL_CALL notifyClosing( const EventObject& Source ) throw (RuntimeException);
78 
79         // XEventListener
80         virtual void SAL_CALL disposing( const EventObject& Source) throw (RuntimeException);
81 
82         bool hasOwnership() const { return m_bHasOwnership; }
83 
84     protected:
85         ~CloseListener_Impl()
86         {
87         }
88 
89     private:
90         bool    m_bHasOwnership;
91     };
92 
93     //------------------------------------------------------------------------------------------------------------------
94     void SAL_CALL CloseListener_Impl::queryClosing( const EventObject& i_source, ::sal_Bool i_deliverOwnership ) throw (CloseVetoException, RuntimeException)
95     {
96         (void)i_source;
97 
98         if ( !m_bHasOwnership )
99             m_bHasOwnership = i_deliverOwnership;
100 
101         throw CloseVetoException();
102     }
103 
104     //------------------------------------------------------------------------------------------------------------------
105     void SAL_CALL CloseListener_Impl::notifyClosing( const EventObject& i_source ) throw (RuntimeException)
106     {
107         (void)i_source;
108     }
109 
110     //------------------------------------------------------------------------------------------------------------------
111     void SAL_CALL CloseListener_Impl::disposing( const EventObject& i_source ) throw (RuntimeException)
112     {
113         (void)i_source;
114     }
115 
116 	//==================================================================================================================
117 	//= CloseVeto_Data
118 	//==================================================================================================================
119     struct DBACCESS_DLLPRIVATE CloseVeto_Data
120     {
121         Reference< XCloseable >                 xCloseable;
122         ::rtl::Reference< CloseListener_Impl >  pListener;
123     };
124 
125 	//==================================================================================================================
126 	//= operations
127 	//==================================================================================================================
128     namespace
129     {
130 	    //--------------------------------------------------------------------------------------------------------------
131         void lcl_init( CloseVeto_Data& i_data, const Reference< XInterface >& i_closeable )
132         {
133             i_data.xCloseable.set( i_closeable, UNO_QUERY );
134             ENSURE_OR_RETURN_VOID( i_data.xCloseable.is(), "CloseVeto: the component is not closeable!" );
135 
136             i_data.pListener = new CloseListener_Impl;
137             i_data.xCloseable->addCloseListener( i_data.pListener.get() );
138         }
139 
140         //--------------------------------------------------------------------------------------------------------------
141         void lcl_deinit( CloseVeto_Data& i_data )
142         {
143             if ( !i_data.xCloseable.is() )
144                 return;
145 
146             i_data.xCloseable->removeCloseListener( i_data.pListener.get() );
147             if ( i_data.pListener->hasOwnership() )
148             {
149                 try
150                 {
151                     i_data.xCloseable->close( sal_True );
152                 }
153                 catch( const CloseVetoException& ) { }
154                 catch( const Exception& )
155                 {
156                 	DBG_UNHANDLED_EXCEPTION();
157                 }
158             }
159         }
160     }
161 
162 	//==================================================================================================================
163 	//= CloseVeto
164 	//==================================================================================================================
165 	//------------------------------------------------------------------------------------------------------------------
166     CloseVeto::CloseVeto( const Reference< XInterface >& i_closeable )
167         :m_pData( new CloseVeto_Data )
168     {
169         lcl_init( *m_pData, i_closeable );
170     }
171 
172 	//------------------------------------------------------------------------------------------------------------------
173     CloseVeto::~CloseVeto()
174     {
175         lcl_deinit( *m_pData );
176     }
177 
178 //......................................................................................................................
179 } // namespace dbaui
180 //......................................................................................................................
181