LCOV - code coverage report
Current view: top level - framework/source/fwe/classes - addonmenu.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 77 180 42.8 %
Date: 2015-06-13 12:38:46 Functions: 8 18 44.4 %
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 <framework/addonmenu.hxx>
      21             : #include <framework/addonsoptions.hxx>
      22             : #include <general.h>
      23             : #include <framework/imageproducer.hxx>
      24             : #include <framework/menuconfiguration.hxx>
      25             : #include <services.h>
      26             : 
      27             : #include <com/sun/star/uno/Reference.hxx>
      28             : #include <com/sun/star/util/URL.hpp>
      29             : #include <com/sun/star/util/XURLTransformer.hpp>
      30             : #include <com/sun/star/frame/ModuleManager.hpp>
      31             : #include <com/sun/star/frame/XModuleManager.hpp>
      32             : 
      33             : #include <tools/config.hxx>
      34             : #include <vcl/svapp.hxx>
      35             : #include <svtools/menuoptions.hxx>
      36             : #include <svl/solar.hrc>
      37             : 
      38             : using namespace ::com::sun::star::uno;
      39             : using namespace ::com::sun::star::lang;
      40             : using namespace ::com::sun::star::frame;
      41             : using namespace ::com::sun::star::beans;
      42             : 
      43             : // Please look at sfx2/inc/sfxsids.hrc the values are defined there. Due to build dependencies
      44             : // we cannot include the header file.
      45             : const sal_uInt16 SID_HELPMENU            = (SID_SFX_START + 410);
      46             : 
      47             : namespace framework
      48             : {
      49             : 
      50         606 : AddonMenu::AddonMenu( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame ) :
      51         606 :     m_xFrame( rFrame )
      52             : {
      53         606 : }
      54             : 
      55        1212 : AddonMenu::~AddonMenu()
      56             : {
      57        9696 :     for ( sal_uInt16 i = 0; i < GetItemCount(); i++ )
      58             :     {
      59        9090 :         if ( GetItemType( i ) != MenuItemType::SEPARATOR )
      60             :         {
      61        7272 :             sal_uInt16 nId = GetItemId( i );
      62        7272 :             delete GetPopupMenu( nId );
      63             :         }
      64             :     }
      65         606 : }
      66             : 
      67             : // Check if command URL string has the unique prefix to identify addon popup menus
      68           0 : bool AddonPopupMenu::IsCommandURLPrefix( const OUString& aCmdURL )
      69             : {
      70           0 :     return aCmdURL.startsWith( ADDONSPOPUPMENU_URL_PREFIX_STR );
      71             : }
      72             : 
      73           0 : AddonPopupMenu::AddonPopupMenu( const com::sun::star::uno::Reference< com::sun::star::frame::XFrame >& rFrame ) :
      74           0 :     AddonMenu( rFrame )
      75             : {
      76           0 : }
      77             : 
      78           0 : AddonPopupMenu::~AddonPopupMenu()
      79             : {
      80           0 : }
      81             : 
      82        1193 : static OUString GetModuleIdentifier(const Reference<XComponentContext>& rContext,
      83             :     const Reference< XFrame >& rFrame)
      84             : {
      85        1193 :     Reference< XModuleManager > xModuleManager(ModuleManager::create(rContext));
      86        1193 :     if ( xModuleManager.is() )
      87             :     {
      88             :         try
      89             :         {
      90        1193 :             return xModuleManager->identify( rFrame );
      91             :         }
      92           0 :         catch ( Exception& )
      93             :         {
      94             :         }
      95             :     }
      96           0 :     return OUString();
      97             : }
      98             : 
      99         597 : bool AddonMenuManager::HasAddonMenuElements()
     100             : {
     101         597 :     return AddonsOptions().HasAddonsMenu();
     102             : }
     103             : 
     104             : // Factory method to create different Add-On menu types
     105           0 : PopupMenu* AddonMenuManager::CreatePopupMenuType( MenuType eMenuType, const Reference< XFrame >& rFrame )
     106             : {
     107           0 :     if ( eMenuType == ADDON_MENU )
     108           0 :         return new AddonMenu( rFrame );
     109           0 :     else if ( eMenuType == ADDON_POPUPMENU )
     110           0 :         return new AddonPopupMenu( rFrame );
     111             :     else
     112           0 :         return NULL;
     113             : }
     114             : 
     115             : // Create the Add-Ons menu
     116           0 : AddonMenu* AddonMenuManager::CreateAddonMenu( const Reference< XFrame >& rFrame,
     117             :                                               const Reference< XComponentContext >& rContext )
     118             : {
     119           0 :     AddonsOptions aOptions;
     120           0 :     AddonMenu*  pAddonMenu      = NULL;
     121           0 :     sal_uInt16      nUniqueMenuId   = ADDONMENU_ITEMID_START;
     122             : 
     123           0 :     const Sequence< Sequence< PropertyValue > >& rAddonMenuEntries = aOptions.GetAddonsMenu();
     124           0 :     if ( rAddonMenuEntries.getLength() > 0 )
     125             :     {
     126           0 :         pAddonMenu = static_cast<AddonMenu *>(AddonMenuManager::CreatePopupMenuType( ADDON_MENU, rFrame ));
     127           0 :         ::rtl::OUString aModuleIdentifier = GetModuleIdentifier( rContext, rFrame );
     128           0 :         AddonMenuManager::BuildMenu( pAddonMenu, ADDON_MENU, MENU_APPEND, nUniqueMenuId, rAddonMenuEntries, rFrame, aModuleIdentifier );
     129             : 
     130             :         // Don't return an empty Add-On menu
     131           0 :         if ( pAddonMenu->GetItemCount() == 0 )
     132             :         {
     133           0 :             delete pAddonMenu;
     134           0 :             pAddonMenu = NULL;
     135           0 :         }
     136             :     }
     137             : 
     138           0 :     return pAddonMenu;
     139             : }
     140             : 
     141             : // Returns the next insert position from nPos.
     142           0 : sal_uInt16 AddonMenuManager::GetNextPos( sal_uInt16 nPos )
     143             : {
     144           0 :     return ( nPos == MENU_APPEND ) ? MENU_APPEND : ( nPos+1 );
     145             : }
     146             : 
     147        3802 : static sal_uInt16 FindMenuId( Menu* pMenu, const OUString& aCommand )
     148             : {
     149        3802 :     sal_uInt16 nPos = 0;
     150        3802 :     OUString aCmd;
     151       12278 :     for ( nPos = 0; nPos < pMenu->GetItemCount(); nPos++ )
     152             :     {
     153        9670 :         sal_uInt16 nId = pMenu->GetItemId( nPos );
     154        9670 :         aCmd = pMenu->GetItemCommand( nId );
     155        9670 :         if ( aCmd == aCommand )
     156        1194 :             return nId;
     157             :     }
     158             : 
     159        2608 :     return USHRT_MAX;
     160             : }
     161             : 
     162             : // Merge the Add-Ons help menu items into the given menu bar at a defined pos
     163        3205 : void AddonMenuManager::MergeAddonHelpMenu( const Reference< XFrame >& rFrame,
     164             :                                            MenuBar* pMergeMenuBar,
     165             :                                            const Reference<XComponentContext>& rContext )
     166             : {
     167        3205 :     if ( pMergeMenuBar )
     168             :     {
     169        3205 :         PopupMenu* pHelpMenu = pMergeMenuBar->GetPopupMenu( SID_HELPMENU );
     170        3205 :         if ( !pHelpMenu )
     171             :         {
     172        3205 :             sal_uInt16 nId = FindMenuId(pMergeMenuBar, OUString(".uno:HelpMenu"));
     173        3205 :             if ( nId != USHRT_MAX )
     174         597 :                 pHelpMenu = pMergeMenuBar->GetPopupMenu( nId );
     175             :         }
     176             : 
     177        3205 :         if ( pHelpMenu )
     178             :         {
     179             :             // Add-Ons help menu items should be inserted after the "registration" menu item
     180         597 :             sal_uInt16 nItemCount       = pHelpMenu->GetItemCount();
     181         597 :             sal_uInt16 nInsSepAfterPos  = MENU_APPEND;
     182         597 :             sal_uInt16 nUniqueMenuId    = ADDONMENU_ITEMID_START;
     183         597 :             AddonsOptions aOptions;
     184             : 
     185             :             // try to detect the about menu item with the command URL
     186         597 :             sal_uInt16 nId = FindMenuId(pHelpMenu, OUString(".uno:About"));
     187         597 :             sal_uInt16 nInsPos = pHelpMenu->GetItemPos( nId );
     188             : 
     189         597 :             const Sequence< Sequence< PropertyValue > >& rAddonHelpMenuEntries = aOptions.GetAddonsHelpMenu();
     190             : 
     191         597 :             if ( nInsPos < nItemCount && pHelpMenu->GetItemType( nInsPos ) != MenuItemType::SEPARATOR )
     192         597 :                 nInsSepAfterPos = nInsPos;
     193             : 
     194        1194 :             ::rtl::OUString aModuleIdentifier = GetModuleIdentifier(rContext, rFrame);
     195         597 :             AddonMenuManager::BuildMenu( pHelpMenu, ADDON_MENU, nInsPos, nUniqueMenuId, rAddonHelpMenuEntries, rFrame, aModuleIdentifier );
     196             : 
     197         597 :             if ( pHelpMenu->GetItemCount() > nItemCount )
     198             :             {
     199           0 :                 if ( nInsSepAfterPos < MENU_APPEND )
     200             :                 {
     201           0 :                     nInsSepAfterPos += ( pHelpMenu->GetItemCount() - nItemCount );
     202           0 :                     if ( pHelpMenu->GetItemType( nInsSepAfterPos ) != MenuItemType::SEPARATOR )
     203           0 :                         pHelpMenu->InsertSeparator(OString(), nInsSepAfterPos);
     204             :                 }
     205           0 :                 pHelpMenu->InsertSeparator(OString(), nItemCount);
     206         597 :             }
     207             :         }
     208             :     }
     209        3205 : }
     210             : 
     211             : // Merge the addon popup menus into the given menu bar at the provided pos.
     212         596 : void AddonMenuManager::MergeAddonPopupMenus( const Reference< XFrame >& rFrame,
     213             :                                              sal_uInt16               nMergeAtPos,
     214             :                                              MenuBar*             pMergeMenuBar,
     215             :                                              const Reference< XComponentContext >& rContext )
     216             : {
     217         596 :     if ( pMergeMenuBar )
     218             :     {
     219         596 :         AddonsOptions   aAddonsOptions;
     220         596 :         sal_uInt16          nInsertPos = nMergeAtPos;
     221             : 
     222        1192 :         OUString                              aTitle;
     223        1192 :         OUString                              aURL;
     224        1192 :         OUString                              aTarget;
     225        1192 :         OUString                              aImageId;
     226        1192 :         OUString                              aContext;
     227        1192 :         Sequence< Sequence< PropertyValue > > aAddonSubMenu;
     228         596 :         sal_uInt16                            nUniqueMenuId = ADDONMENU_ITEMID_START;
     229        1192 :         OUString                              aModuleIdentifier = GetModuleIdentifier(rContext, rFrame);
     230             : 
     231         596 :         const Sequence< Sequence< PropertyValue > >&    rAddonMenuEntries = aAddonsOptions.GetAddonsMenuBarPart();
     232         596 :         for ( sal_Int32 i = 0; i < rAddonMenuEntries.getLength(); i++ )
     233             :         {
     234           0 :             AddonMenuManager::GetMenuEntry( rAddonMenuEntries[i],
     235             :                                             aTitle,
     236             :                                             aURL,
     237             :                                             aTarget,
     238             :                                             aImageId,
     239             :                                             aContext,
     240           0 :                                             aAddonSubMenu );
     241           0 :             if ( !aTitle.isEmpty() &&
     242           0 :                  !aURL.isEmpty()   &&
     243           0 :                  aAddonSubMenu.getLength() > 0 &&
     244           0 :                  AddonMenuManager::IsCorrectContext( aModuleIdentifier, aContext ))
     245             :             {
     246           0 :                 sal_uInt16          nId             = nUniqueMenuId++;
     247           0 :                 AddonPopupMenu* pAddonPopupMenu = static_cast<AddonPopupMenu *>(AddonMenuManager::CreatePopupMenuType( ADDON_POPUPMENU, rFrame ));
     248             : 
     249           0 :                 AddonMenuManager::BuildMenu( pAddonPopupMenu, ADDON_MENU, MENU_APPEND, nUniqueMenuId, aAddonSubMenu, rFrame, aModuleIdentifier );
     250             : 
     251           0 :                 if ( pAddonPopupMenu->GetItemCount() > 0 )
     252             :                 {
     253           0 :                     pAddonPopupMenu->SetCommandURL( aURL );
     254           0 :                     pMergeMenuBar->InsertItem( nId, aTitle, MenuItemBits::NONE, OString(), nInsertPos++ );
     255           0 :                     pMergeMenuBar->SetPopupMenu( nId, pAddonPopupMenu );
     256             : 
     257             :                     // Store the command URL into the VCL menu bar for later identification
     258           0 :                     pMergeMenuBar->SetItemCommand( nId, aURL );
     259             :                 }
     260             :                 else
     261           0 :                     delete pAddonPopupMenu;
     262             :             }
     263         596 :         }
     264             :     }
     265         596 : }
     266             : 
     267             : // Insert the menu and sub menu entries into pCurrentMenu with the aAddonMenuDefinition provided
     268         597 : void AddonMenuManager::BuildMenu( PopupMenu*                            pCurrentMenu,
     269             :                                   MenuType                              nSubMenuType,
     270             :                                   sal_uInt16                            nInsPos,
     271             :                                   sal_uInt16&                           nUniqueMenuId,
     272             :                                   const Sequence< Sequence< PropertyValue > >& aAddonMenuDefinition,
     273             :                                   const Reference< XFrame >&            rFrame,
     274             :                                   const ::rtl::OUString&               rModuleIdentifier )
     275             : {
     276         597 :     Sequence< Sequence< PropertyValue > >   aAddonSubMenu;
     277         597 :     bool                                    bInsertSeparator    = false;
     278         597 :     sal_uInt32                              i                   = 0;
     279         597 :     sal_uInt32                              nElements           = 0;
     280         597 :     sal_uInt32                              nCount              = aAddonMenuDefinition.getLength();
     281        1194 :     AddonsOptions                           aAddonsOptions;
     282             : 
     283        1194 :     OUString aTitle;
     284        1194 :     OUString aURL;
     285        1194 :     OUString aTarget;
     286        1194 :     OUString aImageId;
     287        1194 :     OUString aContext;
     288             : 
     289         597 :     for ( i = 0; i < nCount; ++i )
     290             :     {
     291           0 :         GetMenuEntry( aAddonMenuDefinition[i], aTitle, aURL, aTarget, aImageId, aContext, aAddonSubMenu );
     292             : 
     293           0 :         if ( !IsCorrectContext( rModuleIdentifier, aContext ) || ( aTitle.isEmpty() && aURL.isEmpty() ))
     294           0 :             continue;
     295             : 
     296           0 :         if ( aURL == "private:separator" )
     297           0 :             bInsertSeparator = true;
     298             :         else
     299             :         {
     300           0 :             PopupMenu* pSubMenu = NULL;
     301           0 :             if ( aAddonSubMenu.getLength() > 0 )
     302             :             {
     303           0 :                 pSubMenu = AddonMenuManager::CreatePopupMenuType( nSubMenuType, rFrame );
     304           0 :                 AddonMenuManager::BuildMenu( pSubMenu, nSubMenuType, MENU_APPEND, nUniqueMenuId, aAddonSubMenu, rFrame, rModuleIdentifier );
     305             : 
     306             :                 // Don't create a menu item for an empty sub menu
     307           0 :                 if ( pSubMenu->GetItemCount() == 0 )
     308             :                 {
     309           0 :                     delete pSubMenu;
     310           0 :                     pSubMenu =  NULL;
     311           0 :                     continue;
     312             :                 }
     313             :             }
     314             : 
     315           0 :             if ( bInsertSeparator && nElements > 0 )
     316             :             {
     317             :                 // Insert a separator only when we insert a new element afterwards and we
     318             :                 // have already one before us
     319           0 :                 nElements = 0;
     320           0 :                 bInsertSeparator = false;
     321           0 :                 pCurrentMenu->InsertSeparator(OString(), nInsPos);
     322           0 :                 nInsPos = AddonMenuManager::GetNextPos( nInsPos );
     323             :             }
     324             : 
     325           0 :             sal_uInt16 nId = nUniqueMenuId++;
     326           0 :             pCurrentMenu->InsertItem(nId, aTitle, MenuItemBits::NONE, OString(), nInsPos);
     327           0 :             nInsPos = AddonMenuManager::GetNextPos( nInsPos );
     328             : 
     329           0 :             ++nElements;
     330             : 
     331             :             // Store values from configuration to the New and Wizard menu entries to enable
     332             :             // sfx2 based code to support high contrast mode correctly!
     333           0 :             sal_uIntPtr nAttributePtr = MenuAttributes::CreateAttribute(aTarget, aImageId);
     334           0 :             pCurrentMenu->SetUserValue(nId, nAttributePtr, MenuAttributes::ReleaseAttribute);
     335           0 :             pCurrentMenu->SetItemCommand( nId, aURL );
     336             : 
     337           0 :             if ( pSubMenu )
     338           0 :                 pCurrentMenu->SetPopupMenu( nId, pSubMenu );
     339             :         }
     340         597 :     }
     341         597 : }
     342             : 
     343             : // Retrieve the menu entry property values from a sequence
     344           0 : void AddonMenuManager::GetMenuEntry( const Sequence< PropertyValue >& rAddonMenuEntry,
     345             :                                      OUString& rTitle,
     346             :                                      OUString& rURL,
     347             :                                      OUString& rTarget,
     348             :                                      OUString& rImageId,
     349             :                                      OUString& rContext,
     350             :                                      Sequence< Sequence< PropertyValue > >& rAddonSubMenu )
     351             : {
     352             :     // Reset submenu parameter
     353           0 :     rAddonSubMenu   = Sequence< Sequence< PropertyValue > >();
     354             : 
     355           0 :     for ( int i = 0; i < rAddonMenuEntry.getLength(); i++ )
     356             :     {
     357           0 :         OUString aMenuEntryPropName = rAddonMenuEntry[i].Name;
     358           0 :         if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_URL )
     359           0 :             rAddonMenuEntry[i].Value >>= rURL;
     360           0 :         else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_TITLE )
     361           0 :             rAddonMenuEntry[i].Value >>= rTitle;
     362           0 :         else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_TARGET )
     363           0 :             rAddonMenuEntry[i].Value >>= rTarget;
     364           0 :         else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_IMAGEIDENTIFIER )
     365           0 :             rAddonMenuEntry[i].Value >>= rImageId;
     366           0 :         else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_SUBMENU )
     367           0 :             rAddonMenuEntry[i].Value >>= rAddonSubMenu;
     368           0 :         else if ( aMenuEntryPropName == ADDONSMENUITEM_STRING_CONTEXT )
     369           0 :             rAddonMenuEntry[i].Value >>= rContext;
     370           0 :     }
     371           0 : }
     372             : 
     373             : // Check if the context string matches the provided xModel context
     374           0 : bool AddonMenuManager::IsCorrectContext( const OUString& rModuleIdentifier, const OUString& rContext )
     375             : {
     376           0 :     if ( rContext.isEmpty() )
     377           0 :         return true;
     378             : 
     379           0 :     if ( !rModuleIdentifier.isEmpty() )
     380             :     {
     381           0 :         sal_Int32 nIndex = rContext.indexOf( rModuleIdentifier );
     382           0 :         return ( nIndex >= 0 );
     383             :     }
     384             : 
     385           0 :     return false;
     386             : }
     387             : 
     388             : }
     389             : 
     390             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11