1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
11*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
12*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
13*cdf0e10cSrcweir  *
14*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
15*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
18*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
19*cdf0e10cSrcweir  *
20*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
21*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
22*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
23*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
24*cdf0e10cSrcweir  *
25*cdf0e10cSrcweir ************************************************************************/
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
28*cdf0e10cSrcweir #include "precompiled_svx.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "formdispatchinterceptor.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir /** === begin UNO includes === **/
33*cdf0e10cSrcweir /** === end UNO includes === **/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <tools/debug.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir //........................................................................
38*cdf0e10cSrcweir namespace svxform
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir //........................................................................
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 	/** === begin UNO using === **/
43*cdf0e10cSrcweir 	using ::com::sun::star::uno::Reference;
44*cdf0e10cSrcweir 	using ::com::sun::star::uno::XInterface;
45*cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY;
46*cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY_THROW;
47*cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_SET_THROW;
48*cdf0e10cSrcweir 	using ::com::sun::star::uno::Exception;
49*cdf0e10cSrcweir 	using ::com::sun::star::uno::RuntimeException;
50*cdf0e10cSrcweir 	using ::com::sun::star::uno::Any;
51*cdf0e10cSrcweir 	using ::com::sun::star::uno::makeAny;
52*cdf0e10cSrcweir 	using ::com::sun::star::uno::Sequence;
53*cdf0e10cSrcweir 	using ::com::sun::star::uno::Type;
54*cdf0e10cSrcweir     using ::com::sun::star::frame::XDispatchProviderInterception;
55*cdf0e10cSrcweir     using ::com::sun::star::frame::XDispatchProviderInterceptor;
56*cdf0e10cSrcweir     using ::com::sun::star::lang::XComponent;
57*cdf0e10cSrcweir     using ::com::sun::star::util::URL;
58*cdf0e10cSrcweir     using ::com::sun::star::frame::XDispatch;
59*cdf0e10cSrcweir     using ::com::sun::star::frame::DispatchDescriptor;
60*cdf0e10cSrcweir     using ::com::sun::star::frame::XDispatchProvider;
61*cdf0e10cSrcweir     using ::com::sun::star::lang::EventObject;
62*cdf0e10cSrcweir 	/** === end UNO using === **/
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     //========================================================================
65*cdf0e10cSrcweir     //= DispatchInterceptionMultiplexer
66*cdf0e10cSrcweir     //========================================================================
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     DBG_NAME(DispatchInterceptionMultiplexer)
69*cdf0e10cSrcweir     //------------------------------------------------------------------------
70*cdf0e10cSrcweir     DispatchInterceptionMultiplexer::DispatchInterceptionMultiplexer(
71*cdf0e10cSrcweir 			    const Reference< XDispatchProviderInterception >& _rxToIntercept, DispatchInterceptor* _pMaster )
72*cdf0e10cSrcweir 	    :DispatchInterceptionMultiplexer_BASE(_pMaster && _pMaster->getInterceptorMutex() ? *_pMaster->getInterceptorMutex() : m_aFallback)
73*cdf0e10cSrcweir         ,m_aFallback()
74*cdf0e10cSrcweir         ,m_pMutex( _pMaster && _pMaster->getInterceptorMutex() ? _pMaster->getInterceptorMutex() : &m_aFallback )
75*cdf0e10cSrcweir 	    ,m_xIntercepted(_rxToIntercept)
76*cdf0e10cSrcweir 	    ,m_bListening(sal_False)
77*cdf0e10cSrcweir 	    ,m_pMaster(_pMaster)
78*cdf0e10cSrcweir     {
79*cdf0e10cSrcweir 	    DBG_CTOR(DispatchInterceptionMultiplexer,NULL);
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 	    ::osl::MutexGuard aGuard( *m_pMutex );
82*cdf0e10cSrcweir 	    ::comphelper::increment(m_refCount);
83*cdf0e10cSrcweir 	    if (_rxToIntercept.is())
84*cdf0e10cSrcweir 	    {
85*cdf0e10cSrcweir 		    _rxToIntercept->registerDispatchProviderInterceptor((XDispatchProviderInterceptor*)this);
86*cdf0e10cSrcweir 		    // this should make us the top-level dispatch-provider for the component, via a call to our
87*cdf0e10cSrcweir 		    // setDispatchProvider we should have got an fallback for requests we (i.e. our master) cannot fullfill
88*cdf0e10cSrcweir 		    Reference< XComponent> xInterceptedComponent(_rxToIntercept, UNO_QUERY);
89*cdf0e10cSrcweir 		    if (xInterceptedComponent.is())
90*cdf0e10cSrcweir 		    {
91*cdf0e10cSrcweir 			    xInterceptedComponent->addEventListener(this);
92*cdf0e10cSrcweir 			    m_bListening = sal_True;
93*cdf0e10cSrcweir 		    }
94*cdf0e10cSrcweir 	    }
95*cdf0e10cSrcweir 	    ::comphelper::decrement(m_refCount);
96*cdf0e10cSrcweir     }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     //------------------------------------------------------------------------
99*cdf0e10cSrcweir     DispatchInterceptionMultiplexer::~DispatchInterceptionMultiplexer()
100*cdf0e10cSrcweir     {
101*cdf0e10cSrcweir 	    if (!rBHelper.bDisposed)
102*cdf0e10cSrcweir 		    dispose();
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	    DBG_DTOR(DispatchInterceptionMultiplexer,NULL);
105*cdf0e10cSrcweir     }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     //------------------------------------------------------------------------------
108*cdf0e10cSrcweir     Reference< XDispatch > SAL_CALL DispatchInterceptionMultiplexer::queryDispatch( const URL& aURL, const ::rtl::OUString& aTargetFrameName, sal_Int32 nSearchFlags ) throw(RuntimeException)
109*cdf0e10cSrcweir     {
110*cdf0e10cSrcweir 	    ::osl::MutexGuard aGuard( *m_pMutex );
111*cdf0e10cSrcweir 	    Reference< XDispatch> xResult;
112*cdf0e10cSrcweir 	    // ask our 'real' interceptor
113*cdf0e10cSrcweir 	    if (m_pMaster)
114*cdf0e10cSrcweir 		    xResult = m_pMaster->interceptedQueryDispatch( aURL, aTargetFrameName, nSearchFlags);
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 	    // ask our slave provider
117*cdf0e10cSrcweir 	    if (!xResult.is() && m_xSlaveDispatcher.is())
118*cdf0e10cSrcweir 		    xResult = m_xSlaveDispatcher->queryDispatch(aURL, aTargetFrameName, nSearchFlags);
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 	    return xResult;
121*cdf0e10cSrcweir     }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     //------------------------------------------------------------------------------
124*cdf0e10cSrcweir     Sequence< Reference< XDispatch > > SAL_CALL
125*cdf0e10cSrcweir     DispatchInterceptionMultiplexer::queryDispatches( const Sequence< DispatchDescriptor >& aDescripts ) throw(RuntimeException)
126*cdf0e10cSrcweir     {
127*cdf0e10cSrcweir 	    ::osl::MutexGuard aGuard( *m_pMutex );
128*cdf0e10cSrcweir 	    Sequence< Reference< XDispatch> > aReturn(aDescripts.getLength());
129*cdf0e10cSrcweir 	    Reference< XDispatch>* pReturn = aReturn.getArray();
130*cdf0e10cSrcweir 	    const DispatchDescriptor* pDescripts = aDescripts.getConstArray();
131*cdf0e10cSrcweir 	    for (sal_Int16 i=0; i<aDescripts.getLength(); ++i, ++pReturn, ++pDescripts)
132*cdf0e10cSrcweir 	    {
133*cdf0e10cSrcweir 		    *pReturn = queryDispatch(pDescripts->FeatureURL, pDescripts->FrameName, pDescripts->SearchFlags);
134*cdf0e10cSrcweir 	    }
135*cdf0e10cSrcweir 	    return aReturn;
136*cdf0e10cSrcweir     }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     //------------------------------------------------------------------------------
139*cdf0e10cSrcweir     Reference< XDispatchProvider > SAL_CALL DispatchInterceptionMultiplexer::getSlaveDispatchProvider(	) throw(RuntimeException)
140*cdf0e10cSrcweir     {
141*cdf0e10cSrcweir 	    ::osl::MutexGuard aGuard( *m_pMutex );
142*cdf0e10cSrcweir 	    return m_xSlaveDispatcher;
143*cdf0e10cSrcweir     }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     //------------------------------------------------------------------------------
146*cdf0e10cSrcweir     void SAL_CALL DispatchInterceptionMultiplexer::setSlaveDispatchProvider(const Reference< XDispatchProvider>& xNewDispatchProvider) throw( RuntimeException )
147*cdf0e10cSrcweir     {
148*cdf0e10cSrcweir 	    ::osl::MutexGuard aGuard( *m_pMutex );
149*cdf0e10cSrcweir 	    m_xSlaveDispatcher = xNewDispatchProvider;
150*cdf0e10cSrcweir     }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     //------------------------------------------------------------------------------
153*cdf0e10cSrcweir     Reference< XDispatchProvider> SAL_CALL DispatchInterceptionMultiplexer::getMasterDispatchProvider(void) throw( RuntimeException )
154*cdf0e10cSrcweir     {
155*cdf0e10cSrcweir 	    ::osl::MutexGuard aGuard( *m_pMutex );
156*cdf0e10cSrcweir 	    return m_xMasterDispatcher;
157*cdf0e10cSrcweir     }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     //------------------------------------------------------------------------------
160*cdf0e10cSrcweir     void SAL_CALL DispatchInterceptionMultiplexer::setMasterDispatchProvider(const Reference< XDispatchProvider>& xNewSupplier) throw( RuntimeException )
161*cdf0e10cSrcweir     {
162*cdf0e10cSrcweir 	    ::osl::MutexGuard aGuard( *m_pMutex );
163*cdf0e10cSrcweir 	    m_xMasterDispatcher = xNewSupplier;
164*cdf0e10cSrcweir     }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     //------------------------------------------------------------------------------
167*cdf0e10cSrcweir     void SAL_CALL DispatchInterceptionMultiplexer::disposing(const EventObject& Source) throw( RuntimeException )
168*cdf0e10cSrcweir     {
169*cdf0e10cSrcweir 	    if (m_bListening)
170*cdf0e10cSrcweir 	    {
171*cdf0e10cSrcweir 		    Reference< XDispatchProviderInterception > xIntercepted(m_xIntercepted.get(), UNO_QUERY);
172*cdf0e10cSrcweir 		    if (Source.Source == xIntercepted)
173*cdf0e10cSrcweir 			    ImplDetach();
174*cdf0e10cSrcweir 	    }
175*cdf0e10cSrcweir     }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir     //------------------------------------------------------------------------------
178*cdf0e10cSrcweir     void DispatchInterceptionMultiplexer::ImplDetach()
179*cdf0e10cSrcweir     {
180*cdf0e10cSrcweir 	    ::osl::MutexGuard aGuard( *m_pMutex );
181*cdf0e10cSrcweir 	    OSL_ENSURE(m_bListening, "DispatchInterceptionMultiplexer::ImplDetach: invalid call!");
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 	    // deregister ourself from the interception component
184*cdf0e10cSrcweir 	    Reference< XDispatchProviderInterception > xIntercepted(m_xIntercepted.get(), UNO_QUERY);
185*cdf0e10cSrcweir 	    if (xIntercepted.is())
186*cdf0e10cSrcweir 		    xIntercepted->releaseDispatchProviderInterceptor(static_cast<XDispatchProviderInterceptor*>(this));
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir     //	m_xIntercepted = Reference< XDispatchProviderInterception >();
189*cdf0e10cSrcweir 		    // Don't reset m_xIntercepted: It may be needed by our owner to check for which object we were
190*cdf0e10cSrcweir 		    // responsible. As we hold the object with a weak reference only, this should be no problem.
191*cdf0e10cSrcweir 		    // 88936 - 23.07.2001 - frank.schoenheit@sun.com
192*cdf0e10cSrcweir 	    m_pMaster = NULL;
193*cdf0e10cSrcweir         m_pMutex = &m_aFallback;
194*cdf0e10cSrcweir 	    m_bListening = sal_False;
195*cdf0e10cSrcweir     }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     //------------------------------------------------------------------------------
198*cdf0e10cSrcweir     void DispatchInterceptionMultiplexer::disposing()
199*cdf0e10cSrcweir     {
200*cdf0e10cSrcweir 	    // remove ourself as event listener from the interception component
201*cdf0e10cSrcweir 	    if (m_bListening)
202*cdf0e10cSrcweir 	    {
203*cdf0e10cSrcweir 		    Reference< XComponent> xInterceptedComponent(m_xIntercepted.get(), UNO_QUERY);
204*cdf0e10cSrcweir 		    if (xInterceptedComponent.is())
205*cdf0e10cSrcweir 			    xInterceptedComponent->removeEventListener(static_cast<XEventListener*>(this));
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 		    // detach from the interception component
208*cdf0e10cSrcweir 		    ImplDetach();
209*cdf0e10cSrcweir 	    }
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir //........................................................................
213*cdf0e10cSrcweir } // namespace svxform
214*cdf0e10cSrcweir //........................................................................
215