Bump version to 6.4-15
[LibreOffice.git] / svx / source / form / formfeaturedispatcher.cxx
blob50faf597c463e228799f7c509d22ad64c5eaf942
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 )
50 void OSingleFeatureDispatcher::getUnoState( FeatureStateEvent& /* [out] */ _rState ) const
52 _rState.Source = *const_cast< OSingleFeatureDispatcher* >( this );
54 FeatureState aState( m_xFormOperations->getState( m_nFormFeature ) );
56 _rState.FeatureURL = m_aFeatureURL;
57 _rState.IsEnabled = aState.Enabled;
58 _rState.Requery = false;
59 _rState.State = aState.State;
63 void OSingleFeatureDispatcher::updateAllListeners()
65 ::osl::ClearableMutexGuard aGuard( m_rMutex );
67 FeatureStateEvent aUnoState;
68 getUnoState( aUnoState );
70 if ( ( m_aLastKnownState == aUnoState.State ) && ( m_bLastKnownEnabled == bool(aUnoState.IsEnabled) ) )
71 return;
73 m_aLastKnownState = aUnoState.State;
74 m_bLastKnownEnabled = aUnoState.IsEnabled;
76 notifyStatus( nullptr, aGuard );
80 void OSingleFeatureDispatcher::notifyStatus( const Reference< XStatusListener >& _rxListener, ::osl::ClearableMutexGuard& _rFreeForNotification )
82 FeatureStateEvent aUnoState;
83 getUnoState( aUnoState );
85 if ( _rxListener.is() )
87 try
89 _rFreeForNotification.clear();
90 _rxListener->statusChanged( aUnoState );
92 catch( const Exception& )
94 OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught an exception!" );
97 else
99 ::comphelper::OInterfaceIteratorHelper2 aIter( m_aStatusListeners );
100 _rFreeForNotification.clear();
102 while ( aIter.hasMoreElements() )
106 static_cast< XStatusListener* >( aIter.next() )->statusChanged( aUnoState );
108 catch( const DisposedException& )
110 OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught a DisposedException - removing the listener!" );
111 aIter.remove( );
113 catch( const Exception& )
115 OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught a generic exception while notifying a single listener!" );
122 void SAL_CALL OSingleFeatureDispatcher::dispatch( const URL& _rURL, const Sequence< PropertyValue >& _rArguments )
124 ::osl::ClearableMutexGuard aGuard( m_rMutex );
126 OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::dispatch: not responsible for this URL!" );
128 if ( !m_xFormOperations->isEnabled( m_nFormFeature ) )
129 return;
131 // release our mutex before executing the command
132 sal_Int16 nFormFeature( m_nFormFeature );
133 Reference< XFormOperations > xFormOperations( m_xFormOperations );
134 aGuard.clear();
138 if ( !_rArguments.hasElements() )
140 xFormOperations->execute( nFormFeature );
142 else
143 { // at the moment we only support one parameter
144 ::comphelper::NamedValueCollection aArgs( _rArguments );
145 xFormOperations->executeWithArguments( nFormFeature, aArgs.getNamedValues() );
148 catch( const RuntimeException& )
150 throw;
152 catch( const Exception& )
154 DBG_UNHANDLED_EXCEPTION("svx");
159 void SAL_CALL OSingleFeatureDispatcher::addStatusListener( const Reference< XStatusListener >& _rxControl, const URL& _rURL )
161 OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::addStatusListener: unexpected URL!" );
162 OSL_ENSURE( _rxControl.is(), "OSingleFeatureDispatcher::addStatusListener: senseless call!" );
163 if ( !_rxControl.is() )
164 return;
166 ::osl::ClearableMutexGuard aGuard( m_rMutex );
168 m_aStatusListeners.addInterface( _rxControl );
170 // initially update the status
171 notifyStatus( _rxControl, aGuard );
175 void SAL_CALL OSingleFeatureDispatcher::removeStatusListener( const Reference< XStatusListener >& _rxControl, const URL& _rURL )
177 OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::removeStatusListener: unexpected URL!" );
178 OSL_ENSURE( _rxControl.is(), "OSingleFeatureDispatcher::removeStatusListener: senseless call!" );
179 if ( !_rxControl.is() )
180 return;
182 ::osl::MutexGuard aGuard( m_rMutex );
184 m_aStatusListeners.removeInterface( _rxControl );
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */