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 <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::beans
;
32 using namespace css::lang
;
33 using namespace css::frame
;
38 FrameStatusListener::FrameStatusListener(
39 const Reference
< XComponentContext
>& rxContext
,
40 const Reference
< XFrame
>& xFrame
) :
42 , m_bDisposed( false )
44 , m_xContext( rxContext
)
48 FrameStatusListener::~FrameStatusListener()
53 Any SAL_CALL
FrameStatusListener::queryInterface( const Type
& rType
)
55 Any a
= ::cppu::queryInterface(
57 static_cast< XComponent
* >( this ),
58 static_cast< XFrameActionListener
* >( this ),
59 static_cast< XStatusListener
* >( this ),
60 static_cast< XEventListener
* >( static_cast< XStatusListener
* >( this )),
61 static_cast< XEventListener
* >( static_cast< XFrameActionListener
* >( this )));
66 return OWeakObject::queryInterface( rType
);
69 void SAL_CALL
FrameStatusListener::acquire() throw ()
71 OWeakObject::acquire();
74 void SAL_CALL
FrameStatusListener::release() throw ()
76 OWeakObject::release();
80 void SAL_CALL
FrameStatusListener::dispose()
82 Reference
< XComponent
> xThis
= this;
84 SolarMutexGuard aSolarMutexGuard
;
86 throw DisposedException();
88 for (auto const& listener
: m_aListenerMap
)
92 Reference
< XDispatch
> xDispatch( listener
.second
);
93 Reference
< XURLTransformer
> xURLTransformer( css::util::URLTransformer::create( m_xContext
) );
94 css::util::URL aTargetURL
;
95 aTargetURL
.Complete
= listener
.first
;
96 xURLTransformer
->parseStrict( aTargetURL
);
99 xDispatch
->removeStatusListener( this, aTargetURL
);
101 catch (const Exception
&)
109 void SAL_CALL
FrameStatusListener::addEventListener( const Reference
< XEventListener
>& )
111 // helper class for status updates - no need to support listener
114 void SAL_CALL
FrameStatusListener::removeEventListener( const Reference
< XEventListener
>& )
116 // helper class for status updates - no need to support listener
120 void SAL_CALL
FrameStatusListener::disposing( const EventObject
& Source
)
122 Reference
< XInterface
> xSource( Source
.Source
);
124 SolarMutexGuard aSolarMutexGuard
;
126 for (auto & listener
: m_aListenerMap
)
128 // Compare references and release dispatch references if they are equal.
129 Reference
< XInterface
> xIfac( listener
.second
, UNO_QUERY
);
130 if ( xSource
== xIfac
)
131 listener
.second
.clear();
134 Reference
< XInterface
> xIfac( m_xFrame
, UNO_QUERY
);
135 if ( xIfac
== xSource
)
139 void FrameStatusListener::frameAction( const FrameActionEvent
& Action
)
141 if ( Action
.Action
== FrameAction_CONTEXT_CHANGED
)
145 void FrameStatusListener::addStatusListener( const OUString
& aCommandURL
)
147 Reference
< XDispatch
> xDispatch
;
148 Reference
< XStatusListener
> xStatusListener
;
149 css::util::URL aTargetURL
;
152 SolarMutexGuard aSolarMutexGuard
;
153 URLToDispatchMap::iterator pIter
= m_aListenerMap
.find( aCommandURL
);
155 // Already in the list of status listener. Do nothing.
156 if ( pIter
!= m_aListenerMap
.end() )
159 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
160 if ( m_xContext
.is() && xDispatchProvider
.is() )
162 Reference
< XURLTransformer
> xURLTransformer( css::util::URLTransformer::create( m_xContext
) );
163 aTargetURL
.Complete
= aCommandURL
;
164 xURLTransformer
->parseStrict( aTargetURL
);
165 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
167 xStatusListener
= this;
168 URLToDispatchMap::iterator aIter
= m_aListenerMap
.find( aCommandURL
);
169 if ( aIter
!= m_aListenerMap
.end() )
171 Reference
< XDispatch
> xOldDispatch( aIter
->second
);
172 aIter
->second
= xDispatch
;
176 if ( xOldDispatch
.is() )
177 xOldDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
179 catch (const Exception
&)
184 m_aListenerMap
.emplace( aCommandURL
, xDispatch
);
188 // Call without locked mutex as we are called back from dispatch implementation
191 if ( xDispatch
.is() )
192 xDispatch
->addStatusListener( xStatusListener
, aTargetURL
);
194 catch (const Exception
&)
200 void FrameStatusListener::bindListener()
202 std::vector
< Listener
> aDispatchVector
;
203 Reference
< XStatusListener
> xStatusListener
;
206 SolarMutexGuard aSolarMutexGuard
;
208 // Collect all registered command URL's and store them temporary
209 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
210 if ( m_xContext
.is() && xDispatchProvider
.is() )
212 xStatusListener
= this;
213 for (auto & listener
: m_aListenerMap
)
215 Reference
< XURLTransformer
> xURLTransformer( css::util::URLTransformer::create( m_xContext
) );
216 css::util::URL aTargetURL
;
217 aTargetURL
.Complete
= listener
.first
;
218 xURLTransformer
->parseStrict( aTargetURL
);
220 Reference
< XDispatch
> xDispatch( listener
.second
);
221 if ( xDispatch
.is() )
223 // We already have a dispatch object => we have to requery.
224 // Release old dispatch object and remove it as listener
227 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
229 catch (const Exception
&)
234 // Query for dispatch object. Old dispatch will be released with this, too.
237 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
239 catch (const Exception
&)
242 listener
.second
= xDispatch
;
244 Listener
aListener( aTargetURL
, xDispatch
);
245 aDispatchVector
.push_back( aListener
);
250 // Call without locked mutex as we are called back from dispatch implementation
251 if ( !xStatusListener
.is() )
256 for (Listener
& rListener
: aDispatchVector
)
258 if ( rListener
.xDispatch
.is() )
259 rListener
.xDispatch
->addStatusListener( xStatusListener
, rListener
.aURL
);
262 catch (const Exception
&)
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */