bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / misc / accessiblecontexthelper.cxx
blobde3209f0eb4dd97dd7da2b17d8f7c6dcf42be85f
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 <comphelper/accessiblecontexthelper.hxx>
21 #include <osl/diagnose.h>
22 #include <cppuhelper/weakref.hxx>
23 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
24 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
25 #include <comphelper/accessibleeventnotifier.hxx>
28 namespace comphelper
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::lang;
34 using namespace ::com::sun::star::accessibility;
36 IMutex::~IMutex() {}
38 /** implementation class for OAccessibleContextHelper. No own thread safety!
40 class OContextHelper_Impl
42 private:
43 IMutex* m_pExternalLock; // the optional additional external lock
45 WeakReference< XAccessible > m_aCreator; // the XAccessible which created our XAccessibleContext
47 AccessibleEventNotifier::TClientId m_nClientId;
49 public:
50 inline Reference< XAccessible > getCreator( ) const { return m_aCreator; }
51 inline void setCreator( const Reference< XAccessible >& _rAcc );
53 inline IMutex* getExternalLock( ) { return m_pExternalLock; }
54 inline void setExternalLock( IMutex* _pLock ) { m_pExternalLock = _pLock; }
56 inline AccessibleEventNotifier::TClientId
57 getClientId() const { return m_nClientId; }
58 inline void setClientId( const AccessibleEventNotifier::TClientId _nId )
59 { m_nClientId = _nId; }
61 public:
62 OContextHelper_Impl()
63 :m_pExternalLock( NULL )
64 ,m_nClientId( 0 )
70 inline void OContextHelper_Impl::setCreator( const Reference< XAccessible >& _rAcc )
72 m_aCreator = _rAcc;
75 OAccessibleContextHelper::OAccessibleContextHelper( IMutex* _pExternalLock )
76 :OAccessibleContextHelper_Base( GetMutex() )
77 ,m_pImpl( NULL )
79 assert(_pExternalLock);
80 m_pImpl = new OContextHelper_Impl();
81 m_pImpl->setExternalLock( _pExternalLock );
85 void OAccessibleContextHelper::forgetExternalLock()
87 m_pImpl->setExternalLock( NULL );
91 OAccessibleContextHelper::~OAccessibleContextHelper( )
93 forgetExternalLock();
94 // this ensures that the lock, which may be already destroyed as part of the derivee,
95 // is not used anymore
97 ensureDisposed();
99 delete m_pImpl;
100 m_pImpl = NULL;
104 IMutex* OAccessibleContextHelper::getExternalLock( )
106 return m_pImpl->getExternalLock();
110 void SAL_CALL OAccessibleContextHelper::disposing()
112 // rhbz#1001768: de facto this class is locked by SolarMutex;
113 // do not lock m_Mutex because it may cause deadlock
114 OMutexGuard aGuard( getExternalLock() );
116 if ( m_pImpl->getClientId( ) )
118 AccessibleEventNotifier::revokeClientNotifyDisposing( m_pImpl->getClientId( ), *this );
119 m_pImpl->setClientId( 0 );
124 void SAL_CALL OAccessibleContextHelper::addAccessibleEventListener( const Reference< XAccessibleEventListener >& _rxListener ) throw (RuntimeException, std::exception)
126 OMutexGuard aGuard( getExternalLock() );
127 // don't use the OContextEntryGuard - it will throw an exception if we're not alive
128 // anymore, while the most recent specification for XComponent states that we should
129 // silently ignore the call in such a situation
130 if ( !isAlive() )
132 if ( _rxListener.is() )
133 _rxListener->disposing( EventObject( *this ) );
134 return;
137 if ( _rxListener.is() )
139 if ( !m_pImpl->getClientId( ) )
140 m_pImpl->setClientId( AccessibleEventNotifier::registerClient( ) );
142 AccessibleEventNotifier::addEventListener( m_pImpl->getClientId( ), _rxListener );
147 void SAL_CALL OAccessibleContextHelper::removeAccessibleEventListener( const Reference< XAccessibleEventListener >& _rxListener ) throw (RuntimeException, std::exception)
149 OMutexGuard aGuard( getExternalLock() );
150 // don't use the OContextEntryGuard - it will throw an exception if we're not alive
151 // anymore, while the most recent specification for XComponent states that we should
152 // silently ignore the call in such a situation
153 if ( !isAlive() )
154 return;
156 if ( _rxListener.is() )
158 sal_Int32 nListenerCount = AccessibleEventNotifier::removeEventListener( m_pImpl->getClientId( ), _rxListener );
159 if ( !nListenerCount )
161 // no listeners anymore
162 // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
163 // and at least to us not firing any events anymore, in case somebody calls
164 // NotifyAccessibleEvent, again
165 AccessibleEventNotifier::revokeClient( m_pImpl->getClientId( ) );
166 m_pImpl->setClientId( 0 );
172 void OAccessibleContextHelper::NotifyAccessibleEvent( const sal_Int16 _nEventId,
173 const Any& _rOldValue, const Any& _rNewValue )
175 if ( !m_pImpl->getClientId( ) )
176 // if we don't have a client id for the notifier, then we don't have listeners, then
177 // we don't need to notify anything
178 return;
180 // build an event object
181 AccessibleEventObject aEvent;
182 aEvent.Source = *this;
183 aEvent.EventId = _nEventId;
184 aEvent.OldValue = _rOldValue;
185 aEvent.NewValue = _rNewValue;
187 // let the notifier handle this event
188 AccessibleEventNotifier::addEvent( m_pImpl->getClientId( ), aEvent );
192 bool OAccessibleContextHelper::isAlive() const
194 return !GetBroadcastHelper().bDisposed && !GetBroadcastHelper().bInDispose;
198 void OAccessibleContextHelper::ensureAlive() const
200 if( !isAlive() )
201 throw DisposedException();
205 void OAccessibleContextHelper::ensureDisposed( )
207 if ( !GetBroadcastHelper().bDisposed )
209 OSL_ENSURE( 0 == m_refCount, "OAccessibleContextHelper::ensureDisposed: this method _has_ to be called from without your dtor only!" );
210 acquire();
211 dispose();
216 void OAccessibleContextHelper::lateInit( const Reference< XAccessible >& _rxAccessible )
218 m_pImpl->setCreator( _rxAccessible );
222 Reference< XAccessible > OAccessibleContextHelper::getAccessibleCreator( ) const
224 return m_pImpl->getCreator();
228 sal_Int32 SAL_CALL OAccessibleContextHelper::getAccessibleIndexInParent( ) throw (RuntimeException, std::exception)
230 OExternalLockGuard aGuard( this );
232 // -1 for child not found/no parent (according to specification)
233 sal_Int32 nRet = -1;
238 Reference< XAccessibleContext > xParentContext( implGetParentContext() );
240 // iterate over parent's children and search for this object
241 if ( xParentContext.is() )
243 // our own XAccessible for comparing with the children of our parent
244 Reference< XAccessible > xCreator( m_pImpl->getCreator() );
246 OSL_ENSURE( xCreator.is(), "OAccessibleContextHelper::getAccessibleIndexInParent: invalid creator!" );
247 // two ideas why this could be NULL:
248 // * nobody called our late ctor (init), so we never had a creator at all -> bad
249 // * the creator is already dead. In this case, we should have been disposed, and
250 // never survived the above OContextEntryGuard.
251 // in all other situations the creator should be non-NULL
253 if ( xCreator.is() )
255 sal_Int32 nChildCount = xParentContext->getAccessibleChildCount();
256 for ( sal_Int32 nChild = 0; ( nChild < nChildCount ) && ( -1 == nRet ); ++nChild )
258 Reference< XAccessible > xChild( xParentContext->getAccessibleChild( nChild ) );
259 if ( xChild.get() == xCreator.get() )
260 nRet = nChild;
265 catch( const Exception& )
267 OSL_FAIL( "OAccessibleContextHelper::getAccessibleIndexInParent: caught an exception!" );
270 return nRet;
274 Locale SAL_CALL OAccessibleContextHelper::getLocale( ) throw (IllegalAccessibleComponentStateException, RuntimeException, std::exception)
276 // simply ask the parent
277 Reference< XAccessible > xParent = getAccessibleParent();
278 Reference< XAccessibleContext > xParentContext;
279 if ( xParent.is() )
280 xParentContext = xParent->getAccessibleContext();
282 if ( !xParentContext.is() )
283 throw IllegalAccessibleComponentStateException( OUString(), *this );
285 return xParentContext->getLocale();
289 Reference< XAccessibleContext > OAccessibleContextHelper::implGetParentContext()
291 Reference< XAccessible > xParent = getAccessibleParent();
292 Reference< XAccessibleContext > xParentContext;
293 if ( xParent.is() )
294 xParentContext = xParent->getAccessibleContext();
295 return xParentContext;
299 } // namespace comphelper
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */