1e6ed5fbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3e6ed5fbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4e6ed5fbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5e6ed5fbcSAndrew Rist  * distributed with this work for additional information
6e6ed5fbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7e6ed5fbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8e6ed5fbcSAndrew Rist  * "License"); you may not use this file except in compliance
9e6ed5fbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10e6ed5fbcSAndrew Rist  *
11e6ed5fbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12e6ed5fbcSAndrew Rist  *
13e6ed5fbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14e6ed5fbcSAndrew Rist  * software distributed under the License is distributed on an
15e6ed5fbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16e6ed5fbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17e6ed5fbcSAndrew Rist  * specific language governing permissions and limitations
18e6ed5fbcSAndrew Rist  * under the License.
19e6ed5fbcSAndrew Rist  *
20e6ed5fbcSAndrew Rist  *************************************************************/
21e6ed5fbcSAndrew Rist 
22e6ed5fbcSAndrew Rist 
23cdf0e10cSrcweir #include "vbahelper/vbaglobalbase.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <cppuhelper/component_context.hxx>
26cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
27cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir using namespace com::sun::star;
30cdf0e10cSrcweir using namespace ooo::vba;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir rtl::OUString sApplication( RTL_CONSTASCII_USTRINGPARAM("Application") );
33cdf0e10cSrcweir 
34cdf0e10cSrcweir // special key to return the Application
35cdf0e10cSrcweir rtl::OUString sAppService( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.Application") );
36cdf0e10cSrcweir 
VbaGlobalsBase(const uno::Reference<ov::XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const rtl::OUString & sDocCtxName)37cdf0e10cSrcweir VbaGlobalsBase::VbaGlobalsBase(
38cdf0e10cSrcweir const uno::Reference< ov::XHelperInterface >& xParent,
39cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& xContext, const rtl::OUString& sDocCtxName )
40cdf0e10cSrcweir :  Globals_BASE( xParent, xContext ), msDocCtxName( sDocCtxName )
41cdf0e10cSrcweir {
42cdf0e10cSrcweir     // overwrite context with custom one ( that contains the application )
43*cfd52e18Smseidel     // wrap the service manager as we don't want the disposing context to tear down the 'normal' ServiceManager ( or at least that's what the code appears like it wants to do )
44cdf0e10cSrcweir     uno::Any aSrvMgr;
45cdf0e10cSrcweir     if ( xContext.is() && xContext->getServiceManager().is() )
46cdf0e10cSrcweir     {
47cdf0e10cSrcweir         aSrvMgr = uno::makeAny( xContext->getServiceManager()->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.stoc.OServiceManagerWrapper") ), xContext ) );
48cdf0e10cSrcweir     }
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     ::cppu::ContextEntry_Init aHandlerContextInfo[] =
51cdf0e10cSrcweir     {
52cdf0e10cSrcweir         ::cppu::ContextEntry_Init( sApplication, uno::Any() ),
53cdf0e10cSrcweir         ::cppu::ContextEntry_Init( sDocCtxName, uno::Any() ),
54cdf0e10cSrcweir         ::cppu::ContextEntry_Init( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.lang.theServiceManager" ) ), aSrvMgr )
55cdf0e10cSrcweir     };
56cdf0e10cSrcweir     // don't pass a delegate, this seems to introduce yet another cyclic dependency ( and
57cdf0e10cSrcweir     // some strange behavior
58cdf0e10cSrcweir     mxContext = ::cppu::createComponentContext( aHandlerContextInfo, sizeof( aHandlerContextInfo ) / sizeof( aHandlerContextInfo[0] ), NULL );
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
~VbaGlobalsBase()61cdf0e10cSrcweir VbaGlobalsBase::~VbaGlobalsBase()
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     try
64cdf0e10cSrcweir     {
65cdf0e10cSrcweir         uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY );
66cdf0e10cSrcweir         if ( xNameContainer.is() )
67cdf0e10cSrcweir         {
68cdf0e10cSrcweir             // release document reference ( we don't wan't the component context trying to dispose that )
69cdf0e10cSrcweir             xNameContainer->removeByName( msDocCtxName );
70cdf0e10cSrcweir             // release application reference, as it is holding onto the context
71cdf0e10cSrcweir             xNameContainer->removeByName( sApplication );
72cdf0e10cSrcweir         }
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir     catch ( const uno::Exception& )
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir void
init(const uno::Sequence<beans::PropertyValue> & aInitArgs)80cdf0e10cSrcweir VbaGlobalsBase::init(  const uno::Sequence< beans::PropertyValue >& aInitArgs )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     sal_Int32 nLen = aInitArgs.getLength();
83cdf0e10cSrcweir     for ( sal_Int32 nIndex = 0; nIndex < nLen; ++nIndex )
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY_THROW );
86cdf0e10cSrcweir         if ( aInitArgs[ nIndex ].Name.equals( sApplication ) )
87cdf0e10cSrcweir         {
88cdf0e10cSrcweir             xNameContainer->replaceByName( sApplication, aInitArgs[ nIndex ].Value );
89cdf0e10cSrcweir             uno::Reference< XHelperInterface > xParent( aInitArgs[ nIndex ].Value, uno::UNO_QUERY );
90cdf0e10cSrcweir             mxParent = xParent;
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir         else
93cdf0e10cSrcweir             xNameContainer->replaceByName( aInitArgs[ nIndex ].Name, aInitArgs[ nIndex ].Value );
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL
createInstance(const::rtl::OUString & aServiceSpecifier)98cdf0e10cSrcweir VbaGlobalsBase::createInstance( const ::rtl::OUString& aServiceSpecifier ) throw (uno::Exception, uno::RuntimeException)
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     uno::Reference< uno::XInterface > xReturn;
101cdf0e10cSrcweir     if ( aServiceSpecifier.equals( sAppService ) )
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         // try to extract the Application from the context
104cdf0e10cSrcweir         uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY );
105cdf0e10cSrcweir         xNameContainer->getByName( sApplication ) >>= xReturn;
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir     else if ( hasServiceName( aServiceSpecifier ) )
108cdf0e10cSrcweir         xReturn = mxContext->getServiceManager()->createInstanceWithContext( aServiceSpecifier, mxContext );
109cdf0e10cSrcweir     return xReturn;
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL
createInstanceWithArguments(const::rtl::OUString & aServiceSpecifier,const uno::Sequence<uno::Any> & Arguments)113cdf0e10cSrcweir VbaGlobalsBase::createInstanceWithArguments( const ::rtl::OUString& aServiceSpecifier, const uno::Sequence< uno::Any >& Arguments ) throw (uno::Exception, uno::RuntimeException)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     uno::Reference< uno::XInterface > xReturn;
117cdf0e10cSrcweir     if ( aServiceSpecifier.equals( sAppService ) )
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         // try to extract the Application from the context
120cdf0e10cSrcweir         uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY );
121cdf0e10cSrcweir         xNameContainer->getByName( sApplication ) >>= xReturn;
122cdf0e10cSrcweir     }
123cdf0e10cSrcweir     else if ( hasServiceName( aServiceSpecifier ) )
124cdf0e10cSrcweir         xReturn = mxContext->getServiceManager()->createInstanceWithArgumentsAndContext( aServiceSpecifier, Arguments, mxContext );
125cdf0e10cSrcweir     return xReturn;
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL
getAvailableServiceNames()129cdf0e10cSrcweir VbaGlobalsBase::getAvailableServiceNames(  ) throw (uno::RuntimeException)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     static const rtl::OUString names[] = {
132cdf0e10cSrcweir     // common
133cdf0e10cSrcweir         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.msforms.UserForm" ) ),
134cdf0e10cSrcweir       };
135cdf0e10cSrcweir     static uno::Sequence< rtl::OUString > serviceNames( names, sizeof( names )/ sizeof( names[0] ) );
136cdf0e10cSrcweir     return serviceNames;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir bool
hasServiceName(const rtl::OUString & serviceName)140cdf0e10cSrcweir VbaGlobalsBase::hasServiceName( const rtl::OUString& serviceName )
141cdf0e10cSrcweir {
142cdf0e10cSrcweir     uno::Sequence< rtl::OUString > sServiceNames( getAvailableServiceNames() );
143cdf0e10cSrcweir     sal_Int32 nLen = sServiceNames.getLength();
144cdf0e10cSrcweir     for ( sal_Int32 index = 0; index < nLen; ++index )
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         if ( sServiceNames[ index ].equals( serviceName ) )
147cdf0e10cSrcweir             return true;
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir     return false;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 
153