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

Generated by: LCOV version 1.10