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_comphelper.hxx" 30*cdf0e10cSrcweir #include <comphelper/accessiblecontexthelper.hxx> 31*cdf0e10cSrcweir #include <comphelper/accessibleeventbuffer.hxx> 32*cdf0e10cSrcweir #include <osl/diagnose.h> 33*cdf0e10cSrcweir #include <cppuhelper/weakref.hxx> 34*cdf0e10cSrcweir #include <com/sun/star/accessibility/AccessibleEventId.hpp> 35*cdf0e10cSrcweir #include <com/sun/star/accessibility/AccessibleStateType.hpp> 36*cdf0e10cSrcweir #include <comphelper/accessibleeventnotifier.hxx> 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir //......................................................................... 39*cdf0e10cSrcweir namespace comphelper 40*cdf0e10cSrcweir { 41*cdf0e10cSrcweir //......................................................................... 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 44*cdf0e10cSrcweir using namespace ::com::sun::star::lang; 45*cdf0e10cSrcweir using namespace ::com::sun::star::accessibility; 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir //===================================================================== 48*cdf0e10cSrcweir //= OContextHelper_Impl 49*cdf0e10cSrcweir //===================================================================== 50*cdf0e10cSrcweir /** implementation class for OAccessibleContextHelper. No own thread safety! 51*cdf0e10cSrcweir */ 52*cdf0e10cSrcweir class OContextHelper_Impl 53*cdf0e10cSrcweir { 54*cdf0e10cSrcweir private: 55*cdf0e10cSrcweir OAccessibleContextHelper* m_pAntiImpl; // the owning instance 56*cdf0e10cSrcweir IMutex* m_pExternalLock; // the optional additional external lock 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir ::cppu::OInterfaceContainerHelper* m_pEventListeners; 59*cdf0e10cSrcweir WeakReference< XAccessible > m_aCreator; // the XAccessible which created our XAccessibleContext 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir AccessibleEventNotifier::TClientId m_nClientId; 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir public: 64*cdf0e10cSrcweir inline Reference< XAccessible > getCreator( ) const { return m_aCreator; } 65*cdf0e10cSrcweir inline void setCreator( const Reference< XAccessible >& _rAcc ); 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir inline IMutex* getExternalLock( ) { return m_pExternalLock; } 68*cdf0e10cSrcweir inline void setExternalLock( IMutex* _pLock ) { m_pExternalLock = _pLock; } 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir inline AccessibleEventNotifier::TClientId 71*cdf0e10cSrcweir getClientId() const { return m_nClientId; } 72*cdf0e10cSrcweir inline void setClientId( const AccessibleEventNotifier::TClientId _nId ) 73*cdf0e10cSrcweir { m_nClientId = _nId; } 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir public: 76*cdf0e10cSrcweir OContextHelper_Impl( OAccessibleContextHelper* _pAntiImpl ) 77*cdf0e10cSrcweir :m_pAntiImpl( _pAntiImpl ) 78*cdf0e10cSrcweir ,m_pExternalLock( NULL ) 79*cdf0e10cSrcweir ,m_pEventListeners( NULL ) 80*cdf0e10cSrcweir ,m_nClientId( 0 ) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir }; 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir //--------------------------------------------------------------------- 86*cdf0e10cSrcweir inline void OContextHelper_Impl::setCreator( const Reference< XAccessible >& _rAcc ) 87*cdf0e10cSrcweir { 88*cdf0e10cSrcweir m_aCreator = _rAcc; 89*cdf0e10cSrcweir } 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir //===================================================================== 92*cdf0e10cSrcweir //= OAccessibleContextHelper 93*cdf0e10cSrcweir //===================================================================== 94*cdf0e10cSrcweir //--------------------------------------------------------------------- 95*cdf0e10cSrcweir OAccessibleContextHelper::OAccessibleContextHelper( ) 96*cdf0e10cSrcweir :OAccessibleContextHelper_Base( GetMutex() ) 97*cdf0e10cSrcweir ,m_pImpl( NULL ) 98*cdf0e10cSrcweir { 99*cdf0e10cSrcweir m_pImpl = new OContextHelper_Impl( this ); 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir //--------------------------------------------------------------------- 103*cdf0e10cSrcweir OAccessibleContextHelper::OAccessibleContextHelper( IMutex* _pExternalLock ) 104*cdf0e10cSrcweir :OAccessibleContextHelper_Base( GetMutex() ) 105*cdf0e10cSrcweir ,m_pImpl( NULL ) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir m_pImpl = new OContextHelper_Impl( this ); 108*cdf0e10cSrcweir m_pImpl->setExternalLock( _pExternalLock ); 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir //--------------------------------------------------------------------- 112*cdf0e10cSrcweir void OAccessibleContextHelper::forgetExternalLock() 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir m_pImpl->setExternalLock( NULL ); 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir //--------------------------------------------------------------------- 118*cdf0e10cSrcweir OAccessibleContextHelper::~OAccessibleContextHelper( ) 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir forgetExternalLock(); 121*cdf0e10cSrcweir // this ensures that the lock, which may be already destroyed as part of the derivee, 122*cdf0e10cSrcweir // is not used anymore 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir ensureDisposed(); 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir delete m_pImpl; 127*cdf0e10cSrcweir m_pImpl = NULL; 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir //--------------------------------------------------------------------- 131*cdf0e10cSrcweir IMutex* OAccessibleContextHelper::getExternalLock( ) 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir return m_pImpl->getExternalLock(); 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir //--------------------------------------------------------------------- 137*cdf0e10cSrcweir void SAL_CALL OAccessibleContextHelper::disposing() 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( GetMutex() ); 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir if ( m_pImpl->getClientId( ) ) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir AccessibleEventNotifier::revokeClientNotifyDisposing( m_pImpl->getClientId( ), *this ); 144*cdf0e10cSrcweir m_pImpl->setClientId( 0 ); 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir //--------------------------------------------------------------------- 149*cdf0e10cSrcweir void SAL_CALL OAccessibleContextHelper::addEventListener( const Reference< XAccessibleEventListener >& _rxListener ) throw (RuntimeException) 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir OMutexGuard aGuard( getExternalLock() ); 152*cdf0e10cSrcweir // don't use the OContextEntryGuard - it will throw an exception if we're not alive 153*cdf0e10cSrcweir // anymore, while the most recent specification for XComponent states that we should 154*cdf0e10cSrcweir // silently ignore the call in such a situation 155*cdf0e10cSrcweir if ( !isAlive() ) 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir if ( _rxListener.is() ) 158*cdf0e10cSrcweir _rxListener->disposing( EventObject( *this ) ); 159*cdf0e10cSrcweir return; 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir if ( _rxListener.is() ) 163*cdf0e10cSrcweir { 164*cdf0e10cSrcweir if ( !m_pImpl->getClientId( ) ) 165*cdf0e10cSrcweir m_pImpl->setClientId( AccessibleEventNotifier::registerClient( ) ); 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir AccessibleEventNotifier::addEventListener( m_pImpl->getClientId( ), _rxListener ); 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir //--------------------------------------------------------------------- 172*cdf0e10cSrcweir void SAL_CALL OAccessibleContextHelper::removeEventListener( const Reference< XAccessibleEventListener >& _rxListener ) throw (RuntimeException) 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir OMutexGuard aGuard( getExternalLock() ); 175*cdf0e10cSrcweir // don't use the OContextEntryGuard - it will throw an exception if we're not alive 176*cdf0e10cSrcweir // anymore, while the most recent specification for XComponent states that we should 177*cdf0e10cSrcweir // silently ignore the call in such a situation 178*cdf0e10cSrcweir if ( !isAlive() ) 179*cdf0e10cSrcweir return; 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir if ( _rxListener.is() ) 182*cdf0e10cSrcweir { 183*cdf0e10cSrcweir sal_Int32 nListenerCount = AccessibleEventNotifier::removeEventListener( m_pImpl->getClientId( ), _rxListener ); 184*cdf0e10cSrcweir if ( !nListenerCount ) 185*cdf0e10cSrcweir { 186*cdf0e10cSrcweir // no listeners anymore 187*cdf0e10cSrcweir // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client), 188*cdf0e10cSrcweir // and at least to us not firing any events anymore, in case somebody calls 189*cdf0e10cSrcweir // NotifyAccessibleEvent, again 190*cdf0e10cSrcweir AccessibleEventNotifier::revokeClient( m_pImpl->getClientId( ) ); 191*cdf0e10cSrcweir m_pImpl->setClientId( 0 ); 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir //--------------------------------------------------------------------- 197*cdf0e10cSrcweir void SAL_CALL OAccessibleContextHelper::NotifyAccessibleEvent( const sal_Int16 _nEventId, 198*cdf0e10cSrcweir const Any& _rOldValue, const Any& _rNewValue ) 199*cdf0e10cSrcweir { 200*cdf0e10cSrcweir if ( !m_pImpl->getClientId( ) ) 201*cdf0e10cSrcweir // if we don't have a client id for the notifier, then we don't have listeners, then 202*cdf0e10cSrcweir // we don't need to notify anything 203*cdf0e10cSrcweir return; 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir // build an event object 206*cdf0e10cSrcweir AccessibleEventObject aEvent; 207*cdf0e10cSrcweir aEvent.Source = *this; 208*cdf0e10cSrcweir aEvent.EventId = _nEventId; 209*cdf0e10cSrcweir aEvent.OldValue = _rOldValue; 210*cdf0e10cSrcweir aEvent.NewValue = _rNewValue; 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir // let the notifier handle this event 213*cdf0e10cSrcweir AccessibleEventNotifier::addEvent( m_pImpl->getClientId( ), aEvent ); 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir //--------------------------------------------------------------------- 217*cdf0e10cSrcweir void SAL_CALL OAccessibleContextHelper::BufferAccessibleEvent( const sal_Int16 _nEventId, 218*cdf0e10cSrcweir const Any& _rOldValue, const Any& _rNewValue, 219*cdf0e10cSrcweir AccessibleEventBuffer & _rBuffer ) 220*cdf0e10cSrcweir { 221*cdf0e10cSrcweir // TODO: this whole method (as well as the class AccessibleEventBuffer) should be removed 222*cdf0e10cSrcweir // The reasons why they have been introduces id that we needed to collect a set of events 223*cdf0e10cSrcweir // before notifying them alltogether (after releasing our mutex). With the other 224*cdf0e10cSrcweir // NotifyAccessibleEvent being asynchronous now, this should not be necessary anymore 225*cdf0e10cSrcweir // - clients could use the other version now. 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir // copy our current listeners 228*cdf0e10cSrcweir Sequence< Reference< XInterface > > aListeners; 229*cdf0e10cSrcweir if ( m_pImpl->getClientId( ) ) 230*cdf0e10cSrcweir aListeners = AccessibleEventNotifier::getEventListeners( m_pImpl->getClientId( ) ); 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir if ( aListeners.getLength() ) 233*cdf0e10cSrcweir { 234*cdf0e10cSrcweir AccessibleEventObject aEvent; 235*cdf0e10cSrcweir aEvent.Source = *this; 236*cdf0e10cSrcweir OSL_ENSURE( aEvent.Source.is(), "OAccessibleContextHelper::BufferAccessibleEvent: invalid creator!" ); 237*cdf0e10cSrcweir aEvent.EventId = _nEventId; 238*cdf0e10cSrcweir aEvent.OldValue = _rOldValue; 239*cdf0e10cSrcweir aEvent.NewValue = _rNewValue; 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir _rBuffer.addEvent( aEvent, aListeners ); 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir //--------------------------------------------------------------------- 246*cdf0e10cSrcweir sal_Bool OAccessibleContextHelper::isAlive() const 247*cdf0e10cSrcweir { 248*cdf0e10cSrcweir return !GetBroadcastHelper().bDisposed && !GetBroadcastHelper().bInDispose; 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir //--------------------------------------------------------------------- 252*cdf0e10cSrcweir void OAccessibleContextHelper::ensureAlive() const SAL_THROW( ( DisposedException ) ) 253*cdf0e10cSrcweir { 254*cdf0e10cSrcweir if( !isAlive() ) 255*cdf0e10cSrcweir throw DisposedException(); 256*cdf0e10cSrcweir } 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir //--------------------------------------------------------------------- 259*cdf0e10cSrcweir void OAccessibleContextHelper::ensureDisposed( ) 260*cdf0e10cSrcweir { 261*cdf0e10cSrcweir if ( !GetBroadcastHelper().bDisposed ) 262*cdf0e10cSrcweir { 263*cdf0e10cSrcweir OSL_ENSURE( 0 == m_refCount, "OAccessibleContextHelper::ensureDisposed: this method _has_ to be called from without your dtor only!" ); 264*cdf0e10cSrcweir acquire(); 265*cdf0e10cSrcweir dispose(); 266*cdf0e10cSrcweir } 267*cdf0e10cSrcweir } 268*cdf0e10cSrcweir 269*cdf0e10cSrcweir //--------------------------------------------------------------------- 270*cdf0e10cSrcweir void OAccessibleContextHelper::lateInit( const Reference< XAccessible >& _rxAccessible ) 271*cdf0e10cSrcweir { 272*cdf0e10cSrcweir m_pImpl->setCreator( _rxAccessible ); 273*cdf0e10cSrcweir } 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir //--------------------------------------------------------------------- 276*cdf0e10cSrcweir Reference< XAccessible > OAccessibleContextHelper::getAccessibleCreator( ) const 277*cdf0e10cSrcweir { 278*cdf0e10cSrcweir return m_pImpl->getCreator(); 279*cdf0e10cSrcweir } 280*cdf0e10cSrcweir 281*cdf0e10cSrcweir //--------------------------------------------------------------------- 282*cdf0e10cSrcweir sal_Int32 SAL_CALL OAccessibleContextHelper::getAccessibleIndexInParent( ) throw (RuntimeException) 283*cdf0e10cSrcweir { 284*cdf0e10cSrcweir OExternalLockGuard aGuard( this ); 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir // -1 for child not found/no parent (according to specification) 287*cdf0e10cSrcweir sal_Int32 nRet = -1; 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir try 290*cdf0e10cSrcweir { 291*cdf0e10cSrcweir 292*cdf0e10cSrcweir Reference< XAccessibleContext > xParentContext( implGetParentContext() ); 293*cdf0e10cSrcweir 294*cdf0e10cSrcweir // iterate over parent's children and search for this object 295*cdf0e10cSrcweir if ( xParentContext.is() ) 296*cdf0e10cSrcweir { 297*cdf0e10cSrcweir // our own XAccessible for comparing with the children of our parent 298*cdf0e10cSrcweir Reference< XAccessible > xCreator( m_pImpl->getCreator() ); 299*cdf0e10cSrcweir 300*cdf0e10cSrcweir OSL_ENSURE( xCreator.is(), "OAccessibleContextHelper::getAccessibleIndexInParent: invalid creator!" ); 301*cdf0e10cSrcweir // two ideas why this could be NULL: 302*cdf0e10cSrcweir // * nobody called our late ctor (init), so we never had a creator at all -> bad 303*cdf0e10cSrcweir // * the creator is already dead. In this case, we should have been disposed, and 304*cdf0e10cSrcweir // never survived the above OContextEntryGuard. 305*cdf0e10cSrcweir // in all other situations the creator should be non-NULL 306*cdf0e10cSrcweir 307*cdf0e10cSrcweir if ( xCreator.is() ) 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir sal_Int32 nChildCount = xParentContext->getAccessibleChildCount(); 310*cdf0e10cSrcweir for ( sal_Int32 nChild = 0; ( nChild < nChildCount ) && ( -1 == nRet ); ++nChild ) 311*cdf0e10cSrcweir { 312*cdf0e10cSrcweir Reference< XAccessible > xChild( xParentContext->getAccessibleChild( nChild ) ); 313*cdf0e10cSrcweir if ( xChild.get() == xCreator.get() ) 314*cdf0e10cSrcweir nRet = nChild; 315*cdf0e10cSrcweir } 316*cdf0e10cSrcweir } 317*cdf0e10cSrcweir } 318*cdf0e10cSrcweir } 319*cdf0e10cSrcweir catch( const Exception& ) 320*cdf0e10cSrcweir { 321*cdf0e10cSrcweir OSL_ENSURE( sal_False, "OAccessibleContextHelper::getAccessibleIndexInParent: caught an exception!" ); 322*cdf0e10cSrcweir } 323*cdf0e10cSrcweir 324*cdf0e10cSrcweir return nRet; 325*cdf0e10cSrcweir } 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir //--------------------------------------------------------------------- 328*cdf0e10cSrcweir Locale SAL_CALL OAccessibleContextHelper::getLocale( ) throw (IllegalAccessibleComponentStateException, RuntimeException) 329*cdf0e10cSrcweir { 330*cdf0e10cSrcweir // simply ask the parent 331*cdf0e10cSrcweir Reference< XAccessible > xParent = getAccessibleParent(); 332*cdf0e10cSrcweir Reference< XAccessibleContext > xParentContext; 333*cdf0e10cSrcweir if ( xParent.is() ) 334*cdf0e10cSrcweir xParentContext = xParent->getAccessibleContext(); 335*cdf0e10cSrcweir 336*cdf0e10cSrcweir if ( !xParentContext.is() ) 337*cdf0e10cSrcweir throw IllegalAccessibleComponentStateException( ::rtl::OUString(), *this ); 338*cdf0e10cSrcweir 339*cdf0e10cSrcweir return xParentContext->getLocale(); 340*cdf0e10cSrcweir } 341*cdf0e10cSrcweir 342*cdf0e10cSrcweir //--------------------------------------------------------------------- 343*cdf0e10cSrcweir Reference< XAccessibleContext > OAccessibleContextHelper::implGetParentContext() SAL_THROW( ( RuntimeException ) ) 344*cdf0e10cSrcweir { 345*cdf0e10cSrcweir Reference< XAccessible > xParent = getAccessibleParent(); 346*cdf0e10cSrcweir Reference< XAccessibleContext > xParentContext; 347*cdf0e10cSrcweir if ( xParent.is() ) 348*cdf0e10cSrcweir xParentContext = xParent->getAccessibleContext(); 349*cdf0e10cSrcweir return xParentContext; 350*cdf0e10cSrcweir } 351*cdf0e10cSrcweir 352*cdf0e10cSrcweir //......................................................................... 353*cdf0e10cSrcweir } // namespace comphelper 354*cdf0e10cSrcweir //......................................................................... 355*cdf0e10cSrcweir 356*cdf0e10cSrcweir 357