26#include <com/sun/star/container/XNameAccess.hpp>
27#include <com/sun/star/container/XContainer.hpp>
28#include <com/sun/star/frame/ModuleManager.hpp>
29#include <com/sun/star/task/XJobExecutor.hpp>
30#include <com/sun/star/container/XContainerListener.hpp>
31#include <com/sun/star/lang/XServiceInfo.hpp>
32#include <com/sun/star/document/XEventListener.hpp>
47 css::lang::XServiceInfo
48 , css::task::XJobExecutor
49 , css::container::XContainerListener
50 , css::document::XEventListener >
59class JobExecutor :
public Base
64 css::uno::Reference< css::uno::XComponentContext >
m_xContext;
67 std::vector<OUString> m_lEvents;
75 virtual void disposing(std::unique_lock<std::mutex>& rGuard)
final override;
79 explicit JobExecutor(
const css::uno::Reference< css::uno::XComponentContext >& xContext);
80 virtual ~JobExecutor()
override;
84 return "com.sun.star.comp.framework.JobExecutor";
94 return {
"com.sun.star.task.JobExecutor"};
98 virtual void SAL_CALL trigger(
const OUString& sEvent )
override;
101 void initListeners();
104 virtual void SAL_CALL notifyEvent(
const css::document::EventObject& aEvent )
override;
107 virtual void SAL_CALL
elementInserted(
const css::container::ContainerEvent& aEvent )
override;
108 virtual void SAL_CALL
elementRemoved (
const css::container::ContainerEvent& aEvent )
override;
109 virtual void SAL_CALL
elementReplaced(
const css::container::ContainerEvent& aEvent )
override;
112 virtual void SAL_CALL disposing(
const css::lang::EventObject& aEvent )
override;
122JobExecutor::JobExecutor(
const css::uno::Reference< css::uno::XComponentContext >& xContext )
124 , m_aConfig (xContext,
"/org.openoffice.Office.Jobs/Events")
128void JobExecutor::initListeners()
139 m_aConfig.open(ConfigAccess::E_READONLY);
140 if (m_aConfig.getMode() != ConfigAccess::E_READONLY)
143 css::uno::Reference< css::container::XNameAccess > xRegistry(
144 m_aConfig.cfg(), css::uno::UNO_QUERY);
146 m_lEvents = Converter::convert_seqOUString2OUStringList(
147 xRegistry->getElementNames());
149 css::uno::Reference< css::container::XContainer > xNotifier(
150 m_aConfig.cfg(), css::uno::UNO_QUERY);
161JobExecutor::~JobExecutor()
167void JobExecutor::disposing(std::unique_lock<std::mutex>& ) {
168 css::uno::Reference<css::container::XContainer> notifier;
169 css::uno::Reference<css::container::XContainerListener> listener;
170 if (m_aConfig.getMode() != ConfigAccess::E_CLOSED) {
171 notifier.set(m_aConfig.cfg(), css::uno::UNO_QUERY);
177 notifier->removeContainerListener(listener);
190void SAL_CALL JobExecutor::trigger(
const OUString& sEvent )
192 SAL_INFO(
"fwk",
"JobExecutor::trigger()");
201 if (std::find(m_lEvents.begin(), m_lEvents.end(), sEvent) == m_lEvents.end())
209 std::vector< OUString > lJobs = JobData::getEnabledJobsForEvent(
m_xContext, sEvent);
212 size_t c = lJobs.size();
213 for (
size_t j=0; j<c; ++j)
216 aCfg.setEvent(sEvent, lJobs[j]);
217 aCfg.setEnvironment(JobData::E_EXECUTION);
225 pJob->setJobData(aCfg);
227 pJob->execute(css::uno::Sequence< css::beans::NamedValue >());
231void SAL_CALL JobExecutor::notifyEvent(
const css::document::EventObject& aEvent )
233 static constexpr OUStringLiteral EVENT_ON_DOCUMENT_OPENED(u
"onDocumentOpened");
234 static constexpr OUStringLiteral EVENT_ON_DOCUMENT_ADDED(u
"onDocumentAdded");
236 OUString aModuleIdentifier;
237 ::std::vector< JobData::TJob2DocEventBinding > lJobs;
247 aModuleIdentifier = css::frame::ModuleManager::create(
m_xContext )->identify(
aEvent.Source );
249 catch(
const css::uno::Exception& )
258 (
aEvent.EventName ==
"OnNew") ||
259 (
aEvent.EventName ==
"OnLoad")
262 if (std::find(m_lEvents.begin(), m_lEvents.end(), EVENT_ON_DOCUMENT_OPENED) != m_lEvents.end())
263 JobData::appendEnabledJobsForEvent(
m_xContext, EVENT_ON_DOCUMENT_OPENED, lJobs);
268 (
aEvent.EventName ==
"OnCreate") ||
269 (
aEvent.EventName ==
"OnLoadFinished")
272 if (std::find(m_lEvents.begin(), m_lEvents.end(), EVENT_ON_DOCUMENT_ADDED) != m_lEvents.end())
273 JobData::appendEnabledJobsForEvent(
m_xContext, EVENT_ON_DOCUMENT_ADDED, lJobs);
277 if (std::find(m_lEvents.begin(), m_lEvents.end(),
aEvent.EventName) != m_lEvents.end())
282 for (
auto const& lJob : lJobs)
290 aCfg.setEnvironment(JobData::E_DOCUMENTEVENT);
292 if (!aCfg.hasCorrectContext(aModuleIdentifier))
300 css::uno::Reference< css::frame::XModel >
xModel(
aEvent.Source, css::uno::UNO_QUERY);
302 pJob->setJobData(aCfg);
304 pJob->execute(css::uno::Sequence< css::beans::NamedValue >());
308void SAL_CALL JobExecutor::elementInserted(
const css::container::ContainerEvent& aEvent )
311 if (
aEvent.Accessor >>= sValue)
313 OUString sEvent = ::utl::extractFirstFromConfigurationPath(sValue);
314 if (!sEvent.isEmpty())
316 std::vector<OUString>::iterator pEvent = std::find(m_lEvents.begin(), m_lEvents.end(), sEvent);
317 if (pEvent == m_lEvents.end())
318 m_lEvents.push_back(sEvent);
323void SAL_CALL JobExecutor::elementRemoved (
const css::container::ContainerEvent& aEvent )
326 if (
aEvent.Accessor >>= sValue)
328 OUString sEvent = ::utl::extractFirstFromConfigurationPath(sValue);
329 if (!sEvent.isEmpty())
331 std::vector<OUString>::iterator pEvent = std::find(m_lEvents.begin(), m_lEvents.end(), sEvent);
332 if (pEvent != m_lEvents.end())
333 m_lEvents.erase(pEvent);
338void SAL_CALL JobExecutor::elementReplaced(
const css::container::ContainerEvent& )
357void SAL_CALL JobExecutor::disposing(
const css::lang::EventObject& aEvent )
361 css::uno::Reference< css::uno::XInterface > xCFG(m_aConfig.cfg(), css::uno::UNO_QUERY);
363 (xCFG ==
aEvent.Source ) &&
364 (m_aConfig.getMode() != ConfigAccess::E_CLOSED)
374extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
376 css::uno::XComponentContext *context,
377 css::uno::Sequence<css::uno::Any>
const &)
381 xJobExec->initListeners();
382 return cppu::acquire(xJobExec.get());
implements a simple configuration access @descr Sometimes it's better to have direct config access in...
holds all necessary information about a job and handle it's configuration (if any exist!...
it represent a job; execute it and control its lifetime
css::uno::Reference< css::uno::XComponentContext > m_xContext
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_framework_JobExecutor_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
void SAL_CALL elementReplaced(const css::container::ContainerEvent &Event) override
void SAL_CALL elementRemoved(const css::container::ContainerEvent &Event) override
DECL_LISTENERMULTIPLEXER_END void SAL_CALL elementInserted(const css::container::ContainerEvent &Event) override
#define SAL_INFO(area, stream)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Some jobs can be registered to "logical events", which are generated on demand if another document ev...
Reference< XModel > xModel
Reference< XContainerListener > m_xConfigListener