1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "documenteventexecutor.hxx"
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>
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>
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
;
63 using namespace ::com::sun::star
;
65 // DocumentEventExecutor_Data
66 struct DocumentEventExecutor_Data
68 WeakReference
< XEventsSupplier
> xDocument
;
69 Reference
< XURLTransformer
> xURLTransformer
;
71 DocumentEventExecutor_Data( const Reference
< XEventsSupplier
>& _rxDocument
)
72 :xDocument( _rxDocument
)
79 static void lcl_dispatchScriptURL_throw( DocumentEventExecutor_Data
& _rDocExecData
,
80 const OUString
& _rScriptURL
, const DocumentEvent
& _rTrigger
)
82 Reference
< XModel
> xDocument( _rDocExecData
.xDocument
.get(), UNO_QUERY_THROW
);
84 Reference
< XController
> xController( xDocument
->getCurrentController() );
85 Reference
< XDispatchProvider
> xDispProv
;
86 if ( xController
.is() )
87 xDispProv
.set( xController
->getFrame(), UNO_QUERY
);
88 if ( !xDispProv
.is() )
90 OSL_FAIL( "lcl_dispatchScriptURL_throw: no controller/frame? How should I dispatch?" );
95 aScriptURL
.Complete
= _rScriptURL
;
96 if ( _rDocExecData
.xURLTransformer
.is() )
97 _rDocExecData
.xURLTransformer
->parseStrict( aScriptURL
);
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 SolarMutexGuard aSolarGuard
;
104 Reference
< XDispatch
> xDispatch( xDispProv
->queryDispatch( aScriptURL
, OUString(), 0 ) );
105 if ( !xDispatch
.is() )
107 OSL_FAIL( "lcl_dispatchScriptURL_throw: no dispatcher for the script URL!" );
111 PropertyValue aEventParam
;
112 aEventParam
.Value
<<= _rTrigger
;
113 Sequence
< PropertyValue
> aDispatchArgs( &aEventParam
, 1 );
114 xDispatch
->dispatch( aScriptURL
, aDispatchArgs
);
118 // DocumentEventExecutor
119 DocumentEventExecutor::DocumentEventExecutor( const Reference
<XComponentContext
> & _rContext
,
120 const Reference
< XEventsSupplier
>& _rxDocument
)
121 :m_pData( new DocumentEventExecutor_Data( _rxDocument
) )
123 Reference
< XDocumentEventBroadcaster
> xBroadcaster( _rxDocument
, UNO_QUERY_THROW
);
125 osl_atomic_increment( &m_refCount
);
127 xBroadcaster
->addDocumentEventListener( this );
129 osl_atomic_decrement( &m_refCount
);
133 m_pData
->xURLTransformer
= URLTransformer::create(_rContext
);
135 catch( const Exception
& )
137 DBG_UNHANDLED_EXCEPTION();
141 DocumentEventExecutor::~DocumentEventExecutor()
145 void SAL_CALL
DocumentEventExecutor::documentEventOccured( const DocumentEvent
& _Event
) throw (RuntimeException
, std::exception
)
147 Reference
< XEventsSupplier
> xEventsSupplier( m_pData
->xDocument
.get(), UNO_QUERY
);
148 if ( !xEventsSupplier
.is() )
150 OSL_FAIL( "DocumentEventExecutor::documentEventOccurred: no document anymore, but still being notified?" );
154 Reference
< XModel
> xDocument( xEventsSupplier
, UNO_QUERY_THROW
);
158 Reference
< XNameAccess
> xDocEvents( xEventsSupplier
->getEvents().get(), UNO_SET_THROW
);
159 if ( !xDocEvents
->hasByName( _Event
.EventName
) )
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!" );
167 const ::comphelper::NamedValueCollection
aScriptDescriptor( xDocEvents
->getByName( _Event
.EventName
) );
170 bool bScriptAssigned
= aScriptDescriptor
.get_ensureType( "EventType", sEventType
);
173 bScriptAssigned
= bScriptAssigned
&& aScriptDescriptor
.get_ensureType( "Script", sScript
);
175 if ( !bScriptAssigned
)
176 // no script is assigned to this event
179 bool bDispatchScriptURL
= ( sEventType
== "Script" || sEventType
== "Service" );
180 bool bNonEmptyScript
= !sScript
.isEmpty();
182 OSL_ENSURE( bDispatchScriptURL
&& bNonEmptyScript
,
183 "DocumentEventExecutor::documentEventOccurred: invalid/unsupported script descriptor" );
185 if ( bDispatchScriptURL
&& bNonEmptyScript
)
187 lcl_dispatchScriptURL_throw( *m_pData
, sScript
, _Event
);
190 catch( const RuntimeException
& ) { throw; }
191 catch( const Exception
& )
193 DBG_UNHANDLED_EXCEPTION();
197 void SAL_CALL
DocumentEventExecutor::disposing( const lang::EventObject
& /*_Source*/ ) throw (RuntimeException
, std::exception
)
202 } // namespace dbaccess
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */