Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / unotools / source / misc / desktopterminationobserver.cxx
blob138125a4fc102a4de472e55fb853bd0eec58c8d0
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/desktopterminationobserver.hxx>
22 #include <com/sun/star/frame/TerminationVetoException.hpp>
23 #include <com/sun/star/frame/XTerminateListener.hpp>
24 #include <com/sun/star/frame/Desktop.hpp>
25 #include <cppuhelper/implbase.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <osl/diagnose.h>
28 #include <comphelper/diagnose_ex.hxx>
30 #include <vector>
32 namespace utl
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::frame;
39 namespace
42 typedef ::std::vector< ITerminationListener* > Listeners;
44 struct ListenerAdminData
46 Listeners aListeners;
47 bool bAlreadyTerminated;
48 bool bCreatedAdapter;
50 ListenerAdminData() : bAlreadyTerminated( false ), bCreatedAdapter( false ) { }
53 ListenerAdminData& getListenerAdminData()
55 static ListenerAdminData s_aData;
56 return s_aData;
59 //= OObserverImpl
61 class OObserverImpl : public ::cppu::WeakImplHelper< XTerminateListener >
63 public:
64 static void ensureObservation();
66 private:
67 OObserverImpl();
68 virtual ~OObserverImpl() override;
70 // XTerminateListener
71 virtual void SAL_CALL queryTermination( const EventObject& Event ) override;
72 virtual void SAL_CALL notifyTermination( const EventObject& Event ) override;
74 // XEventListener
75 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
78 OObserverImpl::OObserverImpl()
82 OObserverImpl::~OObserverImpl()
86 void OObserverImpl::ensureObservation()
89 if ( getListenerAdminData().bCreatedAdapter )
90 return;
91 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
92 if ( getListenerAdminData().bCreatedAdapter )
93 return;
95 getListenerAdminData().bCreatedAdapter = true;
98 try
100 Reference< XDesktop2 > xDesktop = Desktop::create( ::comphelper::getProcessComponentContext() );
101 xDesktop->addTerminateListener( new OObserverImpl );
103 catch( const Exception& )
105 TOOLS_WARN_EXCEPTION( "unotools", "OObserverImpl::ensureObservation" );
109 void SAL_CALL OObserverImpl::queryTermination( const EventObject& /*Event*/ )
111 Listeners aToNotify;
113 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
114 aToNotify = getListenerAdminData().aListeners;
117 for (auto const& listener : aToNotify)
119 if ( !listener->queryTermination() )
120 throw TerminationVetoException();
124 void SAL_CALL OObserverImpl::notifyTermination( const EventObject& /*Event*/ )
126 // get the listeners
127 Listeners aToNotify;
129 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
130 OSL_ENSURE( !getListenerAdminData().bAlreadyTerminated, "OObserverImpl::notifyTermination: terminated twice?" );
131 aToNotify = getListenerAdminData().aListeners;
132 getListenerAdminData().bAlreadyTerminated = true;
135 // notify the listeners
136 for (auto const& listener : aToNotify)
138 listener->notifyTermination();
141 // clear the listener container
143 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
144 getListenerAdminData().aListeners.clear();
148 void SAL_CALL OObserverImpl::disposing( const EventObject& /*Event*/ )
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
158 //= DesktopTerminationObserver
160 void DesktopTerminationObserver::registerTerminationListener( ITerminationListener* _pListener )
162 if ( !_pListener )
163 return;
166 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
167 if ( getListenerAdminData().bAlreadyTerminated )
169 _pListener->notifyTermination();
170 return;
173 getListenerAdminData().aListeners.push_back( _pListener );
176 OObserverImpl::ensureObservation();
179 void DesktopTerminationObserver::revokeTerminationListener( ITerminationListener const * _pListener )
181 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
182 Listeners& rListeners = getListenerAdminData().aListeners;
183 rListeners.erase(std::remove(rListeners.begin(), rListeners.end(), _pListener), rListeners.end());
186 } // namespace utl
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */