LCOV - code coverage report
Current view: top level - vbahelper/source/vbahelper - vbacommandbarhelper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 127 0.8 %
Date: 2014-11-03 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 "vbacommandbarhelper.hxx"
      20             : #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
      21             : #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
      22             : #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
      23             : #include <com/sun/star/ui/XModuleUIConfigurationManager.hpp>
      24             : #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
      25             : #include <com/sun/star/ui/XUIElement.hpp>
      26             : #include <com/sun/star/ui/UIElementType.hpp>
      27             : #include <com/sun/star/ui/theWindowStateConfiguration.hpp>
      28             : #include <comphelper/processfactory.hxx>
      29             : #include <comphelper/random.hxx>
      30             : #include <vbahelper/vbahelper.hxx>
      31             : #include <rtl/ustrbuf.hxx>
      32             : #include <time.h>
      33             : #include <map>
      34             : 
      35             : using namespace com::sun::star;
      36             : using namespace ooo::vba;
      37             : 
      38             : typedef std::map< OUString, OUString > MSO2OOCommandbarMap;
      39             : 
      40             : class MSO2OOCommandbarHelper
      41             : {
      42             : private:
      43             :     static MSO2OOCommandbarHelper* pMSO2OOCommandbarHelper;
      44             :     MSO2OOCommandbarMap maBuildinToolbarMap;
      45             : 
      46           0 :     MSO2OOCommandbarHelper()
      47           0 :     {
      48             :         // Buildin toolbars
      49           0 :         maBuildinToolbarMap.insert( std::make_pair( OUString("Standard") ,    OUString("private:resource/toolbar/standardbar" ) ) );
      50           0 :         maBuildinToolbarMap.insert( std::make_pair( OUString("Formatting"),   OUString("private:resource/toolbar/formatobjectbar") ) );
      51           0 :         maBuildinToolbarMap.insert( std::make_pair( OUString("Drawing"),      OUString("private:resource/toolbar/drawbar") ) );
      52           0 :         maBuildinToolbarMap.insert( std::make_pair( OUString("Toolbar List"), OUString("private:resource/toolbar/toolbar") ) );
      53           0 :         maBuildinToolbarMap.insert( std::make_pair( OUString("Forms"),        OUString("private:resource/toolbar/formcontrols") ) );
      54           0 :         maBuildinToolbarMap.insert( std::make_pair( OUString("Form Controls"),OUString("private:resource/toolbar/formcontrols") ) );
      55           0 :         maBuildinToolbarMap.insert( std::make_pair( OUString("Full Screen"),  OUString("private:resource/toolbar/fullscreenbar") ) );
      56           0 :         maBuildinToolbarMap.insert( std::make_pair( OUString("Chart"),        OUString("private:resource/toolbar/flowchartshapes") ) );
      57           0 :         maBuildinToolbarMap.insert( std::make_pair( OUString("Picture"),      OUString("private:resource/toolbar/graphicobjectbar") ) );
      58           0 :         maBuildinToolbarMap.insert( std::make_pair( OUString("WordArt"),      OUString("private:resource/toolbar/fontworkobjectbar") ) );
      59           0 :         maBuildinToolbarMap.insert( std::make_pair( OUString("3-D Settings"), OUString("private:resource/toolbar/extrusionobjectbar") ) );
      60           0 :     }
      61             : 
      62             : public:
      63           0 :     virtual ~MSO2OOCommandbarHelper() {};
      64           0 :     static MSO2OOCommandbarHelper* getMSO2OOCommandbarHelper()
      65             :     {
      66           0 :         if( pMSO2OOCommandbarHelper == NULL )
      67             :         {
      68           0 :             pMSO2OOCommandbarHelper = new MSO2OOCommandbarHelper();
      69             :         }
      70           0 :         return pMSO2OOCommandbarHelper;
      71             :     }
      72             : 
      73           0 :     OUString findBuildinToolbar( const OUString& sToolbarName )
      74             :     {
      75           0 :         MSO2OOCommandbarMap::iterator it = maBuildinToolbarMap.begin();
      76           0 :         for(; it != maBuildinToolbarMap.end(); ++it )
      77             :         {
      78           0 :             OUString sName = it->first;
      79           0 :             if( sName.equalsIgnoreAsciiCase( sToolbarName ) )
      80           0 :                 return it->second;
      81           0 :         }
      82           0 :         return OUString();
      83             :     }
      84             : };
      85             : 
      86             : MSO2OOCommandbarHelper* MSO2OOCommandbarHelper::pMSO2OOCommandbarHelper = NULL;
      87             : 
      88             : 
      89           0 : VbaCommandBarHelper::VbaCommandBarHelper( const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::frame::XModel >& xModel ) throw (css::uno::RuntimeException) : mxContext( xContext ), mxModel( xModel )
      90             : {
      91           0 :     Init();
      92           0 : }
      93             : 
      94           0 : void VbaCommandBarHelper::Init( ) throw (css::uno::RuntimeException)
      95             : {
      96           0 :     uno::Reference< css::ui::XUIConfigurationManagerSupplier > xUICfgSupplier( mxModel, uno::UNO_QUERY_THROW );
      97           0 :     m_xDocCfgMgr = xUICfgSupplier->getUIConfigurationManager();
      98             : 
      99           0 :     uno::Reference< lang::XServiceInfo > xServiceInfo( mxModel, uno::UNO_QUERY_THROW );
     100           0 :     if( xServiceInfo->supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
     101             :     {
     102           0 :         maModuleId = "com.sun.star.sheet.SpreadsheetDocument";
     103             :     }
     104           0 :     else if( xServiceInfo->supportsService( "com.sun.star.text.TextDocument" ) )
     105             :     {
     106           0 :         maModuleId = "com.sun.star.text.TextDocument";
     107             :     }
     108             : 
     109           0 :     if( maModuleId.isEmpty() )
     110             :     {
     111           0 :         throw uno::RuntimeException( "Not implemented" );
     112             :     }
     113             : 
     114             :     css::uno::Reference< css::ui::XModuleUIConfigurationManagerSupplier > xUICfgMgrSupp(
     115           0 :         css::ui::theModuleUIConfigurationManagerSupplier::get(mxContext) );
     116             : 
     117           0 :     m_xAppCfgMgr.set( xUICfgMgrSupp->getUIConfigurationManager( maModuleId ), uno::UNO_QUERY_THROW );
     118             : 
     119           0 :     css::uno::Reference< css::container::XNameAccess > xNameAccess = css::ui::theWindowStateConfiguration::get( mxContext );
     120             : 
     121           0 :     m_xWindowState.set( xNameAccess->getByName( maModuleId ), uno::UNO_QUERY_THROW );
     122           0 : }
     123             : 
     124           0 : css::uno::Reference< css::container::XIndexAccess > VbaCommandBarHelper::getSettings( const OUString& sResourceUrl ) throw (css::uno::RuntimeException)
     125             : {
     126           0 :     if( m_xDocCfgMgr->hasSettings( sResourceUrl ) )
     127           0 :         return m_xDocCfgMgr->getSettings( sResourceUrl, sal_True );
     128           0 :     else if( m_xAppCfgMgr->hasSettings( sResourceUrl ) )
     129           0 :         return m_xAppCfgMgr->getSettings( sResourceUrl, sal_True );
     130             :     else
     131             :     {
     132           0 :         css::uno::Reference< css::container::XIndexAccess > xSettings( m_xAppCfgMgr->createSettings( ), uno::UNO_QUERY_THROW );
     133           0 :         return xSettings;
     134             :     }
     135             : }
     136             : 
     137           0 : void VbaCommandBarHelper::removeSettings( const OUString& sResourceUrl ) throw (css::uno::RuntimeException)
     138             : {
     139           0 :     if( m_xDocCfgMgr->hasSettings( sResourceUrl ) )
     140           0 :         m_xDocCfgMgr->removeSettings( sResourceUrl );
     141           0 :     else if( m_xAppCfgMgr->hasSettings( sResourceUrl ) )
     142           0 :         m_xAppCfgMgr->removeSettings( sResourceUrl );
     143             : 
     144             :     // persistChanges();
     145           0 : }
     146             : 
     147           0 : void VbaCommandBarHelper::ApplyChange( const OUString& sResourceUrl, const css::uno::Reference< css::container::XIndexAccess >& xSettings, bool bTemporary ) throw (css::uno::RuntimeException)
     148             : {
     149           0 :     if( m_xDocCfgMgr->hasSettings( sResourceUrl ) )
     150             :     {
     151           0 :         m_xDocCfgMgr->replaceSettings( sResourceUrl, xSettings );
     152             :     }
     153             :     else
     154             :     {
     155           0 :         m_xDocCfgMgr->insertSettings( sResourceUrl, xSettings );
     156             :     }
     157           0 :     if( !bTemporary )
     158             :     {
     159           0 :         persistChanges();
     160             :     }
     161           0 : }
     162             : 
     163           0 : bool VbaCommandBarHelper::persistChanges() throw (css::uno::RuntimeException)
     164             : {
     165           0 :     uno::Reference< css::ui::XUIConfigurationPersistence > xConfigPersistence( m_xDocCfgMgr, uno::UNO_QUERY_THROW );
     166           0 :     bool result = false;
     167           0 :     if( xConfigPersistence->isModified() )
     168             :     {
     169           0 :         xConfigPersistence->store();
     170           0 :         result = true;
     171             :     }
     172           0 :     return result;
     173             : }
     174             : 
     175           0 : uno::Reference< frame::XLayoutManager > VbaCommandBarHelper::getLayoutManager() throw (uno::RuntimeException)
     176             : {
     177           0 :     uno::Reference< frame::XFrame > xFrame( getModel()->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
     178           0 :     uno::Reference< beans::XPropertySet > xPropertySet( xFrame, uno::UNO_QUERY_THROW );
     179           0 :     uno::Reference< frame::XLayoutManager > xLayoutManager( xPropertySet->getPropertyValue( "LayoutManager" ), uno::UNO_QUERY_THROW );
     180           0 :     return xLayoutManager;
     181             : }
     182             : 
     183           0 : bool VbaCommandBarHelper::hasToolbar( const OUString& sResourceUrl, const OUString& sName ) throw (css::uno::RuntimeException)
     184             : {
     185           0 :     if( m_xDocCfgMgr->hasSettings( sResourceUrl ) )
     186             :     {
     187           0 :         OUString sUIName;
     188           0 :         uno::Reference< beans::XPropertySet > xPropertySet( m_xDocCfgMgr->getSettings( sResourceUrl, sal_False ), uno::UNO_QUERY_THROW );
     189           0 :         xPropertySet->getPropertyValue( ITEM_DESCRIPTOR_UINAME ) >>= sUIName;
     190           0 :         if( sName.equalsIgnoreAsciiCase( sUIName ) )
     191           0 :             return true;
     192             :     }
     193           0 :     return false;
     194             : }
     195             : 
     196             : // return the resource url if found
     197           0 : OUString VbaCommandBarHelper::findToolbarByName( const css::uno::Reference< css::container::XNameAccess >& xNameAccess, const OUString& sName ) throw (css::uno::RuntimeException)
     198             : {
     199           0 :     OUString sResourceUrl;
     200             : 
     201             :     // check if it is an buildin toolbar
     202           0 :     sResourceUrl = MSO2OOCommandbarHelper::getMSO2OOCommandbarHelper()->findBuildinToolbar( sName );
     203           0 :     if( !sResourceUrl.isEmpty() )
     204           0 :         return sResourceUrl;
     205             : 
     206           0 :     uno::Sequence< OUString > allNames = xNameAccess->getElementNames();
     207           0 :     for( sal_Int32 i = 0; i < allNames.getLength(); i++ )
     208             :     {
     209           0 :         sResourceUrl = allNames[i];
     210           0 :         if(sResourceUrl.startsWith( ITEM_TOOLBAR_URL ) )
     211             :         {
     212           0 :             if( hasToolbar( sResourceUrl, sName ) )
     213           0 :                 return sResourceUrl;
     214             :         }
     215             :     }
     216             : 
     217             :     // the customize toolbars creating during importing, should found there.
     218           0 :     static OUString sToolbarPrefix(  "private:resource/toolbar/custom_"  );
     219           0 :     sResourceUrl = sToolbarPrefix.concat( sName );
     220           0 :     if( hasToolbar( sResourceUrl, sName ) )
     221           0 :         return sResourceUrl;
     222             : 
     223           0 :     return OUString();
     224             : }
     225             : 
     226             : // if found, return the position of the control. if not found, return -1
     227           0 : sal_Int32 VbaCommandBarHelper::findControlByName( const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess, const OUString& sName, bool bMenu ) throw (css::uno::RuntimeException)
     228             : {
     229           0 :     sal_Int32 nCount = xIndexAccess->getCount();
     230           0 :     css::uno::Sequence< css::beans::PropertyValue > aProps;
     231           0 :     for( sal_Int32 i = 0; i < nCount; i++ )
     232             :     {
     233           0 :         OUString sLabel;
     234           0 :         xIndexAccess->getByIndex( i ) >>= aProps;
     235           0 :         getPropertyValue( aProps, ITEM_DESCRIPTOR_LABEL ) >>= sLabel;
     236             :         // handle the hotkey marker '~' (remove in toolbars (?), replace by '&' in menus)
     237           0 :         OUStringBuffer aBuffer;
     238           0 :         sal_Int32 index = sLabel.indexOf( '~' );
     239           0 :         if( index < 0 )
     240             :         {
     241           0 :             aBuffer = sLabel;
     242             :         }
     243             :         else
     244             :         {
     245           0 :             aBuffer.append( sLabel.copy( 0, index ) );
     246           0 :             if( bMenu )
     247           0 :                 aBuffer.append( '&' );
     248           0 :             aBuffer.append( sLabel.copy( index + 1 ) );
     249             :         }
     250           0 :         OUString sNewLabel = aBuffer.makeStringAndClear();
     251             :         SAL_INFO("vbahelper", "VbaCommandBarHelper::findControlByName, control name: " << sNewLabel);
     252           0 :         if( sName.equalsIgnoreAsciiCase( sNewLabel ) )
     253           0 :             return i;
     254           0 :     }
     255             : 
     256             :     // not found
     257           0 :     return -1;
     258             : }
     259             : 
     260           0 : OUString VbaCommandBarHelper::generateCustomURL()
     261             : {
     262           0 :     OUString url( ITEM_TOOLBAR_URL );
     263           0 :     url += CUSTOM_TOOLBAR_STR;
     264             : 
     265             :     // use a random number to minimize possible clash with existing custom toolbars
     266           0 :     url += OUString::number(comphelper::rng::uniform_int_distribution(0, std::numeric_limits<int>::max()), 16);
     267           0 :     return url;
     268         480 : }
     269             : 
     270             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10