LibreOffice Module vbahelper (master) 1
vbaglobalbase.cxx
Go to the documentation of this file.
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 */
20#include <sal/macros.h>
21
25#include <com/sun/star/beans/PropertyValue.hpp>
26#include <com/sun/star/beans/XPropertySet.hpp>
27#include <com/sun/star/container/XNameContainer.hpp>
28#include <com/sun/star/lang/XComponent.hpp>
29#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
30#include <com/sun/star/uno/XComponentContext.hpp>
31
32using namespace com::sun::star;
33using namespace ooo::vba;
34
35// special key to return the Application
36const char sAppService[] = "ooo.vba.Application";
37constexpr OUStringLiteral gsApplication( u"Application" );
38
40const uno::Reference< ov::XHelperInterface >& xParent,
41const uno::Reference< uno::XComponentContext >& xContext, const OUString& sDocCtxName )
42 : Globals_BASE( xParent, xContext )
43 , msDocCtxName( sDocCtxName )
44{
45 // overwrite context with custom one ( that contains the application )
46 // 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 )
47 uno::Reference< uno::XInterface > aSrvMgr;
48 if ( xContext.is() && xContext->getServiceManager().is() )
49 {
50 aSrvMgr = xContext->getServiceManager()->createInstanceWithContext( "com.sun.star.comp.stoc.OServiceManagerWrapper" , xContext );
51 }
52
53 ::cppu::ContextEntry_Init aHandlerContextInfo[] =
54 {
56 ::cppu::ContextEntry_Init( sDocCtxName, uno::Any() ),
57 ::cppu::ContextEntry_Init( "/singletons/com.sun.star.lang.theServiceManager" , uno::Any( aSrvMgr ) )
58 };
59 // don't pass a delegate, this seems to introduce yet another cyclic dependency ( and
60 // some strange behavior
61 mxContext = ::cppu::createComponentContext(
62 aHandlerContextInfo,
63 SAL_N_ELEMENTS( aHandlerContextInfo ), nullptr );
64 if ( !aSrvMgr.is() )
65 return;
66
67 try
68 {
69 uno::Reference< beans::XPropertySet >(
70 aSrvMgr, uno::UNO_QUERY_THROW )->
71 setPropertyValue( "DefaultContext", uno::Any( mxContext ) );
72 }
73 catch ( uno::RuntimeException & )
74 {
75 throw;
76 }
77 catch ( uno::Exception & )
78 {
80 throw lang::WrappedTargetRuntimeException(
81 ("VbaGlobalsBase ctor, setting OServiceManagerWrapper"
82 " DefaultContext failed"),
83 uno::Reference< uno::XInterface >(), e);
84 }
85}
86
88{
89 try
90 {
91 uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY );
92 if ( xNameContainer.is() )
93 {
94 // release document reference (we don't want the component context trying to dispose that)
95 xNameContainer->removeByName( msDocCtxName );
96 // release application reference, as it is holding onto the context
97 xNameContainer->removeByName( gsApplication );
98 }
99 uno::Reference< lang::XComponent > xComponent( mxContext, uno::UNO_QUERY );
100 if (xComponent)
101 xComponent->dispose();
102 }
103 catch ( const uno::Exception& )
104 {
105 }
106}
107
108void
109VbaGlobalsBase::init( const uno::Sequence< beans::PropertyValue >& aInitArgs )
110{
111 for ( const auto& rInitArg : aInitArgs )
112 {
113 uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY_THROW );
114 if ( rInitArg.Name == gsApplication )
115 {
116 xNameContainer->replaceByName( gsApplication, rInitArg.Value );
117 uno::Reference< XHelperInterface > xParent( rInitArg.Value, uno::UNO_QUERY );
118 mxParent = xParent;
119 }
120 else
121 xNameContainer->replaceByName( rInitArg.Name, rInitArg.Value );
122 }
123}
124
125uno::Reference< uno::XInterface > SAL_CALL
126VbaGlobalsBase::createInstance( const OUString& aServiceSpecifier )
127{
128 uno::Reference< uno::XInterface > xReturn;
129 if ( aServiceSpecifier == sAppService )
130 {
131 // try to extract the Application from the context
132 uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY );
133 xNameContainer->getByName( gsApplication ) >>= xReturn;
134 }
135 else if ( hasServiceName( aServiceSpecifier ) )
136 xReturn = mxContext->getServiceManager()->createInstanceWithContext( aServiceSpecifier, mxContext );
137 return xReturn;
138}
139
140uno::Reference< uno::XInterface > SAL_CALL
141VbaGlobalsBase::createInstanceWithArguments( const OUString& aServiceSpecifier, const uno::Sequence< uno::Any >& Arguments )
142{
143
144 uno::Reference< uno::XInterface > xReturn;
145 if ( aServiceSpecifier == sAppService )
146 {
147 // try to extract the Application from the context
148 uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY );
149 xNameContainer->getByName( gsApplication ) >>= xReturn;
150 }
151 else if ( hasServiceName( aServiceSpecifier ) )
152 xReturn = mxContext->getServiceManager()->createInstanceWithArgumentsAndContext( aServiceSpecifier, Arguments, mxContext );
153 return xReturn;
154}
155
156uno::Sequence< OUString > SAL_CALL
158{
159 uno::Sequence< OUString > serviceNames { "ooo.vba.msforms.UserForm" };
160 return serviceNames;
161}
162
163bool
164VbaGlobalsBase::hasServiceName( const OUString& serviceName )
165{
166 uno::Sequence< OUString > sServiceNames( getAvailableServiceNames() );
167 return comphelper::findValue(sServiceNames, serviceName) != -1;
168}
169
170
171/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XComponentContext > mxContext
css::uno::WeakReference< ov::XHelperInterface > mxParent
VbaGlobalsBase(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const OUString &sDocCtxName)
const OUString msDocCtxName
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArguments(const OUString &ServiceSpecifier, const css::uno::Sequence< css::uno::Any > &Arguments) override
void init(const css::uno::Sequence< css::beans::PropertyValue > &aInitArgs)
bool hasServiceName(const OUString &serviceName)
virtual ~VbaGlobalsBase() override
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(const OUString &aServiceSpecifier) override
virtual css::uno::Sequence< OUString > SAL_CALL getAvailableServiceNames() override
#define SAL_N_ELEMENTS(arr)
void setPropertyValue(tPropertyValueMap &rOutMap, tPropertyValueMapKey key, const Value &value)
sal_Int32 findValue(const css::uno::Sequence< T1 > &_rList, const T2 &_rValue)
Any SAL_CALL getCaughtException()
const char sAppService[]
constexpr OUStringLiteral gsApplication(u"Application")