Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <dispatch/loaddispatcher.hxx>
21 :
22 : #include <com/sun/star/frame/DispatchResultState.hpp>
23 :
24 : namespace framework{
25 :
26 2 : LoadDispatcher::LoadDispatcher(const css::uno::Reference< css::uno::XComponentContext >& xContext ,
27 : const css::uno::Reference< css::frame::XFrame >& xOwnerFrame ,
28 : const OUString& sTargetName ,
29 : sal_Int32 nSearchFlags)
30 : : m_xContext (xContext )
31 : , m_xOwnerFrame (xOwnerFrame )
32 : , m_sTarget (sTargetName )
33 : , m_nSearchFlags(nSearchFlags)
34 2 : , m_aLoader (xContext )
35 : {
36 2 : }
37 :
38 6 : LoadDispatcher::~LoadDispatcher()
39 : {
40 2 : m_xContext.clear();
41 4 : }
42 :
43 0 : void SAL_CALL LoadDispatcher::dispatchWithNotification(const css::util::URL& aURL ,
44 : const css::uno::Sequence< css::beans::PropertyValue >& lArguments,
45 : const css::uno::Reference< css::frame::XDispatchResultListener >& xListener )
46 : throw(css::uno::RuntimeException, std::exception)
47 : {
48 0 : impl_dispatch( aURL, lArguments, xListener );
49 0 : }
50 :
51 2 : void SAL_CALL LoadDispatcher::dispatch(const css::util::URL& aURL ,
52 : const css::uno::Sequence< css::beans::PropertyValue >& lArguments)
53 : throw(css::uno::RuntimeException, std::exception)
54 : {
55 2 : impl_dispatch( aURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >() );
56 2 : }
57 :
58 0 : css::uno::Any SAL_CALL LoadDispatcher::dispatchWithReturnValue( const css::util::URL& rURL,
59 : const css::uno::Sequence< css::beans::PropertyValue >& lArguments )
60 : throw( css::uno::RuntimeException, std::exception )
61 : {
62 0 : return impl_dispatch( rURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >());
63 : }
64 :
65 0 : void SAL_CALL LoadDispatcher::addStatusListener(const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/,
66 : const css::util::URL& /*aURL*/ )
67 : throw(css::uno::RuntimeException, std::exception)
68 : {
69 0 : }
70 :
71 0 : void SAL_CALL LoadDispatcher::removeStatusListener(const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/,
72 : const css::util::URL& /*aURL*/ )
73 : throw(css::uno::RuntimeException, std::exception)
74 : {
75 0 : }
76 :
77 2 : css::uno::Any LoadDispatcher::impl_dispatch( const css::util::URL& rURL,
78 : const css::uno::Sequence< css::beans::PropertyValue >& lArguments,
79 : const css::uno::Reference< css::frame::XDispatchResultListener >& xListener )
80 : {
81 : // Attention: May be nobody outside hold such temp. dispatch object alive (because
82 : // the container in which we resists isn't implemented threadsafe but updated by a timer
83 : // and clear our reference ...) we should hold us self alive!
84 2 : css::uno::Reference< css::uno::XInterface > xThis(static_cast< css::frame::XNotifyingDispatch* >(this), css::uno::UNO_QUERY);
85 :
86 4 : osl::MutexGuard g(m_mutex);
87 :
88 : // We are the only client of this load env object ... but
89 : // may a dispatch request before is still in progress (?!).
90 : // Then we should wait a little bit and block this new request.
91 : // In case we run into the timeout, we should reject this new request
92 : // and return "FAILED" as result. Otherwhise we can start this new operation.
93 2 : if (!m_aLoader.waitWhileLoading(2000)) // => 2 sec.
94 : {
95 0 : if (xListener.is())
96 0 : xListener->dispatchFinished(
97 0 : css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::DONTKNOW, css::uno::Any())); // DONTKNOW? ... not really started ... not really failed :-)
98 : }
99 :
100 4 : css::uno::Reference< css::frame::XFrame > xBaseFrame(m_xOwnerFrame.get(), css::uno::UNO_QUERY);
101 2 : if (!xBaseFrame.is())
102 : {
103 0 : if (xListener.is())
104 0 : xListener->dispatchFinished(
105 0 : css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::FAILURE, css::uno::Any()));
106 : }
107 :
108 : // OK ... now the internal loader seems to be useable for new requests
109 : // and our owner frame seems to be valid for such operations.
110 : // Initialize it with all new but needed properties and start the loading.
111 4 : css::uno::Reference< css::lang::XComponent > xComponent;
112 : try
113 : {
114 2 : m_aLoader.initializeLoading( rURL.Complete, lArguments, xBaseFrame, m_sTarget, m_nSearchFlags, (LoadEnv::EFeature)(LoadEnv::E_ALLOW_CONTENTHANDLER | LoadEnv::E_WORK_WITH_UI));
115 2 : m_aLoader.startLoading();
116 2 : m_aLoader.waitWhileLoading(); // wait for ever!
117 2 : xComponent = m_aLoader.getTargetComponent();
118 :
119 : // TODO thinking about asynchronous operations and listener support
120 : }
121 0 : catch(const LoadEnvException& e)
122 : {
123 : SAL_WARN(
124 : "fwk.dispatch",
125 : "caught LoadEnvException " << +e.m_nID << " \"" << e.m_sMessage
126 : << "\""
127 : << (e.m_exOriginal.has<css::uno::Exception>()
128 : ? (", " + e.m_exOriginal.getValueTypeName() + " \""
129 : + e.m_exOriginal.get<css::uno::Exception>().Message
130 : + "\"")
131 : : OUString())
132 : << " while dispatching <" << rURL.Complete << ">");
133 0 : xComponent.clear();
134 : }
135 :
136 2 : if (xListener.is())
137 : {
138 0 : if (xComponent.is())
139 0 : xListener->dispatchFinished(
140 0 : css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::SUCCESS, css::uno::Any()));
141 : else
142 0 : xListener->dispatchFinished(
143 0 : css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::FAILURE, css::uno::Any()));
144 : }
145 :
146 : // return the model - like loadComponentFromURL()
147 2 : css::uno::Any aRet;
148 2 : if ( xComponent.is () )
149 2 : aRet = css::uno::makeAny( xComponent );
150 :
151 4 : return aRet;
152 : }
153 :
154 : } // namespace framework
155 :
156 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|