LibreOffice Module vcl (master)  1
vclstatuslistener.hxx
Go to the documentation of this file.
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 
10 #ifndef INCLUDED_VCL_VCLSTATUSLISTENER_HXX
11 #define INCLUDED_VCL_VCLSTATUSLISTENER_HXX
12 
13 #include <cppuhelper/implbase.hxx>
15 #include <vcl/vclptr.hxx>
16 
17 #include <com/sun/star/frame/Desktop.hpp>
18 #include <com/sun/star/frame/XStatusListener.hpp>
19 #include <com/sun/star/frame/XDispatch.hpp>
20 #include <com/sun/star/frame/XDispatchProvider.hpp>
21 #include <com/sun/star/util/URL.hpp>
22 #include <com/sun/star/util/URLTransformer.hpp>
23 
24 template <class T> class VclStatusListener final : public cppu::WeakImplHelper < css::frame::XStatusListener>
25 {
26 public:
27  VclStatusListener<T>(T* widget, const OUString& aCommand);
28 
29 private:
33  css::uno::Reference<css::frame::XDispatch> mxDispatch;
34  css::util::URL maCommandURL;
35  css::uno::Reference<css::frame::XFrame> mxFrame;
36 
37 public:
38  void SAL_CALL statusChanged(const css::frame::FeatureStateEvent& rEvent) override;
39 
40  void SAL_CALL disposing(const css::lang::EventObject& /*Source*/) override;
41 
42  const css::uno::Reference<css::frame::XFrame>& getFrame() { return mxFrame; }
43 
44  void startListening();
45 
46  void dispose();
47 };
48 
49 template<class T>
50 VclStatusListener<T>::VclStatusListener(T* widget, const OUString& aCommand) {
51  mWidget = widget;
52 
53  css::uno::Reference<css::uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
54  css::uno::Reference<css::frame::XDesktop2> xDesktop = css::frame::Desktop::create(xContext);
55 
56  css::uno::Reference<css::frame::XFrame> xFrame(xDesktop->getActiveFrame());
57  if (!xFrame.is())
58  xFrame = xDesktop;
59 
60  mxFrame = xFrame;
61 
62  maCommandURL.Complete = aCommand;
63  css::uno::Reference<css::util::XURLTransformer> xParser = css::util::URLTransformer::create(xContext);
64  xParser->parseStrict(maCommandURL);
65 }
66 
67 template<class T>
69 {
70  if (mxDispatch.is())
71  mxDispatch->removeStatusListener(this, maCommandURL);
72 
73  css::uno::Reference<css::frame::XDispatchProvider> xDispatchProvider(mxFrame, css::uno::UNO_QUERY);
74  if (!xDispatchProvider.is())
75  return;
76 
77  mxDispatch = xDispatchProvider->queryDispatch(maCommandURL, "", 0);
78  if (mxDispatch.is())
79  mxDispatch->addStatusListener(this, maCommandURL);
80 }
81 
82 template<class T>
83 void VclStatusListener<T>::statusChanged(const css::frame::FeatureStateEvent& rEvent)
84 {
85  mWidget->statusChanged(rEvent);
86 }
87 
88 template<class T>
89 void VclStatusListener<T>::disposing(const css::lang::EventObject& /*Source*/)
90 {
91  mxDispatch.clear();
92 }
93 
94 template<class T>
96 {
97  if (mxDispatch.is()) {
98  mxDispatch->removeStatusListener(this, maCommandURL);
99  mxDispatch.clear();
100  }
101  mxFrame.clear();
102  mWidget.clear();
103 }
104 
105 
106 #endif // INCLUDED_VCL_VCLSTATUSLISTENER_HXX
107 
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SAL_CALL disposing(const css::lang::EventObject &) override
css::util::URL maCommandURL
void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &rEvent) override
Reference< XFrame > xFrame
const css::uno::Reference< css::frame::XFrame > & getFrame()
css::uno::Reference< css::frame::XDispatch > mxDispatch
The widget on which actions are performed.
css::uno::Reference< css::frame::XFrame > mxFrame
VclStatusListener(T *widget, const OUString &aCommand)