bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / form / formfeaturedispatcher.cxx
blob664f6a1239ad50a095c32cfe7caa02906e4a88ad
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 .
21 #include "formfeaturedispatcher.hxx"
23 #include <comphelper/namedvaluecollection.hxx>
24 #include <tools/diagnose_ex.h>
27 namespace svx
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;
38 OSingleFeatureDispatcher::OSingleFeatureDispatcher( const URL& _rFeatureURL, const sal_Int16 _nFormFeature,
39 const Reference< XFormOperations >& _rxFormOperations, ::osl::Mutex& _rMutex )
40 :m_rMutex( _rMutex )
41 ,m_aStatusListeners( _rMutex )
42 ,m_xFormOperations( _rxFormOperations )
43 ,m_aFeatureURL( _rFeatureURL )
44 ,m_nFormFeature( _nFormFeature )
45 ,m_bLastKnownEnabled( false )
46 ,m_bDisposed( false )
51 void OSingleFeatureDispatcher::getUnoState( FeatureStateEvent& /* [out] */ _rState ) const
53 _rState.Source = *const_cast< OSingleFeatureDispatcher* >( this );
55 FeatureState aState( m_xFormOperations->getState( m_nFormFeature ) );
57 _rState.FeatureURL = m_aFeatureURL;
58 _rState.IsEnabled = aState.Enabled;
59 _rState.Requery = sal_False;
60 _rState.State = aState.State;
64 void OSingleFeatureDispatcher::updateAllListeners()
66 ::osl::ClearableMutexGuard aGuard( m_rMutex );
68 FeatureStateEvent aUnoState;
69 getUnoState( aUnoState );
71 if ( ( m_aLastKnownState == aUnoState.State ) && ( m_bLastKnownEnabled == bool(aUnoState.IsEnabled) ) )
72 return;
74 m_aLastKnownState = aUnoState.State;
75 m_bLastKnownEnabled = aUnoState.IsEnabled;
77 notifyStatus( NULL, aGuard );
81 void OSingleFeatureDispatcher::notifyStatus( const Reference< XStatusListener >& _rxListener, ::osl::ClearableMutexGuard& _rFreeForNotification )
83 FeatureStateEvent aUnoState;
84 getUnoState( aUnoState );
86 if ( _rxListener.is() )
88 try
90 _rFreeForNotification.clear();
91 _rxListener->statusChanged( aUnoState );
93 catch( const Exception& )
95 OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught an exception!" );
98 else
100 ::cppu::OInterfaceIteratorHelper aIter( m_aStatusListeners );
101 _rFreeForNotification.clear();
103 while ( aIter.hasMoreElements() )
107 static_cast< XStatusListener* >( aIter.next() )->statusChanged( aUnoState );
109 catch( const DisposedException& )
111 OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught a DisposedException - removing the listener!" );
112 aIter.remove( );
114 catch( const Exception& )
116 OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught a generic exception while notifying a single listener!" );
123 void SAL_CALL OSingleFeatureDispatcher::dispatch( const URL& _rURL, const Sequence< PropertyValue >& _rArguments ) throw (RuntimeException, std::exception)
125 ::osl::ClearableMutexGuard aGuard( m_rMutex );
126 checkAlive();
128 OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::dispatch: not responsible for this URL!" );
129 (void)_rURL;
131 if ( !m_xFormOperations->isEnabled( m_nFormFeature ) )
132 return;
134 // release our mutex before executing the command
135 sal_Int16 nFormFeature( m_nFormFeature );
136 Reference< XFormOperations > xFormOperations( m_xFormOperations );
137 aGuard.clear();
141 if ( !_rArguments.getLength() )
143 xFormOperations->execute( nFormFeature );
145 else
146 { // at the moment we only support one parameter
147 ::comphelper::NamedValueCollection aArgs( _rArguments );
148 xFormOperations->executeWithArguments( nFormFeature, aArgs.getNamedValues() );
151 catch( const RuntimeException& )
153 throw;
155 catch( const Exception& )
157 DBG_UNHANDLED_EXCEPTION();
162 void SAL_CALL OSingleFeatureDispatcher::addStatusListener( const Reference< XStatusListener >& _rxControl, const URL& _rURL ) throw (RuntimeException, std::exception)
164 (void)_rURL;
165 OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::addStatusListener: unexpected URL!" );
166 OSL_ENSURE( _rxControl.is(), "OSingleFeatureDispatcher::addStatusListener: senseless call!" );
167 if ( !_rxControl.is() )
168 return;
170 ::osl::ClearableMutexGuard aGuard( m_rMutex );
171 if ( m_bDisposed )
173 EventObject aDisposeEvent( *this );
174 aGuard.clear();
175 _rxControl->disposing( aDisposeEvent );
176 return;
179 m_aStatusListeners.addInterface( _rxControl );
181 // initially update the status
182 notifyStatus( _rxControl, aGuard );
186 void SAL_CALL OSingleFeatureDispatcher::removeStatusListener( const Reference< XStatusListener >& _rxControl, const URL& _rURL ) throw (RuntimeException, std::exception)
188 (void)_rURL;
189 OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::removeStatusListener: unexpected URL!" );
190 OSL_ENSURE( _rxControl.is(), "OSingleFeatureDispatcher::removeStatusListener: senseless call!" );
191 if ( !_rxControl.is() )
192 return;
194 ::osl::MutexGuard aGuard( m_rMutex );
195 checkAlive();
197 m_aStatusListeners.removeInterface( _rxControl );
201 void OSingleFeatureDispatcher::checkAlive() const
203 if ( m_bDisposed )
204 throw DisposedException( OUString(), *const_cast< OSingleFeatureDispatcher* >( this ) );
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */