LCOV - code coverage report
Current view: top level - libreoffice/chart2/source/tools - ModifyListenerHelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 63 69 91.3 %
Date: 2012-12-27 Functions: 15 16 93.8 %
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             : 
      21             : #include "ModifyListenerHelper.hxx"
      22             : #include "WeakListenerAdapter.hxx"
      23             : #include "macros.hxx"
      24             : 
      25             : #include <cppuhelper/interfacecontainer.hxx>
      26             : 
      27             : #include <com/sun/star/frame/XModel.hpp>
      28             : 
      29             : using namespace ::com::sun::star;
      30             : 
      31             : using ::com::sun::star::uno::Reference;
      32             : using ::com::sun::star::uno::Sequence;
      33             : using ::rtl::OUString;
      34             : 
      35             : namespace
      36             : {
      37             : 
      38       14883 : void lcl_fireModifyEvent(
      39             :     ::cppu::OBroadcastHelper & rBroadcastHelper,
      40             :     const Reference< uno::XWeak > & xEventSource,
      41             :     const lang::EventObject * pEvent )
      42             : {
      43             :     ::cppu::OInterfaceContainerHelper * pCntHlp = rBroadcastHelper.getContainer(
      44       14883 :         ::getCppuType( reinterpret_cast< Reference< util::XModifyListener > * >(0)));
      45       14883 :     if( pCntHlp )
      46             :     {
      47        4585 :         lang::EventObject aEventToSend;
      48        4585 :         if( pEvent )
      49        4585 :             aEventToSend = *pEvent;
      50             :         else
      51           0 :             aEventToSend.Source.set( xEventSource );
      52             :         OSL_ENSURE( aEventToSend.Source.is(), "Sending event without source" );
      53             : 
      54        4585 :         ::cppu::OInterfaceIteratorHelper aIt( *pCntHlp );
      55             : 
      56       13755 :         while( aIt.hasMoreElements())
      57             :         {
      58        4585 :             Reference< util::XModifyListener > xModListener( aIt.next(), uno::UNO_QUERY );
      59        4585 :             if( xModListener.is())
      60        4585 :                 xModListener->modified( aEventToSend );
      61        9170 :         }
      62             :     }
      63       14883 : }
      64             : 
      65        7563 : struct lcl_weakReferenceToSame : public ::std::unary_function<
      66             :         ::std::pair<
      67             :             ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener >,
      68             :             ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > >,
      69             :         bool >
      70             : {
      71        2521 :     lcl_weakReferenceToSame( const Reference< util::XModifyListener > & xModListener ) :
      72        2521 :             m_xHardRef( xModListener )
      73        2521 :     {}
      74             : 
      75        2521 :     bool operator() ( const argument_type & xElem )
      76             :     {
      77        2521 :         Reference< util::XModifyListener > xWeakAsHard( xElem.first );
      78        2521 :         if( xWeakAsHard.is())
      79        2521 :             return (xWeakAsHard == m_xHardRef);
      80           0 :         return false;
      81             :     }
      82             : 
      83             : private:
      84             :     Reference< util::XModifyListener > m_xHardRef;
      85             : };
      86             : 
      87             : } //  anonymous namespace
      88             : 
      89             : // ================================================================================
      90             : 
      91             : namespace chart
      92             : {
      93             : namespace ModifyListenerHelper
      94             : {
      95             : 
      96        3567 : uno::Reference< util::XModifyListener > createModifyEventForwarder()
      97             : {
      98        3567 :     return new ModifyEventForwarder();
      99             : }
     100             : 
     101        3567 : ModifyEventForwarder::ModifyEventForwarder() :
     102             :         ::cppu::WeakComponentImplHelper2<
     103             :         ::com::sun::star::util::XModifyBroadcaster,
     104             :         ::com::sun::star::util::XModifyListener >( m_aMutex ),
     105        3567 :         m_aModifyListeners( m_aMutex )
     106             : {
     107        3567 : }
     108             : 
     109       14883 : void ModifyEventForwarder::FireEvent( const lang::EventObject & rEvent )
     110             : {
     111       14883 :     lcl_fireModifyEvent( m_aModifyListeners, Reference< uno::XWeak >(), & rEvent );
     112       14883 : }
     113             : 
     114        2583 : void ModifyEventForwarder::AddListener( const Reference< util::XModifyListener >& aListener )
     115             : {
     116             :     try
     117             :     {
     118        2583 :         Reference< util::XModifyListener > xListenerToAdd( aListener );
     119             : 
     120        2583 :         Reference< uno::XWeak > xWeak( aListener, uno::UNO_QUERY );
     121        2583 :         if( xWeak.is())
     122             :         {
     123             :             // remember the helper class for later remove
     124        2583 :             uno::WeakReference< util::XModifyListener > xWeakRef( aListener );
     125        2583 :             xListenerToAdd.set( new WeakModifyListenerAdapter( xWeakRef ));
     126        2583 :             m_aListenerMap.push_back( tListenerMap::value_type( xWeakRef, xListenerToAdd ));
     127             :         }
     128             : 
     129        2583 :         m_aModifyListeners.addListener( ::getCppuType( &xListenerToAdd ), xListenerToAdd );
     130             :     }
     131           0 :     catch( const uno::Exception & ex )
     132             :     {
     133             :         ASSERT_EXCEPTION( ex );
     134             :     }
     135        2583 : }
     136             : 
     137        2521 : void ModifyEventForwarder::RemoveListener( const Reference< util::XModifyListener >& aListener )
     138             : {
     139             :     try
     140             :     {
     141             :         // look up fitting helper class that has been added
     142        2521 :         Reference< util::XModifyListener > xListenerToRemove( aListener );
     143             :         tListenerMap::iterator aIt(
     144        2521 :             ::std::find_if( m_aListenerMap.begin(), m_aListenerMap.end(), lcl_weakReferenceToSame( aListener )));
     145        2521 :         if( aIt != m_aListenerMap.end())
     146             :         {
     147        2521 :             xListenerToRemove.set( (*aIt).second );
     148             :             // map entry is no longer needed
     149        2521 :             m_aListenerMap.erase( aIt );
     150             :         }
     151             : 
     152        2521 :         m_aModifyListeners.removeListener( ::getCppuType( &aListener ), xListenerToRemove );
     153             :     }
     154           0 :     catch( const uno::Exception & ex )
     155             :     {
     156             :         ASSERT_EXCEPTION( ex );
     157             :     }
     158        2521 : }
     159             : 
     160        3567 : void ModifyEventForwarder::DisposeAndClear( const Reference< uno::XWeak > & xSource )
     161             : {
     162             :     ::cppu::OInterfaceContainerHelper * pCntHlp = m_aModifyListeners.getContainer(
     163        3567 :         ::getCppuType( reinterpret_cast< Reference< util::XModifyListener > * >(0)));
     164        3567 :     if( pCntHlp )
     165        2583 :         pCntHlp->disposeAndClear( lang::EventObject( xSource ) );
     166        3567 : }
     167             : 
     168             : // ____ XModifyBroadcaster ____
     169        2583 : void SAL_CALL ModifyEventForwarder::addModifyListener( const Reference< util::XModifyListener >& aListener )
     170             :     throw (uno::RuntimeException)
     171             : {
     172        2583 :     AddListener( aListener );
     173        2583 : }
     174             : 
     175        2521 : void SAL_CALL ModifyEventForwarder::removeModifyListener( const Reference< util::XModifyListener >& aListener )
     176             :     throw (uno::RuntimeException)
     177             : {
     178        2521 :     RemoveListener( aListener );
     179        2521 : }
     180             : 
     181             : // ____ XModifyListener ____
     182       14883 : void SAL_CALL ModifyEventForwarder::modified( const lang::EventObject& aEvent )
     183             :     throw (uno::RuntimeException)
     184             : {
     185       14883 :     FireEvent( aEvent );
     186       14883 : }
     187             : 
     188             : // ____ XEventListener (base of XModifyListener) ____
     189           0 : void SAL_CALL ModifyEventForwarder::disposing( const lang::EventObject& /* Source */ )
     190             :     throw (uno::RuntimeException)
     191             : {
     192             :     // nothing
     193           0 : }
     194             : 
     195             : // ____ WeakComponentImplHelperBase ____
     196        3567 : void SAL_CALL ModifyEventForwarder::disposing()
     197             : {
     198             :     // dispose was called at this
     199        3567 :     DisposeAndClear( this );
     200        3567 : }
     201             : 
     202             : } //  namespace ModifyListenerHelper
     203             : } //  namespace chart
     204             : 
     205             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10