LCOV - code coverage report
Current view: top level - unotools/source/misc - desktopterminationobserver.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 54 62 87.1 %
Date: 2014-11-03 Functions: 12 12 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10