Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / form / formfeaturedispatcher.cxx
blob8f108f394e3894372436af323f7ee7f65788dce5
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;
39 //= OSingleFeatureDispatcher
42 OSingleFeatureDispatcher::OSingleFeatureDispatcher( const URL& _rFeatureURL, const sal_Int16 _nFormFeature,
43 const Reference< XFormOperations >& _rxFormOperations, ::osl::Mutex& _rMutex )
44 :m_rMutex( _rMutex )
45 ,m_aStatusListeners( _rMutex )
46 ,m_xFormOperations( _rxFormOperations )
47 ,m_aFeatureURL( _rFeatureURL )
48 ,m_nFormFeature( _nFormFeature )
49 ,m_bLastKnownEnabled( false )
50 ,m_bDisposed( 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 ) )
76 return;
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() )
92 try
94 _rFreeForNotification.clear();
95 _rxListener->statusChanged( aUnoState );
97 catch( const Exception& )
99 OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught an exception!" );
102 else
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!" );
116 aIter.remove( );
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 );
130 checkAlive();
132 OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::dispatch: not responsible for this URL!" );
133 (void)_rURL;
135 if ( !m_xFormOperations->isEnabled( m_nFormFeature ) )
136 return;
138 // release our mutex before executing the command
139 sal_Int16 nFormFeature( m_nFormFeature );
140 Reference< XFormOperations > xFormOperations( m_xFormOperations );
141 aGuard.clear();
145 if ( !_rArguments.getLength() )
147 xFormOperations->execute( nFormFeature );
149 else
150 { // at the moment we only support one parameter
151 ::comphelper::NamedValueCollection aArgs( _rArguments );
152 xFormOperations->executeWithArguments( nFormFeature, aArgs.getNamedValues() );
155 catch( const RuntimeException& )
157 throw;
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)
168 (void)_rURL;
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() )
172 return;
174 ::osl::ClearableMutexGuard aGuard( m_rMutex );
175 if ( m_bDisposed )
177 EventObject aDisposeEvent( *this );
178 aGuard.clear();
179 _rxControl->disposing( aDisposeEvent );
180 return;
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)
192 (void)_rURL;
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() )
196 return;
198 ::osl::MutexGuard aGuard( m_rMutex );
199 checkAlive();
201 m_aStatusListeners.removeInterface( _rxControl );
205 void OSingleFeatureDispatcher::checkAlive() const SAL_THROW((DisposedException))
207 if ( m_bDisposed )
208 throw DisposedException( OUString(), *const_cast< OSingleFeatureDispatcher* >( this ) );
212 } // namespace svx
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */