1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <dispatch/loaddispatcher.hxx>
22 #include <com/sun/star/frame/DispatchResultState.hpp>
26 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 , m_aLoader (xContext
)
38 LoadDispatcher::~LoadDispatcher()
43 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
)
48 impl_dispatch( aURL
, lArguments
, xListener
);
51 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
)
55 impl_dispatch( aURL
, lArguments
, css::uno::Reference
< css::frame::XDispatchResultListener
>() );
58 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
)
62 return impl_dispatch( rURL
, lArguments
, css::uno::Reference
< css::frame::XDispatchResultListener
>());
65 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
)
71 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
)
77 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
)
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 css::uno::Reference
< css::uno::XInterface
> xThis(static_cast< css::frame::XNotifyingDispatch
* >(this), css::uno::UNO_QUERY
);
86 osl::MutexGuard
g(m_mutex
);
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 if (!m_aLoader
.waitWhileLoading(2000)) // => 2 sec.
96 xListener
->dispatchFinished(
97 css::frame::DispatchResultEvent(xThis
, css::frame::DispatchResultState::DONTKNOW
, css::uno::Any())); // DONTKNOW? ... not really started ... not really failed :-)
100 css::uno::Reference
< css::frame::XFrame
> xBaseFrame(m_xOwnerFrame
.get(), css::uno::UNO_QUERY
);
101 if (!xBaseFrame
.is())
104 xListener
->dispatchFinished(
105 css::frame::DispatchResultEvent(xThis
, css::frame::DispatchResultState::FAILURE
, css::uno::Any()));
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 css::uno::Reference
< css::lang::XComponent
> xComponent
;
114 m_aLoader
.initializeLoading( rURL
.Complete
, lArguments
, xBaseFrame
, m_sTarget
, m_nSearchFlags
, (LoadEnv::EFeature
)(LoadEnv::E_ALLOW_CONTENTHANDLER
| LoadEnv::E_WORK_WITH_UI
));
115 m_aLoader
.startLoading();
116 m_aLoader
.waitWhileLoading(); // wait for ever!
117 xComponent
= m_aLoader
.getTargetComponent();
119 // TODO thinking about asynchronous operations and listener support
121 catch(const LoadEnvException
& e
)
125 "caught LoadEnvException " << +e
.m_nID
<< " \"" << e
.m_sMessage
127 << (e
.m_exOriginal
.has
<css::uno::Exception
>()
128 ? (", " + e
.m_exOriginal
.getValueTypeName() + " \""
129 + e
.m_exOriginal
.get
<css::uno::Exception
>().Message
132 << " while dispatching <" << rURL
.Complete
<< ">");
139 xListener
->dispatchFinished(
140 css::frame::DispatchResultEvent(xThis
, css::frame::DispatchResultState::SUCCESS
, css::uno::Any()));
142 xListener
->dispatchFinished(
143 css::frame::DispatchResultEvent(xThis
, css::frame::DispatchResultState::FAILURE
, css::uno::Any()));
146 // return the model - like loadComponentFromURL()
148 if ( xComponent
.is () )
149 aRet
= css::uno::makeAny( xComponent
);
154 } // namespace framework
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */