LCOV - code coverage report
Current view: top level - libreoffice/sc/source/ui/vba - vbaworkbooks.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 165 0.0 %
Date: 2012-12-27 Functions: 0 18 0.0 %
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 <boost/unordered_map.hpp>
      57             : #include <vector>
      58             : #include <osl/file.hxx>
      59             : using namespace ::ooo::vba;
      60             : using namespace ::com::sun::star;
      61             : 
      62             : const sal_Int16 CUSTOM_CHAR = 5;
      63             : 
      64           0 : void setUpDocumentModules( const uno::Reference< sheet::XSpreadsheetDocument >& xDoc )
      65             : {
      66           0 :     uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
      67           0 :     ScDocShell* pShell = excel::getDocShell( xModel );
      68           0 :     if ( pShell )
      69             :     {
      70           0 :         String aPrjName( RTL_CONSTASCII_USTRINGPARAM( "Standard" ) );
      71           0 :         pShell->GetBasicManager()->SetName( aPrjName );
      72             : 
      73             :         /*  Set library container to VBA compatibility mode. This will create
      74             :             the VBA Globals object and store it in the Basic manager of the
      75             :             document. */
      76           0 :         uno::Reference<script::XLibraryContainer> xLibContainer = pShell->GetBasicContainer();
      77           0 :         uno::Reference<script::vba::XVBACompatibility> xVBACompat( xLibContainer, uno::UNO_QUERY_THROW );
      78           0 :         xVBACompat->setVBACompatibilityMode( sal_True );
      79             : 
      80           0 :         if( xLibContainer.is() )
      81             :         {
      82           0 :             if( !xLibContainer->hasByName( aPrjName ) )
      83           0 :                 xLibContainer->createLibrary( aPrjName );
      84           0 :             uno::Any aLibAny = xLibContainer->getByName( aPrjName );
      85           0 :             uno::Reference< container::XNameContainer > xLib;
      86           0 :             aLibAny >>= xLib;
      87           0 :             if( xLib.is()  )
      88             :             {
      89           0 :                 uno::Reference< script::vba::XVBAModuleInfo > xVBAModuleInfo( xLib, uno::UNO_QUERY_THROW );
      90           0 :                 uno::Reference< lang::XMultiServiceFactory> xSF( pShell->GetModel(), uno::UNO_QUERY_THROW);
      91           0 :                 uno::Reference< container::XNameAccess > xVBACodeNamedObjectAccess( xSF->createInstance( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.VBAObjectModuleObjectProvider"))), uno::UNO_QUERY_THROW );
      92             :                 // set up the module info for the workbook and sheets in the nealy created
      93             :                 // spreadsheet
      94           0 :                 ScDocument* pDoc = pShell->GetDocument();
      95           0 :                 String sCodeName = pDoc->GetCodeName();
      96           0 :                 if ( sCodeName.Len() == 0 )
      97             :                 {
      98           0 :                     sCodeName = String( RTL_CONSTASCII_USTRINGPARAM("ThisWorkbook") );
      99           0 :                     pDoc->SetCodeName( sCodeName );
     100             :                 }
     101             : 
     102           0 :                 std::vector< rtl::OUString > sDocModuleNames;
     103           0 :                 sDocModuleNames.push_back( sCodeName );
     104             : 
     105           0 :                 uno::Reference<container::XNameAccess > xSheets( xDoc->getSheets(), uno::UNO_QUERY_THROW );
     106           0 :                 uno::Sequence< rtl::OUString > sSheets( xSheets->getElementNames() );
     107             : 
     108           0 :                 for ( sal_Int32 index=0; index < sSheets.getLength() ; ++index )
     109             :                 {
     110           0 :                     sDocModuleNames.push_back( sSheets[ index ] );
     111             :                 }
     112             : 
     113           0 :                 std::vector<rtl::OUString>::iterator it_end = sDocModuleNames.end();
     114             : 
     115           0 :                 for ( std::vector<rtl::OUString>::iterator it = sDocModuleNames.begin(); it != it_end; ++it )
     116             :                 {
     117           0 :                     script::ModuleInfo sModuleInfo;
     118             : 
     119           0 :                     sModuleInfo.ModuleObject.set( xVBACodeNamedObjectAccess->getByName( *it ), uno::UNO_QUERY );
     120           0 :                     sModuleInfo.ModuleType = script::ModuleType::DOCUMENT;
     121           0 :                     xVBAModuleInfo->insertModuleInfo( *it, sModuleInfo );
     122           0 :                     if( xLib->hasByName( *it ) )
     123           0 :                         xLib->replaceByName( *it, uno::makeAny( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Option VBASupport 1\n") ) ) );
     124             :                     else
     125           0 :                         xLib->insertByName( *it, uno::makeAny( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Option VBASupport 1\n" ) ) ) );
     126           0 :                 }
     127           0 :             }
     128           0 :         }
     129           0 :     }
     130           0 : }
     131             : 
     132             : static uno::Any
     133           0 : getWorkbook( uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< sheet::XSpreadsheetDocument > &xDoc, const uno::Reference< XHelperInterface >& xParent )
     134             : {
     135             :     // FIXME: fine as long as ScVbaWorkbook is stateless ...
     136           0 :     uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
     137           0 :     if( !xModel.is() )
     138           0 :         return uno::Any();
     139             : 
     140           0 :     uno::Reference< excel::XWorkbook > xWb( getVBADocument( xModel ), uno::UNO_QUERY );
     141           0 :     if ( xWb.is() )
     142             :     {
     143             :         OSL_TRACE(" *** Returning Module uno Object *** ");
     144           0 :         return uno::Any( xWb );
     145             :     }
     146             : 
     147           0 :     ScVbaWorkbook *pWb = new ScVbaWorkbook( xParent, xContext, xModel );
     148           0 :     return uno::Any( uno::Reference< excel::XWorkbook > (pWb) );
     149             : }
     150             : 
     151           0 : class WorkBookEnumImpl : public EnumerationHelperImpl
     152             : {
     153             :     uno::Any m_aApplication;
     154             : public:
     155           0 :     WorkBookEnumImpl( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Any& aApplication ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), m_aApplication( aApplication ) {}
     156             : 
     157           0 :     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
     158             :     {
     159           0 :         uno::Reference< sheet::XSpreadsheetDocument > xDoc( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
     160           0 :         return getWorkbook( m_xContext, xDoc, m_xParent );
     161             :     }
     162             : 
     163             : };
     164             : 
     165           0 : ScVbaWorkbooks::ScVbaWorkbooks( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext >& xContext ) : ScVbaWorkbooks_BASE( xParent, xContext, VbaDocumentsBase::EXCEL_DOCUMENT )
     166             : {
     167           0 : }
     168             : // XEnumerationAccess
     169             : uno::Type
     170           0 : ScVbaWorkbooks::getElementType() throw (uno::RuntimeException)
     171             : {
     172           0 :     return excel::XWorkbook::static_type(0);
     173             : }
     174             : uno::Reference< container::XEnumeration >
     175           0 : ScVbaWorkbooks::createEnumeration() throw (uno::RuntimeException)
     176             : {
     177             :     // #FIXME its possible the WorkBookEnumImpl here doens't reflect
     178             :     // the state of this object ( although it should ) would be
     179             :     // safer to create an enumeration based on this objects state
     180             :     // rather than one effectively based of the desktop component
     181           0 :     uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
     182           0 :     return new WorkBookEnumImpl( mxParent, mxContext, xEnumerationAccess->createEnumeration(), Application() );
     183             : }
     184             : 
     185             : uno::Any
     186           0 : ScVbaWorkbooks::createCollectionObject( const css::uno::Any& aSource )
     187             : {
     188           0 :     uno::Reference< sheet::XSpreadsheetDocument > xDoc( aSource, uno::UNO_QUERY_THROW );
     189           0 :     return getWorkbook( mxContext, xDoc, mxParent );
     190             : }
     191             : 
     192             : 
     193             : uno::Any SAL_CALL
     194           0 : ScVbaWorkbooks::Add( const uno::Any& Template ) throw (uno::RuntimeException)
     195             : {
     196           0 :     uno::Reference< sheet::XSpreadsheetDocument > xSpreadDoc;
     197           0 :     sal_Int32 nWorkbookType = 0;
     198           0 :     ::rtl::OUString aTemplateFileName;
     199           0 :     if( Template >>= nWorkbookType )
     200             :     {
     201             :         // nWorkbookType is a constant from XlWBATemplate (added in Excel 2007)
     202             :         // TODO: create chart-sheet if supported by Calc
     203             : 
     204           0 :         xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
     205             :         // create a document with one sheet only
     206           0 :         uno::Reference< sheet::XSpreadsheets > xSheets( xSpreadDoc->getSheets(), uno::UNO_SET_THROW );
     207           0 :         uno::Reference< container::XIndexAccess > xSheetsIA( xSheets, uno::UNO_QUERY_THROW );
     208           0 :         while( xSheetsIA->getCount() > 1 )
     209             :         {
     210           0 :             uno::Reference< container::XNamed > xSheetName( xSheetsIA->getByIndex( xSheetsIA->getCount() - 1 ), uno::UNO_QUERY_THROW );
     211           0 :             xSheets->removeByName( xSheetName->getName() );
     212           0 :         }
     213             :     }
     214           0 :     else if( Template >>= aTemplateFileName )
     215             :     {
     216             :         // TODO: create document from template
     217           0 :         xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
     218             :     }
     219           0 :     else if( !Template.hasValue() )
     220             :     {
     221             :         // regular spreadsheet document with configured number of sheets
     222           0 :         xSpreadDoc.set( createDocument(), uno::UNO_QUERY_THROW );
     223             :     }
     224             :     else
     225             :     {
     226             :         // illegal argument
     227           0 :         throw uno::RuntimeException();
     228             :     }
     229             : 
     230             :     // need to set up the document modules ( and vba mode ) here
     231           0 :     excel::setUpDocumentModules( xSpreadDoc );
     232           0 :     if( xSpreadDoc.is() )
     233           0 :         return getWorkbook( mxContext, xSpreadDoc, mxParent );
     234           0 :     return uno::Any();
     235             : }
     236             : 
     237             : void SAL_CALL
     238           0 : ScVbaWorkbooks::Close() throw (uno::RuntimeException)
     239             : {
     240           0 :     closeDocuments();
     241           0 : }
     242             : 
     243             : bool
     244           0 : ScVbaWorkbooks::isTextFile( const rtl::OUString& sType )
     245             : {
     246             :     // will return true if the file is
     247             :     // a) a variant of a text file
     248             :     // b) a csv file
     249             :     // c) unknown
     250             :     // returning true basically means treat this like a csv file
     251           0 :     const static rtl::OUString txtType("generic_Text");
     252           0 :     return sType.equals( txtType ) || sType.isEmpty();
     253             : }
     254             : 
     255             : bool
     256           0 : ScVbaWorkbooks::isSpreadSheetFile( const rtl::OUString& sType )
     257             : {
     258             :     // include calc_QPro etc. ? ( not for the moment anyway )
     259           0 :     if ( sType.indexOf( "calc_MS" ) == 0
     260           0 :     || sType.indexOf( "calc8" ) == 0
     261           0 :     || sType.indexOf( "calc_StarOffice" ) == 0 )
     262           0 :         return true;
     263           0 :     return false;
     264             : }
     265             : 
     266             : rtl::OUString
     267           0 : ScVbaWorkbooks::getFileFilterType( const rtl::OUString& rFileName )
     268             : {
     269           0 :     uno::Reference< document::XTypeDetection > xTypeDetect( mxContext->getServiceManager()->createInstanceWithContext(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.document.TypeDetection")), mxContext), uno::UNO_QUERY_THROW );
     270           0 :     uno::Sequence< beans::PropertyValue > aMediaDesc(1);
     271           0 :     aMediaDesc[ 0 ].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ("URL" ) );
     272           0 :     aMediaDesc[ 0 ].Value <<= rFileName;
     273           0 :     rtl::OUString sType = xTypeDetect->queryTypeByDescriptor( aMediaDesc, sal_True );
     274           0 :     return sType;
     275             : }
     276             : 
     277             : // #TODO# #FIXME# can any of the unused params below be used?
     278             : uno::Any SAL_CALL
     279           0 : ScVbaWorkbooks::Open( const rtl::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)
     280             : {
     281             :     // we need to detect if this is a URL, if not then assume its a file path
     282           0 :     rtl::OUString aURL;
     283           0 :     INetURLObject aObj;
     284           0 :     aObj.SetURL( rFileName );
     285           0 :     bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
     286           0 :     if ( bIsURL )
     287           0 :         aURL = rFileName;
     288             :     else
     289           0 :         osl::FileBase::getFileURLFromSystemPath( rFileName, aURL );
     290             : 
     291           0 :     uno::Sequence< beans::PropertyValue > sProps(0);
     292             : 
     293           0 :     rtl::OUString sType = getFileFilterType( aURL );
     294             :     // A text file means it needs to be processed as a csv file
     295           0 :     if ( isTextFile( sType ) )
     296             :     {
     297           0 :         sal_Int32 nIndex = 0;
     298             :         // Values for format
     299             :         // 1 Tabs
     300             :         // 2 Commas
     301             :         // 3 Spaces
     302             :         // 4 Semicolons
     303             :         // 5 Nothing
     304             :         // 6 Custom character (see the Delimiter argument
     305             :         // no format means use the current delimiter
     306           0 :         sProps.realloc( 3 );
     307           0 :         sProps[ nIndex ].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FilterOptions" ) );
     308           0 :         sal_Int16 delims[] = { 0 /*default not used*/, 9/*tab*/, 44/*comma*/, 32/*space*/, 59/*semicolon*/ };
     309           0 :         static rtl::OUString sRestOfFormat( RTL_CONSTASCII_USTRINGPARAM(",34,0,1" ) );
     310             : 
     311           0 :         rtl::OUString sFormat;
     312           0 :         sal_Int16 nFormat = 0; // default indicator
     313             : 
     314             : 
     315           0 :         if ( Format.hasValue() )
     316             :         {
     317           0 :             Format >>= nFormat; // val of nFormat overwritten if extracted
     318             :             // validate param
     319           0 :             if ( nFormat < 1 || nFormat > 6 )
     320           0 :                 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Illegal value for Format" ) ), uno::Reference< uno::XInterface >() );
     321             :         }
     322             : 
     323           0 :         sal_Int16 nDelim = getCurrentDelim();
     324             : 
     325           0 :         if (  nFormat > 0 && nFormat < CUSTOM_CHAR )
     326             :         {
     327           0 :             nDelim =  delims[ nFormat ];
     328             :         }
     329           0 :         else if ( nFormat > CUSTOM_CHAR )
     330             :         {
     331             :             // Need to check Delimiter param
     332           0 :             if ( !Delimiter.hasValue() )
     333           0 :                 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Expected value for Delimiter" ) ), uno::Reference< uno::XInterface >() );
     334           0 :             rtl::OUString sStr;
     335           0 :             Delimiter >>= sStr;
     336           0 :             String aUniStr( sStr );
     337           0 :             if ( aUniStr.Len() )
     338           0 :                 nDelim = aUniStr.GetChar(0);
     339             :             else
     340           0 :                 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Incorrect value for Delimiter" ) ), uno::Reference< uno::XInterface >() );
     341             :         }
     342             : 
     343           0 :         getCurrentDelim() = nDelim; //set new current
     344             : 
     345           0 :         sFormat = rtl::OUString::valueOf( (sal_Int32)nDelim ) + sRestOfFormat;
     346           0 :         sProps[ nIndex++ ].Value <<= sFormat;
     347           0 :         sProps[ nIndex ].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FilterName") );
     348           0 :         sProps[ nIndex++ ].Value <<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Text - txt - csv (StarCalc)") );
     349             :         // Ensure WORKAROUND_CSV_TXT_BUG_i60158 gets called in typedetection.cxx so
     350             :         // csv is forced for deep detected 'writerxxx' types
     351           0 :         sProps[ nIndex ].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("DocumentService") );
     352           0 :         sProps[ nIndex ].Value <<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sheet.SpreadsheetDocument") );
     353             :     }
     354           0 :     else if ( !isSpreadSheetFile( sType ) )
     355           0 :         throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Bad Format")), uno::Reference< uno::XInterface >() );
     356             : 
     357           0 :     uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( openDocument( rFileName, ReadOnly, sProps ), uno::UNO_QUERY_THROW );
     358           0 :     uno::Any aRet = getWorkbook( mxContext, xSpreadDoc, mxParent );
     359           0 :     uno::Reference< excel::XWorkbook > xWBook( aRet, uno::UNO_QUERY );
     360           0 :     if ( xWBook.is() )
     361           0 :         xWBook->Activate();
     362           0 :     return aRet;
     363             : }
     364             : 
     365             : rtl::OUString
     366           0 : ScVbaWorkbooks::getServiceImplName()
     367             : {
     368           0 :     return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ScVbaWorkbooks"));
     369             : }
     370             : 
     371             : css::uno::Sequence<rtl::OUString>
     372           0 : ScVbaWorkbooks::getServiceNames()
     373             : {
     374           0 :     static uno::Sequence< rtl::OUString > sNames;
     375           0 :     if ( sNames.getLength() == 0 )
     376             :     {
     377           0 :         sNames.realloc( 1 );
     378           0 :         sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Workbooks") );
     379             :     }
     380           0 :     return sNames;
     381             : }
     382             : 
     383             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10