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>
21 #include <threadhelp/readguard.hxx>
22 #include <threadhelp/writeguard.hxx>
24 #include <com/sun/star/frame/DispatchResultState.hpp>
28 LoadDispatcher::LoadDispatcher(const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
,
29 const css::uno::Reference
< css::frame::XFrame
>& xOwnerFrame
,
30 const OUString sTargetName
,
31 sal_Int32 nSearchFlags
)
34 , m_xOwnerFrame (xOwnerFrame
)
35 , m_sTarget (sTargetName
)
36 , m_nSearchFlags(nSearchFlags
)
41 LoadDispatcher::~LoadDispatcher()
46 void SAL_CALL
LoadDispatcher::dispatchWithNotification(const css::util::URL
& aURL
,
47 const css::uno::Sequence
< css::beans::PropertyValue
>& lArguments
,
48 const css::uno::Reference
< css::frame::XDispatchResultListener
>& xListener
)
49 throw(css::uno::RuntimeException
)
51 impl_dispatch( aURL
, lArguments
, xListener
);
54 void SAL_CALL
LoadDispatcher::dispatch(const css::util::URL
& aURL
,
55 const css::uno::Sequence
< css::beans::PropertyValue
>& lArguments
)
56 throw(css::uno::RuntimeException
)
58 impl_dispatch( aURL
, lArguments
, css::uno::Reference
< css::frame::XDispatchResultListener
>() );
61 css::uno::Any SAL_CALL
LoadDispatcher::dispatchWithReturnValue( const css::util::URL
& rURL
,
62 const css::uno::Sequence
< css::beans::PropertyValue
>& lArguments
)
63 throw( css::uno::RuntimeException
)
65 return impl_dispatch( rURL
, lArguments
, css::uno::Reference
< css::frame::XDispatchResultListener
>());
68 void SAL_CALL
LoadDispatcher::addStatusListener(const css::uno::Reference
< css::frame::XStatusListener
>& /*xListener*/,
69 const css::util::URL
& /*aURL*/ )
70 throw(css::uno::RuntimeException
)
74 void SAL_CALL
LoadDispatcher::removeStatusListener(const css::uno::Reference
< css::frame::XStatusListener
>& /*xListener*/,
75 const css::util::URL
& /*aURL*/ )
76 throw(css::uno::RuntimeException
)
80 css::uno::Any
LoadDispatcher::impl_dispatch( const css::util::URL
& rURL
,
81 const css::uno::Sequence
< css::beans::PropertyValue
>& lArguments
,
82 const css::uno::Reference
< css::frame::XDispatchResultListener
>& xListener
)
84 // Attention: May be nobody outside hold such temp. dispatch object alive (because
85 // the container in which we resists isnt implemented threadsafe but updated by a timer
86 // and clear our reference ...) we should hold us self alive!
87 css::uno::Reference
< css::uno::XInterface
> xThis(static_cast< css::frame::XNotifyingDispatch
* >(this), css::uno::UNO_QUERY
);
89 // SAFE -> ----------------------------------
90 ReadGuard
aReadLock(m_aLock
);
92 // We are the only client of this load env object ... but
93 // may a dispatch request before is still in progress (?!).
94 // Then we should wait a little bit and block this new request.
95 // In case we run into the timeout, we should reject this new request
96 // and return "FAILED" as result. Otherwhise we can start this new operation.
97 if (!m_aLoader
.waitWhileLoading(2000)) // => 2 sec.
100 xListener
->dispatchFinished(
101 css::frame::DispatchResultEvent(xThis
, css::frame::DispatchResultState::DONTKNOW
, css::uno::Any())); // DONTKNOW? ... not realy started ... not realy failed :-)
104 css::uno::Reference
< css::frame::XFrame
> xBaseFrame(m_xOwnerFrame
.get(), css::uno::UNO_QUERY
);
105 if (!xBaseFrame
.is())
108 xListener
->dispatchFinished(
109 css::frame::DispatchResultEvent(xThis
, css::frame::DispatchResultState::FAILURE
, css::uno::Any()));
112 // OK ... now the internal loader seems to be useable for new requests
113 // and our owner frame seems to be valid for such operations.
114 // Initialize it with all new but needed properties and start the loading.
115 css::uno::Reference
< css::lang::XComponent
> xComponent
;
118 m_aLoader
.initializeLoading( rURL
.Complete
, lArguments
, xBaseFrame
, m_sTarget
, m_nSearchFlags
, (LoadEnv::EFeature
)(LoadEnv::E_ALLOW_CONTENTHANDLER
| LoadEnv::E_WORK_WITH_UI
));
119 m_aLoader
.startLoading();
120 m_aLoader
.waitWhileLoading(); // wait for ever!
121 xComponent
= m_aLoader
.getTargetComponent();
123 // TODO thinking about asynchronous operations and listener support
125 catch(const LoadEnvException
&)
126 { xComponent
.clear(); }
131 xListener
->dispatchFinished(
132 css::frame::DispatchResultEvent(xThis
, css::frame::DispatchResultState::SUCCESS
, css::uno::Any()));
134 xListener
->dispatchFinished(
135 css::frame::DispatchResultEvent(xThis
, css::frame::DispatchResultState::FAILURE
, css::uno::Any()));
138 // return the model - like loadComponentFromURL()
140 if ( xComponent
.is () )
141 aRet
= css::uno::makeAny( xComponent
);
144 // <- SAFE ----------------------------------
148 } // namespace framework
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */