2010-10-14 08:27:31 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2002-12-06 11:56:46 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2008-04-11 10:02:14 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
2002-12-06 11:56:46 +00:00
|
|
|
*
|
2010-02-12 15:01:35 +01:00
|
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
2002-12-06 11:56:46 +00:00
|
|
|
*
|
2008-04-11 10:02:14 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2002-12-06 11:56:46 +00:00
|
|
|
*
|
2008-04-11 10:02:14 +00:00
|
|
|
* This file is part of OpenOffice.org.
|
2002-12-06 11:56:46 +00:00
|
|
|
*
|
2008-04-11 10:02:14 +00:00
|
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
* only, as published by the Free Software Foundation.
|
2002-12-06 11:56:46 +00:00
|
|
|
*
|
2008-04-11 10:02:14 +00:00
|
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License version 3 for more details
|
|
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
2002-12-06 11:56:46 +00:00
|
|
|
*
|
2008-04-11 10:02:14 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
|
|
* <http://www.openoffice.org/license.html>
|
|
|
|
* for a copy of the LGPLv3 License.
|
2002-12-06 11:56:46 +00:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#include <comphelper/accessibleeventnotifier.hxx>
|
|
|
|
#include <osl/diagnose.h>
|
2004-06-25 16:20:53 +00:00
|
|
|
#include <rtl/instance.hxx>
|
2002-12-06 11:56:46 +00:00
|
|
|
#include <comphelper/guarding.hxx>
|
|
|
|
|
2004-06-25 16:20:53 +00:00
|
|
|
using namespace ::com::sun::star::uno;
|
|
|
|
using namespace ::com::sun::star::lang;
|
|
|
|
using namespace ::com::sun::star::accessibility;
|
|
|
|
using namespace ::comphelper;
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
//= AccessibleEventNotifier
|
|
|
|
//=====================================================================
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct lclMutex
|
|
|
|
: public rtl::Static< ::osl::Mutex, lclMutex > {};
|
|
|
|
struct Clients
|
|
|
|
: public rtl::Static< AccessibleEventNotifier::ClientMap, Clients > {};
|
|
|
|
}
|
|
|
|
|
2002-12-06 11:56:46 +00:00
|
|
|
//.........................................................................
|
|
|
|
namespace comphelper
|
|
|
|
{
|
|
|
|
//.........................................................................
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
AccessibleEventNotifier::TClientId AccessibleEventNotifier::generateId()
|
|
|
|
{
|
|
|
|
TClientId nBiggestUsedId = 0;
|
|
|
|
TClientId nFreeId = 0;
|
|
|
|
|
|
|
|
// look through all registered clients until we find a "gap" in the ids
|
|
|
|
|
|
|
|
// Note that the following relies on the fact the elements in the map are traveled with
|
|
|
|
// ascending keys (aka client ids)
|
2004-06-25 16:20:53 +00:00
|
|
|
AccessibleEventNotifier::ClientMap &rClients = Clients::get();
|
|
|
|
for ( ClientMap::const_iterator aLookup = rClients.begin();
|
|
|
|
aLookup != rClients.end();
|
2002-12-06 11:56:46 +00:00
|
|
|
++aLookup
|
|
|
|
)
|
|
|
|
{
|
|
|
|
TClientId nCurrent = aLookup->first;
|
|
|
|
OSL_ENSURE( nCurrent > nBiggestUsedId, "AccessibleEventNotifier::generateId: map is expected to be sorted ascending!" );
|
|
|
|
|
|
|
|
if ( nCurrent - nBiggestUsedId > 1 )
|
|
|
|
{ // found a "gap"
|
2003-03-19 15:03:25 +00:00
|
|
|
nFreeId = nBiggestUsedId + 1;
|
|
|
|
break;
|
2002-12-06 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nBiggestUsedId = nCurrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !nFreeId )
|
|
|
|
nFreeId = nBiggestUsedId + 1;
|
|
|
|
|
2004-06-25 16:20:53 +00:00
|
|
|
OSL_ENSURE( rClients.end() == rClients.find( nFreeId ),
|
2002-12-06 11:56:46 +00:00
|
|
|
"AccessibleEventNotifier::generateId: algorithm broken!" );
|
|
|
|
|
|
|
|
return nFreeId;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
AccessibleEventNotifier::TClientId AccessibleEventNotifier::registerClient( )
|
|
|
|
{
|
2004-06-25 16:20:53 +00:00
|
|
|
::osl::MutexGuard aGuard( lclMutex::get() );
|
2002-12-06 11:56:46 +00:00
|
|
|
|
|
|
|
// generate a new client id
|
2003-03-19 15:03:25 +00:00
|
|
|
TClientId nNewClientId = generateId( );
|
2002-12-06 11:56:46 +00:00
|
|
|
|
|
|
|
// the event listeners for the new client
|
2004-06-25 16:20:53 +00:00
|
|
|
EventListeners* pNewListeners = new EventListeners( lclMutex::get() );
|
2002-12-06 11:56:46 +00:00
|
|
|
// note that we're using our own mutex here, so the listener containers for all
|
|
|
|
// our clients share this same mutex.
|
2003-03-19 15:03:25 +00:00
|
|
|
// this is a reminiscense to the days where the notifier was asynchronous. Today this is
|
|
|
|
// completely nonsense, and potentially slowing down the Office me thinks ...
|
2002-12-06 11:56:46 +00:00
|
|
|
|
|
|
|
// add the client
|
2004-06-25 16:20:53 +00:00
|
|
|
Clients::get().insert( ClientMap::value_type( nNewClientId, pNewListeners ) );
|
2002-12-06 11:56:46 +00:00
|
|
|
|
|
|
|
// outta here
|
|
|
|
return nNewClientId;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
sal_Bool AccessibleEventNotifier::implLookupClient( const TClientId _nClient, ClientMap::iterator& _rPos )
|
|
|
|
{
|
|
|
|
// look up this client
|
2004-06-25 16:20:53 +00:00
|
|
|
AccessibleEventNotifier::ClientMap &rClients = Clients::get();
|
|
|
|
_rPos = rClients.find( _nClient );
|
|
|
|
OSL_ENSURE( rClients.end() != _rPos, "AccessibleEventNotifier::implLookupClient: invalid client id (did you register your client?)!" );
|
2002-12-06 11:56:46 +00:00
|
|
|
|
2004-06-25 16:20:53 +00:00
|
|
|
return ( rClients.end() != _rPos );
|
2002-12-06 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
void AccessibleEventNotifier::revokeClient( const TClientId _nClient )
|
|
|
|
{
|
2004-06-25 16:20:53 +00:00
|
|
|
::osl::MutexGuard aGuard( lclMutex::get() );
|
2003-01-22 14:48:55 +00:00
|
|
|
|
2003-03-19 15:03:25 +00:00
|
|
|
ClientMap::iterator aClientPos;
|
|
|
|
if ( !implLookupClient( _nClient, aClientPos ) )
|
|
|
|
// already asserted in implLookupClient
|
|
|
|
return;
|
2002-12-06 11:56:46 +00:00
|
|
|
|
2003-03-19 15:03:25 +00:00
|
|
|
// remove it from the clients map
|
|
|
|
delete aClientPos->second;
|
2004-06-25 16:20:53 +00:00
|
|
|
Clients::get().erase( aClientPos );
|
2002-12-06 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
void AccessibleEventNotifier::revokeClientNotifyDisposing( const TClientId _nClient,
|
|
|
|
const Reference< XInterface >& _rxEventSource ) SAL_THROW( ( ) )
|
|
|
|
{
|
2004-06-25 16:20:53 +00:00
|
|
|
::osl::MutexGuard aGuard( lclMutex::get() );
|
2002-12-06 11:56:46 +00:00
|
|
|
|
2003-03-19 15:03:25 +00:00
|
|
|
ClientMap::iterator aClientPos;
|
|
|
|
if ( !implLookupClient( _nClient, aClientPos ) )
|
|
|
|
// already asserted in implLookupClient
|
|
|
|
return;
|
2002-12-06 11:56:46 +00:00
|
|
|
|
2003-03-19 15:03:25 +00:00
|
|
|
// notify the "disposing" event for this client
|
|
|
|
EventObject aDisposalEvent;
|
|
|
|
aDisposalEvent.Source = _rxEventSource;
|
2002-12-06 11:56:46 +00:00
|
|
|
|
2003-05-19 11:57:08 +00:00
|
|
|
// notify the listeners
|
|
|
|
EventListeners* pListeners = aClientPos->second;
|
2002-12-06 11:56:46 +00:00
|
|
|
|
2003-03-19 15:03:25 +00:00
|
|
|
// we do not need the entry in the clients map anymore
|
2003-05-19 11:57:08 +00:00
|
|
|
// (do this before actually notifying, because some client implementations have re-entrance
|
|
|
|
// problems and call into revokeClient while we are notifying from hereing)
|
2004-06-25 16:20:53 +00:00
|
|
|
Clients::get().erase( aClientPos );
|
2003-05-19 11:57:08 +00:00
|
|
|
|
|
|
|
// now really do the notification
|
|
|
|
pListeners->disposeAndClear( aDisposalEvent );
|
|
|
|
delete pListeners;
|
|
|
|
|
2002-12-06 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
sal_Int32 AccessibleEventNotifier::addEventListener(
|
|
|
|
const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener ) SAL_THROW( ( ) )
|
|
|
|
{
|
2004-06-25 16:20:53 +00:00
|
|
|
::osl::MutexGuard aGuard( lclMutex::get() );
|
2002-12-06 11:56:46 +00:00
|
|
|
|
|
|
|
ClientMap::iterator aClientPos;
|
|
|
|
if ( !implLookupClient( _nClient, aClientPos ) )
|
|
|
|
// already asserted in implLookupClient
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ( _rxListener.is() )
|
|
|
|
aClientPos->second->addInterface( _rxListener );
|
|
|
|
|
|
|
|
return aClientPos->second->getLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
sal_Int32 AccessibleEventNotifier::removeEventListener(
|
|
|
|
const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener ) SAL_THROW( ( ) )
|
|
|
|
{
|
2004-06-25 16:20:53 +00:00
|
|
|
::osl::MutexGuard aGuard( lclMutex::get() );
|
2002-12-06 11:56:46 +00:00
|
|
|
|
|
|
|
ClientMap::iterator aClientPos;
|
|
|
|
if ( !implLookupClient( _nClient, aClientPos ) )
|
|
|
|
// already asserted in implLookupClient
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ( _rxListener.is() )
|
|
|
|
aClientPos->second->removeInterface( _rxListener );
|
|
|
|
|
|
|
|
return aClientPos->second->getLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
void AccessibleEventNotifier::addEvent( const TClientId _nClient, const AccessibleEventObject& _rEvent ) SAL_THROW( ( ) )
|
|
|
|
{
|
2003-03-19 15:03:25 +00:00
|
|
|
Sequence< Reference< XInterface > > aListeners;
|
2002-12-06 11:56:46 +00:00
|
|
|
|
2003-03-19 15:03:25 +00:00
|
|
|
// --- <mutex lock> -------------------------------
|
|
|
|
{
|
2004-06-25 16:20:53 +00:00
|
|
|
::osl::MutexGuard aGuard( lclMutex::get() );
|
2002-12-06 11:56:46 +00:00
|
|
|
|
2003-03-19 15:03:25 +00:00
|
|
|
ClientMap::iterator aClientPos;
|
|
|
|
if ( !implLookupClient( _nClient, aClientPos ) )
|
|
|
|
// already asserted in implLookupClient
|
|
|
|
return;
|
2002-12-06 11:56:46 +00:00
|
|
|
|
2003-03-19 15:03:25 +00:00
|
|
|
// since we're synchronous, again, we want to notify immediately
|
|
|
|
aListeners = aClientPos->second->getElements();
|
|
|
|
}
|
|
|
|
// --- </mutex lock> ------------------------------
|
2003-01-22 14:48:55 +00:00
|
|
|
|
2003-03-19 15:03:25 +00:00
|
|
|
// default handling: loop through all listeners, and notify them
|
|
|
|
const Reference< XInterface >* pListeners = aListeners.getConstArray();
|
|
|
|
const Reference< XInterface >* pListenersEnd = pListeners + aListeners.getLength();
|
|
|
|
while ( pListeners != pListenersEnd )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static_cast< XAccessibleEventListener* >( pListeners->get() )->notifyEvent( _rEvent );
|
|
|
|
}
|
2006-06-19 21:46:44 +00:00
|
|
|
catch( const Exception& )
|
2003-03-19 15:03:25 +00:00
|
|
|
{
|
|
|
|
// no assertion, because a broken access remote bridge or something like this
|
|
|
|
// can cause this exception
|
|
|
|
}
|
|
|
|
++pListeners;
|
|
|
|
}
|
2002-12-06 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//.........................................................................
|
|
|
|
} // namespace comphelper
|
|
|
|
//.........................................................................
|
2003-01-22 14:48:55 +00:00
|
|
|
|
2010-10-14 08:27:31 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|