/src/libreoffice/framework/source/dispatch/loaddispatcher.cxx
Line | Count | Source |
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 | | #include <loadenv/loadenvexception.hxx> |
22 | | #include <sal/log.hxx> |
23 | | |
24 | | #include <com/sun/star/frame/DispatchResultState.hpp> |
25 | | #include <utility> |
26 | | |
27 | | namespace framework{ |
28 | | |
29 | | LoadDispatcher::LoadDispatcher(const css::uno::Reference< css::uno::XComponentContext >& xContext , |
30 | | const css::uno::Reference< css::frame::XFrame >& xOwnerFrame , |
31 | | OUString sTargetName , |
32 | | sal_Int32 nSearchFlags) |
33 | 0 | : m_xOwnerFrame (xOwnerFrame ) |
34 | 0 | , m_sTarget (std::move(sTargetName )) |
35 | 0 | , m_nSearchFlags(nSearchFlags) |
36 | 0 | , m_aLoader (xContext ) |
37 | 0 | { |
38 | 0 | } |
39 | | |
40 | | LoadDispatcher::~LoadDispatcher() |
41 | 0 | { |
42 | 0 | } |
43 | | |
44 | | void SAL_CALL LoadDispatcher::dispatchWithNotification(const css::util::URL& aURL , |
45 | | const css::uno::Sequence< css::beans::PropertyValue >& lArguments, |
46 | | const css::uno::Reference< css::frame::XDispatchResultListener >& xListener ) |
47 | 0 | { |
48 | 0 | impl_dispatch( aURL, lArguments, xListener ); |
49 | 0 | } |
50 | | |
51 | | void SAL_CALL LoadDispatcher::dispatch(const css::util::URL& aURL , |
52 | | const css::uno::Sequence< css::beans::PropertyValue >& lArguments) |
53 | 0 | { |
54 | 0 | impl_dispatch( aURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >() ); |
55 | 0 | } |
56 | | |
57 | | css::uno::Any SAL_CALL LoadDispatcher::dispatchWithReturnValue( const css::util::URL& rURL, |
58 | | const css::uno::Sequence< css::beans::PropertyValue >& lArguments ) |
59 | 0 | { |
60 | 0 | return impl_dispatch( rURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >()); |
61 | 0 | } |
62 | | |
63 | | void SAL_CALL LoadDispatcher::addStatusListener(const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/, |
64 | | const css::util::URL& /*aURL*/ ) |
65 | 0 | { |
66 | 0 | } |
67 | | |
68 | | void SAL_CALL LoadDispatcher::removeStatusListener(const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/, |
69 | | const css::util::URL& /*aURL*/ ) |
70 | 0 | { |
71 | 0 | } |
72 | | |
73 | | css::uno::Any LoadDispatcher::impl_dispatch( const css::util::URL& rURL, |
74 | | const css::uno::Sequence< css::beans::PropertyValue >& lArguments, |
75 | | const css::uno::Reference< css::frame::XDispatchResultListener >& xListener ) |
76 | 0 | { |
77 | | // Attention: May be nobody outside hold such temp. dispatch object alive (because |
78 | | // the container in which we resist isn't implemented threadsafe but updated by a timer |
79 | | // and clear our reference...) we should hold us self alive! |
80 | 0 | css::uno::Reference< css::uno::XInterface > xThis(static_cast< css::frame::XNotifyingDispatch* >(this), css::uno::UNO_QUERY); |
81 | |
|
82 | 0 | osl::MutexGuard g(m_mutex); |
83 | | |
84 | | // We are the only client of this load env object... but |
85 | | // may a dispatch request before is still in progress (?!). |
86 | | // Then we should wait a little bit and block this new request. |
87 | | // In case we run into the timeout, we should reject this new request |
88 | | // and return "FAILED" as result. Otherwise we can start this new operation. |
89 | 0 | if (!m_aLoader.waitWhileLoading(2000)) // => 2 sec. |
90 | 0 | { |
91 | 0 | if (xListener.is()) |
92 | 0 | xListener->dispatchFinished( |
93 | 0 | css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::DONTKNOW, css::uno::Any())); // DONTKNOW? ... not really started ... not really failed :-) |
94 | 0 | } |
95 | |
|
96 | 0 | css::uno::Reference< css::frame::XFrame > xBaseFrame(m_xOwnerFrame.get(), css::uno::UNO_QUERY); |
97 | 0 | if (!xBaseFrame.is() && xListener.is()) |
98 | 0 | xListener->dispatchFinished( |
99 | 0 | css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::FAILURE, css::uno::Any())); |
100 | | |
101 | | // OK ... now the internal loader seems to be usable for new requests |
102 | | // and our owner frame seems to be valid for such operations. |
103 | | // Initialize it with all new but needed properties and start the loading. |
104 | 0 | css::uno::Reference< css::lang::XComponent > xComponent; |
105 | 0 | try |
106 | 0 | { |
107 | 0 | m_aLoader.startLoading( rURL.Complete, lArguments, xBaseFrame, m_sTarget, m_nSearchFlags, LoadEnvFeatures::AllowContentHandler | LoadEnvFeatures::WorkWithUI); |
108 | 0 | m_aLoader.waitWhileLoading(); // wait for ever! |
109 | 0 | xComponent = m_aLoader.getTargetComponent(); |
110 | | |
111 | | // TODO thinking about asynchronous operations and listener support |
112 | 0 | } |
113 | 0 | catch(const LoadEnvException& e) |
114 | 0 | { |
115 | 0 | SAL_WARN( |
116 | 0 | "fwk.dispatch", |
117 | 0 | "caught LoadEnvException " << +e.m_nID << " \"" << e.m_sMessage |
118 | 0 | << "\"" |
119 | 0 | << (e.m_exOriginal.has<css::uno::Exception>() |
120 | 0 | ? (", " + e.m_exOriginal.getValueTypeName() + " \"" |
121 | 0 | + e.m_exOriginal.get<css::uno::Exception>().Message |
122 | 0 | + "\"") |
123 | 0 | : OUString()) |
124 | 0 | << " while dispatching <" << rURL.Complete << ">"); |
125 | 0 | xComponent.clear(); |
126 | 0 | } |
127 | | |
128 | 0 | if (xListener.is()) |
129 | 0 | { |
130 | 0 | if (xComponent.is()) |
131 | 0 | xListener->dispatchFinished( |
132 | 0 | css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::SUCCESS, css::uno::Any())); |
133 | 0 | else |
134 | 0 | xListener->dispatchFinished( |
135 | 0 | css::frame::DispatchResultEvent(xThis, css::frame::DispatchResultState::FAILURE, css::uno::Any())); |
136 | 0 | } |
137 | | |
138 | | // return the model - like loadComponentFromURL() |
139 | 0 | css::uno::Any aRet; |
140 | 0 | if ( xComponent.is () ) |
141 | 0 | aRet <<= xComponent; |
142 | |
|
143 | 0 | return aRet; |
144 | 0 | } |
145 | | |
146 | | } // namespace framework |
147 | | |
148 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |