Line data Source code
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 :
20 : #include <unotools/desktopterminationobserver.hxx>
21 :
22 : #include <com/sun/star/frame/XTerminateListener.hpp>
23 : #include <com/sun/star/frame/Desktop.hpp>
24 : #include <cppuhelper/implbase1.hxx>
25 : #include <comphelper/processfactory.hxx>
26 :
27 : #include <list>
28 :
29 : //........................................................................
30 : namespace utl
31 : {
32 : //........................................................................
33 :
34 : using namespace ::com::sun::star::uno;
35 : using namespace ::com::sun::star::lang;
36 : using namespace ::com::sun::star::frame;
37 :
38 : namespace
39 : {
40 : //................................................................
41 : typedef ::std::list< ITerminationListener* > Listeners;
42 :
43 2 : struct ListenerAdminData
44 : {
45 : Listeners aListeners;
46 : bool bAlreadyTerminated;
47 : bool bCreatedAdapter;
48 :
49 2 : ListenerAdminData() : bAlreadyTerminated( false ), bCreatedAdapter( false ) { }
50 : };
51 :
52 : //................................................................
53 72 : ListenerAdminData& getListenerAdminData()
54 : {
55 72 : static ListenerAdminData s_aData;
56 72 : return s_aData;
57 : }
58 :
59 : //================================================================
60 : //= OObserverImpl
61 : //================================================================
62 : class OObserverImpl : public ::cppu::WeakImplHelper1< XTerminateListener >
63 : {
64 : public:
65 : static void ensureObservation();
66 :
67 : protected:
68 : OObserverImpl();
69 : ~OObserverImpl();
70 :
71 : private:
72 : // XTerminateListener
73 : virtual void SAL_CALL queryTermination( const EventObject& Event ) throw (TerminationVetoException, RuntimeException);
74 : virtual void SAL_CALL notifyTermination( const EventObject& Event ) throw (RuntimeException);
75 :
76 : // XEventListener
77 : virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
78 : };
79 :
80 : //--------------------------------------------------------------------
81 2 : OObserverImpl::OObserverImpl()
82 : {
83 2 : }
84 :
85 : //--------------------------------------------------------------------
86 4 : OObserverImpl::~OObserverImpl()
87 : {
88 4 : }
89 :
90 : //--------------------------------------------------------------------
91 15 : void OObserverImpl::ensureObservation()
92 : {
93 : {
94 15 : if ( getListenerAdminData().bCreatedAdapter )
95 26 : return;
96 2 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
97 2 : if ( getListenerAdminData().bCreatedAdapter )
98 0 : return;
99 :
100 2 : getListenerAdminData().bCreatedAdapter = true;
101 : }
102 :
103 : try
104 : {
105 4 : Reference< XDesktop2 > xDesktop = Desktop::create( ::comphelper::getProcessComponentContext() );
106 2 : xDesktop->addTerminateListener( new OObserverImpl );
107 : }
108 0 : catch( const Exception& )
109 : {
110 : OSL_FAIL( "OObserverImpl::ensureObservation: caught an exception!" );
111 : }
112 : }
113 :
114 : //--------------------------------------------------------------------
115 2 : void SAL_CALL OObserverImpl::queryTermination( const EventObject& /*Event*/ ) throw (TerminationVetoException, RuntimeException)
116 : {
117 2 : Listeners aToNotify;
118 : {
119 2 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
120 2 : aToNotify = getListenerAdminData().aListeners;
121 : }
122 :
123 9 : for ( Listeners::const_iterator listener = aToNotify.begin();
124 6 : listener != aToNotify.end();
125 : ++listener
126 : )
127 : {
128 1 : if ( !(*listener)->queryTermination() )
129 0 : throw TerminationVetoException();
130 2 : }
131 2 : }
132 :
133 : //--------------------------------------------------------------------
134 2 : void SAL_CALL OObserverImpl::notifyTermination( const EventObject& /*Event*/ ) throw (RuntimeException)
135 : {
136 : // get the listeners
137 2 : Listeners aToNotify;
138 : {
139 2 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
140 : OSL_ENSURE( !getListenerAdminData().bAlreadyTerminated, "OObserverImpl::notifyTermination: terminated twice?" );
141 2 : aToNotify = getListenerAdminData().aListeners;
142 2 : getListenerAdminData().bAlreadyTerminated = true;
143 : }
144 :
145 : // notify the listeners
146 9 : for ( Listeners::const_iterator listener = aToNotify.begin();
147 6 : listener != aToNotify.end();
148 : ++listener
149 : )
150 : {
151 1 : (*listener)->notifyTermination();
152 : }
153 :
154 : // clear the listener container
155 : {
156 2 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
157 2 : getListenerAdminData().aListeners.clear();
158 2 : }
159 2 : }
160 :
161 : //--------------------------------------------------------------------
162 2 : void SAL_CALL OObserverImpl::disposing( const EventObject& /*Event*/ ) throw (RuntimeException)
163 : {
164 : #if OSL_DEBUG_LEVEL > 0
165 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
166 : OSL_ENSURE( getListenerAdminData().bAlreadyTerminated, "OObserverImpl::disposing: disposing without terminated?" );
167 : #endif
168 : // not interested in
169 2 : }
170 : }
171 :
172 : //====================================================================
173 : //= DesktopTerminationObserver
174 : //====================================================================
175 : //--------------------------------------------------------------------
176 15 : void DesktopTerminationObserver::registerTerminationListener( ITerminationListener* _pListener )
177 : {
178 15 : if ( !_pListener )
179 0 : return;
180 :
181 : {
182 15 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
183 15 : if ( getListenerAdminData().bAlreadyTerminated )
184 : {
185 0 : _pListener->notifyTermination();
186 0 : return;
187 : }
188 :
189 15 : getListenerAdminData().aListeners.push_back( _pListener );
190 : }
191 :
192 15 : OObserverImpl::ensureObservation();
193 : }
194 :
195 : //--------------------------------------------------------------------
196 15 : void DesktopTerminationObserver::revokeTerminationListener( ITerminationListener* _pListener )
197 : {
198 15 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
199 15 : Listeners& rListeners = getListenerAdminData().aListeners;
200 45 : for ( Listeners::iterator lookup = rListeners.begin();
201 30 : lookup != rListeners.end();
202 : ++lookup
203 : )
204 : {
205 14 : if ( *lookup == _pListener )
206 : {
207 14 : rListeners.erase( lookup );
208 14 : break;
209 : }
210 15 : }
211 15 : }
212 :
213 : //........................................................................
214 : } // namespace utl
215 : //........................................................................
216 :
217 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|