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 .
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>
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
;
39 FrameStatusListener::FrameStatusListener(
40 const Reference
< XComponentContext
>& rxContext
,
41 const Reference
< XFrame
>& xFrame
) :
43 , m_bInitialized( sal_True
)
44 , m_bDisposed( sal_False
)
46 , m_xContext( rxContext
)
50 FrameStatusListener::~FrameStatusListener()
55 Any SAL_CALL
FrameStatusListener::queryInterface( const Type
& rType
)
56 throw ( RuntimeException
)
58 Any a
= ::cppu::queryInterface(
60 static_cast< XComponent
* >( this ),
61 static_cast< XFrameActionListener
* >( this ),
62 static_cast< XStatusListener
* >( this ),
63 static_cast< XEventListener
* >( static_cast< XStatusListener
* >( this )),
64 static_cast< XEventListener
* >( static_cast< XFrameActionListener
* >( this )));
69 return OWeakObject::queryInterface( rType
);
72 void SAL_CALL
FrameStatusListener::acquire() throw ()
74 OWeakObject::acquire();
77 void SAL_CALL
FrameStatusListener::release() throw ()
79 OWeakObject::release();
83 void SAL_CALL
FrameStatusListener::dispose()
84 throw (::com::sun::star::uno::RuntimeException
)
86 Reference
< XComponent
> xThis( static_cast< OWeakObject
* >(this), UNO_QUERY
);
88 SolarMutexGuard aSolarMutexGuard
;
90 throw DisposedException();
92 Reference
< XStatusListener
> xStatusListener( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
93 URLToDispatchMap::iterator pIter
= m_aListenerMap
.begin();
94 while ( pIter
!= m_aListenerMap
.end() )
98 Reference
< XDispatch
> xDispatch( pIter
->second
);
99 Reference
< XURLTransformer
> xURLTransformer( com::sun::star::util::URLTransformer::create( m_xContext
) );
100 com::sun::star::util::URL aTargetURL
;
101 aTargetURL
.Complete
= pIter
->first
;
102 xURLTransformer
->parseStrict( aTargetURL
);
104 if ( xDispatch
.is() && xStatusListener
.is() )
105 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
107 catch (const Exception
&)
114 m_bDisposed
= sal_True
;
117 void SAL_CALL
FrameStatusListener::addEventListener( const Reference
< XEventListener
>& )
118 throw ( RuntimeException
)
120 // helper class for status updates - no need to support listener
123 void SAL_CALL
FrameStatusListener::removeEventListener( const Reference
< XEventListener
>& )
124 throw ( RuntimeException
)
126 // helper class for status updates - no need to support listener
130 void SAL_CALL
FrameStatusListener::disposing( const EventObject
& Source
)
131 throw ( RuntimeException
)
133 Reference
< XInterface
> xSource( Source
.Source
);
135 SolarMutexGuard aSolarMutexGuard
;
137 URLToDispatchMap::iterator pIter
= m_aListenerMap
.begin();
138 while ( pIter
!= m_aListenerMap
.end() )
140 // Compare references and release dispatch references if they are equal.
141 Reference
< XInterface
> xIfac( pIter
->second
, UNO_QUERY
);
142 if ( xSource
== xIfac
)
143 pIter
->second
.clear();
146 Reference
< XInterface
> xIfac( m_xFrame
, UNO_QUERY
);
147 if ( xIfac
== xSource
)
151 void FrameStatusListener::frameAction( const FrameActionEvent
& Action
)
152 throw ( RuntimeException
)
154 if ( Action
.Action
== FrameAction_CONTEXT_CHANGED
)
158 void FrameStatusListener::addStatusListener( const OUString
& aCommandURL
)
160 Reference
< XDispatch
> xDispatch
;
161 Reference
< XStatusListener
> xStatusListener
;
162 com::sun::star::util::URL aTargetURL
;
165 SolarMutexGuard aSolarMutexGuard
;
166 URLToDispatchMap::iterator pIter
= m_aListenerMap
.find( aCommandURL
);
168 // Already in the list of status listener. Do nothing.
169 if ( pIter
!= m_aListenerMap
.end() )
172 // Check if we are already initialized. Implementation starts adding itself as status listener when
173 // intialize is called.
174 if ( !m_bInitialized
)
176 // Put into the boost::unordered_map of status listener. Will be activated when initialized is called
177 m_aListenerMap
.insert( URLToDispatchMap::value_type( aCommandURL
, Reference
< XDispatch
>() ));
182 // Add status listener directly as intialize has already been called.
183 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
184 if ( m_xContext
.is() && xDispatchProvider
.is() )
186 Reference
< XURLTransformer
> xURLTransformer( com::sun::star::util::URLTransformer::create( m_xContext
) );
187 aTargetURL
.Complete
= aCommandURL
;
188 xURLTransformer
->parseStrict( aTargetURL
);
189 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
191 xStatusListener
= Reference
< XStatusListener
>( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
192 URLToDispatchMap::iterator aIter
= m_aListenerMap
.find( aCommandURL
);
193 if ( aIter
!= m_aListenerMap
.end() )
195 Reference
< XDispatch
> xOldDispatch( aIter
->second
);
196 aIter
->second
= xDispatch
;
200 if ( xOldDispatch
.is() )
201 xOldDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
203 catch (const Exception
&)
208 m_aListenerMap
.insert( URLToDispatchMap::value_type( aCommandURL
, xDispatch
));
213 // Call without locked mutex as we are called back from dispatch implementation
216 if ( xDispatch
.is() )
217 xDispatch
->addStatusListener( xStatusListener
, aTargetURL
);
219 catch (const Exception
&)
225 void FrameStatusListener::bindListener()
227 std::vector
< Listener
> aDispatchVector
;
228 Reference
< XStatusListener
> xStatusListener
;
231 SolarMutexGuard aSolarMutexGuard
;
233 if ( !m_bInitialized
)
236 // Collect all registered command URL's and store them temporary
237 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
238 if ( m_xContext
.is() && xDispatchProvider
.is() )
240 xStatusListener
= Reference
< XStatusListener
>( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
241 URLToDispatchMap::iterator pIter
= m_aListenerMap
.begin();
242 while ( pIter
!= m_aListenerMap
.end() )
244 Reference
< XURLTransformer
> xURLTransformer( com::sun::star::util::URLTransformer::create( m_xContext
) );
245 com::sun::star::util::URL aTargetURL
;
246 aTargetURL
.Complete
= pIter
->first
;
247 xURLTransformer
->parseStrict( aTargetURL
);
249 Reference
< XDispatch
> xDispatch( pIter
->second
);
250 if ( xDispatch
.is() )
252 // We already have a dispatch object => we have to requery.
253 // Release old dispatch object and remove it as listener
256 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
258 catch (const Exception
&)
263 // Query for dispatch object. Old dispatch will be released with this, too.
266 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
268 catch (const Exception
&)
271 pIter
->second
= xDispatch
;
273 Listener
aListener( aTargetURL
, xDispatch
);
274 aDispatchVector
.push_back( aListener
);
280 // Call without locked mutex as we are called back from dispatch implementation
281 if ( xStatusListener
.is() )
285 for ( sal_uInt32 i
= 0; i
< aDispatchVector
.size(); i
++ )
287 Listener
& rListener
= aDispatchVector
[i
];
288 if ( rListener
.xDispatch
.is() )
289 rListener
.xDispatch
->addStatusListener( xStatusListener
, rListener
.aURL
);
292 catch (const Exception
&)
298 void FrameStatusListener::unbindListener()
300 SolarMutexGuard aSolarMutexGuard
;
302 if ( !m_bInitialized
)
305 // Collect all registered command URL's and store them temporary
306 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
307 if ( m_xContext
.is() && xDispatchProvider
.is() )
309 Reference
< XStatusListener
> xStatusListener( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
310 URLToDispatchMap::iterator pIter
= m_aListenerMap
.begin();
311 while ( pIter
!= m_aListenerMap
.end() )
313 Reference
< XURLTransformer
> xURLTransformer( com::sun::star::util::URLTransformer::create( m_xContext
) );
314 com::sun::star::util::URL aTargetURL
;
315 aTargetURL
.Complete
= pIter
->first
;
316 xURLTransformer
->parseStrict( aTargetURL
);
318 Reference
< XDispatch
> xDispatch( pIter
->second
);
319 if ( xDispatch
.is() )
321 // We already have a dispatch object => we have to requery.
322 // Release old dispatch object and remove it as listener
325 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
327 catch (const Exception
&)
331 pIter
->second
.clear();
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */