LCOV - code coverage report
Current view: top level - dbaccess/source/core/dataaccess - documenteventexecutor.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 53 62 85.5 %
Date: 2014-11-03 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          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         196 :     struct DocumentEventExecutor_Data
      67             :     {
      68             :         WeakReference< XEventsSupplier >    xDocument;
      69             :         Reference< XURLTransformer >        xURLTransformer;
      70             : 
      71         210 :         DocumentEventExecutor_Data( const Reference< XEventsSupplier >& _rxDocument )
      72         210 :             :xDocument( _rxDocument )
      73             :         {
      74         210 :         }
      75             :     };
      76             : 
      77             :     namespace
      78             :     {
      79           6 :         static void lcl_dispatchScriptURL_throw( DocumentEventExecutor_Data& _rDocExecData,
      80             :             const OUString& _rScriptURL, const DocumentEvent& _rTrigger )
      81             :         {
      82           6 :             Reference< XModel > xDocument( _rDocExecData.xDocument.get(), UNO_QUERY_THROW );
      83             : 
      84           6 :             Reference< XController > xController( xDocument->getCurrentController() );
      85           6 :             Reference< XDispatchProvider > xDispProv;
      86           6 :             if ( xController.is() )
      87           6 :                 xDispProv.set( xController->getFrame(), UNO_QUERY );
      88           6 :             if ( !xDispProv.is() )
      89             :             {
      90             :                 OSL_FAIL( "lcl_dispatchScriptURL_throw: no controller/frame? How should I dispatch?" );
      91           0 :                 return;
      92             :             }
      93             : 
      94           6 :             URL aScriptURL;
      95           6 :             aScriptURL.Complete = _rScriptURL;
      96           6 :             if ( _rDocExecData.xURLTransformer.is() )
      97           6 :                 _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           6 :             SolarMutexGuard aSolarGuard;
     103             : 
     104           6 :             Reference< XDispatch > xDispatch( xDispProv->queryDispatch( aScriptURL, OUString(), 0 ) );
     105           6 :             if ( !xDispatch.is() )
     106             :             {
     107             :                 OSL_FAIL( "lcl_dispatchScriptURL_throw: no dispatcher for the script URL!" );
     108           6 :                 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         210 :     DocumentEventExecutor::DocumentEventExecutor( const Reference<XComponentContext> & _rContext,
     120             :             const Reference< XEventsSupplier >& _rxDocument )
     121         210 :         :m_pData( new DocumentEventExecutor_Data( _rxDocument ) )
     122             :     {
     123         210 :         Reference< XDocumentEventBroadcaster > xBroadcaster( _rxDocument, UNO_QUERY_THROW );
     124             : 
     125         210 :         osl_atomic_increment( &m_refCount );
     126             :         {
     127         210 :             xBroadcaster->addDocumentEventListener( this );
     128             :         }
     129         210 :         osl_atomic_decrement( &m_refCount );
     130             : 
     131             :         try
     132             :         {
     133         210 :             m_pData->xURLTransformer = URLTransformer::create(_rContext);
     134             :         }
     135           0 :         catch( const Exception& )
     136             :         {
     137             :             DBG_UNHANDLED_EXCEPTION();
     138         210 :         }
     139         210 :     }
     140             : 
     141         392 :     DocumentEventExecutor::~DocumentEventExecutor()
     142             :     {
     143         392 :     }
     144             : 
     145        1068 :     void SAL_CALL DocumentEventExecutor::documentEventOccured( const DocumentEvent& _Event ) throw (RuntimeException, std::exception)
     146             :     {
     147        1068 :         Reference< XEventsSupplier > xEventsSupplier( m_pData->xDocument.get(), UNO_QUERY );
     148        1068 :         if ( !xEventsSupplier.is() )
     149             :         {
     150             :             OSL_FAIL( "DocumentEventExecutor::documentEventOccurred: no document anymore, but still being notified?" );
     151           0 :             return;
     152             :         }
     153             : 
     154        1078 :         Reference< XModel > xDocument( xEventsSupplier, UNO_QUERY_THROW );
     155             : 
     156             :         try
     157             :         {
     158        1068 :             Reference< XNameAccess > xDocEvents( xEventsSupplier->getEvents().get(), UNO_SET_THROW );
     159        1064 :             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        1070 :             const ::comphelper::NamedValueCollection aScriptDescriptor( xDocEvents->getByName( _Event.EventName ) );
     168             : 
     169        1070 :             OUString sEventType;
     170        1064 :             bool bScriptAssigned = aScriptDescriptor.get_ensureType( "EventType", sEventType );
     171             : 
     172        1070 :             OUString sScript;
     173        1064 :             bScriptAssigned = bScriptAssigned && aScriptDescriptor.get_ensureType( "Script", sScript );
     174             : 
     175        1064 :             if ( !bScriptAssigned )
     176             :                 // no script is assigned to this event
     177        1058 :                 return;
     178             : 
     179           6 :             bool bDispatchScriptURL = ( sEventType == "Script" || sEventType == "Service" );
     180           6 :             bool bNonEmptyScript = !sScript.isEmpty();
     181             : 
     182             :             OSL_ENSURE( bDispatchScriptURL && bNonEmptyScript,
     183             :                 "DocumentEventExecutor::documentEventOccurred: invalid/unsupported script descriptor" );
     184             : 
     185           6 :             if ( bDispatchScriptURL && bNonEmptyScript )
     186             :             {
     187           6 :                 lcl_dispatchScriptURL_throw( *m_pData, sScript, _Event );
     188           6 :             }
     189             :         }
     190           4 :         catch( const RuntimeException& ) { throw; }
     191           0 :         catch( const Exception& )
     192             :         {
     193             :             DBG_UNHANDLED_EXCEPTION();
     194          10 :         }
     195             :     }
     196             : 
     197         210 :     void SAL_CALL DocumentEventExecutor::disposing( const lang::EventObject& /*_Source*/ ) throw (RuntimeException, std::exception)
     198             :     {
     199             :         // not interested in
     200         210 :     }
     201             : 
     202         168 : } // namespace dbaccess
     203             : 
     204             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10