1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir #include "vbahelper/vbaglobalbase.hxx" 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir #include <cppuhelper/component_context.hxx> 30*cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 31*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir using namespace com::sun::star; 34*cdf0e10cSrcweir using namespace ooo::vba; 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir rtl::OUString sApplication( RTL_CONSTASCII_USTRINGPARAM("Application") ); 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir // special key to return the Application 39*cdf0e10cSrcweir rtl::OUString sAppService( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.Application") ); 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir VbaGlobalsBase::VbaGlobalsBase( 42*cdf0e10cSrcweir const uno::Reference< ov::XHelperInterface >& xParent, 43*cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& xContext, const rtl::OUString& sDocCtxName ) 44*cdf0e10cSrcweir : Globals_BASE( xParent, xContext ), msDocCtxName( sDocCtxName ) 45*cdf0e10cSrcweir { 46*cdf0e10cSrcweir // overwrite context with custom one ( that contains the application ) 47*cdf0e10cSrcweir // 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 ) 48*cdf0e10cSrcweir uno::Any aSrvMgr; 49*cdf0e10cSrcweir if ( xContext.is() && xContext->getServiceManager().is() ) 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir aSrvMgr = uno::makeAny( xContext->getServiceManager()->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.stoc.OServiceManagerWrapper") ), xContext ) ); 52*cdf0e10cSrcweir } 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir ::cppu::ContextEntry_Init aHandlerContextInfo[] = 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir ::cppu::ContextEntry_Init( sApplication, uno::Any() ), 57*cdf0e10cSrcweir ::cppu::ContextEntry_Init( sDocCtxName, uno::Any() ), 58*cdf0e10cSrcweir ::cppu::ContextEntry_Init( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.lang.theServiceManager" ) ), aSrvMgr ) 59*cdf0e10cSrcweir }; 60*cdf0e10cSrcweir // don't pass a delegate, this seems to introduce yet another cyclic dependency ( and 61*cdf0e10cSrcweir // some strange behavior 62*cdf0e10cSrcweir mxContext = ::cppu::createComponentContext( aHandlerContextInfo, sizeof( aHandlerContextInfo ) / sizeof( aHandlerContextInfo[0] ), NULL ); 63*cdf0e10cSrcweir } 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir VbaGlobalsBase::~VbaGlobalsBase() 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir try 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY ); 70*cdf0e10cSrcweir if ( xNameContainer.is() ) 71*cdf0e10cSrcweir { 72*cdf0e10cSrcweir // release document reference ( we don't wan't the component context trying to dispose that ) 73*cdf0e10cSrcweir xNameContainer->removeByName( msDocCtxName ); 74*cdf0e10cSrcweir // release application reference, as it is holding onto the context 75*cdf0e10cSrcweir xNameContainer->removeByName( sApplication ); 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir } 78*cdf0e10cSrcweir catch ( const uno::Exception& ) 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir void 84*cdf0e10cSrcweir VbaGlobalsBase::init( const uno::Sequence< beans::PropertyValue >& aInitArgs ) 85*cdf0e10cSrcweir { 86*cdf0e10cSrcweir sal_Int32 nLen = aInitArgs.getLength(); 87*cdf0e10cSrcweir for ( sal_Int32 nIndex = 0; nIndex < nLen; ++nIndex ) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY_THROW ); 90*cdf0e10cSrcweir if ( aInitArgs[ nIndex ].Name.equals( sApplication ) ) 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir xNameContainer->replaceByName( sApplication, aInitArgs[ nIndex ].Value ); 93*cdf0e10cSrcweir uno::Reference< XHelperInterface > xParent( aInitArgs[ nIndex ].Value, uno::UNO_QUERY ); 94*cdf0e10cSrcweir mxParent = xParent; 95*cdf0e10cSrcweir } 96*cdf0e10cSrcweir else 97*cdf0e10cSrcweir xNameContainer->replaceByName( aInitArgs[ nIndex ].Name, aInitArgs[ nIndex ].Value ); 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL 102*cdf0e10cSrcweir VbaGlobalsBase::createInstance( const ::rtl::OUString& aServiceSpecifier ) throw (uno::Exception, uno::RuntimeException) 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir uno::Reference< uno::XInterface > xReturn; 105*cdf0e10cSrcweir if ( aServiceSpecifier.equals( sAppService ) ) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir // try to extract the Application from the context 108*cdf0e10cSrcweir uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY ); 109*cdf0e10cSrcweir xNameContainer->getByName( sApplication ) >>= xReturn; 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir else if ( hasServiceName( aServiceSpecifier ) ) 112*cdf0e10cSrcweir xReturn = mxContext->getServiceManager()->createInstanceWithContext( aServiceSpecifier, mxContext ); 113*cdf0e10cSrcweir return xReturn; 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL 117*cdf0e10cSrcweir VbaGlobalsBase::createInstanceWithArguments( const ::rtl::OUString& aServiceSpecifier, const uno::Sequence< uno::Any >& Arguments ) throw (uno::Exception, uno::RuntimeException) 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir uno::Reference< uno::XInterface > xReturn; 121*cdf0e10cSrcweir if ( aServiceSpecifier.equals( sAppService ) ) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir // try to extract the Application from the context 124*cdf0e10cSrcweir uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY ); 125*cdf0e10cSrcweir xNameContainer->getByName( sApplication ) >>= xReturn; 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir else if ( hasServiceName( aServiceSpecifier ) ) 128*cdf0e10cSrcweir xReturn = mxContext->getServiceManager()->createInstanceWithArgumentsAndContext( aServiceSpecifier, Arguments, mxContext ); 129*cdf0e10cSrcweir return xReturn; 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL 133*cdf0e10cSrcweir VbaGlobalsBase::getAvailableServiceNames( ) throw (uno::RuntimeException) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir static const rtl::OUString names[] = { 136*cdf0e10cSrcweir // common 137*cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.msforms.UserForm" ) ), 138*cdf0e10cSrcweir }; 139*cdf0e10cSrcweir static uno::Sequence< rtl::OUString > serviceNames( names, sizeof( names )/ sizeof( names[0] ) ); 140*cdf0e10cSrcweir return serviceNames; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir bool 144*cdf0e10cSrcweir VbaGlobalsBase::hasServiceName( const rtl::OUString& serviceName ) 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir uno::Sequence< rtl::OUString > sServiceNames( getAvailableServiceNames() ); 147*cdf0e10cSrcweir sal_Int32 nLen = sServiceNames.getLength(); 148*cdf0e10cSrcweir for ( sal_Int32 index = 0; index < nLen; ++index ) 149*cdf0e10cSrcweir { 150*cdf0e10cSrcweir if ( sServiceNames[ index ].equals( serviceName ) ) 151*cdf0e10cSrcweir return true; 152*cdf0e10cSrcweir } 153*cdf0e10cSrcweir return false; 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir 157