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

Generated by: LCOV version 1.10