Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / scripting / source / basprov / basprov.cxx
bloba66367c7382f439e8f68b9263e9ce9314128b237
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 "basprov.hxx"
21 #include "basscript.hxx"
22 #include "baslibnode.hxx"
23 #include <com/sun/star/frame/XModel.hpp>
24 #include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
25 #include <com/sun/star/script/provider/ScriptFrameworkErrorException.hpp>
26 #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
27 #include <com/sun/star/document/XEmbeddedScripts.hpp>
28 #include <com/sun/star/uri/UriReferenceFactory.hpp>
30 #include <cppuhelper/implementationentry.hxx>
31 #include <cppuhelper/supportsservice.hxx>
32 #include <rtl/uri.hxx>
33 #include <osl/process.h>
34 #include <osl/file.hxx>
35 #include <osl/mutex.hxx>
36 #include <vcl/svapp.hxx>
37 #include <basic/sbx.hxx>
38 #include <basic/basmgr.hxx>
39 #include <basic/basicmanagerrepository.hxx>
40 #include <basic/sbstar.hxx>
41 #include <basic/sbmod.hxx>
42 #include <basic/sbmeth.hxx>
43 #include <sfx2/app.hxx>
44 #include <sfx2/objsh.hxx>
46 #include <com/sun/star/util/theMacroExpander.hpp>
47 #include <com/sun/star/script/XLibraryContainer2.hpp>
48 #include <com/sun/star/uri/XUriReference.hpp>
49 #include <com/sun/star/uri/XUriReferenceFactory.hpp>
50 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
52 #include <util/MiscUtils.hxx>
55 using namespace ::com::sun::star;
56 using namespace ::com::sun::star::lang;
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::script;
59 using namespace ::com::sun::star::document;
60 using namespace ::sf_misc;
63 namespace basprov
67 // component operations
70 static OUString getImplementationName_BasicProviderImpl()
72 return OUString( "com.sun.star.comp.scripting.ScriptProviderForBasic" );
76 static Sequence< OUString > getSupportedServiceNames_BasicProviderImpl()
78 static Sequence< OUString >* pNames = nullptr;
79 if ( !pNames )
81 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
82 if ( !pNames )
84 static Sequence< OUString > aNames(4);
85 aNames.getArray()[0] = "com.sun.star.script.provider.ScriptProviderForBasic";
86 aNames.getArray()[1] = "com.sun.star.script.provider.LanguageScriptProvider";
87 aNames.getArray()[2] = "com.sun.star.script.provider.ScriptProvider";
88 aNames.getArray()[3] = "com.sun.star.script.browse.BrowseNode";
89 pNames = &aNames;
92 return *pNames;
96 // BasicProviderImpl
99 BasicProviderImpl::BasicProviderImpl( const Reference< XComponentContext >& xContext )
100 :m_pAppBasicManager( nullptr )
101 ,m_pDocBasicManager( nullptr )
102 ,m_xLibContainerApp( nullptr )
103 ,m_xLibContainerDoc( nullptr )
104 ,m_xContext( xContext )
105 ,m_bIsAppScriptCtx( true )
106 ,m_bIsUserCtx(true)
111 BasicProviderImpl::~BasicProviderImpl()
116 bool BasicProviderImpl::isLibraryShared( const Reference< script::XLibraryContainer >& rxLibContainer, const OUString& rLibName )
118 bool bIsShared = false;
120 Reference< script::XLibraryContainer2 > xLibContainer( rxLibContainer, UNO_QUERY );
121 if ( xLibContainer.is() && xLibContainer->hasByName( rLibName ) && xLibContainer->isLibraryLink( rLibName ) )
123 OUString aFileURL;
124 if ( m_xContext.is() )
126 Reference< uri::XUriReferenceFactory > xUriFac( uri::UriReferenceFactory::create( m_xContext ) );
128 OUString aLinkURL( xLibContainer->getLibraryLinkURL( rLibName ) );
129 Reference< uri::XUriReference > xUriRef( xUriFac->parse( aLinkURL ), UNO_QUERY );
131 if ( xUriRef.is() )
133 OUString aScheme = xUriRef->getScheme();
134 if ( aScheme.equalsIgnoreAsciiCase("file") )
136 aFileURL = aLinkURL;
138 else if ( aScheme.equalsIgnoreAsciiCase("vnd.sun.star.pkg") )
140 OUString aAuthority = xUriRef->getAuthority();
141 if ( aAuthority.matchIgnoreAsciiCase( "vnd.sun.star.expand:" ) )
143 OUString aDecodedURL( aAuthority.copy( sizeof ( "vnd.sun.star.expand:" ) - 1 ) );
144 aDecodedURL = ::rtl::Uri::decode( aDecodedURL, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
145 Reference<util::XMacroExpander> xMacroExpander =
146 util::theMacroExpander::get(m_xContext);
147 aFileURL = xMacroExpander->expandMacros( aDecodedURL );
153 if ( !aFileURL.isEmpty() )
155 osl::DirectoryItem aFileItem;
156 osl::FileStatus aFileStatus( osl_FileStatus_Mask_FileURL );
157 OSL_VERIFY( osl::DirectoryItem::get( aFileURL, aFileItem ) == osl::FileBase::E_None );
158 OSL_VERIFY( aFileItem.getFileStatus( aFileStatus ) == osl::FileBase::E_None );
159 OUString aCanonicalFileURL( aFileStatus.getFileURL() );
161 OUString aSearchURL1( "share/basic" );
162 OUString aSearchURL2( "share/uno_packages" );
163 if( aCanonicalFileURL.indexOf( aSearchURL1 ) != -1 || aCanonicalFileURL.indexOf( aSearchURL2 ) != -1 )
164 bIsShared = true;
168 return bIsShared;
171 // XServiceInfo
172 OUString BasicProviderImpl::getImplementationName( )
174 return getImplementationName_BasicProviderImpl();
177 sal_Bool BasicProviderImpl::supportsService( const OUString& rServiceName )
179 return cppu::supportsService(this, rServiceName);
182 Sequence< OUString > BasicProviderImpl::getSupportedServiceNames( )
184 return getSupportedServiceNames_BasicProviderImpl();
188 // XInitialization
191 void BasicProviderImpl::initialize( const Sequence< Any >& aArguments )
193 // TODO
195 SolarMutexGuard aGuard;
197 if ( aArguments.getLength() != 1 )
199 throw IllegalArgumentException(
200 "BasicProviderImpl::initialize: incorrect argument count.",
201 *this,
206 Reference< frame::XModel > xModel;
208 m_xInvocationContext.set( aArguments[0], UNO_QUERY );
209 if ( m_xInvocationContext.is() )
211 xModel.set( m_xInvocationContext->getScriptContainer(), UNO_QUERY );
212 if ( !xModel.is() )
214 throw IllegalArgumentException(
215 "BasicProviderImpl::initialize: unable to determine the document model from the script invocation context.",
216 *this,
221 else
223 if ( !( aArguments[0] >>= m_sScriptingContext ) )
225 throw IllegalArgumentException(
226 "BasicProviderImpl::initialize: incorrect argument type " + aArguments[0].getValueTypeName(),
227 *this,
232 OUString sDoc = "vnd.sun.star.tdoc";
233 if ( m_sScriptingContext.startsWith( sDoc ) )
235 xModel = MiscUtils::tDocUrlToModel( m_sScriptingContext );
236 // TODO: use ScriptingContantsPool for SCRIPTING_DOC_REF
240 if ( xModel.is() )
242 Reference< XEmbeddedScripts > xDocumentScripts( xModel, UNO_QUERY );
243 if ( xDocumentScripts.is() )
245 m_pDocBasicManager = ::basic::BasicManagerRepository::getDocumentBasicManager( xModel );
246 m_xLibContainerDoc.set( xDocumentScripts->getBasicLibraries(), UNO_QUERY );
247 OSL_ENSURE( m_pDocBasicManager && m_xLibContainerDoc.is(),
248 "BasicProviderImpl::initialize: invalid BasicManager, or invalid script container!" );
250 m_bIsAppScriptCtx = false;
252 else
254 // Provider has been created with application context for user
255 // or share
256 if ( m_sScriptingContext != "user" )
258 m_bIsUserCtx = false;
260 else
263 throw RuntimeException(
264 "BasicProviderImpl::initialize: no scripting context!" );
269 // TODO
270 if ( !m_pAppBasicManager )
271 m_pAppBasicManager = SfxApplication::GetBasicManager();
273 if ( !m_xLibContainerApp.is() )
274 m_xLibContainerApp.set( SfxGetpApp()->GetBasicContainer(), UNO_QUERY );
278 // XScriptProvider
281 Reference < provider::XScript > BasicProviderImpl::getScript( const OUString& scriptURI )
283 // TODO
285 SolarMutexGuard aGuard;
287 Reference< provider::XScript > xScript;
288 Reference< uri::XUriReferenceFactory > xFac ( uri::UriReferenceFactory::create( m_xContext ) );
290 Reference< uri::XUriReference > uriRef(
291 xFac->parse( scriptURI ), UNO_QUERY );
293 Reference < uri::XVndSunStarScriptUrl > sfUri( uriRef, UNO_QUERY );
295 if ( !uriRef.is() || !sfUri.is() )
297 OUString errorMsg("BasicProviderImpl::getScript: failed to parse URI: ");
298 errorMsg = errorMsg.concat( scriptURI );
299 throw provider::ScriptFrameworkErrorException(
300 errorMsg, Reference< XInterface >(),
301 scriptURI, "Basic",
302 provider::ScriptFrameworkErrorType::MALFORMED_URL );
306 OUString aDescription = sfUri->getName();
307 OUString aLocation = sfUri->getParameter( "location" );
309 sal_Int32 nIndex = 0;
310 // In some strange circumstances the Library name can have an
311 // apparently illegal '.' in it ( in imported VBA )
313 BasicManager* pBasicMgr = nullptr;
314 if ( aLocation == "document" )
316 pBasicMgr = m_pDocBasicManager;
318 else if ( aLocation == "application" )
320 pBasicMgr = m_pAppBasicManager;
322 OUString sProjectName;
323 if ( pBasicMgr )
324 sProjectName = pBasicMgr->GetName();
326 OUString aLibrary;
327 if ( !sProjectName.isEmpty() && aDescription.match( sProjectName ) )
329 SAL_WARN("scripting", "LibraryName " << sProjectName << " is part of the url " << aDescription );
330 aLibrary = sProjectName;
331 nIndex = sProjectName.getLength() + 1;
333 else
334 aLibrary = aDescription.getToken( 0, '.', nIndex );
335 OUString aModule;
336 if ( nIndex != -1 )
337 aModule = aDescription.getToken( 0, '.', nIndex );
338 OUString aMethod;
339 if ( nIndex != -1 )
340 aMethod = aDescription.getToken( 0, '.', nIndex );
342 if ( !aLibrary.isEmpty() && !aModule.isEmpty() && !aMethod.isEmpty() && !aLocation.isEmpty() )
345 if ( pBasicMgr )
347 StarBASIC* pBasic = pBasicMgr->GetLib( aLibrary );
348 if ( !pBasic )
350 sal_uInt16 nId = pBasicMgr->GetLibId( aLibrary );
351 if ( nId != LIB_NOTFOUND )
353 pBasicMgr->LoadLib( nId );
354 pBasic = pBasicMgr->GetLib( aLibrary );
357 if ( pBasic )
359 SbModule* pModule = pBasic->FindModule( aModule );
360 if ( pModule )
362 SbMethod* pMethod = pModule->FindMethod( aMethod, SbxClassType::Method );
363 if ( pMethod && !pMethod->IsHidden() )
365 if ( m_pDocBasicManager == pBasicMgr )
366 xScript = new BasicScriptImpl( aDescription, pMethod, *m_pDocBasicManager, m_xInvocationContext );
367 else
368 xScript = new BasicScriptImpl( aDescription, pMethod );
375 if ( !xScript.is() )
377 throw provider::ScriptFrameworkErrorException(
378 "The following Basic script could not be found:\n"
379 "library: '" + aLibrary + "'\n"
380 "module: '" + aModule + "'\n"
381 "method: '" + aMethod + "'\n"
382 "location: '" + aLocation + "'\n",
383 Reference< XInterface >(),
384 scriptURI, "Basic",
385 provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT );
388 return xScript;
392 // XBrowseNode
395 OUString BasicProviderImpl::getName( )
397 return OUString("Basic");
401 Sequence< Reference< browse::XBrowseNode > > BasicProviderImpl::getChildNodes( )
403 SolarMutexGuard aGuard;
405 Reference< script::XLibraryContainer > xLibContainer;
406 BasicManager* pBasicManager = nullptr;
408 if ( m_bIsAppScriptCtx )
410 xLibContainer = m_xLibContainerApp;
411 pBasicManager = m_pAppBasicManager;
413 else
415 xLibContainer = m_xLibContainerDoc;
416 pBasicManager = m_pDocBasicManager;
419 Sequence< Reference< browse::XBrowseNode > > aChildNodes;
421 if ( pBasicManager && xLibContainer.is() )
423 Sequence< OUString > aLibNames = xLibContainer->getElementNames();
424 sal_Int32 nLibCount = aLibNames.getLength();
425 const OUString* pLibNames = aLibNames.getConstArray();
426 aChildNodes.realloc( nLibCount );
427 Reference< browse::XBrowseNode >* pChildNodes = aChildNodes.getArray();
428 sal_Int32 childrenFound = 0;
430 for ( sal_Int32 i = 0 ; i < nLibCount ; ++i )
432 bool bCreate = false;
433 if ( m_bIsAppScriptCtx )
435 bool bShared = isLibraryShared( xLibContainer, pLibNames[i] );
436 if ( ( m_bIsUserCtx && !bShared ) || ( !m_bIsUserCtx && bShared ) )
437 bCreate = true;
439 else
441 bCreate = true;
443 if ( bCreate )
445 pChildNodes[childrenFound++] = static_cast< browse::XBrowseNode* >( new BasicLibraryNodeImpl(
446 m_xContext, m_sScriptingContext, pBasicManager, xLibContainer, pLibNames[i], m_bIsAppScriptCtx ) );
450 if ( childrenFound != nLibCount )
451 aChildNodes.realloc( childrenFound );
454 return aChildNodes;
458 sal_Bool BasicProviderImpl::hasChildNodes( )
460 SolarMutexGuard aGuard;
462 bool bReturn = false;
463 Reference< script::XLibraryContainer > xLibContainer;
464 if ( m_bIsAppScriptCtx )
466 xLibContainer = m_xLibContainerApp;
468 else
470 xLibContainer = m_xLibContainerDoc;
472 if ( xLibContainer.is() )
473 bReturn = xLibContainer->hasElements();
475 return bReturn;
479 sal_Int16 BasicProviderImpl::getType( )
481 return browse::BrowseNodeTypes::CONTAINER;
485 // component operations
488 static Reference< XInterface > SAL_CALL create_BasicProviderImpl(
489 Reference< XComponentContext > const & xContext )
491 return static_cast< lang::XTypeProvider * >( new BasicProviderImpl( xContext ) );
495 static struct ::cppu::ImplementationEntry s_component_entries [] =
498 create_BasicProviderImpl, getImplementationName_BasicProviderImpl,
499 getSupportedServiceNames_BasicProviderImpl, ::cppu::createSingleComponentFactory,
500 nullptr, 0
502 { nullptr, nullptr, nullptr, nullptr, nullptr, 0 }
506 } // namespace basprov
509 // component exports
512 extern "C"
514 SAL_DLLPUBLIC_EXPORT void * SAL_CALL basprov_component_getFactory(
515 const sal_Char * pImplName, void * pServiceManager,
516 void * pRegistryKey )
518 return ::cppu::component_getFactoryHelper(
519 pImplName, pServiceManager, pRegistryKey, ::basprov::s_component_entries );
523 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */