LibreOffice Module unotools (master) 1
desktopterminationobserver.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/frame/TerminationVetoException.hpp>
23#include <com/sun/star/frame/XTerminateListener.hpp>
24#include <com/sun/star/frame/Desktop.hpp>
27#include <osl/diagnose.h>
29
30#include <vector>
31
32namespace utl
33{
34
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::frame;
38
39 namespace
40 {
41
42 typedef ::std::vector< ITerminationListener* > Listeners;
43
44 struct ListenerAdminData
45 {
46 Listeners aListeners;
49
50 ListenerAdminData() : bAlreadyTerminated( false ), bCreatedAdapter( false ) { }
51 };
52
53 ListenerAdminData& getListenerAdminData()
54 {
55 static ListenerAdminData s_aData;
56 return s_aData;
57 }
58
59 //= OObserverImpl
60
61 class OObserverImpl : public ::cppu::WeakImplHelper< XTerminateListener >
62 {
63 public:
64 static void ensureObservation();
65
66 private:
67 OObserverImpl();
68 virtual ~OObserverImpl() override;
69
70 // XTerminateListener
71 virtual void SAL_CALL queryTermination( const EventObject& Event ) override;
72 virtual void SAL_CALL notifyTermination( const EventObject& Event ) override;
73
74 // XEventListener
75 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
76 };
77
78 OObserverImpl::OObserverImpl()
79 {
80 }
81
82 OObserverImpl::~OObserverImpl()
83 {
84 }
85
86 void OObserverImpl::ensureObservation()
87 {
88 {
89 if ( getListenerAdminData().bCreatedAdapter )
90 return;
91 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
92 if ( getListenerAdminData().bCreatedAdapter )
93 return;
94
95 getListenerAdminData().bCreatedAdapter = true;
96 }
97
98 try
99 {
100 Reference< XDesktop2 > xDesktop = Desktop::create( ::comphelper::getProcessComponentContext() );
101 xDesktop->addTerminateListener( new OObserverImpl );
102 }
103 catch( const Exception& )
104 {
105 TOOLS_WARN_EXCEPTION( "unotools", "OObserverImpl::ensureObservation" );
106 }
107 }
108
109 void SAL_CALL OObserverImpl::queryTermination( const EventObject& /*Event*/ )
110 {
111 Listeners aToNotify;
112 {
113 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
114 aToNotify = getListenerAdminData().aListeners;
115 }
116
117 for (auto const& listener : aToNotify)
118 {
119 if ( !listener->queryTermination() )
120 throw TerminationVetoException();
121 }
122 }
123
124 void SAL_CALL OObserverImpl::notifyTermination( const EventObject& /*Event*/ )
125 {
126 // get the listeners
127 Listeners aToNotify;
128 {
129 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
130 OSL_ENSURE( !getListenerAdminData().bAlreadyTerminated, "OObserverImpl::notifyTermination: terminated twice?" );
131 aToNotify = getListenerAdminData().aListeners;
132 getListenerAdminData().bAlreadyTerminated = true;
133 }
134
135 // notify the listeners
136 for (auto const& listener : aToNotify)
137 {
138 listener->notifyTermination();
139 }
140
141 // clear the listener container
142 {
143 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
144 getListenerAdminData().aListeners.clear();
145 }
146 }
147
148 void SAL_CALL OObserverImpl::disposing( const EventObject& /*Event*/ )
149 {
150#if OSL_DEBUG_LEVEL > 0
151 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
152 OSL_ENSURE( getListenerAdminData().bAlreadyTerminated, "OObserverImpl::disposing: disposing without terminated?" );
153#endif
154 // not interested in
155 }
156 }
157
158 //= DesktopTerminationObserver
159
160 void DesktopTerminationObserver::registerTerminationListener( ITerminationListener* _pListener )
161 {
162 if ( !_pListener )
163 return;
164
165 {
166 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
167 if ( getListenerAdminData().bAlreadyTerminated )
168 {
169 _pListener->notifyTermination();
170 return;
171 }
172
173 getListenerAdminData().aListeners.push_back( _pListener );
174 }
175
176 OObserverImpl::ensureObservation();
177 }
178
179 void DesktopTerminationObserver::revokeTerminationListener( ITerminationListener const * _pListener )
180 {
181 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
182 Listeners& rListeners = getListenerAdminData().aListeners;
183 rListeners.erase(std::remove(rListeners.begin(), rListeners.end(), _pListener), rListeners.end());
184 }
185
186} // namespace utl
187
188/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool bAlreadyTerminated
bool bCreatedAdapter
Listeners aListeners
#define TOOLS_WARN_EXCEPTION(area, stream)