tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sd / source / ui / tools / SlotStateListener.cxx
blobb239782e7c7fda0a6e444e2b11a05141c9ef858e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <tools/SlotStateListener.hxx>
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>
26 #include <comphelper/processfactory.hxx>
28 using namespace ::com::sun::star;
30 namespace sd::tools {
32 SlotStateListener::SlotStateListener (
33 Link<const OUString&,void> const & rCallback,
34 const uno::Reference<frame::XDispatchProvider>& rxDispatchProvider,
35 const OUString& rSlotName)
36 : mxDispatchProviderWeak(nullptr)
38 SetCallback(rCallback);
39 ConnectToDispatchProvider(rxDispatchProvider);
40 ObserveSlot(rSlotName);
43 SlotStateListener::~SlotStateListener()
45 ReleaseListeners();
48 void SlotStateListener::SetCallback (const Link<const OUString&,void>& rCallback)
50 ThrowIfDisposed();
52 maCallback = rCallback;
55 void SlotStateListener::ConnectToDispatchProvider (
56 const uno::Reference<frame::XDispatchProvider>& rxDispatchProvider)
58 ThrowIfDisposed();
60 // When we are listening to state changes of slots of another frame then
61 // release these listeners first.
62 if ( ! maRegisteredURLList.empty())
63 ReleaseListeners();
65 mxDispatchProviderWeak = rxDispatchProvider;
68 void SlotStateListener::ObserveSlot (const OUString& rSlotName)
70 ThrowIfDisposed();
72 if (maCallback.IsSet())
74 // Connect the state change listener.
75 util::URL aURL (MakeURL(rSlotName));
76 uno::Reference<frame::XDispatch> xDispatch (GetDispatch(aURL));
77 if (xDispatch.is())
79 maRegisteredURLList.push_back(aURL);
80 xDispatch->addStatusListener(this,aURL);
85 void SlotStateListener::disposing(std::unique_lock<std::mutex>&)
87 ReleaseListeners();
88 mxDispatchProviderWeak.clear();
89 maCallback = Link<const OUString&,void>();
92 util::URL SlotStateListener::MakeURL (const OUString& rSlotName)
94 util::URL aURL;
95 aURL.Complete = rSlotName;
97 uno::Reference<util::XURLTransformer> xTransformer(util::URLTransformer::create(::comphelper::getProcessComponentContext()));
98 xTransformer->parseStrict(aURL);
100 return aURL;
103 uno::Reference<frame::XDispatch>
104 SlotStateListener::GetDispatch (const util::URL& rURL) const
106 uno::Reference<frame::XDispatch> xDispatch;
108 uno::Reference<frame::XDispatchProvider> xDispatchProvider (mxDispatchProviderWeak);
109 if (xDispatchProvider.is())
110 xDispatch = xDispatchProvider->queryDispatch(rURL, OUString(), 0);
112 return xDispatch;
115 void SlotStateListener::statusChanged (
116 const frame::FeatureStateEvent& rState)
118 ThrowIfDisposed();
119 OUString sSlotName (rState.FeatureURL.Complete);
120 maCallback.Call(sSlotName);
123 void SlotStateListener::ReleaseListeners()
125 for (const auto& rURL : maRegisteredURLList)
127 uno::Reference<frame::XDispatch> xDispatch (GetDispatch(rURL));
128 if (xDispatch.is())
130 xDispatch->removeStatusListener(this,rURL);
135 //===== lang::XEventListener ================================================
137 void SAL_CALL SlotStateListener::disposing (
138 const lang::EventObject& )
142 void SlotStateListener::ThrowIfDisposed()
144 if (m_bDisposed)
146 throw lang::DisposedException (u"SlideSorterController object has already been disposed"_ustr,
147 static_cast<uno::XWeak*>(this));
151 } // end of namespace ::sd::tools
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */