LibreOffice Module framework (master) 1
jobdata.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <string_view>
23
24#include <jobs/configaccess.hxx>
25#include <jobs/jobdata.hxx>
26#include <classes/converter.hxx>
27
28#include <com/sun/star/beans/XPropertySet.hpp>
29#include <com/sun/star/beans/XMultiHierarchicalPropertySet.hpp>
30#include <com/sun/star/container/XNameAccess.hpp>
31#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
32
33#include <tools/wldcrd.hxx>
35#include <utility>
36#include <vcl/svapp.hxx>
37
38namespace framework{
39
49JobData::JobData( css::uno::Reference< css::uno::XComponentContext > xContext )
50 : m_xContext (std::move(xContext ))
51{
52 // share code for member initialization with defaults!
53 impl_reset();
54}
55
65{
66 // use the copy operator to share the same code
67 *this = rCopy;
68}
69
79{
80 // Please don't copy the uno service manager reference.
81 // That can change the uno context, which isn't a good idea!
82 m_eMode = rCopy.m_eMode;
84 m_sAlias = rCopy.m_sAlias;
85 m_sService = rCopy.m_sService;
86 m_sContext = rCopy.m_sContext;
87 m_sEvent = rCopy.m_sEvent;
89 return *this;
90}
91
98{
99 impl_reset();
100}
101
111void JobData::setAlias( const OUString& sAlias )
112{
113 // delete all old information! Otherwise we mix it with the new one ...
114 impl_reset();
115
116 // take over the new information
117 m_sAlias = sAlias;
119
120 // try to open the configuration set of this job directly and get a property access to it
121 // We open it readonly here
122 ConfigAccess aConfig(
124 ("/org.openoffice.Office.Jobs/Jobs/"
127 if (aConfig.getMode()==ConfigAccess::E_CLOSED)
128 {
129 impl_reset();
130 return;
131 }
132
133 css::uno::Reference< css::beans::XPropertySet > xJobProperties(aConfig.cfg(), css::uno::UNO_QUERY);
134 if (xJobProperties.is())
135 {
136 css::uno::Any aValue;
137
138 // read uno implementation name
139 aValue = xJobProperties->getPropertyValue("Service");
140 aValue >>= m_sService;
141
142 // read module context list
143 aValue = xJobProperties->getPropertyValue("Context");
144 aValue >>= m_sContext;
145
146 // read whole argument list
147 aValue = xJobProperties->getPropertyValue("Arguments");
148 css::uno::Reference< css::container::XNameAccess > xArgumentList;
149 if (
150 (aValue >>= xArgumentList) &&
151 (xArgumentList.is() )
152 )
153 {
154 css::uno::Sequence< OUString > lArgumentNames = xArgumentList->getElementNames();
155 sal_Int32 nCount = lArgumentNames.getLength();
156 m_lArguments.resize(nCount);
157 for (sal_Int32 i=0; i<nCount; ++i)
158 {
159 m_lArguments[i].Name = lArgumentNames[i];
160 m_lArguments[i].Value = xArgumentList->getByName(m_lArguments[i].Name);
161 }
162 }
163 }
164
165 aConfig.close();
166}
167
176void JobData::setService( const OUString& sService )
177{
178 // delete all old information! Otherwise we mix it with the new one ...
179 impl_reset();
180 // take over the new information
181 m_sService = sService;
183}
184
203void JobData::setEvent( const OUString& sEvent ,
204 const OUString& sAlias )
205{
206 // share code to read all job properties!
207 setAlias(sAlias);
208
209 // take over the new information - which differ against set one of method setAlias()!
210 m_sEvent = sEvent;
212}
213
225void JobData::setJobConfig( std::vector< css::beans::NamedValue >&& lArguments )
226{
227 // update member
228 m_lArguments = std::move(lArguments);
229
230 // update the configuration ... if possible!
231 if (m_eMode!=E_ALIAS)
232 return;
233
234 // It doesn't matter if this config object was already opened before.
235 // It doesn nothing here then ... or it change the mode automatically, if
236 // it was opened using another one before.
237 ConfigAccess aConfig(
239 ("/org.openoffice.Office.Jobs/Jobs/"
242 if (aConfig.getMode()==ConfigAccess::E_CLOSED)
243 return;
244
245 css::uno::Reference< css::beans::XMultiHierarchicalPropertySet > xArgumentList(aConfig.cfg(), css::uno::UNO_QUERY);
246 if (xArgumentList.is())
247 {
248 sal_Int32 nCount = m_lArguments.size();
249 css::uno::Sequence< OUString > lNames (nCount);
250 auto lNamesRange = asNonConstRange(lNames);
251 css::uno::Sequence< css::uno::Any > lValues(nCount);
252 auto lValuesRange = asNonConstRange(lValues);
253
254 for (sal_Int32 i=0; i<nCount; ++i)
255 {
256 lNamesRange [i] = m_lArguments[i].Name;
257 lValuesRange[i] = m_lArguments[i].Value;
258 }
259
260 xArgumentList->setHierarchicalPropertyValues(lNames, lValues);
261 }
262 aConfig.close();
263}
264
272{
273 m_eEnvironment = eEnvironment;
274}
275
282{
283 return m_eMode;
284}
285
287{
288 return m_eEnvironment;
289}
290
292{
293 OUString sDescriptor;
294 switch(m_eEnvironment)
295 {
296 case E_EXECUTION :
297 sDescriptor = "EXECUTOR";
298 break;
299
300 case E_DISPATCH :
301 sDescriptor = "DISPATCH";
302 break;
303
304 case E_DOCUMENTEVENT :
305 sDescriptor = "DOCUMENTEVENT";
306 break;
307 default:
308 break;
309 }
310 return sDescriptor;
311}
312
313OUString JobData::getService() const
314{
315 return m_sService;
316}
317
318OUString JobData::getEvent() const
319{
320 return m_sEvent;
321}
322
323std::vector< css::beans::NamedValue > JobData::getJobConfig() const
324{
325 return m_lArguments;
326}
327
328css::uno::Sequence< css::beans::NamedValue > JobData::getConfig() const
329{
330 css::uno::Sequence< css::beans::NamedValue > lConfig;
331 if (m_eMode==E_ALIAS)
332 {
333 lConfig = { { "Alias", css::uno::Any(m_sAlias) },
334 { "Service", css::uno::Any(m_sService) },
335 { "Context", css::uno::Any(m_sContext) } };
336 }
337 return lConfig;
338}
339
352{
353 return (m_eMode==E_ALIAS || m_eMode==E_EVENT);
354}
355
367{
368 // No configuration - not used from EXECUTOR and not triggered from an event => no chance!
369 if (m_eMode!=E_EVENT)
370 return;
371
372 // update the configuration
373 // It doesn't matter if this config object was already opened before.
374 // It doesn nothing here then ... or it change the mode automatically, if
375 // it was opened using another one before.
376 ConfigAccess aConfig(
378 ("/org.openoffice.Office.Jobs/Events/"
382 if (aConfig.getMode()==ConfigAccess::E_CLOSED)
383 return;
384
385 css::uno::Reference< css::beans::XPropertySet > xPropSet(aConfig.cfg(), css::uno::UNO_QUERY);
386 if (xPropSet.is())
387 {
388 // Convert and write the user timestamp to the configuration.
389 css::uno::Any aValue;
391 xPropSet->setPropertyValue("UserTime", aValue);
392 }
393
394 aConfig.close();
395}
396
397static bool isEnabled( std::u16string_view sAdminTime ,
398 std::u16string_view sUserTime )
399{
400 /*Attention!
401 To prevent interpreting of TriGraphs inside next const string value,
402 we have to encode all '?' signs. Otherwise e.g. "??-" will be translated
403 to "~" ...
404 */
405 WildCard aISOPattern(u"\?\?\?\?-\?\?-\?\?*");
406
407 bool bValidAdmin = aISOPattern.Matches(sAdminTime);
408 bool bValidUser = aISOPattern.Matches(sUserTime );
409
410 // We check for "isEnabled()" here only.
411 // Note further: ISO8601 formatted strings can be compared as strings directly!
412 // FIXME: this is not true! "T1215" is the same time as "T12:15" or "T121500"
413 return (
414 (!bValidAdmin && !bValidUser ) ||
415 ( bValidAdmin && bValidUser && sAdminTime>=sUserTime)
416 );
417}
418
419void JobData::appendEnabledJobsForEvent( const css::uno::Reference< css::uno::XComponentContext >& rxContext,
420 const OUString& sEvent ,
421 ::std::vector< JobData::TJob2DocEventBinding >& lJobs )
422{
423 std::vector< OUString > lAdditionalJobs = JobData::getEnabledJobsForEvent(rxContext, sEvent);
424 sal_Int32 c = lAdditionalJobs.size();
425 sal_Int32 i = 0;
426
427 for (i=0; i<c; ++i)
428 {
429 JobData::TJob2DocEventBinding aBinding(lAdditionalJobs[i], sEvent);
430 lJobs.push_back(aBinding);
431 }
432}
433
434bool JobData::hasCorrectContext(std::u16string_view rModuleIdent) const
435{
436 sal_Int32 nContextLen = m_sContext.getLength();
437 sal_Int32 nModuleIdLen = rModuleIdent.size();
438
439 if ( nContextLen == 0 )
440 return true;
441
442 if ( nModuleIdLen > 0 )
443 {
444 sal_Int32 nIndex = m_sContext.indexOf( rModuleIdent );
445 if ( nIndex >= 0 && ( nIndex+nModuleIdLen <= nContextLen ))
446 {
447 std::u16string_view sContextModule = m_sContext.subView( nIndex, nModuleIdLen );
448 return sContextModule == rModuleIdent;
449 }
450 }
451
452 return false;
453}
454
455std::vector< OUString > JobData::getEnabledJobsForEvent( const css::uno::Reference< css::uno::XComponentContext >& rxContext,
456 std::u16string_view sEvent )
457{
458 // create a config access to "/org.openoffice.Office.Jobs/Events"
459 ConfigAccess aConfig(rxContext, "/org.openoffice.Office.Jobs/Events");
461 if (aConfig.getMode()==ConfigAccess::E_CLOSED)
462 return std::vector< OUString >();
463
464 css::uno::Reference< css::container::XHierarchicalNameAccess > xEventRegistry(aConfig.cfg(), css::uno::UNO_QUERY);
465 if (!xEventRegistry.is())
466 return std::vector< OUString >();
467
468 // check if the given event exist inside list of registered ones
469 OUString sPath(OUString::Concat(sEvent) + "/JobList");
470 if (!xEventRegistry->hasByHierarchicalName(sPath))
471 return std::vector< OUString >();
472
473 // step to the job list, which is a child of the event node inside cfg
474 // e.g. "/org.openoffice.Office.Jobs/Events/<event name>/JobList"
475 css::uno::Any aJobList = xEventRegistry->getByHierarchicalName(sPath);
476 css::uno::Reference< css::container::XNameAccess > xJobList;
477 if (!(aJobList >>= xJobList) || !xJobList.is())
478 return std::vector< OUString >();
479
480 // get all alias names of jobs, which are part of this job list
481 // But Some of them can be disabled by its timestamp values.
482 // We create an additional job name list with the same size, then the original list...
483 // step over all job entries... check her timestamps... and put only job names to the
484 // destination list, which represent an enabled job.
485 const css::uno::Sequence< OUString > lAllJobs = xJobList->getElementNames();
486 sal_Int32 c = lAllJobs.getLength();
487
488 std::vector< OUString > lEnabledJobs(c);
489 sal_Int32 d = 0;
490
491 for (OUString const & jobName : lAllJobs)
492 {
493 css::uno::Reference< css::beans::XPropertySet > xJob;
494 if (
495 !(xJobList->getByName(jobName) >>= xJob) ||
496 !(xJob.is() )
497 )
498 {
499 continue;
500 }
501
502 OUString sAdminTime;
503 xJob->getPropertyValue("AdminTime") >>= sAdminTime;
504
505 OUString sUserTime;
506 xJob->getPropertyValue("UserTime") >>= sUserTime;
507
508 if (!isEnabled(sAdminTime, sUserTime))
509 continue;
510
511 lEnabledJobs[d] = jobName;
512 ++d;
513 }
514 lEnabledJobs.resize(d);
515
516 aConfig.close();
517
518 return lEnabledJobs;
519}
520
533{
536 m_sAlias.clear();
537 m_sService.clear();
538 m_sContext.clear();
539 m_sEvent.clear();
540 m_lArguments.clear();
541}
542
543} // namespace framework
544
545/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
double d
bool Matches(std::u16string_view rStr) const
implements a simple configuration access @descr Sometimes it's better to have direct config access in...
void open(EOpenMode eMode)
open the configuration access in the specified mode @descr We set the opened configuration access as ...
EOpenMode getMode() const
return the internal mode of this instance @descr May be the outside user need any information about s...
void close()
close the internal opened configuration access and flush all changes @descr It checks,...
const css::uno::Reference< css::uno::XInterface > & cfg()
provides an access to the internal wrapped configuration access @descr It's not allowed to safe this ...
@ E_READONLY
config access is open for reading only
@ E_CLOSED
config isn't used yet
@ E_READWRITE
config access is open for reading/writing data
static OUString convert_DateTime2ISO8601(const DateTime &aSource)
Definition: converter.cxx:58
holds all necessary information about a job and handle it's configuration (if any exist!...
Definition: jobdata.hxx:41
void setEvent(const OUString &sEvent, const OUString &sAlias)
initialize this instance with new job values.
Definition: jobdata.cxx:203
void disableJob()
mark a job as non startable for further requests @descr We don't remove the configuration entry!...
Definition: jobdata.cxx:366
~JobData()
let this instance die @descr There is no chance any longer to work.
Definition: jobdata.cxx:97
css::uno::Reference< css::uno::XComponentContext > m_xContext
reference to the uno service manager.
Definition: jobdata.hxx:106
static void appendEnabledJobsForEvent(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const OUString &sEvent, ::std::vector< JobData::TJob2DocEventBinding > &lJobs)
Definition: jobdata.cxx:419
EEnvironment getEnvironment() const
Definition: jobdata.cxx:286
void setService(const OUString &sService)
initialize this instance as a job without configuration @descr This job has no configuration data.
Definition: jobdata.cxx:176
bool hasCorrectContext(std::u16string_view rModuleIdent) const
Definition: jobdata.cxx:434
JobData(css::uno::Reference< css::uno::XComponentContext > xContext)
standard ctor @descr It initialize this new instance.
Definition: jobdata.cxx:49
bool hasConfig() const
return information, if this job is part of the global configuration package org.openoffice....
Definition: jobdata.cxx:351
EMode getMode() const
these functions provides access to our internal members @descr These member represent any information...
Definition: jobdata.cxx:281
EMode
These values can be used to differe between jobs with and jobs without a configuration.
Definition: jobdata.hxx:49
@ E_EVENT
indicates a job with configuration, which was triggered by an event
Definition: jobdata.hxx:57
@ E_SERVICE
indicates a job without configuration (The pure UNO implementation is used only.)
Definition: jobdata.hxx:55
@ E_UNKNOWN_MODE
indicates a missing initialization
Definition: jobdata.hxx:51
@ E_ALIAS
indicates a job with configuration (They alias represent the config key name.)
Definition: jobdata.hxx:53
OUString getEnvironmentDescriptor() const
Definition: jobdata.cxx:291
OUString getEvent() const
Definition: jobdata.cxx:318
EEnvironment m_eEnvironment
Because jobs can be bind to different mechanism inside office, a job should know inside which environ...
Definition: jobdata.hxx:126
OUString m_sContext
the module context list of this job.
Definition: jobdata.hxx:145
EEnvironment
These values represent the environment type, in which a job can run.
Definition: jobdata.hxx:65
@ E_UNKNOWN_ENVIRONMENT
indicates a missing initialization
Definition: jobdata.hxx:67
@ E_DISPATCH
this job is used by the global dispatch framework
Definition: jobdata.hxx:71
@ E_EXECUTION
this job is used by the global JobExecutor service
Definition: jobdata.hxx:69
@ E_DOCUMENTEVENT
this job is used by the global event broadcaster
Definition: jobdata.hxx:73
css::uno::Sequence< css::beans::NamedValue > getConfig() const
Definition: jobdata.cxx:328
static std::vector< OUString > getEnabledJobsForEvent(const css::uno::Reference< css::uno::XComponentContext > &rxContext, std::u16string_view sEvent)
Definition: jobdata.cxx:455
OUString getService() const
Definition: jobdata.cxx:313
void setJobConfig(std::vector< css::beans::NamedValue > &&lArguments)
set the new job specific arguments @descr If a job finish his work, it can give us a new list of argu...
Definition: jobdata.cxx:225
OUString m_sService
the uno implementation name of this job.
Definition: jobdata.hxx:139
std::vector< css::beans::NamedValue > getJobConfig() const
Definition: jobdata.cxx:323
EMode m_eMode
An instance of this class can be used in two different modes:
Definition: jobdata.hxx:118
void setEnvironment(EEnvironment eEnvironment)
set a new environment descriptor for this job @descr It must(!) be done every time this container is ...
Definition: jobdata.cxx:271
JobData & operator=(const JobData &rCopy)
operator for copying JobData instances @descr Sometimes such job data container must be moved from on...
Definition: jobdata.cxx:78
void impl_reset()
reset all internal structures @descr If someone recycles this instance, he can switch from one using ...
Definition: jobdata.cxx:532
std::vector< css::beans::NamedValue > m_lArguments
job specific configuration items... unknown for us! It's read from the configuration.
Definition: jobdata.hxx:162
OUString m_sEvent
a job can be registered for an event.
Definition: jobdata.hxx:156
OUString m_sAlias
the alias name of this job.
Definition: jobdata.hxx:133
void setAlias(const OUString &sAlias)
initialize this instance as a job with configuration @descr They given alias can be used to address s...
Definition: jobdata.cxx:111
int nCount
float u
css::uno::Reference< css::uno::XComponentContext > m_xContext
sal_Int32 nIndex
static bool isEnabled(std::u16string_view sAdminTime, std::u16string_view sUserTime)
Definition: jobdata.cxx:397
int i
OUString wrapConfigurationElementName(std::u16string_view _sElementName)
Some jobs can be registered to "logical events", which are generated on demand if another document ev...
Definition: jobdata.hxx:86
OUString Name