LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/svtools/source/uno - popupmenucontrollerbase.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 91 187 48.7 %
Date: 2013-07-09 Functions: 13 30 43.3 %
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/popupmenucontrollerbase.hxx"
      21             : 
      22             : #include <com/sun/star/awt/XDevice.hpp>
      23             : #include <com/sun/star/beans/PropertyValue.hpp>
      24             : #include <com/sun/star/awt/MenuItemStyle.hpp>
      25             : #include <com/sun/star/frame/XDispatchProvider.hpp>
      26             : #include <com/sun/star/lang/DisposedException.hpp>
      27             : #include <com/sun/star/util/URLTransformer.hpp>
      28             : 
      29             : #include <vcl/menu.hxx>
      30             : #include <vcl/svapp.hxx>
      31             : #include <rtl/ustrbuf.hxx>
      32             : #include <rtl/logfile.hxx>
      33             : #include <osl/mutex.hxx>
      34             : #include <comphelper/processfactory.hxx>
      35             : 
      36             : //_________________________________________________________________________________________________________________
      37             : //  Defines
      38             : //_________________________________________________________________________________________________________________
      39             : //
      40             : 
      41             : 
      42             : using namespace com::sun::star;
      43             : using namespace com::sun::star::uno;
      44             : using namespace com::sun::star::lang;
      45             : using namespace com::sun::star::frame;
      46             : using namespace com::sun::star::beans;
      47             : using namespace com::sun::star::util;
      48             : 
      49             : namespace svt
      50             : {
      51             : 
      52           0 : struct PopupMenuControllerBaseDispatchInfo
      53             : {
      54             :     Reference< XDispatch > mxDispatch;
      55             :     const URL maURL;
      56             :     const Sequence< PropertyValue > maArgs;
      57             : 
      58           0 :     PopupMenuControllerBaseDispatchInfo( const Reference< XDispatch >& xDispatch, const URL& rURL, const Sequence< PropertyValue >& rArgs )
      59           0 :         : mxDispatch( xDispatch ), maURL( rURL ), maArgs( rArgs ) {}
      60             : };
      61             : 
      62         659 : PopupMenuControllerBase::PopupMenuControllerBase( const Reference< XComponentContext >& xContext ) :
      63             :     ::comphelper::OBaseMutex(),
      64             :     PopupMenuControllerBaseType(m_aMutex),
      65         659 :     m_bInitialized( false )
      66             : {
      67         659 :     if ( xContext.is() )
      68         659 :         m_xURLTransformer.set( util::URLTransformer::create( xContext ) );
      69         659 : }
      70             : 
      71         659 : PopupMenuControllerBase::~PopupMenuControllerBase()
      72             : {
      73         659 : }
      74             : 
      75             : // protected function
      76        1309 : void PopupMenuControllerBase::throwIfDisposed() throw ( RuntimeException )
      77             : {
      78        1309 :     if (rBHelper.bDisposed || rBHelper.bInDispose)
      79           0 :         throw com::sun::star::lang::DisposedException();
      80        1309 : }
      81             : 
      82             : // protected function
      83         641 : void PopupMenuControllerBase::resetPopupMenu( com::sun::star::uno::Reference< com::sun::star::awt::XPopupMenu >& rPopupMenu )
      84             : {
      85         641 :     if ( rPopupMenu.is() && rPopupMenu->getItemCount() > 0 )
      86             :     {
      87           0 :         rPopupMenu->clear();
      88             :     }
      89         641 : }
      90             : 
      91         659 : void SAL_CALL PopupMenuControllerBase::disposing()
      92             : {
      93             :     // Reset our members and set disposed flag
      94         659 :     osl::MutexGuard aLock( m_aMutex );
      95         659 :     m_xFrame.clear();
      96         659 :     m_xDispatch.clear();
      97         659 :     m_xPopupMenu.clear();
      98         659 : }
      99             : 
     100             : // XServiceInfo
     101             : 
     102           0 : sal_Bool SAL_CALL PopupMenuControllerBase::supportsService( const OUString& ServiceName ) throw (RuntimeException)
     103             : {
     104           0 :     const Sequence< OUString > aSNL( getSupportedServiceNames() );
     105           0 :     const OUString * pArray = aSNL.getConstArray();
     106             : 
     107           0 :     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
     108           0 :         if( pArray[i] == ServiceName )
     109           0 :             return true;
     110             : 
     111           0 :     return false;
     112             : }
     113             : 
     114             : // XEventListener
     115           0 : void SAL_CALL PopupMenuControllerBase::disposing( const EventObject& ) throw ( RuntimeException )
     116             : {
     117           0 :     osl::MutexGuard aLock( m_aMutex );
     118           0 :     m_xFrame.clear();
     119           0 :     m_xDispatch.clear();
     120           0 :     m_xPopupMenu.clear();
     121           0 : }
     122             : 
     123             : // XMenuListener
     124           0 : void SAL_CALL PopupMenuControllerBase::itemHighlighted( const awt::MenuEvent& ) throw (RuntimeException)
     125             : {
     126           0 : }
     127             : 
     128           0 : void PopupMenuControllerBase::impl_select(const Reference< XDispatch >& _xDispatch,const URL& aURL)
     129             : {
     130           0 :     Sequence<PropertyValue>      aArgs;
     131             :     OSL_ENSURE(_xDispatch.is(),"PopupMenuControllerBase::impl_select: No dispatch");
     132           0 :     if ( _xDispatch.is() )
     133           0 :         _xDispatch->dispatch( aURL, aArgs );
     134           0 : }
     135             : 
     136           0 : void SAL_CALL PopupMenuControllerBase::itemSelected( const awt::MenuEvent& rEvent ) throw (RuntimeException)
     137             : {
     138           0 :     throwIfDisposed();
     139             : 
     140           0 :     osl::MutexGuard aLock( m_aMutex );
     141             : 
     142           0 :     if( m_xPopupMenu.is() )
     143             :     {
     144           0 :         Sequence<PropertyValue> aArgs;
     145           0 :         dispatchCommand( m_xPopupMenu->getCommand( rEvent.MenuId ), aArgs );
     146           0 :     }
     147           0 : }
     148             : 
     149           0 : void PopupMenuControllerBase::dispatchCommand( const OUString& sCommandURL, const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rArgs )
     150             : {
     151           0 :     osl::MutexGuard aLock( m_aMutex );
     152             : 
     153           0 :     throwIfDisposed();
     154             : 
     155             :     try
     156             :     {
     157           0 :         Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY_THROW );
     158           0 :         URL aURL;
     159           0 :         aURL.Complete = sCommandURL;
     160           0 :         m_xURLTransformer->parseStrict( aURL );
     161             : 
     162           0 :         Reference< XDispatch > xDispatch( xDispatchProvider->queryDispatch( aURL, OUString(), 0 ), UNO_QUERY_THROW );
     163             : 
     164           0 :         Application::PostUserEvent( STATIC_LINK(0, PopupMenuControllerBase, ExecuteHdl_Impl), new PopupMenuControllerBaseDispatchInfo( xDispatch, aURL, rArgs ) );
     165             : 
     166             :     }
     167           0 :     catch( Exception& )
     168             :     {
     169           0 :     }
     170             : 
     171           0 : }
     172             : 
     173           0 : IMPL_STATIC_LINK_NOINSTANCE( PopupMenuControllerBase, ExecuteHdl_Impl, PopupMenuControllerBaseDispatchInfo*, pDispatchInfo )
     174             : {
     175           0 :     pDispatchInfo->mxDispatch->dispatch( pDispatchInfo->maURL, pDispatchInfo->maArgs );
     176           0 :     delete pDispatchInfo;
     177           0 :     return 0;
     178             : }
     179             : 
     180           0 : void SAL_CALL PopupMenuControllerBase::itemActivated( const awt::MenuEvent& ) throw (RuntimeException)
     181             : {
     182           0 : }
     183             : 
     184           0 : void SAL_CALL PopupMenuControllerBase::itemDeactivated( const awt::MenuEvent& ) throw (RuntimeException)
     185             : {
     186           0 : }
     187             : 
     188         654 : void SAL_CALL PopupMenuControllerBase::updatePopupMenu() throw ( ::com::sun::star::uno::RuntimeException )
     189             : {
     190         654 :     osl::ClearableMutexGuard aLock( m_aMutex );
     191         654 :     throwIfDisposed();
     192         654 :     aLock.clear();
     193             : 
     194         654 :     updateCommand( m_aCommandURL );
     195         654 : }
     196             : 
     197         654 : void SAL_CALL PopupMenuControllerBase::updateCommand( const OUString& rCommandURL )
     198             : {
     199         654 :     osl::ClearableMutexGuard aLock( m_aMutex );
     200        1308 :     Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
     201        1308 :     Reference< XDispatch > xDispatch( m_xDispatch );
     202        1308 :     URL aTargetURL;
     203         654 :     aTargetURL.Complete = rCommandURL;
     204         654 :     m_xURLTransformer->parseStrict( aTargetURL );
     205         654 :     aLock.clear();
     206             : 
     207             :     // Add/remove status listener to get a status update once
     208         654 :     if ( xDispatch.is() )
     209             :     {
     210         645 :         xDispatch->addStatusListener( xStatusListener, aTargetURL );
     211         645 :         xDispatch->removeStatusListener( xStatusListener, aTargetURL );
     212         654 :     }
     213         654 : }
     214             : 
     215             : 
     216             : // XDispatchProvider
     217             : Reference< XDispatch > SAL_CALL
     218           0 : PopupMenuControllerBase::queryDispatch(
     219             :     const URL& /*aURL*/,
     220             :     const OUString& /*sTarget*/,
     221             :     sal_Int32 /*nFlags*/ )
     222             : throw( RuntimeException )
     223             : {
     224             :     // must be implemented by subclass
     225           0 :     osl::MutexGuard aLock( m_aMutex );
     226           0 :     throwIfDisposed();
     227             : 
     228           0 :     return Reference< XDispatch >();
     229             : }
     230             : 
     231           0 : Sequence< Reference< XDispatch > > SAL_CALL PopupMenuControllerBase::queryDispatches( const Sequence< DispatchDescriptor >& lDescriptor ) throw( RuntimeException )
     232             : {
     233             :     // Create return list - which must have same size then the given descriptor
     234             :     // It's not allowed to pack it!
     235           0 :     osl::ClearableMutexGuard aLock( m_aMutex );
     236           0 :     throwIfDisposed();
     237           0 :     aLock.clear();
     238             : 
     239           0 :     sal_Int32                                                          nCount = lDescriptor.getLength();
     240           0 :     uno::Sequence< uno::Reference< frame::XDispatch > > lDispatcher( nCount );
     241             : 
     242             :     // Step over all descriptors and try to get any dispatcher for it.
     243           0 :     for( sal_Int32 i=0; i<nCount; ++i )
     244             :     {
     245           0 :         lDispatcher[i] = queryDispatch( lDescriptor[i].FeatureURL  ,
     246           0 :                                         lDescriptor[i].FrameName   ,
     247           0 :                                         lDescriptor[i].SearchFlags );
     248             :     }
     249             : 
     250           0 :     return lDispatcher;
     251             : }
     252             : 
     253             : // XDispatch
     254             : void SAL_CALL
     255           0 : PopupMenuControllerBase::dispatch(
     256             :     const URL& /*aURL*/,
     257             :     const Sequence< PropertyValue >& /*seqProperties*/ )
     258             : throw( ::com::sun::star::uno::RuntimeException )
     259             : {
     260             :     // must be implemented by subclass
     261           0 :     osl::MutexGuard aLock( m_aMutex );
     262           0 :     throwIfDisposed();
     263           0 : }
     264             : 
     265             : void SAL_CALL
     266           0 : PopupMenuControllerBase::addStatusListener(
     267             :     const Reference< XStatusListener >& xControl,
     268             :     const URL& aURL )
     269             : throw( ::com::sun::star::uno::RuntimeException )
     270             : {
     271           0 :     osl::ResettableMutexGuard aLock( m_aMutex );
     272           0 :     throwIfDisposed();
     273           0 :     aLock.clear();
     274             : 
     275           0 :     bool bStatusUpdate( false );
     276           0 :     rBHelper.addListener( ::getCppuType( &xControl ), xControl );
     277             : 
     278           0 :     aLock.reset();
     279           0 :     if ( aURL.Complete.indexOf( m_aBaseURL ) == 0 )
     280           0 :         bStatusUpdate = true;
     281           0 :     aLock.clear();
     282             : 
     283           0 :     if ( bStatusUpdate )
     284             :     {
     285             :         // Dummy update for popup menu controllers
     286           0 :         FeatureStateEvent aEvent;
     287           0 :         aEvent.FeatureURL = aURL;
     288           0 :         aEvent.IsEnabled  = sal_True;
     289           0 :         aEvent.Requery    = sal_False;
     290           0 :         aEvent.State      = Any();
     291           0 :         xControl->statusChanged( aEvent );
     292           0 :     }
     293           0 : }
     294             : 
     295           0 : void SAL_CALL PopupMenuControllerBase::removeStatusListener(
     296             :     const Reference< XStatusListener >& xControl,
     297             :     const URL& /*aURL*/ )
     298             : throw( ::com::sun::star::uno::RuntimeException )
     299             : {
     300           0 :     rBHelper.removeListener( ::getCppuType( &xControl ), xControl );
     301           0 : }
     302             : 
     303         643 : OUString PopupMenuControllerBase::determineBaseURL( const OUString& aURL )
     304             : {
     305             :     // Just use the main part of the URL for popup menu controllers
     306         643 :     sal_Int32     nQueryPart( 0 );
     307         643 :     sal_Int32     nSchemePart( 0 );
     308         643 :     OUString aMainURL( "vnd.sun.star.popup:" );
     309             : 
     310         643 :     nSchemePart = aURL.indexOf( ':' );
     311        1286 :     if (( nSchemePart > 0 ) &&
     312         643 :         ( aURL.getLength() > ( nSchemePart+1 )))
     313             :     {
     314         643 :         nQueryPart  = aURL.indexOf( '?', nSchemePart );
     315         643 :         if ( nQueryPart > 0 )
     316           0 :             aMainURL += aURL.copy( nSchemePart, nQueryPart-nSchemePart );
     317         643 :         else if ( nQueryPart == -1 )
     318         643 :             aMainURL += aURL.copy( nSchemePart+1 );
     319             :     }
     320             : 
     321         643 :     return aMainURL;
     322             : }
     323             : 
     324             : // XInitialization
     325         651 : void SAL_CALL PopupMenuControllerBase::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException )
     326             : {
     327         651 :     osl::MutexGuard aLock( m_aMutex );
     328             : 
     329         651 :     sal_Bool bInitalized( m_bInitialized );
     330         651 :     if ( !bInitalized )
     331             :     {
     332         651 :         PropertyValue       aPropValue;
     333        1302 :         OUString       aCommandURL;
     334        1302 :         Reference< XFrame > xFrame;
     335             : 
     336        2580 :         for ( int i = 0; i < aArguments.getLength(); i++ )
     337             :         {
     338        1929 :             if ( aArguments[i] >>= aPropValue )
     339             :             {
     340        1929 :                 if ( aPropValue.Name == "Frame" )
     341         643 :                     aPropValue.Value >>= xFrame;
     342        1286 :                 else if ( aPropValue.Name == "CommandURL" )
     343         643 :                     aPropValue.Value >>= aCommandURL;
     344         643 :                 else if ( aPropValue.Name == "ModuleName" )
     345           0 :                     aPropValue.Value >>= m_aModuleName;
     346             :             }
     347             :         }
     348             : 
     349         651 :         if ( xFrame.is() && !aCommandURL.isEmpty() )
     350             :         {
     351         643 :             m_xFrame        = xFrame;
     352         643 :             m_aCommandURL   = aCommandURL;
     353         643 :             m_aBaseURL      = determineBaseURL( aCommandURL );
     354         643 :             m_bInitialized  = true;
     355         651 :         }
     356         651 :     }
     357         651 : }
     358             : // XPopupMenuController
     359         649 : void SAL_CALL PopupMenuControllerBase::setPopupMenu( const Reference< awt::XPopupMenu >& xPopupMenu ) throw ( RuntimeException )
     360             : {
     361         649 :     osl::MutexGuard aLock( m_aMutex );
     362         649 :     throwIfDisposed();
     363             : 
     364         649 :     if ( m_xFrame.is() && !m_xPopupMenu.is() )
     365             :     {
     366             :         // Create popup menu on demand
     367         642 :         SolarMutexGuard aSolarMutexGuard;
     368             : 
     369         642 :         m_xPopupMenu = xPopupMenu;
     370         642 :         m_xPopupMenu->addMenuListener( Reference< awt::XMenuListener >( (OWeakObject*)this, UNO_QUERY ));
     371             : 
     372        1284 :         Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
     373             : 
     374        1284 :         URL aTargetURL;
     375         642 :         aTargetURL.Complete = m_aCommandURL;
     376         642 :         m_xURLTransformer->parseStrict( aTargetURL );
     377         642 :         m_xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
     378             : 
     379         642 :         impl_setPopupMenu();
     380             : 
     381        1284 :         updatePopupMenu();
     382         649 :     }
     383         649 : }
     384           2 : void PopupMenuControllerBase::impl_setPopupMenu()
     385             : {
     386           2 : }
     387         465 : }
     388             : 
     389             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10