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