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 <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>
25 #include <com/sun/star/beans/PropertyChangeEvent.hpp>
27 #include <comphelper/processfactory.hxx>
29 using namespace ::com::sun::star
;
31 namespace sd
{ namespace tools
{
33 SlotStateListener::SlotStateListener (
34 Link
<const OUString
&,void> const & rCallback
,
35 const uno::Reference
<frame::XDispatchProvider
>& rxDispatchProvider
,
36 const OUString
& rSlotName
)
37 : SlotStateListenerInterfaceBase(maMutex
),
39 mxDispatchProviderWeak(nullptr)
41 SetCallback(rCallback
);
42 ConnectToDispatchProvider(rxDispatchProvider
);
43 ObserveSlot(rSlotName
);
46 SlotStateListener::~SlotStateListener()
51 void SlotStateListener::SetCallback (const Link
<const OUString
&,void>& rCallback
)
55 maCallback
= rCallback
;
58 void SlotStateListener::ConnectToDispatchProvider (
59 const uno::Reference
<frame::XDispatchProvider
>& rxDispatchProvider
)
63 // When we are listening to state changes of slots of another frame then
64 // release these listeners first.
65 if ( ! maRegisteredURLList
.empty())
68 mxDispatchProviderWeak
= rxDispatchProvider
;
71 void SlotStateListener::ObserveSlot (const OUString
& rSlotName
)
75 if (maCallback
.IsSet())
77 // Connect the state change listener.
78 util::URL
aURL (MakeURL(rSlotName
));
79 uno::Reference
<frame::XDispatch
> xDispatch (GetDispatch(aURL
));
82 maRegisteredURLList
.push_back(aURL
);
83 xDispatch
->addStatusListener(this,aURL
);
88 void SlotStateListener::disposing()
91 mxDispatchProviderWeak
= uno::WeakReference
<frame::XDispatchProvider
>(nullptr);
92 maCallback
= Link
<const OUString
&,void>();
95 util::URL
SlotStateListener::MakeURL (const OUString
& rSlotName
)
98 aURL
.Complete
= rSlotName
;
100 uno::Reference
<util::XURLTransformer
> xTransformer(util::URLTransformer::create(::comphelper::getProcessComponentContext()));
101 xTransformer
->parseStrict(aURL
);
106 uno::Reference
<frame::XDispatch
>
107 SlotStateListener::GetDispatch (const util::URL
& rURL
) const
109 uno::Reference
<frame::XDispatch
> xDispatch
;
111 uno::Reference
<frame::XDispatchProvider
> xDispatchProvider (mxDispatchProviderWeak
);
112 if (xDispatchProvider
.is())
113 xDispatch
= xDispatchProvider
->queryDispatch(rURL
, OUString(), 0);
118 void SlotStateListener::statusChanged (
119 const frame::FeatureStateEvent
& rState
)
122 OUString
sSlotName (rState
.FeatureURL
.Complete
);
123 maCallback
.Call(sSlotName
);
126 void SlotStateListener::ReleaseListeners()
128 for (const auto& rURL
: maRegisteredURLList
)
130 uno::Reference
<frame::XDispatch
> xDispatch (GetDispatch(rURL
));
133 xDispatch
->removeStatusListener(this,rURL
);
138 //===== lang::XEventListener ================================================
140 void SAL_CALL
SlotStateListener::disposing (
141 const lang::EventObject
& )
145 void SlotStateListener::ThrowIfDisposed()
147 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
149 throw lang::DisposedException ("SlideSorterController object has already been disposed",
150 static_cast<uno::XWeak
*>(this));
154 } } // end of namespace ::sd::tools
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */