LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/vbahelper/source/vbahelper - vbacommandbars.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 99 1.0 %
Date: 2013-07-09 Functions: 2 18 11.1 %
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             : #include <com/sun/star/lang/XServiceInfo.hpp>
      20             : #include <com/sun/star/frame/XDesktop.hpp>
      21             : #include <com/sun/star/container/XNameAccess.hpp>
      22             : #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
      23             : #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
      24             : #include <com/sun/star/ui/XModuleUIConfigurationManager.hpp>
      25             : #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
      26             : #include <ooo/vba/office/MsoBarType.hpp>
      27             : 
      28             : #include "vbacommandbars.hxx"
      29             : #include "vbacommandbar.hxx"
      30             : 
      31             : using namespace com::sun::star;
      32             : using namespace ooo::vba;
      33             : 
      34             : 
      35             : typedef ::cppu::WeakImplHelper1< container::XEnumeration > CommandBarEnumeration_BASE;
      36             : 
      37           0 : class CommandBarEnumeration : public CommandBarEnumeration_BASE
      38             : {
      39             :     uno::Reference< XHelperInterface > m_xParent;
      40             :     uno::Reference< uno::XComponentContext > m_xContext;
      41             :     VbaCommandBarHelperRef m_pCBarHelper;
      42             :     uno::Sequence< OUString > m_sNames;
      43             :     sal_Int32 m_nCurrentPosition;
      44             : public:
      45           0 :     CommandBarEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, VbaCommandBarHelperRef pHelper) throw ( uno::RuntimeException ) : m_xParent( xParent ), m_xContext( xContext ), m_pCBarHelper( pHelper ) , m_nCurrentPosition( 0 )
      46             :     {
      47           0 :         uno::Reference< container::XNameAccess > xNameAccess = m_pCBarHelper->getPersistentWindowState();
      48           0 :         m_sNames = xNameAccess->getElementNames();
      49           0 :     }
      50           0 :     virtual sal_Bool SAL_CALL hasMoreElements() throw ( uno::RuntimeException )
      51             :     {
      52           0 :         if( m_nCurrentPosition < m_sNames.getLength() )
      53           0 :             return sal_True;
      54           0 :         return sal_False;
      55             :     }
      56           0 :     virtual uno::Any SAL_CALL nextElement() throw ( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
      57             :     {
      58             :         // FIXME: should be add menubar
      59           0 :         if( hasMoreElements() )
      60             :         {
      61           0 :             OUString sResourceUrl( m_sNames[ m_nCurrentPosition++ ] );
      62           0 :             if( sResourceUrl.indexOf( "private:resource/toolbar/" ) != -1 )
      63             :             {
      64           0 :                 uno::Reference< container::XIndexAccess > xCBarSetting = m_pCBarHelper->getSettings( sResourceUrl );
      65           0 :                 uno::Reference< XCommandBar > xCommandBar( new ScVbaCommandBar( m_xParent, m_xContext, m_pCBarHelper, xCBarSetting, sResourceUrl, sal_False ) );
      66             :                 // Strange, shouldn't the Enumeration support match/share the
      67             :                 // iteration code? ( e.g. ScVbaCommandBars::Item(...) )
      68             :                 // and we at least should return here ( something ) it seems
      69           0 :                 return uno::makeAny( xCommandBar );
      70             :              }
      71             :              else
      72           0 :                 return nextElement();
      73             :         }
      74             :         else
      75           0 :             throw container::NoSuchElementException();
      76             :     }
      77             : };
      78             : 
      79           0 : ScVbaCommandBars::ScVbaCommandBars( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XIndexAccess >& xIndexAccess, const uno::Reference< frame::XModel >& xModel ) throw ( uno::RuntimeException ) : CommandBars_BASE( xParent, xContext, xIndexAccess )
      80             : {
      81           0 :     m_pCBarHelper.reset( new VbaCommandBarHelper( mxContext, xModel ) );
      82           0 :     m_xNameAccess = m_pCBarHelper->getPersistentWindowState();
      83           0 : }
      84             : 
      85           0 : ScVbaCommandBars::~ScVbaCommandBars()
      86             : {
      87           0 : }
      88             : 
      89             : // XEnumerationAccess
      90             : uno::Type SAL_CALL
      91           0 : ScVbaCommandBars::getElementType() throw ( uno::RuntimeException )
      92             : {
      93           0 :     return XCommandBar::static_type( 0 );
      94             : }
      95             : 
      96             : uno::Reference< container::XEnumeration >
      97           0 : ScVbaCommandBars::createEnumeration() throw ( uno::RuntimeException )
      98             : {
      99           0 :     return uno::Reference< container::XEnumeration >( new CommandBarEnumeration( this, mxContext, m_pCBarHelper ) );
     100             : }
     101             : 
     102             : uno::Any
     103           0 : ScVbaCommandBars::createCollectionObject( const uno::Any& aSource )
     104             : {
     105             :     // aSource should be a name at this time, because of the class is API wrapper.
     106           0 :     OUString sResourceUrl;
     107           0 :     uno::Reference< container::XIndexAccess > xBarSettings;
     108           0 :     OUString sBarName;
     109           0 :     sal_Bool bMenu = sal_False;
     110           0 :     uno::Any aRet;
     111             : 
     112           0 :     if( aSource >>= sBarName )
     113             :     {
     114             :         // some built-in command bars
     115           0 :         if( m_pCBarHelper->getModuleId() == "com.sun.star.sheet.SpreadsheetDocument" )
     116             :         {
     117           0 :             if( sBarName.equalsIgnoreAsciiCase( "Worksheet Menu Bar" ) )
     118             :             {
     119             :                 // spreadsheet menu bar
     120           0 :                 sResourceUrl = ITEM_MENUBAR_URL;
     121           0 :                 bMenu = sal_True;
     122             :             }
     123           0 :             else if( sBarName.equalsIgnoreAsciiCase( "Cell" ) )
     124             :             {
     125             :                 // EVIL HACK (tm): spreadsheet cell context menu as dummy object without functionality
     126           0 :                 aRet <<= uno::Reference< XCommandBar >( new VbaDummyCommandBar( this, mxContext, sBarName, office::MsoBarType::msoBarTypePopup ) );
     127             :             }
     128             :         }
     129           0 :         else if( m_pCBarHelper->getModuleId() == "com.sun.star.text.TextDocument" )
     130             :         {
     131           0 :             if( sBarName.equalsIgnoreAsciiCase( "Menu Bar" ) )
     132             :             {
     133             :                 // text processor menu bar
     134           0 :                 sResourceUrl = ITEM_MENUBAR_URL;
     135           0 :                 bMenu = sal_True;
     136             :             }
     137             :         }
     138             : 
     139             :         // nothing found - try to resolve from name
     140           0 :         if( !aRet.hasValue() && sResourceUrl.isEmpty() )
     141             :         {
     142           0 :             sResourceUrl = m_pCBarHelper->findToolbarByName( m_xNameAccess, sBarName );
     143           0 :             bMenu = sal_False;
     144             :         }
     145             :     }
     146             : 
     147           0 :     if( !sResourceUrl.isEmpty() )
     148             :     {
     149           0 :         xBarSettings = m_pCBarHelper->getSettings( sResourceUrl );
     150           0 :         aRet <<= uno::Reference< XCommandBar >( new ScVbaCommandBar( this, mxContext, m_pCBarHelper, xBarSettings, sResourceUrl, bMenu ) );
     151             :     }
     152             : 
     153           0 :     if( !aRet.hasValue() )
     154           0 :         throw uno::RuntimeException( "Toolbar do not exist" , uno::Reference< uno::XInterface >() );
     155             : 
     156           0 :     return aRet;
     157             : }
     158             : 
     159             : // XCommandBars
     160             : uno::Reference< XCommandBar > SAL_CALL
     161           0 : ScVbaCommandBars::Add( const css::uno::Any& Name, const css::uno::Any& /*Position*/, const css::uno::Any& /*MenuBar*/, const css::uno::Any& /*Temporary*/ ) throw (css::script::BasicErrorException, css::uno::RuntimeException)
     162             : {
     163             :     // FIXME: only support to add Toolbar
     164             :     // Position - MsoBar MenuBar - sal_Bool
     165             :     // Currently only the Name is supported.
     166           0 :     OUString sName;
     167           0 :     if( Name.hasValue() )
     168           0 :         Name >>= sName;
     169             : 
     170           0 :     OUString sResourceUrl;
     171           0 :     if( !sName.isEmpty() )
     172             :     {
     173           0 :         sResourceUrl = m_pCBarHelper->findToolbarByName( m_xNameAccess, sName );
     174           0 :         if( !sResourceUrl.isEmpty() )
     175           0 :             throw uno::RuntimeException( "Toolbar exists" , uno::Reference< uno::XInterface >() );
     176             :     }
     177             :     else
     178             :     {
     179           0 :         sName = "Custom1";
     180             :     }
     181             : 
     182           0 :     sResourceUrl = VbaCommandBarHelper::generateCustomURL();
     183           0 :     uno::Reference< container::XIndexAccess > xBarSettings( m_pCBarHelper->getSettings( sResourceUrl ), uno::UNO_QUERY_THROW );
     184           0 :     uno::Reference< XCommandBar > xCBar( new ScVbaCommandBar( this, mxContext, m_pCBarHelper, xBarSettings, sResourceUrl, sal_False ) );
     185           0 :     xCBar->setName( sName );
     186           0 :     return xCBar;
     187             : }
     188             : sal_Int32 SAL_CALL
     189           0 : ScVbaCommandBars::getCount() throw(css::uno::RuntimeException)
     190             : {
     191             :     // Filter out all toolbars from the window collection
     192           0 :     sal_Int32 nCount = 1; // there is a Menubar in OOo
     193           0 :     uno::Sequence< ::OUString > allNames = m_xNameAccess->getElementNames();
     194           0 :     for( sal_Int32 i = 0; i < allNames.getLength(); i++ )
     195             :     {
     196           0 :         if(allNames[i].indexOf( "private:resource/toolbar/" ) != -1 )
     197             :         {
     198           0 :             nCount++;
     199             :         }
     200             :     }
     201           0 :     return nCount;
     202             : }
     203             : 
     204             : // ScVbaCollectionBaseImpl
     205             : uno::Any SAL_CALL
     206           0 : ScVbaCommandBars::Item( const uno::Any& aIndex, const uno::Any& /*aIndex2*/ ) throw( uno::RuntimeException )
     207             : {
     208           0 :     if( aIndex.getValueTypeClass() == uno::TypeClass_STRING )
     209             :     {
     210           0 :         return createCollectionObject( aIndex );
     211             :     }
     212             : 
     213             :     // hardcode if "aIndex = 1" that would return "main menu".
     214           0 :     sal_Int16 nIndex = 0;
     215           0 :     aIndex >>= nIndex;
     216           0 :     if( nIndex == 1 )
     217             :     {
     218           0 :         uno::Any aSource;
     219           0 :         if( m_pCBarHelper->getModuleId() == "com.sun.star.sheet.SpreadsheetDocument" )
     220           0 :             aSource <<= OUString("Worksheet Menu Bar");
     221           0 :         else if( m_pCBarHelper->getModuleId() == "com.sun.star.text.TextDocument" )
     222           0 :             aSource <<= OUString("Menu Bar");
     223           0 :         if( aSource.hasValue() )
     224           0 :             return createCollectionObject( aSource );
     225             :     }
     226           0 :     return uno::Any();
     227             : }
     228             : 
     229             : // XHelperInterface
     230             : OUString
     231           0 : ScVbaCommandBars::getServiceImplName()
     232             : {
     233           0 :     return OUString("ScVbaCommandBars");
     234             : }
     235             : 
     236             : uno::Sequence<OUString>
     237           0 : ScVbaCommandBars::getServiceNames()
     238             : {
     239           0 :     static uno::Sequence< OUString > aServiceNames;
     240           0 :     if ( aServiceNames.getLength() == 0 )
     241             :     {
     242           0 :         aServiceNames.realloc( 1 );
     243           0 :         aServiceNames[ 0 ] = "ooo.vba.CommandBars";
     244             :     }
     245           0 :     return aServiceNames;
     246         186 : }
     247             : 
     248             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10