Line data Source code
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 <jobs/configaccess.hxx>
21 : #include <jobs/joburl.hxx>
22 : #include <jobs/job.hxx>
23 : #include <classes/converter.hxx>
24 : #include <general.h>
25 :
26 : #include <com/sun/star/frame/DispatchResultEvent.hpp>
27 : #include <com/sun/star/frame/DispatchResultState.hpp>
28 : #include <com/sun/star/frame/ModuleManager.hpp>
29 : #include <com/sun/star/frame/XNotifyingDispatch.hpp>
30 : #include <com/sun/star/frame/XDispatch.hpp>
31 : #include <com/sun/star/frame/XStatusListener.hpp>
32 : #include <com/sun/star/frame/XDispatchResultListener.hpp>
33 : #include <com/sun/star/frame/XDispatchProvider.hpp>
34 : #include <com/sun/star/lang/XInitialization.hpp>
35 : #include <com/sun/star/lang/XServiceInfo.hpp>
36 :
37 : #include <cppuhelper/supportsservice.hxx>
38 : #include <cppuhelper/implbase4.hxx>
39 : #include <rtl/ref.hxx>
40 : #include <rtl/ustrbuf.hxx>
41 : #include <unotools/configpaths.hxx>
42 : #include <vcl/svapp.hxx>
43 :
44 : using namespace framework;
45 :
46 : namespace {
47 :
48 : /**
49 : @short implements a dispatch object for jobs
50 : @descr Such dispatch object will be used by the generic dispatch mechanism if
51 : an URL "vnd.sun.star.job:alias=<name>" occurs.
52 : Then an instance of this class will be created and used.
53 : This new instance will be called within his method
54 : dispatch() or dispatchWithNotification() for executing the
55 : real job. We do it, control the life cycle of this internal
56 : wrapped job and inform any interested listener if it finish.
57 : */
58 : class JobDispatch : public ::cppu::WeakImplHelper4<
59 : css::lang::XServiceInfo
60 : , css::lang::XInitialization
61 : , css::frame::XDispatchProvider
62 : , css::frame::XNotifyingDispatch > // => XDispatch
63 : {
64 : private:
65 :
66 : /** reference to the uno service manager */
67 : css::uno::Reference< css::uno::XComponentContext > m_xContext;
68 :
69 : /** reference to the frame, inside which this dispatch is used */
70 : css::uno::Reference< css::frame::XFrame > m_xFrame;
71 :
72 : /** name of module (writer, impress etc.) the frame is for */
73 : OUString m_sModuleIdentifier;
74 :
75 : // native interface methods
76 :
77 : public:
78 :
79 : JobDispatch( const css::uno::Reference< css::uno::XComponentContext >& xContext );
80 : virtual ~JobDispatch();
81 :
82 : void impl_dispatchEvent ( const OUString& sEvent ,
83 : const css::uno::Sequence< css::beans::PropertyValue >& lArgs ,
84 : const css::uno::Reference< css::frame::XDispatchResultListener >& xListener );
85 : void impl_dispatchService( const OUString& sService ,
86 : const css::uno::Sequence< css::beans::PropertyValue >& lArgs ,
87 : const css::uno::Reference< css::frame::XDispatchResultListener >& xListener );
88 : void impl_dispatchAlias ( const OUString& sAlias ,
89 : const css::uno::Sequence< css::beans::PropertyValue >& lArgs ,
90 : const css::uno::Reference< css::frame::XDispatchResultListener >& xListener );
91 :
92 : public:
93 1 : virtual OUString SAL_CALL getImplementationName()
94 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
95 : {
96 1 : return OUString("com.sun.star.comp.framework.jobs.JobDispatch");
97 : }
98 :
99 0 : virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
100 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
101 : {
102 0 : return cppu::supportsService(this, ServiceName);
103 : }
104 :
105 1 : virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
106 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
107 : {
108 1 : css::uno::Sequence< OUString > aSeq(1);
109 1 : aSeq[0] = "com.sun.star.frame.ProtocolHandler";
110 1 : return aSeq;
111 : }
112 :
113 : // Xinitialization
114 : virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& lArguments ) throw(css::uno::Exception ,
115 : css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
116 :
117 : // XDispatchProvider
118 : virtual css::uno::Reference< css::frame::XDispatch > SAL_CALL queryDispatch ( const css::util::URL& aURL ,
119 : const OUString& sTargetFrameName ,
120 : sal_Int32 nSearchFlags ) throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
121 : virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptor ) throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
122 :
123 : // XNotifyingDispatch
124 : virtual void SAL_CALL dispatchWithNotification( const css::util::URL& aURL ,
125 : const css::uno::Sequence< css::beans::PropertyValue >& lArgs ,
126 : const css::uno::Reference< css::frame::XDispatchResultListener >& xListener ) throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
127 :
128 : // XDispatch
129 : virtual void SAL_CALL dispatch ( const css::util::URL& aURL ,
130 : const css::uno::Sequence< css::beans::PropertyValue >& lArgs ) throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
131 : virtual void SAL_CALL addStatusListener ( const css::uno::Reference< css::frame::XStatusListener >& xListener ,
132 : const css::util::URL& aURL ) throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
133 : virtual void SAL_CALL removeStatusListener( const css::uno::Reference< css::frame::XStatusListener >& xListener ,
134 : const css::util::URL& aURL ) throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
135 : };
136 :
137 : /**
138 : @short standard ctor
139 : @descr It initialize this new instance.
140 :
141 : @param xContext
142 : reference to the uno service manager
143 : */
144 1 : JobDispatch::JobDispatch( /*IN*/ const css::uno::Reference< css::uno::XComponentContext >& xContext )
145 1 : : m_xContext (xContext )
146 : {
147 1 : }
148 :
149 : /**
150 : @short let this instance die
151 : @descr We have to release all used resources and free used memory.
152 : */
153 3 : JobDispatch::~JobDispatch()
154 : {
155 : // release all used resources
156 1 : m_xContext.clear();
157 1 : m_xFrame.clear();
158 2 : }
159 :
160 : /**
161 : @short implementation of XInitalization
162 : @descr A protocol handler can provide this functionality, if it wish to get additional information
163 : about the context it runs. In this case the frame reference would be given by the outside code.
164 :
165 : @param lArguments
166 : the list of initialization arguments
167 : First parameter should be the frame reference we need.
168 : */
169 0 : void SAL_CALL JobDispatch::initialize( const css::uno::Sequence< css::uno::Any >& lArguments ) throw(css::uno::Exception ,
170 : css::uno::RuntimeException, std::exception)
171 : {
172 0 : SolarMutexGuard g;
173 :
174 0 : for (int a=0; a<lArguments.getLength(); ++a)
175 : {
176 0 : if (a==0)
177 : {
178 0 : lArguments[a] >>= m_xFrame;
179 :
180 : css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
181 0 : css::frame::ModuleManager::create(m_xContext);
182 : try
183 : {
184 0 : m_sModuleIdentifier = xModuleManager->identify( m_xFrame );
185 : }
186 0 : catch( const css::uno::Exception& )
187 0 : {}
188 : }
189 0 : }
190 0 : }
191 :
192 : /**
193 : @short implementation of XDispatchProvider::queryDispatches()
194 : @descr Every protocol handler will be asked for his agreement, if an URL was queried
195 : for which this handler is registered. It's the chance for this handler to validate
196 : the given URL and return a dispatch object (may be itself) or not.
197 :
198 : @param aURL
199 : the queried URL, which should be checked
200 :
201 : @param sTargetFrameName
202 : describes the target frame, in which context this handler will be used
203 : Is mostly set to "", "_self", "_blank", "_default" or a non special one
204 : using SELF/CREATE as search flags.
205 :
206 : @param nSearchFlags
207 : Can be SELF or CREATE only and are set only if sTargetFrameName isn't a special target
208 : */
209 0 : css::uno::Reference< css::frame::XDispatch > SAL_CALL JobDispatch::queryDispatch( /*IN*/ const css::util::URL& aURL ,
210 : /*IN*/ const OUString& /*sTargetFrameName*/ ,
211 : /*IN*/ sal_Int32 /*nSearchFlags*/ ) throw(css::uno::RuntimeException, std::exception)
212 : {
213 0 : css::uno::Reference< css::frame::XDispatch > xDispatch;
214 :
215 0 : JobURL aAnalyzedURL(aURL.Complete);
216 0 : if (aAnalyzedURL.isValid())
217 0 : xDispatch = css::uno::Reference< css::frame::XDispatch >( static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY );
218 :
219 0 : return xDispatch;
220 : }
221 :
222 : /**
223 : @short implementation of XDispatchProvider::queryDispatches()
224 : @descr It's an optimized access for remote, so you can ask for
225 : multiple dispatch objects at the same time.
226 :
227 : @param lDescriptor
228 : a list of queryDispatch() parameter
229 :
230 : @return A list of corresponding dispatch objects.
231 : NULL references are not skipped. Every result
232 : match to one given descriptor item.
233 : */
234 0 : css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL JobDispatch::queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptor ) throw(css::uno::RuntimeException, std::exception)
235 : {
236 : // don't pack resulting list!
237 0 : sal_Int32 nCount = lDescriptor.getLength();
238 0 : css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatches(nCount);
239 :
240 0 : for (sal_Int32 i=0; i<nCount; ++i)
241 0 : lDispatches[i] = queryDispatch( lDescriptor[i].FeatureURL ,
242 0 : lDescriptor[i].FrameName ,
243 0 : lDescriptor[i].SearchFlags );
244 0 : return lDispatches;
245 : }
246 :
247 : /**
248 : @short implementation of XNotifyingDispatch::dispatchWithNotification()
249 : @descr It creates the job service implementation and call execute on it.
250 : Further it starts the life time control of it. (important for async job)
251 : For synchonrous job we react for the returned result directly ... for asynchronous
252 : ones we do it later inside our callback method. But we use the same impl method
253 : doing that to share the code. (see impl_finishJob())
254 :
255 : If a job is already running, (it can only occur for asynchronous jobs)
256 : don't start the same job a second time. Queue in the given dispatch parameter
257 : and return immediately. If the current running job call us back, we will start this
258 : new dispatch request.
259 : If no job is running - queue the parameter too! But then start the new job immediately.
260 : We have to queue it every time - because it hold us alive by ref count!
261 :
262 : @param aURL
263 : describe the job(s), which should be started
264 :
265 : @param lArgs
266 : optional arguments for this request
267 :
268 : @param xListener
269 : an interested listener for possible results of this operation
270 : */
271 0 : void SAL_CALL JobDispatch::dispatchWithNotification( /*IN*/ const css::util::URL& aURL ,
272 : /*IN*/ const css::uno::Sequence< css::beans::PropertyValue >& lArgs ,
273 : /*IN*/ const css::uno::Reference< css::frame::XDispatchResultListener >& xListener ) throw(css::uno::RuntimeException, std::exception)
274 : {
275 0 : JobURL aAnalyzedURL(aURL.Complete);
276 0 : if (aAnalyzedURL.isValid())
277 : {
278 0 : OUString sRequest;
279 0 : if (aAnalyzedURL.getEvent(sRequest))
280 0 : impl_dispatchEvent(sRequest, lArgs, xListener);
281 : else
282 0 : if (aAnalyzedURL.getService(sRequest))
283 0 : impl_dispatchService(sRequest, lArgs, xListener);
284 : else
285 0 : if (aAnalyzedURL.getAlias(sRequest))
286 0 : impl_dispatchAlias(sRequest, lArgs, xListener);
287 0 : }
288 0 : }
289 :
290 : /**
291 : @short dispatch an event
292 : @descr We search all registered jobs for this event and execute it.
293 : After doing so, we inform the given listener about the results.
294 : (There will be one notify for every executed job!)
295 :
296 : @param sEvent
297 : the event, for which jobs can be registered
298 :
299 : @param lArgs
300 : optional arguments for this request
301 : Currently not used!
302 :
303 : @param xListener
304 : an interested listener for possible results of this operation
305 : */
306 0 : void JobDispatch::impl_dispatchEvent( /*IN*/ const OUString& sEvent ,
307 : /*IN*/ const css::uno::Sequence< css::beans::PropertyValue >& lArgs ,
308 : /*IN*/ const css::uno::Reference< css::frame::XDispatchResultListener >& xListener )
309 : {
310 : // get list of all enabled jobs
311 : // The called static helper methods read it from the configuration and
312 : // filter disabled jobs using it's time stamp values.
313 : /* SAFE { */
314 0 : SolarMutexResettableGuard aReadLock;
315 0 : css::uno::Sequence< OUString > lJobs = JobData::getEnabledJobsForEvent(m_xContext, sEvent);
316 0 : aReadLock.clear();
317 : /* } SAFE */
318 :
319 0 : css::uno::Reference< css::frame::XDispatchResultListener > xThis( static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY );
320 :
321 : // no jobs ... no execution
322 : // But a may given listener will know something ...
323 : // I think this operaton was finished successfully.
324 : // It's not really an error, if no registered jobs could be located.
325 : // Step over all found jobs and execute it
326 0 : int nExecutedJobs=0;
327 0 : for (int j=0; j<lJobs.getLength(); ++j)
328 : {
329 : /* SAFE { */
330 0 : aReadLock.reset();
331 :
332 0 : JobData aCfg(m_xContext);
333 0 : aCfg.setEvent(sEvent, lJobs[j]);
334 0 : aCfg.setEnvironment(JobData::E_DISPATCH);
335 0 : const bool bIsEnabled=aCfg.hasCorrectContext(m_sModuleIdentifier);
336 :
337 : /*Attention!
338 : Jobs implements interfaces and dies by ref count!
339 : And freeing of such uno object is done by uno itself.
340 : So we have to use dynamic memory everytimes.
341 : */
342 0 : Job* pJob = new Job(m_xContext, m_xFrame);
343 0 : css::uno::Reference< css::uno::XInterface > xJob(static_cast< ::cppu::OWeakObject* >(pJob), css::uno::UNO_QUERY);
344 0 : pJob->setJobData(aCfg);
345 :
346 0 : aReadLock.clear();
347 : /* } SAFE */
348 :
349 0 : if (!bIsEnabled)
350 0 : continue;
351 :
352 : // Special mode for listener.
353 : // We dont notify it directly here. We delegate that
354 : // to the job implementation. But we must set ourself there too.
355 : // Because this job must fake the source address of the event.
356 : // Otherwise the listener may ignore it.
357 0 : if (xListener.is())
358 0 : pJob->setDispatchResultFake(xListener, xThis);
359 0 : pJob->execute(Converter::convert_seqPropVal2seqNamedVal(lArgs));
360 0 : ++nExecutedJobs;
361 0 : }
362 :
363 0 : if (nExecutedJobs<1 && xListener.is())
364 : {
365 0 : css::frame::DispatchResultEvent aEvent;
366 0 : aEvent.Source = xThis;
367 0 : aEvent.State = css::frame::DispatchResultState::SUCCESS;
368 0 : xListener->dispatchFinished(aEvent);
369 0 : }
370 0 : }
371 :
372 : /**
373 : @short dispatch a service
374 : @descr We use the given name only to create and if possible to initialize
375 : it as an uno service. It can be useful for creating (caching?)
376 : of e.g. one instance services.
377 :
378 : @param sService
379 : the uno implementation or service name of the job, which should be instantiated
380 :
381 : @param lArgs
382 : optional arguments for this request
383 : Currently not used!
384 :
385 : @param xListener
386 : an interested listener for possible results of this operation
387 : */
388 0 : void JobDispatch::impl_dispatchService( /*IN*/ const OUString& sService ,
389 : /*IN*/ const css::uno::Sequence< css::beans::PropertyValue >& lArgs ,
390 : /*IN*/ const css::uno::Reference< css::frame::XDispatchResultListener >& xListener )
391 : {
392 : /* SAFE { */
393 0 : SolarMutexClearableGuard aReadLock;
394 :
395 0 : JobData aCfg(m_xContext);
396 0 : aCfg.setService(sService);
397 0 : aCfg.setEnvironment(JobData::E_DISPATCH);
398 :
399 : /*Attention!
400 : Jobs implements interfaces and dies by ref count!
401 : And freeing of such uno object is done by uno itself.
402 : So we have to use dynamic memory everytimes.
403 : */
404 0 : Job* pJob = new Job(m_xContext, m_xFrame);
405 0 : css::uno::Reference< css::uno::XInterface > xJob(static_cast< ::cppu::OWeakObject* >(pJob), css::uno::UNO_QUERY);
406 0 : pJob->setJobData(aCfg);
407 :
408 0 : aReadLock.clear();
409 : /* } SAFE */
410 :
411 0 : css::uno::Reference< css::frame::XDispatchResultListener > xThis( static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY );
412 :
413 : // Special mode for listener.
414 : // We dont notify it directly here. We delegate that
415 : // to the job implementation. But we must set ourself there too.
416 : // Because this job must fake the source address of the event.
417 : // Otherwise the listener may ignore it.
418 0 : if (xListener.is())
419 0 : pJob->setDispatchResultFake(xListener, xThis);
420 0 : pJob->execute(Converter::convert_seqPropVal2seqNamedVal(lArgs));
421 0 : }
422 :
423 : /**
424 : @short dispatch an alias
425 : @descr We use this alias to locate a job inside the configuration
426 : and execute it. Further we inform the given listener about the results.
427 :
428 : @param sAlias
429 : the alias name of the configured job
430 :
431 : @param lArgs
432 : optional arguments for this request
433 : Currently not used!
434 :
435 : @param xListener
436 : an interested listener for possible results of this operation
437 : */
438 0 : void JobDispatch::impl_dispatchAlias( /*IN*/ const OUString& sAlias ,
439 : /*IN*/ const css::uno::Sequence< css::beans::PropertyValue >& lArgs ,
440 : /*IN*/ const css::uno::Reference< css::frame::XDispatchResultListener >& xListener )
441 : {
442 : /* SAFE { */
443 0 : SolarMutexClearableGuard aReadLock;
444 :
445 0 : JobData aCfg(m_xContext);
446 0 : aCfg.setAlias(sAlias);
447 0 : aCfg.setEnvironment(JobData::E_DISPATCH);
448 :
449 : /*Attention!
450 : Jobs implements interfaces and dies by ref count!
451 : And freeing of such uno object is done by uno itself.
452 : So we have to use dynamic memory everytimes.
453 : */
454 0 : Job* pJob = new Job(m_xContext, m_xFrame);
455 0 : css::uno::Reference< css::uno::XInterface > xJob(static_cast< ::cppu::OWeakObject* >(pJob), css::uno::UNO_QUERY);
456 0 : pJob->setJobData(aCfg);
457 :
458 0 : aReadLock.clear();
459 : /* } SAFE */
460 :
461 0 : css::uno::Reference< css::frame::XDispatchResultListener > xThis( static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY );
462 :
463 : // Special mode for listener.
464 : // We dont notify it directly here. We delegate that
465 : // to the job implementation. But we must set ourself there too.
466 : // Because this job must fake the source address of the event.
467 : // Otherwise the listener may ignore it.
468 0 : if (xListener.is())
469 0 : pJob->setDispatchResultFake(xListener, xThis);
470 0 : pJob->execute(Converter::convert_seqPropVal2seqNamedVal(lArgs));
471 0 : }
472 :
473 : /**
474 : @short implementation of XDispatch::dispatch()
475 : @descr Because the methods dispatch() and dispatchWithNotification() are different in her parameters
476 : only, we can forward this request to dispatchWithNotification() by using an empty listener!
477 :
478 : @param aURL
479 : describe the job(s), which should be started
480 :
481 : @param lArgs
482 : optional arguments for this request
483 :
484 : @see dispatchWithNotification()
485 : */
486 0 : void SAL_CALL JobDispatch::dispatch( /*IN*/ const css::util::URL& aURL ,
487 : /*IN*/ const css::uno::Sequence< css::beans::PropertyValue >& lArgs ) throw(css::uno::RuntimeException, std::exception)
488 : {
489 0 : dispatchWithNotification(aURL, lArgs, css::uno::Reference< css::frame::XDispatchResultListener >());
490 0 : }
491 :
492 : /**
493 : @short not supported
494 : */
495 0 : void SAL_CALL JobDispatch::addStatusListener( /*IN*/ const css::uno::Reference< css::frame::XStatusListener >&,
496 : /*IN*/ const css::util::URL& ) throw(css::uno::RuntimeException, std::exception)
497 : {
498 0 : }
499 :
500 : /**
501 : @short not supported
502 : */
503 0 : void SAL_CALL JobDispatch::removeStatusListener( /*IN*/ const css::uno::Reference< css::frame::XStatusListener >&,
504 : /*IN*/ const css::util::URL& ) throw(css::uno::RuntimeException, std::exception)
505 : {
506 0 : }
507 :
508 : }
509 :
510 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
511 1 : com_sun_star_comp_framework_jobs_JobDispatch_get_implementation(
512 : css::uno::XComponentContext *context,
513 : css::uno::Sequence<css::uno::Any> const &)
514 : {
515 1 : return cppu::acquire(new JobDispatch(context));
516 : }
517 :
518 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|