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 : #include <svtools/framestatuslistener.hxx>
21 : #include <com/sun/star/frame/XDispatchProvider.hpp>
22 : #include <com/sun/star/lang/DisposedException.hpp>
23 : #include <com/sun/star/util/URLTransformer.hpp>
24 : #include <osl/mutex.hxx>
25 : #include <vcl/svapp.hxx>
26 : #include <comphelper/processfactory.hxx>
27 :
28 : using namespace ::cppu;
29 : using namespace ::com::sun::star::awt;
30 : using namespace ::com::sun::star::uno;
31 : using namespace ::com::sun::star::util;
32 : using namespace ::com::sun::star::beans;
33 : using namespace ::com::sun::star::lang;
34 : using namespace ::com::sun::star::frame;
35 :
36 : namespace svt
37 : {
38 :
39 0 : FrameStatusListener::FrameStatusListener(
40 : const Reference< XComponentContext >& rxContext,
41 : const Reference< XFrame >& xFrame ) :
42 : OWeakObject()
43 : , m_bInitialized( true )
44 : , m_bDisposed( false )
45 : , m_xFrame( xFrame )
46 0 : , m_xContext( rxContext )
47 : {
48 0 : }
49 :
50 0 : FrameStatusListener::~FrameStatusListener()
51 : {
52 0 : }
53 :
54 : // XInterface
55 0 : Any SAL_CALL FrameStatusListener::queryInterface( const Type& rType )
56 : throw ( RuntimeException, std::exception )
57 : {
58 : Any a = ::cppu::queryInterface(
59 : rType ,
60 : static_cast< XComponent* >( this ),
61 : static_cast< XFrameActionListener* >( this ),
62 : static_cast< XStatusListener* >( this ),
63 : static_cast< XEventListener* >( static_cast< XStatusListener* >( this )),
64 0 : static_cast< XEventListener* >( static_cast< XFrameActionListener* >( this )));
65 :
66 0 : if ( a.hasValue() )
67 0 : return a;
68 :
69 0 : return OWeakObject::queryInterface( rType );
70 : }
71 :
72 0 : void SAL_CALL FrameStatusListener::acquire() throw ()
73 : {
74 0 : OWeakObject::acquire();
75 0 : }
76 :
77 0 : void SAL_CALL FrameStatusListener::release() throw ()
78 : {
79 0 : OWeakObject::release();
80 0 : }
81 :
82 : // XComponent
83 0 : void SAL_CALL FrameStatusListener::dispose()
84 : throw (::com::sun::star::uno::RuntimeException, std::exception)
85 : {
86 0 : Reference< XComponent > xThis( static_cast< OWeakObject* >(this), UNO_QUERY );
87 :
88 0 : SolarMutexGuard aSolarMutexGuard;
89 0 : if ( m_bDisposed )
90 0 : throw DisposedException();
91 :
92 0 : Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
93 0 : URLToDispatchMap::iterator pIter = m_aListenerMap.begin();
94 0 : while ( pIter != m_aListenerMap.end() )
95 : {
96 : try
97 : {
98 0 : Reference< XDispatch > xDispatch( pIter->second );
99 0 : Reference< XURLTransformer > xURLTransformer( com::sun::star::util::URLTransformer::create( m_xContext ) );
100 0 : com::sun::star::util::URL aTargetURL;
101 0 : aTargetURL.Complete = pIter->first;
102 0 : xURLTransformer->parseStrict( aTargetURL );
103 :
104 0 : if ( xDispatch.is() && xStatusListener.is() )
105 0 : xDispatch->removeStatusListener( xStatusListener, aTargetURL );
106 : }
107 0 : catch (const Exception&)
108 : {
109 : }
110 :
111 0 : ++pIter;
112 : }
113 :
114 0 : m_bDisposed = true;
115 0 : }
116 :
117 0 : void SAL_CALL FrameStatusListener::addEventListener( const Reference< XEventListener >& )
118 : throw ( RuntimeException, std::exception )
119 : {
120 : // helper class for status updates - no need to support listener
121 0 : }
122 :
123 0 : void SAL_CALL FrameStatusListener::removeEventListener( const Reference< XEventListener >& )
124 : throw ( RuntimeException, std::exception )
125 : {
126 : // helper class for status updates - no need to support listener
127 0 : }
128 :
129 : // XEventListener
130 0 : void SAL_CALL FrameStatusListener::disposing( const EventObject& Source )
131 : throw ( RuntimeException, std::exception )
132 : {
133 0 : Reference< XInterface > xSource( Source.Source );
134 :
135 0 : SolarMutexGuard aSolarMutexGuard;
136 :
137 0 : URLToDispatchMap::iterator pIter = m_aListenerMap.begin();
138 0 : while ( pIter != m_aListenerMap.end() )
139 : {
140 : // Compare references and release dispatch references if they are equal.
141 0 : Reference< XInterface > xIfac( pIter->second, UNO_QUERY );
142 0 : if ( xSource == xIfac )
143 0 : pIter->second.clear();
144 0 : }
145 :
146 0 : Reference< XInterface > xIfac( m_xFrame, UNO_QUERY );
147 0 : if ( xIfac == xSource )
148 0 : m_xFrame.clear();
149 0 : }
150 :
151 0 : void FrameStatusListener::frameAction( const FrameActionEvent& Action )
152 : throw ( RuntimeException, std::exception )
153 : {
154 0 : if ( Action.Action == FrameAction_CONTEXT_CHANGED )
155 0 : bindListener();
156 0 : }
157 :
158 0 : void FrameStatusListener::addStatusListener( const OUString& aCommandURL )
159 : {
160 0 : Reference< XDispatch > xDispatch;
161 0 : Reference< XStatusListener > xStatusListener;
162 0 : com::sun::star::util::URL aTargetURL;
163 :
164 : {
165 0 : SolarMutexGuard aSolarMutexGuard;
166 0 : URLToDispatchMap::iterator pIter = m_aListenerMap.find( aCommandURL );
167 :
168 : // Already in the list of status listener. Do nothing.
169 0 : if ( pIter != m_aListenerMap.end() )
170 0 : return;
171 :
172 : // Check if we are already initialized. Implementation starts adding itself as status listener when
173 : // intialize is called.
174 0 : if ( !m_bInitialized )
175 : {
176 : // Put into the boost::unordered_map of status listener. Will be activated when initialized is called
177 0 : m_aListenerMap.insert( URLToDispatchMap::value_type( aCommandURL, Reference< XDispatch >() ));
178 0 : return;
179 : }
180 : else
181 : {
182 : // Add status listener directly as intialize has already been called.
183 0 : Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
184 0 : if ( m_xContext.is() && xDispatchProvider.is() )
185 : {
186 0 : Reference< XURLTransformer > xURLTransformer( com::sun::star::util::URLTransformer::create( m_xContext ) );
187 0 : aTargetURL.Complete = aCommandURL;
188 0 : xURLTransformer->parseStrict( aTargetURL );
189 0 : xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
190 :
191 0 : xStatusListener = Reference< XStatusListener >( static_cast< OWeakObject* >( this ), UNO_QUERY );
192 0 : URLToDispatchMap::iterator aIter = m_aListenerMap.find( aCommandURL );
193 0 : if ( aIter != m_aListenerMap.end() )
194 : {
195 0 : Reference< XDispatch > xOldDispatch( aIter->second );
196 0 : aIter->second = xDispatch;
197 :
198 : try
199 : {
200 0 : if ( xOldDispatch.is() )
201 0 : xOldDispatch->removeStatusListener( xStatusListener, aTargetURL );
202 : }
203 0 : catch (const Exception&)
204 : {
205 0 : }
206 : }
207 : else
208 0 : m_aListenerMap.insert( URLToDispatchMap::value_type( aCommandURL, xDispatch ));
209 0 : }
210 0 : }
211 : }
212 :
213 : // Call without locked mutex as we are called back from dispatch implementation
214 : try
215 : {
216 0 : if ( xDispatch.is() )
217 0 : xDispatch->addStatusListener( xStatusListener, aTargetURL );
218 : }
219 0 : catch (const Exception&)
220 : {
221 0 : }
222 : }
223 :
224 :
225 0 : void FrameStatusListener::bindListener()
226 : {
227 0 : std::vector< Listener > aDispatchVector;
228 0 : Reference< XStatusListener > xStatusListener;
229 :
230 : {
231 0 : SolarMutexGuard aSolarMutexGuard;
232 :
233 0 : if ( !m_bInitialized )
234 0 : return;
235 :
236 : // Collect all registered command URL's and store them temporary
237 0 : Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
238 0 : if ( m_xContext.is() && xDispatchProvider.is() )
239 : {
240 0 : xStatusListener = Reference< XStatusListener >( static_cast< OWeakObject* >( this ), UNO_QUERY );
241 0 : URLToDispatchMap::iterator pIter = m_aListenerMap.begin();
242 0 : while ( pIter != m_aListenerMap.end() )
243 : {
244 0 : Reference< XURLTransformer > xURLTransformer( com::sun::star::util::URLTransformer::create( m_xContext ) );
245 0 : com::sun::star::util::URL aTargetURL;
246 0 : aTargetURL.Complete = pIter->first;
247 0 : xURLTransformer->parseStrict( aTargetURL );
248 :
249 0 : Reference< XDispatch > xDispatch( pIter->second );
250 0 : if ( xDispatch.is() )
251 : {
252 : // We already have a dispatch object => we have to requery.
253 : // Release old dispatch object and remove it as listener
254 : try
255 : {
256 0 : xDispatch->removeStatusListener( xStatusListener, aTargetURL );
257 : }
258 0 : catch (const Exception&)
259 : {
260 : }
261 : }
262 :
263 : // Query for dispatch object. Old dispatch will be released with this, too.
264 : try
265 : {
266 0 : xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
267 : }
268 0 : catch (const Exception&)
269 : {
270 : }
271 0 : pIter->second = xDispatch;
272 :
273 0 : Listener aListener( aTargetURL, xDispatch );
274 0 : aDispatchVector.push_back( aListener );
275 0 : ++pIter;
276 0 : }
277 0 : }
278 : }
279 :
280 : // Call without locked mutex as we are called back from dispatch implementation
281 0 : if ( xStatusListener.is() )
282 : {
283 : try
284 : {
285 0 : for ( sal_uInt32 i = 0; i < aDispatchVector.size(); i++ )
286 : {
287 0 : Listener& rListener = aDispatchVector[i];
288 0 : if ( rListener.xDispatch.is() )
289 0 : rListener.xDispatch->addStatusListener( xStatusListener, rListener.aURL );
290 : }
291 : }
292 0 : catch (const Exception&)
293 : {
294 : }
295 0 : }
296 : }
297 :
298 0 : void FrameStatusListener::unbindListener()
299 : {
300 0 : SolarMutexGuard aSolarMutexGuard;
301 :
302 0 : if ( !m_bInitialized )
303 0 : return;
304 :
305 : // Collect all registered command URL's and store them temporary
306 0 : Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
307 0 : if ( m_xContext.is() && xDispatchProvider.is() )
308 : {
309 0 : Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
310 0 : URLToDispatchMap::iterator pIter = m_aListenerMap.begin();
311 0 : while ( pIter != m_aListenerMap.end() )
312 : {
313 0 : Reference< XURLTransformer > xURLTransformer( com::sun::star::util::URLTransformer::create( m_xContext ) );
314 0 : com::sun::star::util::URL aTargetURL;
315 0 : aTargetURL.Complete = pIter->first;
316 0 : xURLTransformer->parseStrict( aTargetURL );
317 :
318 0 : Reference< XDispatch > xDispatch( pIter->second );
319 0 : if ( xDispatch.is() )
320 : {
321 : // We already have a dispatch object => we have to requery.
322 : // Release old dispatch object and remove it as listener
323 : try
324 : {
325 0 : xDispatch->removeStatusListener( xStatusListener, aTargetURL );
326 : }
327 0 : catch (const Exception&)
328 : {
329 : }
330 : }
331 0 : pIter->second.clear();
332 0 : ++pIter;
333 0 : }
334 0 : }
335 : }
336 :
337 : } // svt
338 :
339 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|