LCOV - code coverage report
Current view: top level - svtools/source/uno - framestatuslistener.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 89 144 61.8 %
Date: 2015-06-13 12:38:46 Functions: 8 14 57.1 %
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 <svtools/framestatuslistener.hxx>
      21             : #include <com/sun/star/frame/XDispatchProvider.hpp>
      22             : #include <com/sun/star/lang/DisposedException.hpp>
      23             : #include <com/sun/star/util/URLTransformer.hpp>
      24             : #include <osl/mutex.hxx>
      25             : #include <cppuhelper/queryinterface.hxx>
      26             : #include <vcl/svapp.hxx>
      27             : #include <comphelper/processfactory.hxx>
      28             : 
      29             : using namespace ::cppu;
      30             : using namespace ::com::sun::star::awt;
      31             : using namespace ::com::sun::star::uno;
      32             : using namespace ::com::sun::star::util;
      33             : using namespace ::com::sun::star::beans;
      34             : using namespace ::com::sun::star::lang;
      35             : using namespace ::com::sun::star::frame;
      36             : 
      37             : namespace svt
      38             : {
      39             : 
      40        1236 : FrameStatusListener::FrameStatusListener(
      41             :     const Reference< XComponentContext >& rxContext,
      42             :     const Reference< XFrame >& xFrame ) :
      43             :     OWeakObject()
      44             :     ,   m_bInitialized( true )
      45             :     ,   m_bDisposed( false )
      46             :     ,   m_xFrame( xFrame )
      47        1236 :     ,   m_xContext( rxContext )
      48             : {
      49        1236 : }
      50             : 
      51        1236 : FrameStatusListener::~FrameStatusListener()
      52             : {
      53        1236 : }
      54             : 
      55             : // XInterface
      56        4944 : Any SAL_CALL FrameStatusListener::queryInterface( const Type& rType )
      57             : throw ( RuntimeException, std::exception )
      58             : {
      59             :     Any a = ::cppu::queryInterface(
      60             :                 rType ,
      61             :                 static_cast< XComponent* >( this ),
      62             :                 static_cast< XFrameActionListener* >( this ),
      63             :                 static_cast< XStatusListener* >( this ),
      64             :                 static_cast< XEventListener* >( static_cast< XStatusListener* >( this )),
      65        4944 :                 static_cast< XEventListener* >( static_cast< XFrameActionListener* >( this )));
      66             : 
      67        4944 :     if ( a.hasValue() )
      68        4944 :         return a;
      69             : 
      70           0 :     return OWeakObject::queryInterface( rType );
      71             : }
      72             : 
      73       17732 : void SAL_CALL FrameStatusListener::acquire() throw ()
      74             : {
      75       17732 :     OWeakObject::acquire();
      76       17732 : }
      77             : 
      78       17732 : void SAL_CALL FrameStatusListener::release() throw ()
      79             : {
      80       17732 :     OWeakObject::release();
      81       17732 : }
      82             : 
      83             : // XComponent
      84           0 : void SAL_CALL FrameStatusListener::dispose()
      85             : throw (::com::sun::star::uno::RuntimeException, std::exception)
      86             : {
      87           0 :     Reference< XComponent > xThis( static_cast< OWeakObject* >(this), UNO_QUERY );
      88             : 
      89           0 :     SolarMutexGuard aSolarMutexGuard;
      90           0 :     if ( m_bDisposed )
      91           0 :         throw DisposedException();
      92             : 
      93           0 :     Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
      94           0 :     URLToDispatchMap::iterator pIter = m_aListenerMap.begin();
      95           0 :     while ( pIter != m_aListenerMap.end() )
      96             :     {
      97             :         try
      98             :         {
      99           0 :             Reference< XDispatch > xDispatch( pIter->second );
     100           0 :             Reference< XURLTransformer > xURLTransformer( com::sun::star::util::URLTransformer::create( m_xContext ) );
     101           0 :             com::sun::star::util::URL aTargetURL;
     102           0 :             aTargetURL.Complete = pIter->first;
     103           0 :             xURLTransformer->parseStrict( aTargetURL );
     104             : 
     105           0 :             if ( xDispatch.is() && xStatusListener.is() )
     106           0 :                 xDispatch->removeStatusListener( xStatusListener, aTargetURL );
     107             :         }
     108           0 :         catch (const Exception&)
     109             :         {
     110             :         }
     111             : 
     112           0 :         ++pIter;
     113             :     }
     114             : 
     115           0 :     m_bDisposed = true;
     116           0 : }
     117             : 
     118           0 : void SAL_CALL FrameStatusListener::addEventListener( const Reference< XEventListener >& )
     119             : throw ( RuntimeException, std::exception )
     120             : {
     121             :     // helper class for status updates - no need to support listener
     122           0 : }
     123             : 
     124           0 : void SAL_CALL FrameStatusListener::removeEventListener( const Reference< XEventListener >& )
     125             : throw ( RuntimeException, std::exception )
     126             : {
     127             :     // helper class for status updates - no need to support listener
     128           0 : }
     129             : 
     130             : // XEventListener
     131           0 : void SAL_CALL FrameStatusListener::disposing( const EventObject& Source )
     132             : throw ( RuntimeException, std::exception )
     133             : {
     134           0 :     Reference< XInterface > xSource( Source.Source );
     135             : 
     136           0 :     SolarMutexGuard aSolarMutexGuard;
     137             : 
     138           0 :     URLToDispatchMap::iterator pIter = m_aListenerMap.begin();
     139           0 :     while ( pIter != m_aListenerMap.end() )
     140             :     {
     141             :         // Compare references and release dispatch references if they are equal.
     142           0 :         Reference< XInterface > xIfac( pIter->second, UNO_QUERY );
     143           0 :         if ( xSource == xIfac )
     144           0 :             pIter->second.clear();
     145           0 :     }
     146             : 
     147           0 :     Reference< XInterface > xIfac( m_xFrame, UNO_QUERY );
     148           0 :     if ( xIfac == xSource )
     149           0 :         m_xFrame.clear();
     150           0 : }
     151             : 
     152           0 : void FrameStatusListener::frameAction( const FrameActionEvent& Action )
     153             : throw ( RuntimeException, std::exception )
     154             : {
     155           0 :     if ( Action.Action == FrameAction_CONTEXT_CHANGED )
     156           0 :         bindListener();
     157           0 : }
     158             : 
     159        1236 : void FrameStatusListener::addStatusListener( const OUString& aCommandURL )
     160             : {
     161        1236 :     Reference< XDispatch >       xDispatch;
     162        2472 :     Reference< XStatusListener > xStatusListener;
     163        2472 :     com::sun::star::util::URL    aTargetURL;
     164             : 
     165             :     {
     166        1236 :         SolarMutexGuard aSolarMutexGuard;
     167        1236 :         URLToDispatchMap::iterator pIter = m_aListenerMap.find( aCommandURL );
     168             : 
     169             :         // Already in the list of status listener. Do nothing.
     170        1236 :         if ( pIter != m_aListenerMap.end() )
     171           0 :             return;
     172             : 
     173             :         // Check if we are already initialized. Implementation starts adding itself as status listener when
     174             :         // intialize is called.
     175        1236 :         if ( !m_bInitialized )
     176             :         {
     177             :             // Put into the unordered_map of status listener. Will be activated when initialized is called
     178           0 :             m_aListenerMap.insert( URLToDispatchMap::value_type( aCommandURL, Reference< XDispatch >() ));
     179           0 :             return;
     180             :         }
     181             :         else
     182             :         {
     183             :             // Add status listener directly as intialize has already been called.
     184        1236 :             Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
     185        1236 :             if ( m_xContext.is() && xDispatchProvider.is() )
     186             :             {
     187        1236 :                 Reference< XURLTransformer > xURLTransformer( com::sun::star::util::URLTransformer::create( m_xContext ) );
     188        1236 :                 aTargetURL.Complete = aCommandURL;
     189        1236 :                 xURLTransformer->parseStrict( aTargetURL );
     190        1236 :                 xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
     191             : 
     192        1236 :                 xStatusListener = Reference< XStatusListener >( static_cast< OWeakObject* >( this ), UNO_QUERY );
     193        1236 :                 URLToDispatchMap::iterator aIter = m_aListenerMap.find( aCommandURL );
     194        1236 :                 if ( aIter != m_aListenerMap.end() )
     195             :                 {
     196           0 :                     Reference< XDispatch > xOldDispatch( aIter->second );
     197           0 :                     aIter->second = xDispatch;
     198             : 
     199             :                     try
     200             :                     {
     201           0 :                         if ( xOldDispatch.is() )
     202           0 :                             xOldDispatch->removeStatusListener( xStatusListener, aTargetURL );
     203             :                     }
     204           0 :                     catch (const Exception&)
     205             :                     {
     206           0 :                     }
     207             :                 }
     208             :                 else
     209        1236 :                     m_aListenerMap.insert( URLToDispatchMap::value_type( aCommandURL, xDispatch ));
     210        1236 :             }
     211        1236 :         }
     212             :     }
     213             : 
     214             :     // Call without locked mutex as we are called back from dispatch implementation
     215             :     try
     216             :     {
     217        1236 :         if ( xDispatch.is() )
     218         690 :             xDispatch->addStatusListener( xStatusListener, aTargetURL );
     219             :     }
     220           0 :     catch (const Exception&)
     221             :     {
     222        1236 :     }
     223             : }
     224             : 
     225             : 
     226        1236 : void FrameStatusListener::bindListener()
     227             : {
     228        1236 :     std::vector< Listener > aDispatchVector;
     229        2472 :     Reference< XStatusListener > xStatusListener;
     230             : 
     231             :     {
     232        1236 :         SolarMutexGuard aSolarMutexGuard;
     233             : 
     234        1236 :         if ( !m_bInitialized )
     235        1236 :             return;
     236             : 
     237             :         // Collect all registered command URL's and store them temporary
     238        2472 :         Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
     239        1236 :         if ( m_xContext.is() && xDispatchProvider.is() )
     240             :         {
     241        1236 :             xStatusListener = Reference< XStatusListener >( static_cast< OWeakObject* >( this ), UNO_QUERY );
     242        1236 :             URLToDispatchMap::iterator pIter = m_aListenerMap.begin();
     243        3708 :             while ( pIter != m_aListenerMap.end() )
     244             :             {
     245        1236 :                 Reference< XURLTransformer > xURLTransformer( com::sun::star::util::URLTransformer::create( m_xContext ) );
     246        2472 :                 com::sun::star::util::URL aTargetURL;
     247        1236 :                 aTargetURL.Complete = pIter->first;
     248        1236 :                 xURLTransformer->parseStrict( aTargetURL );
     249             : 
     250        2472 :                 Reference< XDispatch > xDispatch( pIter->second );
     251        1236 :                 if ( xDispatch.is() )
     252             :                 {
     253             :                     // We already have a dispatch object => we have to requery.
     254             :                     // Release old dispatch object and remove it as listener
     255             :                     try
     256             :                     {
     257         690 :                         xDispatch->removeStatusListener( xStatusListener, aTargetURL );
     258             :                     }
     259           0 :                     catch (const Exception&)
     260             :                     {
     261             :                     }
     262             :                 }
     263             : 
     264             :                 // Query for dispatch object. Old dispatch will be released with this, too.
     265             :                 try
     266             :                 {
     267        1236 :                     xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
     268             :                 }
     269           0 :                 catch (const Exception&)
     270             :                 {
     271             :                 }
     272        1236 :                 pIter->second = xDispatch;
     273             : 
     274        2472 :                 Listener aListener( aTargetURL, xDispatch );
     275        1236 :                 aDispatchVector.push_back( aListener );
     276        1236 :                 ++pIter;
     277        1236 :             }
     278        1236 :         }
     279             :     }
     280             : 
     281             :     // Call without locked mutex as we are called back from dispatch implementation
     282        1236 :     if ( xStatusListener.is() )
     283             :     {
     284             :         try
     285             :         {
     286        2472 :             for ( size_t i = 0; i < aDispatchVector.size(); i++ )
     287             :             {
     288        1236 :                 Listener& rListener = aDispatchVector[i];
     289        1236 :                 if ( rListener.xDispatch.is() )
     290         690 :                     rListener.xDispatch->addStatusListener( xStatusListener, rListener.aURL );
     291             :             }
     292             :         }
     293           0 :         catch (const Exception&)
     294             :         {
     295             :         }
     296        1236 :     }
     297             : }
     298             : 
     299        1236 : void FrameStatusListener::unbindListener()
     300             : {
     301        1236 :     SolarMutexGuard aSolarMutexGuard;
     302             : 
     303        1236 :     if ( !m_bInitialized )
     304        1236 :         return;
     305             : 
     306             :     // Collect all registered command URL's and store them temporary
     307        2472 :     Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
     308        1236 :     if ( m_xContext.is() && xDispatchProvider.is() )
     309             :     {
     310        1236 :         Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
     311        1236 :         URLToDispatchMap::iterator pIter = m_aListenerMap.begin();
     312        3708 :         while ( pIter != m_aListenerMap.end() )
     313             :         {
     314        1236 :             Reference< XURLTransformer > xURLTransformer( com::sun::star::util::URLTransformer::create( m_xContext ) );
     315        2472 :             com::sun::star::util::URL aTargetURL;
     316        1236 :             aTargetURL.Complete = pIter->first;
     317        1236 :             xURLTransformer->parseStrict( aTargetURL );
     318             : 
     319        2472 :             Reference< XDispatch > xDispatch( pIter->second );
     320        1236 :             if ( xDispatch.is() )
     321             :             {
     322             :                 // We already have a dispatch object => we have to requery.
     323             :                 // Release old dispatch object and remove it as listener
     324             :                 try
     325             :                 {
     326         690 :                     xDispatch->removeStatusListener( xStatusListener, aTargetURL );
     327             :                 }
     328           0 :                 catch (const Exception&)
     329             :                 {
     330             :                 }
     331             :             }
     332        1236 :             pIter->second.clear();
     333        1236 :             ++pIter;
     334        2472 :         }
     335        1236 :     }
     336             : }
     337             : 
     338             : } // svt
     339             : 
     340             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11