LCOV - code coverage report
Current view: top level - sc/source/ui/vba - vbaworkbook.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 111 202 55.0 %
Date: 2015-06-13 12:38:46 Functions: 15 27 55.6 %
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 <vbahelper/helperdecl.hxx>
      21             : #include <tools/urlobj.hxx>
      22             : #include <comphelper/unwrapargs.hxx>
      23             : #include <comphelper/servicehelper.hxx>
      24             : 
      25             : #include <com/sun/star/util/XModifiable.hpp>
      26             : #include <com/sun/star/util/XProtectable.hpp>
      27             : #include <com/sun/star/sheet/XSpreadsheetView.hpp>
      28             : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
      29             : #include <com/sun/star/frame/XStorable.hpp>
      30             : #include <com/sun/star/frame/XFrame.hpp>
      31             : #include <com/sun/star/beans/XPropertySet.hpp>
      32             : #include <ooo/vba/excel/XlFileFormat.hpp>
      33             : #include <ooo/vba/excel/XApplication.hpp>
      34             : 
      35             : #include "scextopt.hxx"
      36             : #include "vbaworksheet.hxx"
      37             : #include "vbaworksheets.hxx"
      38             : #include "vbaworkbook.hxx"
      39             : #include "vbawindows.hxx"
      40             : #include "vbastyles.hxx"
      41             : #include "excelvbahelper.hxx"
      42             : #include "vbapalette.hxx"
      43             : #include <osl/file.hxx>
      44             : #include "vbanames.hxx"
      45             : #include "nameuno.hxx"
      46             : #include "docoptio.hxx"
      47             : #include "unonames.hxx"
      48             : 
      49             : // Much of the impl. for the equivalend UNO module is
      50             : // sc/source/ui/unoobj/docuno.cxx, viewuno.cxx
      51             : 
      52             : using namespace ::ooo::vba;
      53             : using namespace ::com::sun::star;
      54             : 
      55           3 : uno::Sequence< sal_Int32 > ScVbaWorkbook::ColorData;
      56             : 
      57           2 : void ScVbaWorkbook::initColorData( const uno::Sequence< sal_Int32 >& sColors )
      58             : {
      59           2 :         const sal_Int32* pSource = sColors.getConstArray();
      60           2 :         sal_Int32* pDest = ColorData.getArray();
      61           2 :         const sal_Int32* pEnd = pSource + sColors.getLength();
      62         114 :         for ( ; pSource != pEnd; ++pSource, ++pDest )
      63         112 :             *pDest = *pSource;
      64           2 : }
      65             : 
      66             : void SAL_CALL
      67           2 : ScVbaWorkbook::ResetColors(  ) throw (::script::BasicErrorException, ::uno::RuntimeException, std::exception)
      68             : {
      69           2 :         uno::Reference< container::XIndexAccess > xIndexAccess( ScVbaPalette::getDefaultPalette(), uno::UNO_QUERY_THROW );
      70           2 :         sal_Int32 nLen = xIndexAccess->getCount();
      71           2 :         ColorData.realloc( nLen );
      72             : 
      73           4 :         uno::Sequence< sal_Int32 > dDefaultColors( nLen );
      74           2 :         sal_Int32* pDest = dDefaultColors.getArray();
      75         114 :         for ( sal_Int32 index=0; index < nLen; ++pDest, ++index )
      76         112 :             xIndexAccess->getByIndex( index )  >>= (*pDest);
      77           4 :         initColorData( dDefaultColors );
      78           2 : }
      79             : 
      80             : ::uno::Any SAL_CALL
      81           3 : ScVbaWorkbook::Colors( const ::uno::Any& Index ) throw (::script::BasicErrorException, ::uno::RuntimeException, std::exception)
      82             : {
      83           3 :     uno::Any aRet;
      84           3 :     if ( Index.getValue() )
      85             :     {
      86           3 :         sal_Int32 nIndex = 0;
      87           3 :         Index >>= nIndex;
      88           3 :         aRet = uno::makeAny( XLRGBToOORGB( ColorData[ --nIndex ] ) );
      89             :     }
      90             :     else
      91           0 :         aRet = uno::makeAny( ColorData );
      92           3 :     return aRet;
      93             : }
      94             : 
      95           1 : bool ScVbaWorkbook::setFilterPropsFromFormat( sal_Int32 nFormat, uno::Sequence< beans::PropertyValue >& rProps )
      96             : {
      97           1 :     bool bRes = false;
      98           1 :     for ( sal_Int32 index = 0; index < rProps.getLength(); ++index )
      99             :     {
     100           1 :         if ( rProps[ index ].Name == "FilterName" )
     101             :         {
     102           1 :             switch( nFormat )
     103             :             {
     104             :                 case excel::XlFileFormat::xlCSV:
     105           0 :                     rProps[ index ].Value = uno::Any( OUString("Text - txt - csv (StarCalc)") );
     106           0 :                     break;
     107             :                 case excel::XlFileFormat::xlDBF4:
     108           0 :                     rProps[ index ].Value = uno::Any( OUString("DBF") );
     109           0 :                     break;
     110             :                 case excel::XlFileFormat::xlDIF:
     111           0 :                     rProps[ index ].Value = uno::Any( OUString("DIF") );
     112           0 :                     break;
     113             :                 case excel::XlFileFormat::xlWK3:
     114           0 :                     rProps[ index ].Value = uno::Any( OUString("Lotus") );
     115           0 :                     break;
     116             :                 case excel::XlFileFormat::xlExcel4Workbook:
     117           0 :                     rProps[ index ].Value = uno::Any( OUString("MS Excel 4.0") );
     118           0 :                     break;
     119             :                 case excel::XlFileFormat::xlExcel5:
     120           0 :                     rProps[ index ].Value = uno::Any( OUString("MS Excel 5.0/95") );
     121           0 :                     break;
     122             :                 case excel::XlFileFormat::xlHtml:
     123           0 :                     rProps[ index ].Value = uno::Any( OUString("HTML (StarCalc)") );
     124           0 :                     break;
     125             :                 case excel::XlFileFormat::xlExcel9795:
     126             :                 default:
     127           1 :                     rProps[ index ].Value = uno::Any( OUString("MS Excel 97") );
     128           1 :                     break;
     129             :             }
     130           1 :             bRes = true;
     131           1 :             break;
     132             :         }
     133             :     }
     134           1 :     return bRes;
     135             : }
     136             : 
     137             : ::sal_Int32 SAL_CALL
     138           1 : ScVbaWorkbook::getFileFormat(  ) throw (::uno::RuntimeException, std::exception)
     139             : {
     140           1 :         sal_Int32 aFileFormat = 0;
     141           1 :         OUString aFilterName;
     142           2 :         uno::Sequence< beans::PropertyValue > aArgs = getModel()->getArgs();
     143             : 
     144             :         // #FIXME - seems suspect should we not walk through the properties
     145             :         // to find the FilterName
     146           1 :         if ( aArgs[0].Name == "FilterName" ) {
     147           0 :             aArgs[0].Value >>= aFilterName;
     148             :         } else {
     149           1 :            aArgs[1].Value >>= aFilterName;
     150             :         }
     151             : 
     152           1 :         if (aFilterName == "Text - txt - csv (StarCalc)") {
     153           0 :             aFileFormat = excel::XlFileFormat::xlCSV; //xlFileFormat.
     154             :         }
     155             : 
     156           1 :         if ( aFilterName == "DBF" ) {
     157           0 :             aFileFormat = excel::XlFileFormat::xlDBF4;
     158             :         }
     159             : 
     160           1 :         if ( aFilterName == "DIF" ) {
     161           0 :             aFileFormat = excel::XlFileFormat::xlDIF;
     162             :         }
     163             : 
     164           1 :         if ( aFilterName == "Lotus" ) {
     165           0 :             aFileFormat = excel::XlFileFormat::xlWK3;
     166             :         }
     167             : 
     168           1 :         if ( aFilterName == "MS Excel 4.0" ) {
     169           0 :             aFileFormat = excel::XlFileFormat::xlExcel4Workbook;
     170             :         }
     171             : 
     172           1 :         if ( aFilterName == "MS Excel 5.0/95" ) {
     173           0 :             aFileFormat = excel::XlFileFormat::xlExcel5;
     174             :         }
     175             : 
     176           1 :         if ( aFilterName == "MS Excel 97" ) {
     177           1 :             aFileFormat = excel::XlFileFormat::xlExcel9795;
     178             :         }
     179             : 
     180           1 :         if (aFilterName == "HTML (StarCalc)") {
     181           0 :             aFileFormat = excel::XlFileFormat::xlHtml;
     182             :         }
     183             : 
     184           1 :         if ( aFilterName == "calc_StarOffice_XML_Calc_Template" ) {
     185           0 :             aFileFormat = excel::XlFileFormat::xlTemplate;
     186             :         }
     187             : 
     188           1 :         if (aFilterName == "StarOffice XML (Calc)") {
     189           0 :             aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
     190             :         }
     191           1 :         if ( aFilterName == "calc8" ) {
     192           0 :             aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
     193             :         }
     194             : 
     195           2 :         return aFileFormat;
     196             : }
     197             : 
     198             : void
     199          49 : ScVbaWorkbook::init()
     200             : {
     201          49 :     if ( !ColorData.getLength() )
     202           1 :         ResetColors();
     203          49 : }
     204             : 
     205          22 : ScVbaWorkbook::ScVbaWorkbook(   const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, css::uno::Reference< css::frame::XModel > xModel ) : ScVbaWorkbook_BASE( xParent, xContext, xModel )
     206             : {
     207          22 :     init();
     208          22 : }
     209             : 
     210          27 : ScVbaWorkbook::ScVbaWorkbook( uno::Sequence< uno::Any> const & args,
     211          27 :     uno::Reference< uno::XComponentContext> const & xContext ) : ScVbaWorkbook_BASE( args, xContext )
     212             : {
     213          27 :     init();
     214          27 : }
     215             : 
     216             : uno::Reference< excel::XWorksheet >
     217         233 : ScVbaWorkbook::getActiveSheet() throw (uno::RuntimeException, std::exception)
     218             : {
     219         233 :     uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), uno::UNO_SET_THROW );
     220         466 :         uno::Reference< sheet::XSpreadsheetView > xView( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     221         466 :     uno::Reference< sheet::XSpreadsheet > xSheet( xView->getActiveSheet(), uno::UNO_SET_THROW );
     222             :     // #162503# return the original sheet module wrapper object, instead of a new instance
     223         466 :     uno::Reference< excel::XWorksheet > xWorksheet( excel::getUnoSheetModuleObj( xSheet ), uno::UNO_QUERY );
     224         233 :     if( xWorksheet.is() ) return xWorksheet;
     225             :     // #i116936# excel::getUnoSheetModuleObj() may return null in documents without global VBA mode enabled
     226         233 :     return new ScVbaWorksheet( this, mxContext, xSheet, xModel );
     227             : }
     228             : 
     229             : uno::Any SAL_CALL
     230           0 : ScVbaWorkbook::Sheets( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
     231             : {
     232           0 :     return Worksheets( aIndex );
     233             : }
     234             : 
     235             : uno::Any SAL_CALL
     236         397 : ScVbaWorkbook::Worksheets( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
     237             : {
     238         397 :     uno::Reference< frame::XModel > xModel( getModel() );
     239         794 :     uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( xModel, uno::UNO_QUERY_THROW );
     240         794 :     uno::Reference<container::XIndexAccess > xSheets( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
     241         794 :     uno::Reference< XCollection > xWorkSheets(  new ScVbaWorksheets( this, mxContext, xSheets, xModel ) );
     242         397 :     if (  aIndex.getValueTypeClass() == uno::TypeClass_VOID )
     243             :     {
     244           0 :         return uno::Any( xWorkSheets );
     245             :     }
     246             :     // pass on to collection
     247         794 :     return uno::Any( xWorkSheets->Item( aIndex, uno::Any() ) );
     248             : }
     249             : uno::Any SAL_CALL
     250           0 : ScVbaWorkbook::Windows( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
     251             : {
     252             : 
     253           0 :     uno::Reference< excel::XWindows >  xWindows( new ScVbaWindows( getParent(), mxContext ) );
     254           0 :     if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
     255           0 :         return uno::Any( xWindows );
     256           0 :     return uno::Any( xWindows->Item( aIndex, uno::Any() ) );
     257             : }
     258             : 
     259             : void SAL_CALL
     260           1 : ScVbaWorkbook::Activate() throw (uno::RuntimeException, std::exception)
     261             : {
     262           1 :     VbaDocumentBase::Activate();
     263           1 : }
     264             : 
     265             : void
     266           0 : ScVbaWorkbook::Protect( const uno::Any &aPassword ) throw (uno::RuntimeException)
     267             : {
     268           0 :     VbaDocumentBase::Protect( aPassword );
     269           0 : }
     270             : 
     271             : sal_Bool
     272           0 : ScVbaWorkbook::getProtectStructure() throw (uno::RuntimeException, std::exception)
     273             : {
     274           0 :     uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
     275           0 :     return xProt->isProtected();
     276             : }
     277             : 
     278           0 : sal_Bool SAL_CALL ScVbaWorkbook::getPrecisionAsDisplayed() throw (uno::RuntimeException, std::exception)
     279             : {
     280           0 :     uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
     281           0 :     ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
     282           0 :     return rDoc.GetDocOptions().IsCalcAsShown();
     283             : }
     284             : 
     285           0 : void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed ) throw (uno::RuntimeException, std::exception)
     286             : {
     287           0 :     uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
     288           0 :     ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
     289           0 :     ScDocOptions aOpt = rDoc.GetDocOptions();
     290           0 :     aOpt.SetCalcAsShown( _precisionAsDisplayed );
     291           0 :     rDoc.SetDocOptions( aOpt );
     292           0 : }
     293             : 
     294             : void
     295           0 : ScVbaWorkbook::SaveCopyAs( const OUString& sFileName ) throw ( uno::RuntimeException, std::exception)
     296             : {
     297           0 :     OUString aURL;
     298           0 :     osl::FileBase::getFileURLFromSystemPath( sFileName, aURL );
     299           0 :     uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
     300           0 :     uno::Sequence<  beans::PropertyValue > storeProps(1);
     301           0 :     storeProps[0].Name = "FilterName";
     302           0 :     storeProps[0].Value <<= OUString( "MS Excel 97" );
     303           0 :     xStor->storeToURL( aURL, storeProps );
     304           0 : }
     305             : 
     306             : void SAL_CALL
     307           1 : ScVbaWorkbook::SaveAs( const uno::Any& FileName, const uno::Any& FileFormat, const uno::Any& /*Password*/, const uno::Any& /*WriteResPassword*/, const uno::Any& /*ReadOnlyRecommended*/, const uno::Any& /*CreateBackup*/, const uno::Any& /*AccessMode*/, const uno::Any& /*ConflictResolution*/, const uno::Any& /*AddToMru*/, const uno::Any& /*TextCodepage*/, const uno::Any& /*TextVisualLayout*/, const uno::Any& /*Local*/ ) throw (::com::sun::star::uno::RuntimeException, std::exception)
     308             : {
     309           1 :     OUString sFileName;
     310           1 :     FileName >>= sFileName;
     311           2 :     OUString sURL;
     312           1 :     osl::FileBase::getFileURLFromSystemPath( sFileName, sURL );
     313             :     // detect if there is no path if there is no path then we need
     314             :     // to use the current current folder
     315           2 :     INetURLObject aURL( sURL );
     316           1 :     sURL = aURL.GetMainURL( INetURLObject::DECODE_TO_IURI );
     317           1 :     if( sURL.isEmpty() )
     318             :     {
     319             :         // need to add cur dir ( of this workbook ) or else the 'Work' dir
     320           0 :         sURL = getModel()->getURL();
     321             : 
     322           0 :         if ( sURL.isEmpty() )
     323             :         {
     324             :             // not path available from 'this' document
     325             :             // need to add the 'document'/work directory then
     326           0 :             uno::Reference< excel::XApplication > xApplication ( Application(),uno::UNO_QUERY_THROW );
     327           0 :             OUString sWorkPath = xApplication->getDefaultFilePath();
     328           0 :             OUString sWorkURL;
     329           0 :             osl::FileBase::getFileURLFromSystemPath( sWorkPath, sWorkURL );
     330           0 :             aURL.SetURL( sWorkURL );
     331             :         }
     332             :         else
     333             :         {
     334           0 :             aURL.SetURL( sURL );
     335           0 :             aURL.Append( sFileName );
     336             :         }
     337           0 :         sURL = aURL.GetMainURL( INetURLObject::DECODE_TO_IURI );
     338             : 
     339             :     }
     340             : 
     341           1 :     sal_Int32 nFileFormat = excel::XlFileFormat::xlExcel9795;
     342           1 :     FileFormat >>= nFileFormat;
     343             : 
     344           2 :     uno::Sequence<  beans::PropertyValue > storeProps(1);
     345           1 :     storeProps[0].Name = "FilterName" ;
     346             : 
     347           1 :     setFilterPropsFromFormat( nFileFormat, storeProps );
     348             : 
     349           2 :     uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
     350           2 :     OUString sFilterName;
     351           1 :     storeProps[0].Value >>= sFilterName;
     352           2 :     xStor->storeAsURL( sURL, storeProps );
     353           1 : }
     354             : 
     355             : css::uno::Any SAL_CALL
     356           0 : ScVbaWorkbook::Styles( const uno::Any& Item ) throw (::script::BasicErrorException, uno::RuntimeException, std::exception)
     357             : {
     358             :     // quick look and Styles object doesn't seem to have a valid parent
     359             :     // or a least the object browser just shows an object that has no
     360             :     // variables ( therefore... leave as NULL for now )
     361           0 :     uno::Reference< XCollection > dStyles = new ScVbaStyles( uno::Reference< XHelperInterface >(), mxContext, getModel() );
     362           0 :     if ( Item.hasValue() )
     363           0 :         return dStyles->Item( Item, uno::Any() );
     364           0 :     return uno::makeAny( dStyles );
     365             : }
     366             : 
     367             : uno::Any SAL_CALL
     368          22 : ScVbaWorkbook::Names( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
     369             : {
     370          22 :     uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
     371          44 :     uno::Reference< beans::XPropertySet > xProps( xModel, uno::UNO_QUERY_THROW );
     372          44 :     uno::Reference< sheet::XNamedRanges > xNamedRanges(  xProps->getPropertyValue("NamedRanges"), uno::UNO_QUERY_THROW );
     373          44 :     uno::Reference< XCollection > xNames( new ScVbaNames( this, mxContext, xNamedRanges, xModel ) );
     374          22 :     if ( aIndex.hasValue() )
     375           7 :         return uno::Any( xNames->Item( aIndex, uno::Any() ) );
     376          37 :     return uno::Any( xNames );
     377             : }
     378             : 
     379             : OUString
     380           0 : ScVbaWorkbook::getServiceImplName()
     381             : {
     382           0 :     return OUString("ScVbaWorkbook");
     383             : }
     384             : 
     385             : uno::Sequence< OUString >
     386           0 : ScVbaWorkbook::getServiceNames()
     387             : {
     388           0 :     static uno::Sequence< OUString > aServiceNames;
     389           0 :     if ( aServiceNames.getLength() == 0 )
     390             :     {
     391           0 :         aServiceNames.realloc( 1 );
     392           0 :         aServiceNames[ 0 ] = "ooo.vba.excel.Workbook";
     393             :     }
     394           0 :     return aServiceNames;
     395             : }
     396             : 
     397             : OUString SAL_CALL
     398           0 : ScVbaWorkbook::getCodeName() throw (css::uno::RuntimeException, std::exception)
     399             : {
     400           0 :     uno::Reference< beans::XPropertySet > xModelProp( getModel(), uno::UNO_QUERY_THROW );
     401           0 :     return xModelProp->getPropertyValue("CodeName").get< OUString >();
     402             : }
     403             : 
     404             : sal_Int64
     405           0 : ScVbaWorkbook::getSomething(const uno::Sequence<sal_Int8 >& rId ) throw(css::uno::RuntimeException, std::exception)
     406             : {
     407           0 :     if (rId.getLength() == 16 &&
     408           0 :         0 == memcmp( ScVbaWorksheet::getUnoTunnelId().getConstArray(), rId.getConstArray(), 16 ))
     409             :     {
     410           0 :         return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
     411             :     }
     412           0 :     return 0;
     413             : }
     414             : 
     415             : namespace workbook
     416             : {
     417             : namespace sdecl = comphelper::service_decl;
     418           3 : sdecl::vba_service_class_<ScVbaWorkbook, sdecl::with_args<true> > serviceImpl;
     419           3 : extern sdecl::ServiceDecl const serviceDecl(
     420             :     serviceImpl,
     421             :     "ScVbaWorkbook",
     422             :     "ooo.vba.excel.Workbook" );
     423           9 : }
     424             : 
     425             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11