LibreOffice Module scripting (master) 1
basprov.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
29
31#include <rtl/uri.hxx>
32#include <sal/log.hxx>
33#include <osl/file.hxx>
34#include <vcl/svapp.hxx>
35#include <basic/basmgr.hxx>
37#include <basic/sbstar.hxx>
38#include <basic/sbmod.hxx>
39#include <basic/sbmeth.hxx>
40#include <sfx2/app.hxx>
41
42#include <com/sun/star/util/theMacroExpander.hpp>
43#include <com/sun/star/script/XLibraryContainer2.hpp>
44#include <com/sun/star/uri/XUriReference.hpp>
45#include <com/sun/star/uri/XUriReferenceFactory.hpp>
46#include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
47
48#include <util/MiscUtils.hxx>
49
50
51using namespace ::com::sun::star;
52using namespace ::com::sun::star::lang;
53using namespace ::com::sun::star::uno;
54using namespace ::com::sun::star::script;
55using namespace ::com::sun::star::document;
56using namespace ::sf_misc;
57
58
59namespace basprov
60{
61
62 // BasicProviderImpl
63
64
65 BasicProviderImpl::BasicProviderImpl( const Reference< XComponentContext >& xContext )
66 :m_pAppBasicManager( nullptr )
67 ,m_pDocBasicManager( nullptr )
68 ,m_xContext( xContext )
69 ,m_bIsAppScriptCtx( true )
70 ,m_bIsUserCtx(true)
71 {
72 }
73
74
76 {
77 SolarMutexGuard aGuard;
79 }
80
81
82 bool BasicProviderImpl::isLibraryShared( const Reference< script::XLibraryContainer >& rxLibContainer, const OUString& rLibName )
83 {
84 bool bIsShared = false;
85
86 Reference< script::XLibraryContainer2 > xLibContainer( rxLibContainer, UNO_QUERY );
87 if ( xLibContainer.is() && xLibContainer->hasByName( rLibName ) && xLibContainer->isLibraryLink( rLibName ) )
88 {
89 OUString aFileURL;
90 if ( m_xContext.is() )
91 {
92 Reference< uri::XUriReferenceFactory > xUriFac( uri::UriReferenceFactory::create( m_xContext ) );
93
94 OUString aLinkURL( xLibContainer->getLibraryLinkURL( rLibName ) );
95 Reference< uri::XUriReference > xUriRef = xUriFac->parse( aLinkURL );
96
97 if ( xUriRef.is() )
98 {
99 OUString aScheme = xUriRef->getScheme();
100 if ( aScheme.equalsIgnoreAsciiCase("file") )
101 {
102 aFileURL = aLinkURL;
103 }
104 else if ( aScheme.equalsIgnoreAsciiCase("vnd.sun.star.pkg") )
105 {
106 OUString aDecodedURL = xUriRef->getAuthority();
107 if ( aDecodedURL.startsWithIgnoreAsciiCase( "vnd.sun.star.expand:", &aDecodedURL ) )
108 {
109 aDecodedURL = ::rtl::Uri::decode( aDecodedURL, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
110 Reference<util::XMacroExpander> xMacroExpander =
111 util::theMacroExpander::get(m_xContext);
112 aFileURL = xMacroExpander->expandMacros( aDecodedURL );
113 }
114 }
115 }
116 }
117
118 if ( !aFileURL.isEmpty() )
119 {
120 osl::DirectoryItem aFileItem;
121 osl::FileStatus aFileStatus( osl_FileStatus_Mask_FileURL );
122 OSL_VERIFY( osl::DirectoryItem::get( aFileURL, aFileItem ) == osl::FileBase::E_None );
123 OSL_VERIFY( aFileItem.getFileStatus( aFileStatus ) == osl::FileBase::E_None );
124 OUString aCanonicalFileURL( aFileStatus.getFileURL() );
125
126 if( aCanonicalFileURL.indexOf( "share/basic" ) != -1
127 || aCanonicalFileURL.indexOf( "share/uno_packages" ) != -1 )
128 bIsShared = true;
129 }
130 }
131
132 return bIsShared;
133 }
134
135 // SfxListener
137 {
138 if (auto pManager = dynamic_cast<const BasicManager*>(&rBC))
139 if (pManager == m_pAppBasicManager && rHint.GetId() == SfxHintId::Dying)
140 {
142 m_pAppBasicManager = nullptr;
143 }
144 }
145
146 // XServiceInfo
148 {
149 return "com.sun.star.comp.scripting.ScriptProviderForBasic";
150 }
151
152 sal_Bool BasicProviderImpl::supportsService( const OUString& rServiceName )
153 {
154 return cppu::supportsService(this, rServiceName);
155 }
156
158 {
159 return {
160 "com.sun.star.script.provider.ScriptProviderForBasic",
161 "com.sun.star.script.provider.LanguageScriptProvider",
162 "com.sun.star.script.provider.ScriptProvider",
163 "com.sun.star.script.browse.BrowseNode"};
164 }
165
166
167 // XInitialization
168
169
170 void BasicProviderImpl::initialize( const Sequence< Any >& aArguments )
171 {
172 // TODO
173
174 SolarMutexGuard aGuard;
175
176 if ( aArguments.getLength() != 1 )
177 {
178 throw IllegalArgumentException(
179 "BasicProviderImpl::initialize: incorrect argument count.",
180 *this,
181 1
182 );
183 }
184
186
187 m_xInvocationContext.set( aArguments[0], UNO_QUERY );
188 if ( m_xInvocationContext.is() )
189 {
190 xModel.set( m_xInvocationContext->getScriptContainer(), UNO_QUERY );
191 if ( !xModel.is() )
192 {
193 throw IllegalArgumentException(
194 "BasicProviderImpl::initialize: unable to determine the document model from the script invocation context.",
195 *this,
196 1
197 );
198 }
199 }
200 else
201 {
202 if ( !( aArguments[0] >>= m_sScriptingContext ) )
203 {
204 throw IllegalArgumentException(
205 "BasicProviderImpl::initialize: incorrect argument type " + aArguments[0].getValueTypeName(),
206 *this,
207 1
208 );
209 }
210
211 if ( m_sScriptingContext.startsWith( "vnd.sun.star.tdoc" ) )
212 {
213 xModel = MiscUtils::tDocUrlToModel( m_sScriptingContext );
214 }
215 }
216
217 if ( xModel.is() )
218 {
219 Reference< XEmbeddedScripts > xDocumentScripts( xModel, UNO_QUERY );
220 if ( xDocumentScripts.is() )
221 {
223 m_xLibContainerDoc = xDocumentScripts->getBasicLibraries();
224 OSL_ENSURE( m_pDocBasicManager && m_xLibContainerDoc.is(),
225 "BasicProviderImpl::initialize: invalid BasicManager, or invalid script container!" );
226 }
227 m_bIsAppScriptCtx = false;
228 }
229 else
230 {
231 // Provider has been created with application context for user
232 // or share
233 if ( m_sScriptingContext != "user" )
234 {
235 m_bIsUserCtx = false;
236 }
237 else
238 {
239 /*
240 throw RuntimeException(
241 "BasicProviderImpl::initialize: no scripting context!" );
242 */
243 }
244 }
245
246 // TODO
247 if ( !m_pAppBasicManager )
248 {
252 }
253
254 if ( !m_xLibContainerApp.is() )
256 }
257
258
259 // XScriptProvider
260
261
262 Reference < provider::XScript > BasicProviderImpl::getScript( const OUString& scriptURI )
263 {
264 // TODO
265
266 SolarMutexGuard aGuard;
267
268 Reference< provider::XScript > xScript;
269 Reference< uri::XUriReferenceFactory > xFac ( uri::UriReferenceFactory::create( m_xContext ) );
270
271 Reference< uri::XUriReference > uriRef = xFac->parse( scriptURI );
272
273 Reference < uri::XVndSunStarScriptUrl > sfUri( uriRef, UNO_QUERY );
274
275 if ( !uriRef.is() || !sfUri.is() )
276 {
277 throw provider::ScriptFrameworkErrorException(
278 "BasicProviderImpl::getScript: failed to parse URI: " + scriptURI,
279 Reference< XInterface >(),
280 scriptURI, "Basic",
281 provider::ScriptFrameworkErrorType::MALFORMED_URL );
282 }
283
284
285 OUString aDescription = sfUri->getName();
286 OUString aLocation = sfUri->getParameter( "location" );
287
288 sal_Int32 nIndex = 0;
289 // In some strange circumstances the Library name can have an
290 // apparently illegal '.' in it ( in imported VBA )
291
292 BasicManager* pBasicMgr = nullptr;
293 if ( aLocation == "document" )
294 {
295 pBasicMgr = m_pDocBasicManager;
296 }
297 else if ( aLocation == "application" )
298 {
299 pBasicMgr = m_pAppBasicManager;
300 }
301 OUString sProjectName;
302 if ( pBasicMgr )
303 sProjectName = pBasicMgr->GetName();
304
305 OUString aLibrary;
306 if ( !sProjectName.isEmpty() && aDescription.match( sProjectName ) )
307 {
308 SAL_WARN("scripting", "LibraryName " << sProjectName << " is part of the url " << aDescription );
309 aLibrary = sProjectName;
310 nIndex = sProjectName.getLength() + 1;
311 }
312 else
313 aLibrary = aDescription.getToken( 0, '.', nIndex );
314 OUString aModule;
315 if ( nIndex != -1 )
316 aModule = aDescription.getToken( 0, '.', nIndex );
317 OUString aMethod;
318 if ( nIndex != -1 )
319 aMethod = aDescription.getToken( 0, '.', nIndex );
320
321 if ( !aLibrary.isEmpty() && !aModule.isEmpty() && !aMethod.isEmpty() && !aLocation.isEmpty() )
322 {
323
324 if ( pBasicMgr )
325 {
326 StarBASIC* pBasic = pBasicMgr->GetLib( aLibrary );
327 if ( !pBasic )
328 {
329 sal_uInt16 nId = pBasicMgr->GetLibId( aLibrary );
330 if ( nId != LIB_NOTFOUND )
331 {
332 pBasicMgr->LoadLib( nId );
333 pBasic = pBasicMgr->GetLib( aLibrary );
334 }
335 }
336 if ( pBasic )
337 {
338 SbModule* pModule = pBasic->FindModule( aModule );
339 if ( pModule )
340 {
341 SbMethod* pMethod = pModule->FindMethod( aMethod, SbxClassType::Method );
342 if ( pMethod && !pMethod->IsHidden() )
343 {
344 if ( m_pDocBasicManager == pBasicMgr )
345 xScript = new BasicScriptImpl( aDescription, pMethod, *m_pDocBasicManager, m_xInvocationContext );
346 else
347 xScript = new BasicScriptImpl( aDescription, pMethod );
348 }
349 }
350 }
351 }
352 }
353
354 if ( !xScript.is() )
355 {
356 throw provider::ScriptFrameworkErrorException(
357 "The following Basic script could not be found:\n"
358 "library: '" + aLibrary + "'\n"
359 "module: '" + aModule + "'\n"
360 "method: '" + aMethod + "'\n"
361 "location: '" + aLocation + "'\n",
362 Reference< XInterface >(),
363 scriptURI, "Basic",
364 provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT );
365 }
366
367 return xScript;
368 }
369
370
371 // XBrowseNode
372
373
375 {
376 return "Basic";
377 }
378
379
380 Sequence< Reference< browse::XBrowseNode > > BasicProviderImpl::getChildNodes( )
381 {
382 SolarMutexGuard aGuard;
383
385 BasicManager* pBasicManager = nullptr;
386
387 if ( m_bIsAppScriptCtx )
388 {
389 xLibContainer = m_xLibContainerApp;
390 pBasicManager = m_pAppBasicManager;
391 }
392 else
393 {
394 xLibContainer = m_xLibContainerDoc;
395 pBasicManager = m_pDocBasicManager;
396 }
397
398 Sequence< Reference< browse::XBrowseNode > > aChildNodes;
399
400 if ( pBasicManager && xLibContainer.is() )
401 {
402 const Sequence< OUString > aLibNames = xLibContainer->getElementNames();
403 sal_Int32 nLibCount = aLibNames.getLength();
404 aChildNodes.realloc( nLibCount );
405 Reference< browse::XBrowseNode >* pChildNodes = aChildNodes.getArray();
406 sal_Int32 childrenFound = 0;
407
408 for ( const OUString& rLibName : aLibNames )
409 {
410 bool bCreate = false;
411 if ( m_bIsAppScriptCtx )
412 {
413 const bool bShared = isLibraryShared( xLibContainer, rLibName );
414 if (m_bIsUserCtx != bShared)
415 bCreate = true;
416 }
417 else
418 {
419 bCreate = true;
420 }
421 if ( bCreate )
422 {
423 pChildNodes[childrenFound++]
425 xLibContainer, rLibName, m_bIsAppScriptCtx);
426 }
427 }
428
429 if ( childrenFound != nLibCount )
430 aChildNodes.realloc( childrenFound );
431 }
432
433 return aChildNodes;
434 }
435
436
438 {
439 SolarMutexGuard aGuard;
440
441 bool bReturn = false;
443 if ( m_bIsAppScriptCtx )
444 {
445 xLibContainer = m_xLibContainerApp;
446 }
447 else
448 {
449 xLibContainer = m_xLibContainerDoc;
450 }
451 if ( xLibContainer.is() )
452 bReturn = xLibContainer->hasElements();
453
454 return bReturn;
455 }
456
457
459 {
460 return browse::BrowseNodeTypes::CONTAINER;
461 }
462
463
464 // component operations
465
466 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
468 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
469 {
470 return cppu::acquire(new BasicProviderImpl(context));
471 }
472
473
474} // namespace basprov
475
476/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
SfxApplication * SfxGetpApp()
#define LIB_NOTFOUND
StarBASIC * GetLib(sal_uInt16 nLib) const
const OUString & GetName() const
bool LoadLib(sal_uInt16 nLib)
sal_uInt16 GetLibId(std::u16string_view rName) const
SbMethod * FindMethod(const OUString &, SbxClassType)
bool IsHidden() const
css::script::XLibraryContainer * GetBasicContainer()
static BasicManager * GetBasicManager()
SfxHintId GetId() const
void StartListening(SfxBroadcaster &rBroadcaster, DuplicateHandling eDuplicateHanding=DuplicateHandling::Unexpected)
void EndListeningAll()
void EndListening(SfxBroadcaster &rBroadcaster, bool bRemoveAllDuplicates=false)
SbModule * FindModule(std::u16string_view)
static BasicManager * getDocumentBasicManager(const css::uno::Reference< css::frame::XModel > &_rxDocumentModel)
bool isLibraryShared(const css::uno::Reference< css::script::XLibraryContainer > &rxLibContainer, const OUString &rLibName)
Definition: basprov.cxx:82
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
Definition: basprov.cxx:170
virtual OUString SAL_CALL getImplementationName() override
Definition: basprov.cxx:147
virtual OUString SAL_CALL getName() override
Definition: basprov.cxx:374
virtual css::uno::Sequence< css::uno::Reference< css::script::browse::XBrowseNode > > SAL_CALL getChildNodes() override
Definition: basprov.cxx:380
virtual sal_Int16 SAL_CALL getType() override
Definition: basprov.cxx:458
css::uno::Reference< css::script::XLibraryContainer > m_xLibContainerApp
Definition: basprov.hxx:53
virtual css::uno::Reference< css::script::provider::XScript > SAL_CALL getScript(const OUString &scriptURI) override
Definition: basprov.cxx:262
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: basprov.cxx:157
css::uno::Reference< css::script::XLibraryContainer > m_xLibContainerDoc
Definition: basprov.hxx:54
css::uno::Reference< css::document::XScriptInvocationContext > m_xInvocationContext
Definition: basprov.hxx:56
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: basprov.cxx:136
BasicProviderImpl(const css::uno::Reference< css::uno::XComponentContext > &xContext)
Definition: basprov.cxx:65
BasicManager * m_pDocBasicManager
Definition: basprov.hxx:52
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: basprov.hxx:55
BasicManager * m_pAppBasicManager
Definition: basprov.hxx:51
virtual sal_Bool SAL_CALL hasChildNodes() override
Definition: basprov.cxx:437
virtual ~BasicProviderImpl() override
Definition: basprov.cxx:75
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: basprov.cxx:152
Sequence< PropertyValue > aArguments
sal_Int32 nIndex
#define SAL_WARN(area, stream)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * scripting_BasicProviderImpl_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: basprov.cxx:467
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
sal_Int16 nId
Reference< XModel > xModel
unsigned char sal_Bool