LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/vbahelper/source/vbahelper - vbadocumentbase.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 58 131 44.3 %
Date: 2013-07-09 Functions: 10 18 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/vbadocumentbase.hxx"
      21             : #include "vbahelper/helperdecl.hxx"
      22             : 
      23             : #include <com/sun/star/lang/DisposedException.hpp>
      24             : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
      25             : #include <com/sun/star/util/XModifiable.hpp>
      26             : #include <com/sun/star/util/XProtectable.hpp>
      27             : #include <com/sun/star/util/XCloseable.hpp>
      28             : #include <com/sun/star/util/URLTransformer.hpp>
      29             : #include <com/sun/star/util/XURLTransformer.hpp>
      30             : #include <com/sun/star/frame/XStorable.hpp>
      31             : #include <com/sun/star/frame/XFrame.hpp>
      32             : #include <com/sun/star/frame/XTitle.hpp>
      33             : #include <com/sun/star/document/XEmbeddedScripts.hpp> //Michael E. Bohn
      34             : #include <com/sun/star/beans/XPropertySet.hpp>
      35             : #include <ooo/vba/XApplicationBase.hpp>
      36             : 
      37             : #include <cppuhelper/exc_hlp.hxx>
      38             : #include <comphelper/unwrapargs.hxx>
      39             : #include <tools/urlobj.hxx>
      40             : #include <osl/file.hxx>
      41             : 
      42             : using namespace ::com::sun::star;
      43             : using namespace ::ooo::vba;
      44             : 
      45          22 : VbaDocumentBase::VbaDocumentBase( const uno::Reference< ov::XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, uno::Reference< frame::XModel > xModel ) : VbaDocumentBase_BASE( xParent, xContext ),  mxModel( xModel )
      46             : {
      47          22 : }
      48             : 
      49          26 : VbaDocumentBase::VbaDocumentBase( uno::Sequence< uno::Any> const & args,
      50          26 :     uno::Reference< uno::XComponentContext> const & xContext ) : VbaDocumentBase_BASE( getXSomethingFromArgs< XHelperInterface >( args, 0 ), xContext ),  mxModel( getXSomethingFromArgs< frame::XModel >( args, 1 ) )
      51             : {
      52          26 : }
      53             : 
      54             : OUString
      55           9 : VbaDocumentBase::getName() throw (uno::RuntimeException)
      56             : {
      57           9 :     return VbaDocumentBase::getNameFromModel( getModel() );
      58             : }
      59             : 
      60          21 : OUString VbaDocumentBase::getNameFromModel( const uno::Reference< frame::XModel >& xModel )
      61             : {
      62          21 :     OUString sName = xModel.is() ? xModel->getURL() : OUString();
      63          21 :     if ( !sName.isEmpty() )
      64             :     {
      65             : 
      66          20 :         INetURLObject aURL( xModel->getURL() );
      67          20 :         ::osl::File::getSystemPathFromFileURL( aURL.GetLastName(), sName );
      68             :     }
      69             :     else
      70             :     {
      71           1 :         uno::Reference< frame::XTitle > xTitle( xModel, uno::UNO_QUERY_THROW );
      72           1 :         sName = xTitle->getTitle();
      73           1 :         sName = sName.trim();
      74             :     }
      75          21 :     return sName;
      76             : }
      77             : OUString
      78           1 : VbaDocumentBase::getPath() throw (uno::RuntimeException)
      79             : {
      80           1 :     INetURLObject aURL( getModel()->getURL() );
      81           2 :     OUString sURL = aURL.GetMainURL( INetURLObject::DECODE_TO_IURI );
      82           1 :     OUString sPath;
      83           1 :     if( !sURL.isEmpty() )
      84             :     {
      85           1 :        sURL = sURL.copy( 0, sURL.getLength() - aURL.GetLastName().getLength() - 1 );
      86           1 :        ::osl::File::getSystemPathFromFileURL( sURL, sPath );
      87             :     }
      88           2 :     return sPath;
      89             : }
      90             : 
      91             : OUString
      92           1 : VbaDocumentBase::getFullName() throw (uno::RuntimeException)
      93             : {
      94           1 :     OUString sPath = getName();
      95             :     //::osl::File::getSystemPathFromFileURL( getModel()->getURL(), sPath );
      96           1 :     return sPath;
      97             : }
      98             : 
      99             : void
     100           1 : VbaDocumentBase::Close( const uno::Any &rSaveArg, const uno::Any &rFileArg,
     101             :                       const uno::Any &rRouteArg ) throw (uno::RuntimeException)
     102             : {
     103           1 :     sal_Bool bSaveChanges = sal_False;
     104           1 :     OUString aFileName;
     105           1 :     sal_Bool bRouteWorkbook = sal_True;
     106             : 
     107           1 :     rSaveArg >>= bSaveChanges;
     108           1 :     sal_Bool bFileName =  ( rFileArg >>= aFileName );
     109           1 :     rRouteArg >>= bRouteWorkbook;
     110           2 :     uno::Reference< frame::XStorable > xStorable( getModel(), uno::UNO_QUERY_THROW );
     111           2 :     uno::Reference< util::XModifiable > xModifiable( getModel(), uno::UNO_QUERY_THROW );
     112             : 
     113           1 :     if( bSaveChanges )
     114             :     {
     115           0 :         if( xStorable->isReadonly() )
     116             :         {
     117           0 :             throw uno::RuntimeException("Unable to save to a read only file ", uno::Reference< XInterface >() );
     118             :         }
     119           0 :         if( bFileName )
     120           0 :             xStorable->storeAsURL( aFileName, uno::Sequence< beans::PropertyValue >(0) );
     121             :         else
     122           0 :             xStorable->store();
     123             :     }
     124             :     else
     125           1 :         xModifiable->setModified( false );
     126             : 
     127             :     // first try to close the document using UI dispatch functionality
     128           1 :     sal_Bool bUIClose = sal_False;
     129             :     try
     130             :     {
     131           1 :         uno::Reference< frame::XController > xController( getModel()->getCurrentController(), uno::UNO_SET_THROW );
     132           2 :         uno::Reference< frame::XDispatchProvider > xDispatchProvider( xController->getFrame(), uno::UNO_QUERY_THROW );
     133             : 
     134           2 :         uno::Reference< lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW );
     135           2 :         uno::Reference< util::XURLTransformer > xURLTransformer( util::URLTransformer::create(mxContext) );
     136             : 
     137           2 :         util::URL aURL;
     138           1 :         aURL.Complete = ".uno:CloseDoc";
     139           1 :         xURLTransformer->parseStrict( aURL );
     140             : 
     141             :         uno::Reference< css::frame::XDispatch > xDispatch(
     142           1 :                 xDispatchProvider->queryDispatch( aURL, "_self" , 0 ),
     143           2 :                 uno::UNO_SET_THROW );
     144           1 :         xDispatch->dispatch( aURL, uno::Sequence< beans::PropertyValue >() );
     145           2 :         bUIClose = sal_True;
     146             :     }
     147           0 :     catch(const uno::Exception&)
     148             :     {
     149             :     }
     150             : 
     151           1 :     if ( !bUIClose )
     152             :     {
     153             :         // if it is not possible to use UI dispatch, try to close the model directly
     154           0 :         bool bCloseable = false;
     155           0 :         uno::Reference< frame::XModel > xModel = getModel();
     156             :         try
     157             :         {
     158           0 :             uno::Reference< util::XCloseable > xCloseable( xModel, uno::UNO_QUERY );
     159             : 
     160             :             // use close(boolean DeliverOwnership)
     161             :             // The boolean parameter DeliverOwnership tells objects vetoing the close
     162             :             // process that they may assume ownership if they object the closure by
     163             :             // throwing a CloseVetoException. Here we give up ownership. To be on the
     164             :             // safe side, catch possible veto exception anyway.
     165           0 :             if ( xCloseable.is() )
     166             :             {
     167           0 :                 bCloseable = true;
     168           0 :                 xCloseable->close(sal_True);
     169           0 :             }
     170             :         }
     171           0 :         catch (const uno::Exception &)
     172             :         {
     173             :             // vetoed
     174             :         }
     175           0 :         if (!bCloseable)
     176             :         {
     177             :             try {
     178             :                 // If close is not supported by this model - try to dispose it.
     179             :                 // But if the model disagree with a reset request for the modify state
     180             :                 // we shouldn't do so. Otherwhise some strange things can happen.
     181           0 :                 uno::Reference< lang::XComponent > xDisposable ( xModel, uno::UNO_QUERY_THROW );
     182           0 :                 xDisposable->dispose();
     183             :             }
     184           0 :             catch(const uno::Exception&)
     185             :             {
     186             :             }
     187           0 :         }
     188           1 :     }
     189           1 : }
     190             : 
     191             : void
     192           0 : VbaDocumentBase::Protect( const uno::Any &aPassword ) throw (uno::RuntimeException)
     193             : {
     194           0 :     OUString rPassword;
     195           0 :     uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
     196             :     SAL_INFO("vbahelper", "Workbook::Protect stub");
     197           0 :     if(  aPassword >>= rPassword )
     198           0 :         xProt->protect( rPassword );
     199             :     else
     200           0 :         xProt->protect( OUString() );
     201           0 : }
     202             : 
     203             : void
     204           0 : VbaDocumentBase::Unprotect( const uno::Any &aPassword ) throw (uno::RuntimeException)
     205             : {
     206           0 :     OUString rPassword;
     207           0 :     uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
     208           0 :     if( !xProt->isProtected() )
     209           0 :         throw uno::RuntimeException("File is already unprotected", uno::Reference< XInterface >() );
     210             :     else
     211             :     {
     212           0 :         if( aPassword >>= rPassword )
     213           0 :             xProt->unprotect( rPassword );
     214             :         else
     215           0 :             xProt->unprotect( OUString() );
     216           0 :     }
     217           0 : }
     218             : 
     219             : void
     220           0 : VbaDocumentBase::setSaved( sal_Bool bSave ) throw (uno::RuntimeException)
     221             : {
     222           0 :     uno::Reference< util::XModifiable > xModifiable( getModel(), uno::UNO_QUERY_THROW );
     223             :     try
     224             :     {
     225           0 :         xModifiable->setModified( !bSave );
     226             :     }
     227           0 :     catch (const lang::DisposedException&)
     228             :     {
     229             :         // impossibility to set the modified state on disposed document should not trigger an error
     230             :     }
     231           0 :     catch (const beans::PropertyVetoException&)
     232             :     {
     233           0 :         uno::Any aCaught( ::cppu::getCaughtException() );
     234             :         throw lang::WrappedTargetRuntimeException(
     235             :                 "Can't change modified state of model!",
     236             :                 uno::Reference< uno::XInterface >(),
     237           0 :                 aCaught );
     238           0 :     }
     239           0 : }
     240             : 
     241             : sal_Bool
     242           0 : VbaDocumentBase::getSaved() throw (uno::RuntimeException)
     243             : {
     244           0 :     uno::Reference< util::XModifiable > xModifiable( getModel(), uno::UNO_QUERY_THROW );
     245           0 :     return !xModifiable->isModified();
     246             : }
     247             : 
     248             : void
     249           0 : VbaDocumentBase::Save() throw (uno::RuntimeException)
     250             : {
     251           0 :     OUString url(".uno:Save");
     252           0 :     uno::Reference< frame::XModel > xModel = getModel();
     253           0 :     dispatchRequests(xModel,url);
     254           0 : }
     255             : 
     256             : void
     257           1 : VbaDocumentBase::Activate() throw (uno::RuntimeException)
     258             : {
     259           1 :     uno::Reference< frame::XFrame > xFrame( getModel()->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
     260           1 :     xFrame->activate();
     261           1 : }
     262             : 
     263             : uno::Any SAL_CALL
     264           0 : VbaDocumentBase::getVBProject() throw (uno::RuntimeException)
     265             : {
     266           0 :     if( !mxVBProject.is() ) try
     267             :     {
     268           0 :         uno::Reference< XApplicationBase > xApp( Application(), uno::UNO_QUERY_THROW );
     269           0 :         uno::Reference< XInterface > xVBE( xApp->getVBE(), uno::UNO_QUERY_THROW );
     270           0 :         uno::Sequence< uno::Any > aArgs( 2 );
     271           0 :         aArgs[ 0 ] <<= xVBE;          // the VBE
     272           0 :         aArgs[ 1 ] <<= getModel();    // document model for script container access
     273           0 :         uno::Reference< lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW );
     274           0 :         mxVBProject = xServiceManager->createInstanceWithArgumentsAndContext(
     275           0 :             "ooo.vba.vbide.VBProject", aArgs, mxContext );
     276             :     }
     277           0 :     catch(const uno::Exception&)
     278             :     {
     279             :     }
     280           0 :     return uno::Any( mxVBProject );
     281             : }
     282             : 
     283             : OUString
     284           0 : VbaDocumentBase::getServiceImplName()
     285             : {
     286           0 :     return OUString( "VbaDocumentBase" );
     287             : }
     288             : 
     289             : uno::Sequence< OUString >
     290           0 : VbaDocumentBase::getServiceNames()
     291             : {
     292           0 :     static uno::Sequence< OUString > aServiceNames;
     293           0 :     if ( aServiceNames.getLength() == 0 )
     294             :     {
     295           0 :         aServiceNames.realloc( 1 );
     296           0 :         aServiceNames[ 0 ] = "ooo.vba.VbaDocumentBase";
     297             :     }
     298           0 :     return aServiceNames;
     299         186 : }
     300             : 
     301             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10