Branch libreoffice-5-0-4
[LibreOffice.git] / scripting / source / basprov / basmethnode.cxx
blob51ff569c1da77db9041b26e673cfd345d01c0088
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 OUString( "URI" )
47 #define BASPROV_PROPERTY_EDITABLE "Editable"
49 #define BASPROV_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT | PropertyAttribute::READONLY
53 namespace basprov
58 // BasicMethodNodeImpl
61 BasicMethodNodeImpl::BasicMethodNodeImpl( const Reference< XComponentContext >& rxContext,
62 const OUString& sScriptingContext, SbMethod* pMethod, bool isAppScript )
63 : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
64 ,OPropertyContainer( GetBroadcastHelper() )
65 ,m_xContext( rxContext )
66 ,m_sScriptingContext( sScriptingContext )
67 ,m_pMethod( pMethod )
68 ,m_bIsAppScript( isAppScript )
69 ,m_bEditable( true )
71 if ( m_pMethod )
73 SbModule* pModule = m_pMethod->GetModule();
74 if ( pModule )
76 StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
77 if ( pBasic )
79 m_sURI = "vnd.sun.star.script:";
80 m_sURI += pBasic->GetName();
81 m_sURI += ".";
82 m_sURI += pModule->GetName();
83 m_sURI += ".";
84 m_sURI += m_pMethod->GetName();
85 m_sURI += "?language=Basic&location=";
86 if ( m_bIsAppScript )
87 m_sURI += "application";
88 else
89 m_sURI += "document";
94 registerProperty( BASPROV_PROPERTY_URI, BASPROV_PROPERTY_ID_URI, BASPROV_DEFAULT_ATTRIBS(), &m_sURI, cppu::UnoType<decltype(m_sURI)>::get() );
95 registerProperty( BASPROV_PROPERTY_EDITABLE, BASPROV_PROPERTY_ID_EDITABLE, BASPROV_DEFAULT_ATTRIBS(), &m_bEditable, cppu::UnoType<decltype(m_bEditable)>::get() );
100 BasicMethodNodeImpl::~BasicMethodNodeImpl()
105 // XInterface
108 IMPLEMENT_FORWARD_XINTERFACE2( BasicMethodNodeImpl, BasicMethodNodeImpl_BASE, OPropertyContainer )
111 // XTypeProvider
114 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicMethodNodeImpl, BasicMethodNodeImpl_BASE, OPropertyContainer )
117 // XBrowseNode
120 OUString BasicMethodNodeImpl::getName( ) throw (RuntimeException, std::exception)
122 SolarMutexGuard aGuard;
124 OUString sMethodName;
125 if ( m_pMethod )
126 sMethodName = m_pMethod->GetName();
128 return sMethodName;
133 Sequence< Reference< browse::XBrowseNode > > BasicMethodNodeImpl::getChildNodes( ) throw (RuntimeException, std::exception)
135 SolarMutexGuard aGuard;
137 return Sequence< Reference< browse::XBrowseNode > >();
142 sal_Bool BasicMethodNodeImpl::hasChildNodes( ) throw (RuntimeException, std::exception)
144 SolarMutexGuard aGuard;
146 return sal_False;
151 sal_Int16 BasicMethodNodeImpl::getType( ) throw (RuntimeException, std::exception)
153 SolarMutexGuard aGuard;
155 return browse::BrowseNodeTypes::SCRIPT;
159 // OPropertySetHelper
162 ::cppu::IPropertyArrayHelper& BasicMethodNodeImpl::getInfoHelper( )
164 return *getArrayHelper();
168 // OPropertyArrayUsageHelper
171 ::cppu::IPropertyArrayHelper* BasicMethodNodeImpl::createArrayHelper( ) const
173 Sequence< Property > aProps;
174 describeProperties( aProps );
175 return new ::cppu::OPropertyArrayHelper( aProps );
179 // XPropertySet
182 Reference< XPropertySetInfo > BasicMethodNodeImpl::getPropertySetInfo( ) throw (RuntimeException, std::exception)
184 Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
185 return xInfo;
189 // XInvocation
192 Reference< XIntrospectionAccess > BasicMethodNodeImpl::getIntrospection( ) throw (RuntimeException, std::exception)
194 return Reference< XIntrospectionAccess >();
199 Any BasicMethodNodeImpl::invoke( const OUString& aFunctionName, const Sequence< Any >& aParams,
200 Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
201 throw (IllegalArgumentException, script::CannotConvertException,
202 reflection::InvocationTargetException, RuntimeException, std::exception)
204 (void)aParams;
205 (void)aOutParamIndex;
206 (void)aOutParam;
208 if ( aFunctionName == BASPROV_PROPERTY_EDITABLE )
210 OUString sDocURL, sLibName, sModName;
211 sal_uInt16 nLine1 = 0, nLine2;
213 if ( !m_bIsAppScript )
215 Reference< frame::XModel > xModel = MiscUtils::tDocUrlToModel( m_sScriptingContext );
217 if ( xModel.is() )
219 sDocURL = xModel->getURL();
220 if ( sDocURL.isEmpty() )
222 Sequence < PropertyValue > aProps = xModel->getArgs();
223 sal_Int32 nProps = aProps.getLength();
224 const PropertyValue* pProps = aProps.getConstArray();
225 for ( sal_Int32 i = 0; i < nProps; ++i )
227 // TODO: according to MBA the property 'Title' may change in future
228 if ( pProps[i].Name == "Title" )
230 pProps[i].Value >>= sDocURL;
231 break;
238 if ( m_pMethod )
240 m_pMethod->GetLineRange( nLine1, nLine2 );
241 SbModule* pModule = m_pMethod->GetModule();
242 if ( pModule )
244 sModName = pModule->GetName();
245 StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
246 if ( pBasic )
247 sLibName = pBasic->GetName();
251 if ( m_xContext.is() )
253 Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( m_xContext );
255 Reference < frame::XDispatchProvider > xProv( xDesktop->getCurrentFrame(), UNO_QUERY );
257 if ( xProv.is() )
259 Reference< frame::XDispatchHelper > xHelper( frame::DispatchHelper::create( m_xContext ) );
261 Sequence < PropertyValue > aArgs(7);
262 aArgs[0].Name = "Document";
263 aArgs[0].Value <<= sDocURL;
264 aArgs[1].Name = "LibName";
265 aArgs[1].Value <<= sLibName;
266 aArgs[2].Name = "Name";
267 aArgs[2].Value <<= sModName;
268 aArgs[3].Name = "Type";
269 aArgs[3].Value <<= OUString("Module");
270 aArgs[4].Name = "Line";
271 aArgs[4].Value <<= static_cast< sal_uInt32 >( nLine1 );
272 xHelper->executeDispatch( xProv, OUString(".uno:BasicIDEAppear"), OUString(), 0, aArgs );
276 else
278 throw IllegalArgumentException(
279 "BasicMethodNodeImpl::invoke: function name not supported!",
280 Reference< XInterface >(), 1 );
283 return Any();
288 void BasicMethodNodeImpl::setValue( const OUString& aPropertyName, const Any& aValue )
289 throw (UnknownPropertyException, script::CannotConvertException,
290 reflection::InvocationTargetException, RuntimeException, std::exception)
292 (void)aPropertyName;
293 (void)aValue;
295 throw UnknownPropertyException(
296 "BasicMethodNodeImpl::setValue: property name is unknown!" );
301 Any BasicMethodNodeImpl::getValue( const OUString& aPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
303 (void)aPropertyName;
305 throw UnknownPropertyException(
306 "BasicMethodNodeImpl::getValue: property name is unknown!" );
311 sal_Bool BasicMethodNodeImpl::hasMethod( const OUString& aName ) throw (RuntimeException, std::exception)
313 bool bReturn = false;
314 if ( aName == BASPROV_PROPERTY_EDITABLE )
315 bReturn = true;
317 return bReturn;
322 sal_Bool BasicMethodNodeImpl::hasProperty( const OUString& aName ) throw (RuntimeException, std::exception)
324 (void)aName;
326 return sal_False;
332 } // namespace basprov
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */