1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SlotStateListener.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "tools/SlotStateListener.hxx"
35 #include <com/sun/star/frame/XStatusListener.hpp>
36 #include <com/sun/star/frame/XDispatchProvider.hpp>
37 #ifndef _COM_SUN_STAR_FRAME_XDISPATCHP_HPP_
38 #include <com/sun/star/frame/XDispatch.hpp>
40 #include <com/sun/star/util/XURLTransformer.hpp>
41 #include <com/sun/star/beans/PropertyChangeEvent.hpp>
43 #include <comphelper/processfactory.hxx>
45 using namespace ::com::sun::star
;
46 using namespace ::rtl
;
48 namespace sd
{ namespace tools
{
51 SlotStateListener::SlotStateListener (void)
52 : SlotStateListenerInterfaceBase(maMutex
),
54 mxDispatchProviderWeak(NULL
)
61 SlotStateListener::SlotStateListener (
63 const uno::Reference
<frame::XDispatchProvider
>& rxDispatchProvider
,
64 const ::rtl::OUString
& rSlotName
)
65 : SlotStateListenerInterfaceBase(maMutex
),
67 mxDispatchProviderWeak(NULL
)
69 SetCallback(rCallback
);
70 ConnectToDispatchProvider(rxDispatchProvider
);
71 ObserveSlot(rSlotName
);
77 SlotStateListener::~SlotStateListener (void)
85 void SlotStateListener::SetCallback (const Link
& rCallback
)
89 maCallback
= rCallback
;
95 void SlotStateListener::ConnectToDispatchProvider (
96 const uno::Reference
<frame::XDispatchProvider
>& rxDispatchProvider
)
100 // When we are listening to state changes of slots of another frame then
101 // release these listeners first.
102 if ( ! maRegisteredURLList
.empty())
105 mxDispatchProviderWeak
= rxDispatchProvider
;
111 void SlotStateListener::ObserveSlot (const ::rtl::OUString
& rSlotName
)
115 if (maCallback
.IsSet())
117 // Connect the state change listener.
118 util::URL
aURL (MakeURL(rSlotName
));
119 uno::Reference
<frame::XDispatch
> xDispatch (GetDispatch(aURL
));
122 maRegisteredURLList
.push_back(aURL
);
123 xDispatch
->addStatusListener(this,aURL
);
131 bool SlotStateListener::IsValid (void) const
133 return maRegisteredURLList
.size() > 0;
139 void SlotStateListener::disposing (void)
142 mxDispatchProviderWeak
= uno::WeakReference
<frame::XDispatchProvider
>(NULL
);
149 util::URL
SlotStateListener::MakeURL (const OUString
& rSlotName
) const
153 aURL
.Complete
= rSlotName
;
155 uno::Reference
<lang::XMultiServiceFactory
> xServiceManager (
156 ::comphelper::getProcessServiceFactory());
157 if (xServiceManager
.is())
159 uno::Reference
<util::XURLTransformer
> xTransformer(xServiceManager
->createInstance(
160 OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.util.URLTransformer"))),
162 if (xTransformer
.is())
163 xTransformer
->parseStrict(aURL
);
172 uno::Reference
<frame::XDispatch
>
173 SlotStateListener::GetDispatch (const util::URL
& rURL
) const
175 uno::Reference
<frame::XDispatch
> xDispatch
;
177 uno::Reference
<frame::XDispatchProvider
> xDispatchProvider (mxDispatchProviderWeak
);
178 if (xDispatchProvider
.is())
179 xDispatch
= xDispatchProvider
->queryDispatch(rURL
, OUString(), 0);
187 void SlotStateListener::statusChanged (
188 const frame::FeatureStateEvent
& rState
)
189 throw (uno::RuntimeException
)
192 OUString
sSlotName (rState
.FeatureURL
.Complete
);
193 if (maCallback
.IsSet())
194 maCallback
.Call(&sSlotName
);
200 void SlotStateListener::ReleaseListeners (void)
202 if ( ! maRegisteredURLList
.empty())
204 RegisteredURLList::iterator
iURL (maRegisteredURLList
.begin());
205 RegisteredURLList::iterator
iEnd (maRegisteredURLList
.end());
206 for (; iURL
!=iEnd
; ++iURL
)
208 uno::Reference
<frame::XDispatch
> xDispatch (GetDispatch(*iURL
));
211 xDispatch
->removeStatusListener(this,*iURL
);
220 //===== lang::XEventListener ================================================
222 void SAL_CALL
SlotStateListener::disposing (
223 const lang::EventObject
& )
224 throw (uno::RuntimeException
)
231 void SlotStateListener::ThrowIfDisposed (void)
232 throw (lang::DisposedException
)
234 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
236 throw lang::DisposedException (
237 OUString(RTL_CONSTASCII_USTRINGPARAM(
238 "SlideSorterController object has already been disposed")),
239 static_cast<uno::XWeak
*>(this));
246 } } // end of namespace ::sd::tools