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 <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 <cppuhelper/queryinterface.hxx>
25 #include <vcl/svapp.hxx>
27 using namespace ::cppu
;
28 using namespace css::awt
;
29 using namespace css::uno
;
30 using namespace css::util
;
31 using namespace css::lang
;
32 using namespace css::frame
;
37 FrameStatusListener::FrameStatusListener(
38 const Reference
< XComponentContext
>& rxContext
,
39 const Reference
< XFrame
>& xFrame
) :
41 , m_bDisposed( false )
43 , m_xContext( rxContext
)
47 FrameStatusListener::~FrameStatusListener()
52 Any SAL_CALL
FrameStatusListener::queryInterface( const Type
& rType
)
54 Any a
= ::cppu::queryInterface(
56 static_cast< XComponent
* >( this ),
57 static_cast< XFrameActionListener
* >( this ),
58 static_cast< XStatusListener
* >( this ),
59 static_cast< XEventListener
* >( static_cast< XStatusListener
* >( this )),
60 static_cast< XEventListener
* >( static_cast< XFrameActionListener
* >( this )));
65 return OWeakObject::queryInterface( rType
);
68 void SAL_CALL
FrameStatusListener::acquire() noexcept
70 OWeakObject::acquire();
73 void SAL_CALL
FrameStatusListener::release() noexcept
75 OWeakObject::release();
79 void SAL_CALL
FrameStatusListener::dispose()
81 Reference
< XComponent
> xThis
= this;
83 SolarMutexGuard aSolarMutexGuard
;
87 for (auto const& listener
: m_aListenerMap
)
91 Reference
< XDispatch
> xDispatch( listener
.second
);
92 Reference
< XURLTransformer
> xURLTransformer( css::util::URLTransformer::create( m_xContext
) );
93 css::util::URL aTargetURL
;
94 aTargetURL
.Complete
= listener
.first
;
95 xURLTransformer
->parseStrict( aTargetURL
);
98 xDispatch
->removeStatusListener( this, aTargetURL
);
100 catch (const Exception
&)
108 void SAL_CALL
FrameStatusListener::addEventListener( const Reference
< XEventListener
>& )
110 // helper class for status updates - no need to support listener
113 void SAL_CALL
FrameStatusListener::removeEventListener( const Reference
< XEventListener
>& )
115 // helper class for status updates - no need to support listener
119 void SAL_CALL
FrameStatusListener::disposing( const EventObject
& Source
)
121 Reference
< XInterface
> xSource( Source
.Source
);
123 SolarMutexGuard aSolarMutexGuard
;
125 for (auto & listener
: m_aListenerMap
)
127 // Compare references and release dispatch references if they are equal.
128 Reference
< XInterface
> xIfac( listener
.second
, UNO_QUERY
);
129 if ( xSource
== xIfac
)
130 listener
.second
.clear();
133 Reference
< XInterface
> xIfac( m_xFrame
, UNO_QUERY
);
134 if ( xIfac
== xSource
)
138 void FrameStatusListener::frameAction( const FrameActionEvent
& Action
)
140 if ( Action
.Action
== FrameAction_CONTEXT_CHANGED
)
144 void FrameStatusListener::addStatusListener( const OUString
& aCommandURL
)
146 Reference
< XDispatch
> xDispatch
;
147 Reference
< XStatusListener
> xStatusListener
;
148 css::util::URL aTargetURL
;
151 SolarMutexGuard aSolarMutexGuard
;
152 URLToDispatchMap::iterator pIter
= m_aListenerMap
.find( aCommandURL
);
154 // Already in the list of status listener. Do nothing.
155 if ( pIter
!= m_aListenerMap
.end() )
158 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
159 if ( m_xContext
.is() && xDispatchProvider
.is() )
161 Reference
< XURLTransformer
> xURLTransformer( css::util::URLTransformer::create( m_xContext
) );
162 aTargetURL
.Complete
= aCommandURL
;
163 xURLTransformer
->parseStrict( aTargetURL
);
164 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
166 xStatusListener
= this;
167 URLToDispatchMap::iterator aIter
= m_aListenerMap
.find( aCommandURL
);
168 if ( aIter
!= m_aListenerMap
.end() )
170 Reference
< XDispatch
> xOldDispatch( aIter
->second
);
171 aIter
->second
= xDispatch
;
175 if ( xOldDispatch
.is() )
176 xOldDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
178 catch (const Exception
&)
183 m_aListenerMap
.emplace( aCommandURL
, xDispatch
);
187 // Call without locked mutex as we are called back from dispatch implementation
190 if ( xDispatch
.is() )
191 xDispatch
->addStatusListener( xStatusListener
, aTargetURL
);
193 catch (const Exception
&)
199 void FrameStatusListener::bindListener()
201 std::vector
< Listener
> aDispatchVector
;
202 Reference
< XStatusListener
> xStatusListener
;
205 SolarMutexGuard aSolarMutexGuard
;
207 // Collect all registered command URL's and store them temporary
208 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
209 if ( m_xContext
.is() && xDispatchProvider
.is() )
211 xStatusListener
= this;
212 for (auto & listener
: m_aListenerMap
)
214 Reference
< XURLTransformer
> xURLTransformer( css::util::URLTransformer::create( m_xContext
) );
215 css::util::URL aTargetURL
;
216 aTargetURL
.Complete
= listener
.first
;
217 xURLTransformer
->parseStrict( aTargetURL
);
219 Reference
< XDispatch
> xDispatch( listener
.second
);
220 if ( xDispatch
.is() )
222 // We already have a dispatch object => we have to requery.
223 // Release old dispatch object and remove it as listener
226 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
228 catch (const Exception
&)
233 // Query for dispatch object. Old dispatch will be released with this, too.
236 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
238 catch (const Exception
&)
241 listener
.second
= xDispatch
;
243 aDispatchVector
.emplace_back(std::move(aTargetURL
), xDispatch
);
248 // Call without locked mutex as we are called back from dispatch implementation
249 if ( !xStatusListener
.is() )
254 for (Listener
& rListener
: aDispatchVector
)
256 if ( rListener
.xDispatch
.is() )
257 rListener
.xDispatch
->addStatusListener( xStatusListener
, rListener
.aURL
);
260 catch (const Exception
&)
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */