fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / comphelper / source / misc / accessiblecontexthelper.cxx
blobbb9047d5ca820ed524d506cd9c3f2cde7ae0f9a7
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>
27 //.........................................................................
28 namespace comphelper
30 //.........................................................................
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 //=====================================================================
39 //= OContextHelper_Impl
40 //=====================================================================
41 /** implementation class for OAccessibleContextHelper. No own thread safety!
43 class OContextHelper_Impl
45 private:
46 IMutex* m_pExternalLock; // the optional additional external lock
48 WeakReference< XAccessible > m_aCreator; // the XAccessible which created our XAccessibleContext
50 AccessibleEventNotifier::TClientId m_nClientId;
52 public:
53 inline Reference< XAccessible > getCreator( ) const { return m_aCreator; }
54 inline void setCreator( const Reference< XAccessible >& _rAcc );
56 inline IMutex* getExternalLock( ) { return m_pExternalLock; }
57 inline void setExternalLock( IMutex* _pLock ) { m_pExternalLock = _pLock; }
59 inline AccessibleEventNotifier::TClientId
60 getClientId() const { return m_nClientId; }
61 inline void setClientId( const AccessibleEventNotifier::TClientId _nId )
62 { m_nClientId = _nId; }
64 public:
65 OContextHelper_Impl()
66 :m_pExternalLock( NULL )
67 ,m_nClientId( 0 )
72 //---------------------------------------------------------------------
73 inline void OContextHelper_Impl::setCreator( const Reference< XAccessible >& _rAcc )
75 m_aCreator = _rAcc;
78 //=====================================================================
79 //= OAccessibleContextHelper
80 //=====================================================================
81 //---------------------------------------------------------------------
82 OAccessibleContextHelper::OAccessibleContextHelper( )
83 :OAccessibleContextHelper_Base( GetMutex() )
84 ,m_pImpl( NULL )
86 m_pImpl = new OContextHelper_Impl();
89 //---------------------------------------------------------------------
90 OAccessibleContextHelper::OAccessibleContextHelper( IMutex* _pExternalLock )
91 :OAccessibleContextHelper_Base( GetMutex() )
92 ,m_pImpl( NULL )
94 m_pImpl = new OContextHelper_Impl();
95 m_pImpl->setExternalLock( _pExternalLock );
98 //---------------------------------------------------------------------
99 void OAccessibleContextHelper::forgetExternalLock()
101 m_pImpl->setExternalLock( NULL );
104 //---------------------------------------------------------------------
105 OAccessibleContextHelper::~OAccessibleContextHelper( )
107 forgetExternalLock();
108 // this ensures that the lock, which may be already destroyed as part of the derivee,
109 // is not used anymore
111 ensureDisposed();
113 delete m_pImpl;
114 m_pImpl = NULL;
117 //---------------------------------------------------------------------
118 IMutex* OAccessibleContextHelper::getExternalLock( )
120 return m_pImpl->getExternalLock();
123 //---------------------------------------------------------------------
124 void SAL_CALL OAccessibleContextHelper::disposing()
126 // rhbz#1001768: de facto this class is locked by SolarMutex;
127 // do not lock m_Mutex because it may cause deadlock
128 OMutexGuard aGuard( getExternalLock() );
130 if ( m_pImpl->getClientId( ) )
132 AccessibleEventNotifier::revokeClientNotifyDisposing( m_pImpl->getClientId( ), *this );
133 m_pImpl->setClientId( 0 );
137 //---------------------------------------------------------------------
138 void SAL_CALL OAccessibleContextHelper::addAccessibleEventListener( const Reference< XAccessibleEventListener >& _rxListener ) throw (RuntimeException)
140 OMutexGuard aGuard( getExternalLock() );
141 // don't use the OContextEntryGuard - it will throw an exception if we're not alive
142 // anymore, while the most recent specification for XComponent states that we should
143 // silently ignore the call in such a situation
144 if ( !isAlive() )
146 if ( _rxListener.is() )
147 _rxListener->disposing( EventObject( *this ) );
148 return;
151 if ( _rxListener.is() )
153 if ( !m_pImpl->getClientId( ) )
154 m_pImpl->setClientId( AccessibleEventNotifier::registerClient( ) );
156 AccessibleEventNotifier::addEventListener( m_pImpl->getClientId( ), _rxListener );
160 //---------------------------------------------------------------------
161 void SAL_CALL OAccessibleContextHelper::removeAccessibleEventListener( const Reference< XAccessibleEventListener >& _rxListener ) throw (RuntimeException)
163 OMutexGuard aGuard( getExternalLock() );
164 // don't use the OContextEntryGuard - it will throw an exception if we're not alive
165 // anymore, while the most recent specification for XComponent states that we should
166 // silently ignore the call in such a situation
167 if ( !isAlive() )
168 return;
170 if ( _rxListener.is() )
172 sal_Int32 nListenerCount = AccessibleEventNotifier::removeEventListener( m_pImpl->getClientId( ), _rxListener );
173 if ( !nListenerCount )
175 // no listeners anymore
176 // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
177 // and at least to us not firing any events anymore, in case somebody calls
178 // NotifyAccessibleEvent, again
179 AccessibleEventNotifier::revokeClient( m_pImpl->getClientId( ) );
180 m_pImpl->setClientId( 0 );
185 //---------------------------------------------------------------------
186 void SAL_CALL OAccessibleContextHelper::NotifyAccessibleEvent( const sal_Int16 _nEventId,
187 const Any& _rOldValue, const Any& _rNewValue )
189 if ( !m_pImpl->getClientId( ) )
190 // if we don't have a client id for the notifier, then we don't have listeners, then
191 // we don't need to notify anything
192 return;
194 // build an event object
195 AccessibleEventObject aEvent;
196 aEvent.Source = *this;
197 aEvent.EventId = _nEventId;
198 aEvent.OldValue = _rOldValue;
199 aEvent.NewValue = _rNewValue;
201 // let the notifier handle this event
202 AccessibleEventNotifier::addEvent( m_pImpl->getClientId( ), aEvent );
205 //---------------------------------------------------------------------
206 sal_Bool OAccessibleContextHelper::isAlive() const
208 return !GetBroadcastHelper().bDisposed && !GetBroadcastHelper().bInDispose;
211 //---------------------------------------------------------------------
212 void OAccessibleContextHelper::ensureAlive() const SAL_THROW( ( DisposedException ) )
214 if( !isAlive() )
215 throw DisposedException();
218 //---------------------------------------------------------------------
219 void OAccessibleContextHelper::ensureDisposed( )
221 if ( !GetBroadcastHelper().bDisposed )
223 OSL_ENSURE( 0 == m_refCount, "OAccessibleContextHelper::ensureDisposed: this method _has_ to be called from without your dtor only!" );
224 acquire();
225 dispose();
229 //---------------------------------------------------------------------
230 void OAccessibleContextHelper::lateInit( const Reference< XAccessible >& _rxAccessible )
232 m_pImpl->setCreator( _rxAccessible );
235 //---------------------------------------------------------------------
236 Reference< XAccessible > OAccessibleContextHelper::getAccessibleCreator( ) const
238 return m_pImpl->getCreator();
241 //---------------------------------------------------------------------
242 sal_Int32 SAL_CALL OAccessibleContextHelper::getAccessibleIndexInParent( ) throw (RuntimeException)
244 OExternalLockGuard aGuard( this );
246 // -1 for child not found/no parent (according to specification)
247 sal_Int32 nRet = -1;
252 Reference< XAccessibleContext > xParentContext( implGetParentContext() );
254 // iterate over parent's children and search for this object
255 if ( xParentContext.is() )
257 // our own XAccessible for comparing with the children of our parent
258 Reference< XAccessible > xCreator( m_pImpl->getCreator() );
260 OSL_ENSURE( xCreator.is(), "OAccessibleContextHelper::getAccessibleIndexInParent: invalid creator!" );
261 // two ideas why this could be NULL:
262 // * nobody called our late ctor (init), so we never had a creator at all -> bad
263 // * the creator is already dead. In this case, we should have been disposed, and
264 // never survived the above OContextEntryGuard.
265 // in all other situations the creator should be non-NULL
267 if ( xCreator.is() )
269 sal_Int32 nChildCount = xParentContext->getAccessibleChildCount();
270 for ( sal_Int32 nChild = 0; ( nChild < nChildCount ) && ( -1 == nRet ); ++nChild )
272 Reference< XAccessible > xChild( xParentContext->getAccessibleChild( nChild ) );
273 if ( xChild.get() == xCreator.get() )
274 nRet = nChild;
279 catch( const Exception& )
281 OSL_FAIL( "OAccessibleContextHelper::getAccessibleIndexInParent: caught an exception!" );
284 return nRet;
287 //---------------------------------------------------------------------
288 Locale SAL_CALL OAccessibleContextHelper::getLocale( ) throw (IllegalAccessibleComponentStateException, RuntimeException)
290 // simply ask the parent
291 Reference< XAccessible > xParent = getAccessibleParent();
292 Reference< XAccessibleContext > xParentContext;
293 if ( xParent.is() )
294 xParentContext = xParent->getAccessibleContext();
296 if ( !xParentContext.is() )
297 throw IllegalAccessibleComponentStateException( OUString(), *this );
299 return xParentContext->getLocale();
302 //---------------------------------------------------------------------
303 Reference< XAccessibleContext > OAccessibleContextHelper::implGetParentContext() SAL_THROW( ( RuntimeException ) )
305 Reference< XAccessible > xParent = getAccessibleParent();
306 Reference< XAccessibleContext > xParentContext;
307 if ( xParent.is() )
308 xParentContext = xParent->getAccessibleContext();
309 return xParentContext;
312 //.........................................................................
313 } // namespace comphelper
314 //.........................................................................
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */