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

Generated by: LCOV version 1.11