Bump version to 21.06.18.1
[LibreOffice.git] / sd / source / ui / tools / SlotStateListener.cxx
blob5fb078f14127d70242456bc879f6c112f8a001b2
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 : SlotStateListenerInterfaceBase(maMutex),
37 maCallback(),
38 mxDispatchProviderWeak(nullptr)
40 SetCallback(rCallback);
41 ConnectToDispatchProvider(rxDispatchProvider);
42 ObserveSlot(rSlotName);
45 SlotStateListener::~SlotStateListener()
47 ReleaseListeners();
50 void SlotStateListener::SetCallback (const Link<const OUString&,void>& rCallback)
52 ThrowIfDisposed();
54 maCallback = rCallback;
57 void SlotStateListener::ConnectToDispatchProvider (
58 const uno::Reference<frame::XDispatchProvider>& rxDispatchProvider)
60 ThrowIfDisposed();
62 // When we are listening to state changes of slots of another frame then
63 // release these listeners first.
64 if ( ! maRegisteredURLList.empty())
65 ReleaseListeners();
67 mxDispatchProviderWeak = rxDispatchProvider;
70 void SlotStateListener::ObserveSlot (const OUString& rSlotName)
72 ThrowIfDisposed();
74 if (maCallback.IsSet())
76 // Connect the state change listener.
77 util::URL aURL (MakeURL(rSlotName));
78 uno::Reference<frame::XDispatch> xDispatch (GetDispatch(aURL));
79 if (xDispatch.is())
81 maRegisteredURLList.push_back(aURL);
82 xDispatch->addStatusListener(this,aURL);
87 void SlotStateListener::disposing()
89 ReleaseListeners();
90 mxDispatchProviderWeak = uno::WeakReference<frame::XDispatchProvider>(nullptr);
91 maCallback = Link<const OUString&,void>();
94 util::URL SlotStateListener::MakeURL (const OUString& rSlotName)
96 util::URL aURL;
97 aURL.Complete = rSlotName;
99 uno::Reference<util::XURLTransformer> xTransformer(util::URLTransformer::create(::comphelper::getProcessComponentContext()));
100 xTransformer->parseStrict(aURL);
102 return aURL;
105 uno::Reference<frame::XDispatch>
106 SlotStateListener::GetDispatch (const util::URL& rURL) const
108 uno::Reference<frame::XDispatch> xDispatch;
110 uno::Reference<frame::XDispatchProvider> xDispatchProvider (mxDispatchProviderWeak);
111 if (xDispatchProvider.is())
112 xDispatch = xDispatchProvider->queryDispatch(rURL, OUString(), 0);
114 return xDispatch;
117 void SlotStateListener::statusChanged (
118 const frame::FeatureStateEvent& rState)
120 ThrowIfDisposed();
121 OUString sSlotName (rState.FeatureURL.Complete);
122 maCallback.Call(sSlotName);
125 void SlotStateListener::ReleaseListeners()
127 for (const auto& rURL : maRegisteredURLList)
129 uno::Reference<frame::XDispatch> xDispatch (GetDispatch(rURL));
130 if (xDispatch.is())
132 xDispatch->removeStatusListener(this,rURL);
137 //===== lang::XEventListener ================================================
139 void SAL_CALL SlotStateListener::disposing (
140 const lang::EventObject& )
144 void SlotStateListener::ThrowIfDisposed()
146 if (rBHelper.bDisposed || rBHelper.bInDispose)
148 throw lang::DisposedException ("SlideSorterController object has already been disposed",
149 static_cast<uno::XWeak*>(this));
153 } // end of namespace ::sd::tools
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */