bump product version to 4.1.6.2
[LibreOffice.git] / sd / source / ui / tools / SlotStateListener.cxx
blob00833dd2c270613632807a49d8b926c239e8f52b
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 .
21 #include "tools/SlotStateListener.hxx"
22 #include <com/sun/star/frame/XStatusListener.hpp>
23 #include <com/sun/star/frame/XDispatchProvider.hpp>
24 #include <com/sun/star/frame/XDispatch.hpp>
25 #include <com/sun/star/util/URLTransformer.hpp>
26 #include <com/sun/star/util/XURLTransformer.hpp>
27 #include <com/sun/star/beans/PropertyChangeEvent.hpp>
29 #include <comphelper/processfactory.hxx>
31 using namespace ::com::sun::star;
32 using namespace ::rtl;
34 namespace sd { namespace tools {
37 SlotStateListener::SlotStateListener (
38 Link& rCallback,
39 const uno::Reference<frame::XDispatchProvider>& rxDispatchProvider,
40 const OUString& rSlotName)
41 : SlotStateListenerInterfaceBase(maMutex),
42 maCallback(),
43 mxDispatchProviderWeak(NULL)
45 SetCallback(rCallback);
46 ConnectToDispatchProvider(rxDispatchProvider);
47 ObserveSlot(rSlotName);
53 SlotStateListener::~SlotStateListener (void)
55 ReleaseListeners();
61 void SlotStateListener::SetCallback (const Link& rCallback)
63 ThrowIfDisposed();
65 maCallback = rCallback;
71 void SlotStateListener::ConnectToDispatchProvider (
72 const uno::Reference<frame::XDispatchProvider>& rxDispatchProvider)
74 ThrowIfDisposed();
76 // When we are listening to state changes of slots of another frame then
77 // release these listeners first.
78 if ( ! maRegisteredURLList.empty())
79 ReleaseListeners();
81 mxDispatchProviderWeak = rxDispatchProvider;
87 void SlotStateListener::ObserveSlot (const OUString& rSlotName)
89 ThrowIfDisposed();
91 if (maCallback.IsSet())
93 // Connect the state change listener.
94 util::URL aURL (MakeURL(rSlotName));
95 uno::Reference<frame::XDispatch> xDispatch (GetDispatch(aURL));
96 if (xDispatch.is())
98 maRegisteredURLList.push_back(aURL);
99 xDispatch->addStatusListener(this,aURL);
107 void SlotStateListener::disposing (void)
109 ReleaseListeners();
110 mxDispatchProviderWeak = uno::WeakReference<frame::XDispatchProvider>(NULL);
111 maCallback = Link();
117 util::URL SlotStateListener::MakeURL (const OUString& rSlotName) const
119 util::URL aURL;
120 aURL.Complete = rSlotName;
122 uno::Reference<util::XURLTransformer> xTransformer(util::URLTransformer::create(::comphelper::getProcessComponentContext()));
123 xTransformer->parseStrict(aURL);
125 return aURL;
131 uno::Reference<frame::XDispatch>
132 SlotStateListener::GetDispatch (const util::URL& rURL) const
134 uno::Reference<frame::XDispatch> xDispatch;
136 uno::Reference<frame::XDispatchProvider> xDispatchProvider (mxDispatchProviderWeak);
137 if (xDispatchProvider.is())
138 xDispatch = xDispatchProvider->queryDispatch(rURL, OUString(), 0);
140 return xDispatch;
146 void SlotStateListener::statusChanged (
147 const frame::FeatureStateEvent& rState)
148 throw (uno::RuntimeException)
150 ThrowIfDisposed();
151 OUString sSlotName (rState.FeatureURL.Complete);
152 if (maCallback.IsSet())
153 maCallback.Call(&sSlotName);
159 void SlotStateListener::ReleaseListeners (void)
161 if ( ! maRegisteredURLList.empty())
163 RegisteredURLList::iterator iURL (maRegisteredURLList.begin());
164 RegisteredURLList::iterator iEnd (maRegisteredURLList.end());
165 for (; iURL!=iEnd; ++iURL)
167 uno::Reference<frame::XDispatch> xDispatch (GetDispatch(*iURL));
168 if (xDispatch.is())
170 xDispatch->removeStatusListener(this,*iURL);
179 //===== lang::XEventListener ================================================
181 void SAL_CALL SlotStateListener::disposing (
182 const lang::EventObject& )
183 throw (uno::RuntimeException)
190 void SlotStateListener::ThrowIfDisposed (void)
191 throw (lang::DisposedException)
193 if (rBHelper.bDisposed || rBHelper.bInDispose)
195 throw lang::DisposedException ("SlideSorterController object has already been disposed",
196 static_cast<uno::XWeak*>(this));
203 } } // end of namespace ::sd::tools
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */