LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/vbahelper/source/vbahelper - vbadocumentsbase.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 64 124 51.6 %
Date: 2013-07-09 Functions: 16 24 66.7 %
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/vbadocumentsbase.hxx"
      21             : 
      22             : #include <comphelper/mediadescriptor.hxx>
      23             : #include <comphelper/processfactory.hxx>
      24             : #include <cppuhelper/implbase1.hxx>
      25             : #include <cppuhelper/implbase3.hxx>
      26             : #include <com/sun/star/frame/XDesktop.hpp>
      27             : #include <com/sun/star/container/XEnumerationAccess.hpp>
      28             : #include <com/sun/star/frame/XComponentLoader.hpp>
      29             : #include <com/sun/star/lang/XComponent.hpp>
      30             : #include <com/sun/star/frame/Desktop.hpp>
      31             : #include <com/sun/star/frame/XModel.hpp>
      32             : #include <com/sun/star/frame/XFrame.hpp>
      33             : #include <com/sun/star/frame/XTitle.hpp>
      34             : #include <com/sun/star/frame/FrameSearchFlag.hpp>
      35             : #include <com/sun/star/util/XModifiable.hpp>
      36             : #include <com/sun/star/frame/XStorable.hpp>
      37             : #include <com/sun/star/lang/DisposedException.hpp>
      38             : #include <com/sun/star/beans/PropertyVetoException.hpp>
      39             : #include <com/sun/star/util/XCloseable.hpp>
      40             : #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
      41             : #include <com/sun/star/document/XTypeDetection.hpp>
      42             : #include <com/sun/star/document/MacroExecMode.hpp>
      43             : #include <com/sun/star/uri/XUriReference.hpp>
      44             : #include <com/sun/star/uri/XUriReferenceFactory.hpp>
      45             : #include <com/sun/star/lang/XServiceInfo.hpp>
      46             : #include <sfx2/objsh.hxx>
      47             : #include <tools/urlobj.hxx>
      48             : #include <boost/unordered_map.hpp>
      49             : #include <osl/file.hxx>
      50             : 
      51             : #include "vbahelper/vbahelper.hxx"
      52             : #include "vbahelper/vbaapplicationbase.hxx"
      53             : #include "vbahelper/vbadocumentbase.hxx"
      54             : 
      55             : using namespace ::ooo::vba;
      56             : using namespace ::com::sun::star;
      57             : 
      58             : static const char aSpreadsheetDocument[] = "com.sun.star.sheet.SpreadsheetDocument";
      59             : static const char aTextDocument[] = "com.sun.star.text.TextDocument";
      60             : 
      61             : typedef  boost::unordered_map< OUString,
      62             : sal_Int32, OUStringHash,
      63             : ::std::equal_to< OUString > > NameIndexHash;
      64             : 
      65             : typedef std::vector < uno::Reference< frame::XModel > > Documents;
      66             : 
      67             : typedef ::cppu::WeakImplHelper1< container::XEnumeration > DocumentsEnumImpl_BASE;
      68             : 
      69             : // #FIXME clearly this is a candidate for some sort of helper base class as
      70             : // this is a copy of SelectedSheetsEnum ( vbawindow.cxx )
      71             : 
      72          20 : class DocumentsEnumImpl : public DocumentsEnumImpl_BASE
      73             : {
      74             :     uno::Reference< uno::XComponentContext > m_xContext;
      75             :     Documents m_documents;
      76             :     Documents::const_iterator m_it;
      77             : 
      78             : public:
      79           0 :     DocumentsEnumImpl( const uno::Reference< uno::XComponentContext >& xContext, const Documents& docs ) throw ( uno::RuntimeException ) :  m_xContext( xContext ), m_documents( docs )
      80             :     {
      81           0 :         m_it = m_documents.begin();
      82           0 :     }
      83          10 :     DocumentsEnumImpl( const uno::Reference< uno::XComponentContext >& xContext ) throw ( uno::RuntimeException ) :  m_xContext( xContext )
      84             :     {
      85          10 :         uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( m_xContext );
      86          20 :         uno::Reference< container::XEnumeration > mxComponents = xDesktop->getComponents()->createEnumeration();
      87          32 :         while( mxComponents->hasMoreElements() )
      88             :         {
      89          12 :             uno::Reference< frame::XModel > xNext( mxComponents->nextElement(), uno::UNO_QUERY );
      90          12 :             if ( xNext.is() )
      91          12 :                 m_documents.push_back( xNext );
      92          12 :         }
      93          20 :         m_it = m_documents.begin();
      94          10 :     }
      95             :     // XEnumeration
      96          34 :     virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
      97             :     {
      98          34 :         return m_it != m_documents.end();
      99             :     }
     100             : 
     101          12 :     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
     102             :     {
     103          12 :         if ( !hasMoreElements() )
     104             :         {
     105           0 :             throw container::NoSuchElementException();
     106             :         }
     107          12 :         return makeAny( *(m_it++) );
     108             :     }
     109             : };
     110             : 
     111             : // #FIXME clearly this is also a candidate for some sort of helper base class as
     112             : // a very similar one is used in vbawindow ( SelectedSheetsEnumAccess )
     113             : // Maybe a template base class that does all of the operations on the hashmap
     114             : // and vector only, and the sub-class does everything else
     115             : // => ctor, createEnumeration & factory method need be defined ( to be called
     116             : // by getByIndex, getByName )
     117             : typedef ::cppu::WeakImplHelper3< container::XEnumerationAccess
     118             :     , com::sun::star::container::XIndexAccess
     119             :     , com::sun::star::container::XNameAccess
     120             :     > DocumentsAccessImpl_BASE;
     121             : 
     122          20 : class DocumentsAccessImpl : public DocumentsAccessImpl_BASE
     123             : {
     124             :     uno::Reference< uno::XComponentContext > m_xContext;
     125             :     Documents m_documents;
     126             :     NameIndexHash namesToIndices;
     127             :     VbaDocumentsBase::DOCUMENT_TYPE meDocType;
     128             : public:
     129          10 :     DocumentsAccessImpl( const uno::Reference< uno::XComponentContext >& xContext, VbaDocumentsBase::DOCUMENT_TYPE eDocType ) throw (uno::RuntimeException) :m_xContext( xContext ), meDocType( eDocType )
     130             :     {
     131          10 :         uno::Reference< container::XEnumeration > xEnum = new DocumentsEnumImpl( m_xContext );
     132          10 :         sal_Int32 nIndex=0;
     133          32 :         while( xEnum->hasMoreElements() )
     134             :         {
     135          12 :             uno::Reference< lang::XServiceInfo > xServiceInfo( xEnum->nextElement(), uno::UNO_QUERY );
     136          36 :             if ( xServiceInfo.is()
     137          60 :                 && (  ( xServiceInfo->supportsService( OUString(aSpreadsheetDocument) ) && meDocType == VbaDocumentsBase::EXCEL_DOCUMENT )
     138          12 :                 || ( xServiceInfo->supportsService( OUString(aTextDocument) ) && meDocType == VbaDocumentsBase::WORD_DOCUMENT ) ) )
     139             :             {
     140          12 :                 uno::Reference< frame::XModel > xModel( xServiceInfo, uno::UNO_QUERY_THROW ); // that the spreadsheetdocument is a xmodel is a given
     141          12 :                 m_documents.push_back( xModel );
     142          24 :                 OUString sName = VbaDocumentBase::getNameFromModel( xModel );
     143          24 :                 namesToIndices[ sName ] = nIndex++;
     144             :             }
     145          22 :         }
     146             : 
     147          10 :     }
     148             : 
     149             :     //XEnumerationAccess
     150           0 :     virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration(  ) throw (uno::RuntimeException)
     151             :     {
     152           0 :         return new DocumentsEnumImpl( m_xContext, m_documents );
     153             :     }
     154             :     // XIndexAccess
     155           1 :     virtual ::sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException)
     156             :     {
     157           1 :         return m_documents.size();
     158             :     }
     159           7 :     virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw ( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
     160             :     {
     161           7 :         if ( Index < 0
     162           7 :             || static_cast< Documents::size_type >(Index) >= m_documents.size() )
     163           0 :             throw lang::IndexOutOfBoundsException();
     164           7 :         return makeAny( m_documents[ Index ] ); // returns xspreadsheetdoc
     165             :     }
     166             : 
     167             :     //XElementAccess
     168           0 :     virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException)
     169             :     {
     170           0 :         return frame::XModel::static_type(0);
     171             :     }
     172             : 
     173           0 :     virtual ::sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException)
     174             :     {
     175           0 :         return (!m_documents.empty());
     176             :     }
     177             : 
     178             :     //XNameAccess
     179           1 :     virtual uno::Any SAL_CALL getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
     180             :     {
     181           1 :         NameIndexHash::const_iterator it = namesToIndices.find( aName );
     182           1 :         if ( it == namesToIndices.end() )
     183           0 :             throw container::NoSuchElementException();
     184           1 :         return makeAny( m_documents[ it->second ] );
     185             : 
     186             :     }
     187             : 
     188           0 :     virtual uno::Sequence< OUString > SAL_CALL getElementNames(  ) throw (uno::RuntimeException)
     189             :     {
     190           0 :         uno::Sequence< OUString > names( namesToIndices.size() );
     191           0 :         OUString* pString = names.getArray();
     192           0 :         NameIndexHash::const_iterator it = namesToIndices.begin();
     193           0 :         NameIndexHash::const_iterator it_end = namesToIndices.end();
     194           0 :         for ( ; it != it_end; ++it, ++pString )
     195           0 :             *pString = it->first;
     196           0 :         return names;
     197             :     }
     198             : 
     199           0 :     virtual ::sal_Bool SAL_CALL hasByName( const OUString& aName ) throw (uno::RuntimeException)
     200             :     {
     201           0 :         NameIndexHash::const_iterator it = namesToIndices.find( aName );
     202           0 :         return (it != namesToIndices.end());
     203             :     }
     204             : 
     205             : };
     206             : 
     207          10 : VbaDocumentsBase::VbaDocumentsBase( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext >& xContext, DOCUMENT_TYPE eDocType ) throw (uno::RuntimeException) : VbaDocumentsBase_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( new DocumentsAccessImpl( xContext, eDocType ) ) ), meDocType( eDocType )
     208             : {
     209          10 : }
     210             : 
     211             : namespace {
     212             : 
     213           1 : void lclSetupComponent( const uno::Reference< lang::XComponent >& rxComponent, sal_Bool bScreenUpdating, sal_Bool bInteractive )
     214             : {
     215           1 :     if( !bScreenUpdating ) try
     216             :     {
     217           0 :         uno::Reference< frame::XModel >( rxComponent, uno::UNO_QUERY_THROW )->lockControllers();
     218             :     }
     219           0 :     catch( uno::Exception& )
     220             :     {
     221             :     }
     222             : 
     223           1 :     if( !bInteractive ) try
     224             :     {
     225           0 :         uno::Reference< frame::XModel > xModel( rxComponent, uno::UNO_QUERY_THROW );
     226           0 :         uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
     227           0 :         uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
     228           0 :         uno::Reference< awt::XWindow >( xFrame->getContainerWindow(), uno::UNO_SET_THROW )->setEnable( sal_False );
     229             :     }
     230           0 :     catch( uno::Exception& )
     231             :     {
     232             :     }
     233           1 : }
     234             : 
     235             : } // namespace
     236             : 
     237           1 : uno::Any VbaDocumentsBase::createDocument() throw (uno::RuntimeException)
     238             : {
     239             :     // #163808# determine state of Application.ScreenUpdating and Application.Interactive symbols (before new document is opened)
     240           1 :     uno::Reference< XApplicationBase > xApplication( Application(), uno::UNO_QUERY );
     241           1 :     sal_Bool bScreenUpdating = !xApplication.is() || xApplication->getScreenUpdating();
     242           1 :     sal_Bool bInteractive = !xApplication.is() || xApplication->getInteractive();
     243             : 
     244           2 :     uno::Reference< frame::XDesktop2 > xLoader = frame::Desktop::create(mxContext);
     245           2 :     OUString sURL;
     246           1 :     if( meDocType == WORD_DOCUMENT )
     247           0 :         sURL = "private:factory/swriter";
     248           1 :     else if( meDocType == EXCEL_DOCUMENT )
     249           1 :         sURL = "private:factory/scalc";
     250             :     else
     251           0 :         throw uno::RuntimeException( "Not implemented" , uno::Reference< uno::XInterface >() );
     252             : 
     253             :     // prepare the media descriptor
     254           2 :     ::comphelper::MediaDescriptor aMediaDesc;
     255           1 :     aMediaDesc[ ::comphelper::MediaDescriptor::PROP_MACROEXECUTIONMODE() ] <<= document::MacroExecMode::USE_CONFIG;
     256           1 :     aMediaDesc.setComponentDataEntry( "ApplyFormDesignMode" , uno::Any( false ) );
     257             : 
     258             :     // create the new document
     259           1 :     uno::Reference< lang::XComponent > xComponent = xLoader->loadComponentFromURL(
     260             :                                        sURL , "_blank", 0,
     261           2 :                                        aMediaDesc.getAsConstPropertyValueList() );
     262             : 
     263             :     // #163808# lock document controllers and container window if specified by application
     264           1 :     lclSetupComponent( xComponent, bScreenUpdating, bInteractive );
     265             : 
     266           2 :     return uno::makeAny( xComponent );
     267             : }
     268             : 
     269           0 : void VbaDocumentsBase::closeDocuments() throw (uno::RuntimeException)
     270             : {
     271             : // #FIXME this *MUST* be wrong documents::close surely closes ALL documents
     272             : // in the collection, use of getCurrentDocument here is totally wrong
     273             : /*
     274             :     uno::Reference< lang::XMultiComponentFactory > xSMgr(
     275             :         mxContext->getServiceManager(), uno::UNO_QUERY_THROW );
     276             :     uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
     277             :     OUString url = OUString(  ".uno:CloseDoc");
     278             :     dispatchRequests(xModel,url);
     279             : */
     280           0 : }
     281             : 
     282             : // #TODO# #FIXME# can any of the unused params below be used?
     283           0 : uno::Any VbaDocumentsBase::openDocument( const OUString& rFileName, const uno::Any& ReadOnly, const uno::Sequence< beans::PropertyValue >& rProps ) throw (uno::RuntimeException)
     284             : {
     285             :     // #163808# determine state of Application.ScreenUpdating and Application.Interactive symbols (before new document is opened)
     286           0 :     uno::Reference< XApplicationBase > xApplication( Application(), uno::UNO_QUERY );
     287           0 :     sal_Bool bScreenUpdating = !xApplication.is() || xApplication->getScreenUpdating();
     288           0 :     sal_Bool bInteractive = !xApplication.is() || xApplication->getInteractive();
     289             : 
     290             :     // we need to detect if this is a URL, if not then assume its a file path
     291           0 :         OUString aURL;
     292           0 :         INetURLObject aObj;
     293           0 :     aObj.SetURL( rFileName );
     294           0 :     bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
     295           0 :     if ( bIsURL )
     296           0 :         aURL = rFileName;
     297             :     else
     298           0 :         osl::FileBase::getFileURLFromSystemPath( rFileName, aURL );
     299           0 :     uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( mxContext );
     300             : 
     301           0 :     uno::Sequence< beans::PropertyValue > sProps( rProps );
     302           0 :     sProps.realloc( sProps.getLength() + 1 );
     303           0 :     sProps[ sProps.getLength() - 1 ].Name = "MacroExecutionMode";
     304           0 :     sProps[ sProps.getLength() - 1 ].Value <<= document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
     305             : 
     306           0 :     if ( ReadOnly.hasValue()  )
     307             :     {
     308           0 :         sal_Bool bIsReadOnly = sal_False; ReadOnly >>= bIsReadOnly;
     309           0 :         if ( bIsReadOnly )
     310             :         {
     311           0 :             sProps.realloc( sProps.getLength() + 1 );
     312           0 :             sProps[ sProps.getLength() - 1 ].Name = "ReadOnly";
     313           0 :             sProps[ sProps.getLength() - 1 ].Value <<= true;
     314             :         }
     315             :     }
     316             : 
     317           0 :     uno::Reference< lang::XComponent > xComponent = xDesktop->loadComponentFromURL( aURL,
     318             :         "_default" ,
     319             :         frame::FrameSearchFlag::CREATE,
     320           0 :         sProps);
     321             : 
     322             :     // #163808# lock document controllers and container window if specified by application
     323           0 :     lclSetupComponent( xComponent, bScreenUpdating, bInteractive );
     324             : 
     325           0 :     return uno::makeAny( xComponent );
     326         186 : }
     327             : 
     328             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10