LCOV - code coverage report
Current view: top level - libreoffice/framework/source/uielement - menubarmerger.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 179 0.0 %
Date: 2012-12-27 Functions: 0 12 0.0 %
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             : 
      21             : #include <uielement/menubarmerger.hxx>
      22             : #include <framework/addonsoptions.hxx>
      23             : 
      24             : using namespace ::com::sun::star;
      25             : 
      26             : static const char SEPARATOR_STRING[]               = "private:separator";
      27             : static const sal_uInt32 SEPARATOR_STRING_LEN       = 17;
      28             : 
      29             : static const char MERGECOMMAND_ADDAFTER[]          = "AddAfter";
      30             : static const sal_uInt32 MERGECOMMAND_ADDAFTER_LEN  = 8;
      31             : static const char MERGECOMMAND_ADDBEFORE[]         = "AddBefore";
      32             : static const sal_uInt32 MERGECOMMAND_ADDBEFORE_LEN = 9;
      33             : static const char MERGECOMMAND_REPLACE[]           = "Replace";
      34             : static const sal_uInt32 MERGECOMMAND_REPLACE_LEN   = 7;
      35             : static const char MERGECOMMAND_REMOVE[]            = "Remove";
      36             : static const sal_uInt32 MERGECOMMAND_REMOVE_LEN    = 6;
      37             : 
      38             : static const char MERGEFALLBACK_ADDPATH[]           = "AddPath";
      39             : static const char MERGEFALLBACK_ADDPATH_LEN         = 7;
      40             : static const char MERGEFALLBACK_IGNORE[]            = "Ignore";
      41             : static const char MERGEFALLBACK_IGNORE_LEN          = 6;
      42             : 
      43             : 
      44             : namespace framework
      45             : {
      46             : 
      47             : /**
      48             :  Check whether a module identifier is part of a context
      49             :  defined by a colon separated list of module identifier.
      50             : 
      51             :  @param
      52             :      rContext
      53             : 
      54             :      Describes a context string list where all contexts
      55             :      are delimited by a colon. For more information about
      56             :      the module identifier used as context strings see the
      57             :      IDL description of com::sun::star::frame::XModuleManager
      58             : 
      59             :   @param
      60             :      rModuleIdentifier
      61             : 
      62             :      A string describing a module identifier. See IDL
      63             :      description of com::sun::star::frame::XModuleManager.
      64             : 
      65             : */
      66           0 : bool MenuBarMerger::IsCorrectContext( const ::rtl::OUString& rContext, const ::rtl::OUString& rModuleIdentifier )
      67             : {
      68           0 :     return ( rContext.isEmpty() || ( rContext.indexOf( rModuleIdentifier ) >= 0 ));
      69             : }
      70             : 
      71           0 : void MenuBarMerger::RetrieveReferencePath(
      72             :     const ::rtl::OUString& rReferencePathString,
      73             :     ::std::vector< ::rtl::OUString >& rReferencePath )
      74             : {
      75           0 :     const sal_Char aDelimiter = '\\';
      76             : 
      77           0 :     rReferencePath.clear();
      78           0 :     sal_Int32 nIndex( 0 );
      79           0 :     do
      80             :     {
      81           0 :         ::rtl::OUString aToken = rReferencePathString.getToken( 0, aDelimiter, nIndex );
      82           0 :         if ( !aToken.isEmpty() )
      83           0 :             rReferencePath.push_back( aToken );
      84             :     }
      85             :     while ( nIndex >= 0 );
      86           0 : }
      87             : 
      88           0 : ReferencePathInfo MenuBarMerger::FindReferencePath(
      89             :     const ::std::vector< ::rtl::OUString >& rReferencePath,
      90             :     Menu* pMenu )
      91             : {
      92           0 :     sal_uInt32       i( 0 );
      93           0 :     const sal_uInt32 nCount( rReferencePath.size() );
      94             : 
      95             :     ReferencePathInfo aResult;
      96           0 :     if ( !nCount )
      97             :     {
      98           0 :         aResult.eResult = RP_MENUITEM_NOT_FOUND;
      99           0 :         return aResult;
     100             :     }
     101             : 
     102           0 :     Menu*            pCurrMenu( pMenu );
     103           0 :     RPResultInfo     eResult( RP_OK );
     104             : 
     105           0 :     sal_Int32  nLevel( - 1 );
     106           0 :     sal_uInt16 nPos( MENU_ITEM_NOTFOUND );
     107           0 :     do
     108             :     {
     109           0 :         ++nLevel;
     110           0 :         ::rtl::OUString aCmd( rReferencePath[i] );
     111             : 
     112           0 :         if ( i == nCount-1 )
     113             :         {
     114             :             // Check last reference path element. Must be a leave (menu item).
     115           0 :             sal_uInt16 nTmpPos = FindMenuItem( aCmd, pCurrMenu );
     116           0 :             if ( nTmpPos != MENU_ITEM_NOTFOUND )
     117           0 :                 nPos = nTmpPos;
     118           0 :             eResult = ( nTmpPos != MENU_ITEM_NOTFOUND ) ? RP_OK : RP_MENUITEM_NOT_FOUND;
     119             :         }
     120             :         else
     121             :         {
     122             :             // Check reference path element. Must be a node (popup menu)!
     123           0 :             sal_uInt16 nTmpPos = FindMenuItem( aCmd, pCurrMenu );
     124           0 :             if ( nTmpPos != MENU_ITEM_NOTFOUND )
     125             :             {
     126           0 :                 sal_uInt16 nItemId = pCurrMenu->GetItemId( nTmpPos );
     127           0 :                 Menu* pTmpMenu     = pCurrMenu->GetPopupMenu( nItemId );
     128           0 :                 if ( pTmpMenu != 0 )
     129           0 :                     pCurrMenu = pTmpMenu;
     130             :                 else
     131             :                 {
     132           0 :                     nPos    = nTmpPos;
     133           0 :                     eResult = RP_MENUITEM_INSTEAD_OF_POPUPMENU_FOUND;
     134             :                 }
     135             :             }
     136             :             else
     137           0 :                 eResult = RP_POPUPMENU_NOT_FOUND;
     138             :         }
     139           0 :         i++;
     140             :     }
     141             :     while (( pCurrMenu != 0 ) && ( i < nCount ) && ( eResult == RP_OK ));
     142             : 
     143           0 :     aResult.pPopupMenu = pCurrMenu;
     144           0 :     aResult.nPos       = nPos;
     145           0 :     aResult.nLevel     = nLevel;
     146           0 :     aResult.eResult    = eResult;
     147             : 
     148           0 :     return aResult;
     149             : }
     150             : 
     151           0 : sal_uInt16 MenuBarMerger::FindMenuItem( const ::rtl::OUString& rCmd, Menu* pCurrMenu )
     152             : {
     153           0 :     for ( sal_uInt16 i = 0; i < pCurrMenu->GetItemCount(); i++ )
     154             :     {
     155           0 :         const sal_uInt16 nItemId = pCurrMenu->GetItemId( i );
     156           0 :         if ( nItemId > 0 )
     157             :         {
     158           0 :             if ( rCmd == ::rtl::OUString( pCurrMenu->GetItemCommand( nItemId )))
     159           0 :                 return i;
     160             :         }
     161             :     }
     162             : 
     163           0 :     return MENU_ITEM_NOTFOUND;
     164             : }
     165             : 
     166           0 : bool MenuBarMerger::CreateSubMenu(
     167             :     Menu*                     pSubMenu,
     168             :     sal_uInt16&               nItemId,
     169             :     const ::rtl::OUString&    rModuleIdentifier,
     170             :     const AddonMenuContainer& rAddonSubMenu )
     171             : {
     172           0 :     const sal_uInt32 nSize = rAddonSubMenu.size();
     173           0 :     for ( sal_uInt32 i = 0; i < nSize; i++ )
     174             :     {
     175           0 :         const AddonMenuItem& rMenuItem = rAddonSubMenu[i];
     176             : 
     177           0 :         if ( IsCorrectContext( rMenuItem.aContext, rModuleIdentifier ))
     178             :         {
     179           0 :             if ( rMenuItem.aURL.equalsAsciiL( SEPARATOR_STRING, SEPARATOR_STRING_LEN ))
     180             :             {
     181           0 :                 pSubMenu->InsertSeparator( MENU_APPEND );
     182             :             }
     183             :             else
     184             :             {
     185           0 :                 pSubMenu->InsertItem( nItemId, rMenuItem.aTitle, 0, MENU_APPEND );
     186           0 :                 pSubMenu->SetItemCommand( nItemId, rMenuItem.aURL );
     187           0 :                 if ( !rMenuItem.aSubMenu.empty() )
     188             :                 {
     189           0 :                     PopupMenu* pPopupMenu = new PopupMenu();
     190           0 :                     pSubMenu->SetPopupMenu( nItemId, pPopupMenu );
     191           0 :                     ++nItemId;
     192             : 
     193           0 :                     CreateSubMenu( pPopupMenu, nItemId, rModuleIdentifier, rMenuItem.aSubMenu );
     194             :                 }
     195             :                 else
     196           0 :                     ++nItemId;
     197             :             }
     198             :         }
     199             :     }
     200             : 
     201           0 :     return true;
     202             : }
     203             : 
     204           0 : bool MenuBarMerger::MergeMenuItems(
     205             :     Menu*                     pMenu,
     206             :     sal_uInt16                nPos,
     207             :     sal_uInt16                nModIndex,
     208             :     sal_uInt16&               nItemId,
     209             :     const ::rtl::OUString&    rModuleIdentifier,
     210             :     const AddonMenuContainer& rAddonMenuItems )
     211             : {
     212           0 :     sal_uInt16       nIndex( 0 );
     213           0 :     const sal_uInt32 nSize = rAddonMenuItems.size();
     214           0 :     for ( sal_uInt32 i = 0; i < nSize; i++ )
     215             :     {
     216           0 :         const AddonMenuItem& rMenuItem = rAddonMenuItems[i];
     217             : 
     218           0 :         if ( IsCorrectContext( rMenuItem.aContext, rModuleIdentifier ))
     219             :         {
     220           0 :             if ( rMenuItem.aURL.equalsAsciiL( SEPARATOR_STRING, SEPARATOR_STRING_LEN ))
     221             :             {
     222           0 :                 pMenu->InsertSeparator( nPos+nModIndex+nIndex );
     223             :             }
     224             :             else
     225             :             {
     226           0 :                 pMenu->InsertItem( nItemId, rMenuItem.aTitle, 0, nPos+nModIndex+nIndex );
     227           0 :                 pMenu->SetItemCommand( nItemId, rMenuItem.aURL );
     228           0 :                 if ( !rMenuItem.aSubMenu.empty() )
     229             :                 {
     230           0 :                     PopupMenu* pSubMenu = new PopupMenu();
     231           0 :                     pMenu->SetPopupMenu( nItemId, pSubMenu );
     232           0 :                     ++nItemId;
     233             : 
     234           0 :                     CreateSubMenu( pSubMenu, nItemId, rModuleIdentifier, rMenuItem.aSubMenu );
     235             :                 }
     236             :                 else
     237           0 :                     ++nItemId;
     238             :             }
     239           0 :             ++nIndex;
     240             :         }
     241             :     }
     242             : 
     243           0 :     return true;
     244             : }
     245             : 
     246           0 : bool MenuBarMerger::ReplaceMenuItem(
     247             :     Menu*                     pMenu,
     248             :     sal_uInt16                nPos,
     249             :     sal_uInt16&               rItemId,
     250             :     const ::rtl::OUString&    rModuleIdentifier,
     251             :     const AddonMenuContainer& rAddonMenuItems )
     252             : {
     253             :     // There is no replace available. Therfore we first have to
     254             :     // remove the old menu entry,
     255           0 :     pMenu->RemoveItem( nPos );
     256             : 
     257           0 :     return MergeMenuItems( pMenu, nPos, 0, rItemId, rModuleIdentifier, rAddonMenuItems );
     258             : }
     259             : 
     260           0 : bool MenuBarMerger::RemoveMenuItems(
     261             :     Menu*                     pMenu,
     262             :     sal_uInt16                nPos,
     263             :     const ::rtl::OUString&    rMergeCommandParameter )
     264             : {
     265           0 :     const sal_uInt16 nParam( sal_uInt16( rMergeCommandParameter.toInt32() ));
     266           0 :     sal_uInt16       nCount( 1 );
     267             : 
     268           0 :     nCount = std::max( nParam, nCount );
     269             : 
     270           0 :     sal_uInt16 i = 0;
     271           0 :     while (( nPos < pMenu->GetItemCount() ) && ( i < nCount ))
     272             :     {
     273           0 :         pMenu->RemoveItem( nPos );
     274           0 :         ++i;
     275             :     }
     276             : 
     277           0 :     return true;
     278             : }
     279             : 
     280           0 : bool MenuBarMerger::ProcessMergeOperation(
     281             :     Menu*                     pMenu,
     282             :     sal_uInt16                nPos,
     283             :     sal_uInt16&               nItemId,
     284             :     const ::rtl::OUString&    rMergeCommand,
     285             :     const ::rtl::OUString&    rMergeCommandParameter,
     286             :     const ::rtl::OUString&    rModuleIdentifier,
     287             :     const AddonMenuContainer& rAddonMenuItems )
     288             : {
     289           0 :     sal_uInt16 nModIndex( 0 );
     290             : 
     291           0 :     if ( rMergeCommand.equalsAsciiL( MERGECOMMAND_ADDBEFORE, MERGECOMMAND_ADDBEFORE_LEN ))
     292             :     {
     293           0 :         nModIndex = 0;
     294           0 :         return MergeMenuItems( pMenu, nPos, nModIndex, nItemId, rModuleIdentifier, rAddonMenuItems );
     295             :     }
     296           0 :     else if ( rMergeCommand.equalsAsciiL( MERGECOMMAND_ADDAFTER, MERGECOMMAND_ADDAFTER_LEN ))
     297             :     {
     298           0 :         nModIndex = 1;
     299           0 :         return MergeMenuItems( pMenu, nPos, nModIndex, nItemId, rModuleIdentifier, rAddonMenuItems );
     300             :     }
     301           0 :     else if ( rMergeCommand.equalsAsciiL( MERGECOMMAND_REPLACE, MERGECOMMAND_REPLACE_LEN ))
     302             :     {
     303           0 :         return ReplaceMenuItem( pMenu, nPos, nItemId, rModuleIdentifier, rAddonMenuItems );
     304             :     }
     305           0 :     else if ( rMergeCommand.equalsAsciiL( MERGECOMMAND_REMOVE, MERGECOMMAND_REMOVE_LEN ))
     306             :     {
     307           0 :         return RemoveMenuItems( pMenu, nPos, rMergeCommandParameter );
     308             :     }
     309             : 
     310           0 :     return false;
     311             : }
     312             : 
     313           0 : bool MenuBarMerger::ProcessFallbackOperation(
     314             :     const ReferencePathInfo&                aRefPathInfo,
     315             :     sal_uInt16&                             rItemId,
     316             :     const ::rtl::OUString&                  rMergeCommand,
     317             :     const ::rtl::OUString&                  rMergeFallback,
     318             :     const ::std::vector< ::rtl::OUString >& rReferencePath,
     319             :     const ::rtl::OUString&                  rModuleIdentifier,
     320             :     const AddonMenuContainer&               rAddonMenuItems )
     321             : {
     322           0 :     if (( rMergeFallback.equalsAsciiL( MERGEFALLBACK_IGNORE, MERGEFALLBACK_IGNORE_LEN )) ||
     323           0 :         ( rMergeCommand.equalsAsciiL(  MERGECOMMAND_REPLACE, MERGECOMMAND_REPLACE_LEN )) ||
     324           0 :         ( rMergeCommand.equalsAsciiL(  MERGECOMMAND_REMOVE, MERGECOMMAND_REMOVE_LEN   )) )
     325             :     {
     326           0 :         return true;
     327             :     }
     328           0 :     else if ( rMergeFallback.equalsAsciiL( MERGEFALLBACK_ADDPATH, MERGEFALLBACK_ADDPATH_LEN ))
     329             :     {
     330           0 :         Menu*            pCurrMenu( aRefPathInfo.pPopupMenu );
     331           0 :         sal_Int32        nLevel( aRefPathInfo.nLevel );
     332           0 :         const sal_Int32  nSize( rReferencePath.size() );
     333           0 :         bool             bFirstLevel( true );
     334             : 
     335           0 :         while ( nLevel < nSize )
     336             :         {
     337           0 :             if ( nLevel == nSize-1 )
     338             :             {
     339           0 :                 const sal_uInt32 nCount = rAddonMenuItems.size();
     340           0 :                 for ( sal_uInt32 i = 0; i < nCount; ++i )
     341             :                 {
     342           0 :                     const AddonMenuItem& rMenuItem = rAddonMenuItems[i];
     343           0 :                     if ( IsCorrectContext( rMenuItem.aContext, rModuleIdentifier ))
     344             :                     {
     345           0 :                         if ( rMenuItem.aURL.equalsAsciiL( SEPARATOR_STRING, SEPARATOR_STRING_LEN ))
     346           0 :                             pCurrMenu->InsertSeparator( MENU_APPEND );
     347             :                         else
     348             :                         {
     349           0 :                             pCurrMenu->InsertItem( rItemId, rMenuItem.aTitle, 0, MENU_APPEND );
     350           0 :                             pCurrMenu->SetItemCommand( rItemId, rMenuItem.aURL );
     351           0 :                             ++rItemId;
     352             :                         }
     353             :                     }
     354             :                 }
     355             :             }
     356             :             else
     357             :             {
     358           0 :                 const ::rtl::OUString aCmd( rReferencePath[nLevel] );
     359             : 
     360           0 :                 sal_uInt16 nInsPos( MENU_APPEND );
     361           0 :                 PopupMenu* pPopupMenu( new PopupMenu );
     362             : 
     363           0 :                 if ( bFirstLevel && ( aRefPathInfo.eResult == RP_MENUITEM_INSTEAD_OF_POPUPMENU_FOUND ))
     364             :                 {
     365             :                     // special case: menu item without popup
     366           0 :                     nInsPos = aRefPathInfo.nPos;
     367           0 :                     sal_uInt16 nSetItemId = pCurrMenu->GetItemId( nInsPos );
     368           0 :                     pCurrMenu->SetItemCommand( nSetItemId, aCmd );
     369           0 :                     pCurrMenu->SetPopupMenu( nSetItemId, pPopupMenu );
     370             :                 }
     371             :                 else
     372             :                 {
     373             :                     // normal case: insert a new item with popup
     374           0 :                     pCurrMenu->InsertItem( rItemId, ::rtl::OUString(), 0, MENU_APPEND );
     375           0 :                     pCurrMenu->SetItemCommand( rItemId, aCmd );
     376           0 :                     pCurrMenu->SetPopupMenu( rItemId, pPopupMenu );
     377             :                 }
     378             : 
     379           0 :                 pCurrMenu = pPopupMenu;
     380           0 :                 ++rItemId;
     381           0 :                 bFirstLevel = false;
     382             :             }
     383           0 :             ++nLevel;
     384             :         }
     385           0 :         return true;
     386             :     }
     387             : 
     388           0 :     return false;
     389             : }
     390             : 
     391           0 : void MenuBarMerger::GetMenuEntry(
     392             :     const uno::Sequence< beans::PropertyValue >& rAddonMenuEntry,
     393             :     AddonMenuItem&                               rAddonMenuItem )
     394             : {
     395             :     // Reset submenu member
     396           0 :     rAddonMenuItem.aSubMenu.clear();
     397             : 
     398           0 :     for ( sal_Int32 i = 0; i < rAddonMenuEntry.getLength(); i++ )
     399             :     {
     400           0 :         ::rtl::OUString aMenuEntryPropName = rAddonMenuEntry[i].Name;
     401           0 :         if ( aMenuEntryPropName.equalsAsciiL( ADDONSMENUITEM_STRING_URL, ADDONSMENUITEM_URL_LEN ))
     402           0 :             rAddonMenuEntry[i].Value >>= rAddonMenuItem.aURL;
     403           0 :         else if ( aMenuEntryPropName.equalsAsciiL( ADDONSMENUITEM_STRING_TITLE, ADDONSMENUITEM_TITLE_LEN ))
     404           0 :             rAddonMenuEntry[i].Value >>= rAddonMenuItem.aTitle;
     405           0 :         else if ( aMenuEntryPropName.equalsAsciiL( ADDONSMENUITEM_STRING_TARGET, ADDONSMENUITEM_TARGET_LEN ))
     406           0 :             rAddonMenuEntry[i].Value >>= rAddonMenuItem.aTarget;
     407           0 :         else if ( aMenuEntryPropName.equalsAsciiL( ADDONSMENUITEM_STRING_SUBMENU, ADDONSMENUITEM_SUBMENU_LEN ))
     408             :         {
     409           0 :             uno::Sequence< uno::Sequence< beans::PropertyValue > > aSubMenu;
     410           0 :             rAddonMenuEntry[i].Value >>= aSubMenu;
     411           0 :             GetSubMenu( aSubMenu, rAddonMenuItem.aSubMenu );
     412             :         }
     413           0 :         else if ( aMenuEntryPropName.equalsAsciiL( ADDONSMENUITEM_STRING_CONTEXT, ADDONSMENUITEM_CONTEXT_LEN ))
     414           0 :             rAddonMenuEntry[i].Value >>= rAddonMenuItem.aContext;
     415           0 :         else if ( aMenuEntryPropName.equalsAsciiL( ADDONSMENUITEM_STRING_IMAGEIDENTIFIER, ADDONSMENUITEM_IMAGEIDENTIFIER_LEN ))
     416           0 :             rAddonMenuEntry[i].Value >>= rAddonMenuItem.aImageId;
     417           0 :     }
     418           0 : }
     419             : 
     420           0 : void MenuBarMerger::GetSubMenu(
     421             :     const uno::Sequence< uno::Sequence< beans::PropertyValue > >& rSubMenuEntries,
     422             :     AddonMenuContainer&                                           rSubMenu )
     423             : {
     424           0 :     rSubMenu.clear();
     425             : 
     426           0 :     const sal_Int32 nCount = rSubMenuEntries.getLength();
     427           0 :     rSubMenu.reserve(rSubMenu.size() + nCount);
     428           0 :     for ( sal_Int32 i = 0; i < nCount; i++ )
     429             :     {
     430           0 :         const uno::Sequence< beans::PropertyValue >& rMenuEntry = rSubMenuEntries[ i ];
     431             : 
     432           0 :         AddonMenuItem aMenuItem;
     433           0 :         GetMenuEntry( rMenuEntry, aMenuItem );
     434           0 :         rSubMenu.push_back( aMenuItem );
     435           0 :     }
     436           0 : }
     437             : 
     438             : } // namespace framework
     439             : 
     440             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10