1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_scripting.hxx"
26 #include "basmethnode.hxx"
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <com/sun/star/frame/XDesktop.hpp>
29 #include <com/sun/star/frame/XDispatchHelper.hpp>
30 #include <com/sun/star/frame/XDispatchProvider.hpp>
31 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
32 #include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
33 #include <vos/mutex.hxx>
34 #include <vcl/svapp.hxx>
35 #include <basic/sbstar.hxx>
36 #include <basic/sbmeth.hxx>
37 #include <basic/sbmod.hxx>
38 
39 #include <util/MiscUtils.hxx>
40 
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::lang;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::beans;
45 using namespace ::comphelper;
46 using namespace ::com::sun::star::script;
47 using namespace ::sf_misc;
48 
49 #define BASPROV_PROPERTY_ID_URI         1
50 #define BASPROV_PROPERTY_ID_EDITABLE    2
51 
52 #define BASPROV_PROPERTY_URI            ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URI" ) )
53 #define BASPROV_PROPERTY_EDITABLE       ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Editable" ) )
54 
55 #define BASPROV_DEFAULT_ATTRIBS()       PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT | PropertyAttribute::READONLY
56 
57 
58 //.........................................................................
59 namespace basprov
60 {
61 //.........................................................................
62 
63     // =============================================================================
64     // BasicMethodNodeImpl
65     // =============================================================================
66 
BasicMethodNodeImpl(const Reference<XComponentContext> & rxContext,const::rtl::OUString & sScriptingContext,SbMethod * pMethod,bool isAppScript)67     BasicMethodNodeImpl::BasicMethodNodeImpl( const Reference< XComponentContext >& rxContext,
68         const ::rtl::OUString& sScriptingContext, SbMethod* pMethod, bool isAppScript )
69         : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
70         ,OPropertyContainer( GetBroadcastHelper() )
71         ,m_xContext( rxContext )
72         ,m_sScriptingContext( sScriptingContext )
73         ,m_pMethod( pMethod )
74         ,m_bIsAppScript( isAppScript )
75         ,m_bEditable( sal_True )
76     {
77         if ( m_pMethod )
78         {
79             SbModule* pModule = m_pMethod->GetModule();
80             if ( pModule )
81             {
82                 StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
83                 if ( pBasic )
84                 {
85                     m_sURI = ::rtl::OUString::createFromAscii( "vnd.sun.star.script:" );
86                     m_sURI += pBasic->GetName();
87                     m_sURI += ::rtl::OUString::createFromAscii( "." );
88                     m_sURI += pModule->GetName();
89                     m_sURI += ::rtl::OUString::createFromAscii( "." );
90                     m_sURI += m_pMethod->GetName();
91                     m_sURI += ::rtl::OUString::createFromAscii( "?language=Basic&location=" );
92                     if ( m_bIsAppScript )
93                         m_sURI += ::rtl::OUString::createFromAscii( "application" );
94                     else
95                         m_sURI += ::rtl::OUString::createFromAscii( "document" );
96                 }
97             }
98         }
99 
100         registerProperty( BASPROV_PROPERTY_URI,      BASPROV_PROPERTY_ID_URI,      BASPROV_DEFAULT_ATTRIBS(), &m_sURI,      ::getCppuType( &m_sURI ) );
101         registerProperty( BASPROV_PROPERTY_EDITABLE, BASPROV_PROPERTY_ID_EDITABLE, BASPROV_DEFAULT_ATTRIBS(), &m_bEditable, ::getCppuType( &m_bEditable ) );
102     }
103 
104     // -----------------------------------------------------------------------------
105 
~BasicMethodNodeImpl()106     BasicMethodNodeImpl::~BasicMethodNodeImpl()
107     {
108     }
109 
110     // -----------------------------------------------------------------------------
111     // XInterface
112     // -----------------------------------------------------------------------------
113 
IMPLEMENT_FORWARD_XINTERFACE2(BasicMethodNodeImpl,BasicMethodNodeImpl_BASE,OPropertyContainer)114     IMPLEMENT_FORWARD_XINTERFACE2( BasicMethodNodeImpl, BasicMethodNodeImpl_BASE, OPropertyContainer )
115 
116     // -----------------------------------------------------------------------------
117     // XTypeProvider
118     // -----------------------------------------------------------------------------
119 
120     IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicMethodNodeImpl, BasicMethodNodeImpl_BASE, OPropertyContainer )
121 
122     // -----------------------------------------------------------------------------
123     // XBrowseNode
124     // -----------------------------------------------------------------------------
125 
126     ::rtl::OUString BasicMethodNodeImpl::getName(  ) throw (RuntimeException)
127     {
128         ::vos::OGuard aGuard( Application::GetSolarMutex() );
129 
130         ::rtl::OUString sMethodName;
131         if ( m_pMethod )
132             sMethodName = m_pMethod->GetName();
133 
134         return sMethodName;
135     }
136 
137     // -----------------------------------------------------------------------------
138 
getChildNodes()139     Sequence< Reference< browse::XBrowseNode > > BasicMethodNodeImpl::getChildNodes(  ) throw (RuntimeException)
140     {
141         ::vos::OGuard aGuard( Application::GetSolarMutex() );
142 
143         return Sequence< Reference< browse::XBrowseNode > >();
144     }
145 
146     // -----------------------------------------------------------------------------
147 
hasChildNodes()148     sal_Bool BasicMethodNodeImpl::hasChildNodes(  ) throw (RuntimeException)
149     {
150         ::vos::OGuard aGuard( Application::GetSolarMutex() );
151 
152         return sal_False;
153     }
154 
155     // -----------------------------------------------------------------------------
156 
getType()157     sal_Int16 BasicMethodNodeImpl::getType(  ) throw (RuntimeException)
158     {
159         ::vos::OGuard aGuard( Application::GetSolarMutex() );
160 
161         return browse::BrowseNodeTypes::SCRIPT;
162     }
163 
164     // -----------------------------------------------------------------------------
165     // OPropertySetHelper
166     // -----------------------------------------------------------------------------
167 
getInfoHelper()168     ::cppu::IPropertyArrayHelper& BasicMethodNodeImpl::getInfoHelper(  )
169     {
170         return *getArrayHelper();
171     }
172 
173     // -----------------------------------------------------------------------------
174     // OPropertyArrayUsageHelper
175     // -----------------------------------------------------------------------------
176 
createArrayHelper() const177     ::cppu::IPropertyArrayHelper* BasicMethodNodeImpl::createArrayHelper(  ) const
178     {
179         Sequence< Property > aProps;
180         describeProperties( aProps );
181         return new ::cppu::OPropertyArrayHelper( aProps );
182     }
183 
184     // -----------------------------------------------------------------------------
185     // XPropertySet
186     // -----------------------------------------------------------------------------
187 
getPropertySetInfo()188     Reference< XPropertySetInfo > BasicMethodNodeImpl::getPropertySetInfo(  ) throw (RuntimeException)
189     {
190         Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
191         return xInfo;
192     }
193 
194     // -----------------------------------------------------------------------------
195     // XInvocation
196     // -----------------------------------------------------------------------------
197 
getIntrospection()198     Reference< XIntrospectionAccess > BasicMethodNodeImpl::getIntrospection(  ) throw (RuntimeException)
199     {
200         return Reference< XIntrospectionAccess >();
201     }
202 
203     // -----------------------------------------------------------------------------
204 
invoke(const::rtl::OUString & aFunctionName,const Sequence<Any> & aParams,Sequence<sal_Int16> & aOutParamIndex,Sequence<Any> & aOutParam)205     Any BasicMethodNodeImpl::invoke( const ::rtl::OUString& aFunctionName, const Sequence< Any >& aParams,
206         Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
207         throw (IllegalArgumentException, script::CannotConvertException,
208                reflection::InvocationTargetException, RuntimeException)
209     {
210 		(void)aParams;
211 		(void)aOutParamIndex;
212 		(void)aOutParam;
213 
214         if ( aFunctionName == BASPROV_PROPERTY_EDITABLE )
215         {
216             ::rtl::OUString sDocURL, sLibName, sModName;
217             sal_uInt16 nLine1 = 0, nLine2;
218 
219             if ( !m_bIsAppScript )
220             {
221                 Reference< frame::XModel > xModel = MiscUtils::tDocUrlToModel( m_sScriptingContext );
222 
223                 if ( xModel.is() )
224                 {
225                     sDocURL = xModel->getURL();
226                     if ( sDocURL.getLength() == 0 )
227                     {
228                         Sequence < PropertyValue > aProps = xModel->getArgs();
229                         sal_Int32 nProps = aProps.getLength();
230                         const PropertyValue* pProps = aProps.getConstArray();
231                         for ( sal_Int32 i = 0; i < nProps; ++i )
232                         {
233                             // TODO: according to MBA the property 'Title' may change in future
234                             if ( pProps[i].Name == ::rtl::OUString::createFromAscii( "Title" ) )
235                             {
236                                 pProps[i].Value >>= sDocURL;
237                                 break;
238                             }
239                         }
240                     }
241                 }
242             }
243 
244             if ( m_pMethod )
245             {
246                 m_pMethod->GetLineRange( nLine1, nLine2 );
247                 SbModule* pModule = m_pMethod->GetModule();
248                 if ( pModule )
249                 {
250                     sModName = pModule->GetName();
251                     StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
252                     if ( pBasic )
253                         sLibName = pBasic->GetName();
254                 }
255             }
256 
257             if ( m_xContext.is() )
258             {
259                 Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
260 
261                 if ( xSMgr.is() )
262                 {
263                     Reference< frame::XDesktop > xDesktop( xSMgr->createInstanceWithContext(
264                         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ), m_xContext ), UNO_QUERY );
265 
266                     if ( xDesktop.is() )
267                     {
268                         Reference < frame::XDispatchProvider > xProv( xDesktop->getCurrentFrame(), UNO_QUERY );
269 
270                         if ( xProv.is() )
271                         {
272                             Reference< frame::XDispatchHelper > xHelper( xSMgr->createInstanceWithContext(
273                                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.DispatchHelper" ) ), m_xContext ), UNO_QUERY );
274 
275                             if ( xHelper.is() )
276                             {
277                                 Sequence < PropertyValue > aArgs(7);
278                                 aArgs[0].Name = ::rtl::OUString::createFromAscii( "Document" );
279                                 aArgs[0].Value <<= sDocURL;
280                                 aArgs[1].Name = ::rtl::OUString::createFromAscii( "LibName" );
281                                 aArgs[1].Value <<= sLibName;
282                                 aArgs[2].Name = ::rtl::OUString::createFromAscii( "Name" );
283                                 aArgs[2].Value <<= sModName;
284                                 aArgs[3].Name = ::rtl::OUString::createFromAscii( "Type" );
285                                 aArgs[3].Value <<= ::rtl::OUString::createFromAscii( "Module" );
286                                 aArgs[4].Name = ::rtl::OUString::createFromAscii( "Line" );
287                                 aArgs[4].Value <<= static_cast< sal_uInt32 >( nLine1 );
288                                 xHelper->executeDispatch( xProv, ::rtl::OUString::createFromAscii( ".uno:BasicIDEAppear" ), ::rtl::OUString(), 0, aArgs );
289                             }
290                         }
291                     }
292                 }
293             }
294         }
295         else
296         {
297             throw IllegalArgumentException(
298                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicMethodNodeImpl::invoke: function name not supported!" ) ),
299                 Reference< XInterface >(), 1 );
300         }
301 
302         return Any();
303     }
304 
305     // -----------------------------------------------------------------------------
306 
setValue(const::rtl::OUString & aPropertyName,const Any & aValue)307     void BasicMethodNodeImpl::setValue( const ::rtl::OUString& aPropertyName, const Any& aValue )
308         throw (UnknownPropertyException, script::CannotConvertException,
309                reflection::InvocationTargetException, RuntimeException)
310     {
311 		(void)aPropertyName;
312 		(void)aValue;
313 
314         throw UnknownPropertyException(
315             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicMethodNodeImpl::setValue: property name is unknown!" ) ),
316             Reference< XInterface >() );
317     }
318 
319     // -----------------------------------------------------------------------------
320 
getValue(const::rtl::OUString & aPropertyName)321     Any BasicMethodNodeImpl::getValue( const ::rtl::OUString& aPropertyName ) throw (UnknownPropertyException, RuntimeException)
322     {
323 		(void)aPropertyName;
324 
325         throw UnknownPropertyException(
326             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicMethodNodeImpl::getValue: property name is unknown!" ) ),
327             Reference< XInterface >() );
328     }
329 
330     // -----------------------------------------------------------------------------
331 
hasMethod(const::rtl::OUString & aName)332     sal_Bool BasicMethodNodeImpl::hasMethod( const ::rtl::OUString& aName ) throw (RuntimeException)
333     {
334         sal_Bool bReturn = sal_False;
335         if ( aName == BASPROV_PROPERTY_EDITABLE )
336             bReturn = sal_True;
337 
338         return bReturn;
339     }
340 
341     // -----------------------------------------------------------------------------
342 
hasProperty(const::rtl::OUString & aName)343     sal_Bool BasicMethodNodeImpl::hasProperty( const ::rtl::OUString& aName ) throw (RuntimeException)
344     {
345 		(void)aName;
346 
347         return sal_False;
348     }
349 
350     // -----------------------------------------------------------------------------
351 
352 //.........................................................................
353 }	// namespace basprov
354 //.........................................................................
355