Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / core / dataaccess / documenteventnotifier.cxx
blob53ede81cdd26a191a5ae6634ad8ee2904cf436c1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "documenteventnotifier.hxx"
22 #include <com/sun/star/frame/DoubleInitializationException.hpp>
24 #include <comphelper/asyncnotification.hxx>
25 #include <cppuhelper/interfacecontainer.hxx>
26 #include <cppuhelper/weak.hxx>
27 #include <tools/diagnose_ex.h>
29 namespace dbaccess
32 using ::com::sun::star::uno::Reference;
33 using ::com::sun::star::uno::XInterface;
34 using ::com::sun::star::uno::UNO_QUERY;
35 using ::com::sun::star::uno::UNO_QUERY_THROW;
36 using ::com::sun::star::uno::UNO_SET_THROW;
37 using ::com::sun::star::uno::Exception;
38 using ::com::sun::star::uno::RuntimeException;
39 using ::com::sun::star::uno::Any;
40 using ::com::sun::star::uno::makeAny;
41 using ::com::sun::star::uno::Sequence;
42 using ::com::sun::star::uno::Type;
43 using ::com::sun::star::frame::DoubleInitializationException;
44 using ::com::sun::star::document::XDocumentEventListener;
45 using ::com::sun::star::document::DocumentEvent;
46 using ::com::sun::star::frame::XController2;
48 using namespace ::com::sun::star;
50 // DocumentEventHolder
51 typedef ::comphelper::EventHolder< DocumentEvent > DocumentEventHolder;
53 // DocumentEventNotifier_Impl
54 class DocumentEventNotifier_Impl : public ::comphelper::IEventProcessor
56 oslInterlockedCount m_refCount;
57 ::cppu::OWeakObject& m_rDocument;
58 ::osl::Mutex& m_rMutex;
59 bool m_bInitialized;
60 bool m_bDisposed;
61 ::rtl::Reference< ::comphelper::AsyncEventNotifier > m_pEventBroadcaster;
62 ::cppu::OInterfaceContainerHelper m_aLegacyEventListeners;
63 ::cppu::OInterfaceContainerHelper m_aDocumentEventListeners;
65 public:
66 DocumentEventNotifier_Impl( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
67 :m_refCount( 0 )
68 ,m_rDocument( _rBroadcasterDocument )
69 ,m_rMutex( _rMutex )
70 ,m_bInitialized( false )
71 ,m_bDisposed( false )
72 ,m_aLegacyEventListeners( _rMutex )
73 ,m_aDocumentEventListeners( _rMutex )
77 // IEventProcessor
78 virtual void SAL_CALL acquire() throw () SAL_OVERRIDE;
79 virtual void SAL_CALL release() throw () SAL_OVERRIDE;
81 void addLegacyEventListener( const Reference< document::XEventListener >& _Listener )
83 m_aLegacyEventListeners.addInterface( _Listener );
86 void removeLegacyEventListener( const Reference< document::XEventListener >& _Listener )
88 m_aLegacyEventListeners.removeInterface( _Listener );
91 void addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
93 m_aDocumentEventListeners.addInterface( _Listener );
96 void removeDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
98 m_aDocumentEventListeners.removeInterface( _Listener );
101 void disposing();
103 void onDocumentInitialized();
105 void notifyDocumentEvent( const OUString& _EventName, const Reference< XController2 >& _ViewController,
106 const Any& _Supplement )
108 impl_notifyEvent_nothrow( DocumentEvent(
109 m_rDocument, _EventName, _ViewController, _Supplement ) );
112 void notifyDocumentEventAsync( const OUString& _EventName, const Reference< XController2 >& _ViewController,
113 const Any& _Supplement )
115 impl_notifyEventAsync_nothrow( DocumentEvent(
116 m_rDocument, _EventName, _ViewController, _Supplement ) );
119 protected:
120 virtual ~DocumentEventNotifier_Impl()
124 // IEventProcessor
125 virtual void processEvent( const ::comphelper::AnyEvent& _rEvent ) SAL_OVERRIDE;
127 private:
128 void impl_notifyEvent_nothrow( const DocumentEvent& _rEvent );
129 void impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent );
132 void SAL_CALL DocumentEventNotifier_Impl::acquire() throw ()
134 osl_atomic_increment( &m_refCount );
137 void SAL_CALL DocumentEventNotifier_Impl::release() throw ()
139 if ( 0 == osl_atomic_decrement( &m_refCount ) )
140 delete this;
143 void DocumentEventNotifier_Impl::disposing()
145 // SYNCHRONIZED ->
146 // cancel any pending asynchronous events
147 ::osl::ResettableMutexGuard aGuard( m_rMutex );
148 if ( m_pEventBroadcaster.is() )
150 m_pEventBroadcaster->removeEventsForProcessor( this );
151 m_pEventBroadcaster->terminate();
152 //TODO: a protocol is missing how to join with the thread before
153 // exit(3), to ensure the thread is no longer relying on any
154 // infrastructure while that infrastructure is being shut down
155 // in atexit handlers; simply calling join here leads to
156 // deadlock, as this thread holds the solar mutex while the
157 // other thread is typically blocked waiting for the solar mutex
158 m_pEventBroadcaster.clear();
161 lang::EventObject aEvent( m_rDocument );
162 aGuard.clear();
163 // <-- SYNCHRONIZED
165 m_aLegacyEventListeners.disposeAndClear( aEvent );
166 m_aDocumentEventListeners.disposeAndClear( aEvent );
168 // SYNCHRONIZED ->
169 aGuard.reset();
170 m_bDisposed = true;
171 // <-- SYNCHRONIZED
174 void DocumentEventNotifier_Impl::onDocumentInitialized()
176 if ( m_bInitialized )
177 throw DoubleInitializationException();
179 m_bInitialized = true;
180 if ( m_pEventBroadcaster.is() )
181 // there are already pending asynchronous events
182 m_pEventBroadcaster->launch();
185 void DocumentEventNotifier_Impl::impl_notifyEvent_nothrow( const DocumentEvent& _rEvent )
187 OSL_PRECOND( m_bInitialized,
188 "DocumentEventNotifier_Impl::impl_notifyEvent_nothrow: only to be called when the document is already initialized!" );
191 document::EventObject aLegacyEvent( _rEvent.Source, _rEvent.EventName );
192 m_aLegacyEventListeners.notifyEach( &document::XEventListener::notifyEvent, aLegacyEvent );
194 catch(const Exception&)
196 DBG_UNHANDLED_EXCEPTION();
200 m_aDocumentEventListeners.notifyEach( &XDocumentEventListener::documentEventOccured, _rEvent );
202 catch( const Exception& )
204 DBG_UNHANDLED_EXCEPTION();
208 void DocumentEventNotifier_Impl::impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent )
210 if ( !m_pEventBroadcaster.is() )
212 m_pEventBroadcaster.set(
213 new ::comphelper::AsyncEventNotifier("DocumentEventNotifier"));
214 if ( m_bInitialized )
215 // start processing the events if and only if we (our document, respectively) are
216 // already initialized
217 m_pEventBroadcaster->launch();
219 m_pEventBroadcaster->addEvent( new DocumentEventHolder( _rEvent ), this );
222 void DocumentEventNotifier_Impl::processEvent( const ::comphelper::AnyEvent& _rEvent )
224 // beware, this is called from the notification thread
226 ::osl::MutexGuard aGuard( m_rMutex );
227 if ( m_bDisposed )
228 return;
230 const DocumentEventHolder& rEventHolder = dynamic_cast< const DocumentEventHolder& >( _rEvent );
231 impl_notifyEvent_nothrow( rEventHolder.getEventObject() );
234 // DocumentEventNotifier
235 DocumentEventNotifier::DocumentEventNotifier( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
236 :m_pImpl( new DocumentEventNotifier_Impl( _rBroadcasterDocument, _rMutex ) )
240 DocumentEventNotifier::~DocumentEventNotifier()
244 void DocumentEventNotifier::disposing()
246 m_pImpl->disposing();
249 void DocumentEventNotifier::onDocumentInitialized()
251 m_pImpl->onDocumentInitialized();
254 void DocumentEventNotifier::addLegacyEventListener( const Reference< document::XEventListener >& _Listener )
256 m_pImpl->addLegacyEventListener( _Listener );
259 void DocumentEventNotifier::removeLegacyEventListener( const Reference< document::XEventListener >& _Listener )
261 m_pImpl->removeLegacyEventListener( _Listener );
264 void DocumentEventNotifier::addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
266 m_pImpl->addDocumentEventListener( _Listener );
269 void DocumentEventNotifier::removeDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
271 m_pImpl->removeDocumentEventListener( _Listener );
274 void DocumentEventNotifier::notifyDocumentEvent( const OUString& _EventName,
275 const Reference< XController2 >& _ViewController, const Any& _Supplement )
277 m_pImpl->notifyDocumentEvent( _EventName, _ViewController, _Supplement );
280 void DocumentEventNotifier::notifyDocumentEventAsync( const OUString& _EventName,
281 const Reference< XController2 >& _ViewController, const Any& _Supplement )
283 m_pImpl->notifyDocumentEventAsync( _EventName, _ViewController, _Supplement );
286 } // namespace dbaccess
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */