Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / scripting / source / basprov / basmethnode.cxx
blobe9da25e5cfe33577d65efc58b411336ba0f62362
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "basmethnode.hxx"
21 #include <com/sun/star/beans/PropertyAttribute.hpp>
22 #include <com/sun/star/frame/DispatchHelper.hpp>
23 #include <com/sun/star/frame/Desktop.hpp>
24 #include <com/sun/star/frame/XDispatchProvider.hpp>
25 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
26 #include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
27 #include <osl/mutex.hxx>
28 #include <vcl/svapp.hxx>
29 #include <basic/sbstar.hxx>
30 #include <basic/sbmeth.hxx>
31 #include <basic/sbmod.hxx>
33 #include <util/MiscUtils.hxx>
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::beans;
39 using namespace ::comphelper;
40 using namespace ::com::sun::star::script;
41 using namespace ::sf_misc;
43 #define BASPROV_PROPERTY_ID_URI 1
44 #define BASPROV_PROPERTY_ID_EDITABLE 2
46 #define BASPROV_PROPERTY_URI "URI"
47 #define BASPROV_PROPERTY_EDITABLE "Editable"
49 #define BASPROV_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT | PropertyAttribute::READONLY
52 namespace basprov
56 // BasicMethodNodeImpl
59 BasicMethodNodeImpl::BasicMethodNodeImpl( const Reference< XComponentContext >& rxContext,
60 const OUString& sScriptingContext, SbMethod* pMethod, bool isAppScript )
61 : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
62 ,OPropertyContainer( GetBroadcastHelper() )
63 ,m_xContext( rxContext )
64 ,m_sScriptingContext( sScriptingContext )
65 ,m_pMethod( pMethod )
66 ,m_bIsAppScript( isAppScript )
67 ,m_bEditable( true )
69 if ( m_pMethod )
71 SbModule* pModule = m_pMethod->GetModule();
72 if ( pModule )
74 StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
75 if ( pBasic )
77 m_sURI = "vnd.sun.star.script:";
78 m_sURI += pBasic->GetName();
79 m_sURI += ".";
80 m_sURI += pModule->GetName();
81 m_sURI += ".";
82 m_sURI += m_pMethod->GetName();
83 m_sURI += "?language=Basic&location=";
84 if ( m_bIsAppScript )
85 m_sURI += "application";
86 else
87 m_sURI += "document";
92 registerProperty( BASPROV_PROPERTY_URI, BASPROV_PROPERTY_ID_URI, BASPROV_DEFAULT_ATTRIBS(), &m_sURI, cppu::UnoType<decltype(m_sURI)>::get() );
93 registerProperty( BASPROV_PROPERTY_EDITABLE, BASPROV_PROPERTY_ID_EDITABLE, BASPROV_DEFAULT_ATTRIBS(), &m_bEditable, cppu::UnoType<decltype(m_bEditable)>::get() );
97 BasicMethodNodeImpl::~BasicMethodNodeImpl()
102 // XInterface
105 IMPLEMENT_FORWARD_XINTERFACE2( BasicMethodNodeImpl, BasicMethodNodeImpl_BASE, OPropertyContainer )
108 // XTypeProvider
111 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicMethodNodeImpl, BasicMethodNodeImpl_BASE, OPropertyContainer )
114 // XBrowseNode
117 OUString BasicMethodNodeImpl::getName( )
119 SolarMutexGuard aGuard;
121 OUString sMethodName;
122 if ( m_pMethod )
123 sMethodName = m_pMethod->GetName();
125 return sMethodName;
129 Sequence< Reference< browse::XBrowseNode > > BasicMethodNodeImpl::getChildNodes( )
131 return Sequence< Reference< browse::XBrowseNode > >();
135 sal_Bool BasicMethodNodeImpl::hasChildNodes( )
137 return false;
141 sal_Int16 BasicMethodNodeImpl::getType( )
143 return browse::BrowseNodeTypes::SCRIPT;
147 // OPropertySetHelper
150 ::cppu::IPropertyArrayHelper& BasicMethodNodeImpl::getInfoHelper( )
152 return *getArrayHelper();
156 // OPropertyArrayUsageHelper
159 ::cppu::IPropertyArrayHelper* BasicMethodNodeImpl::createArrayHelper( ) const
161 Sequence< Property > aProps;
162 describeProperties( aProps );
163 return new ::cppu::OPropertyArrayHelper( aProps );
167 // XPropertySet
170 Reference< XPropertySetInfo > BasicMethodNodeImpl::getPropertySetInfo( )
172 Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
173 return xInfo;
177 // XInvocation
180 Reference< XIntrospectionAccess > BasicMethodNodeImpl::getIntrospection( )
182 return Reference< XIntrospectionAccess >();
186 Any BasicMethodNodeImpl::invoke( const OUString& aFunctionName, const Sequence< Any >& aParams,
187 Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
189 (void)aParams;
190 (void)aOutParamIndex;
191 (void)aOutParam;
193 if ( aFunctionName == BASPROV_PROPERTY_EDITABLE )
195 OUString sDocURL, sLibName, sModName;
196 sal_uInt16 nLine1 = 0, nLine2;
198 if ( !m_bIsAppScript )
200 Reference< frame::XModel > xModel = MiscUtils::tDocUrlToModel( m_sScriptingContext );
202 if ( xModel.is() )
204 sDocURL = xModel->getURL();
205 if ( sDocURL.isEmpty() )
207 Sequence < PropertyValue > aProps = xModel->getArgs();
208 sal_Int32 nProps = aProps.getLength();
209 const PropertyValue* pProps = aProps.getConstArray();
210 for ( sal_Int32 i = 0; i < nProps; ++i )
212 // TODO: according to MBA the property 'Title' may change in future
213 if ( pProps[i].Name == "Title" )
215 pProps[i].Value >>= sDocURL;
216 break;
223 if ( m_pMethod )
225 m_pMethod->GetLineRange( nLine1, nLine2 );
226 SbModule* pModule = m_pMethod->GetModule();
227 if ( pModule )
229 sModName = pModule->GetName();
230 StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
231 if ( pBasic )
232 sLibName = pBasic->GetName();
236 if ( m_xContext.is() )
238 Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( m_xContext );
240 Reference < frame::XDispatchProvider > xProv( xDesktop->getCurrentFrame(), UNO_QUERY );
242 if ( xProv.is() )
244 Reference< frame::XDispatchHelper > xHelper( frame::DispatchHelper::create( m_xContext ) );
246 Sequence < PropertyValue > aArgs(7);
247 aArgs[0].Name = "Document";
248 aArgs[0].Value <<= sDocURL;
249 aArgs[1].Name = "LibName";
250 aArgs[1].Value <<= sLibName;
251 aArgs[2].Name = "Name";
252 aArgs[2].Value <<= sModName;
253 aArgs[3].Name = "Type";
254 aArgs[3].Value <<= OUString("Module");
255 aArgs[4].Name = "Line";
256 aArgs[4].Value <<= static_cast< sal_uInt32 >( nLine1 );
257 xHelper->executeDispatch( xProv, ".uno:BasicIDEAppear", OUString(), 0, aArgs );
261 else
263 throw IllegalArgumentException(
264 "BasicMethodNodeImpl::invoke: function name not supported!",
265 Reference< XInterface >(), 1 );
268 return Any();
272 void BasicMethodNodeImpl::setValue( const OUString& aPropertyName, const Any& aValue )
274 (void)aPropertyName;
275 (void)aValue;
277 throw UnknownPropertyException(
278 "BasicMethodNodeImpl::setValue: property name is unknown!" );
282 Any BasicMethodNodeImpl::getValue( const OUString& aPropertyName )
284 (void)aPropertyName;
286 throw UnknownPropertyException(
287 "BasicMethodNodeImpl::getValue: property name is unknown!" );
291 sal_Bool BasicMethodNodeImpl::hasMethod( const OUString& aName )
293 bool bReturn = false;
294 if ( aName == BASPROV_PROPERTY_EDITABLE )
295 bReturn = true;
297 return bReturn;
301 sal_Bool BasicMethodNodeImpl::hasProperty( const OUString& aName )
303 (void)aName;
305 return false;
309 } // namespace basprov
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */