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>
25 #include <comphelper/diagnose_ex.hxx>
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
;
39 OSingleFeatureDispatcher::OSingleFeatureDispatcher( URL _aFeatureURL
, const sal_Int16 _nFormFeature
,
40 const Reference
< XFormOperations
>& _rxFormOperations
, ::osl::Mutex
& _rMutex
)
42 ,m_aStatusListeners( _rMutex
)
43 ,m_xFormOperations( _rxFormOperations
)
44 ,m_aFeatureURL(std::move( _aFeatureURL
))
45 ,m_nFormFeature( _nFormFeature
)
46 ,m_bLastKnownEnabled( 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
= 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
) ) )
74 m_aLastKnownState
= aUnoState
.State
;
75 m_bLastKnownEnabled
= aUnoState
.IsEnabled
;
77 notifyStatus( nullptr, aGuard
);
81 void OSingleFeatureDispatcher::notifyStatus( const Reference
< XStatusListener
>& _rxListener
, ::osl::ClearableMutexGuard
& _rFreeForNotification
)
83 FeatureStateEvent aUnoState
;
84 getUnoState( aUnoState
);
86 if ( _rxListener
.is() )
90 _rFreeForNotification
.clear();
91 _rxListener
->statusChanged( aUnoState
);
93 catch( const Exception
& )
95 TOOLS_WARN_EXCEPTION( "svx", "OSingleFeatureDispatcher::notifyStatus" );
100 ::comphelper::OInterfaceIteratorHelper3
aIter( m_aStatusListeners
);
101 _rFreeForNotification
.clear();
103 while ( aIter
.hasMoreElements() )
107 aIter
.next()->statusChanged( aUnoState
);
109 catch( const DisposedException
& )
111 TOOLS_WARN_EXCEPTION("svx.form",
112 "caught a DisposedException - removing the listener!");
115 catch( const Exception
& )
117 TOOLS_WARN_EXCEPTION(
119 "caught a generic exception while notifying a single listener!");
126 void SAL_CALL
OSingleFeatureDispatcher::dispatch( const URL
& _rURL
, const Sequence
< PropertyValue
>& _rArguments
)
128 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
130 OSL_ENSURE( _rURL
.Complete
== m_aFeatureURL
.Complete
, "OSingleFeatureDispatcher::dispatch: not responsible for this URL!" );
132 if ( !m_xFormOperations
->isEnabled( m_nFormFeature
) )
135 // release our mutex before executing the command
136 sal_Int16
nFormFeature( m_nFormFeature
);
137 Reference
< XFormOperations
> xFormOperations( m_xFormOperations
);
142 if ( !_rArguments
.hasElements() )
144 xFormOperations
->execute( nFormFeature
);
147 { // at the moment we only support one parameter
148 ::comphelper::NamedValueCollection
aArgs( _rArguments
);
149 xFormOperations
->executeWithArguments( nFormFeature
, aArgs
.getNamedValues() );
152 catch( const RuntimeException
& )
156 catch( const Exception
& )
158 DBG_UNHANDLED_EXCEPTION("svx");
163 void SAL_CALL
OSingleFeatureDispatcher::addStatusListener( const Reference
< XStatusListener
>& _rxControl
, const URL
& _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() )
170 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
172 m_aStatusListeners
.addInterface( _rxControl
);
174 // initially update the status
175 notifyStatus( _rxControl
, aGuard
);
179 void SAL_CALL
OSingleFeatureDispatcher::removeStatusListener( const Reference
< XStatusListener
>& _rxControl
, const URL
& _rURL
)
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() )
186 ::osl::MutexGuard
aGuard( m_rMutex
);
188 m_aStatusListeners
.removeInterface( _rxControl
);
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */