1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "precompiled_dbaccess.hxx"
25 
26 #include "closeveto.hxx"
27 
28 /** === begin UNO includes === **/
29 #include <com/sun/star/util/XCloseable.hpp>
30 /** === end UNO includes === **/
31 
32 #include <cppuhelper/implbase1.hxx>
33 #include <rtl/ref.hxx>
34 #include <tools/diagnose_ex.h>
35 
36 //......................................................................................................................
37 namespace dbaui
38 {
39 //......................................................................................................................
40 
41 	/** === begin UNO using === **/
42 	using ::com::sun::star::uno::Reference;
43 	using ::com::sun::star::uno::XInterface;
44 	using ::com::sun::star::uno::UNO_QUERY;
45 	using ::com::sun::star::uno::UNO_QUERY_THROW;
46 	using ::com::sun::star::uno::UNO_SET_THROW;
47 	using ::com::sun::star::uno::Exception;
48 	using ::com::sun::star::uno::RuntimeException;
49 	using ::com::sun::star::uno::Any;
50 	using ::com::sun::star::uno::makeAny;
51 	using ::com::sun::star::uno::Sequence;
52 	using ::com::sun::star::uno::Type;
53     using ::com::sun::star::util::XCloseable;
54     using ::com::sun::star::util::XCloseListener;
55     using ::com::sun::star::util::CloseVetoException;
56     using ::com::sun::star::lang::EventObject;
57 	/** === end UNO using === **/
58 
59 	//==================================================================================================================
60 	//= CloseListener_Impl
61 	//==================================================================================================================
62     typedef ::cppu::WeakImplHelper1 <   XCloseListener
63                                     >   CloseListener_Base;
64     class DBACCESS_DLLPRIVATE CloseListener_Impl : public CloseListener_Base
65     {
66     public:
CloseListener_Impl()67         CloseListener_Impl()
68             :m_bHasOwnership( false )
69         {
70         }
71 
72         // XCloseListener
73         virtual void SAL_CALL queryClosing( const EventObject& Source, ::sal_Bool GetsOwnership ) throw (CloseVetoException, RuntimeException);
74         virtual void SAL_CALL notifyClosing( const EventObject& Source ) throw (RuntimeException);
75 
76         // XEventListener
77         virtual void SAL_CALL disposing( const EventObject& Source) throw (RuntimeException);
78 
hasOwnership() const79         bool hasOwnership() const { return m_bHasOwnership; }
80 
81     protected:
~CloseListener_Impl()82         ~CloseListener_Impl()
83         {
84         }
85 
86     private:
87         bool    m_bHasOwnership;
88     };
89 
90     //------------------------------------------------------------------------------------------------------------------
queryClosing(const EventObject & i_source,::sal_Bool i_deliverOwnership)91     void SAL_CALL CloseListener_Impl::queryClosing( const EventObject& i_source, ::sal_Bool i_deliverOwnership ) throw (CloseVetoException, RuntimeException)
92     {
93         (void)i_source;
94 
95         if ( !m_bHasOwnership )
96             m_bHasOwnership = i_deliverOwnership;
97 
98         throw CloseVetoException();
99     }
100 
101     //------------------------------------------------------------------------------------------------------------------
notifyClosing(const EventObject & i_source)102     void SAL_CALL CloseListener_Impl::notifyClosing( const EventObject& i_source ) throw (RuntimeException)
103     {
104         (void)i_source;
105     }
106 
107     //------------------------------------------------------------------------------------------------------------------
disposing(const EventObject & i_source)108     void SAL_CALL CloseListener_Impl::disposing( const EventObject& i_source ) throw (RuntimeException)
109     {
110         (void)i_source;
111     }
112 
113 	//==================================================================================================================
114 	//= CloseVeto_Data
115 	//==================================================================================================================
116     struct DBACCESS_DLLPRIVATE CloseVeto_Data
117     {
118         Reference< XCloseable >                 xCloseable;
119         ::rtl::Reference< CloseListener_Impl >  pListener;
120     };
121 
122 	//==================================================================================================================
123 	//= operations
124 	//==================================================================================================================
125     namespace
126     {
127 	    //--------------------------------------------------------------------------------------------------------------
lcl_init(CloseVeto_Data & i_data,const Reference<XInterface> & i_closeable)128         void lcl_init( CloseVeto_Data& i_data, const Reference< XInterface >& i_closeable )
129         {
130             i_data.xCloseable.set( i_closeable, UNO_QUERY );
131             ENSURE_OR_RETURN_VOID( i_data.xCloseable.is(), "CloseVeto: the component is not closeable!" );
132 
133             i_data.pListener = new CloseListener_Impl;
134             i_data.xCloseable->addCloseListener( i_data.pListener.get() );
135         }
136 
137         //--------------------------------------------------------------------------------------------------------------
lcl_deinit(CloseVeto_Data & i_data)138         void lcl_deinit( CloseVeto_Data& i_data )
139         {
140             if ( !i_data.xCloseable.is() )
141                 return;
142 
143             i_data.xCloseable->removeCloseListener( i_data.pListener.get() );
144             if ( i_data.pListener->hasOwnership() )
145             {
146                 try
147                 {
148                     i_data.xCloseable->close( sal_True );
149                 }
150                 catch( const CloseVetoException& ) { }
151                 catch( const Exception& )
152                 {
153                 	DBG_UNHANDLED_EXCEPTION();
154                 }
155             }
156         }
157     }
158 
159 	//==================================================================================================================
160 	//= CloseVeto
161 	//==================================================================================================================
162 	//------------------------------------------------------------------------------------------------------------------
CloseVeto(const Reference<XInterface> & i_closeable)163     CloseVeto::CloseVeto( const Reference< XInterface >& i_closeable )
164         :m_pData( new CloseVeto_Data )
165     {
166         lcl_init( *m_pData, i_closeable );
167     }
168 
169 	//------------------------------------------------------------------------------------------------------------------
~CloseVeto()170     CloseVeto::~CloseVeto()
171     {
172         lcl_deinit( *m_pData );
173     }
174 
175 //......................................................................................................................
176 } // namespace dbaui
177 //......................................................................................................................
178