/src/libreoffice/svx/source/form/formfeaturedispatcher.cxx
Line | Count | Source |
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 <utility> |
25 | | #include <comphelper/diagnose_ex.hxx> |
26 | | |
27 | | |
28 | | namespace 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 | 0 | :m_rMutex( _rMutex ) |
42 | 0 | ,m_aStatusListeners( _rMutex ) |
43 | 0 | ,m_xFormOperations( _rxFormOperations ) |
44 | 0 | ,m_aFeatureURL(std::move( _aFeatureURL )) |
45 | 0 | ,m_nFormFeature( _nFormFeature ) |
46 | 0 | ,m_bLastKnownEnabled( false ) |
47 | 0 | { |
48 | 0 | } |
49 | | |
50 | | |
51 | | void OSingleFeatureDispatcher::getUnoState( FeatureStateEvent& /* [out] */ _rState ) const |
52 | 0 | { |
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 = false; |
60 | 0 | _rState.State = aState.State; |
61 | 0 | } |
62 | | |
63 | | |
64 | | void OSingleFeatureDispatcher::updateAllListeners() |
65 | 0 | { |
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 == bool(aUnoState.IsEnabled) ) ) |
72 | 0 | return; |
73 | | |
74 | 0 | m_aLastKnownState = aUnoState.State; |
75 | 0 | m_bLastKnownEnabled = aUnoState.IsEnabled; |
76 | |
|
77 | 0 | notifyStatus( nullptr, aGuard ); |
78 | 0 | } |
79 | | |
80 | | |
81 | | void OSingleFeatureDispatcher::notifyStatus( const Reference< XStatusListener >& _rxListener, ::osl::ClearableMutexGuard& _rFreeForNotification ) |
82 | 0 | { |
83 | 0 | FeatureStateEvent aUnoState; |
84 | 0 | getUnoState( aUnoState ); |
85 | |
|
86 | 0 | if ( _rxListener.is() ) |
87 | 0 | { |
88 | 0 | try |
89 | 0 | { |
90 | 0 | _rFreeForNotification.clear(); |
91 | 0 | _rxListener->statusChanged( aUnoState ); |
92 | 0 | } |
93 | 0 | catch( const Exception& ) |
94 | 0 | { |
95 | 0 | TOOLS_WARN_EXCEPTION( "svx", "OSingleFeatureDispatcher::notifyStatus" ); |
96 | 0 | } |
97 | 0 | } |
98 | 0 | else |
99 | 0 | { |
100 | 0 | ::comphelper::OInterfaceIteratorHelper3 aIter( m_aStatusListeners ); |
101 | 0 | _rFreeForNotification.clear(); |
102 | |
|
103 | 0 | while ( aIter.hasMoreElements() ) |
104 | 0 | { |
105 | 0 | try |
106 | 0 | { |
107 | 0 | aIter.next()->statusChanged( aUnoState ); |
108 | 0 | } |
109 | 0 | catch( const DisposedException& ) |
110 | 0 | { |
111 | 0 | TOOLS_WARN_EXCEPTION("svx.form", |
112 | 0 | "caught a DisposedException - removing the listener!"); |
113 | 0 | aIter.remove( ); |
114 | 0 | } |
115 | 0 | catch( const Exception& ) |
116 | 0 | { |
117 | 0 | TOOLS_WARN_EXCEPTION( |
118 | 0 | "svx.form", |
119 | 0 | "caught a generic exception while notifying a single listener!"); |
120 | 0 | } |
121 | 0 | } |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | | |
126 | | void SAL_CALL OSingleFeatureDispatcher::dispatch( const URL& _rURL, const Sequence< PropertyValue >& _rArguments ) |
127 | 0 | { |
128 | 0 | ::osl::ClearableMutexGuard aGuard( m_rMutex ); |
129 | |
|
130 | 0 | OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::dispatch: not responsible for this URL!" ); |
131 | |
|
132 | 0 | if ( !m_xFormOperations->isEnabled( m_nFormFeature ) ) |
133 | 0 | return; |
134 | | |
135 | | // release our mutex before executing the command |
136 | 0 | sal_Int16 nFormFeature( m_nFormFeature ); |
137 | 0 | Reference< XFormOperations > xFormOperations( m_xFormOperations ); |
138 | 0 | aGuard.clear(); |
139 | |
|
140 | 0 | try |
141 | 0 | { |
142 | 0 | if ( !_rArguments.hasElements() ) |
143 | 0 | { |
144 | 0 | xFormOperations->execute( nFormFeature ); |
145 | 0 | } |
146 | 0 | else |
147 | 0 | { // at the moment we only support one parameter |
148 | 0 | ::comphelper::NamedValueCollection aArgs( _rArguments ); |
149 | 0 | xFormOperations->executeWithArguments( nFormFeature, aArgs.getNamedValues() ); |
150 | 0 | } |
151 | 0 | } |
152 | 0 | catch( const RuntimeException& ) |
153 | 0 | { |
154 | 0 | throw; |
155 | 0 | } |
156 | 0 | catch( const Exception& ) |
157 | 0 | { |
158 | 0 | DBG_UNHANDLED_EXCEPTION("svx"); |
159 | 0 | } |
160 | 0 | } |
161 | | |
162 | | |
163 | | void SAL_CALL OSingleFeatureDispatcher::addStatusListener( const Reference< XStatusListener >& _rxControl, const URL& _rURL ) |
164 | 0 | { |
165 | 0 | OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::addStatusListener: unexpected URL!" ); |
166 | 0 | 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 | |
|
172 | 0 | m_aStatusListeners.addInterface( _rxControl ); |
173 | | |
174 | | // initially update the status |
175 | 0 | notifyStatus( _rxControl, aGuard ); |
176 | 0 | } |
177 | | |
178 | | |
179 | | void SAL_CALL OSingleFeatureDispatcher::removeStatusListener( const Reference< XStatusListener >& _rxControl, const URL& _rURL ) |
180 | 0 | { |
181 | 0 | OSL_ENSURE( _rURL.Complete == m_aFeatureURL.Complete, "OSingleFeatureDispatcher::removeStatusListener: unexpected URL!" ); |
182 | 0 | OSL_ENSURE( _rxControl.is(), "OSingleFeatureDispatcher::removeStatusListener: senseless call!" ); |
183 | 0 | if ( !_rxControl.is() ) |
184 | 0 | return; |
185 | | |
186 | 0 | ::osl::MutexGuard aGuard( m_rMutex ); |
187 | |
|
188 | 0 | m_aStatusListeners.removeInterface( _rxControl ); |
189 | 0 | } |
190 | | |
191 | | |
192 | | } |
193 | | |
194 | | |
195 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |