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