LCOV - code coverage report
Current view: top level - unotools/source/misc - desktopterminationobserver.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 62 0.0 %
Date: 2014-04-14 Functions: 0 12 0.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           0 :         struct ListenerAdminData
      42             :         {
      43             :             Listeners   aListeners;
      44             :             bool        bAlreadyTerminated;
      45             :             bool        bCreatedAdapter;
      46             : 
      47           0 :             ListenerAdminData() : bAlreadyTerminated( false ), bCreatedAdapter( false ) { }
      48             :         };
      49             : 
      50           0 :         ListenerAdminData& getListenerAdminData()
      51             :         {
      52           0 :             static ListenerAdminData s_aData;
      53           0 :             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           0 :         OObserverImpl::OObserverImpl()
      77             :         {
      78           0 :         }
      79             : 
      80           0 :         OObserverImpl::~OObserverImpl()
      81             :         {
      82           0 :         }
      83             : 
      84           0 :         void OObserverImpl::ensureObservation()
      85             :         {
      86             :             {
      87           0 :                 if ( getListenerAdminData().bCreatedAdapter )
      88           0 :                     return;
      89           0 :                 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
      90           0 :                 if ( getListenerAdminData().bCreatedAdapter )
      91           0 :                     return;
      92             : 
      93           0 :                 getListenerAdminData().bCreatedAdapter = true;
      94             :             }
      95             : 
      96             :             try
      97             :             {
      98           0 :                 Reference< XDesktop2 > xDesktop = Desktop::create( ::comphelper::getProcessComponentContext() );
      99           0 :                 xDesktop->addTerminateListener( new OObserverImpl );
     100             :             }
     101           0 :             catch( const Exception& )
     102             :             {
     103             :                 OSL_FAIL( "OObserverImpl::ensureObservation: caught an exception!" );
     104             :             }
     105             :         }
     106             : 
     107           0 :         void SAL_CALL OObserverImpl::queryTermination( const EventObject& /*Event*/ ) throw (TerminationVetoException, RuntimeException, std::exception)
     108             :         {
     109           0 :             Listeners aToNotify;
     110             :             {
     111           0 :                 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
     112           0 :                 aToNotify = getListenerAdminData().aListeners;
     113             :             }
     114             : 
     115           0 :             for ( Listeners::const_iterator listener = aToNotify.begin();
     116           0 :                   listener != aToNotify.end();
     117             :                   ++listener
     118             :                 )
     119             :             {
     120           0 :                 if ( !(*listener)->queryTermination() )
     121           0 :                     throw TerminationVetoException();
     122           0 :             }
     123           0 :         }
     124             : 
     125           0 :         void SAL_CALL OObserverImpl::notifyTermination( const EventObject& /*Event*/ ) throw (RuntimeException, std::exception)
     126             :         {
     127             :             // get the listeners
     128           0 :             Listeners aToNotify;
     129             :             {
     130           0 :                 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
     131             :                 OSL_ENSURE( !getListenerAdminData().bAlreadyTerminated, "OObserverImpl::notifyTermination: terminated twice?" );
     132           0 :                 aToNotify = getListenerAdminData().aListeners;
     133           0 :                 getListenerAdminData().bAlreadyTerminated = true;
     134             :             }
     135             : 
     136             :             // notify the listeners
     137           0 :             for ( Listeners::const_iterator listener = aToNotify.begin();
     138           0 :                   listener != aToNotify.end();
     139             :                   ++listener
     140             :                 )
     141             :             {
     142           0 :                 (*listener)->notifyTermination();
     143             :             }
     144             : 
     145             :             // clear the listener container
     146             :             {
     147           0 :                 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
     148           0 :                 getListenerAdminData().aListeners.clear();
     149           0 :             }
     150           0 :         }
     151             : 
     152           0 :         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           0 :         }
     160             :     }
     161             : 
     162             :     //= DesktopTerminationObserver
     163             : 
     164           0 :     void DesktopTerminationObserver::registerTerminationListener( ITerminationListener* _pListener )
     165             :     {
     166           0 :         if ( !_pListener )
     167           0 :             return;
     168             : 
     169             :         {
     170           0 :             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
     171           0 :             if ( getListenerAdminData().bAlreadyTerminated )
     172             :             {
     173           0 :                 _pListener->notifyTermination();
     174           0 :                 return;
     175             :             }
     176             : 
     177           0 :             getListenerAdminData().aListeners.push_back( _pListener );
     178             :         }
     179             : 
     180           0 :         OObserverImpl::ensureObservation();
     181             :     }
     182             : 
     183           0 :     void DesktopTerminationObserver::revokeTerminationListener( ITerminationListener* _pListener )
     184             :     {
     185           0 :         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
     186           0 :         Listeners& rListeners = getListenerAdminData().aListeners;
     187           0 :         for ( Listeners::iterator lookup = rListeners.begin();
     188           0 :               lookup != rListeners.end();
     189             :               ++lookup
     190             :               )
     191             :         {
     192           0 :             if ( *lookup == _pListener )
     193             :             {
     194           0 :                 rListeners.erase( lookup );
     195           0 :                 break;
     196             :             }
     197           0 :         }
     198           0 :     }
     199             : 
     200             : } // namespace utl
     201             : 
     202             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10