Avoid potential negative array index access to cached text.
[LibreOffice.git] / scripting / source / basprov / basmethnode.cxx
blobf26e4c9a931b36449112b4b1cb8d5ce6dd3fdc79
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/script/browse/BrowseNodeTypes.hpp>
27 #include <comphelper/propertyvalue.hxx>
28 #include <utility>
29 #include <vcl/svapp.hxx>
30 #include <basic/sbstar.hxx>
31 #include <basic/sbmeth.hxx>
32 #include <basic/sbmod.hxx>
34 #include <util/MiscUtils.hxx>
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::beans;
40 using namespace ::comphelper;
41 using namespace ::com::sun::star::script;
42 using namespace ::sf_misc;
44 #define BASPROV_PROPERTY_ID_URI 1
45 #define BASPROV_PROPERTY_ID_EDITABLE 2
47 constexpr OUStringLiteral BASPROV_PROPERTY_URI = u"URI";
48 constexpr OUString BASPROV_PROPERTY_EDITABLE = u"Editable"_ustr;
50 #define BASPROV_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT | PropertyAttribute::READONLY
53 namespace basprov
57 // BasicMethodNodeImpl
60 BasicMethodNodeImpl::BasicMethodNodeImpl( const Reference< XComponentContext >& rxContext,
61 OUString sScriptingContext, SbMethod* pMethod, bool isAppScript )
62 : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
63 ,OPropertyContainer( GetBroadcastHelper() )
64 ,m_xContext( rxContext )
65 ,m_sScriptingContext(std::move( sScriptingContext ))
66 ,m_pMethod( pMethod )
67 ,m_bIsAppScript( isAppScript )
68 ,m_bEditable( true )
70 if ( m_pMethod )
72 SbModule* pModule = m_pMethod->GetModule();
73 if ( pModule )
75 StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
76 if ( pBasic )
78 m_sURI = "vnd.sun.star.script:";
79 m_sURI += pBasic->GetName();
80 m_sURI += ".";
81 m_sURI += pModule->GetName();
82 m_sURI += ".";
83 m_sURI += m_pMethod->GetName();
84 m_sURI += "?language=Basic&location=";
85 if ( m_bIsAppScript )
86 m_sURI += "application";
87 else
88 m_sURI += "document";
93 registerProperty( BASPROV_PROPERTY_URI, BASPROV_PROPERTY_ID_URI, BASPROV_DEFAULT_ATTRIBS(), &m_sURI, cppu::UnoType<decltype(m_sURI)>::get() );
94 registerProperty( BASPROV_PROPERTY_EDITABLE, BASPROV_PROPERTY_ID_EDITABLE, BASPROV_DEFAULT_ATTRIBS(), &m_bEditable, cppu::UnoType<decltype(m_bEditable)>::get() );
98 BasicMethodNodeImpl::~BasicMethodNodeImpl()
103 // XInterface
106 IMPLEMENT_FORWARD_XINTERFACE2( BasicMethodNodeImpl, BasicMethodNodeImpl_BASE, OPropertyContainer )
109 // XTypeProvider
112 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicMethodNodeImpl, BasicMethodNodeImpl_BASE, OPropertyContainer )
115 // XBrowseNode
118 OUString BasicMethodNodeImpl::getName( )
120 SolarMutexGuard aGuard;
122 OUString sMethodName;
123 if ( m_pMethod )
124 sMethodName = m_pMethod->GetName();
126 return sMethodName;
130 Sequence< Reference< browse::XBrowseNode > > BasicMethodNodeImpl::getChildNodes( )
132 return Sequence< Reference< browse::XBrowseNode > >();
136 sal_Bool BasicMethodNodeImpl::hasChildNodes( )
138 return false;
142 sal_Int16 BasicMethodNodeImpl::getType( )
144 return browse::BrowseNodeTypes::SCRIPT;
148 // OPropertySetHelper
151 ::cppu::IPropertyArrayHelper& BasicMethodNodeImpl::getInfoHelper( )
153 return *getArrayHelper();
157 // OPropertyArrayUsageHelper
160 ::cppu::IPropertyArrayHelper* BasicMethodNodeImpl::createArrayHelper( ) const
162 Sequence< Property > aProps;
163 describeProperties( aProps );
164 return new ::cppu::OPropertyArrayHelper( aProps );
168 // XPropertySet
171 Reference< XPropertySetInfo > BasicMethodNodeImpl::getPropertySetInfo( )
173 Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
174 return xInfo;
178 // XInvocation
181 Reference< XIntrospectionAccess > BasicMethodNodeImpl::getIntrospection( )
183 return Reference< XIntrospectionAccess >();
187 Any BasicMethodNodeImpl::invoke( const OUString& aFunctionName, const Sequence< Any >&,
188 Sequence< sal_Int16 >&, Sequence< Any >& )
190 if ( aFunctionName != BASPROV_PROPERTY_EDITABLE )
192 throw IllegalArgumentException(
193 "BasicMethodNodeImpl::invoke: function name not supported!",
194 Reference< XInterface >(), 1 );
197 OUString sDocURL, sLibName, sModName;
198 sal_uInt16 nLine1 = 0;
200 if ( !m_bIsAppScript )
202 Reference< frame::XModel > xModel = MiscUtils::tDocUrlToModel( m_sScriptingContext );
204 if ( xModel.is() )
206 sDocURL = xModel->getURL();
207 if ( sDocURL.isEmpty() )
209 const Sequence < PropertyValue > aProps = xModel->getArgs();
210 // TODO: according to MBA the property 'Title' may change in future
211 const PropertyValue* pProp = std::find_if(aProps.begin(), aProps.end(),
212 [](const PropertyValue& rProp) { return rProp.Name == "Title"; });
213 if (pProp != aProps.end())
214 pProp->Value >>= sDocURL;
219 if ( m_pMethod )
221 sal_uInt16 nLine2;
222 m_pMethod->GetLineRange( nLine1, nLine2 );
223 SbModule* pModule = m_pMethod->GetModule();
224 if ( pModule )
226 sModName = pModule->GetName();
227 StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
228 if ( pBasic )
229 sLibName = pBasic->GetName();
233 if ( m_xContext.is() )
235 Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( m_xContext );
237 Reference < frame::XDispatchProvider > xProv( xDesktop->getCurrentFrame(), UNO_QUERY );
239 if ( xProv.is() )
241 Reference< frame::XDispatchHelper > xHelper( frame::DispatchHelper::create( m_xContext ) );
243 Sequence < PropertyValue > aArgs{
244 comphelper::makePropertyValue("Document", sDocURL),
245 comphelper::makePropertyValue("LibName", sLibName),
246 comphelper::makePropertyValue("Name", sModName),
247 comphelper::makePropertyValue("Type", OUString("Module")),
248 comphelper::makePropertyValue("Line", static_cast< sal_uInt32 >( nLine1 ))
250 xHelper->executeDispatch( xProv, ".uno:BasicIDEAppear", OUString(), 0, aArgs );
255 return Any();
259 void BasicMethodNodeImpl::setValue( const OUString&, const Any& )
261 throw UnknownPropertyException(
262 "BasicMethodNodeImpl::setValue: property name is unknown!" );
266 Any BasicMethodNodeImpl::getValue( const OUString& )
268 throw UnknownPropertyException(
269 "BasicMethodNodeImpl::getValue: property name is unknown!" );
273 sal_Bool BasicMethodNodeImpl::hasMethod( const OUString& aName )
275 bool bReturn = false;
276 if ( aName == BASPROV_PROPERTY_EDITABLE )
277 bReturn = true;
279 return bReturn;
283 sal_Bool BasicMethodNodeImpl::hasProperty( const OUString& )
285 return false;
289 } // namespace basprov
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */