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"
21 #include "databasedocument.hxx"
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>
29 #include <comphelper/namedvaluecollection.hxx>
30 #include <cppuhelper/weakref.hxx>
31 #include <comphelper/diagnose_ex.hxx>
32 #include <vcl/svapp.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
;
63 void lcl_dispatchScriptURL_throw(
64 ::unotools::WeakReference
< ODatabaseDocument
> const & xWeakDocument
,
65 css::uno::Reference
< css::util::XURLTransformer
> const & xURLTransformer
,
66 const OUString
& _rScriptURL
, const DocumentEvent
& _rTrigger
)
68 rtl::Reference
< ODatabaseDocument
> xDocument( xWeakDocument
.get() );
70 Reference
< XController
> xController( xDocument
->getCurrentController() );
71 Reference
< XDispatchProvider
> xDispProv
;
72 if ( xController
.is() )
73 xDispProv
.set( xController
->getFrame(), UNO_QUERY
);
74 if ( !xDispProv
.is() )
76 OSL_FAIL( "lcl_dispatchScriptURL_throw: no controller/frame? How should I dispatch?" );
81 aScriptURL
.Complete
= _rScriptURL
;
82 if ( xURLTransformer
.is() )
83 xURLTransformer
->parseStrict( aScriptURL
);
85 // unfortunately, executing a script can trigger all kind of complex stuff, and unfortunately, not
86 // every component involved into this properly cares for thread safety. To be on the safe side,
87 // we lock the solar mutex here.
88 SolarMutexGuard aSolarGuard
;
90 Reference
< XDispatch
> xDispatch( xDispProv
->queryDispatch( aScriptURL
, OUString(), 0 ) );
91 if ( !xDispatch
.is() )
93 OSL_FAIL( "lcl_dispatchScriptURL_throw: no dispatcher for the script URL!" );
97 PropertyValue aEventParam
;
98 aEventParam
.Value
<<= _rTrigger
;
99 Sequence
< PropertyValue
> aDispatchArgs( &aEventParam
, 1 );
100 xDispatch
->dispatch( aScriptURL
, aDispatchArgs
);
104 // DocumentEventExecutor
105 DocumentEventExecutor::DocumentEventExecutor( const Reference
<XComponentContext
> & _rContext
,
106 const rtl::Reference
< ODatabaseDocument
>& _rxDocument
)
107 :mxDocument( _rxDocument
)
109 osl_atomic_increment( &m_refCount
);
111 _rxDocument
->addDocumentEventListener( this );
113 osl_atomic_decrement( &m_refCount
);
117 mxURLTransformer
= URLTransformer::create(_rContext
);
119 catch( const Exception
& )
121 DBG_UNHANDLED_EXCEPTION("dbaccess");
125 DocumentEventExecutor::~DocumentEventExecutor()
129 void SAL_CALL
DocumentEventExecutor::documentEventOccured( const DocumentEvent
& Event
)
131 rtl::Reference
< ODatabaseDocument
> xDocument( mxDocument
.get() );
134 OSL_FAIL( "DocumentEventExecutor::documentEventOccurred: no document anymore, but still being notified?" );
140 Reference
< XNameAccess
> xDocEvents( xDocument
->getEvents(), UNO_SET_THROW
);
141 if ( !xDocEvents
->hasByName( Event
.EventName
) )
143 // this is worth an assertion: We are listener at the very same document which we just asked
144 // for its events. So when EventName is fired, why isn't it supported by xDocEvents?
145 OSL_FAIL( "DocumentEventExecutor::documentEventOccurred: an unsupported event is notified!" );
149 const ::comphelper::NamedValueCollection
aScriptDescriptor( xDocEvents
->getByName( Event
.EventName
) );
152 bool bScriptAssigned
= aScriptDescriptor
.get_ensureType( u
"EventType"_ustr
, sEventType
);
155 bScriptAssigned
= bScriptAssigned
&& aScriptDescriptor
.get_ensureType( u
"Script"_ustr
, sScript
);
157 if ( !bScriptAssigned
)
158 // no script is assigned to this event
161 bool bDispatchScriptURL
= ( sEventType
== "Script" || sEventType
== "Service" );
162 bool bNonEmptyScript
= !sScript
.isEmpty();
164 OSL_ENSURE( bDispatchScriptURL
&& bNonEmptyScript
,
165 "DocumentEventExecutor::documentEventOccurred: invalid/unsupported script descriptor" );
167 if ( bDispatchScriptURL
&& bNonEmptyScript
)
169 lcl_dispatchScriptURL_throw( mxDocument
, mxURLTransformer
, sScript
, Event
);
172 catch( const RuntimeException
& ) { throw; }
173 catch( const Exception
& )
175 DBG_UNHANDLED_EXCEPTION("dbaccess");
179 void SAL_CALL
DocumentEventExecutor::disposing( const lang::EventObject
& /*_Source*/ )
184 } // namespace dbaccess
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */