LibreOffice Module dbaccess (master) 1
documenteventexecutor.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
21
22#include <com/sun/star/document/XDocumentEventBroadcaster.hpp>
23#include <com/sun/star/util/URLTransformer.hpp>
24#include <com/sun/star/util/XURLTransformer.hpp>
25#include <com/sun/star/frame/XModel.hpp>
26#include <com/sun/star/frame/XDispatchProvider.hpp>
27
31#include <vcl/svapp.hxx>
32
33namespace dbaccess
34{
35
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::UNO_QUERY;
38 using ::com::sun::star::uno::UNO_QUERY_THROW;
39 using ::com::sun::star::uno::UNO_SET_THROW;
40 using ::com::sun::star::uno::Exception;
41 using ::com::sun::star::uno::RuntimeException;
42 using ::com::sun::star::uno::Sequence;
43 using ::com::sun::star::uno::WeakReference;
44 using ::com::sun::star::uno::XComponentContext;
45 using ::com::sun::star::document::XDocumentEventBroadcaster;
46 using ::com::sun::star::document::XEventsSupplier;
47 using ::com::sun::star::container::XNameAccess;
48 using ::com::sun::star::frame::XModel;
49 using ::com::sun::star::util::URLTransformer;
50 using ::com::sun::star::util::XURLTransformer;
51 using ::com::sun::star::frame::XDispatchProvider;
52 using ::com::sun::star::frame::XDispatch;
53 using ::com::sun::star::util::URL;
54 using ::com::sun::star::beans::PropertyValue;
55 using ::com::sun::star::frame::XController;
56 using ::com::sun::star::document::DocumentEvent;
57
58 using namespace ::com::sun::star;
59
60 namespace
61 {
62 void lcl_dispatchScriptURL_throw(
63 css::uno::WeakReference< css::document::XEventsSupplier > const & xWeakDocument,
64 css::uno::Reference< css::util::XURLTransformer > const & xURLTransformer,
65 const OUString& _rScriptURL, const DocumentEvent& _rTrigger )
66 {
67 Reference< XModel > xDocument( xWeakDocument.get(), UNO_QUERY_THROW );
68
69 Reference< XController > xController( xDocument->getCurrentController() );
70 Reference< XDispatchProvider > xDispProv;
71 if ( xController.is() )
72 xDispProv.set( xController->getFrame(), UNO_QUERY );
73 if ( !xDispProv.is() )
74 {
75 OSL_FAIL( "lcl_dispatchScriptURL_throw: no controller/frame? How should I dispatch?" );
76 return;
77 }
78
79 URL aScriptURL;
80 aScriptURL.Complete = _rScriptURL;
81 if ( xURLTransformer.is() )
82 xURLTransformer->parseStrict( aScriptURL );
83
84 // unfortunately, executing a script can trigger all kind of complex stuff, and unfortunately, not
85 // every component involved into this properly cares for thread safety. To be on the safe side,
86 // we lock the solar mutex here.
87 SolarMutexGuard aSolarGuard;
88
89 Reference< XDispatch > xDispatch( xDispProv->queryDispatch( aScriptURL, OUString(), 0 ) );
90 if ( !xDispatch.is() )
91 {
92 OSL_FAIL( "lcl_dispatchScriptURL_throw: no dispatcher for the script URL!" );
93 return;
94 }
95
96 PropertyValue aEventParam;
97 aEventParam.Value <<= _rTrigger;
98 Sequence< PropertyValue > aDispatchArgs( &aEventParam, 1 );
99 xDispatch->dispatch( aScriptURL, aDispatchArgs );
100 }
101 }
102
103 // DocumentEventExecutor
104 DocumentEventExecutor::DocumentEventExecutor( const Reference<XComponentContext> & _rContext,
105 const Reference< XEventsSupplier >& _rxDocument )
106 :mxDocument( _rxDocument )
107 {
108 Reference< XDocumentEventBroadcaster > xBroadcaster( _rxDocument, UNO_QUERY_THROW );
109
110 osl_atomic_increment( &m_refCount );
111 {
112 xBroadcaster->addDocumentEventListener( this );
113 }
114 osl_atomic_decrement( &m_refCount );
115
116 try
117 {
118 mxURLTransformer = URLTransformer::create(_rContext);
119 }
120 catch( const Exception& )
121 {
122 DBG_UNHANDLED_EXCEPTION("dbaccess");
123 }
124 }
125
127 {
128 }
129
130 void SAL_CALL DocumentEventExecutor::documentEventOccured( const DocumentEvent& Event )
131 {
132 Reference< XEventsSupplier > xEventsSupplier( mxDocument.get(), UNO_QUERY );
133 if ( !xEventsSupplier )
134 {
135 OSL_FAIL( "DocumentEventExecutor::documentEventOccurred: no document anymore, but still being notified?" );
136 return;
137 }
138
139 Reference< XModel > xDocument( xEventsSupplier, UNO_QUERY_THROW );
140
141 try
142 {
143 Reference< XNameAccess > xDocEvents( xEventsSupplier->getEvents(), UNO_SET_THROW );
144 if ( !xDocEvents->hasByName( Event.EventName ) )
145 {
146 // this is worth an assertion: We are listener at the very same document which we just asked
147 // for its events. So when EventName is fired, why isn't it supported by xDocEvents?
148 OSL_FAIL( "DocumentEventExecutor::documentEventOccurred: an unsupported event is notified!" );
149 return;
150 }
151
152 const ::comphelper::NamedValueCollection aScriptDescriptor( xDocEvents->getByName( Event.EventName ) );
153
154 OUString sEventType;
155 bool bScriptAssigned = aScriptDescriptor.get_ensureType( "EventType", sEventType );
156
157 OUString sScript;
158 bScriptAssigned = bScriptAssigned && aScriptDescriptor.get_ensureType( "Script", sScript );
159
160 if ( !bScriptAssigned )
161 // no script is assigned to this event
162 return;
163
164 bool bDispatchScriptURL = ( sEventType == "Script" || sEventType == "Service" );
165 bool bNonEmptyScript = !sScript.isEmpty();
166
167 OSL_ENSURE( bDispatchScriptURL && bNonEmptyScript,
168 "DocumentEventExecutor::documentEventOccurred: invalid/unsupported script descriptor" );
169
170 if ( bDispatchScriptURL && bNonEmptyScript )
171 {
172 lcl_dispatchScriptURL_throw( mxDocument, mxURLTransformer, sScript, Event );
173 }
174 }
175 catch( const RuntimeException& ) { throw; }
176 catch( const Exception& )
177 {
178 DBG_UNHANDLED_EXCEPTION("dbaccess");
179 }
180 }
181
182 void SAL_CALL DocumentEventExecutor::disposing( const lang::EventObject& /*_Source*/ )
183 {
184 // not interested in
185 }
186
187} // namespace dbaccess
188
189/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
css::uno::WeakReference< css::document::XEventsSupplier > mxDocument
DocumentEventExecutor(const css::uno::Reference< css::uno::XComponentContext > &_rContext, const css::uno::Reference< css::document::XEventsSupplier > &_rxDocument)
css::uno::Reference< css::util::XURLTransformer > mxURLTransformer
virtual void SAL_CALL documentEventOccured(const css::document::DocumentEvent &Event) override
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XDispatch > xDispatch
ULONG m_refCount
@ Exception
Reference< XController > xController
the controller of the sub component. Must not be <NULL>
constexpr OUStringLiteral sScript
constexpr OUStringLiteral sEventType