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 -----------------------------------------------*/
LoadDispatcher(const css::uno::Reference<css::lang::XMultiServiceFactory> & xSMGR,const css::uno::Reference<css::frame::XFrame> & xOwnerFrame,const::rtl::OUString sTargetName,sal_Int32 nSearchFlags)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 -----------------------------------------------*/
~LoadDispatcher()69 LoadDispatcher::~LoadDispatcher()
70 {
71 m_xSMGR.clear();
72 }
73
74 /*-----------------------------------------------
75 20.08.2003 09:58
76 -----------------------------------------------*/
dispatchWithNotification(const css::util::URL & aURL,const css::uno::Sequence<css::beans::PropertyValue> & lArguments,const css::uno::Reference<css::frame::XDispatchResultListener> & xListener)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 {
81 impl_dispatch( aURL, lArguments, xListener );
82 }
83
84 /*-----------------------------------------------
85 20.08.2003 09:16
86 -----------------------------------------------*/
dispatch(const css::util::URL & aURL,const css::uno::Sequence<css::beans::PropertyValue> & lArguments)87 void SAL_CALL LoadDispatcher::dispatch(const css::util::URL& aURL ,
88 const css::uno::Sequence< css::beans::PropertyValue >& lArguments)
89 {
90 impl_dispatch( aURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >() );
91 }
92
93 /*-----------------------------------------------
94 14.04.2008
95 -----------------------------------------------*/
dispatchWithReturnValue(const css::util::URL & rURL,const css::uno::Sequence<css::beans::PropertyValue> & lArguments)96 css::uno::Any SAL_CALL LoadDispatcher::dispatchWithReturnValue( const css::util::URL& rURL,
97 const css::uno::Sequence< css::beans::PropertyValue >& lArguments )
98 {
99 return impl_dispatch( rURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >());
100 }
101
102 /*-----------------------------------------------
103 20.08.2003 10:48
104 -----------------------------------------------*/
addStatusListener(const css::uno::Reference<css::frame::XStatusListener> &,const css::util::URL &)105 void SAL_CALL LoadDispatcher::addStatusListener(const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/,
106 const css::util::URL& /*aURL*/ )
107 {
108 }
109
110 /*-----------------------------------------------
111 20.08.2003 10:49
112 -----------------------------------------------*/
removeStatusListener(const css::uno::Reference<css::frame::XStatusListener> &,const css::util::URL &)113 void SAL_CALL LoadDispatcher::removeStatusListener(const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/,
114 const css::util::URL& /*aURL*/ )
115 {
116 }
117
118 /*-----------------------------------------------
119 20.08.2003 09:58
120 -----------------------------------------------*/
impl_dispatch(const css::util::URL & rURL,const css::uno::Sequence<css::beans::PropertyValue> & lArguments,const css::uno::Reference<css::frame::XDispatchResultListener> & xListener)121 css::uno::Any LoadDispatcher::impl_dispatch( const css::util::URL& rURL,
122 const css::uno::Sequence< css::beans::PropertyValue >& lArguments,
123 const css::uno::Reference< css::frame::XDispatchResultListener >& xListener )
124 {
125 // Attention: May be nobody outside hold such temp. dispatch object alive (because
126 // the container in which we resists isn't implemented threadsafe but updated by a timer
127 // and clear our reference ...) we should hold us self alive!
128 css::uno::Reference< css::uno::XInterface > xThis(static_cast< css::frame::XNotifyingDispatch* >(this), css::uno::UNO_QUERY);
129
130 // SAFE -> ----------------------------------
131 ReadGuard aReadLock(m_aLock);
132
133 // We are the only client of this load env object ... but
134 // maybe a dispatch request before is still in progress (?!).
135 // Then we should wait a little bit and block this new request.
136 // In case we run into the timeout, we should reject this new request
137 // and return "FAILED" as result. Otherwise we can start this new operation.
138 if (!m_aLoader.waitWhileLoading(2000)) // => 2 sec.
139 {
140 if (xListener.is())
141 xListener->dispatchFinished(
142 css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::DONTKNOW, css::uno::Any())); // DONTKNOW? ... not really started ... not really failed :-)
143 }
144
145 css::uno::Reference< css::frame::XFrame > xBaseFrame(m_xOwnerFrame.get(), css::uno::UNO_QUERY);
146 if (!xBaseFrame.is())
147 {
148 if (xListener.is())
149 xListener->dispatchFinished(
150 css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::FAILURE, css::uno::Any()));
151 }
152
153 // OK ... now the internal loader seems to be useable for new requests
154 // and our owner frame seems to be valid for such operations.
155 // Initialize it with all new but needed properties and start the loading.
156 css::uno::Reference< css::lang::XComponent > xComponent;
157 try
158 {
159 m_aLoader.initializeLoading( rURL.Complete, lArguments, xBaseFrame, m_sTarget, m_nSearchFlags, (LoadEnv::EFeature)(LoadEnv::E_ALLOW_CONTENTHANDLER | LoadEnv::E_WORK_WITH_UI));
160 m_aLoader.startLoading();
161 m_aLoader.waitWhileLoading(); // wait for ever!
162 xComponent = m_aLoader.getTargetComponent();
163
164 // TODO thinking about asynchronous operations and listener support
165 }
166 catch(const LoadEnvException&)
167 { xComponent.clear(); }
168
169 if (xListener.is())
170 {
171 if (xComponent.is())
172 xListener->dispatchFinished(
173 css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::SUCCESS, css::uno::Any()));
174 else
175 xListener->dispatchFinished(
176 css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::FAILURE, css::uno::Any()));
177 }
178
179 // return the model - like loadComponentFromURL()
180 css::uno::Any aRet;
181 if ( xComponent.is () )
182 aRet = css::uno::makeAny( xComponent );
183
184 aReadLock.unlock();
185 // <- SAFE ----------------------------------
186 return aRet;
187 }
188
189 } // namespace framework
190