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