1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "basprov.hxx"
#include "basscript.hxx"
#include "baslibnode.hxx"
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
#include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
#include <com/sun/star/document/XEmbeddedScripts.hpp>
#include <com/sun/star/uri/UriReferenceFactory.hpp>
#include <cppuhelper/implementationentry.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <rtl/uri.hxx>
#include <osl/process.h>
#include <osl/file.hxx>
#include <osl/mutex.hxx>
#include <vcl/svapp.hxx>
#include <basic/sbx.hxx>
#include <basic/basmgr.hxx>
#include <basic/basicmanagerrepository.hxx>
#include <basic/sbstar.hxx>
#include <basic/sbmod.hxx>
#include <basic/sbmeth.hxx>
#include <sfx2/app.hxx>
#include <sfx2/objsh.hxx>
#include <com/sun/star/util/theMacroExpander.hpp>
#include <com/sun/star/script/XLibraryContainer2.hpp>
#include <com/sun/star/uri/XUriReference.hpp>
#include <com/sun/star/uri/XUriReferenceFactory.hpp>
#include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
#include <util/MiscUtils.hxx>
using namespace ::com::sun::star;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::script;
using namespace ::com::sun::star::document;
using namespace ::sf_misc;
//.........................................................................
namespace basprov
{
//.........................................................................
// =============================================================================
// component operations
// =============================================================================
static OUString getImplementationName_BasicProviderImpl()
{
static OUString* pImplName = 0;
if ( !pImplName )
{
::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
if ( !pImplName )
{
static OUString aImplName( "com.sun.star.comp.scripting.ScriptProviderForBasic" );
pImplName = &aImplName;
}
}
return *pImplName;
}
// -----------------------------------------------------------------------------
static Sequence< OUString > getSupportedServiceNames_BasicProviderImpl()
{
static Sequence< OUString >* pNames = 0;
if ( !pNames )
{
::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
if ( !pNames )
{
static Sequence< OUString > aNames(4);
aNames.getArray()[0] = "com.sun.star.script.provider.ScriptProviderForBasic";
aNames.getArray()[1] = "com.sun.star.script.provider.LanguageScriptProvider";
aNames.getArray()[2] = "com.sun.star.script.provider.ScriptProvider";
aNames.getArray()[3] = "com.sun.star.script.browse.BrowseNode";
pNames = &aNames;
}
}
return *pNames;
}
// =============================================================================
// BasicProviderImpl
// =============================================================================
BasicProviderImpl::BasicProviderImpl( const Reference< XComponentContext >& xContext )
:m_pAppBasicManager( 0 )
,m_pDocBasicManager( 0 )
,m_xLibContainerApp( 0 )
,m_xLibContainerDoc( 0 )
,m_xContext( xContext )
,m_bIsAppScriptCtx( true )
,m_bIsUserCtx(true)
{
}
// -----------------------------------------------------------------------------
BasicProviderImpl::~BasicProviderImpl()
{
}
// -----------------------------------------------------------------------------
bool BasicProviderImpl::isLibraryShared( const Reference< script::XLibraryContainer >& rxLibContainer, const OUString& rLibName )
{
bool bIsShared = false;
Reference< script::XLibraryContainer2 > xLibContainer( rxLibContainer, UNO_QUERY );
if ( xLibContainer.is() && xLibContainer->hasByName( rLibName ) && xLibContainer->isLibraryLink( rLibName ) )
{
OUString aFileURL;
if ( m_xContext.is() )
{
Reference< uri::XUriReferenceFactory > xUriFac( uri::UriReferenceFactory::create( m_xContext ) );
OUString aLinkURL( xLibContainer->getLibraryLinkURL( rLibName ) );
Reference< uri::XUriReference > xUriRef( xUriFac->parse( aLinkURL ), UNO_QUERY );
if ( xUriRef.is() )
{
OUString aScheme = xUriRef->getScheme();
if ( aScheme.equalsIgnoreAsciiCase("file") )
{
aFileURL = aLinkURL;
}
else if ( aScheme.equalsIgnoreAsciiCase("vnd.sun.star.pkg") )
{
OUString aAuthority = xUriRef->getAuthority();
if ( aAuthority.matchIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.expand:" ) ) )
{
OUString aDecodedURL( aAuthority.copy( sizeof ( "vnd.sun.star.expand:" ) - 1 ) );
aDecodedURL = ::rtl::Uri::decode( aDecodedURL, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
Reference<util::XMacroExpander> xMacroExpander =
util::theMacroExpander::get(m_xContext);
aFileURL = xMacroExpander->expandMacros( aDecodedURL );
}
}
}
}
if ( !aFileURL.isEmpty() )
{
osl::DirectoryItem aFileItem;
osl::FileStatus aFileStatus( osl_FileStatus_Mask_FileURL );
OSL_VERIFY( osl::DirectoryItem::get( aFileURL, aFileItem ) == osl::FileBase::E_None );
OSL_VERIFY( aFileItem.getFileStatus( aFileStatus ) == osl::FileBase::E_None );
OUString aCanonicalFileURL( aFileStatus.getFileURL() );
OUString aSearchURL1( "share/basic" );
OUString aSearchURL2( "share/uno_packages" );
if( aCanonicalFileURL.indexOf( aSearchURL1 ) != -1 || aCanonicalFileURL.indexOf( aSearchURL2 ) != -1 )
bIsShared = true;
}
}
return bIsShared;
}
// XServiceInfo
OUString BasicProviderImpl::getImplementationName( ) throw (RuntimeException)
{
return getImplementationName_BasicProviderImpl();
}
sal_Bool BasicProviderImpl::supportsService( const OUString& rServiceName ) throw (RuntimeException)
{
return cppu::supportsService(this, rServiceName);
}
Sequence< OUString > BasicProviderImpl::getSupportedServiceNames( ) throw (RuntimeException)
{
return getSupportedServiceNames_BasicProviderImpl();
}
// -----------------------------------------------------------------------------
// XInitialization
// -----------------------------------------------------------------------------
void BasicProviderImpl::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
{
// TODO
SolarMutexGuard aGuard;
if ( aArguments.getLength() != 1 )
{
throw IllegalArgumentException(
OUString( "BasicProviderImpl::initialize: incorrect argument count." ),
*this,
1
);
}
Reference< frame::XModel > xModel;
m_xInvocationContext.set( aArguments[0], UNO_QUERY );
if ( m_xInvocationContext.is() )
{
xModel.set( m_xInvocationContext->getScriptContainer(), UNO_QUERY );
if ( !xModel.is() )
{
throw IllegalArgumentException(
OUString( "BasicProviderImpl::initialize: unable to determine the document model from the script invocation context." ),
*this,
1
);
}
}
else
{
if ( !( aArguments[0] >>= m_sScriptingContext ) )
{
throw IllegalArgumentException(
OUString( "BasicProviderImpl::initialize: incorrect argument type " ).concat( aArguments[0].getValueTypeName() ),
*this,
1
);
}
OUString sDoc = "vnd.sun.star.tdoc";
if ( m_sScriptingContext.startsWith( sDoc ) )
{
xModel = MiscUtils::tDocUrlToModel( m_sScriptingContext );
// TODO: use ScriptingContantsPool for SCRIPTING_DOC_REF
}
}
if ( xModel.is() )
{
Reference< XEmbeddedScripts > xDocumentScripts( xModel, UNO_QUERY );
if ( xDocumentScripts.is() )
{
m_pDocBasicManager = ::basic::BasicManagerRepository::getDocumentBasicManager( xModel );
m_xLibContainerDoc.set( xDocumentScripts->getBasicLibraries(), UNO_QUERY );
OSL_ENSURE( m_pDocBasicManager && m_xLibContainerDoc.is(),
"BasicProviderImpl::initialize: invalid BasicManager, or invalid script container!" );
}
m_bIsAppScriptCtx = false;
}
else
{
// Provider has been created with application context for user
// or share
if ( m_sScriptingContext != "user" )
{
m_bIsUserCtx = false;
}
else
{
/*
throw RuntimeException(
OUString( "BasicProviderImpl::initialize: no scripting context!" ),
Reference< XInterface >() );
*/
}
}
// TODO
if ( !m_pAppBasicManager )
m_pAppBasicManager = SFX_APP()->GetBasicManager();
if ( !m_xLibContainerApp.is() )
m_xLibContainerApp = Reference< script::XLibraryContainer >( SFX_APP()->GetBasicContainer(), UNO_QUERY );
}
// -----------------------------------------------------------------------------
// XScriptProvider
// -----------------------------------------------------------------------------
Reference < provider::XScript > BasicProviderImpl::getScript( const OUString& scriptURI )
throw ( provider::ScriptFrameworkErrorException, RuntimeException)
{
// TODO
SolarMutexGuard aGuard;
Reference< provider::XScript > xScript;
Reference< uri::XUriReferenceFactory > xFac ( uri::UriReferenceFactory::create( m_xContext ) );
Reference< uri::XUriReference > uriRef(
xFac->parse( scriptURI ), UNO_QUERY );
Reference < uri::XVndSunStarScriptUrl > sfUri( uriRef, UNO_QUERY );
if ( !uriRef.is() || !sfUri.is() )
{
OUString errorMsg("BasicProviderImpl::getScript: failed to parse URI: ");
errorMsg = errorMsg.concat( scriptURI );
throw provider::ScriptFrameworkErrorException(
errorMsg, Reference< XInterface >(),
scriptURI, "Basic",
provider::ScriptFrameworkErrorType::MALFORMED_URL );
}
OUString aDescription = sfUri->getName();
OUString aLocation = sfUri->getParameter(
OUString("location") );
sal_Int32 nIndex = 0;
// In some strange circumstances the Library name can have an
// apparantly illegal '.' in it ( in imported VBA )
BasicManager* pBasicMgr = NULL;
if ( aLocation == "document" )
{
pBasicMgr = m_pDocBasicManager;
}
else if ( aLocation == "application" )
{
pBasicMgr = m_pAppBasicManager;
}
OUString sProjectName;
if ( pBasicMgr )
sProjectName = pBasicMgr->GetName();
OUString aLibrary;
if ( !sProjectName.isEmpty() && aDescription.match( sProjectName ) )
{
OSL_TRACE("LibraryName %s is part of the url %s",
OUStringToOString( sProjectName, RTL_TEXTENCODING_UTF8 ).getStr(),
OUStringToOString( aDescription, RTL_TEXTENCODING_UTF8 ).getStr() );
aLibrary = sProjectName;
nIndex = sProjectName.getLength() + 1;
}
else
aLibrary = aDescription.getToken( 0, (sal_Unicode)'.', nIndex );
OUString aModule;
if ( nIndex != -1 )
aModule = aDescription.getToken( 0, (sal_Unicode)'.', nIndex );
OUString aMethod;
if ( nIndex != -1 )
aMethod = aDescription.getToken( 0, (sal_Unicode)'.', nIndex );
if ( !aLibrary.isEmpty() && !aModule.isEmpty() && !aMethod.isEmpty() && !aLocation.isEmpty() )
{
if ( pBasicMgr )
{
StarBASIC* pBasic = pBasicMgr->GetLib( aLibrary );
if ( !pBasic )
{
sal_uInt16 nId = pBasicMgr->GetLibId( aLibrary );
if ( nId != LIB_NOTFOUND )
{
pBasicMgr->LoadLib( nId );
pBasic = pBasicMgr->GetLib( aLibrary );
}
}
if ( pBasic )
{
SbModule* pModule = pBasic->FindModule( aModule );
if ( pModule )
{
SbxArray* pMethods = pModule->GetMethods();
if ( pMethods )
{
SbMethod* pMethod = static_cast< SbMethod* >( pMethods->Find( aMethod, SbxCLASS_METHOD ) );
if ( pMethod && !pMethod->IsHidden() )
{
if ( m_pDocBasicManager == pBasicMgr )
xScript = new BasicScriptImpl( aDescription, pMethod, *m_pDocBasicManager, m_xInvocationContext );
else
xScript = new BasicScriptImpl( aDescription, pMethod );
}
}
}
}
}
}
if ( !xScript.is() )
{
OUStringBuffer aMessage;
aMessage.appendAscii( "The following Basic script could not be found:\n" );
aMessage.appendAscii( "library: '" ).append( aLibrary ).appendAscii( "'\n" );
aMessage.appendAscii( "module: '" ).append( aModule ).appendAscii( "'\n" );
aMessage.appendAscii( "method: '" ).append( aMethod ).appendAscii( "'\n" );
aMessage.appendAscii( "location: '" ).append( aLocation ).appendAscii( "'\n" );
throw provider::ScriptFrameworkErrorException(
aMessage.makeStringAndClear(),
Reference< XInterface >(),
scriptURI, "Basic",
provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT );
}
return xScript;
}
// -----------------------------------------------------------------------------
// XBrowseNode
// -----------------------------------------------------------------------------
OUString BasicProviderImpl::getName( ) throw (RuntimeException)
{
// TODO
SolarMutexGuard aGuard;
return OUString("Basic");
}
// -----------------------------------------------------------------------------
Sequence< Reference< browse::XBrowseNode > > BasicProviderImpl::getChildNodes( ) throw (RuntimeException)
{
SolarMutexGuard aGuard;
Reference< script::XLibraryContainer > xLibContainer;
BasicManager* pBasicManager = NULL;
if ( m_bIsAppScriptCtx )
{
xLibContainer = m_xLibContainerApp;
pBasicManager = m_pAppBasicManager;
}
else
{
xLibContainer = m_xLibContainerDoc;
pBasicManager = m_pDocBasicManager;
}
Sequence< Reference< browse::XBrowseNode > > aChildNodes;
if ( pBasicManager && xLibContainer.is() )
{
Sequence< OUString > aLibNames = xLibContainer->getElementNames();
sal_Int32 nLibCount = aLibNames.getLength();
const OUString* pLibNames = aLibNames.getConstArray();
aChildNodes.realloc( nLibCount );
Reference< browse::XBrowseNode >* pChildNodes = aChildNodes.getArray();
sal_Int32 childrenFound = 0;
for ( sal_Int32 i = 0 ; i < nLibCount ; ++i )
{
bool bCreate = false;
if ( m_bIsAppScriptCtx )
{
bool bShared = isLibraryShared( xLibContainer, pLibNames[i] );
if ( ( m_bIsUserCtx && !bShared ) || ( !m_bIsUserCtx && bShared ) )
bCreate = true;
}
else
{
bCreate = true;
}
if ( bCreate )
{
pChildNodes[childrenFound++] = static_cast< browse::XBrowseNode* >( new BasicLibraryNodeImpl(
m_xContext, m_sScriptingContext, pBasicManager, xLibContainer, pLibNames[i], m_bIsAppScriptCtx ) );
}
}
if ( childrenFound != nLibCount )
aChildNodes.realloc( childrenFound );
}
return aChildNodes;
}
// -----------------------------------------------------------------------------
sal_Bool BasicProviderImpl::hasChildNodes( ) throw (RuntimeException)
{
SolarMutexGuard aGuard;
sal_Bool bReturn = sal_False;
Reference< script::XLibraryContainer > xLibContainer;
if ( m_bIsAppScriptCtx )
{
xLibContainer = m_xLibContainerApp;
}
else
{
xLibContainer = m_xLibContainerDoc;
}
if ( xLibContainer.is() )
bReturn = xLibContainer->hasElements();
return bReturn;
}
// -----------------------------------------------------------------------------
sal_Int16 BasicProviderImpl::getType( ) throw (RuntimeException)
{
SolarMutexGuard aGuard;
return browse::BrowseNodeTypes::CONTAINER;
}
// =============================================================================
// component operations
// =============================================================================
static Reference< XInterface > SAL_CALL create_BasicProviderImpl(
Reference< XComponentContext > const & xContext )
SAL_THROW(())
{
return static_cast< lang::XTypeProvider * >( new BasicProviderImpl( xContext ) );
}
// -----------------------------------------------------------------------------
static struct ::cppu::ImplementationEntry s_component_entries [] =
{
{
create_BasicProviderImpl, getImplementationName_BasicProviderImpl,
getSupportedServiceNames_BasicProviderImpl, ::cppu::createSingleComponentFactory,
0, 0
},
{ 0, 0, 0, 0, 0, 0 }
};
// -----------------------------------------------------------------------------
//.........................................................................
} // namespace basprov
//.........................................................................
// =============================================================================
// component exports
// =============================================================================
extern "C"
{
SAL_DLLPUBLIC_EXPORT void * SAL_CALL basprov_component_getFactory(
const sal_Char * pImplName, lang::XMultiServiceFactory * pServiceManager,
registry::XRegistryKey * pRegistryKey )
{
return ::cppu::component_getFactoryHelper(
pImplName, pServiceManager, pRegistryKey, ::basprov::s_component_entries );
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|