1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::frame
;
42 typedef ::std::vector
< ITerminationListener
* > Listeners
;
44 struct ListenerAdminData
47 bool bAlreadyTerminated
;
50 ListenerAdminData() : bAlreadyTerminated( false ), bCreatedAdapter( false ) { }
53 ListenerAdminData
& getListenerAdminData()
55 static ListenerAdminData s_aData
;
61 class OObserverImpl
: public ::cppu::WeakImplHelper
< XTerminateListener
>
64 static void ensureObservation();
68 virtual ~OObserverImpl() override
;
71 virtual void SAL_CALL
queryTermination( const EventObject
& Event
) override
;
72 virtual void SAL_CALL
notifyTermination( const EventObject
& Event
) override
;
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
)
91 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
92 if ( getListenerAdminData().bCreatedAdapter
)
95 getListenerAdminData().bCreatedAdapter
= true;
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*/ )
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*/ )
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?" );
158 //= DesktopTerminationObserver
160 void DesktopTerminationObserver::registerTerminationListener( ITerminationListener
* _pListener
)
166 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
167 if ( getListenerAdminData().bAlreadyTerminated
)
169 _pListener
->notifyTermination();
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());
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */