LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/vbahelper/source/vbahelper - vbaglobalbase.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 44 65 67.7 %
Date: 2013-07-09 Functions: 8 11 72.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             : #include "vbahelper/vbaglobalbase.hxx"
      20             : #include <sal/macros.h>
      21             : 
      22             : #include <cppuhelper/component_context.hxx>
      23             : #include <cppuhelper/exc_hlp.hxx>
      24             : #include <comphelper/processfactory.hxx>
      25             : #include <com/sun/star/container/XNameContainer.hpp>
      26             : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
      27             : 
      28             : using namespace com::sun::star;
      29             : using namespace ooo::vba;
      30             : 
      31             : // special key to return the Application
      32             : const char sAppService[] = "ooo.vba.Application";
      33             : 
      34          27 : VbaGlobalsBase::VbaGlobalsBase(
      35             : const uno::Reference< ov::XHelperInterface >& xParent,
      36             : const uno::Reference< uno::XComponentContext >& xContext, const OUString& sDocCtxName )
      37             :     : Globals_BASE( xParent, xContext )
      38             :     , msDocCtxName( sDocCtxName )
      39          27 :     , msApplication( "Application" )
      40             : {
      41             :     // overwrite context with custom one ( that contains the application )
      42             :     // wrap the service manager as we don't want the disposing context to tear down the 'normal' ServiceManager ( or at least thats what the code appears like it wants to do )
      43          27 :     uno::Reference< uno::XInterface > aSrvMgr;
      44          27 :     if ( xContext.is() && xContext->getServiceManager().is() )
      45             :     {
      46          27 :         aSrvMgr = xContext->getServiceManager()->createInstanceWithContext( "com.sun.star.comp.stoc.OServiceManagerWrapper" , xContext );
      47             :     }
      48             : 
      49             :     ::cppu::ContextEntry_Init aHandlerContextInfo[] =
      50             :     {
      51             :         ::cppu::ContextEntry_Init( msApplication, uno::Any() ),
      52             :         ::cppu::ContextEntry_Init( sDocCtxName, uno::Any() ),
      53             :         ::cppu::ContextEntry_Init( "/singletons/com.sun.star.lang.theServiceManager" , uno::makeAny( aSrvMgr ) )
      54          54 :     };
      55             :     // don't pass a delegate, this seems to introduce yet another cyclic dependency ( and
      56             :     // some strange behavior
      57          81 :     mxContext = ::cppu::createComponentContext(
      58             :                         aHandlerContextInfo,
      59          54 :                         ( sizeof ( aHandlerContextInfo ) / sizeof ( aHandlerContextInfo[0] ) ), NULL );
      60          27 :     if ( aSrvMgr.is() )
      61             :     {
      62             :         try
      63             :         {
      64             :             uno::Reference< beans::XPropertySet >(
      65          54 :                 aSrvMgr, uno::UNO_QUERY_THROW )->
      66          27 :                 setPropertyValue( "DefaultContext", uno::makeAny( mxContext ) );
      67             :         }
      68           0 :         catch ( uno::RuntimeException & )
      69             :         {
      70           0 :             throw;
      71             :         }
      72           0 :         catch ( uno::Exception & )
      73             :         {
      74           0 :             uno::Any e(cppu::getCaughtException());
      75             :             throw lang::WrappedTargetRuntimeException(
      76             :                 ("VbaGlobalsBase ctor, setting OServiceManagerWrapper"
      77             :                  " DefaultContext failed"),
      78           0 :                 uno::Reference< uno::XInterface >(), e);
      79             :         }
      80          27 :     }
      81          27 : }
      82             : 
      83           0 : VbaGlobalsBase::~VbaGlobalsBase()
      84             : {
      85             :     try
      86             :     {
      87           0 :         uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY );
      88           0 :         if ( xNameContainer.is() )
      89             :         {
      90             :             // release document reference ( we don't wan't the component context trying to dispose that )
      91           0 :             xNameContainer->removeByName( msDocCtxName );
      92             :             // release application reference, as it is holding onto the context
      93           0 :             xNameContainer->removeByName( msApplication );
      94           0 :         }
      95             :     }
      96           0 :     catch ( const uno::Exception& )
      97             :     {
      98             :     }
      99           0 : }
     100             : 
     101             : void
     102          27 : VbaGlobalsBase::init(  const uno::Sequence< beans::PropertyValue >& aInitArgs )
     103             : {
     104          27 :     sal_Int32 nLen = aInitArgs.getLength();
     105          81 :     for ( sal_Int32 nIndex = 0; nIndex < nLen; ++nIndex )
     106             :     {
     107          54 :         uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY_THROW );
     108          54 :         if ( aInitArgs[ nIndex ].Name.equals( msApplication ) )
     109             :         {
     110          27 :             xNameContainer->replaceByName( msApplication, aInitArgs[ nIndex ].Value );
     111          27 :             uno::Reference< XHelperInterface > xParent( aInitArgs[ nIndex ].Value, uno::UNO_QUERY );
     112          27 :             mxParent = xParent;
     113             :         }
     114             :         else
     115          27 :             xNameContainer->replaceByName( aInitArgs[ nIndex ].Name, aInitArgs[ nIndex ].Value );
     116          54 :     }
     117          27 : }
     118             : 
     119             : uno::Reference< uno::XInterface > SAL_CALL
     120           0 : VbaGlobalsBase::createInstance( const OUString& aServiceSpecifier ) throw (uno::Exception, uno::RuntimeException)
     121             : {
     122           0 :     uno::Reference< uno::XInterface > xReturn;
     123           0 :     if ( aServiceSpecifier == sAppService )
     124             :     {
     125             :         // try to extract the Application from the context
     126           0 :         uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY );
     127           0 :         xNameContainer->getByName( msApplication ) >>= xReturn;
     128             :     }
     129           0 :     else if ( hasServiceName( aServiceSpecifier ) )
     130           0 :         xReturn = mxContext->getServiceManager()->createInstanceWithContext( aServiceSpecifier, mxContext );
     131           0 :     return xReturn;
     132             : }
     133             : 
     134             : uno::Reference< uno::XInterface > SAL_CALL
     135         188 : VbaGlobalsBase::createInstanceWithArguments( const OUString& aServiceSpecifier, const uno::Sequence< uno::Any >& Arguments ) throw (uno::Exception, uno::RuntimeException)
     136             : {
     137             : 
     138         188 :     uno::Reference< uno::XInterface > xReturn;
     139         188 :     if ( aServiceSpecifier == sAppService )
     140             :     {
     141             :         // try to extract the Application from the context
     142          25 :         uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY );
     143          25 :         xNameContainer->getByName( msApplication ) >>= xReturn;
     144             :     }
     145         163 :     else if ( hasServiceName( aServiceSpecifier ) )
     146         150 :         xReturn = mxContext->getServiceManager()->createInstanceWithArgumentsAndContext( aServiceSpecifier, Arguments, mxContext );
     147         188 :     return xReturn;
     148             : }
     149             : 
     150             : uno::Sequence< OUString > SAL_CALL
     151           3 : VbaGlobalsBase::getAvailableServiceNames(  ) throw (uno::RuntimeException)
     152             : {
     153             :     static const OUString names[] = {
     154             :     // common
     155             :         OUString("ooo.vba.msforms.UserForm"),
     156           6 :       };
     157           3 :     static uno::Sequence< OUString > serviceNames( names, sizeof( names )/ sizeof( names[0] ) );
     158           3 :     return serviceNames;
     159             : }
     160             : 
     161             : bool
     162         163 : VbaGlobalsBase::hasServiceName( const OUString& serviceName )
     163             : {
     164         163 :     uno::Sequence< OUString > sServiceNames( getAvailableServiceNames() );
     165         163 :     sal_Int32 nLen = sServiceNames.getLength();
     166         752 :     for ( sal_Int32 index = 0; index < nLen; ++index )
     167             :     {
     168         739 :         if ( sServiceNames[ index ].equals( serviceName ) )
     169         150 :             return true;
     170             :     }
     171          13 :     return false;
     172         186 : }
     173             : 
     174             : 
     175             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10