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>
24 #include <tools/diagnose_ex.h>
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
)
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
) ) )
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() )
89 _rFreeForNotification
.clear();
90 _rxListener
->statusChanged( aUnoState
);
92 catch( const Exception
& )
94 TOOLS_WARN_EXCEPTION( "svx", "OSingleFeatureDispatcher::notifyStatus" );
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 TOOLS_WARN_EXCEPTION("svx.form",
111 "caught a DisposedException - removing the listener!");
114 catch( const Exception
& )
116 TOOLS_WARN_EXCEPTION(
118 "caught a generic exception while notifying a single listener!");
125 void SAL_CALL
OSingleFeatureDispatcher::dispatch( const URL
& _rURL
, const Sequence
< PropertyValue
>& _rArguments
)
127 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
129 OSL_ENSURE( _rURL
.Complete
== m_aFeatureURL
.Complete
, "OSingleFeatureDispatcher::dispatch: not responsible for this URL!" );
131 if ( !m_xFormOperations
->isEnabled( m_nFormFeature
) )
134 // release our mutex before executing the command
135 sal_Int16
nFormFeature( m_nFormFeature
);
136 Reference
< XFormOperations
> xFormOperations( m_xFormOperations
);
141 if ( !_rArguments
.hasElements() )
143 xFormOperations
->execute( nFormFeature
);
146 { // at the moment we only support one parameter
147 ::comphelper::NamedValueCollection
aArgs( _rArguments
);
148 xFormOperations
->executeWithArguments( nFormFeature
, aArgs
.getNamedValues() );
151 catch( const RuntimeException
& )
155 catch( const Exception
& )
157 DBG_UNHANDLED_EXCEPTION("svx");
162 void SAL_CALL
OSingleFeatureDispatcher::addStatusListener( const Reference
< XStatusListener
>& _rxControl
, const URL
& _rURL
)
164 OSL_ENSURE( _rURL
.Complete
== m_aFeatureURL
.Complete
, "OSingleFeatureDispatcher::addStatusListener: unexpected URL!" );
165 OSL_ENSURE( _rxControl
.is(), "OSingleFeatureDispatcher::addStatusListener: senseless call!" );
166 if ( !_rxControl
.is() )
169 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
171 m_aStatusListeners
.addInterface( _rxControl
);
173 // initially update the status
174 notifyStatus( _rxControl
, aGuard
);
178 void SAL_CALL
OSingleFeatureDispatcher::removeStatusListener( const Reference
< XStatusListener
>& _rxControl
, const URL
& _rURL
)
180 OSL_ENSURE( _rURL
.Complete
== m_aFeatureURL
.Complete
, "OSingleFeatureDispatcher::removeStatusListener: unexpected URL!" );
181 OSL_ENSURE( _rxControl
.is(), "OSingleFeatureDispatcher::removeStatusListener: senseless call!" );
182 if ( !_rxControl
.is() )
185 ::osl::MutexGuard
aGuard( m_rMutex
);
187 m_aStatusListeners
.removeInterface( _rxControl
);
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */