1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fmdispatch.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include "fmdispatch.hxx"
34 #include "formcontrolling.hxx"
36 //........................................................................
39 //........................................................................
41 using namespace ::com::sun::star::uno
;
42 using namespace ::com::sun::star::lang
;
43 using namespace ::com::sun::star::frame
;
44 using namespace ::com::sun::star::beans
;
45 using namespace ::com::sun::star::util
;
46 using namespace ::com::sun::star::form::runtime
;
48 //====================================================================
49 //= OSingleFeatureDispatcher
50 //====================================================================
51 //--------------------------------------------------------------------
52 OSingleFeatureDispatcher::OSingleFeatureDispatcher( const URL
& _rFeatureURL
, sal_Int32 _nFeatureId
,
53 const FormControllerHelper
& _rController
, ::osl::Mutex
& _rMutex
)
55 ,m_aStatusListeners( _rMutex
)
56 ,m_rController( _rController
)
57 ,m_aFeatureURL( _rFeatureURL
)
58 ,m_nFeatureId( _nFeatureId
)
59 ,m_bLastKnownEnabled( sal_False
)
60 ,m_bDisposed( sal_False
)
64 //--------------------------------------------------------------------
65 void OSingleFeatureDispatcher::dispose()
68 ::osl::MutexGuard
aGuard( m_rMutex
);
73 EventObject
aDisposeEvent( *this );
74 m_aStatusListeners
.disposeAndClear( aDisposeEvent
);
77 ::osl::MutexGuard
aGuard( m_rMutex
);
78 m_bDisposed
= sal_True
;
82 //--------------------------------------------------------------------
83 void OSingleFeatureDispatcher::getUnoState( FeatureStateEvent
& /* [out] */ _rState
) const
86 _rState
.Source
= *const_cast< OSingleFeatureDispatcher
* >( this );
88 m_rController
.getState( m_nFeatureId
, aState
);
90 _rState
.FeatureURL
= m_aFeatureURL
;
91 _rState
.IsEnabled
= aState
.Enabled
;
92 _rState
.Requery
= sal_False
;
93 _rState
.State
= aState
.State
;
96 //--------------------------------------------------------------------
97 void OSingleFeatureDispatcher::updateAllListeners()
99 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
101 FeatureStateEvent aUnoState
;
102 getUnoState( aUnoState
);
104 if ( ( m_aLastKnownState
== aUnoState
.State
) && ( m_bLastKnownEnabled
== aUnoState
.IsEnabled
) )
107 m_aLastKnownState
= aUnoState
.State
;
108 m_bLastKnownEnabled
= aUnoState
.IsEnabled
;
110 notifyStatus( NULL
, aGuard
);
113 //--------------------------------------------------------------------
114 void OSingleFeatureDispatcher::notifyStatus( const Reference
< XStatusListener
>& _rxListener
, ::osl::ClearableMutexGuard
& _rFreeForNotification
)
116 FeatureStateEvent aUnoState
;
117 getUnoState( aUnoState
);
119 if ( _rxListener
.is() )
123 _rFreeForNotification
.clear();
124 _rxListener
->statusChanged( aUnoState
);
126 catch( const Exception
& )
128 OSL_ENSURE( sal_False
, "OSingleFeatureDispatcher::notifyStatus: caught an exception!" );
133 ::cppu::OInterfaceIteratorHelper
aIter( m_aStatusListeners
);
134 _rFreeForNotification
.clear();
136 while ( aIter
.hasMoreElements() )
140 static_cast< XStatusListener
* >( aIter
.next() )->statusChanged( aUnoState
);
142 catch( const DisposedException
& )
144 OSL_ENSURE( sal_False
, "OSingleFeatureDispatcher::notifyStatus: caught a DisposedException - removing the listener!" );
147 catch( const Exception
& )
149 OSL_ENSURE( sal_False
, "OSingleFeatureDispatcher::notifyStatus: caught a generic exception while notifying a single listener!" );
155 //--------------------------------------------------------------------
156 void SAL_CALL
OSingleFeatureDispatcher::dispatch( const URL
& _rURL
, const Sequence
< PropertyValue
>& _rArguments
) throw (RuntimeException
)
158 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
161 OSL_ENSURE( _rURL
.Complete
== m_aFeatureURL
.Complete
, "OSingleFeatureDispatcher::dispatch: not responsible for this URL!" );
164 if ( m_rController
.isEnabled( m_nFeatureId
) )
166 // release our mutex before executing the slot?
167 sal_Int32
nFeatureId( m_nFeatureId
);
170 if ( !_rArguments
.getLength() )
172 m_rController
.execute( nFeatureId
);
175 { // at the moment we only support one parameter
176 m_rController
.execute( nFeatureId
, _rArguments
[0].Name
, _rArguments
[0].Value
);
181 //--------------------------------------------------------------------
182 void SAL_CALL
OSingleFeatureDispatcher::addStatusListener( const Reference
< XStatusListener
>& _rxControl
, const URL
& _rURL
) throw (RuntimeException
)
185 OSL_ENSURE( _rURL
.Complete
== m_aFeatureURL
.Complete
, "OSingleFeatureDispatcher::addStatusListener: unexpected URL!" );
186 OSL_ENSURE( _rxControl
.is(), "OSingleFeatureDispatcher::addStatusListener: senseless call!" );
187 if ( !_rxControl
.is() )
190 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
193 EventObject
aDisposeEvent( *this );
195 _rxControl
->disposing( aDisposeEvent
);
199 m_aStatusListeners
.addInterface( _rxControl
);
201 // initially update the status
202 notifyStatus( _rxControl
, aGuard
);
205 //--------------------------------------------------------------------
206 void SAL_CALL
OSingleFeatureDispatcher::removeStatusListener( const Reference
< XStatusListener
>& _rxControl
, const URL
& _rURL
) throw (RuntimeException
)
209 OSL_ENSURE( _rURL
.Complete
== m_aFeatureURL
.Complete
, "OSingleFeatureDispatcher::removeStatusListener: unexpected URL!" );
210 OSL_ENSURE( _rxControl
.is(), "OSingleFeatureDispatcher::removeStatusListener: senseless call!" );
211 if ( !_rxControl
.is() )
214 ::osl::MutexGuard
aGuard( m_rMutex
);
217 m_aStatusListeners
.removeInterface( _rxControl
);
220 //--------------------------------------------------------------------
221 void OSingleFeatureDispatcher::checkAlive() const SAL_THROW((DisposedException
))
224 throw DisposedException( ::rtl::OUString(), *const_cast< OSingleFeatureDispatcher
* >( this ) );
227 //........................................................................
229 //........................................................................