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

Generated by: LCOV version 1.10