2010-10-12 15:59:03 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-14 17:39:53 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
2010-02-12 15:01:35 +01:00
|
|
|
*
|
2012-06-14 17:39:53 +01:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2010-02-12 15:01:35 +01:00
|
|
|
*
|
2012-06-14 17:39:53 +01:00
|
|
|
* This file incorporates work covered by the following license notice:
|
2010-02-12 15:01:35 +01:00
|
|
|
*
|
2012-06-14 17:39:53 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2008-10-16 06:57:26 +00:00
|
|
|
|
|
|
|
#include "documenteventnotifier.hxx"
|
|
|
|
|
|
|
|
#include <com/sun/star/frame/DoubleInitializationException.hpp>
|
|
|
|
|
|
|
|
#include <comphelper/asyncnotification.hxx>
|
2016-01-19 19:45:45 +02:00
|
|
|
#include <comphelper/interfacecontainer2.hxx>
|
2008-10-16 06:57:26 +00:00
|
|
|
#include <cppuhelper/weak.hxx>
|
|
|
|
#include <tools/diagnose_ex.h>
|
|
|
|
|
|
|
|
namespace dbaccess
|
|
|
|
{
|
|
|
|
|
|
|
|
using ::com::sun::star::uno::Reference;
|
|
|
|
using ::com::sun::star::uno::XInterface;
|
|
|
|
using ::com::sun::star::uno::Exception;
|
|
|
|
using ::com::sun::star::uno::RuntimeException;
|
|
|
|
using ::com::sun::star::uno::Any;
|
|
|
|
using ::com::sun::star::uno::makeAny;
|
|
|
|
using ::com::sun::star::uno::Sequence;
|
|
|
|
using ::com::sun::star::uno::Type;
|
|
|
|
using ::com::sun::star::frame::DoubleInitializationException;
|
|
|
|
using ::com::sun::star::document::XDocumentEventListener;
|
|
|
|
using ::com::sun::star::document::DocumentEvent;
|
|
|
|
using ::com::sun::star::frame::XController2;
|
2013-04-01 18:45:57 +02:00
|
|
|
|
2008-10-16 06:57:26 +00:00
|
|
|
using namespace ::com::sun::star;
|
|
|
|
|
2013-08-17 23:43:14 +02:00
|
|
|
// DocumentEventHolder
|
2008-10-16 06:57:26 +00:00
|
|
|
typedef ::comphelper::EventHolder< DocumentEvent > DocumentEventHolder;
|
|
|
|
|
2013-08-17 23:43:14 +02:00
|
|
|
// DocumentEventNotifier_Impl
|
2008-10-16 06:57:26 +00:00
|
|
|
class DocumentEventNotifier_Impl : public ::comphelper::IEventProcessor
|
|
|
|
{
|
|
|
|
oslInterlockedCount m_refCount;
|
|
|
|
::cppu::OWeakObject& m_rDocument;
|
|
|
|
::osl::Mutex& m_rMutex;
|
|
|
|
bool m_bInitialized;
|
|
|
|
bool m_bDisposed;
|
|
|
|
::rtl::Reference< ::comphelper::AsyncEventNotifier > m_pEventBroadcaster;
|
2016-01-19 19:45:45 +02:00
|
|
|
::comphelper::OInterfaceContainerHelper2 m_aLegacyEventListeners;
|
|
|
|
::comphelper::OInterfaceContainerHelper2 m_aDocumentEventListeners;
|
2008-10-16 06:57:26 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
DocumentEventNotifier_Impl( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
|
|
|
|
:m_refCount( 0 )
|
|
|
|
,m_rDocument( _rBroadcasterDocument )
|
|
|
|
,m_rMutex( _rMutex )
|
|
|
|
,m_bInitialized( false )
|
|
|
|
,m_bDisposed( false )
|
|
|
|
,m_aLegacyEventListeners( _rMutex )
|
|
|
|
,m_aDocumentEventListeners( _rMutex )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-14 11:53:40 +01:00
|
|
|
// IEventProcessor
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual void SAL_CALL acquire() throw () override;
|
|
|
|
virtual void SAL_CALL release() throw () override;
|
2008-10-16 06:57:26 +00:00
|
|
|
|
|
|
|
void addLegacyEventListener( const Reference< document::XEventListener >& _Listener )
|
|
|
|
{
|
|
|
|
m_aLegacyEventListeners.addInterface( _Listener );
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeLegacyEventListener( const Reference< document::XEventListener >& _Listener )
|
|
|
|
{
|
|
|
|
m_aLegacyEventListeners.removeInterface( _Listener );
|
|
|
|
}
|
|
|
|
|
|
|
|
void addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
|
|
|
|
{
|
|
|
|
m_aDocumentEventListeners.addInterface( _Listener );
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
|
|
|
|
{
|
|
|
|
m_aDocumentEventListeners.removeInterface( _Listener );
|
|
|
|
}
|
|
|
|
|
|
|
|
void disposing();
|
|
|
|
|
|
|
|
void onDocumentInitialized();
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
void notifyDocumentEvent( const OUString& _EventName, const Reference< XController2 >& _ViewController,
|
2008-10-16 06:57:26 +00:00
|
|
|
const Any& _Supplement )
|
|
|
|
{
|
|
|
|
impl_notifyEvent_nothrow( DocumentEvent(
|
|
|
|
m_rDocument, _EventName, _ViewController, _Supplement ) );
|
|
|
|
}
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
void notifyDocumentEventAsync( const OUString& _EventName, const Reference< XController2 >& _ViewController,
|
2008-10-16 06:57:26 +00:00
|
|
|
const Any& _Supplement )
|
|
|
|
{
|
|
|
|
impl_notifyEventAsync_nothrow( DocumentEvent(
|
|
|
|
m_rDocument, _EventName, _ViewController, _Supplement ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~DocumentEventNotifier_Impl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// IEventProcessor
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual void processEvent( const ::comphelper::AnyEvent& _rEvent ) override;
|
2008-10-16 06:57:26 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void impl_notifyEvent_nothrow( const DocumentEvent& _rEvent );
|
|
|
|
void impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent );
|
|
|
|
};
|
|
|
|
|
2014-03-14 11:53:40 +01:00
|
|
|
void SAL_CALL DocumentEventNotifier_Impl::acquire() throw ()
|
2008-10-16 06:57:26 +00:00
|
|
|
{
|
2012-09-22 01:51:12 -05:00
|
|
|
osl_atomic_increment( &m_refCount );
|
2008-10-16 06:57:26 +00:00
|
|
|
}
|
|
|
|
|
2014-03-14 11:53:40 +01:00
|
|
|
void SAL_CALL DocumentEventNotifier_Impl::release() throw ()
|
2008-10-16 06:57:26 +00:00
|
|
|
{
|
2012-09-22 01:51:12 -05:00
|
|
|
if ( 0 == osl_atomic_decrement( &m_refCount ) )
|
2008-10-16 06:57:26 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentEventNotifier_Impl::disposing()
|
|
|
|
{
|
2008-12-01 12:31:27 +00:00
|
|
|
// SYNCHRONIZED ->
|
2008-10-16 06:57:26 +00:00
|
|
|
// cancel any pending asynchronous events
|
2008-12-01 12:31:27 +00:00
|
|
|
::osl::ResettableMutexGuard aGuard( m_rMutex );
|
2008-10-16 06:57:26 +00:00
|
|
|
if ( m_pEventBroadcaster.is() )
|
|
|
|
{
|
|
|
|
m_pEventBroadcaster->removeEventsForProcessor( this );
|
|
|
|
m_pEventBroadcaster->terminate();
|
2012-03-09 09:12:54 +01:00
|
|
|
//TODO: a protocol is missing how to join with the thread before
|
|
|
|
// exit(3), to ensure the thread is no longer relying on any
|
|
|
|
// infrastructure while that infrastructure is being shut down
|
|
|
|
// in atexit handlers; simply calling join here leads to
|
|
|
|
// deadlock, as this thread holds the solar mutex while the
|
|
|
|
// other thread is typically blocked waiting for the solar mutex
|
2012-02-23 10:37:09 +01:00
|
|
|
m_pEventBroadcaster.clear();
|
2008-10-16 06:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lang::EventObject aEvent( m_rDocument );
|
2008-12-01 12:31:27 +00:00
|
|
|
aGuard.clear();
|
|
|
|
// <-- SYNCHRONIZED
|
|
|
|
|
2008-10-16 06:57:26 +00:00
|
|
|
m_aLegacyEventListeners.disposeAndClear( aEvent );
|
|
|
|
m_aDocumentEventListeners.disposeAndClear( aEvent );
|
|
|
|
|
2008-12-01 12:31:27 +00:00
|
|
|
// SYNCHRONIZED ->
|
|
|
|
aGuard.reset();
|
2008-10-16 06:57:26 +00:00
|
|
|
m_bDisposed = true;
|
2008-12-01 12:31:27 +00:00
|
|
|
// <-- SYNCHRONIZED
|
2008-10-16 06:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentEventNotifier_Impl::onDocumentInitialized()
|
|
|
|
{
|
|
|
|
if ( m_bInitialized )
|
|
|
|
throw DoubleInitializationException();
|
|
|
|
|
|
|
|
m_bInitialized = true;
|
|
|
|
if ( m_pEventBroadcaster.is() )
|
|
|
|
// there are already pending asynchronous events
|
2012-02-23 10:37:09 +01:00
|
|
|
m_pEventBroadcaster->launch();
|
2008-10-16 06:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentEventNotifier_Impl::impl_notifyEvent_nothrow( const DocumentEvent& _rEvent )
|
|
|
|
{
|
|
|
|
OSL_PRECOND( m_bInitialized,
|
|
|
|
"DocumentEventNotifier_Impl::impl_notifyEvent_nothrow: only to be called when the document is already initialized!" );
|
|
|
|
try
|
|
|
|
{
|
|
|
|
document::EventObject aLegacyEvent( _rEvent.Source, _rEvent.EventName );
|
|
|
|
m_aLegacyEventListeners.notifyEach( &document::XEventListener::notifyEvent, aLegacyEvent );
|
|
|
|
}
|
|
|
|
catch(const Exception&)
|
|
|
|
{
|
|
|
|
DBG_UNHANDLED_EXCEPTION();
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_aDocumentEventListeners.notifyEach( &XDocumentEventListener::documentEventOccured, _rEvent );
|
|
|
|
}
|
|
|
|
catch( const Exception& )
|
|
|
|
{
|
|
|
|
DBG_UNHANDLED_EXCEPTION();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentEventNotifier_Impl::impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent )
|
|
|
|
{
|
|
|
|
if ( !m_pEventBroadcaster.is() )
|
|
|
|
{
|
2012-02-23 10:37:09 +01:00
|
|
|
m_pEventBroadcaster.set(
|
|
|
|
new ::comphelper::AsyncEventNotifier("DocumentEventNotifier"));
|
2008-10-16 06:57:26 +00:00
|
|
|
if ( m_bInitialized )
|
|
|
|
// start processing the events if and only if we (our document, respectively) are
|
|
|
|
// already initialized
|
2012-02-23 10:37:09 +01:00
|
|
|
m_pEventBroadcaster->launch();
|
2008-10-16 06:57:26 +00:00
|
|
|
}
|
|
|
|
m_pEventBroadcaster->addEvent( new DocumentEventHolder( _rEvent ), this );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentEventNotifier_Impl::processEvent( const ::comphelper::AnyEvent& _rEvent )
|
|
|
|
{
|
|
|
|
// beware, this is called from the notification thread
|
|
|
|
{
|
|
|
|
::osl::MutexGuard aGuard( m_rMutex );
|
|
|
|
if ( m_bDisposed )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const DocumentEventHolder& rEventHolder = dynamic_cast< const DocumentEventHolder& >( _rEvent );
|
|
|
|
impl_notifyEvent_nothrow( rEventHolder.getEventObject() );
|
|
|
|
}
|
|
|
|
|
2013-08-17 23:43:14 +02:00
|
|
|
// DocumentEventNotifier
|
2008-10-16 06:57:26 +00:00
|
|
|
DocumentEventNotifier::DocumentEventNotifier( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
|
|
|
|
:m_pImpl( new DocumentEventNotifier_Impl( _rBroadcasterDocument, _rMutex ) )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentEventNotifier::~DocumentEventNotifier()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentEventNotifier::disposing()
|
|
|
|
{
|
|
|
|
m_pImpl->disposing();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentEventNotifier::onDocumentInitialized()
|
|
|
|
{
|
|
|
|
m_pImpl->onDocumentInitialized();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentEventNotifier::addLegacyEventListener( const Reference< document::XEventListener >& _Listener )
|
|
|
|
{
|
|
|
|
m_pImpl->addLegacyEventListener( _Listener );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentEventNotifier::removeLegacyEventListener( const Reference< document::XEventListener >& _Listener )
|
|
|
|
{
|
|
|
|
m_pImpl->removeLegacyEventListener( _Listener );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentEventNotifier::addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
|
|
|
|
{
|
|
|
|
m_pImpl->addDocumentEventListener( _Listener );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentEventNotifier::removeDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
|
|
|
|
{
|
|
|
|
m_pImpl->removeDocumentEventListener( _Listener );
|
|
|
|
}
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
void DocumentEventNotifier::notifyDocumentEvent( const OUString& _EventName,
|
2008-10-16 06:57:26 +00:00
|
|
|
const Reference< XController2 >& _ViewController, const Any& _Supplement )
|
|
|
|
{
|
|
|
|
m_pImpl->notifyDocumentEvent( _EventName, _ViewController, _Supplement );
|
|
|
|
}
|
|
|
|
|
2013-04-07 12:06:47 +02:00
|
|
|
void DocumentEventNotifier::notifyDocumentEventAsync( const OUString& _EventName,
|
2008-10-16 06:57:26 +00:00
|
|
|
const Reference< XController2 >& _ViewController, const Any& _Supplement )
|
|
|
|
{
|
|
|
|
m_pImpl->notifyDocumentEventAsync( _EventName, _ViewController, _Supplement );
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dbaccess
|
2013-08-17 23:43:14 +02:00
|
|
|
|
2010-10-12 15:59:03 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|