1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * Copyright 2008 by Sun Microsystems, Inc.
6 * OpenOffice.org - a multi-platform office productivity suite
8 * $RCSfile: documenteventexecutor.cxx,v $
10 * $Revision: 1.1.2.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
28 ************************************************************************/
30 // MARKER(update_precomp.py): autogen include statement, do not remove
31 #include "precompiled_dbaccess.hxx"
33 #include "documenteventexecutor.hxx"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/document/XDocumentEventBroadcaster.hpp>
37 #include <com/sun/star/util/XURLTransformer.hpp>
38 #include <com/sun/star/frame/XModel.hpp>
39 #include <com/sun/star/frame/XDispatchProvider.hpp>
40 /** === end UNO includes === **/
42 #include <comphelper/componentcontext.hxx>
43 #include <comphelper/namedvaluecollection.hxx>
44 #include <cppuhelper/weakref.hxx>
45 #include <tools/diagnose_ex.h>
46 #include <vcl/svapp.hxx>
47 #include <vos/mutex.hxx>
49 //........................................................................
52 //........................................................................
54 /** === begin UNO using === **/
55 using ::com::sun::star::uno::Reference
;
56 using ::com::sun::star::uno::XInterface
;
57 using ::com::sun::star::uno::UNO_QUERY
;
58 using ::com::sun::star::uno::UNO_QUERY_THROW
;
59 using ::com::sun::star::uno::UNO_SET_THROW
;
60 using ::com::sun::star::uno::Exception
;
61 using ::com::sun::star::uno::RuntimeException
;
62 using ::com::sun::star::uno::Any
;
63 using ::com::sun::star::uno::makeAny
;
64 using ::com::sun::star::uno::Sequence
;
65 using ::com::sun::star::uno::Type
;
66 using ::com::sun::star::uno::WeakReference
;
67 using ::com::sun::star::document::XDocumentEventBroadcaster
;
68 using ::com::sun::star::document::XEventsSupplier
;
69 using ::com::sun::star::container::XNameAccess
;
70 using ::com::sun::star::frame::XModel
;
71 using ::com::sun::star::util::XURLTransformer
;
72 using ::com::sun::star::frame::XDispatchProvider
;
73 using ::com::sun::star::frame::XDispatch
;
74 using ::com::sun::star::util::URL
;
75 using ::com::sun::star::beans::PropertyValue
;
76 using ::com::sun::star::frame::XController
;
77 using ::com::sun::star::document::DocumentEvent
;
78 /** === end UNO using === **/
79 using namespace ::com::sun::star
;
81 //====================================================================
82 //= DocumentEventExecutor_Data
83 //====================================================================
84 struct DocumentEventExecutor_Data
86 WeakReference
< XEventsSupplier
> xDocument
;
87 Reference
< XURLTransformer
> xURLTransformer
;
89 DocumentEventExecutor_Data( const Reference
< XEventsSupplier
>& _rxDocument
)
90 :xDocument( _rxDocument
)
95 //--------------------------------------------------------------------
98 static void lcl_dispatchScriptURL_throw( DocumentEventExecutor_Data
& _rDocExecData
,
99 const ::rtl::OUString
& _rScriptURL
, const DocumentEvent
& _rTrigger
)
101 Reference
< XModel
> xDocument( _rDocExecData
.xDocument
.get(), UNO_QUERY_THROW
);
103 Reference
< XController
> xController( xDocument
->getCurrentController() );
104 Reference
< XDispatchProvider
> xDispProv
;
105 if ( xController
.is() )
106 xDispProv
.set( xController
->getFrame(), UNO_QUERY
);
107 if ( !xDispProv
.is() )
109 OSL_ENSURE( false, "lcl_dispatchScriptURL_throw: no controller/frame? How should I dispatch?" );
114 aScriptURL
.Complete
= _rScriptURL
;
115 if ( _rDocExecData
.xURLTransformer
.is() )
116 _rDocExecData
.xURLTransformer
->parseStrict( aScriptURL
);
118 // unfortunately, executing a script can trigger all kind of complex stuff, and unfortunately, not
119 // every component involved into this properly cares for thread safety. To be on the safe side,
120 // we lock the solar mutex here.
121 ::vos::OGuard
aSolarGuard( Application::GetSolarMutex() );
123 Reference
< XDispatch
> xDispatch( xDispProv
->queryDispatch( aScriptURL
, ::rtl::OUString(), 0 ) );
124 if ( !xDispatch
.is() )
126 OSL_ENSURE( false, "lcl_dispatchScriptURL_throw: no dispatcher for the script URL!" );
130 PropertyValue aEventParam
;
131 aEventParam
.Value
<<= _rTrigger
;
132 Sequence
< PropertyValue
> aDispatchArgs( &aEventParam
, 1 );
133 xDispatch
->dispatch( aScriptURL
, aDispatchArgs
);
137 //====================================================================
138 //= DocumentEventExecutor
139 //====================================================================
140 //--------------------------------------------------------------------
141 DocumentEventExecutor::DocumentEventExecutor( const ::comphelper::ComponentContext
& _rContext
,
142 const Reference
< XEventsSupplier
>& _rxDocument
)
143 :m_pData( new DocumentEventExecutor_Data( _rxDocument
) )
145 Reference
< XDocumentEventBroadcaster
> xBroadcaster( _rxDocument
, UNO_QUERY_THROW
);
147 osl_incrementInterlockedCount( &m_refCount
);
149 xBroadcaster
->addDocumentEventListener( this );
151 osl_decrementInterlockedCount( &m_refCount
);
155 _rContext
.createComponent( "com.sun.star.util.URLTransformer", m_pData
->xURLTransformer
);
157 catch( const Exception
& )
159 DBG_UNHANDLED_EXCEPTION();
163 //--------------------------------------------------------------------
164 DocumentEventExecutor::~DocumentEventExecutor()
168 //--------------------------------------------------------------------
169 void SAL_CALL
DocumentEventExecutor::documentEventOccured( const DocumentEvent
& _Event
) throw (RuntimeException
)
171 Reference
< XEventsSupplier
> xEventsSupplier( m_pData
->xDocument
.get(), UNO_QUERY
);
172 if ( !xEventsSupplier
.is() )
174 OSL_ENSURE( false, "DocumentEventExecutor::documentEventOccured: no document anymore, but still being notified?" );
178 Reference
< XModel
> xDocument( xEventsSupplier
, UNO_QUERY_THROW
);
182 Reference
< XNameAccess
> xDocEvents( xEventsSupplier
->getEvents().get(), UNO_SET_THROW
);
183 if ( !xDocEvents
->hasByName( _Event
.EventName
) )
185 // this is worth an assertion: We are listener at the very same document which we just asked
186 // for its events. So when EventName is fired, why isn't it supported by xDocEvents?
187 OSL_ENSURE( false, "DocumentEventExecutor::documentEventOccured: an unsupported event is notified!" );
191 const ::comphelper::NamedValueCollection
aScriptDescriptor( xDocEvents
->getByName( _Event
.EventName
) );
194 ::rtl::OUString sEventType
;
195 bool bScriptAssigned
= aScriptDescriptor
.get_ensureType( "EventType", sEventType
);
197 ::rtl::OUString sScript
;
198 bScriptAssigned
= bScriptAssigned
&& aScriptDescriptor
.get_ensureType( "Script", sScript
);
200 if ( !bScriptAssigned
)
201 // no script is assigned to this event
204 bool bDispatchScriptURL
=
205 ( sEventType
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Script" ) )
206 || sEventType
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Service" ) )
208 bool bNonEmptyScript
= sScript
.getLength() != 0;
210 OSL_ENSURE( bDispatchScriptURL
&& bNonEmptyScript
,
211 "DocumentEventExecutor::documentEventOccured: invalid/unsupported script descriptor" );
213 if ( bDispatchScriptURL
&& bNonEmptyScript
)
215 lcl_dispatchScriptURL_throw( *m_pData
, sScript
, _Event
);
218 catch( const RuntimeException
& ) { throw; }
219 catch( const Exception
& )
221 DBG_UNHANDLED_EXCEPTION();
225 //--------------------------------------------------------------------
226 void SAL_CALL
DocumentEventExecutor::disposing( const lang::EventObject
& /*_Source*/ ) throw (RuntimeException
)
232 //........................................................................
233 } // namespace dbaccess
234 //........................................................................