1*96de5490SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*96de5490SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*96de5490SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*96de5490SAndrew Rist * distributed with this work for additional information 6*96de5490SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*96de5490SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*96de5490SAndrew Rist * "License"); you may not use this file except in compliance 9*96de5490SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*96de5490SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*96de5490SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*96de5490SAndrew Rist * software distributed under the License is distributed on an 15*96de5490SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*96de5490SAndrew Rist * KIND, either express or implied. See the License for the 17*96de5490SAndrew Rist * specific language governing permissions and limitations 18*96de5490SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*96de5490SAndrew Rist *************************************************************/ 21*96de5490SAndrew Rist 22*96de5490SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "documenteventexecutor.hxx" 28cdf0e10cSrcweir 29cdf0e10cSrcweir /** === begin UNO includes === **/ 30cdf0e10cSrcweir #include <com/sun/star/document/XDocumentEventBroadcaster.hpp> 31cdf0e10cSrcweir #include <com/sun/star/util/XURLTransformer.hpp> 32cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp> 33cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchProvider.hpp> 34cdf0e10cSrcweir /** === end UNO includes === **/ 35cdf0e10cSrcweir 36cdf0e10cSrcweir #include <comphelper/componentcontext.hxx> 37cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx> 38cdf0e10cSrcweir #include <cppuhelper/weakref.hxx> 39cdf0e10cSrcweir #include <tools/diagnose_ex.h> 40cdf0e10cSrcweir #include <vcl/svapp.hxx> 41cdf0e10cSrcweir #include <vos/mutex.hxx> 42cdf0e10cSrcweir 43cdf0e10cSrcweir //........................................................................ 44cdf0e10cSrcweir namespace dbaccess 45cdf0e10cSrcweir { 46cdf0e10cSrcweir //........................................................................ 47cdf0e10cSrcweir 48cdf0e10cSrcweir /** === begin UNO using === **/ 49cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 50cdf0e10cSrcweir using ::com::sun::star::uno::XInterface; 51cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 52cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW; 53cdf0e10cSrcweir using ::com::sun::star::uno::UNO_SET_THROW; 54cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 55cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 56cdf0e10cSrcweir using ::com::sun::star::uno::Any; 57cdf0e10cSrcweir using ::com::sun::star::uno::makeAny; 58cdf0e10cSrcweir using ::com::sun::star::uno::Sequence; 59cdf0e10cSrcweir using ::com::sun::star::uno::Type; 60cdf0e10cSrcweir using ::com::sun::star::uno::WeakReference; 61cdf0e10cSrcweir using ::com::sun::star::document::XDocumentEventBroadcaster; 62cdf0e10cSrcweir using ::com::sun::star::document::XEventsSupplier; 63cdf0e10cSrcweir using ::com::sun::star::container::XNameAccess; 64cdf0e10cSrcweir using ::com::sun::star::frame::XModel; 65cdf0e10cSrcweir using ::com::sun::star::util::XURLTransformer; 66cdf0e10cSrcweir using ::com::sun::star::frame::XDispatchProvider; 67cdf0e10cSrcweir using ::com::sun::star::frame::XDispatch; 68cdf0e10cSrcweir using ::com::sun::star::util::URL; 69cdf0e10cSrcweir using ::com::sun::star::beans::PropertyValue; 70cdf0e10cSrcweir using ::com::sun::star::frame::XController; 71cdf0e10cSrcweir using ::com::sun::star::document::DocumentEvent; 72cdf0e10cSrcweir /** === end UNO using === **/ 73cdf0e10cSrcweir using namespace ::com::sun::star; 74cdf0e10cSrcweir 75cdf0e10cSrcweir //==================================================================== 76cdf0e10cSrcweir //= DocumentEventExecutor_Data 77cdf0e10cSrcweir //==================================================================== 78cdf0e10cSrcweir struct DocumentEventExecutor_Data 79cdf0e10cSrcweir { 80cdf0e10cSrcweir WeakReference< XEventsSupplier > xDocument; 81cdf0e10cSrcweir Reference< XURLTransformer > xURLTransformer; 82cdf0e10cSrcweir DocumentEventExecutor_Datadbaccess::DocumentEventExecutor_Data83cdf0e10cSrcweir DocumentEventExecutor_Data( const Reference< XEventsSupplier >& _rxDocument ) 84cdf0e10cSrcweir :xDocument( _rxDocument ) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir } 87cdf0e10cSrcweir }; 88cdf0e10cSrcweir 89cdf0e10cSrcweir //-------------------------------------------------------------------- 90cdf0e10cSrcweir namespace 91cdf0e10cSrcweir { lcl_dispatchScriptURL_throw(DocumentEventExecutor_Data & _rDocExecData,const::rtl::OUString & _rScriptURL,const DocumentEvent & _rTrigger)92cdf0e10cSrcweir static void lcl_dispatchScriptURL_throw( DocumentEventExecutor_Data& _rDocExecData, 93cdf0e10cSrcweir const ::rtl::OUString& _rScriptURL, const DocumentEvent& _rTrigger ) 94cdf0e10cSrcweir { 95cdf0e10cSrcweir Reference< XModel > xDocument( _rDocExecData.xDocument.get(), UNO_QUERY_THROW ); 96cdf0e10cSrcweir 97cdf0e10cSrcweir Reference< XController > xController( xDocument->getCurrentController() ); 98cdf0e10cSrcweir Reference< XDispatchProvider > xDispProv; 99cdf0e10cSrcweir if ( xController.is() ) 100cdf0e10cSrcweir xDispProv.set( xController->getFrame(), UNO_QUERY ); 101cdf0e10cSrcweir if ( !xDispProv.is() ) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir OSL_ENSURE( false, "lcl_dispatchScriptURL_throw: no controller/frame? How should I dispatch?" ); 104cdf0e10cSrcweir return; 105cdf0e10cSrcweir } 106cdf0e10cSrcweir 107cdf0e10cSrcweir URL aScriptURL; 108cdf0e10cSrcweir aScriptURL.Complete = _rScriptURL; 109cdf0e10cSrcweir if ( _rDocExecData.xURLTransformer.is() ) 110cdf0e10cSrcweir _rDocExecData.xURLTransformer->parseStrict( aScriptURL ); 111cdf0e10cSrcweir 112cdf0e10cSrcweir // unfortunately, executing a script can trigger all kind of complex stuff, and unfortunately, not 113cdf0e10cSrcweir // every component involved into this properly cares for thread safety. To be on the safe side, 114cdf0e10cSrcweir // we lock the solar mutex here. 115cdf0e10cSrcweir ::vos::OGuard aSolarGuard( Application::GetSolarMutex() ); 116cdf0e10cSrcweir 117cdf0e10cSrcweir Reference< XDispatch > xDispatch( xDispProv->queryDispatch( aScriptURL, ::rtl::OUString(), 0 ) ); 118cdf0e10cSrcweir if ( !xDispatch.is() ) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir OSL_ENSURE( false, "lcl_dispatchScriptURL_throw: no dispatcher for the script URL!" ); 121cdf0e10cSrcweir return; 122cdf0e10cSrcweir } 123cdf0e10cSrcweir 124cdf0e10cSrcweir PropertyValue aEventParam; 125cdf0e10cSrcweir aEventParam.Value <<= _rTrigger; 126cdf0e10cSrcweir Sequence< PropertyValue > aDispatchArgs( &aEventParam, 1 ); 127cdf0e10cSrcweir xDispatch->dispatch( aScriptURL, aDispatchArgs ); 128cdf0e10cSrcweir } 129cdf0e10cSrcweir } 130cdf0e10cSrcweir 131cdf0e10cSrcweir //==================================================================== 132cdf0e10cSrcweir //= DocumentEventExecutor 133cdf0e10cSrcweir //==================================================================== 134cdf0e10cSrcweir //-------------------------------------------------------------------- DocumentEventExecutor(const::comphelper::ComponentContext & _rContext,const Reference<XEventsSupplier> & _rxDocument)135cdf0e10cSrcweir DocumentEventExecutor::DocumentEventExecutor( const ::comphelper::ComponentContext& _rContext, 136cdf0e10cSrcweir const Reference< XEventsSupplier >& _rxDocument ) 137cdf0e10cSrcweir :m_pData( new DocumentEventExecutor_Data( _rxDocument ) ) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir Reference< XDocumentEventBroadcaster > xBroadcaster( _rxDocument, UNO_QUERY_THROW ); 140cdf0e10cSrcweir 141cdf0e10cSrcweir osl_incrementInterlockedCount( &m_refCount ); 142cdf0e10cSrcweir { 143cdf0e10cSrcweir xBroadcaster->addDocumentEventListener( this ); 144cdf0e10cSrcweir } 145cdf0e10cSrcweir osl_decrementInterlockedCount( &m_refCount ); 146cdf0e10cSrcweir 147cdf0e10cSrcweir try 148cdf0e10cSrcweir { 149cdf0e10cSrcweir _rContext.createComponent( "com.sun.star.util.URLTransformer", m_pData->xURLTransformer ); 150cdf0e10cSrcweir } 151cdf0e10cSrcweir catch( const Exception& ) 152cdf0e10cSrcweir { 153cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 154cdf0e10cSrcweir } 155cdf0e10cSrcweir } 156cdf0e10cSrcweir 157cdf0e10cSrcweir //-------------------------------------------------------------------- ~DocumentEventExecutor()158cdf0e10cSrcweir DocumentEventExecutor::~DocumentEventExecutor() 159cdf0e10cSrcweir { 160cdf0e10cSrcweir } 161cdf0e10cSrcweir 162cdf0e10cSrcweir //-------------------------------------------------------------------- documentEventOccured(const DocumentEvent & _Event)163cdf0e10cSrcweir void SAL_CALL DocumentEventExecutor::documentEventOccured( const DocumentEvent& _Event ) throw (RuntimeException) 164cdf0e10cSrcweir { 165cdf0e10cSrcweir Reference< XEventsSupplier > xEventsSupplier( m_pData->xDocument.get(), UNO_QUERY ); 166cdf0e10cSrcweir if ( !xEventsSupplier.is() ) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir OSL_ENSURE( false, "DocumentEventExecutor::documentEventOccured: no document anymore, but still being notified?" ); 169cdf0e10cSrcweir return; 170cdf0e10cSrcweir } 171cdf0e10cSrcweir 172cdf0e10cSrcweir Reference< XModel > xDocument( xEventsSupplier, UNO_QUERY_THROW ); 173cdf0e10cSrcweir 174cdf0e10cSrcweir try 175cdf0e10cSrcweir { 176cdf0e10cSrcweir Reference< XNameAccess > xDocEvents( xEventsSupplier->getEvents().get(), UNO_SET_THROW ); 177cdf0e10cSrcweir if ( !xDocEvents->hasByName( _Event.EventName ) ) 178cdf0e10cSrcweir { 179cdf0e10cSrcweir // this is worth an assertion: We are listener at the very same document which we just asked 180cdf0e10cSrcweir // for its events. So when EventName is fired, why isn't it supported by xDocEvents? 181cdf0e10cSrcweir OSL_ENSURE( false, "DocumentEventExecutor::documentEventOccured: an unsupported event is notified!" ); 182cdf0e10cSrcweir return; 183cdf0e10cSrcweir } 184cdf0e10cSrcweir 185cdf0e10cSrcweir const ::comphelper::NamedValueCollection aScriptDescriptor( xDocEvents->getByName( _Event.EventName ) ); 186cdf0e10cSrcweir 187cdf0e10cSrcweir 188cdf0e10cSrcweir ::rtl::OUString sEventType; 189cdf0e10cSrcweir bool bScriptAssigned = aScriptDescriptor.get_ensureType( "EventType", sEventType ); 190cdf0e10cSrcweir 191cdf0e10cSrcweir ::rtl::OUString sScript; 192cdf0e10cSrcweir bScriptAssigned = bScriptAssigned && aScriptDescriptor.get_ensureType( "Script", sScript ); 193cdf0e10cSrcweir 194cdf0e10cSrcweir if ( !bScriptAssigned ) 195cdf0e10cSrcweir // no script is assigned to this event 196cdf0e10cSrcweir return; 197cdf0e10cSrcweir 198cdf0e10cSrcweir bool bDispatchScriptURL = 199cdf0e10cSrcweir ( sEventType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Script" ) ) 200cdf0e10cSrcweir || sEventType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Service" ) ) 201cdf0e10cSrcweir ); 202cdf0e10cSrcweir bool bNonEmptyScript = sScript.getLength() != 0; 203cdf0e10cSrcweir 204cdf0e10cSrcweir OSL_ENSURE( bDispatchScriptURL && bNonEmptyScript, 205cdf0e10cSrcweir "DocumentEventExecutor::documentEventOccured: invalid/unsupported script descriptor" ); 206cdf0e10cSrcweir 207cdf0e10cSrcweir if ( bDispatchScriptURL && bNonEmptyScript ) 208cdf0e10cSrcweir { 209cdf0e10cSrcweir lcl_dispatchScriptURL_throw( *m_pData, sScript, _Event ); 210cdf0e10cSrcweir } 211cdf0e10cSrcweir } 212cdf0e10cSrcweir catch( const RuntimeException& ) { throw; } 213cdf0e10cSrcweir catch( const Exception& ) 214cdf0e10cSrcweir { 215cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 216cdf0e10cSrcweir } 217cdf0e10cSrcweir } 218cdf0e10cSrcweir 219cdf0e10cSrcweir //-------------------------------------------------------------------- disposing(const lang::EventObject &)220cdf0e10cSrcweir void SAL_CALL DocumentEventExecutor::disposing( const lang::EventObject& /*_Source*/ ) throw (RuntimeException) 221cdf0e10cSrcweir { 222cdf0e10cSrcweir // not interested in 223cdf0e10cSrcweir } 224cdf0e10cSrcweir 225cdf0e10cSrcweir 226cdf0e10cSrcweir //........................................................................ 227cdf0e10cSrcweir } // namespace dbaccess 228cdf0e10cSrcweir //........................................................................ 229