LCOV - code coverage report
Current view: top level - basctl/source/basicide - doceventnotifier.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 0 69 0.0 %
Date: 2014-04-11 Functions: 0 15 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 "doceventnotifier.hxx"
      21             : #include "scriptdocument.hxx"
      22             : 
      23             : #include <com/sun/star/frame/theGlobalEventBroadcaster.hpp>
      24             : #include <com/sun/star/document/XDocumentEventBroadcaster.hpp>
      25             : 
      26             : #include <vcl/svapp.hxx>
      27             : 
      28             : #include <tools/diagnose_ex.h>
      29             : 
      30             : #include <comphelper/processfactory.hxx>
      31             : 
      32             : #include <osl/mutex.hxx>
      33             : #include <sal/macros.h>
      34             : 
      35             : #include <cppuhelper/compbase1.hxx>
      36             : #include <cppuhelper/basemutex.hxx>
      37             : 
      38             : namespace basctl
      39             : {
      40             : 
      41             :     using ::com::sun::star::document::XDocumentEventBroadcaster;
      42             :     using ::com::sun::star::document::XDocumentEventListener;
      43             :     using ::com::sun::star::document::DocumentEvent;
      44             :     using ::com::sun::star::uno::XComponentContext;
      45             :     using ::com::sun::star::uno::RuntimeException;
      46             :     using ::com::sun::star::uno::Reference;
      47             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      48             :     using ::com::sun::star::uno::Exception;
      49             :     using ::com::sun::star::frame::XModel;
      50             :     using ::com::sun::star::frame::theGlobalEventBroadcaster;
      51             :     using ::com::sun::star::uno::UNO_QUERY;
      52             : 
      53             :     namespace csslang = ::com::sun::star::lang;
      54             : 
      55             :     // DocumentEventNotifier::Impl
      56             : 
      57             :     typedef ::cppu::WeakComponentImplHelper1    <   XDocumentEventListener
      58             :                                                 >   DocumentEventNotifier_Impl_Base;
      59             : 
      60             :     enum ListenerAction
      61             :     {
      62             :         RegisterListener,
      63             :         RemoveListener
      64             :     };
      65             : 
      66             :     /** impl class for DocumentEventNotifier
      67             :     */
      68             :     class DocumentEventNotifier::Impl   :public ::boost::noncopyable
      69             :                                         ,public ::cppu::BaseMutex
      70             :                                         ,public DocumentEventNotifier_Impl_Base
      71             :     {
      72             :     public:
      73             :         Impl (DocumentEventListener&, Reference<XModel> const& rxDocument);
      74             :         virtual ~Impl ();
      75             : 
      76             :         // XDocumentEventListener
      77             :         virtual void SAL_CALL documentEventOccured( const DocumentEvent& Event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      78             : 
      79             :         // XEventListener
      80             :         virtual void SAL_CALL disposing( const csslang::EventObject& Event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      81             : 
      82             :         // ComponentHelper
      83             :         virtual void SAL_CALL disposing() SAL_OVERRIDE;
      84             : 
      85             :     private:
      86             :         /// determines whether the instance is already disposed
      87           0 :         bool    impl_isDisposed_nothrow() const { return m_pListener == NULL; }
      88             : 
      89             :         /// disposes the instance
      90             :         void    impl_dispose_nothrow();
      91             : 
      92             :         /// registers or revokes the instance as listener at the global event broadcaster
      93             :         void    impl_listenerAction_nothrow( ListenerAction _eAction );
      94             : 
      95             :     private:
      96             :         DocumentEventListener*  m_pListener;
      97             :         Reference< XModel >     m_xModel;
      98             :     };
      99             : 
     100           0 :     DocumentEventNotifier::Impl::Impl (DocumentEventListener& rListener, Reference<XModel> const& rxDocument) :
     101             :         DocumentEventNotifier_Impl_Base(m_aMutex),
     102             :         m_pListener(&rListener),
     103           0 :         m_xModel(rxDocument)
     104             :     {
     105           0 :         osl_atomic_increment( &m_refCount );
     106           0 :         impl_listenerAction_nothrow( RegisterListener );
     107           0 :         osl_atomic_decrement( &m_refCount );
     108           0 :     }
     109             : 
     110           0 :     DocumentEventNotifier::Impl::~Impl ()
     111             :     {
     112           0 :         if ( !impl_isDisposed_nothrow() )
     113             :         {
     114           0 :             acquire();
     115           0 :             dispose();
     116             :         }
     117           0 :     }
     118             : 
     119           0 :     void SAL_CALL DocumentEventNotifier::Impl::documentEventOccured( const DocumentEvent& _rEvent ) throw (RuntimeException, std::exception)
     120             :     {
     121           0 :         ::osl::ClearableMutexGuard aGuard( m_aMutex );
     122             : 
     123             :         OSL_PRECOND( !impl_isDisposed_nothrow(), "DocumentEventNotifier::Impl::notifyEvent: disposed, but still getting events?" );
     124           0 :         if ( impl_isDisposed_nothrow() )
     125           0 :             return;
     126             : 
     127           0 :         Reference< XModel > xDocument( _rEvent.Source, UNO_QUERY );
     128             :         OSL_ENSURE( xDocument.is(), "DocumentEventNotifier::Impl::notifyEvent: illegal source document!" );
     129           0 :         if ( !xDocument.is() )
     130           0 :             return;
     131             : 
     132             :         struct EventEntry
     133             :         {
     134             :             const sal_Char* pEventName;
     135             :             void (DocumentEventListener::*listenerMethod)( const ScriptDocument& _rDocument );
     136             :         };
     137             :         EventEntry aEvents[] = {
     138             :             { "OnNew",          &DocumentEventListener::onDocumentCreated },
     139             :             { "OnLoad",         &DocumentEventListener::onDocumentOpened },
     140             :             { "OnSave",         &DocumentEventListener::onDocumentSave },
     141             :             { "OnSaveDone",     &DocumentEventListener::onDocumentSaveDone },
     142             :             { "OnSaveAs",       &DocumentEventListener::onDocumentSaveAs },
     143             :             { "OnSaveAsDone",   &DocumentEventListener::onDocumentSaveAsDone },
     144             :             { "OnUnload",       &DocumentEventListener::onDocumentClosed },
     145             :             { "OnTitleChanged", &DocumentEventListener::onDocumentTitleChanged },
     146             :             { "OnModeChanged",  &DocumentEventListener::onDocumentModeChanged }
     147           0 :         };
     148             : 
     149           0 :         for ( size_t i=0; i < SAL_N_ELEMENTS( aEvents ); ++i )
     150             :         {
     151           0 :             if ( !_rEvent.EventName.equalsAscii( aEvents[i].pEventName ) )
     152           0 :                 continue;
     153             : 
     154           0 :             ScriptDocument aDocument( xDocument );
     155             :             {
     156             :                 // the listener implementations usually require the SolarMutex, so lock it here.
     157             :                 // But ensure the proper order of locking the solar and the own mutex
     158           0 :                 aGuard.clear();
     159           0 :                 SolarMutexGuard aSolarGuard;
     160           0 :                 ::osl::MutexGuard aGuard2( m_aMutex );
     161             : 
     162           0 :                 if ( impl_isDisposed_nothrow() )
     163             :                     // somebody took the chance to dispose us -> bail out
     164           0 :                     return;
     165             : 
     166           0 :                 (m_pListener->*aEvents[i].listenerMethod)( aDocument );
     167             :             }
     168           0 :             break;
     169           0 :         }
     170             :     }
     171             : 
     172           0 :     void SAL_CALL DocumentEventNotifier::Impl::disposing( const csslang::EventObject& /*Event*/ ) throw (RuntimeException, std::exception)
     173             :     {
     174           0 :         SolarMutexGuard aSolarGuard;
     175           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     176             : 
     177           0 :         if ( !impl_isDisposed_nothrow() )
     178           0 :             impl_dispose_nothrow();
     179           0 :     }
     180             : 
     181           0 :     void SAL_CALL DocumentEventNotifier::Impl::disposing()
     182             :     {
     183           0 :         impl_listenerAction_nothrow( RemoveListener );
     184           0 :         impl_dispose_nothrow();
     185           0 :     }
     186             : 
     187           0 :     void DocumentEventNotifier::Impl::impl_dispose_nothrow()
     188             :     {
     189           0 :         m_pListener = NULL;
     190           0 :         m_xModel.clear();
     191           0 :     }
     192             : 
     193           0 :     void DocumentEventNotifier::Impl::impl_listenerAction_nothrow( ListenerAction _eAction )
     194             :     {
     195             :         try
     196             :         {
     197           0 :             Reference< XDocumentEventBroadcaster > xBroadcaster;
     198           0 :             if ( m_xModel.is() )
     199           0 :                 xBroadcaster.set( m_xModel, UNO_QUERY_THROW );
     200             :             else
     201             :             {
     202             :                 Reference< com::sun::star::uno::XComponentContext > aContext(
     203           0 :                     comphelper::getProcessComponentContext() );
     204           0 :                 xBroadcaster = theGlobalEventBroadcaster::get(aContext);
     205             :             }
     206             : 
     207             :             void ( SAL_CALL XDocumentEventBroadcaster::*listenerAction )( const Reference< XDocumentEventListener >& ) =
     208           0 :                 ( _eAction == RegisterListener ) ? &XDocumentEventBroadcaster::addDocumentEventListener : &XDocumentEventBroadcaster::removeDocumentEventListener;
     209           0 :             (xBroadcaster.get()->*listenerAction)( this );
     210             :         }
     211           0 :         catch( const Exception& )
     212             :         {
     213             :             DBG_UNHANDLED_EXCEPTION();
     214             :         }
     215           0 :     }
     216             : 
     217             :     // DocumentEventNotifier
     218             : 
     219           0 :     DocumentEventNotifier::DocumentEventNotifier (DocumentEventListener& rListener, Reference<XModel> const& rxDocument) :
     220           0 :         m_pImpl(new Impl(rListener, rxDocument))
     221           0 :     { }
     222             : 
     223           0 :     DocumentEventNotifier::DocumentEventNotifier (DocumentEventListener& rListener) :
     224           0 :         m_pImpl(new Impl(rListener, Reference<XModel>()))
     225           0 :     { }
     226             : 
     227           0 :     DocumentEventNotifier::~DocumentEventNotifier()
     228             :     {
     229           0 :     }
     230             : 
     231           0 :     void DocumentEventNotifier::dispose()
     232             :     {
     233           0 :         m_pImpl->dispose();
     234           0 :     }
     235             : 
     236             :     // DocumentEventListener
     237             : 
     238           0 :     DocumentEventListener::~DocumentEventListener()
     239             :     {
     240           0 :     }
     241             : 
     242             : } // namespace basctl
     243             : 
     244             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10