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