LibreOffice Module basic (master) 1
eventatt.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
22#include <comphelper/string.hxx>
23
24#include <com/sun/star/awt/XControlContainer.hpp>
25#include <com/sun/star/awt/XControl.hpp>
26#include <com/sun/star/awt/DialogProvider.hpp>
27#include <com/sun/star/beans/XPropertySet.hpp>
28#include <com/sun/star/container/XEnumerationAccess.hpp>
29#include <com/sun/star/container/XNameContainer.hpp>
30#include <com/sun/star/frame/XModel.hpp>
31#include <com/sun/star/frame/Desktop.hpp>
32#include <com/sun/star/script/XLibraryContainer.hpp>
33#include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
34#include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
35#include <com/sun/star/script/provider/XScriptProvider.hpp>
36#include <com/sun/star/io/XInputStreamProvider.hpp>
37
39#include <basic/basmgr.hxx>
40
41#include <sal/log.hxx>
43#include <vcl/svapp.hxx>
44#include <sbunoobj.hxx>
45#include <basic/sberrors.hxx>
46#include <basic/sbstar.hxx>
47#include <basic/sbmeth.hxx>
48#include <basic/sbuno.hxx>
49#include <runtime.hxx>
50#include <sbintern.hxx>
51#include <eventatt.hxx>
52
54
55using namespace ::com::sun::star;
56using namespace ::com::sun::star::uno;
57using namespace ::com::sun::star::script;
58using namespace ::com::sun::star::lang;
59using namespace ::com::sun::star::beans;
60using namespace ::com::sun::star::container;
61using namespace ::com::sun::star::reflection;
62using namespace ::com::sun::star::awt;
63using namespace ::com::sun::star::io;
64using namespace ::cppu;
65
66namespace {
67
68void SFURL_firing_impl( const ScriptEvent& aScriptEvent, Any* pRet, const Reference< frame::XModel >& xModel )
69{
70 SAL_INFO("basic", "Processing script url " << aScriptEvent.ScriptCode);
71 try
72 {
73 Reference< provider::XScriptProvider > xScriptProvider;
74 if ( xModel.is() )
75 {
76 Reference< provider::XScriptProviderSupplier > xSupplier( xModel, UNO_QUERY );
77 OSL_ENSURE( xSupplier.is(), "SFURL_firing_impl: failed to get script provider supplier" );
78 if ( xSupplier.is() )
79 xScriptProvider.set( xSupplier->getScriptProvider() );
80 }
81 else
82 {
83 Reference< XComponentContext > xContext(
85 Reference< provider::XScriptProviderFactory > xFactory =
86 provider::theMasterScriptProviderFactory::get( xContext );
87
88 Any aCtx;
89 aCtx <<= OUString("user");
90 xScriptProvider = xFactory->createScriptProvider( aCtx );
91 }
92
93 if ( !xScriptProvider.is() )
94 {
95 SAL_INFO("basic", "Failed to create msp");
96 return;
97 }
98 Sequence< Any > inArgs( 0 );
99 Sequence< Any > outArgs( 0 );
100 Sequence< sal_Int16 > outIndex;
101
102 // get Arguments for script
103 inArgs = aScriptEvent.Arguments;
104
105 Reference< provider::XScript > xScript = xScriptProvider->getScript( aScriptEvent.ScriptCode );
106
107 if ( !xScript.is() )
108 {
109 SAL_INFO("basic", "Failed to Failed to obtain XScript");
110 return;
111 }
112
113 Any result = xScript->invoke( inArgs, outIndex, outArgs );
114 if ( pRet )
115 {
116 *pRet = result;
117 }
118 }
119 catch ( const RuntimeException& )
120 {
121 TOOLS_INFO_EXCEPTION("basic", "" );
122 }
123 catch ( const Exception& )
124 {
125 TOOLS_INFO_EXCEPTION("basic", "" );
126 }
127
128}
129
130
131class BasicScriptListener_Impl : public WeakImplHelper< XScriptListener >
132{
133 StarBASICRef maBasicRef;
134 Reference< frame::XModel > m_xModel;
135
136 void firing_impl(const ScriptEvent& aScriptEvent, Any* pRet);
137
138public:
139 BasicScriptListener_Impl( StarBASIC* pBasic, const Reference< frame::XModel >& xModel )
140 : maBasicRef( pBasic ), m_xModel( xModel ) {}
141
142 // Methods of XAllListener
143 virtual void SAL_CALL firing(const ScriptEvent& aScriptEvent) override;
144 virtual Any SAL_CALL approveFiring(const ScriptEvent& aScriptEvent) override;
145
146 // Methods of XEventListener
147 virtual void SAL_CALL disposing(const EventObject& Source) override;
148};
149
150// Methods XAllListener
151void BasicScriptListener_Impl::firing( const ScriptEvent& aScriptEvent )
152{
154
155 firing_impl( aScriptEvent, nullptr );
156}
157
158Any BasicScriptListener_Impl::approveFiring( const ScriptEvent& aScriptEvent )
159{
161
162 Any aRetAny;
163 firing_impl( aScriptEvent, &aRetAny );
164 return aRetAny;
165}
166
167// Methods XEventListener
168void BasicScriptListener_Impl::disposing(const EventObject& )
169{
170 // TODO: ???
171 //SolarMutexGuard aGuard;
172 //xSbxObj.Clear();
173}
174
175
176void BasicScriptListener_Impl::firing_impl( const ScriptEvent& aScriptEvent, Any* pRet )
177{
178 if( aScriptEvent.ScriptType == "StarBasic" )
179 {
180 // Full qualified name?
181 OUString aMacro( aScriptEvent.ScriptCode );
182 OUString aLibName;
183 OUString aLocation;
184 if( comphelper::string::getTokenCount(aMacro, '.') == 3 )
185 {
186 sal_Int32 nLast = 0;
187 std::u16string_view aFullLibName = o3tl::getToken(aMacro, 0, '.', nLast );
188
189 size_t nIndex = aFullLibName.find( ':' );
190 if (nIndex != std::u16string_view::npos)
191 {
192 aLocation = aFullLibName.substr( 0, nIndex );
193 aLibName = aFullLibName.substr( nIndex + 1 );
194 }
195
196 aMacro = aMacro.copy( nLast );
197 }
198
199 SbxObject* p = maBasicRef.get();
200 SbxObject* pParent = p->GetParent();
201 SbxObject* pParentParent = pParent ? pParent->GetParent() : nullptr;
202
203 StarBASICRef xAppStandardBasic;
204 StarBASICRef xDocStandardBasic;
205 if( pParentParent )
206 {
207 // Own basic must be document library
208 xAppStandardBasic = static_cast<StarBASIC*>(pParentParent);
209 xDocStandardBasic = static_cast<StarBASIC*>(pParent);
210 }
211 else if( pParent )
212 {
213 OUString aName = p->GetName();
214 if( aName == "Standard" )
215 {
216 // Own basic is doc standard lib
217 xDocStandardBasic = static_cast<StarBASIC*>(p);
218 }
219 xAppStandardBasic = static_cast<StarBASIC*>(pParent);
220 }
221 else
222 {
223 xAppStandardBasic = static_cast<StarBASIC*>(p);
224 }
225
226 bool bSearchLib = true;
227 StarBASICRef xLibSearchBasic;
228 if( aLocation == "application" )
229 {
230 xLibSearchBasic = xAppStandardBasic;
231 }
232 else if( aLocation == "document" )
233 {
234 xLibSearchBasic = xDocStandardBasic;
235 }
236 else
237 {
238 bSearchLib = false;
239 }
240 SbxVariable* pMethVar = nullptr;
241 // Be still tolerant and make default search if no search basic exists
242 if( bSearchLib && xLibSearchBasic.is() )
243 {
244 sal_Int32 nCount = xLibSearchBasic->GetObjects()->Count();
245 for( sal_Int32 nObj = -1; nObj < nCount ; nObj++ )
246 {
247 StarBASIC* pBasic;
248 if( nObj == -1 )
249 {
250 pBasic = xLibSearchBasic.get();
251 }
252 else
253 {
254 SbxVariable* pVar = xLibSearchBasic->GetObjects()->Get(nObj);
255 pBasic = dynamic_cast<StarBASIC*>( pVar );
256 }
257 if( pBasic )
258 {
259 OUString aName = pBasic->GetName();
260 if( aName == aLibName )
261 {
262 // Search only in the lib, not automatically in application basic
263 SbxFlagBits nFlags = pBasic->GetFlags();
265 pMethVar = pBasic->Find( aMacro, SbxClassType::DontCare );
266 pBasic->SetFlags( nFlags );
267 break;
268 }
269 }
270 }
271 }
272
273 // Default: Be tolerant and search everywhere
274 if( (!pMethVar || dynamic_cast<const SbMethod*>( pMethVar) == nullptr) && maBasicRef.is() )
275 {
276 pMethVar = maBasicRef->FindQualified( aMacro, SbxClassType::DontCare );
277 }
278 SbMethod* pMeth = dynamic_cast<SbMethod*>( pMethVar );
279 if( !pMeth )
280 {
281 return;
282 }
283 // Setup parameters
284 SbxArrayRef xArray;
285 sal_Int32 nCnt = aScriptEvent.Arguments.getLength();
286 if( nCnt )
287 {
288 xArray = new SbxArray;
289 const Any *pArgs = aScriptEvent.Arguments.getConstArray();
290 for( sal_Int32 i = 0; i < nCnt; i++ )
291 {
293 unoToSbxValue( xVar.get(), pArgs[i] );
294 xArray->Put(xVar.get(), sal::static_int_cast<sal_uInt32>(i + 1));
295 }
296 }
297
298 // Call method
299 SbxVariableRef xValue = pRet ? new SbxVariable : nullptr;
300 if( xArray.is() )
301 {
302 pMeth->SetParameters( xArray.get() );
303 }
304 pMeth->Call( xValue.get() );
305 if( pRet )
306 {
307 *pRet = sbxToUnoValue( xValue.get() );
308 }
309 pMeth->SetParameters( nullptr );
310 }
311 else // scripting framework script
312 {
313 //callBasic via scripting framework
314 SFURL_firing_impl( aScriptEvent, pRet, m_xModel );
315 }
316}
317
318css::uno::Reference< css::container::XNameContainer > implFindDialogLibForDialog( const Any& rDlgAny, SbxObject* pBasic )
319{
320 css::uno::Reference< css::container::XNameContainer > aRetDlgLib;
321
322 SbxVariable* pDlgLibContVar = pBasic->Find("DialogLibraries", SbxClassType::Object);
323 if( auto pDlgLibContUnoObj = dynamic_cast<SbUnoObject*>( pDlgLibContVar) )
324 {
325 Any aDlgLibContAny = pDlgLibContUnoObj->getUnoAny();
326
327 Reference< XLibraryContainer > xDlgLibContNameAccess( aDlgLibContAny, UNO_QUERY );
328 OSL_ENSURE( xDlgLibContNameAccess.is(), "implFindDialogLibForDialog: no lib container for the given dialog!" );
329 if( xDlgLibContNameAccess.is() )
330 {
331 Sequence< OUString > aLibNames = xDlgLibContNameAccess->getElementNames();
332 const OUString* pLibNames = aLibNames.getConstArray();
333 sal_Int32 nLibNameCount = aLibNames.getLength();
334
335 for( sal_Int32 iLib = 0 ; iLib < nLibNameCount ; iLib++ )
336 {
337 if ( !xDlgLibContNameAccess->isLibraryLoaded( pLibNames[ iLib ] ) )
338 // if the library isn't loaded, then the dialog cannot originate from this lib
339 continue;
340
341 Any aDlgLibAny = xDlgLibContNameAccess->getByName( pLibNames[ iLib ] );
342
343 Reference< XNameContainer > xDlgLibNameCont( aDlgLibAny, UNO_QUERY );
344 OSL_ENSURE( xDlgLibNameCont.is(), "implFindDialogLibForDialog: invalid dialog lib!" );
345 if( xDlgLibNameCont.is() )
346 {
347 Sequence< OUString > aDlgNames = xDlgLibNameCont->getElementNames();
348 const OUString* pDlgNames = aDlgNames.getConstArray();
349 sal_Int32 nDlgNameCount = aDlgNames.getLength();
350
351 for( sal_Int32 iDlg = 0 ; iDlg < nDlgNameCount ; iDlg++ )
352 {
353 Any aDlgAny = xDlgLibNameCont->getByName( pDlgNames[ iDlg ] );
354 if( aDlgAny == rDlgAny )
355 {
356 aRetDlgLib = xDlgLibNameCont;
357 break;
358 }
359 }
360 }
361 }
362 }
363 }
364
365 return aRetDlgLib;
366}
367
368css::uno::Reference< css::container::XNameContainer > implFindDialogLibForDialogBasic( const Any& aAnyISP, SbxObject* pBasic, StarBASIC*& pFoundBasic )
369{
370 css::uno::Reference< css::container::XNameContainer > aDlgLib;
371 // Find dialog library for dialog, direct access is not possible here
372 StarBASIC* pStartedBasic = static_cast<StarBASIC*>(pBasic);
373 SbxObject* pParentBasic = pStartedBasic ? pStartedBasic->GetParent() : nullptr;
374 SbxObject* pParentParentBasic = pParentBasic ? pParentBasic->GetParent() : nullptr;
375
376 SbxObject* pSearchBasic1 = nullptr;
377 SbxObject* pSearchBasic2 = nullptr;
378 if( pParentParentBasic )
379 {
380 pSearchBasic1 = pParentBasic;
381 pSearchBasic2 = pParentParentBasic;
382 }
383 else
384 {
385 pSearchBasic1 = pStartedBasic;
386 pSearchBasic2 = pParentBasic;
387 }
388 if( pSearchBasic1 )
389 {
390 aDlgLib = implFindDialogLibForDialog( aAnyISP, pSearchBasic1 );
391
392 if ( aDlgLib.is() )
393 pFoundBasic = static_cast<StarBASIC*>(pSearchBasic1);
394
395 else if( pSearchBasic2 )
396 {
397 aDlgLib = implFindDialogLibForDialog( aAnyISP, pSearchBasic2 );
398 if ( aDlgLib.is() )
399 pFoundBasic = static_cast<StarBASIC*>(pSearchBasic2);
400 }
401 }
402 return aDlgLib;
403}
404
405}
406
408{
409 Reference< XComponentContext > xContext( comphelper::getProcessComponentContext() );
410
411 // We need at least 1 parameter
412 if (rPar.Count() < 2)
413 {
415 return;
416 }
417
418 // Get dialog
419 SbxBaseRef pObj = rPar.Get(1)->GetObject();
420 SbUnoObject* pUnoObj = dynamic_cast<SbUnoObject*>(pObj.get());
421 if( !pUnoObj )
422 {
424 return;
425 }
426 Any aAnyISP = pUnoObj->getUnoAny();
427 TypeClass eType = aAnyISP.getValueType().getTypeClass();
428
429 if( eType != TypeClass_INTERFACE )
430 {
432 return;
433 }
434
435 // Create new uno dialog
436 Reference< XNameContainer > xDialogModel( xContext->getServiceManager()->createInstanceWithContext(
437 "com.sun.star.awt.UnoControlDialogModel", xContext), UNO_QUERY );
438 if( !xDialogModel.is() )
439 {
440 return;
441 }
442 Reference< XInputStreamProvider > xISP;
443 aAnyISP >>= xISP;
444 if( !xISP.is() )
445 {
446 return;
447 }
448
449 // Import the DialogModel
450 Reference< XInputStream > xInput( xISP->createInputStream() );
451
452 // i83963 Force decoration
453 uno::Reference< beans::XPropertySet > xDlgModPropSet( xDialogModel, uno::UNO_QUERY );
454 if( xDlgModPropSet.is() )
455 {
456 try
457 {
458 bool bDecoration = true;
459 OUString aDecorationPropName("Decoration");
460 Any aDecorationAny = xDlgModPropSet->getPropertyValue( aDecorationPropName );
461 aDecorationAny >>= bDecoration;
462 if( !bDecoration )
463 {
464 xDlgModPropSet->setPropertyValue( aDecorationPropName, Any( true ) );
465 xDlgModPropSet->setPropertyValue( "Title", Any( OUString() ) );
466 }
467 }
468 catch(const UnknownPropertyException& )
469 {}
470 }
471
472 css::uno::Reference< css::container::XNameContainer > aDlgLib;
473 bool bDocDialog = false;
474 StarBASIC* pFoundBasic = nullptr;
475 SAL_INFO("basic", "About to try get a hold of ThisComponent");
476 Reference< frame::XModel > xModel = StarBASIC::GetModelFromBasic( GetSbData()->pInst->GetBasic() ) ;
477 aDlgLib = implFindDialogLibForDialogBasic( aAnyISP, GetSbData()->pInst->GetBasic(), pFoundBasic );
478 // If we found the dialog then it belongs to the Search basic
479 if ( !pFoundBasic )
480 {
481 Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( xContext );
482 Reference< container::XEnumeration > xModels;
483 Reference< container::XEnumerationAccess > xComponents = xDesktop->getComponents();
484 if ( xComponents.is() )
485 {
486 xModels = xComponents->createEnumeration();
487 }
488 if ( xModels.is() )
489 {
490 while ( xModels->hasMoreElements() )
491 {
492 Reference< frame::XModel > xNextModel( xModels->nextElement(), UNO_QUERY );
493 if ( xNextModel.is() )
494 {
496 if ( pMgr )
497 {
498 aDlgLib = implFindDialogLibForDialogBasic( aAnyISP, pMgr->GetLib(0), pFoundBasic );
499 }
500 if ( aDlgLib.is() )
501 {
502 bDocDialog = true;
503 xModel = xNextModel;
504 break;
505 }
506 }
507 }
508 }
509 }
510 if ( pFoundBasic )
511 {
512 bDocDialog = pFoundBasic->IsDocBasic();
513 }
514 Reference< XScriptListener > xScriptListener = new BasicScriptListener_Impl( GetSbData()->pInst->GetBasic(), xModel );
515
516 // Create a "living" Dialog
517 Reference< XControl > xCntrl;
518 try
519 {
520 Reference< XDialogProvider > xDlgProv;
521 if( bDocDialog )
522 xDlgProv = css::awt::DialogProvider::createWithModelAndScripting( xContext, xModel, xInput, aDlgLib, xScriptListener );
523 else
524 xDlgProv = css::awt::DialogProvider::createWithModelAndScripting( xContext, uno::Reference< frame::XModel >(), xInput, aDlgLib, xScriptListener );
525
526 xCntrl.set( xDlgProv->createDialog(OUString() ), UNO_QUERY_THROW );
527 // Add dialog model to dispose vector
528 Reference< XComponent > xDlgComponent( xCntrl->getModel(), UNO_QUERY );
529 GetSbData()->pInst->getComponentVector().push_back( xDlgComponent );
530 // need ThisComponent from calling script
531 }
532 // preserve existing bad behaviour, it's possible... but probably
533 // illegal to open 2 dialogs ( they ARE modal ) when this happens, sometimes
534 // create dialog fails. So, in this case let's not throw, just leave basic
535 // detect the unset object.
536 catch(const uno::Exception& )
537 {
538 }
539
540 // Return dialog
541 Any aRetVal;
542 aRetVal <<= xCntrl;
543 SbxVariableRef refVar = rPar.Get(0);
544 unoToSbxValue( refVar.get(), aRetVal );
545}
546
547
548/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
StarBASIC * GetLib(sal_uInt16 nLib) const
Definition: basmgr.cxx:1169
ErrCode Call(SbxValue *pRet, SbxVariable *pCaller=nullptr)
Definition: sbxmod.cxx:2054
css::uno::Any getUnoAny()
Definition: sbunoobj.cxx:2823
ComponentVector_t & getComponentVector()
Definition: runtime.hxx:182
Definition: sbx.hxx:95
sal_uInt32 Count() const
Definition: sbxarray.cxx:87
SbxVariable * Get(sal_uInt32)
Definition: sbxarray.cxx:108
void SetFlags(SbxFlagBits n)
Definition: sbxcore.hxx:102
SbxFlagBits GetFlags() const
Definition: sbxcore.hxx:105
void ResetFlag(SbxFlagBits n)
Definition: sbxcore.hxx:111
virtual SbxVariable * Find(const OUString &, SbxClassType)
Definition: sbxobj.cxx:182
SbxBase * GetObject() const
Definition: sbxvar.hxx:157
const SbxObject * GetParent() const
Definition: sbxvar.hxx:296
void SetParameters(SbxArray *p)
Definition: sbxvar.cxx:178
const OUString & GetName(SbxNameType=SbxNameType::NONE) const
Definition: sbxvar.cxx:199
virtual SbxVariable * Find(const OUString &, SbxClassType) override
Definition: sb.cxx:1248
static css::uno::Reference< css::frame::XModel > GetModelFromBasic(SbxObject *pBasic)
Definition: sb.cxx:1886
static void Error(ErrCode, const OUString &rMsg={})
Definition: sb.cxx:1683
bool IsDocBasic() const
Definition: sbstar.hxx:139
static BasicManager * getDocumentBasicManager(const css::uno::Reference< css::frame::XModel > &_rxDocumentModel)
returns the BasicManager belonging to the given document
T * get() const
bool is() const
int nCount
#define TOOLS_INFO_EXCEPTION(area, stream)
Reference< frame::XModel > m_xModel
void RTL_Impl_CreateUnoDialog(SbxArray &rPar)
Definition: eventatt.cxx:407
Reference< XSingleServiceFactory > xFactory
DocumentType eType
sal_Int32 nIndex
OUString aName
void * p
#define SAL_INFO(area, stream)
constexpr OUStringLiteral aDecorationPropName
sal_Int32 getTokenCount(std::string_view rIn, char cTok)
Reference< XComponentContext > getProcessComponentContext()
int i
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
#define ERRCODE_BASIC_BAD_ARGUMENT
Definition: sberrors.hxx:26
SbiGlobals * GetSbData()
Definition: sbintern.cxx:26
void unoToSbxValue(SbxVariable *pVar, const Any &aValue)
Definition: sbunoobj.cxx:610
Any sbxToUnoValue(const SbxValue *pVar)
Definition: sbunoobj.cxx:1137
@ SbxVARIANT
Definition: sbxdef.hxx:51
SbxFlagBits
Definition: sbxdef.hxx:131
SbiInstance * pInst
Definition: sbintern.hxx:108
Reference< XModel > xModel
Any result