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 29 : 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 29 : , 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 29 : uno::Reference< uno::XInterface > aSrvMgr;
44 29 : if ( xContext.is() && xContext->getServiceManager().is() )
45 : {
46 29 : 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 58 : };
55 : // don't pass a delegate, this seems to introduce yet another cyclic dependency ( and
56 : // some strange behavior
57 87 : mxContext = ::cppu::createComponentContext(
58 : aHandlerContextInfo,
59 58 : ( sizeof ( aHandlerContextInfo ) / sizeof ( aHandlerContextInfo[0] ) ), NULL );
60 29 : if ( aSrvMgr.is() )
61 : {
62 : try
63 : {
64 : uno::Reference< beans::XPropertySet >(
65 58 : aSrvMgr, uno::UNO_QUERY_THROW )->
66 29 : 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 29 : }
81 29 : }
82 :
83 58 : VbaGlobalsBase::~VbaGlobalsBase()
84 : {
85 : try
86 : {
87 29 : uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY );
88 29 : if ( xNameContainer.is() )
89 : {
90 : // release document reference ( we don't wan't the component context trying to dispose that )
91 29 : xNameContainer->removeByName( msDocCtxName );
92 : // release application reference, as it is holding onto the context
93 29 : xNameContainer->removeByName( msApplication );
94 29 : }
95 : }
96 0 : catch ( const uno::Exception& )
97 : {
98 : }
99 29 : }
100 :
101 : void
102 29 : VbaGlobalsBase::init( const uno::Sequence< beans::PropertyValue >& aInitArgs )
103 : {
104 29 : sal_Int32 nLen = aInitArgs.getLength();
105 87 : for ( sal_Int32 nIndex = 0; nIndex < nLen; ++nIndex )
106 : {
107 58 : uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY_THROW );
108 58 : if ( aInitArgs[ nIndex ].Name.equals( msApplication ) )
109 : {
110 29 : xNameContainer->replaceByName( msApplication, aInitArgs[ nIndex ].Value );
111 29 : uno::Reference< XHelperInterface > xParent( aInitArgs[ nIndex ].Value, uno::UNO_QUERY );
112 29 : mxParent = xParent;
113 : }
114 : else
115 29 : xNameContainer->replaceByName( aInitArgs[ nIndex ].Name, aInitArgs[ nIndex ].Value );
116 58 : }
117 29 : }
118 :
119 : uno::Reference< uno::XInterface > SAL_CALL
120 0 : VbaGlobalsBase::createInstance( const OUString& aServiceSpecifier ) throw (uno::Exception, uno::RuntimeException, std::exception)
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 198 : VbaGlobalsBase::createInstanceWithArguments( const OUString& aServiceSpecifier, const uno::Sequence< uno::Any >& Arguments ) throw (uno::Exception, uno::RuntimeException, std::exception)
136 : {
137 :
138 198 : uno::Reference< uno::XInterface > xReturn;
139 198 : if ( aServiceSpecifier == sAppService )
140 : {
141 : // try to extract the Application from the context
142 27 : uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY );
143 27 : xNameContainer->getByName( msApplication ) >>= xReturn;
144 : }
145 171 : else if ( hasServiceName( aServiceSpecifier ) )
146 158 : xReturn = mxContext->getServiceManager()->createInstanceWithArgumentsAndContext( aServiceSpecifier, Arguments, mxContext );
147 198 : return xReturn;
148 : }
149 :
150 : uno::Sequence< OUString > SAL_CALL
151 3 : VbaGlobalsBase::getAvailableServiceNames( ) throw (uno::RuntimeException, std::exception)
152 : {
153 3 : uno::Sequence< OUString > serviceNames(1);
154 3 : serviceNames[0] = "ooo.vba.msforms.UserForm";
155 3 : return serviceNames;
156 : }
157 :
158 : bool
159 171 : VbaGlobalsBase::hasServiceName( const OUString& serviceName )
160 : {
161 171 : uno::Sequence< OUString > sServiceNames( getAvailableServiceNames() );
162 171 : sal_Int32 nLen = sServiceNames.getLength();
163 794 : for ( sal_Int32 index = 0; index < nLen; ++index )
164 : {
165 781 : if ( sServiceNames[ index ].equals( serviceName ) )
166 158 : return true;
167 : }
168 13 : return false;
169 : }
170 :
171 :
172 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|