1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include "formfeaturedispatcher.hxx"
23 #include <comphelper/namedvaluecollection.hxx>
24 #include <tools/diagnose_ex.h>
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::lang
;
33 using namespace ::com::sun::star::frame
;
34 using namespace ::com::sun::star::beans
;
35 using namespace ::com::sun::star::util
;
36 using namespace ::com::sun::star::form::runtime
;
39 //= OSingleFeatureDispatcher
42 OSingleFeatureDispatcher::OSingleFeatureDispatcher( const URL
& _rFeatureURL
, const sal_Int16 _nFormFeature
,
43 const Reference
< XFormOperations
>& _rxFormOperations
, ::osl::Mutex
& _rMutex
)
45 ,m_aStatusListeners( _rMutex
)
46 ,m_xFormOperations( _rxFormOperations
)
47 ,m_aFeatureURL( _rFeatureURL
)
48 ,m_nFormFeature( _nFormFeature
)
49 ,m_bLastKnownEnabled( false )
55 void OSingleFeatureDispatcher::getUnoState( FeatureStateEvent
& /* [out] */ _rState
) const
57 _rState
.Source
= *const_cast< OSingleFeatureDispatcher
* >( this );
59 FeatureState
aState( m_xFormOperations
->getState( m_nFormFeature
) );
61 _rState
.FeatureURL
= m_aFeatureURL
;
62 _rState
.IsEnabled
= aState
.Enabled
;
63 _rState
.Requery
= sal_False
;
64 _rState
.State
= aState
.State
;
68 void OSingleFeatureDispatcher::updateAllListeners()
70 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
72 FeatureStateEvent aUnoState
;
73 getUnoState( aUnoState
);
75 if ( ( m_aLastKnownState
== aUnoState
.State
) && ( (m_bLastKnownEnabled
? 1 : 0) == aUnoState
.IsEnabled
) )
78 m_aLastKnownState
= aUnoState
.State
;
79 m_bLastKnownEnabled
= aUnoState
.IsEnabled
;
81 notifyStatus( NULL
, aGuard
);
85 void OSingleFeatureDispatcher::notifyStatus( const Reference
< XStatusListener
>& _rxListener
, ::osl::ClearableMutexGuard
& _rFreeForNotification
)
87 FeatureStateEvent aUnoState
;
88 getUnoState( aUnoState
);
90 if ( _rxListener
.is() )
94 _rFreeForNotification
.clear();
95 _rxListener
->statusChanged( aUnoState
);
97 catch( const Exception
& )
99 OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught an exception!" );
104 ::cppu::OInterfaceIteratorHelper
aIter( m_aStatusListeners
);
105 _rFreeForNotification
.clear();
107 while ( aIter
.hasMoreElements() )
111 static_cast< XStatusListener
* >( aIter
.next() )->statusChanged( aUnoState
);
113 catch( const DisposedException
& )
115 OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught a DisposedException - removing the listener!" );
118 catch( const Exception
& )
120 OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught a generic exception while notifying a single listener!" );
127 void SAL_CALL
OSingleFeatureDispatcher::dispatch( const URL
& _rURL
, const Sequence
< PropertyValue
>& _rArguments
) throw (RuntimeException
, std::exception
)
129 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
132 OSL_ENSURE( _rURL
.Complete
== m_aFeatureURL
.Complete
, "OSingleFeatureDispatcher::dispatch: not responsible for this URL!" );
135 if ( !m_xFormOperations
->isEnabled( m_nFormFeature
) )
138 // release our mutex before executing the command
139 sal_Int16
nFormFeature( m_nFormFeature
);
140 Reference
< XFormOperations
> xFormOperations( m_xFormOperations
);
145 if ( !_rArguments
.getLength() )
147 xFormOperations
->execute( nFormFeature
);
150 { // at the moment we only support one parameter
151 ::comphelper::NamedValueCollection
aArgs( _rArguments
);
152 xFormOperations
->executeWithArguments( nFormFeature
, aArgs
.getNamedValues() );
155 catch( const RuntimeException
& )
159 catch( const Exception
& )
161 DBG_UNHANDLED_EXCEPTION();
166 void SAL_CALL
OSingleFeatureDispatcher::addStatusListener( const Reference
< XStatusListener
>& _rxControl
, const URL
& _rURL
) throw (RuntimeException
, std::exception
)
169 OSL_ENSURE( _rURL
.Complete
== m_aFeatureURL
.Complete
, "OSingleFeatureDispatcher::addStatusListener: unexpected URL!" );
170 OSL_ENSURE( _rxControl
.is(), "OSingleFeatureDispatcher::addStatusListener: senseless call!" );
171 if ( !_rxControl
.is() )
174 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
177 EventObject
aDisposeEvent( *this );
179 _rxControl
->disposing( aDisposeEvent
);
183 m_aStatusListeners
.addInterface( _rxControl
);
185 // initially update the status
186 notifyStatus( _rxControl
, aGuard
);
190 void SAL_CALL
OSingleFeatureDispatcher::removeStatusListener( const Reference
< XStatusListener
>& _rxControl
, const URL
& _rURL
) throw (RuntimeException
, std::exception
)
193 OSL_ENSURE( _rURL
.Complete
== m_aFeatureURL
.Complete
, "OSingleFeatureDispatcher::removeStatusListener: unexpected URL!" );
194 OSL_ENSURE( _rxControl
.is(), "OSingleFeatureDispatcher::removeStatusListener: senseless call!" );
195 if ( !_rxControl
.is() )
198 ::osl::MutexGuard
aGuard( m_rMutex
);
201 m_aStatusListeners
.removeInterface( _rxControl
);
205 void OSingleFeatureDispatcher::checkAlive() const SAL_THROW((DisposedException
))
208 throw DisposedException( OUString(), *const_cast< OSingleFeatureDispatcher
* >( this ) );
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */