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::UNO_QUERY
;
39 using ::com::sun::star::uno::UNO_QUERY_THROW
;
40 using ::com::sun::star::uno::UNO_SET_THROW
;
41 using ::com::sun::star::uno::Exception
;
42 using ::com::sun::star::uno::RuntimeException
;
43 using ::com::sun::star::uno::Sequence
;
44 using ::com::sun::star::uno::WeakReference
;
45 using ::com::sun::star::uno::XComponentContext
;
46 using ::com::sun::star::document::XDocumentEventBroadcaster
;
47 using ::com::sun::star::document::XEventsSupplier
;
48 using ::com::sun::star::container::XNameAccess
;
49 using ::com::sun::star::frame::XModel
;
50 using ::com::sun::star::util::URLTransformer
;
51 using ::com::sun::star::util::XURLTransformer
;
52 using ::com::sun::star::frame::XDispatchProvider
;
53 using ::com::sun::star::frame::XDispatch
;
54 using ::com::sun::star::util::URL
;
55 using ::com::sun::star::beans::PropertyValue
;
56 using ::com::sun::star::frame::XController
;
57 using ::com::sun::star::document::DocumentEvent
;
59 using namespace ::com::sun::star
;
61 // DocumentEventExecutor_Data
62 struct DocumentEventExecutor_Data
64 WeakReference
< XEventsSupplier
> xDocument
;
65 Reference
< XURLTransformer
> xURLTransformer
;
67 explicit DocumentEventExecutor_Data( const Reference
< XEventsSupplier
>& _rxDocument
)
68 :xDocument( _rxDocument
)
75 void lcl_dispatchScriptURL_throw( DocumentEventExecutor_Data
& _rDocExecData
,
76 const OUString
& _rScriptURL
, const DocumentEvent
& _rTrigger
)
78 Reference
< XModel
> xDocument( _rDocExecData
.xDocument
.get(), UNO_QUERY_THROW
);
80 Reference
< XController
> xController( xDocument
->getCurrentController() );
81 Reference
< XDispatchProvider
> xDispProv
;
82 if ( xController
.is() )
83 xDispProv
.set( xController
->getFrame(), UNO_QUERY
);
84 if ( !xDispProv
.is() )
86 OSL_FAIL( "lcl_dispatchScriptURL_throw: no controller/frame? How should I dispatch?" );
91 aScriptURL
.Complete
= _rScriptURL
;
92 if ( _rDocExecData
.xURLTransformer
.is() )
93 _rDocExecData
.xURLTransformer
->parseStrict( aScriptURL
);
95 // unfortunately, executing a script can trigger all kind of complex stuff, and unfortunately, not
96 // every component involved into this properly cares for thread safety. To be on the safe side,
97 // we lock the solar mutex here.
98 SolarMutexGuard aSolarGuard
;
100 Reference
< XDispatch
> xDispatch( xDispProv
->queryDispatch( aScriptURL
, OUString(), 0 ) );
101 if ( !xDispatch
.is() )
103 OSL_FAIL( "lcl_dispatchScriptURL_throw: no dispatcher for the script URL!" );
107 PropertyValue aEventParam
;
108 aEventParam
.Value
<<= _rTrigger
;
109 Sequence
< PropertyValue
> aDispatchArgs( &aEventParam
, 1 );
110 xDispatch
->dispatch( aScriptURL
, aDispatchArgs
);
114 // DocumentEventExecutor
115 DocumentEventExecutor::DocumentEventExecutor( const Reference
<XComponentContext
> & _rContext
,
116 const Reference
< XEventsSupplier
>& _rxDocument
)
117 :m_pData( new DocumentEventExecutor_Data( _rxDocument
) )
119 Reference
< XDocumentEventBroadcaster
> xBroadcaster( _rxDocument
, UNO_QUERY_THROW
);
121 osl_atomic_increment( &m_refCount
);
123 xBroadcaster
->addDocumentEventListener( this );
125 osl_atomic_decrement( &m_refCount
);
129 m_pData
->xURLTransformer
= URLTransformer::create(_rContext
);
131 catch( const Exception
& )
133 DBG_UNHANDLED_EXCEPTION();
137 DocumentEventExecutor::~DocumentEventExecutor()
141 void SAL_CALL
DocumentEventExecutor::documentEventOccured( const DocumentEvent
& Event
) throw (RuntimeException
, std::exception
)
143 Reference
< XEventsSupplier
> xEventsSupplier( m_pData
->xDocument
.get(), UNO_QUERY
);
144 if ( !xEventsSupplier
.is() )
146 OSL_FAIL( "DocumentEventExecutor::documentEventOccurred: no document anymore, but still being notified?" );
150 Reference
< XModel
> xDocument( xEventsSupplier
, UNO_QUERY_THROW
);
154 Reference
< XNameAccess
> xDocEvents( xEventsSupplier
->getEvents().get(), UNO_SET_THROW
);
155 if ( !xDocEvents
->hasByName( Event
.EventName
) )
157 // this is worth an assertion: We are listener at the very same document which we just asked
158 // for its events. So when EventName is fired, why isn't it supported by xDocEvents?
159 OSL_FAIL( "DocumentEventExecutor::documentEventOccurred: an unsupported event is notified!" );
163 const ::comphelper::NamedValueCollection
aScriptDescriptor( xDocEvents
->getByName( Event
.EventName
) );
166 bool bScriptAssigned
= aScriptDescriptor
.get_ensureType( "EventType", sEventType
);
169 bScriptAssigned
= bScriptAssigned
&& aScriptDescriptor
.get_ensureType( "Script", sScript
);
171 if ( !bScriptAssigned
)
172 // no script is assigned to this event
175 bool bDispatchScriptURL
= ( sEventType
== "Script" || sEventType
== "Service" );
176 bool bNonEmptyScript
= !sScript
.isEmpty();
178 OSL_ENSURE( bDispatchScriptURL
&& bNonEmptyScript
,
179 "DocumentEventExecutor::documentEventOccurred: invalid/unsupported script descriptor" );
181 if ( bDispatchScriptURL
&& bNonEmptyScript
)
183 lcl_dispatchScriptURL_throw( *m_pData
, sScript
, Event
);
186 catch( const RuntimeException
& ) { throw; }
187 catch( const Exception
& )
189 DBG_UNHANDLED_EXCEPTION();
193 void SAL_CALL
DocumentEventExecutor::disposing( const lang::EventObject
& /*_Source*/ ) throw (RuntimeException
, std::exception
)
198 } // namespace dbaccess
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */