1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_framework.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //_______________________________________________
32*cdf0e10cSrcweir // my own includes
33*cdf0e10cSrcweir #include <dispatch/loaddispatcher.hxx>
34*cdf0e10cSrcweir #include <threadhelp/readguard.hxx>
35*cdf0e10cSrcweir #include <threadhelp/writeguard.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir //_______________________________________________
38*cdf0e10cSrcweir // interface includes
39*cdf0e10cSrcweir #include <com/sun/star/frame/DispatchResultState.hpp>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir //_______________________________________________
42*cdf0e10cSrcweir // includes of other projects
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir //_______________________________________________
45*cdf0e10cSrcweir // namespace
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir namespace framework{
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace css = ::com::sun::star;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //_______________________________________________
52*cdf0e10cSrcweir // declarations
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir /*-----------------------------------------------
55*cdf0e10cSrcweir     20.08.2003 09:52
56*cdf0e10cSrcweir -----------------------------------------------*/
57*cdf0e10cSrcweir LoadDispatcher::LoadDispatcher(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR       ,
58*cdf0e10cSrcweir                                const css::uno::Reference< css::frame::XFrame >&              xOwnerFrame ,
59*cdf0e10cSrcweir                                const ::rtl::OUString                                         sTargetName ,
60*cdf0e10cSrcweir                                      sal_Int32                                               nSearchFlags)
61*cdf0e10cSrcweir     : ThreadHelpBase(            )
62*cdf0e10cSrcweir     , m_xSMGR       (xSMGR       )
63*cdf0e10cSrcweir     , m_xOwnerFrame (xOwnerFrame )
64*cdf0e10cSrcweir     , m_sTarget     (sTargetName )
65*cdf0e10cSrcweir     , m_nSearchFlags(nSearchFlags)
66*cdf0e10cSrcweir     , m_aLoader     (xSMGR       )
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir /*-----------------------------------------------
71*cdf0e10cSrcweir     20.08.2003 09:12
72*cdf0e10cSrcweir -----------------------------------------------*/
73*cdf0e10cSrcweir LoadDispatcher::~LoadDispatcher()
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir     m_xSMGR.clear();
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir /*-----------------------------------------------
79*cdf0e10cSrcweir     20.08.2003 09:58
80*cdf0e10cSrcweir -----------------------------------------------*/
81*cdf0e10cSrcweir void SAL_CALL LoadDispatcher::dispatchWithNotification(const css::util::URL&                                             aURL      ,
82*cdf0e10cSrcweir                                                        const css::uno::Sequence< css::beans::PropertyValue >&            lArguments,
83*cdf0e10cSrcweir                                                        const css::uno::Reference< css::frame::XDispatchResultListener >& xListener )
84*cdf0e10cSrcweir     throw(css::uno::RuntimeException)
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir     impl_dispatch( aURL, lArguments, xListener );
87*cdf0e10cSrcweir }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir /*-----------------------------------------------
90*cdf0e10cSrcweir     20.08.2003 09:16
91*cdf0e10cSrcweir -----------------------------------------------*/
92*cdf0e10cSrcweir void SAL_CALL LoadDispatcher::dispatch(const css::util::URL&                                  aURL      ,
93*cdf0e10cSrcweir                                        const css::uno::Sequence< css::beans::PropertyValue >& lArguments)
94*cdf0e10cSrcweir     throw(css::uno::RuntimeException)
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir     impl_dispatch( aURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >() );
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir /*-----------------------------------------------
100*cdf0e10cSrcweir     14.04.2008
101*cdf0e10cSrcweir -----------------------------------------------*/
102*cdf0e10cSrcweir css::uno::Any SAL_CALL LoadDispatcher::dispatchWithReturnValue( const css::util::URL& rURL,
103*cdf0e10cSrcweir                                                                 const css::uno::Sequence< css::beans::PropertyValue >& lArguments )
104*cdf0e10cSrcweir     throw( css::uno::RuntimeException )
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir     return impl_dispatch( rURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >());
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir /*-----------------------------------------------
110*cdf0e10cSrcweir     20.08.2003 10:48
111*cdf0e10cSrcweir -----------------------------------------------*/
112*cdf0e10cSrcweir void SAL_CALL LoadDispatcher::addStatusListener(const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/,
113*cdf0e10cSrcweir                                                 const css::util::URL&                                     /*aURL*/     )
114*cdf0e10cSrcweir     throw(css::uno::RuntimeException)
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir /*-----------------------------------------------
119*cdf0e10cSrcweir     20.08.2003 10:49
120*cdf0e10cSrcweir -----------------------------------------------*/
121*cdf0e10cSrcweir void SAL_CALL LoadDispatcher::removeStatusListener(const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/,
122*cdf0e10cSrcweir                                                    const css::util::URL&                                     /*aURL*/     )
123*cdf0e10cSrcweir     throw(css::uno::RuntimeException)
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir /*-----------------------------------------------
128*cdf0e10cSrcweir     20.08.2003 09:58
129*cdf0e10cSrcweir -----------------------------------------------*/
130*cdf0e10cSrcweir css::uno::Any LoadDispatcher::impl_dispatch( const css::util::URL& rURL,
131*cdf0e10cSrcweir                                              const css::uno::Sequence< css::beans::PropertyValue >& lArguments,
132*cdf0e10cSrcweir                                              const css::uno::Reference< css::frame::XDispatchResultListener >& xListener )
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir     // Attention: May be nobody outside hold such temp. dispatch object alive (because
135*cdf0e10cSrcweir     // the container in which we resists isnt implemented threadsafe but updated by a timer
136*cdf0e10cSrcweir     // and clear our reference ...) we should hold us self alive!
137*cdf0e10cSrcweir     css::uno::Reference< css::uno::XInterface > xThis(static_cast< css::frame::XNotifyingDispatch* >(this), css::uno::UNO_QUERY);
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     // SAFE -> ----------------------------------
140*cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     // We are the only client of this load env object ... but
143*cdf0e10cSrcweir     // may a dispatch request before is still in progress (?!).
144*cdf0e10cSrcweir     // Then we should wait a little bit and block this new request.
145*cdf0e10cSrcweir     // In case we run into the timeout, we should reject this new request
146*cdf0e10cSrcweir     // and return "FAILED" as result. Otherwhise we can start this new operation.
147*cdf0e10cSrcweir     if (!m_aLoader.waitWhileLoading(2000)) // => 2 sec.
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir         if (xListener.is())
150*cdf0e10cSrcweir             xListener->dispatchFinished(
151*cdf0e10cSrcweir                 css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::DONTKNOW, css::uno::Any())); // DONTKNOW? ... not realy started ... not realy failed :-)
152*cdf0e10cSrcweir     }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     css::uno::Reference< css::frame::XFrame > xBaseFrame(m_xOwnerFrame.get(), css::uno::UNO_QUERY);
155*cdf0e10cSrcweir     if (!xBaseFrame.is())
156*cdf0e10cSrcweir     {
157*cdf0e10cSrcweir         if (xListener.is())
158*cdf0e10cSrcweir             xListener->dispatchFinished(
159*cdf0e10cSrcweir                 css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::FAILURE, css::uno::Any()));
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     // OK ... now the internal loader seems to be useable for new requests
163*cdf0e10cSrcweir     // and our owner frame seems to be valid for such operations.
164*cdf0e10cSrcweir     // Initialize it with all new but needed properties and start the loading.
165*cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent > xComponent;
166*cdf0e10cSrcweir     try
167*cdf0e10cSrcweir     {
168*cdf0e10cSrcweir         m_aLoader.initializeLoading( rURL.Complete, lArguments, xBaseFrame, m_sTarget, m_nSearchFlags, (LoadEnv::EFeature)(LoadEnv::E_ALLOW_CONTENTHANDLER | LoadEnv::E_WORK_WITH_UI));
169*cdf0e10cSrcweir         m_aLoader.startLoading();
170*cdf0e10cSrcweir         m_aLoader.waitWhileLoading(); // wait for ever!
171*cdf0e10cSrcweir         xComponent = m_aLoader.getTargetComponent();
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir         // TODO thinking about asynchronous operations and listener support
174*cdf0e10cSrcweir     }
175*cdf0e10cSrcweir     catch(const LoadEnvException&)
176*cdf0e10cSrcweir         { xComponent.clear(); }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir     if (xListener.is())
179*cdf0e10cSrcweir     {
180*cdf0e10cSrcweir         if (xComponent.is())
181*cdf0e10cSrcweir             xListener->dispatchFinished(
182*cdf0e10cSrcweir                 css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::SUCCESS, css::uno::Any()));
183*cdf0e10cSrcweir         else
184*cdf0e10cSrcweir             xListener->dispatchFinished(
185*cdf0e10cSrcweir                 css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::FAILURE, css::uno::Any()));
186*cdf0e10cSrcweir     }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir     // return the model - like loadComponentFromURL()
189*cdf0e10cSrcweir     css::uno::Any aRet;
190*cdf0e10cSrcweir     if ( xComponent.is () )
191*cdf0e10cSrcweir         aRet = css::uno::makeAny( xComponent );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir     aReadLock.unlock();
194*cdf0e10cSrcweir     // <- SAFE ----------------------------------
195*cdf0e10cSrcweir     return aRet;
196*cdf0e10cSrcweir }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir } // namespace framework
199