LCOV - code coverage report
Current view: top level - libreoffice/sc/source/filter/excel - xltoolbar.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 18 229 7.9 %
Date: 2012-12-27 Functions: 6 27 22.2 %
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             :  * Version: MPL 1.1 / GPLv3+ / LGPLv3+
       4             :  *
       5             :  * The contents of this file are subject to the Mozilla Public License Version
       6             :  * 1.1 (the "License"); you may not use this file except in compliance with
       7             :  * the License or as specified alternatively below. You may obtain a copy of
       8             :  * the License at http://www.mozilla.org/MPL/
       9             :  *
      10             :  * Software distributed under the License is distributed on an "AS IS" basis,
      11             :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12             :  * for the specific language governing rights and limitations under the
      13             :  * License.
      14             :  *
      15             :  * The Initial Developer of the Original Code is
      16             :  *       Noel Power <noel.power@novell.com>
      17             :  * Portions created by the Initial Developer are Copyright (C) 2010 the
      18             :  * Initial Developer. All Rights Reserved.
      19             :  *
      20             :  * Contributor(s):
      21             :  *       Noel Power <noel.power@novell.com>
      22             :  *
      23             :  * For minor contributions see the git repository.
      24             :  *
      25             :  * Alternatively, the contents of this file may be used under the terms of
      26             :  * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
      27             :  * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
      28             :  * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
      29             :  * instead of those above.
      30             :  */
      31             : #include "xltoolbar.hxx"
      32             : #include <rtl/ustrbuf.hxx>
      33             : #include <stdarg.h>
      34             : #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
      35             : #include <com/sun/star/ui/ModuleUIConfigurationManagerSupplier.hpp>
      36             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      37             : #include <com/sun/star/lang/XSingleComponentFactory.hpp>
      38             : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
      39             : #include <com/sun/star/ui/XImageManager.hpp>
      40             : #include <com/sun/star/ui/ItemType.hpp>
      41             : #include <fstream>
      42             : #include <comphelper/processfactory.hxx>
      43             : #include <vcl/graph.hxx>
      44             : #include <map>
      45             : using namespace com::sun::star;
      46             : 
      47             : typedef std::map< sal_Int16, rtl::OUString > IdToString;
      48             : 
      49           0 : class MSOExcelCommandConvertor : public MSOCommandConvertor
      50             : {
      51             :    IdToString msoToOOcmd;
      52             :    IdToString tcidToOOcmd;
      53             : public:
      54             :     MSOExcelCommandConvertor();
      55             :     virtual rtl::OUString MSOCommandToOOCommand( sal_Int16 msoCmd );
      56             :     virtual rtl::OUString MSOTCIDToOOCommand( sal_Int16 key );
      57             : };
      58             : 
      59           0 : MSOExcelCommandConvertor::MSOExcelCommandConvertor()
      60             : {
      61             : /*
      62             :     // mso command id to ooo command string
      63             :     // #FIXME and *HUNDREDS* of id's to added here
      64             :     msoToOOcmd[ 0x20b ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:CloseDoc") );
      65             :     msoToOOcmd[ 0x50 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:Open") );
      66             : 
      67             :    // mso tcid to ooo command string
      68             :     // #FIXME and *HUNDREDS* of id's to added here
      69             :    tcidToOOcmd[ 0x9d9 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:Print") );
      70             : */
      71           0 : }
      72             : 
      73           0 : rtl::OUString MSOExcelCommandConvertor::MSOCommandToOOCommand( sal_Int16 key )
      74             : {
      75           0 :     rtl::OUString sResult;
      76           0 :     IdToString::iterator it = msoToOOcmd.find( key );
      77           0 :     if ( it != msoToOOcmd.end() )
      78           0 :         sResult = it->second;
      79           0 :     return sResult;
      80             : }
      81             : 
      82           0 : rtl::OUString MSOExcelCommandConvertor::MSOTCIDToOOCommand( sal_Int16 key )
      83             : {
      84           0 :     rtl::OUString sResult;
      85           0 :     IdToString::iterator it = tcidToOOcmd.find( key );
      86           0 :     if ( it != tcidToOOcmd.end() )
      87           0 :         sResult = it->second;
      88           0 :     return sResult;
      89             : }
      90             : 
      91             : 
      92             : 
      93           3 : CTBS::CTBS() : bSignature(0), bVersion(0), reserved1(0), reserved2(0), reserved3(0), ctb(0), ctbViews(0), ictbView(0)
      94             : {
      95           3 : }
      96             : 
      97           0 : ScCTB::ScCTB(sal_uInt16 nNum ) : nViews( nNum ), ectbid(0)
      98             : {
      99           0 : }
     100             : 
     101           0 : bool ScCTB::Read( SvStream &rS )
     102             : {
     103             :     OSL_TRACE("ScCTB::Read() stream pos 0x%x", rS.Tell() );
     104           0 :     nOffSet = rS.Tell();
     105           0 :     tb.Read( rS );
     106           0 :     for ( sal_uInt16 index = 0; index < nViews; ++index )
     107             :     {
     108           0 :         TBVisualData aVisData;
     109           0 :         aVisData.Read( rS );
     110           0 :         rVisualData.push_back( aVisData );
     111           0 :     }
     112           0 :     rS >> ectbid;
     113             : 
     114           0 :     for ( sal_Int16 index = 0; index < tb.getcCL(); ++index )
     115             :     {
     116           0 :         ScTBC aTBC;
     117           0 :         aTBC.Read( rS );
     118           0 :         rTBC.push_back( aTBC );
     119           0 :     }
     120           0 :     return true;
     121             : }
     122             : 
     123           0 : void ScCTB::Print( FILE* fp )
     124             : {
     125           0 :     Indent a;
     126           0 :     indent_printf( fp, "[ 0x%x ] ScCTB -- dump\n", nOffSet );
     127           0 :     indent_printf( fp, "  nViews 0x%x\n", nViews);
     128           0 :     tb.Print( fp );
     129             : 
     130           0 :     std::vector<TBVisualData>::iterator visData_end = rVisualData.end();
     131           0 :     sal_Int32 counter = 0;
     132           0 :     for ( std::vector<TBVisualData>::iterator it = rVisualData.begin(); it != visData_end; ++it )
     133             :     {
     134             : 
     135           0 :         indent_printf( fp, "  TBVisualData [%d]\n", counter++ );
     136           0 :         Indent b;
     137           0 :         it->Print( fp );
     138           0 :     }
     139           0 :     indent_printf( fp, "  ectbid 0x%x\n", ectbid);
     140           0 :     std::vector<ScTBC>::iterator it_end = rTBC.end();
     141           0 :     counter = 0;
     142           0 :     for ( std::vector<ScTBC>::iterator it = rTBC.begin(); it != it_end; ++it )
     143             :     {
     144           0 :         indent_printf( fp, "  ScTBC [%d]\n", counter++);
     145           0 :         Indent c;
     146           0 :         it->Print( fp );
     147           0 :     }
     148           0 : }
     149             : 
     150           0 : bool ScCTB::IsMenuToolbar()
     151             : {
     152           0 :     return tb.IsMenuToolbar();
     153             : }
     154             : 
     155           0 : bool ScCTB::ImportMenuTB( ScCTBWrapper& rWrapper, const css::uno::Reference< css::container::XIndexContainer >& xMenuDesc, CustomToolBarImportHelper& helper )
     156             : {
     157           0 :     sal_Int32 index = 0;
     158           0 :     for ( std::vector< ScTBC >::iterator it =  rTBC.begin(); it != rTBC.end(); ++it, ++index )
     159             :     {
     160           0 :         if ( !it->ImportToolBarControl( rWrapper, xMenuDesc, helper, IsMenuToolbar() ) )
     161           0 :             return false;
     162             :     }
     163           0 :     return true;
     164             : }
     165             : 
     166           0 : bool ScCTB::ImportCustomToolBar( ScCTBWrapper& rWrapper, CustomToolBarImportHelper& helper )
     167             : {
     168             : 
     169           0 :     static rtl::OUString sToolbarPrefix( RTL_CONSTASCII_USTRINGPARAM( "private:resource/toolbar/custom_" ) );
     170           0 :     bool bRes = false;
     171             :     try
     172             :     {
     173           0 :         if ( !tb.IsEnabled() )
     174           0 :             return true;  // didn't fail, just ignoring
     175             : 
     176             :         // Create default setting
     177           0 :         uno::Reference< container::XIndexContainer > xIndexContainer( helper.getCfgManager()->createSettings(), uno::UNO_QUERY_THROW );
     178           0 :         uno::Reference< container::XIndexAccess > xIndexAccess( xIndexContainer, uno::UNO_QUERY_THROW );
     179           0 :         uno::Reference< beans::XPropertySet > xProps( xIndexContainer, uno::UNO_QUERY_THROW );
     180           0 :         WString& name = tb.getName();
     181             :         // set UI name for toolbar
     182           0 :         xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("UIName") ), uno::makeAny( name.getString() ) );
     183             : 
     184           0 :         rtl::OUString sToolBarName = sToolbarPrefix.concat( name.getString() );
     185           0 :         for ( std::vector< ScTBC >::iterator it =  rTBC.begin(); it != rTBC.end(); ++it )
     186             :         {
     187           0 :             if ( !it->ImportToolBarControl( rWrapper, xIndexContainer, helper, IsMenuToolbar() ) )
     188           0 :                 return false;
     189             :         }
     190             : 
     191             :         OSL_TRACE("Name of toolbar :-/ %s", rtl::OUStringToOString( sToolBarName, RTL_TEXTENCODING_UTF8 ).getStr() );
     192             : 
     193           0 :         helper.getCfgManager()->insertSettings( sToolBarName, xIndexAccess );
     194           0 :         helper.applyIcons();
     195             : 
     196           0 :         uno::Reference< ui::XUIConfigurationPersistence > xPersistence( helper.getCfgManager()->getImageManager(), uno::UNO_QUERY_THROW );
     197           0 :         xPersistence->store();
     198             : 
     199           0 :         xPersistence.set( helper.getCfgManager(), uno::UNO_QUERY_THROW );
     200           0 :         xPersistence->store();
     201             : 
     202           0 :         bRes = true;
     203             :     }
     204           0 :     catch( uno::Exception& )
     205             :     {
     206           0 :        bRes = false;
     207             :     }
     208           0 :     return bRes;
     209             : }
     210           3 : bool CTBS::Read( SvStream &rS )
     211             : {
     212             :     OSL_TRACE("CTBS::Read() stream pos 0x%x", rS.Tell() );
     213           3 :     nOffSet = rS.Tell();
     214           3 :     rS >> bSignature >> bVersion >> reserved1 >> reserved2 >> reserved3 >> ctb >> ctbViews >> ictbView;
     215           3 :     return true;
     216             : }
     217             : 
     218           0 : void CTBS::Print( FILE* fp )
     219             : {
     220           0 :     Indent a;
     221           0 :     indent_printf( fp, "[ 0x%x ] CTBS -- dump\n", nOffSet );
     222             : 
     223           0 :     indent_printf( fp, "  bSignature 0x%x\n", bSignature);
     224           0 :     indent_printf( fp, "  bVersion 0x%x\n", bVersion);
     225             : 
     226           0 :     indent_printf( fp, "  reserved1 0x%x\n", reserved1 );
     227           0 :     indent_printf( fp, "  reserved2 0x%x\n", reserved2 );
     228           0 :     indent_printf( fp, "  reserved3 0x%x\n", reserved3 );
     229             : 
     230           0 :     indent_printf( fp, "  ctb 0x%x\n", ctb );
     231           0 :     indent_printf( fp, "  ctbViews 0x%x\n", ctbViews );
     232           0 :     indent_printf( fp, "  ictbView 0x%x\n", ictbView );
     233           0 : }
     234             : 
     235           0 : ScTBC::ScTBC()
     236             : {
     237           0 : }
     238             : 
     239             : bool
     240           0 : ScTBC::Read(SvStream &rS)
     241             : {
     242             :     OSL_TRACE("ScTBC::Read() stream pos 0x%x", rS.Tell() );
     243           0 :     nOffSet = rS.Tell();
     244           0 :     if ( !tbch.Read( rS ) )
     245           0 :         return false;
     246           0 :     sal_uInt16 tcid = tbch.getTcID();
     247           0 :     sal_uInt8 tct = tbch.getTct();
     248           0 :     if (  ( tcid != 0x0001 && tcid != 0x06CC && tcid != 0x03D8 && tcid != 0x03EC && tcid != 0x1051 ) && ( ( tct > 0 && tct < 0x0B ) || ( ( tct > 0x0B && tct < 0x10 ) || tct == 0x15 ) ) )
     249             :     {
     250           0 :         tbcCmd.reset( new TBCCmd );
     251           0 :         if ( !  tbcCmd->Read( rS ) )
     252           0 :             return false;
     253             :     }
     254           0 :     if ( tct != 0x16 )
     255             :     {
     256           0 :         tbcd.reset( new TBCData( tbch ) );
     257           0 :         if ( !tbcd->Read( rS ) )
     258           0 :             return false;
     259             :     }
     260           0 :     return true;
     261             : }
     262             : 
     263             : 
     264             : void
     265           0 : ScTBC::Print(FILE* fp)
     266             : {
     267           0 :     Indent a;
     268           0 :     indent_printf( fp, "[ 0x%x ] ScTBC -- dump\n", nOffSet );
     269           0 :     tbch.Print( fp );
     270           0 :     if ( tbcCmd.get() )
     271           0 :         tbcCmd->Print( fp );
     272           0 :     if ( tbcd.get() )
     273           0 :         tbcd->Print( fp );
     274           0 : }
     275             : 
     276           0 : bool ScTBC::ImportToolBarControl( ScCTBWrapper& rWrapper, const css::uno::Reference< css::container::XIndexContainer >& toolbarcontainer, CustomToolBarImportHelper& helper, bool bIsMenuToolbar )
     277             : {
     278             :     // how to identify built-in-command ?
     279             : //    bool bBuiltin = false;
     280           0 :     if ( tbcd.get() )
     281             :     {
     282           0 :         std::vector< css::beans::PropertyValue > props;
     283           0 :         bool bBeginGroup = false;
     284           0 :         if ( ! tbcd->ImportToolBarControl( helper, props, bBeginGroup, bIsMenuToolbar ) )
     285           0 :             return false;
     286           0 :         TBCMenuSpecific* pMenu = tbcd->getMenuSpecific();
     287           0 :         if ( pMenu )
     288             :         {
     289             :             // search for ScCTB with the appropriate name ( it contains the
     290             :             // menu items, although we cannot import ( or create ) a menu on
     291             :             // a custom toolbar we can import the menu items in a separate
     292             :             // toolbar ( better than nothing )
     293           0 :             ScCTB* pCustTB = rWrapper.GetCustomizationData( pMenu->Name() );
     294           0 :             if ( pCustTB )
     295             :             {
     296           0 :                  uno::Reference< container::XIndexContainer > xMenuDesc;
     297           0 :                  uno::Reference< lang::XMultiServiceFactory > xMSF( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
     298           0 :                  xMenuDesc.set( xMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.IndexedPropertyValues" ) ) ), uno::UNO_QUERY_THROW );
     299           0 :                  if ( !pCustTB->ImportMenuTB( rWrapper, xMenuDesc, helper ) )
     300           0 :                      return false;
     301           0 :                  if ( !bIsMenuToolbar )
     302             :                  {
     303           0 :                      if ( !helper.createMenu( pMenu->Name(), uno::Reference< container::XIndexAccess >( xMenuDesc, uno::UNO_QUERY ), true ) )
     304           0 :                          return false;
     305             :                  }
     306             :                  else
     307             :                  {
     308           0 :                      beans::PropertyValue aProp;
     309           0 :                      aProp.Name =  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ItemDescriptorContainer") );
     310           0 :                      aProp.Value <<= xMenuDesc;
     311           0 :                      props.push_back( aProp );
     312           0 :                  }
     313             :             }
     314             :         }
     315             : 
     316           0 :         if ( bBeginGroup )
     317             :         {
     318             :             // insert spacer
     319           0 :             uno::Sequence< beans::PropertyValue > sProps( 1 );
     320           0 :             sProps[ 0 ].Name =  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Type") );
     321           0 :             sProps[ 0 ].Value = uno::makeAny( ui::ItemType::SEPARATOR_LINE );
     322           0 :             toolbarcontainer->insertByIndex( toolbarcontainer->getCount(), uno::makeAny( sProps ) );
     323             :         }
     324           0 :         uno::Sequence< beans::PropertyValue > sProps( props.size() );
     325           0 :         beans::PropertyValue* pProp = sProps.getArray();
     326             : 
     327           0 :         for ( std::vector< css::beans::PropertyValue >::iterator it = props.begin(); it != props.end(); ++it, ++pProp )
     328           0 :             *pProp = *it;
     329             : 
     330           0 :         toolbarcontainer->insertByIndex( toolbarcontainer->getCount(), uno::makeAny( sProps ) );
     331             :     }
     332           0 :     return true;
     333             : }
     334             : 
     335             : void
     336           0 : TBCCmd::Print(FILE* fp)
     337             : {
     338           0 :     Indent a;
     339           0 :     indent_printf( fp, " TBCCmd -- dump\n" );
     340           0 :     indent_printf( fp, "   cmdID 0x%x\n", cmdID );
     341           0 :     indent_printf( fp, "   A ( fHideDrawing ) %s\n", A ? "true" : "false" );
     342           0 :     indent_printf( fp, "   B ( reserved - ignored ) %s\n", A ? "true" : "false" );
     343           0 :     indent_printf( fp, "   cmdType 0x%x\n", cmdType );
     344           0 :     indent_printf( fp, "   C ( reserved - ignored ) %s\n", A ? "true" : "false" );
     345           0 :     indent_printf( fp, "   reserved3 0x%x\n", reserved3 );
     346           0 : }
     347             : 
     348           0 : bool TBCCmd::Read( SvStream &rS )
     349             : {
     350             :     OSL_TRACE("TBCCmd::Read() stream pos 0x%x", rS.Tell() );
     351           0 :     nOffSet = rS.Tell();
     352           0 :     rS >> cmdID;
     353             :     sal_uInt16 temp;
     354           0 :     rS >> temp;
     355             :     OSL_TRACE("TBCmd temp = 0x%x", temp );
     356           0 :     A = (temp & 0x8000 ) == 0x8000;
     357           0 :     B = (temp & 0x4000) == 0x4000;
     358           0 :     cmdType = ( temp & 0x3E00 ) >> 9;
     359           0 :     C = ( temp & 0x100 ) == 0x100;
     360           0 :     reserved3 = ( temp & 0xFF );
     361           0 :     return true;
     362             : }
     363             : 
     364           3 : ScCTBWrapper::ScCTBWrapper()
     365             : {
     366           3 : }
     367             : 
     368           3 : ScCTBWrapper::~ScCTBWrapper()
     369             : {
     370           3 : }
     371             : 
     372             : bool
     373           3 : ScCTBWrapper::Read( SvStream &rS)
     374             : {
     375             :     OSL_TRACE("ScCTBWrapper::Read() stream pos 0x%x", rS.Tell() );
     376           3 :     nOffSet = rS.Tell();
     377           3 :     if ( !ctbSet.Read( rS ) )
     378           0 :         return false;
     379           3 :     for ( sal_uInt16 index = 0; index < ctbSet.ctb; ++index )
     380             :     {
     381           0 :         ScCTB aCTB( ctbSet.ctbViews );
     382           0 :         if ( !aCTB.Read( rS ) )
     383           0 :             return false;
     384           0 :         rCTB.push_back( aCTB );
     385           0 :     }
     386           3 :     return true;
     387             : }
     388             : 
     389             : void
     390           0 : ScCTBWrapper::Print( FILE* fp )
     391             : {
     392           0 :     Indent a;
     393           0 :     indent_printf( fp, "[ 0x%x ] ScCTBWrapper -- dump\n", nOffSet );
     394           0 :     ctbSet.Print( fp );
     395           0 :     std::vector<ScCTB>::iterator it_end = rCTB.end();
     396           0 :     for ( std::vector<ScCTB>::iterator it = rCTB.begin(); it != it_end; ++it )
     397             :     {
     398           0 :         Indent b;
     399           0 :         it->Print( fp );
     400           0 :     }
     401           0 : }
     402             : 
     403           0 : ScCTB* ScCTBWrapper::GetCustomizationData( const rtl::OUString& sTBName )
     404             : {
     405           0 :     ScCTB* pCTB = NULL;
     406           0 :     for ( std::vector< ScCTB >::iterator it = rCTB.begin(); it != rCTB.end(); ++it )
     407             :     {
     408           0 :         if ( it->GetName().equals( sTBName ) )
     409             :         {
     410           0 :             pCTB = &(*it);
     411           0 :             break;
     412             :         }
     413             :     }
     414           0 :     return pCTB;
     415             : }
     416             : 
     417           3 : bool ScCTBWrapper::ImportCustomToolBar( SfxObjectShell& rDocSh )
     418             : {
     419           3 :     if(rCTB.empty())
     420           3 :         return true;
     421             : 
     422           0 :     uno::Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
     423           0 :     uno::Reference< ui::XModuleUIConfigurationManagerSupplier > xAppCfgSupp( ui::ModuleUIConfigurationManagerSupplier::create(xContext) );
     424             : 
     425           0 :     std::vector<ScCTB>::iterator it_end = rCTB.end();
     426           0 :     for ( std::vector<ScCTB>::iterator it = rCTB.begin(); it != it_end; ++it )
     427             :     {
     428             :         // for each customtoolbar
     429           0 :         CustomToolBarImportHelper helper( rDocSh, xAppCfgSupp->getUIConfigurationManager( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sheet.SpreadsheetDocument" ) ) ) );
     430           0 :         helper.setMSOCommandMap( new  MSOExcelCommandConvertor() );
     431             :         // Ignore menu toolbars, excel doesn't ( afaics ) store
     432             :         // menu customizations ( but you can have menus in a customtoolbar
     433             :         // such menus will be dealt with when they are encountered
     434             :         // as part of importing the appropriate MenuSpecific toolbar control )
     435             : 
     436             : 
     437           0 :         if ( !(*it).IsMenuToolbar() )
     438             :         {
     439           0 :             if ( !(*it).ImportCustomToolBar( *this, helper ) )
     440           0 :                 return false;
     441             :         }
     442           0 :     }
     443           0 :     return true;
     444             : }
     445             : 
     446             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10