Branch data Line data Source code
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 "dlgevtatt.hxx"
21 : :
22 : : #include "dlgprov.hxx"
23 : :
24 : : #include <sfx2/sfx.hrc>
25 : : #include <sfx2/app.hxx>
26 : : #include <vcl/msgbox.hxx>
27 : : #include <tools/diagnose_ex.h>
28 : :
29 : : #include <com/sun/star/awt/XControl.hpp>
30 : : #include <com/sun/star/awt/XControlContainer.hpp>
31 : : #include <com/sun/star/awt/XDialogEventHandler.hpp>
32 : : #include <com/sun/star/awt/XContainerWindowEventHandler.hpp>
33 : : #include <com/sun/star/beans/XPropertySet.hpp>
34 : : #include <com/sun/star/script/ScriptEventDescriptor.hpp>
35 : : #include <com/sun/star/script/XScriptEventsSupplier.hpp>
36 : : #include <com/sun/star/script/provider/XScriptProvider.hpp>
37 : : #include <com/sun/star/script/provider/XScriptProviderFactory.hpp>
38 : : #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
39 : : #include <com/sun/star/script/vba/XVBACompatibility.hpp>
40 : : #include <com/sun/star/lang/NoSuchMethodException.hpp>
41 : : #include <com/sun/star/reflection/XIdlMethod.hpp>
42 : : #include <com/sun/star/beans/MethodConcept.hpp>
43 : : #include <com/sun/star/beans/XMaterialHolder.hpp>
44 : :
45 : : #include <ooo/vba/XVBAToOOEventDescGen.hpp>
46 : :
47 : : using namespace ::com::sun::star;
48 : : using namespace ::com::sun::star::awt;
49 : : using namespace ::com::sun::star::beans;
50 : : using namespace ::com::sun::star::lang;
51 : : using namespace ::com::sun::star::script;
52 : : using namespace ::com::sun::star::uno;
53 : : using namespace ::com::sun::star::script;
54 : : using namespace ::com::sun::star::reflection;
55 : :
56 : :
57 : : //.........................................................................
58 : : namespace dlgprov
59 : : {
60 : :
61 : 0 : class DialogSFScriptListenerImpl : public DialogScriptListenerImpl
62 : : {
63 : : protected:
64 : : Reference< frame::XModel > m_xModel;
65 : : virtual void firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* pRet );
66 : : public:
67 : 0 : DialogSFScriptListenerImpl( const Reference< XComponentContext >& rxContext, const Reference< frame::XModel >& rxModel ) : DialogScriptListenerImpl( rxContext ), m_xModel( rxModel ) {}
68 : : };
69 : :
70 : 0 : class DialogLegacyScriptListenerImpl : public DialogSFScriptListenerImpl
71 : : {
72 : : protected:
73 : : virtual void firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* pRet );
74 : : public:
75 : 0 : DialogLegacyScriptListenerImpl( const Reference< XComponentContext >& rxContext, const Reference< frame::XModel >& rxModel ) : DialogSFScriptListenerImpl( rxContext, rxModel ){}
76 : : };
77 : :
78 : 0 : class DialogUnoScriptListenerImpl : public DialogSFScriptListenerImpl
79 : : {
80 : : Reference< awt::XControl > m_xControl;
81 : : Reference< XInterface > m_xHandler;
82 : : Reference< beans::XIntrospectionAccess > m_xIntrospectionAccess;
83 : : bool m_bDialogProviderMode;
84 : :
85 : : virtual void firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* pRet );
86 : :
87 : : public:
88 : : DialogUnoScriptListenerImpl( const Reference< XComponentContext >& rxContext,
89 : : const Reference< frame::XModel >& rxModel,
90 : : const Reference< awt::XControl >& rxControl,
91 : : const Reference< XInterface >& rxHandler,
92 : : const Reference< beans::XIntrospectionAccess >& rxIntrospectionAccess,
93 : : bool bDialogProviderMode ); // false: ContainerWindowProvider mode
94 : :
95 : : };
96 : :
97 : 0 : class DialogVBAScriptListenerImpl : public DialogScriptListenerImpl
98 : : {
99 : : protected:
100 : : rtl::OUString msDialogCodeName;
101 : : rtl::OUString msDialogLibName;
102 : : Reference< script::XScriptListener > mxListener;
103 : : virtual void firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* pRet );
104 : : public:
105 : : DialogVBAScriptListenerImpl( const Reference< XComponentContext >& rxContext, const Reference< awt::XControl >& rxControl, const Reference< frame::XModel >& xModel, const rtl::OUString& sDialogLibName );
106 : : };
107 : :
108 : 0 : DialogVBAScriptListenerImpl::DialogVBAScriptListenerImpl( const Reference< XComponentContext >& rxContext, const Reference< awt::XControl >& rxControl, const Reference< frame::XModel >& xModel, const rtl::OUString& sDialogLibName ) : DialogScriptListenerImpl( rxContext ), msDialogLibName( sDialogLibName )
109 : : {
110 : 0 : Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
111 : 0 : Sequence< Any > args(1);
112 : 0 : if ( xSMgr.is() )
113 : : {
114 : 0 : args[0] <<= xModel;
115 : 0 : mxListener = Reference< XScriptListener >( xSMgr->createInstanceWithArgumentsAndContext( ::rtl::OUString( "ooo.vba.EventListener" ), args, m_xContext ), UNO_QUERY );
116 : : }
117 : 0 : if ( rxControl.is() )
118 : : {
119 : : try
120 : : {
121 : 0 : Reference< XPropertySet > xProps( rxControl->getModel(), UNO_QUERY_THROW );
122 : 0 : xProps->getPropertyValue( rtl::OUString("Name" ) ) >>= msDialogCodeName;
123 : 0 : xProps.set( mxListener, UNO_QUERY_THROW );
124 : 0 : xProps->setPropertyValue( rtl::OUString("Model" ), args[ 0 ] );
125 : : }
126 : 0 : catch( const Exception& )
127 : : {
128 : : DBG_UNHANDLED_EXCEPTION();
129 : : }
130 : 0 : }
131 : :
132 : 0 : }
133 : :
134 : 0 : void DialogVBAScriptListenerImpl::firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* )
135 : : {
136 : 0 : if ( aScriptEvent.ScriptType == "VBAInterop" && mxListener.is() )
137 : : {
138 : 0 : ScriptEvent aScriptEventCopy( aScriptEvent );
139 : 0 : aScriptEventCopy.ScriptCode = msDialogLibName.concat( rtl::OUString( "." ) ).concat( msDialogCodeName );
140 : : try
141 : : {
142 : 0 : mxListener->firing( aScriptEventCopy );
143 : : }
144 : 0 : catch( const Exception& )
145 : : {
146 : : DBG_UNHANDLED_EXCEPTION();
147 : 0 : }
148 : : }
149 : 0 : }
150 : :
151 : : //.........................................................................
152 : :
153 : : // =============================================================================
154 : : // DialogEventsAttacherImpl
155 : : // =============================================================================
156 : :
157 : 0 : DialogEventsAttacherImpl::DialogEventsAttacherImpl( const Reference< XComponentContext >& rxContext, const Reference< frame::XModel >& rxModel, const Reference< awt::XControl >& rxControl, const Reference< XInterface >& rxHandler, const Reference< beans::XIntrospectionAccess >& rxIntrospect, bool bProviderMode, const Reference< script::XScriptListener >& rxRTLListener, const rtl::OUString& sDialogLibName )
158 : 0 : :mbUseFakeVBAEvents( false ), m_xContext( rxContext )
159 : : {
160 : : // key listeners by protocol when ScriptType = 'Script'
161 : : // otherwise key is the ScriptType e.g. StarBasic
162 : 0 : if ( rxRTLListener.is() ) // set up handler for RTL_BASIC
163 : 0 : listernersForTypes[ rtl::OUString("StarBasic") ] = rxRTLListener;
164 : : else
165 : 0 : listernersForTypes[ rtl::OUString("StarBasic") ] = new DialogLegacyScriptListenerImpl( rxContext, rxModel );
166 : : // handler for Script & ::rtl::OUString("vnd.sun.star.UNO:")
167 : 0 : listernersForTypes[ rtl::OUString("vnd.sun.star.UNO") ] = new DialogUnoScriptListenerImpl( rxContext, rxModel, rxControl, rxHandler, rxIntrospect, bProviderMode );
168 : 0 : listernersForTypes[ rtl::OUString("vnd.sun.star.script") ] = new DialogSFScriptListenerImpl( rxContext, rxModel );
169 : :
170 : : // determine the VBA compatibility mode from the Basic library container
171 : : try
172 : : {
173 : 0 : uno::Reference< beans::XPropertySet > xModelProps( rxModel, uno::UNO_QUERY_THROW );
174 : : uno::Reference< script::vba::XVBACompatibility > xVBACompat(
175 : 0 : xModelProps->getPropertyValue( ::rtl::OUString( "BasicLibraries" ) ), uno::UNO_QUERY_THROW );
176 : 0 : mbUseFakeVBAEvents = xVBACompat->getVBACompatibilityMode();
177 : : }
178 : 0 : catch( uno::Exception& )
179 : : {
180 : : }
181 : 0 : if ( mbUseFakeVBAEvents )
182 : 0 : listernersForTypes[ rtl::OUString("VBAInterop") ] = new DialogVBAScriptListenerImpl( rxContext, rxControl, rxModel, sDialogLibName );
183 : 0 : }
184 : :
185 : : // -----------------------------------------------------------------------------
186 : :
187 : 0 : DialogEventsAttacherImpl::~DialogEventsAttacherImpl()
188 : : {
189 : 0 : }
190 : :
191 : : // -----------------------------------------------------------------------------
192 : : Reference< script::XScriptListener >
193 : 0 : DialogEventsAttacherImpl::getScriptListenerForKey( const rtl::OUString& sKey ) throw ( RuntimeException )
194 : : {
195 : 0 : ListenerHash::iterator it = listernersForTypes.find( sKey );
196 : 0 : if ( it == listernersForTypes.end() )
197 : 0 : throw RuntimeException(); // more text info here please
198 : 0 : return it->second;
199 : : }
200 : 0 : Reference< XScriptEventsSupplier > DialogEventsAttacherImpl::getFakeVbaEventsSupplier( const Reference< XControl >& xControl, rtl::OUString& sControlName )
201 : : {
202 : 0 : Reference< XScriptEventsSupplier > xEventsSupplier;
203 : 0 : Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
204 : 0 : if ( xSMgr.is() )
205 : : {
206 : 0 : Reference< ooo::vba::XVBAToOOEventDescGen > xVBAToOOEvtDesc( xSMgr->createInstanceWithContext( ::rtl::OUString( "ooo.vba.VBAToOOEventDesc" ), m_xContext ), UNO_QUERY );
207 : 0 : if ( xVBAToOOEvtDesc.is() )
208 : 0 : xEventsSupplier.set( xVBAToOOEvtDesc->getEventSupplier( xControl, sControlName ), UNO_QUERY );
209 : :
210 : : }
211 : 0 : return xEventsSupplier;
212 : : }
213 : :
214 : : // -----------------------------------------------------------------------------
215 : 0 : void SAL_CALL DialogEventsAttacherImpl::attachEventsToControl( const Reference< XControl>& xControl, const Reference< XScriptEventsSupplier >& xEventsSupplier, const Any& Helper )
216 : : {
217 : 0 : if ( xEventsSupplier.is() )
218 : : {
219 : 0 : Reference< container::XNameContainer > xEventCont = xEventsSupplier->getEvents();
220 : :
221 : 0 : Reference< XControlModel > xControlModel = xControl->getModel();
222 : 0 : Reference< XPropertySet > xProps( xControlModel, uno::UNO_QUERY );
223 : 0 : rtl::OUString sName;
224 : 0 : xProps->getPropertyValue( rtl::OUString("Name") ) >>= sName;
225 : 0 : if ( xEventCont.is() )
226 : : {
227 : 0 : Sequence< ::rtl::OUString > aNames = xEventCont->getElementNames();
228 : 0 : const ::rtl::OUString* pNames = aNames.getConstArray();
229 : 0 : sal_Int32 nNameCount = aNames.getLength();
230 : :
231 : 0 : for ( sal_Int32 j = 0; j < nNameCount; ++j )
232 : : {
233 : 0 : ScriptEventDescriptor aDesc;
234 : :
235 : 0 : Any aElement = xEventCont->getByName( pNames[ j ] );
236 : 0 : aElement >>= aDesc;
237 : 0 : rtl::OUString sKey = aDesc.ScriptType;
238 : 0 : if ( aDesc.ScriptType == "Script" || aDesc.ScriptType == "UNO" )
239 : : {
240 : 0 : sal_Int32 nIndex = aDesc.ScriptCode.indexOf( ':' );
241 : 0 : sKey = aDesc.ScriptCode.copy( 0, nIndex );
242 : : }
243 : : Reference< XAllListener > xAllListener =
244 : 0 : new DialogAllListenerImpl( getScriptListenerForKey( sKey ), aDesc.ScriptType, aDesc.ScriptCode );
245 : :
246 : : // try first to attach event to the ControlModel
247 : 0 : bool bSuccess = false;
248 : : try
249 : : {
250 : 0 : Reference< XEventListener > xListener_ = m_xEventAttacher->attachSingleEventListener(
251 : : xControlModel, xAllListener, Helper, aDesc.ListenerType,
252 : 0 : aDesc.AddListenerParam, aDesc.EventMethod );
253 : :
254 : 0 : if ( xListener_.is() )
255 : 0 : bSuccess = true;
256 : : }
257 : 0 : catch ( const Exception& )
258 : : {
259 : : DBG_UNHANDLED_EXCEPTION();
260 : : }
261 : :
262 : : try
263 : : {
264 : : // if we had no success, try to attach to the control
265 : 0 : if ( !bSuccess )
266 : : {
267 : 0 : Reference< XEventListener > xListener_ = m_xEventAttacher->attachSingleEventListener(
268 : : xControl, xAllListener, Helper, aDesc.ListenerType,
269 : 0 : aDesc.AddListenerParam, aDesc.EventMethod );
270 : : }
271 : : }
272 : 0 : catch ( const Exception& )
273 : : {
274 : : DBG_UNHANDLED_EXCEPTION();
275 : : }
276 : 0 : }
277 : 0 : }
278 : : }
279 : 0 : }
280 : :
281 : :
282 : 0 : void DialogEventsAttacherImpl::nestedAttachEvents( const Sequence< Reference< XInterface > >& Objects, const Any& Helper, rtl::OUString& sDialogCodeName )
283 : : {
284 : 0 : const Reference< XInterface >* pObjects = Objects.getConstArray();
285 : 0 : sal_Int32 nObjCount = Objects.getLength();
286 : :
287 : 0 : for ( sal_Int32 i = 0; i < nObjCount; ++i )
288 : : {
289 : : // We know that we have to do with instances of XControl.
290 : : // Otherwise this is not the right implementation for
291 : : // XScriptEventsAttacher and we have to give up.
292 : 0 : Reference< XControl > xControl( pObjects[ i ], UNO_QUERY );
293 : 0 : Reference< XControlContainer > xControlContainer( xControl, UNO_QUERY );
294 : 0 : Reference< XDialog > xDialog( xControl, UNO_QUERY );
295 : 0 : if ( !xControl.is() )
296 : 0 : throw IllegalArgumentException();
297 : :
298 : : // get XEventsSupplier from control model
299 : 0 : Reference< XControlModel > xControlModel = xControl->getModel();
300 : 0 : Reference< XScriptEventsSupplier > xEventsSupplier( xControlModel, UNO_QUERY );
301 : 0 : attachEventsToControl( xControl, xEventsSupplier, Helper );
302 : 0 : if ( mbUseFakeVBAEvents )
303 : : {
304 : 0 : xEventsSupplier.set( getFakeVbaEventsSupplier( xControl, sDialogCodeName ) );
305 : 0 : Any newHelper(xControl );
306 : 0 : attachEventsToControl( xControl, xEventsSupplier, newHelper );
307 : : }
308 : 0 : if ( xControlContainer.is() && !xDialog.is() )
309 : : {
310 : 0 : Sequence< Reference< XControl > > aControls = xControlContainer->getControls();
311 : 0 : sal_Int32 nControlCount = aControls.getLength();
312 : :
313 : 0 : Sequence< Reference< XInterface > > aObjects( nControlCount );
314 : 0 : Reference< XInterface >* pObjectsModify = aObjects.getArray();
315 : 0 : const Reference< XControl >* pControls = aControls.getConstArray();
316 : :
317 : 0 : for ( sal_Int32 j = 0; j < nControlCount; ++j )
318 : : {
319 : 0 : pObjectsModify[j] = Reference< XInterface >( pControls[j], UNO_QUERY );
320 : : }
321 : 0 : nestedAttachEvents( aObjects, Helper, sDialogCodeName );
322 : : }
323 : 0 : }
324 : 0 : }
325 : :
326 : : // -----------------------------------------------------------------------------
327 : : // XScriptEventsAttacher
328 : : // -----------------------------------------------------------------------------
329 : :
330 : 0 : void SAL_CALL DialogEventsAttacherImpl::attachEvents( const Sequence< Reference< XInterface > >& Objects,
331 : : const com::sun::star::uno::Reference<com::sun::star::script::XScriptListener>&,
332 : : const Any& Helper )
333 : : throw (IllegalArgumentException, IntrospectionException, CannotCreateAdapterException,
334 : : ServiceNotRegisteredException, RuntimeException)
335 : : {
336 : : // get EventAttacher
337 : : {
338 : 0 : ::osl::MutexGuard aGuard( getMutex() );
339 : :
340 : 0 : if ( !m_xEventAttacher.is() )
341 : : {
342 : 0 : Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
343 : 0 : if ( xSMgr.is() )
344 : : {
345 : 0 : m_xEventAttacher = Reference< XEventAttacher >( xSMgr->createInstanceWithContext(
346 : 0 : ::rtl::OUString( "com.sun.star.script.EventAttacher" ), m_xContext ), UNO_QUERY );
347 : :
348 : 0 : if ( !m_xEventAttacher.is() )
349 : 0 : throw ServiceNotRegisteredException();
350 : : }
351 : : else
352 : : {
353 : 0 : throw RuntimeException();
354 : 0 : }
355 : :
356 : 0 : }
357 : : }
358 : 0 : rtl::OUString sDialogCodeName;
359 : 0 : sal_Int32 nObjCount = Objects.getLength();
360 : 0 : Reference< awt::XControl > xDlgControl( Objects[ nObjCount - 1 ], uno::UNO_QUERY ); // last object is the dialog
361 : 0 : if ( xDlgControl.is() )
362 : : {
363 : 0 : Reference< XPropertySet > xProps( xDlgControl->getModel(), UNO_QUERY );
364 : : try
365 : : {
366 : 0 : xProps->getPropertyValue( rtl::OUString("Name" ) ) >>= sDialogCodeName;
367 : : }
368 : 0 : catch( Exception& ){}
369 : : }
370 : : // go over all objects
371 : 0 : nestedAttachEvents( Objects, Helper, sDialogCodeName );
372 : 0 : }
373 : :
374 : :
375 : : // =============================================================================
376 : : // DialogAllListenerImpl
377 : : // =============================================================================
378 : :
379 : 0 : DialogAllListenerImpl::DialogAllListenerImpl( const Reference< XScriptListener >& rxListener,
380 : : const ::rtl::OUString& rScriptType, const ::rtl::OUString& rScriptCode )
381 : : :m_xScriptListener( rxListener )
382 : : ,m_sScriptType( rScriptType )
383 : 0 : ,m_sScriptCode( rScriptCode )
384 : : {
385 : 0 : }
386 : :
387 : : // -----------------------------------------------------------------------------
388 : :
389 : 0 : DialogAllListenerImpl::~DialogAllListenerImpl()
390 : : {
391 : 0 : }
392 : :
393 : : // -----------------------------------------------------------------------------
394 : :
395 : 0 : void DialogAllListenerImpl::firing_impl( const AllEventObject& Event, Any* pRet )
396 : : {
397 : 0 : ScriptEvent aScriptEvent;
398 : 0 : aScriptEvent.Source = (OWeakObject *)this; // get correct XInterface
399 : 0 : aScriptEvent.ListenerType = Event.ListenerType;
400 : 0 : aScriptEvent.MethodName = Event.MethodName;
401 : 0 : aScriptEvent.Arguments = Event.Arguments;
402 : 0 : aScriptEvent.Helper = Event.Helper;
403 : 0 : aScriptEvent.ScriptType = m_sScriptType;
404 : 0 : aScriptEvent.ScriptCode = m_sScriptCode;
405 : :
406 : 0 : if ( m_xScriptListener.is() )
407 : : {
408 : 0 : if ( pRet )
409 : 0 : *pRet = m_xScriptListener->approveFiring( aScriptEvent );
410 : : else
411 : 0 : m_xScriptListener->firing( aScriptEvent );
412 : 0 : }
413 : 0 : }
414 : :
415 : : // -----------------------------------------------------------------------------
416 : : // XEventListener
417 : : // -----------------------------------------------------------------------------
418 : :
419 : 0 : void DialogAllListenerImpl::disposing(const EventObject& ) throw ( RuntimeException )
420 : : {
421 : 0 : }
422 : :
423 : : // -----------------------------------------------------------------------------
424 : : // XAllListener
425 : : // -----------------------------------------------------------------------------
426 : :
427 : 0 : void DialogAllListenerImpl::firing( const AllEventObject& Event ) throw ( RuntimeException )
428 : : {
429 : : //::osl::MutexGuard aGuard( getMutex() );
430 : :
431 : 0 : firing_impl( Event, NULL );
432 : 0 : }
433 : :
434 : : // -----------------------------------------------------------------------------
435 : :
436 : 0 : Any DialogAllListenerImpl::approveFiring( const AllEventObject& Event )
437 : : throw ( reflection::InvocationTargetException, RuntimeException )
438 : : {
439 : : //::osl::MutexGuard aGuard( getMutex() );
440 : :
441 : 0 : Any aReturn;
442 : 0 : firing_impl( Event, &aReturn );
443 : 0 : return aReturn;
444 : : }
445 : :
446 : :
447 : : // =============================================================================
448 : : // DialogScriptListenerImpl
449 : : // =============================================================================
450 : :
451 : 0 : DialogUnoScriptListenerImpl::DialogUnoScriptListenerImpl( const Reference< XComponentContext >& rxContext,
452 : : const Reference< ::com::sun::star::frame::XModel >& rxModel,
453 : : const Reference< ::com::sun::star::awt::XControl >& rxControl,
454 : : const Reference< ::com::sun::star::uno::XInterface >& rxHandler,
455 : : const Reference< ::com::sun::star::beans::XIntrospectionAccess >& rxIntrospectionAccess,
456 : : bool bDialogProviderMode )
457 : : : DialogSFScriptListenerImpl( rxContext, rxModel )
458 : : ,m_xControl( rxControl )
459 : : ,m_xHandler( rxHandler )
460 : : ,m_xIntrospectionAccess( rxIntrospectionAccess )
461 : 0 : ,m_bDialogProviderMode( bDialogProviderMode )
462 : : {
463 : 0 : }
464 : :
465 : : // -----------------------------------------------------------------------------
466 : :
467 : 0 : DialogScriptListenerImpl::~DialogScriptListenerImpl()
468 : : {
469 : 0 : }
470 : :
471 : : // -----------------------------------------------------------------------------
472 : 0 : void DialogSFScriptListenerImpl::firing_impl( const ScriptEvent& aScriptEvent, Any* pRet )
473 : : {
474 : : try
475 : : {
476 : 0 : Reference< provider::XScriptProvider > xScriptProvider;
477 : 0 : if ( m_xModel.is() )
478 : : {
479 : 0 : Reference< provider::XScriptProviderSupplier > xSupplier( m_xModel, UNO_QUERY );
480 : : OSL_ENSURE( xSupplier.is(), "DialogScriptListenerImpl::firing_impl: failed to get script provider supplier" );
481 : 0 : if ( xSupplier.is() )
482 : 0 : xScriptProvider.set( xSupplier->getScriptProvider() );
483 : : }
484 : : else
485 : : {
486 : : OSL_ASSERT( m_xContext.is() );
487 : 0 : if ( m_xContext.is() )
488 : : {
489 : : Reference< provider::XScriptProviderFactory > xFactory(
490 : 0 : m_xContext->getValueByName(
491 : 0 : ::rtl::OUString("/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory") ),
492 : 0 : UNO_QUERY );
493 : : OSL_ENSURE( xFactory.is(), "SFURL_firing_impl: failed to get master script provider factory" );
494 : 0 : if ( xFactory.is() )
495 : : {
496 : 0 : Any aCtx;
497 : 0 : aCtx <<= ::rtl::OUString("user");
498 : 0 : xScriptProvider.set( xFactory->createScriptProvider( aCtx ), UNO_QUERY );
499 : 0 : }
500 : : }
501 : : }
502 : :
503 : : OSL_ENSURE( xScriptProvider.is(), "DialogScriptListenerImpl::firing_impl: failed to get script provider" );
504 : :
505 : 0 : if ( xScriptProvider.is() )
506 : : {
507 : 0 : Reference< provider::XScript > xScript = xScriptProvider->getScript( aScriptEvent.ScriptCode );
508 : : OSL_ENSURE( xScript.is(), "DialogScriptListenerImpl::firing_impl: failed to get script" );
509 : :
510 : 0 : if ( xScript.is() )
511 : : {
512 : 0 : Sequence< Any > aInParams;
513 : 0 : Sequence< sal_Int16 > aOutParamsIndex;
514 : 0 : Sequence< Any > aOutParams;
515 : :
516 : : // get arguments for script
517 : 0 : aInParams = aScriptEvent.Arguments;
518 : :
519 : 0 : Any aResult = xScript->invoke( aInParams, aOutParamsIndex, aOutParams );
520 : 0 : if ( pRet )
521 : 0 : *pRet = aResult;
522 : 0 : }
523 : 0 : }
524 : : }
525 : 0 : catch ( const Exception& )
526 : : {
527 : : DBG_UNHANDLED_EXCEPTION();
528 : : }
529 : 0 : }
530 : :
531 : 0 : void DialogLegacyScriptListenerImpl::firing_impl( const ScriptEvent& aScriptEvent, Any* pRet )
532 : : {
533 : 0 : ::rtl::OUString sScriptURL;
534 : 0 : ::rtl::OUString sScriptCode( aScriptEvent.ScriptCode );
535 : :
536 : 0 : if ( aScriptEvent.ScriptType.compareToAscii( "StarBasic" ) == 0 )
537 : : {
538 : : // StarBasic script: convert ScriptCode to scriptURL
539 : 0 : sal_Int32 nIndex = sScriptCode.indexOf( ':' );
540 : 0 : if ( nIndex >= 0 && nIndex < sScriptCode.getLength() )
541 : : {
542 : 0 : sScriptURL = ::rtl::OUString("vnd.sun.star.script:");
543 : 0 : sScriptURL += sScriptCode.copy( nIndex + 1 );
544 : 0 : sScriptURL += ::rtl::OUString("?language=Basic&location=");
545 : 0 : sScriptURL += sScriptCode.copy( 0, nIndex );
546 : : }
547 : 0 : ScriptEvent aSFScriptEvent( aScriptEvent );
548 : 0 : aSFScriptEvent.ScriptCode = sScriptURL;
549 : 0 : DialogSFScriptListenerImpl::firing_impl( aSFScriptEvent, pRet );
550 : 0 : }
551 : 0 : }
552 : :
553 : 0 : void DialogUnoScriptListenerImpl::firing_impl( const ScriptEvent& aScriptEvent, Any* pRet )
554 : : {
555 : 0 : static ::rtl::OUString sUnoURLScheme("vnd.sun.star.UNO:");
556 : :
557 : 0 : ::rtl::OUString sScriptCode( aScriptEvent.ScriptCode );
558 : 0 : ::rtl::OUString aMethodName = aScriptEvent.ScriptCode.copy( sUnoURLScheme.getLength() );
559 : :
560 : 0 : const Any* pArguments = aScriptEvent.Arguments.getConstArray();
561 : 0 : Any aEventObject = pArguments[0];
562 : :
563 : 0 : bool bHandled = false;
564 : 0 : if( m_xHandler.is() )
565 : : {
566 : 0 : if( m_bDialogProviderMode )
567 : : {
568 : 0 : Reference< XDialogEventHandler > xDialogEventHandler( m_xHandler, UNO_QUERY );
569 : 0 : if( xDialogEventHandler.is() )
570 : : {
571 : 0 : Reference< XDialog > xDialog( m_xControl, UNO_QUERY );
572 : 0 : bHandled = xDialogEventHandler->callHandlerMethod( xDialog, aEventObject, aMethodName );
573 : 0 : }
574 : : }
575 : : else
576 : : {
577 : 0 : Reference< XContainerWindowEventHandler > xContainerWindowEventHandler( m_xHandler, UNO_QUERY );
578 : 0 : if( xContainerWindowEventHandler.is() )
579 : : {
580 : 0 : Reference< XWindow > xWindow( m_xControl, UNO_QUERY );
581 : 0 : bHandled = xContainerWindowEventHandler->callHandlerMethod( xWindow, aEventObject, aMethodName );
582 : 0 : }
583 : : }
584 : : }
585 : :
586 : 0 : Any aRet;
587 : 0 : if( !bHandled && m_xIntrospectionAccess.is() )
588 : : {
589 : : try
590 : : {
591 : : // Methode ansprechen
592 : 0 : const Reference< XIdlMethod >& rxMethod = m_xIntrospectionAccess->
593 : 0 : getMethod( aMethodName, MethodConcept::ALL - MethodConcept::DANGEROUS );
594 : :
595 : : Reference< XMaterialHolder > xMaterialHolder =
596 : 0 : Reference< XMaterialHolder >::query( m_xIntrospectionAccess );
597 : 0 : Any aHandlerObject = xMaterialHolder->getMaterial();
598 : :
599 : 0 : Sequence< Reference< XIdlClass > > aParamTypeSeq = rxMethod->getParameterTypes();
600 : 0 : sal_Int32 nParamCount = aParamTypeSeq.getLength();
601 : 0 : if( nParamCount == 0 )
602 : : {
603 : 0 : Sequence<Any> args;
604 : 0 : rxMethod->invoke( aHandlerObject, args );
605 : 0 : bHandled = true;
606 : : }
607 : 0 : else if( nParamCount == 2 )
608 : : {
609 : : // Signature check automatically done by reflection
610 : 0 : Sequence<Any> Args(2);
611 : 0 : Any* pArgs = Args.getArray();
612 : 0 : if( m_bDialogProviderMode )
613 : : {
614 : 0 : Reference< XDialog > xDialog( m_xControl, UNO_QUERY );
615 : 0 : pArgs[0] <<= xDialog;
616 : : }
617 : : else
618 : : {
619 : 0 : Reference< XWindow > xWindow( m_xControl, UNO_QUERY );
620 : 0 : pArgs[0] <<= xWindow;
621 : : }
622 : 0 : pArgs[1] = aEventObject;
623 : 0 : aRet = rxMethod->invoke( aHandlerObject, Args );
624 : 0 : bHandled = true;
625 : 0 : }
626 : : }
627 : 0 : catch( const Exception& )
628 : : {
629 : : DBG_UNHANDLED_EXCEPTION();
630 : : }
631 : : }
632 : :
633 : 0 : if( bHandled )
634 : : {
635 : 0 : if( pRet )
636 : 0 : *pRet = aRet;
637 : : }
638 : : else
639 : : {
640 : 0 : ResMgr* pResMgr = SFX_APP()->GetSfxResManager();
641 : 0 : if( pResMgr )
642 : : {
643 : 0 : String aRes( ResId(STR_ERRUNOEVENTBINDUNG, *pResMgr) );
644 : 0 : ::rtl::OUString aQuoteChar( "\"" );
645 : :
646 : 0 : ::rtl::OUString aOURes = aRes;
647 : 0 : sal_Int32 nIndex = aOURes.indexOf( '%' );
648 : :
649 : 0 : ::rtl::OUString aOUFinal;
650 : 0 : aOUFinal += aOURes.copy( 0, nIndex );
651 : 0 : aOUFinal += aQuoteChar;
652 : 0 : aOUFinal += aMethodName;
653 : 0 : aOUFinal += aQuoteChar;
654 : 0 : aOUFinal += aOURes.copy( nIndex + 2 );
655 : :
656 : 0 : ErrorBox( NULL, WinBits( WB_OK ), aOUFinal ).Execute();
657 : : }
658 : 0 : }
659 : 0 : }
660 : :
661 : : // -----------------------------------------------------------------------------
662 : : // XEventListener
663 : : // -----------------------------------------------------------------------------
664 : :
665 : 0 : void DialogScriptListenerImpl::disposing(const EventObject& ) throw ( RuntimeException )
666 : : {
667 : 0 : }
668 : :
669 : : // -----------------------------------------------------------------------------
670 : : // XScriptListener
671 : : // -----------------------------------------------------------------------------
672 : :
673 : 0 : void DialogScriptListenerImpl::firing( const ScriptEvent& aScriptEvent ) throw ( RuntimeException )
674 : : {
675 : : //::osl::MutexGuard aGuard( getMutex() );
676 : :
677 : 0 : firing_impl( aScriptEvent, NULL );
678 : 0 : }
679 : :
680 : : // -----------------------------------------------------------------------------
681 : :
682 : 0 : Any DialogScriptListenerImpl::approveFiring( const ScriptEvent& aScriptEvent )
683 : : throw ( reflection::InvocationTargetException, RuntimeException )
684 : : {
685 : : //::osl::MutexGuard aGuard( getMutex() );
686 : :
687 : 0 : Any aReturn;
688 : 0 : firing_impl( aScriptEvent, &aReturn );
689 : 0 : return aReturn;
690 : : }
691 : :
692 : : // -----------------------------------------------------------------------------
693 : :
694 : : //.........................................................................
695 : : } // namespace dlgprov
696 : : //.........................................................................
697 : :
698 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|