23#include <com/sun/star/frame/Desktop.hpp>
24#include <com/sun/star/frame/TerminationVetoException.hpp>
25#include <com/sun/star/task/XJob.hpp>
26#include <com/sun/star/task/XAsyncJob.hpp>
27#include <com/sun/star/util/CloseVetoException.hpp>
28#include <com/sun/star/util/XCloseBroadcaster.hpp>
29#include <com/sun/star/util/XCloseable.hpp>
30#include <com/sun/star/lang/DisposedException.hpp>
53Job::Job(
const css::uno::Reference< css::uno::XComponentContext >& xContext ,
54 css::uno::Reference< css::frame::XFrame > xFrame )
55 : m_aJobCfg (xContext )
58 , m_bListenOnDesktop (false )
59 , m_bListenOnFrame (false )
60 , m_bListenOnModel (false )
61 , m_bPendingCloseFrame (false )
62 , m_bPendingCloseModel (false )
63 , m_eRunState (E_NEW )
80Job::Job(
const css::uno::Reference< css::uno::XComponentContext >& xContext ,
81 css::uno::Reference< css::frame::XModel > xModel )
82 : m_aJobCfg (xContext )
85 , m_bListenOnDesktop (false )
86 , m_bListenOnFrame (false )
87 , m_bListenOnModel (false )
88 , m_bPendingCloseFrame (false )
89 , m_bPendingCloseModel (false )
90 , m_eRunState (E_NEW )
116void Job::setDispatchResultFake(
const css::uno::Reference< css::frame::XDispatchResultListener >& xListener ,
117 const css::uno::Reference< css::uno::XInterface >& xSourceFake )
122 if (m_eRunState != E_NEW)
124 SAL_INFO(
"fwk",
"Job::setJobData(): job may still running or already finished");
128 m_xResultListener = xListener;
129 m_xResultSourceFake = xSourceFake;
132void Job::setJobData(
const JobData& aData )
137 if (m_eRunState != E_NEW)
139 SAL_INFO(
"fwk",
"Job::setJobData(): job may still running or already finished");
157void Job::execute(
const css::uno::Sequence< css::beans::NamedValue >& lDynamicArgs )
160 class SolarMutexAntiGuard {
167 ~SolarMutexAntiGuard()
175 if (m_eRunState != E_NEW)
177 SAL_INFO(
"fwk",
"Job::execute(): job may still running or already finished");
182 m_eRunState = E_RUNNING;
183 impl_startListening();
185 css::uno::Reference< css::task::XAsyncJob > xAJob;
186 css::uno::Reference< css::task::XJob > xSJob;
187 css::uno::Sequence< css::beans::NamedValue > lJobArgs = impl_generateJobArgs(lDynamicArgs);
191 css::uno::Reference< css::task::XJobListener > xThis(
this);
198 m_xJob =
m_xContext->getServiceManager()->createInstanceWithContext(m_aJobCfg.getService(),
m_xContext);
199 xSJob.set(m_xJob, css::uno::UNO_QUERY);
201 xAJob.set(m_xJob, css::uno::UNO_QUERY);
206 m_aAsyncWait.reset();
207 SolarMutexAntiGuard
const ag(aWriteLock);
209 xAJob->executeAsync(lJobArgs, xThis);
219 css::uno::Any aResult;
221 SolarMutexAntiGuard
const ag(aWriteLock);
223 aResult = xSJob->execute(lJobArgs);
226 impl_reactForJobResult(aResult);
229 #if OSL_DEBUG_LEVEL > 0
230 catch(
const css::uno::Exception&)
235 catch(
const css::uno::Exception&)
241 impl_stopListening();
242 if (m_eRunState == E_RUNNING)
243 m_eRunState = E_STOPPED_OR_FINISHED;
250 if (m_bPendingCloseFrame)
252 m_bPendingCloseFrame =
false;
253 css::uno::Reference< css::util::XCloseable > xClose(m_xFrame, css::uno::UNO_QUERY);
260 catch(
const css::util::CloseVetoException&) {}
264 if (m_bPendingCloseModel)
266 m_bPendingCloseModel =
false;
267 css::uno::Reference< css::util::XCloseable > xClose(m_xModel, css::uno::UNO_QUERY);
274 catch(
const css::util::CloseVetoException&) {}
297 impl_stopListening();
299 if (m_eRunState != E_DISPOSED)
303 css::uno::Reference< css::lang::XComponent > xDispose(m_xJob, css::uno::UNO_QUERY);
307 m_eRunState = E_DISPOSED;
310 catch(
const css::lang::DisposedException&)
312 m_eRunState = E_DISPOSED;
320 m_xResultListener.clear();
321 m_xResultSourceFake.clear();
322 m_bPendingCloseFrame =
false;
323 m_bPendingCloseModel =
false;
341css::uno::Sequence< css::beans::NamedValue > Job::impl_generateJobArgs(
const css::uno::Sequence< css::beans::NamedValue >& lDynamicArgs )
343 css::uno::Sequence< css::beans::NamedValue > lAllArgs;
349 JobData::EMode
eMode = m_aJobCfg.getMode();
358 if (eMode==JobData::E_EVENT)
360 css::uno::Sequence< css::beans::NamedValue > lEnvArgs(nLen);
361 auto plEnvArgs = lEnvArgs.getArray();
362 plEnvArgs[0].Name =
"EnvType";
363 plEnvArgs[0].Value <<= m_aJobCfg.getEnvironmentDescriptor();
369 plEnvArgs[
i].Name =
"Frame";
375 plEnvArgs[
i].Name =
"Model";
378 if (eMode==JobData::E_EVENT)
381 plEnvArgs[
i].Name =
"EventName";
382 plEnvArgs[
i].Value <<= m_aJobCfg.getEvent();
388 css::uno::Sequence< css::beans::NamedValue > lConfigArgs;
389 std::vector< css::beans::NamedValue > lJobConfigArgs;
390 if (eMode==JobData::E_ALIAS || eMode==JobData::E_EVENT)
392 lConfigArgs = m_aJobCfg.getConfig();
393 lJobConfigArgs = m_aJobCfg.getJobConfig();
400 if (lConfigArgs.hasElements())
402 sal_Int32
nLength = lAllArgs.getLength();
403 lAllArgs.realloc(nLength+1);
404 auto plAllArgs = lAllArgs.getArray();
405 plAllArgs[
nLength].Name =
"Config";
406 plAllArgs[
nLength].Value <<= lConfigArgs;
408 if (!lJobConfigArgs.empty())
410 sal_Int32
nLength = lAllArgs.getLength();
411 lAllArgs.realloc(nLength+1);
412 auto plAllArgs = lAllArgs.getArray();
413 plAllArgs[
nLength].Name =
"JobConfig";
416 if (lEnvArgs.hasElements())
418 sal_Int32
nLength = lAllArgs.getLength();
419 lAllArgs.realloc(nLength+1);
420 auto plAllArgs = lAllArgs.getArray();
421 plAllArgs[
nLength].Name =
"Environment";
422 plAllArgs[
nLength].Value <<= lEnvArgs;
424 if (lDynamicArgs.hasElements())
426 sal_Int32
nLength = lAllArgs.getLength();
427 lAllArgs.realloc(nLength+1);
428 auto plAllArgs = lAllArgs.getArray();
429 plAllArgs[
nLength].Name =
"DynamicData";
430 plAllArgs[
nLength].Value <<= lDynamicArgs;
447void Job::impl_reactForJobResult(
const css::uno::Any& aResult )
452 JobResult aAnalyzedResult(aResult);
456 JobData::EEnvironment eEnvironment = m_aJobCfg.getEnvironment();
461 (m_aJobCfg.hasConfig() ) &&
462 (aAnalyzedResult.existPart(JobResult::E_ARGUMENTS))
465 m_aJobCfg.setJobConfig(aAnalyzedResult.getArguments());
472 (m_aJobCfg.hasConfig() ) &&
473 (aAnalyzedResult.existPart(JobResult::E_DEACTIVATE))
476 m_aJobCfg.disableJob();
482 (eEnvironment == JobData::E_DISPATCH ) &&
483 (m_xResultListener.is() ) &&
484 (aAnalyzedResult.existPart(JobResult::E_DISPATCHRESULT))
490 css::frame::DispatchResultEvent
aEvent = aAnalyzedResult.getDispatchResult();
491 aEvent.Source = m_xResultSourceFake;
492 m_xResultListener->dispatchFinished(aEvent);
512void Job::impl_startListening()
522 css::uno::Reference< css::frame::XTerminateListener > xThis(
this);
524 m_bListenOnDesktop =
true;
526 catch(
const css::uno::Exception&)
533 if (
m_xFrame.is() && !m_bListenOnFrame)
537 css::uno::Reference< css::util::XCloseBroadcaster > xCloseable(m_xFrame , css::uno::UNO_QUERY);
538 css::uno::Reference< css::util::XCloseListener > xThis(
this);
541 xCloseable->addCloseListener(xThis);
542 m_bListenOnFrame =
true;
545 catch(
const css::uno::Exception&)
547 m_bListenOnFrame =
false;
552 if (!
m_xModel.is() || m_bListenOnModel)
557 css::uno::Reference< css::util::XCloseBroadcaster > xCloseable(m_xModel , css::uno::UNO_QUERY);
558 css::uno::Reference< css::util::XCloseListener > xThis(
this);
561 xCloseable->addCloseListener(xThis);
562 m_bListenOnModel =
true;
565 catch(
const css::uno::Exception&)
567 m_bListenOnModel =
false;
575void Job::impl_stopListening()
584 css::uno::Reference< css::frame::XTerminateListener > xThis(
this);
587 m_bListenOnDesktop =
false;
589 catch(
const css::uno::Exception&)
595 if (
m_xFrame.is() && m_bListenOnFrame)
599 css::uno::Reference< css::util::XCloseBroadcaster > xCloseable(m_xFrame , css::uno::UNO_QUERY);
600 css::uno::Reference< css::util::XCloseListener > xThis(
this);
603 xCloseable->removeCloseListener(xThis);
604 m_bListenOnFrame =
false;
607 catch(
const css::uno::Exception&)
613 if (!(
m_xModel.is() && m_bListenOnModel))
618 css::uno::Reference< css::util::XCloseBroadcaster > xCloseable(m_xModel , css::uno::UNO_QUERY);
619 css::uno::Reference< css::util::XCloseListener > xThis(
this);
622 xCloseable->removeCloseListener(xThis);
623 m_bListenOnModel =
false;
626 catch(
const css::uno::Exception&)
645void SAL_CALL Job::jobFinished(
const css::uno::Reference< css::task::XAsyncJob >& xJob ,
646 const css::uno::Any& aResult )
653 if (m_xJob.is() && m_xJob==xJob)
658 impl_reactForJobResult(aResult);
684void SAL_CALL Job::queryTermination(
const css::lang::EventObject& )
689 css::uno::Reference< css::util::XCloseable > xClose(m_xJob, css::uno::UNO_QUERY);
694 xClose->close(
false);
695 m_eRunState = E_STOPPED_OR_FINISHED;
697 catch(
const css::util::CloseVetoException&) {}
700 if (m_eRunState != E_STOPPED_OR_FINISHED)
702 css::uno::Reference< css::uno::XInterface > xThis(
static_cast< ::
cppu::OWeakObject*
>(
this), css::uno::UNO_QUERY);
703 throw css::frame::TerminationVetoException(
"job still in progress", xThis);
720void SAL_CALL Job::notifyTermination(
const css::lang::EventObject& )
744void SAL_CALL Job::queryClosing(
const css::lang::EventObject& aEvent ,
751 if (m_eRunState != E_RUNNING)
756 css::uno::Reference< css::util::XCloseable > xClose(m_xJob, css::uno::UNO_QUERY);
759 xClose->close(bGetsOwnership);
762 m_eRunState = E_STOPPED_OR_FINISHED;
771 css::uno::Reference< css::lang::XComponent > xDispose(m_xJob, css::uno::UNO_QUERY);
775 m_eRunState = E_DISPOSED;
778 catch(
const css::lang::DisposedException&)
782 m_eRunState = E_DISPOSED;
785 if (m_eRunState != E_DISPOSED)
794 css::uno::Reference< css::uno::XInterface > xThis(
static_cast< ::
cppu::OWeakObject*
>(
this), css::uno::UNO_QUERY);
795 throw css::util::CloseVetoException(
"job still in progress", xThis);
813void SAL_CALL Job::notifyClosing(
const css::lang::EventObject& )
827void SAL_CALL Job::disposing(
const css::lang::EventObject& aEvent )
836 m_bListenOnDesktop =
false;
841 m_bListenOnFrame =
false;
846 m_bListenOnModel =
false;
css::uno::Reference< css::lang::XComponent > m_xFrame
Job(const css::uno::Reference< css::uno::XComponentContext > &xContext, css::uno::Reference< css::frame::XFrame > xFrame)
Reference< XDesktop2 > m_xDesktop
#define TOOLS_INFO_EXCEPTION(area, stream)
Reference< frame::XModel > m_xModel
css::uno::Reference< css::uno::XComponentContext > m_xContext
#define SAL_INFO(area, stream)
constexpr OUStringLiteral aData
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Reference< XFrame > xFrame
Reference< XModel > xModel