1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_basctl.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "doceventnotifier.hxx"
32*cdf0e10cSrcweir #include "scriptdocument.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir /** === begin UNO includes === **/
35*cdf0e10cSrcweir #include <com/sun/star/document/XEventBroadcaster.hpp>
36*cdf0e10cSrcweir /** === end UNO includes === **/
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <vcl/svapp.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
43*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <vos/mutex.hxx>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <cppuhelper/compbase1.hxx>
48*cdf0e10cSrcweir #include <cppuhelper/basemutex.hxx>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir //........................................................................
51*cdf0e10cSrcweir namespace basctl
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir //........................................................................
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 	/** === begin UNO using === **/
56*cdf0e10cSrcweir     using ::com::sun::star::document::XEventBroadcaster;
57*cdf0e10cSrcweir     using ::com::sun::star::document::XEventListener;
58*cdf0e10cSrcweir     using ::com::sun::star::document::EventObject;
59*cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
60*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
61*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
62*cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
63*cdf0e10cSrcweir     using ::com::sun::star::frame::XModel;
64*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY;
65*cdf0e10cSrcweir 	/** === end UNO using === **/
66*cdf0e10cSrcweir     namespace csslang = ::com::sun::star::lang;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 	//====================================================================
69*cdf0e10cSrcweir 	//= DocumentEventNotifier_Impl
70*cdf0e10cSrcweir 	//====================================================================
71*cdf0e10cSrcweir     typedef ::cppu::WeakComponentImplHelper1    <   XEventListener
72*cdf0e10cSrcweir                                                 >   DocumentEventNotifier_Impl_Base;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     enum ListenerAction
75*cdf0e10cSrcweir     {
76*cdf0e10cSrcweir         RegisterListener,
77*cdf0e10cSrcweir         RemoveListener
78*cdf0e10cSrcweir     };
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     /** impl class for DocumentEventNotifier
81*cdf0e10cSrcweir     */
82*cdf0e10cSrcweir     class DocumentEventNotifier_Impl    :public ::boost::noncopyable
83*cdf0e10cSrcweir                                         ,public ::cppu::BaseMutex
84*cdf0e10cSrcweir                                         ,public DocumentEventNotifier_Impl_Base
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir     public:
87*cdf0e10cSrcweir         DocumentEventNotifier_Impl( DocumentEventListener& _rListener, const Reference< XModel >& _rxDocument );
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir         // document::XEventListener
90*cdf0e10cSrcweir         virtual void SAL_CALL notifyEvent( const EventObject& Event ) throw (RuntimeException);
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         // lang::XEventListener
93*cdf0e10cSrcweir         virtual void SAL_CALL disposing( const csslang::EventObject& Event ) throw (RuntimeException);
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         // ComponentHelper
96*cdf0e10cSrcweir         virtual void SAL_CALL disposing();
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     protected:
99*cdf0e10cSrcweir         ~DocumentEventNotifier_Impl();
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     private:
102*cdf0e10cSrcweir         /// determines whether the instance is already disposed
103*cdf0e10cSrcweir         bool    impl_isDisposed_nothrow() const { return m_pListener == NULL; }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir         /// disposes the instance
106*cdf0e10cSrcweir         void    impl_dispose_nothrow();
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir         /// registers or revokes the instance as listener at the global event broadcaster
109*cdf0e10cSrcweir         void    impl_listenerAction_nothrow( ListenerAction _eAction );
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     private:
112*cdf0e10cSrcweir         DocumentEventListener*  m_pListener;
113*cdf0e10cSrcweir         Reference< XModel >     m_xModel;
114*cdf0e10cSrcweir     };
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 	//--------------------------------------------------------------------
117*cdf0e10cSrcweir     DocumentEventNotifier_Impl::DocumentEventNotifier_Impl( DocumentEventListener& _rListener, const Reference< XModel >& _rxDocument )
118*cdf0e10cSrcweir         :DocumentEventNotifier_Impl_Base( m_aMutex )
119*cdf0e10cSrcweir         ,m_pListener( &_rListener )
120*cdf0e10cSrcweir         ,m_xModel( _rxDocument )
121*cdf0e10cSrcweir     {
122*cdf0e10cSrcweir         osl_incrementInterlockedCount( &m_refCount );
123*cdf0e10cSrcweir         impl_listenerAction_nothrow( RegisterListener );
124*cdf0e10cSrcweir         osl_decrementInterlockedCount( &m_refCount );
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 	//--------------------------------------------------------------------
128*cdf0e10cSrcweir     DocumentEventNotifier_Impl::~DocumentEventNotifier_Impl()
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir         if ( !impl_isDisposed_nothrow() )
131*cdf0e10cSrcweir         {
132*cdf0e10cSrcweir             acquire();
133*cdf0e10cSrcweir             dispose();
134*cdf0e10cSrcweir         }
135*cdf0e10cSrcweir     }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     //--------------------------------------------------------------------
138*cdf0e10cSrcweir     void SAL_CALL DocumentEventNotifier_Impl::notifyEvent( const EventObject& _rEvent ) throw (RuntimeException)
139*cdf0e10cSrcweir     {
140*cdf0e10cSrcweir         ::osl::ClearableMutexGuard aGuard( m_aMutex );
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir         OSL_PRECOND( !impl_isDisposed_nothrow(), "DocumentEventNotifier_Impl::notifyEvent: disposed, but still getting events?" );
143*cdf0e10cSrcweir         if ( impl_isDisposed_nothrow() )
144*cdf0e10cSrcweir             return;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         Reference< XModel > xDocument( _rEvent.Source, UNO_QUERY );
147*cdf0e10cSrcweir         OSL_ENSURE( xDocument.is(), "DocumentEventNotifier_Impl::notifyEvent: illegal source document!" );
148*cdf0e10cSrcweir         if ( !xDocument.is() )
149*cdf0e10cSrcweir             return;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir         struct EventEntry
152*cdf0e10cSrcweir         {
153*cdf0e10cSrcweir             const sal_Char* pEventName;
154*cdf0e10cSrcweir             void (DocumentEventListener::*listenerMethod)( const ScriptDocument& _rDocument );
155*cdf0e10cSrcweir         };
156*cdf0e10cSrcweir         EventEntry aEvents[] = {
157*cdf0e10cSrcweir             { "OnNew",          &DocumentEventListener::onDocumentCreated },
158*cdf0e10cSrcweir             { "OnLoad",         &DocumentEventListener::onDocumentOpened },
159*cdf0e10cSrcweir             { "OnSave",         &DocumentEventListener::onDocumentSave },
160*cdf0e10cSrcweir             { "OnSaveDone",     &DocumentEventListener::onDocumentSaveDone },
161*cdf0e10cSrcweir             { "OnSaveAs",       &DocumentEventListener::onDocumentSaveAs },
162*cdf0e10cSrcweir             { "OnSaveAsDone",   &DocumentEventListener::onDocumentSaveAsDone },
163*cdf0e10cSrcweir             { "OnUnload",       &DocumentEventListener::onDocumentClosed },
164*cdf0e10cSrcweir             { "OnTitleChanged", &DocumentEventListener::onDocumentTitleChanged },
165*cdf0e10cSrcweir             { "OnModeChanged",  &DocumentEventListener::onDocumentModeChanged }
166*cdf0e10cSrcweir         };
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         for ( size_t i=0; i < sizeof( aEvents ) / sizeof( aEvents[0] ); ++i )
169*cdf0e10cSrcweir         {
170*cdf0e10cSrcweir             if ( !_rEvent.EventName.equalsAscii( aEvents[i].pEventName ) )
171*cdf0e10cSrcweir                 continue;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir             ScriptDocument aDocument( xDocument );
174*cdf0e10cSrcweir             {
175*cdf0e10cSrcweir                 // the listener implementations usually require the SolarMutex, so lock it here.
176*cdf0e10cSrcweir                 // But ensure the proper order of locking the solar and the own mutex
177*cdf0e10cSrcweir                 aGuard.clear();
178*cdf0e10cSrcweir                 ::vos::OClearableGuard aSolarGuard( Application::GetSolarMutex() );
179*cdf0e10cSrcweir                 ::osl::MutexGuard aGuard2( m_aMutex );
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir                 if ( impl_isDisposed_nothrow() )
182*cdf0e10cSrcweir                     // somebody took the chance to dispose us -> bail out
183*cdf0e10cSrcweir                     return;
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir                 (m_pListener->*aEvents[i].listenerMethod)( aDocument );
186*cdf0e10cSrcweir             }
187*cdf0e10cSrcweir             break;
188*cdf0e10cSrcweir         }
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     //--------------------------------------------------------------------
192*cdf0e10cSrcweir     void SAL_CALL DocumentEventNotifier_Impl::disposing( const csslang::EventObject& /*Event*/ ) throw (RuntimeException)
193*cdf0e10cSrcweir     {
194*cdf0e10cSrcweir         ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
195*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir         if ( !impl_isDisposed_nothrow() )
198*cdf0e10cSrcweir             impl_dispose_nothrow();
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     //--------------------------------------------------------------------
202*cdf0e10cSrcweir     void SAL_CALL DocumentEventNotifier_Impl::disposing()
203*cdf0e10cSrcweir     {
204*cdf0e10cSrcweir         impl_listenerAction_nothrow( RemoveListener );
205*cdf0e10cSrcweir         impl_dispose_nothrow();
206*cdf0e10cSrcweir     }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir     //--------------------------------------------------------------------
209*cdf0e10cSrcweir     void DocumentEventNotifier_Impl::impl_dispose_nothrow()
210*cdf0e10cSrcweir     {
211*cdf0e10cSrcweir         m_pListener = NULL;
212*cdf0e10cSrcweir         m_xModel.clear();
213*cdf0e10cSrcweir     }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     //--------------------------------------------------------------------
216*cdf0e10cSrcweir     void DocumentEventNotifier_Impl::impl_listenerAction_nothrow( ListenerAction _eAction )
217*cdf0e10cSrcweir     {
218*cdf0e10cSrcweir         try
219*cdf0e10cSrcweir         {
220*cdf0e10cSrcweir             Reference< XEventBroadcaster > xBroadcaster;
221*cdf0e10cSrcweir             if ( m_xModel.is() )
222*cdf0e10cSrcweir                 xBroadcaster.set( m_xModel, UNO_QUERY_THROW );
223*cdf0e10cSrcweir             else
224*cdf0e10cSrcweir             {
225*cdf0e10cSrcweir                 ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
226*cdf0e10cSrcweir                 xBroadcaster.set(
227*cdf0e10cSrcweir                     aContext.createComponent( "com.sun.star.frame.GlobalEventBroadcaster" ),
228*cdf0e10cSrcweir                     UNO_QUERY_THROW );
229*cdf0e10cSrcweir             }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir             void ( SAL_CALL XEventBroadcaster::*listenerAction )( const Reference< XEventListener >& ) =
232*cdf0e10cSrcweir                 ( _eAction == RegisterListener ) ? &XEventBroadcaster::addEventListener : &XEventBroadcaster::removeEventListener;
233*cdf0e10cSrcweir             (xBroadcaster.get()->*listenerAction)( this );
234*cdf0e10cSrcweir         }
235*cdf0e10cSrcweir         catch( const Exception& )
236*cdf0e10cSrcweir         {
237*cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
238*cdf0e10cSrcweir         }
239*cdf0e10cSrcweir     }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir 	//====================================================================
242*cdf0e10cSrcweir 	//= DocumentEventNotifier
243*cdf0e10cSrcweir 	//====================================================================
244*cdf0e10cSrcweir 	//--------------------------------------------------------------------
245*cdf0e10cSrcweir     DocumentEventNotifier::DocumentEventNotifier( DocumentEventListener& _rListener, const Reference< XModel >& _rxDocument )
246*cdf0e10cSrcweir         :m_pImpl( new DocumentEventNotifier_Impl( _rListener, _rxDocument ) )
247*cdf0e10cSrcweir     {
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 	//--------------------------------------------------------------------
251*cdf0e10cSrcweir     DocumentEventNotifier::DocumentEventNotifier( DocumentEventListener& _rListener )
252*cdf0e10cSrcweir         :m_pImpl( new DocumentEventNotifier_Impl( _rListener, Reference< XModel >() ) )
253*cdf0e10cSrcweir     {
254*cdf0e10cSrcweir     }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 	//--------------------------------------------------------------------
257*cdf0e10cSrcweir     DocumentEventNotifier::~DocumentEventNotifier()
258*cdf0e10cSrcweir     {
259*cdf0e10cSrcweir     }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 	//--------------------------------------------------------------------
262*cdf0e10cSrcweir     void DocumentEventNotifier::dispose()
263*cdf0e10cSrcweir     {
264*cdf0e10cSrcweir         m_pImpl->dispose();
265*cdf0e10cSrcweir     }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir 	//====================================================================
268*cdf0e10cSrcweir 	//= DocumentEventListener
269*cdf0e10cSrcweir 	//====================================================================
270*cdf0e10cSrcweir     DocumentEventListener::~DocumentEventListener()
271*cdf0e10cSrcweir     {
272*cdf0e10cSrcweir     }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir //........................................................................
275*cdf0e10cSrcweir } // namespace basctl
276*cdf0e10cSrcweir //........................................................................
277