1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_unotools.hxx"
30*cdf0e10cSrcweir #include <unotools/desktopterminationobserver.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir /** === begin UNO includes === **/
33*cdf0e10cSrcweir #include <com/sun/star/frame/XTerminateListener.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
35*cdf0e10cSrcweir /** === end UNO includes === **/
36*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
37*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <list>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir //........................................................................
42*cdf0e10cSrcweir namespace utl
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir //........................................................................
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
47*cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
48*cdf0e10cSrcweir     using namespace ::com::sun::star::frame;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir     namespace
51*cdf0e10cSrcweir     {
52*cdf0e10cSrcweir         //................................................................
53*cdf0e10cSrcweir         typedef ::std::list< ITerminationListener* > Listeners;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir         struct ListenerAdminData
56*cdf0e10cSrcweir         {
57*cdf0e10cSrcweir             Listeners   aListeners;
58*cdf0e10cSrcweir             bool        bAlreadyTerminated;
59*cdf0e10cSrcweir             bool        bCreatedAdapter;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir             ListenerAdminData() : bAlreadyTerminated( false ), bCreatedAdapter( false ) { }
62*cdf0e10cSrcweir         };
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir         //................................................................
65*cdf0e10cSrcweir         ListenerAdminData& getListenerAdminData()
66*cdf0e10cSrcweir         {
67*cdf0e10cSrcweir             static ListenerAdminData s_aData;
68*cdf0e10cSrcweir             return s_aData;
69*cdf0e10cSrcweir         }
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir         //================================================================
72*cdf0e10cSrcweir 	    //= OObserverImpl
73*cdf0e10cSrcweir 	    //================================================================
74*cdf0e10cSrcweir         class OObserverImpl : public ::cppu::WeakImplHelper1< XTerminateListener >
75*cdf0e10cSrcweir         {
76*cdf0e10cSrcweir         public:
77*cdf0e10cSrcweir             static void ensureObservation();
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir         protected:
80*cdf0e10cSrcweir             OObserverImpl();
81*cdf0e10cSrcweir             ~OObserverImpl();
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir         private:
84*cdf0e10cSrcweir             // XTerminateListener
85*cdf0e10cSrcweir             virtual void SAL_CALL queryTermination( const EventObject& Event ) throw (TerminationVetoException, RuntimeException);
86*cdf0e10cSrcweir             virtual void SAL_CALL notifyTermination( const EventObject& Event ) throw (RuntimeException);
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir             // XEventListener
89*cdf0e10cSrcweir             virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
90*cdf0e10cSrcweir         };
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         //--------------------------------------------------------------------
93*cdf0e10cSrcweir         OObserverImpl::OObserverImpl()
94*cdf0e10cSrcweir         {
95*cdf0e10cSrcweir         }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir         //--------------------------------------------------------------------
98*cdf0e10cSrcweir         OObserverImpl::~OObserverImpl()
99*cdf0e10cSrcweir         {
100*cdf0e10cSrcweir         }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir         //--------------------------------------------------------------------
103*cdf0e10cSrcweir         void OObserverImpl::ensureObservation()
104*cdf0e10cSrcweir         {
105*cdf0e10cSrcweir             {
106*cdf0e10cSrcweir                 if ( getListenerAdminData().bCreatedAdapter )
107*cdf0e10cSrcweir                     return;
108*cdf0e10cSrcweir                 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
109*cdf0e10cSrcweir                 if ( getListenerAdminData().bCreatedAdapter )
110*cdf0e10cSrcweir                     return;
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir                 getListenerAdminData().bCreatedAdapter = true;
113*cdf0e10cSrcweir             }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir             try
116*cdf0e10cSrcweir             {
117*cdf0e10cSrcweir                 Reference< XDesktop > xDesktop;
118*cdf0e10cSrcweir                 xDesktop = xDesktop.query( ::comphelper::getProcessServiceFactory()->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ) ) );
119*cdf0e10cSrcweir                 OSL_ENSURE( xDesktop.is(), "OObserverImpl::ensureObservation: could not ensureObservation the desktop!" );
120*cdf0e10cSrcweir                 if ( xDesktop.is() )
121*cdf0e10cSrcweir                     xDesktop->addTerminateListener( new OObserverImpl );
122*cdf0e10cSrcweir             }
123*cdf0e10cSrcweir             catch( const Exception& )
124*cdf0e10cSrcweir             {
125*cdf0e10cSrcweir             	OSL_ENSURE( sal_False, "OObserverImpl::ensureObservation: caught an exception!" );
126*cdf0e10cSrcweir             }
127*cdf0e10cSrcweir         }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         //--------------------------------------------------------------------
130*cdf0e10cSrcweir         void SAL_CALL OObserverImpl::queryTermination( const EventObject& /*Event*/ ) throw (TerminationVetoException, RuntimeException)
131*cdf0e10cSrcweir         {
132*cdf0e10cSrcweir             Listeners aToNotify;
133*cdf0e10cSrcweir             {
134*cdf0e10cSrcweir                 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
135*cdf0e10cSrcweir                 aToNotify = getListenerAdminData().aListeners;
136*cdf0e10cSrcweir             }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir             for ( Listeners::const_iterator listener = aToNotify.begin();
139*cdf0e10cSrcweir                   listener != aToNotify.end();
140*cdf0e10cSrcweir                   ++listener
141*cdf0e10cSrcweir                 )
142*cdf0e10cSrcweir             {
143*cdf0e10cSrcweir                 if ( !(*listener)->queryTermination() )
144*cdf0e10cSrcweir                     throw TerminationVetoException();
145*cdf0e10cSrcweir             }
146*cdf0e10cSrcweir         }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir         //--------------------------------------------------------------------
149*cdf0e10cSrcweir         void SAL_CALL OObserverImpl::notifyTermination( const EventObject& /*Event*/ ) throw (RuntimeException)
150*cdf0e10cSrcweir         {
151*cdf0e10cSrcweir             // get the listeners
152*cdf0e10cSrcweir             Listeners aToNotify;
153*cdf0e10cSrcweir             {
154*cdf0e10cSrcweir                 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
155*cdf0e10cSrcweir                 OSL_ENSURE( !getListenerAdminData().bAlreadyTerminated, "OObserverImpl::notifyTermination: terminated twice?" );
156*cdf0e10cSrcweir                 aToNotify = getListenerAdminData().aListeners;
157*cdf0e10cSrcweir                 getListenerAdminData().bAlreadyTerminated = true;
158*cdf0e10cSrcweir             }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir             // notify the listeners
161*cdf0e10cSrcweir             for ( Listeners::const_iterator listener = aToNotify.begin();
162*cdf0e10cSrcweir                   listener != aToNotify.end();
163*cdf0e10cSrcweir                   ++listener
164*cdf0e10cSrcweir                 )
165*cdf0e10cSrcweir             {
166*cdf0e10cSrcweir                 (*listener)->notifyTermination();
167*cdf0e10cSrcweir             }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir             // clear the listener container
170*cdf0e10cSrcweir             {
171*cdf0e10cSrcweir                 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
172*cdf0e10cSrcweir                 getListenerAdminData().aListeners.clear();
173*cdf0e10cSrcweir             }
174*cdf0e10cSrcweir         }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir         //--------------------------------------------------------------------
177*cdf0e10cSrcweir         void SAL_CALL OObserverImpl::disposing( const EventObject& /*Event*/ ) throw (RuntimeException)
178*cdf0e10cSrcweir         {
179*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
180*cdf0e10cSrcweir             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
181*cdf0e10cSrcweir             OSL_ENSURE( getListenerAdminData().bAlreadyTerminated, "OObserverImpl::disposing: disposing without terminated?" );
182*cdf0e10cSrcweir #endif
183*cdf0e10cSrcweir             // not interested in
184*cdf0e10cSrcweir         }
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 	//====================================================================
188*cdf0e10cSrcweir 	//= DesktopTerminationObserver
189*cdf0e10cSrcweir 	//====================================================================
190*cdf0e10cSrcweir 	//--------------------------------------------------------------------
191*cdf0e10cSrcweir     void DesktopTerminationObserver::registerTerminationListener( ITerminationListener* _pListener )
192*cdf0e10cSrcweir     {
193*cdf0e10cSrcweir         if ( !_pListener )
194*cdf0e10cSrcweir             return;
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir         {
197*cdf0e10cSrcweir             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
198*cdf0e10cSrcweir             if ( getListenerAdminData().bAlreadyTerminated )
199*cdf0e10cSrcweir             {
200*cdf0e10cSrcweir                 _pListener->notifyTermination();
201*cdf0e10cSrcweir                 return;
202*cdf0e10cSrcweir             }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir             getListenerAdminData().aListeners.push_back( _pListener );
205*cdf0e10cSrcweir         }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir         OObserverImpl::ensureObservation();
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 	//--------------------------------------------------------------------
211*cdf0e10cSrcweir     void DesktopTerminationObserver::revokeTerminationListener( ITerminationListener* _pListener )
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
214*cdf0e10cSrcweir         Listeners& rListeners = getListenerAdminData().aListeners;
215*cdf0e10cSrcweir         for ( Listeners::iterator lookup = rListeners.begin();
216*cdf0e10cSrcweir               lookup != rListeners.end();
217*cdf0e10cSrcweir               ++lookup
218*cdf0e10cSrcweir               )
219*cdf0e10cSrcweir         {
220*cdf0e10cSrcweir             if ( *lookup == _pListener )
221*cdf0e10cSrcweir             {
222*cdf0e10cSrcweir                 rListeners.erase( lookup );
223*cdf0e10cSrcweir                 break;
224*cdf0e10cSrcweir             }
225*cdf0e10cSrcweir         }
226*cdf0e10cSrcweir     }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir //........................................................................
229*cdf0e10cSrcweir } // namespace utl
230*cdf0e10cSrcweir //........................................................................
231*cdf0e10cSrcweir 
232