Update ooo320-m1
[ooovba.git] / basctl / source / basicide / doceventnotifier.cxx
blobf03b0e994f67ece4ae91488a1596182c8e9db7a6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: doceventnotifier.cxx,v $
10 * $Revision: 1.3 $
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.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basctl.hxx"
34 #include "doceventnotifier.hxx"
35 #include "scriptdocument.hxx"
37 /** === begin UNO includes === **/
38 #include <com/sun/star/document/XEventBroadcaster.hpp>
39 /** === end UNO includes === **/
41 #include <vcl/svapp.hxx>
43 #include <tools/diagnose_ex.h>
45 #include <comphelper/componentcontext.hxx>
46 #include <comphelper/processfactory.hxx>
48 #include <vos/mutex.hxx>
50 #include <cppuhelper/compbase1.hxx>
51 #include <cppuhelper/basemutex.hxx>
53 //........................................................................
54 namespace basctl
56 //........................................................................
58 /** === begin UNO using === **/
59 using ::com::sun::star::document::XEventBroadcaster;
60 using ::com::sun::star::document::XEventListener;
61 using ::com::sun::star::document::EventObject;
62 using ::com::sun::star::uno::RuntimeException;
63 using ::com::sun::star::uno::Reference;
64 using ::com::sun::star::uno::UNO_QUERY_THROW;
65 using ::com::sun::star::uno::Exception;
66 using ::com::sun::star::frame::XModel;
67 using ::com::sun::star::uno::UNO_QUERY;
68 /** === end UNO using === **/
69 namespace csslang = ::com::sun::star::lang;
71 //====================================================================
72 //= DocumentEventNotifier_Impl
73 //====================================================================
74 typedef ::cppu::WeakComponentImplHelper1 < XEventListener
75 > DocumentEventNotifier_Impl_Base;
77 enum ListenerAction
79 RegisterListener,
80 RemoveListener
83 /** impl class for DocumentEventNotifier
85 class DocumentEventNotifier_Impl :public ::boost::noncopyable
86 ,public ::cppu::BaseMutex
87 ,public DocumentEventNotifier_Impl_Base
89 public:
90 DocumentEventNotifier_Impl( DocumentEventListener& _rListener, const Reference< XModel >& _rxDocument );
92 // document::XEventListener
93 virtual void SAL_CALL notifyEvent( const EventObject& Event ) throw (RuntimeException);
95 // lang::XEventListener
96 virtual void SAL_CALL disposing( const csslang::EventObject& Event ) throw (RuntimeException);
98 // ComponentHelper
99 virtual void SAL_CALL disposing();
101 protected:
102 ~DocumentEventNotifier_Impl();
104 private:
105 /// determines whether the instance is already disposed
106 bool impl_isDisposed_nothrow() const { return m_pListener == NULL; }
108 /// disposes the instance
109 void impl_dispose_nothrow();
111 /// registers or revokes the instance as listener at the global event broadcaster
112 void impl_listenerAction_nothrow( ListenerAction _eAction );
114 private:
115 DocumentEventListener* m_pListener;
116 Reference< XModel > m_xModel;
119 //--------------------------------------------------------------------
120 DocumentEventNotifier_Impl::DocumentEventNotifier_Impl( DocumentEventListener& _rListener, const Reference< XModel >& _rxDocument )
121 :DocumentEventNotifier_Impl_Base( m_aMutex )
122 ,m_pListener( &_rListener )
123 ,m_xModel( _rxDocument )
125 osl_incrementInterlockedCount( &m_refCount );
126 impl_listenerAction_nothrow( RegisterListener );
127 osl_decrementInterlockedCount( &m_refCount );
130 //--------------------------------------------------------------------
131 DocumentEventNotifier_Impl::~DocumentEventNotifier_Impl()
133 if ( !impl_isDisposed_nothrow() )
135 acquire();
136 dispose();
140 //--------------------------------------------------------------------
141 void SAL_CALL DocumentEventNotifier_Impl::notifyEvent( const EventObject& _rEvent ) throw (RuntimeException)
143 ::osl::ClearableMutexGuard aGuard( m_aMutex );
145 OSL_PRECOND( !impl_isDisposed_nothrow(), "DocumentEventNotifier_Impl::notifyEvent: disposed, but still getting events?" );
146 if ( impl_isDisposed_nothrow() )
147 return;
149 Reference< XModel > xDocument( _rEvent.Source, UNO_QUERY );
150 OSL_ENSURE( xDocument.is(), "DocumentEventNotifier_Impl::notifyEvent: illegal source document!" );
151 if ( !xDocument.is() )
152 return;
154 struct EventEntry
156 const sal_Char* pEventName;
157 void (DocumentEventListener::*listenerMethod)( const ScriptDocument& _rDocument );
159 EventEntry aEvents[] = {
160 { "OnNew", &DocumentEventListener::onDocumentCreated },
161 { "OnLoad", &DocumentEventListener::onDocumentOpened },
162 { "OnSave", &DocumentEventListener::onDocumentSave },
163 { "OnSaveDone", &DocumentEventListener::onDocumentSaveDone },
164 { "OnSaveAs", &DocumentEventListener::onDocumentSaveAs },
165 { "OnSaveAsDone", &DocumentEventListener::onDocumentSaveAsDone },
166 { "OnUnload", &DocumentEventListener::onDocumentClosed },
167 { "OnTitleChanged", &DocumentEventListener::onDocumentTitleChanged },
168 { "OnModeChanged", &DocumentEventListener::onDocumentModeChanged }
171 for ( size_t i=0; i < sizeof( aEvents ) / sizeof( aEvents[0] ); ++i )
173 if ( !_rEvent.EventName.equalsAscii( aEvents[i].pEventName ) )
174 continue;
176 ScriptDocument aDocument( xDocument );
178 // the listener implementations usually require the SolarMutex, so lock it here.
179 // But ensure the proper order of locking the solar and the own mutex
180 aGuard.clear();
181 ::vos::OClearableGuard aSolarGuard( Application::GetSolarMutex() );
182 ::osl::MutexGuard aGuard2( m_aMutex );
184 if ( impl_isDisposed_nothrow() )
185 // somebody took the chance to dispose us -> bail out
186 return;
188 (m_pListener->*aEvents[i].listenerMethod)( aDocument );
190 break;
194 //--------------------------------------------------------------------
195 void SAL_CALL DocumentEventNotifier_Impl::disposing( const csslang::EventObject& /*Event*/ ) throw (RuntimeException)
197 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
198 ::osl::MutexGuard aGuard( m_aMutex );
200 if ( !impl_isDisposed_nothrow() )
201 impl_dispose_nothrow();
204 //--------------------------------------------------------------------
205 void SAL_CALL DocumentEventNotifier_Impl::disposing()
207 impl_listenerAction_nothrow( RemoveListener );
208 impl_dispose_nothrow();
211 //--------------------------------------------------------------------
212 void DocumentEventNotifier_Impl::impl_dispose_nothrow()
214 m_pListener = NULL;
215 m_xModel.clear();
218 //--------------------------------------------------------------------
219 void DocumentEventNotifier_Impl::impl_listenerAction_nothrow( ListenerAction _eAction )
223 Reference< XEventBroadcaster > xBroadcaster;
224 if ( m_xModel.is() )
225 xBroadcaster.set( m_xModel, UNO_QUERY_THROW );
226 else
228 ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
229 xBroadcaster.set(
230 aContext.createComponent( "com.sun.star.frame.GlobalEventBroadcaster" ),
231 UNO_QUERY_THROW );
234 void ( SAL_CALL XEventBroadcaster::*listenerAction )( const Reference< XEventListener >& ) =
235 ( _eAction == RegisterListener ) ? &XEventBroadcaster::addEventListener : &XEventBroadcaster::removeEventListener;
236 (xBroadcaster.get()->*listenerAction)( this );
238 catch( const Exception& )
240 DBG_UNHANDLED_EXCEPTION();
244 //====================================================================
245 //= DocumentEventNotifier
246 //====================================================================
247 //--------------------------------------------------------------------
248 DocumentEventNotifier::DocumentEventNotifier( DocumentEventListener& _rListener, const Reference< XModel >& _rxDocument )
249 :m_pImpl( new DocumentEventNotifier_Impl( _rListener, _rxDocument ) )
253 //--------------------------------------------------------------------
254 DocumentEventNotifier::DocumentEventNotifier( DocumentEventListener& _rListener )
255 :m_pImpl( new DocumentEventNotifier_Impl( _rListener, Reference< XModel >() ) )
259 //--------------------------------------------------------------------
260 DocumentEventNotifier::~DocumentEventNotifier()
264 //--------------------------------------------------------------------
265 void DocumentEventNotifier::dispose()
267 m_pImpl->dispose();
270 //====================================================================
271 //= DocumentEventListener
272 //====================================================================
273 DocumentEventListener::~DocumentEventListener()
277 //........................................................................
278 } // namespace basctl
279 //........................................................................