LCOV - code coverage report
Current view: top level - sc/source/ui/vba - vbaworkbooks.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 25 118 21.2 %
Date: 2015-06-13 12:38:46 Functions: 6 19 31.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             : #include <comphelper/processfactory.hxx>
      20             : 
      21             : #include <cppuhelper/implbase1.hxx>
      22             : #include <cppuhelper/implbase3.hxx>
      23             : 
      24             : #include <com/sun/star/frame/XDesktop.hpp>
      25             : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
      26             : #include <com/sun/star/container/XEnumerationAccess.hpp>
      27             : #include <com/sun/star/frame/XComponentLoader.hpp>
      28             : #include <com/sun/star/lang/XComponent.hpp>
      29             : #include <com/sun/star/frame/XModel.hpp>
      30             : #include <com/sun/star/frame/XFrame.hpp>
      31             : #include <com/sun/star/frame/FrameSearchFlag.hpp>
      32             : #include <com/sun/star/util/XModifiable.hpp>
      33             : #include <com/sun/star/frame/XStorable.hpp>
      34             : #include <com/sun/star/lang/DisposedException.hpp>
      35             : #include <com/sun/star/beans/PropertyVetoException.hpp>
      36             : #include <com/sun/star/util/XCloseable.hpp>
      37             : #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
      38             : #include <com/sun/star/document/XTypeDetection.hpp>
      39             : #include <com/sun/star/uri/XUriReference.hpp>
      40             : #include <com/sun/star/uri/XUriReferenceFactory.hpp>
      41             : #include <com/sun/star/script/vba/VBAEventId.hpp>
      42             : #include <com/sun/star/script/vba/XVBACompatibility.hpp>
      43             : #include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
      44             : #include <com/sun/star/script/vba/XVBAModuleInfo.hpp>
      45             : #include <com/sun/star/script/ModuleInfo.hpp>
      46             : #include <com/sun/star/script/ModuleType.hpp>
      47             : 
      48             : #include <sfx2/objsh.hxx>
      49             : #include <tools/urlobj.hxx>
      50             : 
      51             : #include "vbaglobals.hxx"
      52             : #include "vbaworkbook.hxx"
      53             : #include "vbaworkbooks.hxx"
      54             : #include <vbahelper/vbahelper.hxx>
      55             : 
      56             : #include <vector>
      57             : #include <osl/file.hxx>
      58             : using namespace ::ooo::vba;
      59             : using namespace ::com::sun::star;
      60             : 
      61             : const sal_Int16 CUSTOM_CHAR = 5;
      62             : 
      63             : static uno::Any
      64           9 : getWorkbook( uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< sheet::XSpreadsheetDocument > &xDoc, const uno::Reference< XHelperInterface >& xParent )
      65             : {
      66             :     // FIXME: fine as long as ScVbaWorkbook is stateless ...
      67           9 :     uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
      68           9 :     if( !xModel.is() )
      69           0 :         return uno::Any();
      70             : 
      71          18 :     uno::Reference< excel::XWorkbook > xWb( getVBADocument( xModel ), uno::UNO_QUERY );
      72           9 :     if ( xWb.is() )
      73             :     {
      74             :         OSL_TRACE(" *** Returning Module uno Object *** ");
      75           9 :         return uno::Any( xWb );
      76             :     }
      77             : 
      78           0 :     ScVbaWorkbook *pWb = new ScVbaWorkbook( xParent, xContext, xModel );
      79           9 :     return uno::Any( uno::Reference< excel::XWorkbook > (pWb) );
      80             : }
      81             : 
      82           0 : class WorkBookEnumImpl : public EnumerationHelperImpl
      83             : {
      84             : public:
      85           0 :     WorkBookEnumImpl( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ) {}
      86             : 
      87           0 :     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
      88             :     {
      89           0 :         uno::Reference< sheet::XSpreadsheetDocument > xDoc( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
      90           0 :         return getWorkbook( m_xContext, xDoc, m_xParent );
      91             :     }
      92             : 
      93             : };
      94             : 
      95          10 : ScVbaWorkbooks::ScVbaWorkbooks( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext >& xContext ) : ScVbaWorkbooks_BASE( xParent, xContext, VbaDocumentsBase::EXCEL_DOCUMENT )
      96             : {
      97          10 : }
      98             : // XEnumerationAccess
      99             : uno::Type
     100           0 : ScVbaWorkbooks::getElementType() throw (uno::RuntimeException)
     101             : {
     102           0 :     return cppu::UnoType<excel::XWorkbook>::get();
     103             : }
     104             : uno::Reference< container::XEnumeration >
     105           0 : ScVbaWorkbooks::createEnumeration() throw (uno::RuntimeException)
     106             : {
     107             :     // #FIXME its possible the WorkBookEnumImpl here doesn't reflect
     108             :     // the state of this object ( although it should ) would be
     109             :     // safer to create an enumeration based on this objects state
     110             :     // rather than one effectively based of the desktop component
     111           0 :     uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
     112           0 :     return new WorkBookEnumImpl( mxParent, mxContext, xEnumerationAccess->createEnumeration() );
     113             : }
     114             : 
     115             : uno::Any
     116           8 : ScVbaWorkbooks::createCollectionObject( const css::uno::Any& aSource )
     117             : {
     118           8 :     uno::Reference< sheet::XSpreadsheetDocument > xDoc( aSource, uno::UNO_QUERY_THROW );
     119           8 :     return getWorkbook( mxContext, xDoc, mxParent );
     120             : }
     121             : 
     122             : uno::Any SAL_CALL
     123           1 : ScVbaWorkbooks::Add( const uno::Any& Template ) throw (uno::RuntimeException, std::exception)
     124             : {
     125           1 :     uno::Reference< sheet::XSpreadsheetDocument > xSpreadDoc;
     126           1 :     sal_Int32 nWorkbookType = 0;
     127           2 :     OUString aTemplateFileName;
     128           1 :     if( Template >>= nWorkbookType )
     129             :     {
     130             :         // nWorkbookType is a constant from XlWBATemplate (added in Excel 2007)
     131             :         // TODO: create chart-sheet if supported by Calc
     132             : 
     133           0 :         xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
     134             :         // create a document with one sheet only
     135           0 :         uno::Reference< sheet::XSpreadsheets > xSheets( xSpreadDoc->getSheets(), uno::UNO_SET_THROW );
     136           0 :         uno::Reference< container::XIndexAccess > xSheetsIA( xSheets, uno::UNO_QUERY_THROW );
     137           0 :         while( xSheetsIA->getCount() > 1 )
     138             :         {
     139           0 :             uno::Reference< container::XNamed > xSheetName( xSheetsIA->getByIndex( xSheetsIA->getCount() - 1 ), uno::UNO_QUERY_THROW );
     140           0 :             xSheets->removeByName( xSheetName->getName() );
     141           0 :         }
     142             :     }
     143           1 :     else if( Template >>= aTemplateFileName )
     144             :     {
     145             :         // TODO: create document from template
     146           0 :         xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
     147             :     }
     148           1 :     else if( !Template.hasValue() )
     149             :     {
     150             :         // regular spreadsheet document with configured number of sheets
     151           1 :         xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
     152             :     }
     153             :     else
     154             :     {
     155             :         // illegal argument
     156           0 :         throw uno::RuntimeException();
     157             :     }
     158             : 
     159             :     // need to set up the document modules ( and vba mode ) here
     160           1 :     excel::setUpDocumentModules( xSpreadDoc );
     161           1 :     if( xSpreadDoc.is() )
     162           1 :         return getWorkbook( mxContext, xSpreadDoc, mxParent );
     163           1 :     return uno::Any();
     164             : }
     165             : 
     166             : void SAL_CALL
     167           0 : ScVbaWorkbooks::Close() throw (uno::RuntimeException, std::exception)
     168             : {
     169           0 : }
     170             : 
     171             : bool
     172           0 : ScVbaWorkbooks::isTextFile( const OUString& sType )
     173             : {
     174             :     // will return true if the file is
     175             :     // a) a variant of a text file
     176             :     // b) a csv file
     177             :     // c) unknown
     178             :     // returning true basically means treat this like a csv file
     179           0 :     return sType == "generic_Text" || sType.isEmpty();
     180             : }
     181             : 
     182             : bool
     183           0 : ScVbaWorkbooks::isSpreadSheetFile( const OUString& sType )
     184             : {
     185             :     // include calc_QPro etc. ? ( not for the moment anyway )
     186           0 :     if ( sType.startsWith( "calc_MS" )
     187           0 :       || sType.startsWith( "calc8" )
     188           0 :       || sType.startsWith( "calc_StarOffice" ) )
     189           0 :         return true;
     190           0 :     return false;
     191             : }
     192             : 
     193             : OUString
     194           0 : ScVbaWorkbooks::getFileFilterType( const OUString& rFileName )
     195             : {
     196           0 :     uno::Reference< document::XTypeDetection > xTypeDetect( mxContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", mxContext), uno::UNO_QUERY_THROW );
     197           0 :     uno::Sequence< beans::PropertyValue > aMediaDesc(1);
     198           0 :     aMediaDesc[ 0 ].Name = "URL";
     199           0 :     aMediaDesc[ 0 ].Value <<= rFileName;
     200           0 :     OUString sType = xTypeDetect->queryTypeByDescriptor( aMediaDesc, sal_True );
     201           0 :     return sType;
     202             : }
     203             : 
     204             : // #TODO# #FIXME# can any of the unused params below be used?
     205             : uno::Any SAL_CALL
     206           0 : ScVbaWorkbooks::Open( const OUString& rFileName, const uno::Any& /*UpdateLinks*/, const uno::Any& ReadOnly, const uno::Any& Format, const uno::Any& /*Password*/, const uno::Any& /*WriteResPassword*/, const uno::Any& /*IgnoreReadOnlyRecommended*/, const uno::Any& /*Origin*/, const uno::Any& Delimiter, const uno::Any& /*Editable*/, const uno::Any& /*Notify*/, const uno::Any& /*Converter*/, const uno::Any& /*AddToMru*/ ) throw (uno::RuntimeException, std::exception)
     207             : {
     208             :     // we need to detect if this is a URL, if not then assume it's a file path
     209           0 :     OUString aURL;
     210           0 :     INetURLObject aObj;
     211           0 :     aObj.SetURL( rFileName );
     212           0 :     bool bIsURL = aObj.GetProtocol() != INetProtocol::NotValid;
     213           0 :     if ( bIsURL )
     214           0 :         aURL = rFileName;
     215             :     else
     216           0 :         osl::FileBase::getFileURLFromSystemPath( rFileName, aURL );
     217             : 
     218           0 :     uno::Sequence< beans::PropertyValue > sProps(0);
     219             : 
     220           0 :     OUString sType = getFileFilterType( aURL );
     221             :     // A text file means it needs to be processed as a csv file
     222           0 :     if ( isTextFile( sType ) )
     223             :     {
     224           0 :         sal_Int32 nIndex = 0;
     225             :         // Values for format
     226             :         // 1 Tabs
     227             :         // 2 Commas
     228             :         // 3 Spaces
     229             :         // 4 Semicolons
     230             :         // 5 Nothing
     231             :         // 6 Custom character (see the Delimiter argument
     232             :         // no format means use the current delimiter
     233           0 :         sProps.realloc( 3 );
     234           0 :         sProps[ nIndex ].Name = "FilterOptions";
     235           0 :         sal_Int16 delims[] = { 0 /*default not used*/, 9/*tab*/, 44/*comma*/, 32/*space*/, 59/*semicolon*/ };
     236             :         static const char sRestOfFormat[] = ",34,0,1";
     237             : 
     238           0 :         OUString sFormat;
     239           0 :         sal_Int16 nFormat = 0; // default indicator
     240             : 
     241           0 :         if ( Format.hasValue() )
     242             :         {
     243           0 :             Format >>= nFormat; // val of nFormat overwritten if extracted
     244             :             // validate param
     245           0 :             if ( nFormat < 1 || nFormat > 6 )
     246           0 :                 throw uno::RuntimeException("Illegal value for Format" );
     247             :         }
     248             : 
     249           0 :         sal_Int16 nDelim = getCurrentDelim();
     250             : 
     251           0 :         if (  nFormat > 0 && nFormat < CUSTOM_CHAR )
     252             :         {
     253           0 :             nDelim =  delims[ nFormat ];
     254             :         }
     255           0 :         else if ( nFormat > CUSTOM_CHAR )
     256             :         {
     257             :             // Need to check Delimiter param
     258           0 :             if ( !Delimiter.hasValue() )
     259           0 :                 throw uno::RuntimeException("Expected value for Delimiter" );
     260           0 :             OUString sStr;
     261           0 :             Delimiter >>= sStr;
     262           0 :             if ( !sStr.isEmpty() )
     263           0 :                 nDelim = sStr[0];
     264             :             else
     265           0 :                 throw uno::RuntimeException("Incorrect value for Delimiter" );
     266             :         }
     267             : 
     268           0 :         getCurrentDelim() = nDelim; //set new current
     269             : 
     270           0 :         sFormat = OUString::number( nDelim ) + sRestOfFormat;
     271           0 :         sProps[ nIndex++ ].Value <<= sFormat;
     272           0 :         sProps[ nIndex ].Name = "FilterName";
     273           0 :         sProps[ nIndex++ ].Value <<= OUString( "Text - txt - csv (StarCalc)" );
     274             :         // Ensure WORKAROUND_CSV_TXT_BUG_i60158 gets called in typedetection.cxx so
     275             :         // csv is forced for deep detected 'writerxxx' types
     276           0 :         sProps[ nIndex ].Name = "DocumentService";
     277           0 :         sProps[ nIndex ].Value <<= OUString("com.sun.star.sheet.SpreadsheetDocument");
     278             :     }
     279           0 :     else if ( !isSpreadSheetFile( sType ) )
     280           0 :         throw uno::RuntimeException("Bad Format" );
     281             : 
     282           0 :     uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( openDocument( rFileName, ReadOnly, sProps ), uno::UNO_QUERY_THROW );
     283           0 :     uno::Any aRet = getWorkbook( mxContext, xSpreadDoc, mxParent );
     284           0 :     uno::Reference< excel::XWorkbook > xWBook( aRet, uno::UNO_QUERY );
     285           0 :     if ( xWBook.is() )
     286           0 :         xWBook->Activate();
     287           0 :     return aRet;
     288             : }
     289             : 
     290             : OUString
     291           0 : ScVbaWorkbooks::getServiceImplName()
     292             : {
     293           0 :     return OUString("ScVbaWorkbooks");
     294             : }
     295             : 
     296             : css::uno::Sequence<OUString>
     297           0 : ScVbaWorkbooks::getServiceNames()
     298             : {
     299           0 :     static uno::Sequence< OUString > sNames;
     300           0 :     if ( sNames.getLength() == 0 )
     301             :     {
     302           0 :         sNames.realloc( 1 );
     303           0 :         sNames[0] = "ooo.vba.excel.Workbooks";
     304             :     }
     305           0 :     return sNames;
     306           9 : }
     307             : 
     308             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11