LCOV - code coverage report
Current view: top level - framework/source/uielement - macrosmenucontroller.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 12 84 14.3 %
Date: 2015-06-13 12:38:46 Functions: 11 19 57.9 %
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 <uielement/macrosmenucontroller.hxx>
      21             : #include <uielement/menubarmanager.hxx>
      22             : #include "services.h"
      23             : #include <classes/resource.hrc>
      24             : #include <classes/fwkresid.hxx>
      25             : #include <framework/imageproducer.hxx>
      26             : #include <com/sun/star/awt/MenuItemStyle.hpp>
      27             : #include <com/sun/star/beans/PropertyValue.hpp>
      28             : #include <com/sun/star/beans/XPropertySet.hpp>
      29             : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
      30             : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
      31             : #include <com/sun/star/frame/XModuleManager.hpp>
      32             : #include <comphelper/processfactory.hxx>
      33             : #include <vcl/svapp.hxx>
      34             : #include <vcl/i18nhelp.hxx>
      35             : #include <rtl/ustrbuf.hxx>
      36             : #include "helper/mischelper.hxx"
      37             : #include "helpid.hrc"
      38             : #include <osl/mutex.hxx>
      39             : 
      40             : using namespace com::sun::star::uno;
      41             : using namespace com::sun::star::lang;
      42             : using namespace com::sun::star::frame;
      43             : using namespace com::sun::star::beans;
      44             : using namespace com::sun::star::util;
      45             : using namespace com::sun::star::style;
      46             : using namespace com::sun::star::container;
      47             : using namespace ::com::sun::star::frame;
      48             : 
      49             : namespace framework
      50             : {
      51             : class
      52          74 : DEFINE_XSERVICEINFO_MULTISERVICE_2      (   MacrosMenuController                    ,
      53             :                                             OWeakObject                             ,
      54             :                                             SERVICENAME_POPUPMENUCONTROLLER         ,
      55             :                                             IMPLEMENTATIONNAME_MACROSMENUCONTROLLER
      56             :                                         )
      57             : 
      58           3 : DEFINE_INIT_SERVICE                     (   MacrosMenuController, {} )
      59             : 
      60           3 : MacrosMenuController::MacrosMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& xContext ) :
      61             :     svt::PopupMenuControllerBase( xContext ),
      62           3 :     m_xContext( xContext)
      63             : {
      64           3 : }
      65             : 
      66           6 : MacrosMenuController::~MacrosMenuController()
      67             : {
      68             :     OSL_TRACE("calling dtor");
      69           6 : }
      70             : 
      71             : // private function
      72           0 : void MacrosMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu )
      73             : {
      74           0 :     VCLXPopupMenu* pVCLPopupMenu = static_cast<VCLXPopupMenu *>(VCLXMenu::GetImplementation( rPopupMenu ));
      75           0 :     PopupMenu*     pPopupMenu    = 0;
      76             : 
      77           0 :     SolarMutexGuard aSolarMutexGuard;
      78             : 
      79           0 :     resetPopupMenu( rPopupMenu );
      80           0 :     if ( pVCLPopupMenu )
      81           0 :         pPopupMenu = static_cast<PopupMenu *>(pVCLPopupMenu->GetMenu());
      82             : 
      83           0 :     if (!pPopupMenu)
      84           0 :         return;
      85             : 
      86             :     // insert basic
      87           0 :     OUString aCommand(".uno:MacroDialog");
      88           0 :     OUString aDisplayName = RetrieveLabelFromCommand( aCommand );
      89           0 :     pPopupMenu->InsertItem( 2, aDisplayName );
      90           0 :     pPopupMenu->SetItemCommand( 2, aCommand );
      91             : 
      92             :     // insert providers but not basic or java
      93           0 :     addScriptItems( pPopupMenu, 4);
      94             : }
      95             : 
      96             : // XEventListener
      97           0 : void SAL_CALL MacrosMenuController::disposing( const EventObject& ) throw ( RuntimeException, std::exception )
      98             : {
      99           0 :     Reference< css::awt::XMenuListener > xHolder(static_cast<OWeakObject *>(this), UNO_QUERY );
     100             : 
     101           0 :     osl::MutexGuard aLock( m_aMutex );
     102             :     OSL_TRACE("disposing");
     103           0 :     m_xFrame.clear();
     104           0 :     m_xDispatch.clear();
     105           0 :     m_xContext.clear();
     106             : 
     107           0 :     if ( m_xPopupMenu.is() )
     108             :     {
     109           0 :         m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(static_cast<OWeakObject *>(this), UNO_QUERY ));
     110             :         OSL_TRACE("removed listener");
     111             :     }
     112           0 :     m_xPopupMenu.clear();
     113           0 : }
     114             : 
     115             : // XStatusListener
     116           1 : void SAL_CALL MacrosMenuController::statusChanged( const FeatureStateEvent& ) throw ( RuntimeException, std::exception )
     117             : {
     118           1 :     osl::MutexGuard aLock( m_aMutex );
     119           1 :     if ( m_xPopupMenu.is() )
     120             :     {
     121           0 :         fillPopupMenu( m_xPopupMenu );
     122           1 :     }
     123           1 : }
     124             : 
     125             : // XMenuListener
     126           0 : void MacrosMenuController::impl_select(const Reference< XDispatch >& /*_xDispatch*/,const ::com::sun::star::util::URL& aTargetURL)
     127             : {
     128             :     // need to requery, since we handle more than one type of Command
     129             :     // if we don't do this only .uno:ScriptOrganizer commands are executed
     130           0 :     Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
     131           0 :     Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
     132           0 :     if( xDispatch.is() )
     133             :     {
     134           0 :         ExecuteInfo* pExecuteInfo = new ExecuteInfo;
     135           0 :         pExecuteInfo->xDispatch     = xDispatch;
     136           0 :         pExecuteInfo->aTargetURL    = aTargetURL;
     137           0 :         Application::PostUserEvent( LINK(0, MacrosMenuController , ExecuteHdl_Impl), pExecuteInfo );
     138           0 :     }
     139           0 : }
     140             : 
     141           0 : IMPL_STATIC_LINK( MacrosMenuController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
     142             : {
     143             :    try
     144             :    {
     145             :        // Asynchronous execution as this can lead to our own destruction!
     146             :        // Framework can recycle our current frame and the layout manager disposes all user interface
     147             :        // elements if a component gets detached from its frame!
     148           0 :        pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
     149             :    }
     150           0 :    catch ( const Exception& )
     151             :    {
     152             :    }
     153           0 :    delete pExecuteInfo;
     154           0 :    return 0;
     155             : }
     156             : 
     157           0 : OUString MacrosMenuController::RetrieveLabelFromCommand(const OUString& rCmdURL)
     158             : {
     159           0 :     bool bModuleIdentified = !m_aModuleIdentifier.isEmpty();
     160           0 :     return framework::RetrieveLabelFromCommand(rCmdURL, m_xContext, m_xUICommandLabels, m_xFrame, m_aModuleIdentifier, bModuleIdentified, "Label");
     161             : }
     162             : 
     163           0 : void MacrosMenuController::addScriptItems( PopupMenu* pPopupMenu, sal_uInt16 startItemId )
     164             : {
     165           0 :     const OUString aCmdBase(".uno:ScriptOrganizer?ScriptOrganizer.Language:string=");
     166           0 :     const OUString ellipsis( "..." );
     167           0 :     const OUString providerKey("com.sun.star.script.provider.ScriptProviderFor");
     168           0 :     const OUString languageProviderName("com.sun.star.script.provider.LanguageScriptProvider");
     169           0 :     sal_uInt16 itemId = startItemId;
     170           0 :     Reference< XContentEnumerationAccess > xEnumAccess = Reference< XContentEnumerationAccess >( m_xContext->getServiceManager(), UNO_QUERY_THROW );
     171           0 :     Reference< XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( languageProviderName );
     172             : 
     173           0 :     while ( xEnum->hasMoreElements() )
     174             :     {
     175           0 :         Reference< XServiceInfo > xServiceInfo;
     176           0 :         if ( !( xEnum->nextElement() >>= xServiceInfo ) )
     177             :         {
     178           0 :             break;
     179             :         }
     180           0 :         Sequence< OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
     181             : 
     182           0 :         if ( serviceNames.getLength() > 0 )
     183             :         {
     184           0 :             for ( sal_Int32 index = 0; index < serviceNames.getLength(); index++ )
     185             :             {
     186           0 :                 if ( serviceNames[ index ].startsWith( providerKey ) )
     187             :                 {
     188           0 :                     OUString serviceName = serviceNames[ index ];
     189           0 :                     OUString aCommand = aCmdBase;
     190           0 :                     OUString aDisplayName = serviceName.copy( providerKey.getLength() );
     191           0 :                     if( aDisplayName == "Java" || aDisplayName == "Basic" )
     192             :                     {
     193             :                         // no entries for Java & Basic added elsewhere
     194           0 :                         break;
     195             :                     }
     196           0 :                     aCommand += aDisplayName;
     197           0 :                     aDisplayName += ellipsis;
     198           0 :                     pPopupMenu->InsertItem( itemId, aDisplayName );
     199           0 :                     pPopupMenu->SetItemCommand( itemId, aCommand );
     200           0 :                     itemId++;
     201           0 :                     break;
     202             :                 }
     203             :             }
     204             :         }
     205           0 :     }
     206           0 : }
     207             : 
     208             : }
     209             : 
     210             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11