merge the formfield patch from ooo-build
[ooovba.git] / scripting / source / basprov / basmethnode.cxx
blobdae5689373a887a6e91374fb8e08ab1368bdce7f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: basmethnode.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_scripting.hxx"
33 #include "basmethnode.hxx"
34 #include <com/sun/star/beans/PropertyAttribute.hpp>
35 #include <com/sun/star/frame/XDesktop.hpp>
36 #include <com/sun/star/frame/XDispatchHelper.hpp>
37 #include <com/sun/star/frame/XDispatchProvider.hpp>
38 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
39 #include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
40 #include <vos/mutex.hxx>
41 #include <vcl/svapp.hxx>
42 #include <basic/sbstar.hxx>
43 #include <basic/sbmeth.hxx>
44 #include <basic/sbmod.hxx>
46 #include <util/MiscUtils.hxx>
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::beans;
52 using namespace ::comphelper;
53 using namespace ::com::sun::star::script;
54 using namespace ::sf_misc;
56 #define BASPROV_PROPERTY_ID_URI 1
57 #define BASPROV_PROPERTY_ID_EDITABLE 2
59 #define BASPROV_PROPERTY_URI ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URI" ) )
60 #define BASPROV_PROPERTY_EDITABLE ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Editable" ) )
62 #define BASPROV_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT | PropertyAttribute::READONLY
65 //.........................................................................
66 namespace basprov
68 //.........................................................................
70 // =============================================================================
71 // BasicMethodNodeImpl
72 // =============================================================================
74 BasicMethodNodeImpl::BasicMethodNodeImpl( const Reference< XComponentContext >& rxContext,
75 const ::rtl::OUString& sScriptingContext, SbMethod* pMethod, bool isAppScript )
76 : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
77 ,OPropertyContainer( GetBroadcastHelper() )
78 ,m_xContext( rxContext )
79 ,m_sScriptingContext( sScriptingContext )
80 ,m_pMethod( pMethod )
81 ,m_bIsAppScript( isAppScript )
82 ,m_bEditable( sal_True )
84 if ( m_pMethod )
86 SbModule* pModule = m_pMethod->GetModule();
87 if ( pModule )
89 StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
90 if ( pBasic )
92 m_sURI = ::rtl::OUString::createFromAscii( "vnd.sun.star.script:" );
93 m_sURI += pBasic->GetName();
94 m_sURI += ::rtl::OUString::createFromAscii( "." );
95 m_sURI += pModule->GetName();
96 m_sURI += ::rtl::OUString::createFromAscii( "." );
97 m_sURI += m_pMethod->GetName();
98 m_sURI += ::rtl::OUString::createFromAscii( "?language=Basic&location=" );
99 if ( m_bIsAppScript )
100 m_sURI += ::rtl::OUString::createFromAscii( "application" );
101 else
102 m_sURI += ::rtl::OUString::createFromAscii( "document" );
107 registerProperty( BASPROV_PROPERTY_URI, BASPROV_PROPERTY_ID_URI, BASPROV_DEFAULT_ATTRIBS(), &m_sURI, ::getCppuType( &m_sURI ) );
108 registerProperty( BASPROV_PROPERTY_EDITABLE, BASPROV_PROPERTY_ID_EDITABLE, BASPROV_DEFAULT_ATTRIBS(), &m_bEditable, ::getCppuType( &m_bEditable ) );
111 // -----------------------------------------------------------------------------
113 BasicMethodNodeImpl::~BasicMethodNodeImpl()
117 // -----------------------------------------------------------------------------
118 // XInterface
119 // -----------------------------------------------------------------------------
121 IMPLEMENT_FORWARD_XINTERFACE2( BasicMethodNodeImpl, BasicMethodNodeImpl_BASE, OPropertyContainer )
123 // -----------------------------------------------------------------------------
124 // XTypeProvider
125 // -----------------------------------------------------------------------------
127 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicMethodNodeImpl, BasicMethodNodeImpl_BASE, OPropertyContainer )
129 // -----------------------------------------------------------------------------
130 // XBrowseNode
131 // -----------------------------------------------------------------------------
133 ::rtl::OUString BasicMethodNodeImpl::getName( ) throw (RuntimeException)
135 ::vos::OGuard aGuard( Application::GetSolarMutex() );
137 ::rtl::OUString sMethodName;
138 if ( m_pMethod )
139 sMethodName = m_pMethod->GetName();
141 return sMethodName;
144 // -----------------------------------------------------------------------------
146 Sequence< Reference< browse::XBrowseNode > > BasicMethodNodeImpl::getChildNodes( ) throw (RuntimeException)
148 ::vos::OGuard aGuard( Application::GetSolarMutex() );
150 return Sequence< Reference< browse::XBrowseNode > >();
153 // -----------------------------------------------------------------------------
155 sal_Bool BasicMethodNodeImpl::hasChildNodes( ) throw (RuntimeException)
157 ::vos::OGuard aGuard( Application::GetSolarMutex() );
159 return sal_False;
162 // -----------------------------------------------------------------------------
164 sal_Int16 BasicMethodNodeImpl::getType( ) throw (RuntimeException)
166 ::vos::OGuard aGuard( Application::GetSolarMutex() );
168 return browse::BrowseNodeTypes::SCRIPT;
171 // -----------------------------------------------------------------------------
172 // OPropertySetHelper
173 // -----------------------------------------------------------------------------
175 ::cppu::IPropertyArrayHelper& BasicMethodNodeImpl::getInfoHelper( )
177 return *getArrayHelper();
180 // -----------------------------------------------------------------------------
181 // OPropertyArrayUsageHelper
182 // -----------------------------------------------------------------------------
184 ::cppu::IPropertyArrayHelper* BasicMethodNodeImpl::createArrayHelper( ) const
186 Sequence< Property > aProps;
187 describeProperties( aProps );
188 return new ::cppu::OPropertyArrayHelper( aProps );
191 // -----------------------------------------------------------------------------
192 // XPropertySet
193 // -----------------------------------------------------------------------------
195 Reference< XPropertySetInfo > BasicMethodNodeImpl::getPropertySetInfo( ) throw (RuntimeException)
197 Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
198 return xInfo;
201 // -----------------------------------------------------------------------------
202 // XInvocation
203 // -----------------------------------------------------------------------------
205 Reference< XIntrospectionAccess > BasicMethodNodeImpl::getIntrospection( ) throw (RuntimeException)
207 return Reference< XIntrospectionAccess >();
210 // -----------------------------------------------------------------------------
212 Any BasicMethodNodeImpl::invoke( const ::rtl::OUString& aFunctionName, const Sequence< Any >& aParams,
213 Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
214 throw (IllegalArgumentException, script::CannotConvertException,
215 reflection::InvocationTargetException, RuntimeException)
217 (void)aParams;
218 (void)aOutParamIndex;
219 (void)aOutParam;
221 if ( aFunctionName == BASPROV_PROPERTY_EDITABLE )
223 ::rtl::OUString sDocURL, sLibName, sModName;
224 USHORT nLine1 = 0, nLine2;
226 if ( !m_bIsAppScript )
228 Reference< frame::XModel > xModel = MiscUtils::tDocUrlToModel( m_sScriptingContext );
230 if ( xModel.is() )
232 sDocURL = xModel->getURL();
233 if ( sDocURL.getLength() == 0 )
235 Sequence < PropertyValue > aProps = xModel->getArgs();
236 sal_Int32 nProps = aProps.getLength();
237 const PropertyValue* pProps = aProps.getConstArray();
238 for ( sal_Int32 i = 0; i < nProps; ++i )
240 // TODO: according to MBA the property 'Title' may change in future
241 if ( pProps[i].Name == ::rtl::OUString::createFromAscii( "Title" ) )
243 pProps[i].Value >>= sDocURL;
244 break;
251 if ( m_pMethod )
253 m_pMethod->GetLineRange( nLine1, nLine2 );
254 SbModule* pModule = m_pMethod->GetModule();
255 if ( pModule )
257 sModName = pModule->GetName();
258 StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
259 if ( pBasic )
260 sLibName = pBasic->GetName();
264 if ( m_xContext.is() )
266 Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
268 if ( xSMgr.is() )
270 Reference< frame::XDesktop > xDesktop( xSMgr->createInstanceWithContext(
271 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ), m_xContext ), UNO_QUERY );
273 if ( xDesktop.is() )
275 Reference < frame::XDispatchProvider > xProv( xDesktop->getCurrentFrame(), UNO_QUERY );
277 if ( xProv.is() )
279 Reference< frame::XDispatchHelper > xHelper( xSMgr->createInstanceWithContext(
280 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.DispatchHelper" ) ), m_xContext ), UNO_QUERY );
282 if ( xHelper.is() )
284 Sequence < PropertyValue > aArgs(7);
285 aArgs[0].Name = ::rtl::OUString::createFromAscii( "Document" );
286 aArgs[0].Value <<= sDocURL;
287 aArgs[1].Name = ::rtl::OUString::createFromAscii( "LibName" );
288 aArgs[1].Value <<= sLibName;
289 aArgs[2].Name = ::rtl::OUString::createFromAscii( "Name" );
290 aArgs[2].Value <<= sModName;
291 aArgs[3].Name = ::rtl::OUString::createFromAscii( "Type" );
292 aArgs[3].Value <<= ::rtl::OUString::createFromAscii( "Module" );
293 aArgs[4].Name = ::rtl::OUString::createFromAscii( "Line" );
294 aArgs[4].Value <<= static_cast< sal_uInt32 >( nLine1 );
295 xHelper->executeDispatch( xProv, ::rtl::OUString::createFromAscii( ".uno:BasicIDEAppear" ), ::rtl::OUString(), 0, aArgs );
302 else
304 throw IllegalArgumentException(
305 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicMethodNodeImpl::invoke: function name not supported!" ) ),
306 Reference< XInterface >(), 1 );
309 return Any();
312 // -----------------------------------------------------------------------------
314 void BasicMethodNodeImpl::setValue( const ::rtl::OUString& aPropertyName, const Any& aValue )
315 throw (UnknownPropertyException, script::CannotConvertException,
316 reflection::InvocationTargetException, RuntimeException)
318 (void)aPropertyName;
319 (void)aValue;
321 throw UnknownPropertyException(
322 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicMethodNodeImpl::setValue: property name is unknown!" ) ),
323 Reference< XInterface >() );
326 // -----------------------------------------------------------------------------
328 Any BasicMethodNodeImpl::getValue( const ::rtl::OUString& aPropertyName ) throw (UnknownPropertyException, RuntimeException)
330 (void)aPropertyName;
332 throw UnknownPropertyException(
333 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicMethodNodeImpl::getValue: property name is unknown!" ) ),
334 Reference< XInterface >() );
337 // -----------------------------------------------------------------------------
339 sal_Bool BasicMethodNodeImpl::hasMethod( const ::rtl::OUString& aName ) throw (RuntimeException)
341 sal_Bool bReturn = sal_False;
342 if ( aName == BASPROV_PROPERTY_EDITABLE )
343 bReturn = sal_True;
345 return bReturn;
348 // -----------------------------------------------------------------------------
350 sal_Bool BasicMethodNodeImpl::hasProperty( const ::rtl::OUString& aName ) throw (RuntimeException)
352 (void)aName;
354 return sal_False;
357 // -----------------------------------------------------------------------------
359 //.........................................................................
360 } // namespace basprov
361 //.........................................................................