LibreOffice Module sd (master) 1
SlotStateListener.cxx
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 * 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
21#include <com/sun/star/frame/XDispatchProvider.hpp>
22#include <com/sun/star/frame/XDispatch.hpp>
23#include <com/sun/star/util/URLTransformer.hpp>
24#include <com/sun/star/util/XURLTransformer.hpp>
25
27
28using namespace ::com::sun::star;
29
30namespace sd::tools {
31
33 Link<const OUString&,void> const & rCallback,
34 const uno::Reference<frame::XDispatchProvider>& rxDispatchProvider,
35 const OUString& rSlotName)
36 : mxDispatchProviderWeak(nullptr)
37{
38 SetCallback(rCallback);
39 ConnectToDispatchProvider(rxDispatchProvider);
40 ObserveSlot(rSlotName);
41}
42
44{
46}
47
49{
51
52 maCallback = rCallback;
53}
54
56 const uno::Reference<frame::XDispatchProvider>& rxDispatchProvider)
57{
59
60 // When we are listening to state changes of slots of another frame then
61 // release these listeners first.
62 if ( ! maRegisteredURLList.empty())
64
65 mxDispatchProviderWeak = rxDispatchProvider;
66}
67
68void SlotStateListener::ObserveSlot (const OUString& rSlotName)
69{
71
72 if (maCallback.IsSet())
73 {
74 // Connect the state change listener.
75 util::URL aURL (MakeURL(rSlotName));
76 uno::Reference<frame::XDispatch> xDispatch (GetDispatch(aURL));
77 if (xDispatch.is())
78 {
79 maRegisteredURLList.push_back(aURL);
80 xDispatch->addStatusListener(this,aURL);
81 }
82 }
83}
84
85void SlotStateListener::disposing(std::unique_lock<std::mutex>&)
86{
90}
91
92util::URL SlotStateListener::MakeURL (const OUString& rSlotName)
93{
94 util::URL aURL;
95 aURL.Complete = rSlotName;
96
97 uno::Reference<util::XURLTransformer> xTransformer(util::URLTransformer::create(::comphelper::getProcessComponentContext()));
98 xTransformer->parseStrict(aURL);
99
100 return aURL;
101}
102
103uno::Reference<frame::XDispatch>
104 SlotStateListener::GetDispatch (const util::URL& rURL) const
105{
106 uno::Reference<frame::XDispatch> xDispatch;
107
108 uno::Reference<frame::XDispatchProvider> xDispatchProvider (mxDispatchProviderWeak);
109 if (xDispatchProvider.is())
110 xDispatch = xDispatchProvider->queryDispatch(rURL, OUString(), 0);
111
112 return xDispatch;
113}
114
116 const frame::FeatureStateEvent& rState)
117{
119 OUString sSlotName (rState.FeatureURL.Complete);
120 maCallback.Call(sSlotName);
121}
122
124{
125 for (const auto& rURL : maRegisteredURLList)
126 {
127 uno::Reference<frame::XDispatch> xDispatch (GetDispatch(rURL));
128 if (xDispatch.is())
129 {
130 xDispatch->removeStatusListener(this,rURL);
131 }
132 }
133}
134
135//===== lang::XEventListener ================================================
136
137void SAL_CALL SlotStateListener::disposing (
138 const lang::EventObject& )
139{
140}
141
143{
144 if (m_bDisposed)
145 {
146 throw lang::DisposedException ("SlideSorterController object has already been disposed",
147 static_cast<uno::XWeak*>(this));
148 }
149}
150
151} // end of namespace ::sd::tools
152
153/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &rState) override
Called by slot state change broadcasters.
void ReleaseListeners()
Deregister all currently active state change listeners.
css::uno::Reference< css::frame::XDispatch > GetDispatch(const css::util::URL &rURL) const
Return an XDispatch object for the given URL.
std::vector< css::util::URL > maRegisteredURLList
Remember the URLs that describe slots whose state changes we are listening to.
void SetCallback(const Link< const OUString &, void > &rCallback)
Set the callback to the given value.
virtual ~SlotStateListener() override
The constructor de-registers all remaining listeners.
css::uno::WeakReference< css::frame::XDispatchProvider > mxDispatchProviderWeak
SlotStateListener(Link< const OUString &, void > const &rCallback, const css::uno::Reference< css::frame::XDispatchProvider > &rxDispatchProvider, const OUString &rSlotName)
This convenience version of the constructor takes all parameters that are necessary to observe a sing...
Link< const OUString &, void > maCallback
virtual void SAL_CALL disposing(const css::lang::EventObject &rEvent) override
void ConnectToDispatchProvider(const css::uno::Reference< css::frame::XDispatchProvider > &rxDispatchProvider)
Set the frame whose slots shall be observed.
static css::util::URL MakeURL(const OUString &rSlotName)
Transform the given string into a URL object.
void ObserveSlot(const OUString &rSlotName)
Observe the slot specified by the given name.
Reference< XDispatch > xDispatch
URL aURL