LibreOffice Module svx (master) 1
formfeaturedispatcher.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
22
24#include <utility>
26
27
28namespace svx
29{
30
31
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::lang;
34 using namespace ::com::sun::star::frame;
35 using namespace ::com::sun::star::beans;
36 using namespace ::com::sun::star::util;
37 using namespace ::com::sun::star::form::runtime;
38
39 OSingleFeatureDispatcher::OSingleFeatureDispatcher( URL _aFeatureURL, const sal_Int16 _nFormFeature,
40 const Reference< XFormOperations >& _rxFormOperations, ::osl::Mutex& _rMutex )
41 :m_rMutex( _rMutex )
42 ,m_aStatusListeners( _rMutex )
43 ,m_xFormOperations( _rxFormOperations )
44 ,m_aFeatureURL(std::move( _aFeatureURL ))
45 ,m_nFormFeature( _nFormFeature )
46 ,m_bLastKnownEnabled( false )
47 {
48 }
49
50
51 void OSingleFeatureDispatcher::getUnoState( FeatureStateEvent& /* [out] */ _rState ) const
52 {
53 _rState.Source = *const_cast< OSingleFeatureDispatcher* >( this );
54
55 FeatureState aState( m_xFormOperations->getState( m_nFormFeature ) );
56
57 _rState.FeatureURL = m_aFeatureURL;
58 _rState.IsEnabled = aState.Enabled;
59 _rState.Requery = false;
60 _rState.State = aState.State;
61 }
62
63
65 {
66 ::osl::ClearableMutexGuard aGuard( m_rMutex );
67
68 FeatureStateEvent aUnoState;
69 getUnoState( aUnoState );
70
71 if ( ( m_aLastKnownState == aUnoState.State ) && ( m_bLastKnownEnabled == bool(aUnoState.IsEnabled) ) )
72 return;
73
74 m_aLastKnownState = aUnoState.State;
75 m_bLastKnownEnabled = aUnoState.IsEnabled;
76
77 notifyStatus( nullptr, aGuard );
78 }
79
80
81 void OSingleFeatureDispatcher::notifyStatus( const Reference< XStatusListener >& _rxListener, ::osl::ClearableMutexGuard& _rFreeForNotification )
82 {
83 FeatureStateEvent aUnoState;
84 getUnoState( aUnoState );
85
86 if ( _rxListener.is() )
87 {
88 try
89 {
90 _rFreeForNotification.clear();
91 _rxListener->statusChanged( aUnoState );
92 }
93 catch( const Exception& )
94 {
95 TOOLS_WARN_EXCEPTION( "svx", "OSingleFeatureDispatcher::notifyStatus" );
96 }
97 }
98 else
99 {
101 _rFreeForNotification.clear();
102
103 while ( aIter.hasMoreElements() )
104 {
105 try
106 {
107 aIter.next()->statusChanged( aUnoState );
108 }
109 catch( const DisposedException& )
110 {
111 TOOLS_WARN_EXCEPTION("svx.form",
112 "caught a DisposedException - removing the listener!");
113 aIter.remove( );
114 }
115 catch( const Exception& )
116 {
118 "svx.form",
119 "caught a generic exception while notifying a single listener!");
120 }
121 }
122 }
123 }
124
125
126 void SAL_CALL OSingleFeatureDispatcher::dispatch( const URL& _rURL, const Sequence< PropertyValue >& _rArguments )
127 {
128 ::osl::ClearableMutexGuard aGuard( m_rMutex );
129
130 OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::dispatch: not responsible for this URL!" );
131
132 if ( !m_xFormOperations->isEnabled( m_nFormFeature ) )
133 return;
134
135 // release our mutex before executing the command
136 sal_Int16 nFormFeature( m_nFormFeature );
137 Reference< XFormOperations > xFormOperations( m_xFormOperations );
138 aGuard.clear();
139
140 try
141 {
142 if ( !_rArguments.hasElements() )
143 {
144 xFormOperations->execute( nFormFeature );
145 }
146 else
147 { // at the moment we only support one parameter
148 ::comphelper::NamedValueCollection aArgs( _rArguments );
149 xFormOperations->executeWithArguments( nFormFeature, aArgs.getNamedValues() );
150 }
151 }
152 catch( const RuntimeException& )
153 {
154 throw;
155 }
156 catch( const Exception& )
157 {
159 }
160 }
161
162
163 void SAL_CALL OSingleFeatureDispatcher::addStatusListener( const Reference< XStatusListener >& _rxControl, const URL& _rURL )
164 {
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;
169
170 ::osl::ClearableMutexGuard aGuard( m_rMutex );
171
172 m_aStatusListeners.addInterface( _rxControl );
173
174 // initially update the status
175 notifyStatus( _rxControl, aGuard );
176 }
177
178
179 void SAL_CALL OSingleFeatureDispatcher::removeStatusListener( const Reference< XStatusListener >& _rxControl, const URL& _rURL )
180 {
181 OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::removeStatusListener: unexpected URL!" );
182 OSL_ENSURE( _rxControl.is(), "OSingleFeatureDispatcher::removeStatusListener: senseless call!" );
183 if ( !_rxControl.is() )
184 return;
185
186 ::osl::MutexGuard aGuard( m_rMutex );
187
189 }
190
191
192}
193
194
195/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< css::beans::NamedValue > getNamedValues() const
sal_Int32 addInterface(const css::uno::Reference< ListenerT > &rxIFace)
sal_Int32 removeInterface(const css::uno::Reference< ListenerT > &rxIFace)
css::uno::Reference< ListenerT > const & next()
void updateAllListeners()
notifies all our listeners of the current state
OSingleFeatureDispatcher(css::util::URL _aFeatureURL, const sal_Int16 _nFormFeature, const css::uno::Reference< css::form::runtime::XFormOperations > &_rxFormOperations, ::osl::Mutex &_rMutex)
constructs the dispatcher
css::uno::Reference< css::form::runtime::XFormOperations > m_xFormOperations
void notifyStatus(const css::uno::Reference< css::frame::XStatusListener > &_rxListener, ::osl::ClearableMutexGuard &_rFreeForNotification)
notifies our current state to one or all listeners
virtual void SAL_CALL dispatch(const css::util::URL &_rURL, const css::uno::Sequence< css::beans::PropertyValue > &_rArguments) override
void getUnoState(css::frame::FeatureStateEvent &_rState) const
retrieves the current status of our feature, in a format which can be used for UNO notifications
virtual void SAL_CALL addStatusListener(const css::uno::Reference< css::frame::XStatusListener > &_rxControl, const css::util::URL &_rURL) override
::comphelper::OInterfaceContainerHelper3< css::frame::XStatusListener > m_aStatusListeners
virtual void SAL_CALL removeStatusListener(const css::uno::Reference< css::frame::XStatusListener > &_rxControl, const css::util::URL &_rURL) override
::osl::Mutex & m_rMutex
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
const sal_Int16 nFormFeature
@ Exception