LibreOffice Module unotools (master) 1
closeveto.cxx
Go to the documentation of this file.
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
21
22#include <com/sun/star/util/CloseVetoException.hpp>
23#include <com/sun/star/util/XCloseable.hpp>
24
26#include <rtl/ref.hxx>
28
29namespace utl
30{
31
32 using ::com::sun::star::uno::Reference;
33 using ::com::sun::star::uno::XInterface;
34 using ::com::sun::star::uno::UNO_QUERY;
35 using ::com::sun::star::uno::Exception;
36 using ::com::sun::star::util::XCloseable;
37 using ::com::sun::star::util::XCloseListener;
38 using ::com::sun::star::util::CloseVetoException;
39 using ::com::sun::star::lang::EventObject;
40
41 //= CloseListener_Impl
42
43 typedef ::cppu::WeakImplHelper < XCloseListener
45
46 namespace {
47
48 class CloseListener_Impl : public CloseListener_Base
49 {
50 public:
51 explicit CloseListener_Impl(bool const bHasOwnership)
52 : m_bHasOwnership(bHasOwnership)
53 {
54 }
55
56 // XCloseListener
57 virtual void SAL_CALL queryClosing( const EventObject& Source, sal_Bool GetsOwnership ) override;
58 virtual void SAL_CALL notifyClosing( const EventObject& Source ) override;
59
60 // XEventListener
61 virtual void SAL_CALL disposing( const EventObject& Source) override;
62
63 bool hasOwnership() const { return m_bHasOwnership; }
64
65 protected:
66 virtual ~CloseListener_Impl() override
67 {
68 }
69
70 private:
72 };
73
74 }
75
76 void SAL_CALL CloseListener_Impl::queryClosing( const EventObject&, sal_Bool i_deliverOwnership )
77 {
78 if ( !m_bHasOwnership )
79 m_bHasOwnership = i_deliverOwnership;
80
81 throw CloseVetoException();
82 }
83
84 void SAL_CALL CloseListener_Impl::notifyClosing( const EventObject& ) {}
85
86 void SAL_CALL CloseListener_Impl::disposing( const EventObject& ) {}
87
88 //= CloseVeto_Data
89
91 {
92 Reference< XCloseable > xCloseable;
94 };
95
96 //= operations
97
98 namespace
99 {
100
101 void lcl_init( CloseVeto_Data& i_data, const Reference< XInterface >& i_closeable,
102 bool const hasOwnership)
103 {
104 i_data.xCloseable.set( i_closeable, UNO_QUERY );
105 ENSURE_OR_RETURN_VOID( i_data.xCloseable.is(), "CloseVeto: the component is not closeable!" );
106
107 i_data.pListener = new CloseListener_Impl(hasOwnership);
108 i_data.xCloseable->addCloseListener( i_data.pListener );
109 }
110
111 void lcl_deinit( CloseVeto_Data const & i_data )
112 {
113 if ( !i_data.xCloseable.is() )
114 return;
115
116 i_data.xCloseable->removeCloseListener( i_data.pListener );
117 if ( i_data.pListener->hasOwnership() )
118 {
119 try
120 {
121 i_data.xCloseable->close( true );
122 }
123 catch( const CloseVetoException& ) { }
124 catch( const Exception& )
125 {
126 DBG_UNHANDLED_EXCEPTION("unotools");
127 }
128 }
129 }
130 }
131
132 //= CloseVeto
133 CloseVeto::CloseVeto(const Reference< XInterface >& i_closeable,
134 bool const hasOwnership)
135 : m_xData(new CloseVeto_Data)
136 {
137 lcl_init(*m_xData, i_closeable, hasOwnership);
138 }
139
141 {
142 lcl_deinit(*m_xData);
143 }
144
145} // namespace utl
146
147/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
CloseVeto(const css::uno::Reference< css::uno::XInterface > &i_closeable, bool bHasOwnership=false)
Definition: closeveto.cxx:133
std::unique_ptr< CloseVeto_Data > m_xData
Definition: closeveto.hxx:48
bool m_bHasOwnership
Definition: closeveto.cxx:71
#define DBG_UNHANDLED_EXCEPTION(...)
#define ENSURE_OR_RETURN_VOID(c, m)
::cppu::WeakImplHelper< XCloseListener > CloseListener_Base
Definition: closeveto.cxx:44
::rtl::Reference< CloseListener_Impl > pListener
Definition: closeveto.cxx:93
Reference< XCloseable > xCloseable
Definition: closeveto.cxx:92
unsigned char sal_Bool