xref: /trunk/main/scripting/source/basprov/basmethnode.cxx (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
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 
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 
106     BasicMethodNodeImpl::~BasicMethodNodeImpl()
107     {
108     }
109 
110     // -----------------------------------------------------------------------------
111     // XInterface
112     // -----------------------------------------------------------------------------
113 
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(  )
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 
139     Sequence< Reference< browse::XBrowseNode > > BasicMethodNodeImpl::getChildNodes(  )
140     {
141         ::vos::OGuard aGuard( Application::GetSolarMutex() );
142 
143         return Sequence< Reference< browse::XBrowseNode > >();
144     }
145 
146     // -----------------------------------------------------------------------------
147 
148     sal_Bool BasicMethodNodeImpl::hasChildNodes(  )
149     {
150         ::vos::OGuard aGuard( Application::GetSolarMutex() );
151 
152         return sal_False;
153     }
154 
155     // -----------------------------------------------------------------------------
156 
157     sal_Int16 BasicMethodNodeImpl::getType(  )
158     {
159         ::vos::OGuard aGuard( Application::GetSolarMutex() );
160 
161         return browse::BrowseNodeTypes::SCRIPT;
162     }
163 
164     // -----------------------------------------------------------------------------
165     // OPropertySetHelper
166     // -----------------------------------------------------------------------------
167 
168     ::cppu::IPropertyArrayHelper& BasicMethodNodeImpl::getInfoHelper(  )
169     {
170         return *getArrayHelper();
171     }
172 
173     // -----------------------------------------------------------------------------
174     // OPropertyArrayUsageHelper
175     // -----------------------------------------------------------------------------
176 
177     ::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 
188     Reference< XPropertySetInfo > BasicMethodNodeImpl::getPropertySetInfo(  )
189     {
190         Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
191         return xInfo;
192     }
193 
194     // -----------------------------------------------------------------------------
195     // XInvocation
196     // -----------------------------------------------------------------------------
197 
198     Reference< XIntrospectionAccess > BasicMethodNodeImpl::getIntrospection(  )
199     {
200         return Reference< XIntrospectionAccess >();
201     }
202 
203     // -----------------------------------------------------------------------------
204 
205     Any BasicMethodNodeImpl::invoke( const ::rtl::OUString& aFunctionName, const Sequence< Any >& aParams,
206         Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
207     {
208         (void)aParams;
209         (void)aOutParamIndex;
210         (void)aOutParam;
211 
212         if ( aFunctionName == BASPROV_PROPERTY_EDITABLE )
213         {
214             ::rtl::OUString sDocURL, sLibName, sModName;
215             sal_uInt16 nLine1 = 0, nLine2;
216 
217             if ( !m_bIsAppScript )
218             {
219                 Reference< frame::XModel > xModel = MiscUtils::tDocUrlToModel( m_sScriptingContext );
220 
221                 if ( xModel.is() )
222                 {
223                     sDocURL = xModel->getURL();
224                     if ( sDocURL.getLength() == 0 )
225                     {
226                         Sequence < PropertyValue > aProps = xModel->getArgs();
227                         sal_Int32 nProps = aProps.getLength();
228                         const PropertyValue* pProps = aProps.getConstArray();
229                         for ( sal_Int32 i = 0; i < nProps; ++i )
230                         {
231                             // TODO: according to MBA the property 'Title' may change in future
232                             if ( pProps[i].Name == ::rtl::OUString::createFromAscii( "Title" ) )
233                             {
234                                 pProps[i].Value >>= sDocURL;
235                                 break;
236                             }
237                         }
238                     }
239                 }
240             }
241 
242             if ( m_pMethod )
243             {
244                 m_pMethod->GetLineRange( nLine1, nLine2 );
245                 SbModule* pModule = m_pMethod->GetModule();
246                 if ( pModule )
247                 {
248                     sModName = pModule->GetName();
249                     StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
250                     if ( pBasic )
251                         sLibName = pBasic->GetName();
252                 }
253             }
254 
255             if ( m_xContext.is() )
256             {
257                 Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
258 
259                 if ( xSMgr.is() )
260                 {
261                     Reference< frame::XDesktop > xDesktop( xSMgr->createInstanceWithContext(
262                         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ), m_xContext ), UNO_QUERY );
263 
264                     if ( xDesktop.is() )
265                     {
266                         Reference < frame::XDispatchProvider > xProv( xDesktop->getCurrentFrame(), UNO_QUERY );
267 
268                         if ( xProv.is() )
269                         {
270                             Reference< frame::XDispatchHelper > xHelper( xSMgr->createInstanceWithContext(
271                                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.DispatchHelper" ) ), m_xContext ), UNO_QUERY );
272 
273                             if ( xHelper.is() )
274                             {
275                                 Sequence < PropertyValue > aArgs(7);
276                                 aArgs[0].Name = ::rtl::OUString::createFromAscii( "Document" );
277                                 aArgs[0].Value <<= sDocURL;
278                                 aArgs[1].Name = ::rtl::OUString::createFromAscii( "LibName" );
279                                 aArgs[1].Value <<= sLibName;
280                                 aArgs[2].Name = ::rtl::OUString::createFromAscii( "Name" );
281                                 aArgs[2].Value <<= sModName;
282                                 aArgs[3].Name = ::rtl::OUString::createFromAscii( "Type" );
283                                 aArgs[3].Value <<= ::rtl::OUString::createFromAscii( "Module" );
284                                 aArgs[4].Name = ::rtl::OUString::createFromAscii( "Line" );
285                                 aArgs[4].Value <<= static_cast< sal_uInt32 >( nLine1 );
286                                 xHelper->executeDispatch( xProv, ::rtl::OUString::createFromAscii( ".uno:BasicIDEAppear" ), ::rtl::OUString(), 0, aArgs );
287                             }
288                         }
289                     }
290                 }
291             }
292         }
293         else
294         {
295             throw IllegalArgumentException(
296                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicMethodNodeImpl::invoke: function name not supported!" ) ),
297                 Reference< XInterface >(), 1 );
298         }
299 
300         return Any();
301     }
302 
303     // -----------------------------------------------------------------------------
304 
305     void BasicMethodNodeImpl::setValue( const ::rtl::OUString& aPropertyName, const Any& aValue )
306     {
307         (void)aPropertyName;
308         (void)aValue;
309 
310         throw UnknownPropertyException(
311             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicMethodNodeImpl::setValue: property name is unknown!" ) ),
312             Reference< XInterface >() );
313     }
314 
315     // -----------------------------------------------------------------------------
316 
317     Any BasicMethodNodeImpl::getValue( const ::rtl::OUString& aPropertyName )
318     {
319         (void)aPropertyName;
320 
321         throw UnknownPropertyException(
322             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicMethodNodeImpl::getValue: property name is unknown!" ) ),
323             Reference< XInterface >() );
324     }
325 
326     // -----------------------------------------------------------------------------
327 
328     sal_Bool BasicMethodNodeImpl::hasMethod( const ::rtl::OUString& aName )
329     {
330         sal_Bool bReturn = sal_False;
331         if ( aName == BASPROV_PROPERTY_EDITABLE )
332             bReturn = sal_True;
333 
334         return bReturn;
335     }
336 
337     // -----------------------------------------------------------------------------
338 
339     sal_Bool BasicMethodNodeImpl::hasProperty( const ::rtl::OUString& aName )
340     {
341         (void)aName;
342 
343         return sal_False;
344     }
345 
346     // -----------------------------------------------------------------------------
347 
348 //.........................................................................
349 }   // namespace basprov
350 //.........................................................................
351