bump product version to 4.1.6.2
[LibreOffice.git] / unotools / source / misc / closeveto.cxx
blobe361f4a234a02627fcd0aba44babeb49070ec6ea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <unotools/closeveto.hxx>
22 #include <com/sun/star/util/XCloseable.hpp>
24 #include <cppuhelper/implbase1.hxx>
25 #include <rtl/ref.hxx>
26 #include <tools/diagnose_ex.h>
28 //......................................................................................................................
29 namespace utl
31 //......................................................................................................................
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;
49 //==================================================================================================================
50 //= CloseListener_Impl
51 //==================================================================================================================
52 typedef ::cppu::WeakImplHelper1 < XCloseListener
53 > CloseListener_Base;
54 class SAL_DLLPRIVATE CloseListener_Impl : public CloseListener_Base
56 public:
57 CloseListener_Impl()
58 :m_bHasOwnership( false )
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);
66 // XEventListener
67 virtual void SAL_CALL disposing( const EventObject& Source) throw (RuntimeException);
69 bool hasOwnership() const { return m_bHasOwnership; }
71 protected:
72 ~CloseListener_Impl()
76 private:
77 bool m_bHasOwnership;
80 //------------------------------------------------------------------------------------------------------------------
81 void SAL_CALL CloseListener_Impl::queryClosing( const EventObject& i_source, ::sal_Bool i_deliverOwnership ) throw (CloseVetoException, RuntimeException)
83 (void)i_source;
85 if ( !m_bHasOwnership )
86 m_bHasOwnership = i_deliverOwnership;
88 throw CloseVetoException();
91 //------------------------------------------------------------------------------------------------------------------
92 void SAL_CALL CloseListener_Impl::notifyClosing( const EventObject& i_source ) throw (RuntimeException)
94 (void)i_source;
97 //------------------------------------------------------------------------------------------------------------------
98 void SAL_CALL CloseListener_Impl::disposing( const EventObject& i_source ) throw (RuntimeException)
100 (void)i_source;
103 //==================================================================================================================
104 //= CloseVeto_Data
105 //==================================================================================================================
106 struct SAL_DLLPRIVATE CloseVeto_Data
108 Reference< XCloseable > xCloseable;
109 ::rtl::Reference< CloseListener_Impl > pListener;
112 //==================================================================================================================
113 //= operations
114 //==================================================================================================================
115 namespace
117 //--------------------------------------------------------------------------------------------------------------
118 void lcl_init( CloseVeto_Data& i_data, const Reference< XInterface >& i_closeable )
120 i_data.xCloseable.set( i_closeable, UNO_QUERY );
121 ENSURE_OR_RETURN_VOID( i_data.xCloseable.is(), "CloseVeto: the component is not closeable!" );
123 i_data.pListener = new CloseListener_Impl;
124 i_data.xCloseable->addCloseListener( i_data.pListener.get() );
127 //--------------------------------------------------------------------------------------------------------------
128 void lcl_deinit( CloseVeto_Data& i_data )
130 if ( !i_data.xCloseable.is() )
131 return;
133 i_data.xCloseable->removeCloseListener( i_data.pListener.get() );
134 if ( i_data.pListener->hasOwnership() )
138 i_data.xCloseable->close( sal_True );
140 catch( const CloseVetoException& ) { }
141 catch( const Exception& )
143 DBG_UNHANDLED_EXCEPTION();
149 //==================================================================================================================
150 //= CloseVeto
151 //==================================================================================================================
152 //------------------------------------------------------------------------------------------------------------------
153 CloseVeto::CloseVeto( const Reference< XInterface >& i_closeable )
154 :m_pData( new CloseVeto_Data )
156 lcl_init( *m_pData, i_closeable );
159 //------------------------------------------------------------------------------------------------------------------
160 CloseVeto::~CloseVeto()
162 lcl_deinit( *m_pData );
165 //......................................................................................................................
166 } // namespace utl
167 //......................................................................................................................
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */