Debian Codegraph @

basic/source/classes/eventatt.cxx

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