1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/ScriptFrameworkErrorType.hpp>
26 #include <com/sun/star/document/XEmbeddedScripts.hpp>
27 #include <com/sun/star/uri/UriReferenceFactory.hpp>
29 #include <cppuhelper/implementationentry.hxx>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <rtl/uri.hxx>
32 #include <osl/process.h>
33 #include <osl/file.hxx>
34 #include <osl/mutex.hxx>
35 #include <vcl/svapp.hxx>
36 #include <basic/sbx.hxx>
37 #include <basic/basmgr.hxx>
38 #include <basic/basicmanagerrepository.hxx>
39 #include <basic/sbstar.hxx>
40 #include <basic/sbmod.hxx>
41 #include <basic/sbmeth.hxx>
42 #include <sfx2/app.hxx>
43 #include <sfx2/objsh.hxx>
45 #include <com/sun/star/util/theMacroExpander.hpp>
46 #include <com/sun/star/script/XLibraryContainer2.hpp>
47 #include <com/sun/star/uri/XUriReference.hpp>
48 #include <com/sun/star/uri/XUriReferenceFactory.hpp>
49 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
51 #include <util/MiscUtils.hxx>
54 using namespace ::com::sun::star
;
55 using namespace ::com::sun::star::lang
;
56 using namespace ::com::sun::star::uno
;
57 using namespace ::com::sun::star::script
;
58 using namespace ::com::sun::star::document
;
59 using namespace ::sf_misc
;
67 // component operations
70 static OUString
getImplementationName_BasicProviderImpl()
72 return OUString( "com.sun.star.comp.scripting.ScriptProviderForBasic" );
77 static Sequence
< OUString
> getSupportedServiceNames_BasicProviderImpl()
79 static Sequence
< OUString
>* pNames
= 0;
82 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
85 static Sequence
< OUString
> aNames(4);
86 aNames
.getArray()[0] = "com.sun.star.script.provider.ScriptProviderForBasic";
87 aNames
.getArray()[1] = "com.sun.star.script.provider.LanguageScriptProvider";
88 aNames
.getArray()[2] = "com.sun.star.script.provider.ScriptProvider";
89 aNames
.getArray()[3] = "com.sun.star.script.browse.BrowseNode";
100 BasicProviderImpl::BasicProviderImpl( const Reference
< XComponentContext
>& xContext
)
101 :m_pAppBasicManager( 0 )
102 ,m_pDocBasicManager( 0 )
103 ,m_xLibContainerApp( 0 )
104 ,m_xLibContainerDoc( 0 )
105 ,m_xContext( xContext
)
106 ,m_bIsAppScriptCtx( true )
113 BasicProviderImpl::~BasicProviderImpl()
119 bool BasicProviderImpl::isLibraryShared( const Reference
< script::XLibraryContainer
>& rxLibContainer
, const OUString
& rLibName
)
121 bool bIsShared
= false;
123 Reference
< script::XLibraryContainer2
> xLibContainer( rxLibContainer
, UNO_QUERY
);
124 if ( xLibContainer
.is() && xLibContainer
->hasByName( rLibName
) && xLibContainer
->isLibraryLink( rLibName
) )
127 if ( m_xContext
.is() )
129 Reference
< uri::XUriReferenceFactory
> xUriFac( uri::UriReferenceFactory::create( m_xContext
) );
131 OUString
aLinkURL( xLibContainer
->getLibraryLinkURL( rLibName
) );
132 Reference
< uri::XUriReference
> xUriRef( xUriFac
->parse( aLinkURL
), UNO_QUERY
);
136 OUString aScheme
= xUriRef
->getScheme();
137 if ( aScheme
.equalsIgnoreAsciiCase("file") )
141 else if ( aScheme
.equalsIgnoreAsciiCase("vnd.sun.star.pkg") )
143 OUString aAuthority
= xUriRef
->getAuthority();
144 if ( aAuthority
.matchIgnoreAsciiCase( "vnd.sun.star.expand:" ) )
146 OUString
aDecodedURL( aAuthority
.copy( sizeof ( "vnd.sun.star.expand:" ) - 1 ) );
147 aDecodedURL
= ::rtl::Uri::decode( aDecodedURL
, rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
);
148 Reference
<util::XMacroExpander
> xMacroExpander
=
149 util::theMacroExpander::get(m_xContext
);
150 aFileURL
= xMacroExpander
->expandMacros( aDecodedURL
);
156 if ( !aFileURL
.isEmpty() )
158 osl::DirectoryItem aFileItem
;
159 osl::FileStatus
aFileStatus( osl_FileStatus_Mask_FileURL
);
160 OSL_VERIFY( osl::DirectoryItem::get( aFileURL
, aFileItem
) == osl::FileBase::E_None
);
161 OSL_VERIFY( aFileItem
.getFileStatus( aFileStatus
) == osl::FileBase::E_None
);
162 OUString
aCanonicalFileURL( aFileStatus
.getFileURL() );
164 OUString
aSearchURL1( "share/basic" );
165 OUString
aSearchURL2( "share/uno_packages" );
166 if( aCanonicalFileURL
.indexOf( aSearchURL1
) != -1 || aCanonicalFileURL
.indexOf( aSearchURL2
) != -1 )
175 OUString
BasicProviderImpl::getImplementationName( ) throw (RuntimeException
, std::exception
)
177 return getImplementationName_BasicProviderImpl();
180 sal_Bool
BasicProviderImpl::supportsService( const OUString
& rServiceName
) throw (RuntimeException
, std::exception
)
182 return cppu::supportsService(this, rServiceName
);
185 Sequence
< OUString
> BasicProviderImpl::getSupportedServiceNames( ) throw (RuntimeException
, std::exception
)
187 return getSupportedServiceNames_BasicProviderImpl();
194 void BasicProviderImpl::initialize( const Sequence
< Any
>& aArguments
) throw (Exception
, RuntimeException
, std::exception
)
198 SolarMutexGuard aGuard
;
200 if ( aArguments
.getLength() != 1 )
202 throw IllegalArgumentException(
203 "BasicProviderImpl::initialize: incorrect argument count.",
209 Reference
< frame::XModel
> xModel
;
211 m_xInvocationContext
.set( aArguments
[0], UNO_QUERY
);
212 if ( m_xInvocationContext
.is() )
214 xModel
.set( m_xInvocationContext
->getScriptContainer(), UNO_QUERY
);
217 throw IllegalArgumentException(
218 "BasicProviderImpl::initialize: unable to determine the document model from the script invocation context.",
226 if ( !( aArguments
[0] >>= m_sScriptingContext
) )
228 throw IllegalArgumentException(
229 "BasicProviderImpl::initialize: incorrect argument type " + aArguments
[0].getValueTypeName(),
235 OUString sDoc
= "vnd.sun.star.tdoc";
236 if ( m_sScriptingContext
.startsWith( sDoc
) )
238 xModel
= MiscUtils::tDocUrlToModel( m_sScriptingContext
);
239 // TODO: use ScriptingContantsPool for SCRIPTING_DOC_REF
245 Reference
< XEmbeddedScripts
> xDocumentScripts( xModel
, UNO_QUERY
);
246 if ( xDocumentScripts
.is() )
248 m_pDocBasicManager
= ::basic::BasicManagerRepository::getDocumentBasicManager( xModel
);
249 m_xLibContainerDoc
.set( xDocumentScripts
->getBasicLibraries(), UNO_QUERY
);
250 OSL_ENSURE( m_pDocBasicManager
&& m_xLibContainerDoc
.is(),
251 "BasicProviderImpl::initialize: invalid BasicManager, or invalid script container!" );
253 m_bIsAppScriptCtx
= false;
257 // Provider has been created with application context for user
259 if ( m_sScriptingContext
!= "user" )
261 m_bIsUserCtx
= false;
266 throw RuntimeException(
267 "BasicProviderImpl::initialize: no scripting context!" );
273 if ( !m_pAppBasicManager
)
274 m_pAppBasicManager
= SfxApplication::GetBasicManager();
276 if ( !m_xLibContainerApp
.is() )
277 m_xLibContainerApp
= Reference
< script::XLibraryContainer
>( SfxGetpApp()->GetBasicContainer(), UNO_QUERY
);
285 Reference
< provider::XScript
> BasicProviderImpl::getScript( const OUString
& scriptURI
)
286 throw ( provider::ScriptFrameworkErrorException
, RuntimeException
, std::exception
)
290 SolarMutexGuard aGuard
;
292 Reference
< provider::XScript
> xScript
;
293 Reference
< uri::XUriReferenceFactory
> xFac ( uri::UriReferenceFactory::create( m_xContext
) );
295 Reference
< uri::XUriReference
> uriRef(
296 xFac
->parse( scriptURI
), UNO_QUERY
);
298 Reference
< uri::XVndSunStarScriptUrl
> sfUri( uriRef
, UNO_QUERY
);
300 if ( !uriRef
.is() || !sfUri
.is() )
302 OUString
errorMsg("BasicProviderImpl::getScript: failed to parse URI: ");
303 errorMsg
= errorMsg
.concat( scriptURI
);
304 throw provider::ScriptFrameworkErrorException(
305 errorMsg
, Reference
< XInterface
>(),
307 provider::ScriptFrameworkErrorType::MALFORMED_URL
);
311 OUString aDescription
= sfUri
->getName();
312 OUString aLocation
= sfUri
->getParameter( OUString("location") );
314 sal_Int32 nIndex
= 0;
315 // In some strange circumstances the Library name can have an
316 // apparently illegal '.' in it ( in imported VBA )
318 BasicManager
* pBasicMgr
= NULL
;
319 if ( aLocation
== "document" )
321 pBasicMgr
= m_pDocBasicManager
;
323 else if ( aLocation
== "application" )
325 pBasicMgr
= m_pAppBasicManager
;
327 OUString sProjectName
;
329 sProjectName
= pBasicMgr
->GetName();
332 if ( !sProjectName
.isEmpty() && aDescription
.match( sProjectName
) )
334 OSL_TRACE("LibraryName %s is part of the url %s",
335 OUStringToOString( sProjectName
, RTL_TEXTENCODING_UTF8
).getStr(),
336 OUStringToOString( aDescription
, RTL_TEXTENCODING_UTF8
).getStr() );
337 aLibrary
= sProjectName
;
338 nIndex
= sProjectName
.getLength() + 1;
341 aLibrary
= aDescription
.getToken( 0, (sal_Unicode
)'.', nIndex
);
344 aModule
= aDescription
.getToken( 0, (sal_Unicode
)'.', nIndex
);
347 aMethod
= aDescription
.getToken( 0, (sal_Unicode
)'.', nIndex
);
349 if ( !aLibrary
.isEmpty() && !aModule
.isEmpty() && !aMethod
.isEmpty() && !aLocation
.isEmpty() )
354 StarBASIC
* pBasic
= pBasicMgr
->GetLib( aLibrary
);
357 sal_uInt16 nId
= pBasicMgr
->GetLibId( aLibrary
);
358 if ( nId
!= LIB_NOTFOUND
)
360 pBasicMgr
->LoadLib( nId
);
361 pBasic
= pBasicMgr
->GetLib( aLibrary
);
366 SbModule
* pModule
= pBasic
->FindModule( aModule
);
369 SbxArray
* pMethods
= pModule
->GetMethods();
372 SbMethod
* pMethod
= static_cast< SbMethod
* >( pMethods
->Find( aMethod
, SbxCLASS_METHOD
) );
373 if ( pMethod
&& !pMethod
->IsHidden() )
375 if ( m_pDocBasicManager
== pBasicMgr
)
376 xScript
= new BasicScriptImpl( aDescription
, pMethod
, *m_pDocBasicManager
, m_xInvocationContext
);
378 xScript
= new BasicScriptImpl( aDescription
, pMethod
);
388 OUStringBuffer aMessage
;
389 aMessage
.appendAscii( "The following Basic script could not be found:\n" );
390 aMessage
.appendAscii( "library: '" ).append( aLibrary
).appendAscii( "'\n" );
391 aMessage
.appendAscii( "module: '" ).append( aModule
).appendAscii( "'\n" );
392 aMessage
.appendAscii( "method: '" ).append( aMethod
).appendAscii( "'\n" );
393 aMessage
.appendAscii( "location: '" ).append( aLocation
).appendAscii( "'\n" );
394 throw provider::ScriptFrameworkErrorException(
395 aMessage
.makeStringAndClear(),
396 Reference
< XInterface
>(),
398 provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT
);
408 OUString
BasicProviderImpl::getName( ) throw (RuntimeException
, std::exception
)
410 return OUString("Basic");
415 Sequence
< Reference
< browse::XBrowseNode
> > BasicProviderImpl::getChildNodes( ) throw (RuntimeException
, std::exception
)
417 SolarMutexGuard aGuard
;
419 Reference
< script::XLibraryContainer
> xLibContainer
;
420 BasicManager
* pBasicManager
= NULL
;
422 if ( m_bIsAppScriptCtx
)
424 xLibContainer
= m_xLibContainerApp
;
425 pBasicManager
= m_pAppBasicManager
;
429 xLibContainer
= m_xLibContainerDoc
;
430 pBasicManager
= m_pDocBasicManager
;
433 Sequence
< Reference
< browse::XBrowseNode
> > aChildNodes
;
435 if ( pBasicManager
&& xLibContainer
.is() )
437 Sequence
< OUString
> aLibNames
= xLibContainer
->getElementNames();
438 sal_Int32 nLibCount
= aLibNames
.getLength();
439 const OUString
* pLibNames
= aLibNames
.getConstArray();
440 aChildNodes
.realloc( nLibCount
);
441 Reference
< browse::XBrowseNode
>* pChildNodes
= aChildNodes
.getArray();
442 sal_Int32 childrenFound
= 0;
444 for ( sal_Int32 i
= 0 ; i
< nLibCount
; ++i
)
446 bool bCreate
= false;
447 if ( m_bIsAppScriptCtx
)
449 bool bShared
= isLibraryShared( xLibContainer
, pLibNames
[i
] );
450 if ( ( m_bIsUserCtx
&& !bShared
) || ( !m_bIsUserCtx
&& bShared
) )
459 pChildNodes
[childrenFound
++] = static_cast< browse::XBrowseNode
* >( new BasicLibraryNodeImpl(
460 m_xContext
, m_sScriptingContext
, pBasicManager
, xLibContainer
, pLibNames
[i
], m_bIsAppScriptCtx
) );
464 if ( childrenFound
!= nLibCount
)
465 aChildNodes
.realloc( childrenFound
);
473 sal_Bool
BasicProviderImpl::hasChildNodes( ) throw (RuntimeException
, std::exception
)
475 SolarMutexGuard aGuard
;
477 bool bReturn
= false;
478 Reference
< script::XLibraryContainer
> xLibContainer
;
479 if ( m_bIsAppScriptCtx
)
481 xLibContainer
= m_xLibContainerApp
;
485 xLibContainer
= m_xLibContainerDoc
;
487 if ( xLibContainer
.is() )
488 bReturn
= xLibContainer
->hasElements();
495 sal_Int16
BasicProviderImpl::getType( ) throw (RuntimeException
, std::exception
)
497 SolarMutexGuard aGuard
;
499 return browse::BrowseNodeTypes::CONTAINER
;
503 // component operations
506 static Reference
< XInterface
> SAL_CALL
create_BasicProviderImpl(
507 Reference
< XComponentContext
> const & xContext
)
509 return static_cast< lang::XTypeProvider
* >( new BasicProviderImpl( xContext
) );
514 static struct ::cppu::ImplementationEntry s_component_entries
[] =
517 create_BasicProviderImpl
, getImplementationName_BasicProviderImpl
,
518 getSupportedServiceNames_BasicProviderImpl
, ::cppu::createSingleComponentFactory
,
527 } // namespace basprov
537 SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
basprov_component_getFactory(
538 const sal_Char
* pImplName
, void * pServiceManager
,
539 void * pRegistryKey
)
541 return ::cppu::component_getFactoryHelper(
542 pImplName
, pServiceManager
, pRegistryKey
, ::basprov::s_component_entries
);
546 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */