Line data Source code
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 :
21 : #include "formfeaturedispatcher.hxx"
22 :
23 : #include <comphelper/namedvaluecollection.hxx>
24 : #include <tools/diagnose_ex.h>
25 :
26 :
27 : namespace svx
28 : {
29 :
30 :
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;
37 :
38 0 : 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 )
46 0 : ,m_bDisposed( false )
47 : {
48 0 : }
49 :
50 :
51 0 : void OSingleFeatureDispatcher::getUnoState( FeatureStateEvent& /* [out] */ _rState ) const
52 : {
53 0 : _rState.Source = *const_cast< OSingleFeatureDispatcher* >( this );
54 :
55 0 : FeatureState aState( m_xFormOperations->getState( m_nFormFeature ) );
56 :
57 0 : _rState.FeatureURL = m_aFeatureURL;
58 0 : _rState.IsEnabled = aState.Enabled;
59 0 : _rState.Requery = sal_False;
60 0 : _rState.State = aState.State;
61 0 : }
62 :
63 :
64 0 : void OSingleFeatureDispatcher::updateAllListeners()
65 : {
66 0 : ::osl::ClearableMutexGuard aGuard( m_rMutex );
67 :
68 0 : FeatureStateEvent aUnoState;
69 0 : getUnoState( aUnoState );
70 :
71 0 : if ( ( m_aLastKnownState == aUnoState.State ) && ( (m_bLastKnownEnabled ? 1 : 0) == aUnoState.IsEnabled ) )
72 0 : return;
73 :
74 0 : m_aLastKnownState = aUnoState.State;
75 0 : m_bLastKnownEnabled = aUnoState.IsEnabled;
76 :
77 0 : notifyStatus( NULL, aGuard );
78 : }
79 :
80 :
81 0 : void OSingleFeatureDispatcher::notifyStatus( const Reference< XStatusListener >& _rxListener, ::osl::ClearableMutexGuard& _rFreeForNotification )
82 : {
83 0 : FeatureStateEvent aUnoState;
84 0 : getUnoState( aUnoState );
85 :
86 0 : if ( _rxListener.is() )
87 : {
88 : try
89 : {
90 0 : _rFreeForNotification.clear();
91 0 : _rxListener->statusChanged( aUnoState );
92 : }
93 0 : catch( const Exception& )
94 : {
95 : OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught an exception!" );
96 : }
97 : }
98 : else
99 : {
100 0 : ::cppu::OInterfaceIteratorHelper aIter( m_aStatusListeners );
101 0 : _rFreeForNotification.clear();
102 :
103 0 : while ( aIter.hasMoreElements() )
104 : {
105 : try
106 : {
107 0 : static_cast< XStatusListener* >( aIter.next() )->statusChanged( aUnoState );
108 : }
109 0 : catch( const DisposedException& )
110 : {
111 : OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught a DisposedException - removing the listener!" );
112 0 : aIter.remove( );
113 : }
114 0 : catch( const Exception& )
115 : {
116 : OSL_FAIL( "OSingleFeatureDispatcher::notifyStatus: caught a generic exception while notifying a single listener!" );
117 : }
118 0 : }
119 0 : }
120 0 : }
121 :
122 :
123 0 : void SAL_CALL OSingleFeatureDispatcher::dispatch( const URL& _rURL, const Sequence< PropertyValue >& _rArguments ) throw (RuntimeException, std::exception)
124 : {
125 0 : ::osl::ClearableMutexGuard aGuard( m_rMutex );
126 0 : checkAlive();
127 :
128 : OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::dispatch: not responsible for this URL!" );
129 : (void)_rURL;
130 :
131 0 : if ( !m_xFormOperations->isEnabled( m_nFormFeature ) )
132 0 : return;
133 :
134 : // release our mutex before executing the command
135 0 : sal_Int16 nFormFeature( m_nFormFeature );
136 0 : Reference< XFormOperations > xFormOperations( m_xFormOperations );
137 0 : aGuard.clear();
138 :
139 : try
140 : {
141 0 : if ( !_rArguments.getLength() )
142 : {
143 0 : xFormOperations->execute( nFormFeature );
144 : }
145 : else
146 : { // at the moment we only support one parameter
147 0 : ::comphelper::NamedValueCollection aArgs( _rArguments );
148 0 : xFormOperations->executeWithArguments( nFormFeature, aArgs.getNamedValues() );
149 : }
150 : }
151 0 : catch( const RuntimeException& )
152 : {
153 0 : throw;
154 : }
155 0 : catch( const Exception& )
156 : {
157 : DBG_UNHANDLED_EXCEPTION();
158 0 : }
159 : }
160 :
161 :
162 0 : void SAL_CALL OSingleFeatureDispatcher::addStatusListener( const Reference< XStatusListener >& _rxControl, const URL& _rURL ) throw (RuntimeException, std::exception)
163 : {
164 : (void)_rURL;
165 : OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::addStatusListener: unexpected URL!" );
166 : OSL_ENSURE( _rxControl.is(), "OSingleFeatureDispatcher::addStatusListener: senseless call!" );
167 0 : if ( !_rxControl.is() )
168 0 : return;
169 :
170 0 : ::osl::ClearableMutexGuard aGuard( m_rMutex );
171 0 : if ( m_bDisposed )
172 : {
173 0 : EventObject aDisposeEvent( *this );
174 0 : aGuard.clear();
175 0 : _rxControl->disposing( aDisposeEvent );
176 0 : return;
177 : }
178 :
179 0 : m_aStatusListeners.addInterface( _rxControl );
180 :
181 : // initially update the status
182 0 : notifyStatus( _rxControl, aGuard );
183 : }
184 :
185 :
186 0 : void SAL_CALL OSingleFeatureDispatcher::removeStatusListener( const Reference< XStatusListener >& _rxControl, const URL& _rURL ) throw (RuntimeException, std::exception)
187 : {
188 : (void)_rURL;
189 : OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::removeStatusListener: unexpected URL!" );
190 : OSL_ENSURE( _rxControl.is(), "OSingleFeatureDispatcher::removeStatusListener: senseless call!" );
191 0 : if ( !_rxControl.is() )
192 0 : return;
193 :
194 0 : ::osl::MutexGuard aGuard( m_rMutex );
195 0 : checkAlive();
196 :
197 0 : m_aStatusListeners.removeInterface( _rxControl );
198 : }
199 :
200 :
201 0 : void OSingleFeatureDispatcher::checkAlive() const
202 : {
203 0 : if ( m_bDisposed )
204 0 : throw DisposedException( OUString(), *const_cast< OSingleFeatureDispatcher* >( this ) );
205 0 : }
206 :
207 :
208 : } // namespace svx
209 :
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|