1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jobdispatch.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 //________________________________
36 #include <jobs/jobdispatch.hxx>
37 #include <jobs/joburl.hxx>
38 #include <jobs/job.hxx>
39 #include <threadhelp/readguard.hxx>
40 #include <threadhelp/writeguard.hxx>
41 #include <threadhelp/resetableguard.hxx>
42 #include <classes/converter.hxx>
46 //________________________________
48 #include <com/sun/star/beans/XPropertySet.hpp>
49 #include <com/sun/star/frame/DispatchResultState.hpp>
51 //________________________________
52 // includes of other projects
53 #include <rtl/ustrbuf.hxx>
54 #include <vcl/svapp.hxx>
56 //________________________________
61 //________________________________
64 //________________________________
65 // non exported definitions
67 //________________________________
70 DEFINE_XINTERFACE_6( JobDispatch
,
72 DIRECT_INTERFACE(css::lang::XTypeProvider
),
73 DIRECT_INTERFACE(css::frame::XDispatchProvider
),
74 DIRECT_INTERFACE(css::lang::XInitialization
),
75 DIRECT_INTERFACE(css::lang::XServiceInfo
),
76 DIRECT_INTERFACE(css::frame::XNotifyingDispatch
),
77 DIRECT_INTERFACE(css::frame::XDispatch
)
80 DEFINE_XTYPEPROVIDER_6( JobDispatch
,
81 css::lang::XTypeProvider
,
82 css::frame::XDispatchProvider
,
83 css::frame::XNotifyingDispatch
,
84 css::lang::XInitialization
,
85 css::lang::XServiceInfo
,
89 DEFINE_XSERVICEINFO_MULTISERVICE( JobDispatch
,
91 SERVICENAME_PROTOCOLHANDLER
,
92 IMPLEMENTATIONNAME_JOBDISPATCH
95 DEFINE_INIT_SERVICE( JobDispatch
,
98 I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
99 to create a new instance of this class by our own supported service factory.
100 see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further informations!
105 //________________________________
108 @descr It initialize this new instance.
111 reference to the uno service manager
113 JobDispatch::JobDispatch( /*IN*/ const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
)
114 : ThreadHelpBase(&Application::GetSolarMutex())
120 //________________________________
122 @short let this instance die
123 @descr We have to release all used ressources and free used memory.
125 JobDispatch::~JobDispatch()
127 // release all used ressources
128 m_xSMGR
= css::uno::Reference
< css::lang::XMultiServiceFactory
>();
129 m_xFrame
= css::uno::Reference
< css::frame::XFrame
>();
132 //________________________________
134 @short implementation of XInitalization
135 @descr A protocol handler can provide this functionality, if it wish to get additional informations
136 about the context it runs. In this case the frame reference would be given by the outside code.
139 the list of initialization arguments
140 First parameter should be the frame reference we need.
142 void SAL_CALL
JobDispatch::initialize( const css::uno::Sequence
< css::uno::Any
>& lArguments
) throw(css::uno::Exception
,
143 css::uno::RuntimeException
)
146 WriteGuard
aWriteLock(m_aLock
);
148 for (int a
=0; a
<lArguments
.getLength(); ++a
)
151 lArguments
[a
] >>= m_xFrame
;
158 //________________________________
160 @short implementation of XDispatchProvider::queryDispatches()
161 @descr Every protocol handler will be asked for his agreement, if an URL was queried
162 for which this handler is registered. It's the chance for this handler to validate
163 the given URL and return a dispatch object (may be itself) or not.
166 the queried URL, which should be checked
168 @param sTargetFrameName
169 describes the target frame, in which context this handler will be used
170 Is mostly set to "", "_self", "_blank", "_default" or a non special one
171 using SELF/CREATE as search flags.
174 Can be SELF or CREATE only and are set only if sTargetFrameName isn't a special target
176 css::uno::Reference
< css::frame::XDispatch
> SAL_CALL
JobDispatch::queryDispatch( /*IN*/ const css::util::URL
& aURL
,
177 /*IN*/ const ::rtl::OUString
& /*sTargetFrameName*/ ,
178 /*IN*/ sal_Int32
/*nSearchFlags*/ ) throw(css::uno::RuntimeException
)
180 css::uno::Reference
< css::frame::XDispatch
> xDispatch
;
182 JobURL
aAnalyzedURL(aURL
.Complete
);
183 if (aAnalyzedURL
.isValid())
184 xDispatch
= css::uno::Reference
< css::frame::XDispatch
>( static_cast< ::cppu::OWeakObject
* >(this), css::uno::UNO_QUERY
);
189 //________________________________
191 @short implementation of XDispatchProvider::queryDispatches()
192 @descr It's an optimized access for remote, so you can ask for
193 multiple dispatch objects at the same time.
196 a list of queryDispatch() parameter
198 @return A list of corresponding dispatch objects.
199 NULL references are not skipped. Every result
200 match to one given descriptor item.
202 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
)
204 // don't pack resulting list!
205 sal_Int32 nCount
= lDescriptor
.getLength();
206 css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > lDispatches(nCount
);
208 for (sal_Int32 i
=0; i
<nCount
; ++i
)
209 lDispatches
[i
] = queryDispatch( lDescriptor
[i
].FeatureURL
,
210 lDescriptor
[i
].FrameName
,
211 lDescriptor
[i
].SearchFlags
);
215 //________________________________
217 @short implementation of XNotifyingDispatch::dispatchWithNotification()
218 @descr It creates the job service implementation and call execute on it.
219 Further it starts the life time control of it. (important for async job)
220 For synchonrous job we react for the returned result directly ... for asynchronous
221 ones we do it later inside our callback method. But we use the same impl method
222 doing that to share the code. (see impl_finishJob())
224 If a job is already running, (it can only occure for asynchronous jobs)
225 don't start the same job a second time. Queue in the given dispatch parameter
226 and return immediatly. If the current running job call us back, we will start this
227 new dispatch request.
228 If no job is running - queue the parameter too! But then start the new job immediatly.
229 We have to queue it every time - because it hold us alive by ref count!
232 describe the job(s), which should be started
235 optional arguments for this request
238 an interested listener for possible results of this operation
240 void SAL_CALL
JobDispatch::dispatchWithNotification( /*IN*/ const css::util::URL
& aURL
,
241 /*IN*/ const css::uno::Sequence
< css::beans::PropertyValue
>& lArgs
,
242 /*IN*/ const css::uno::Reference
< css::frame::XDispatchResultListener
>& xListener
) throw(css::uno::RuntimeException
)
244 JobURL
aAnalyzedURL(aURL
.Complete
);
245 if (aAnalyzedURL
.isValid())
247 ::rtl::OUString sRequest
;
248 if (aAnalyzedURL
.getEvent(sRequest
))
249 impl_dispatchEvent(sRequest
, lArgs
, xListener
);
251 if (aAnalyzedURL
.getService(sRequest
))
252 impl_dispatchService(sRequest
, lArgs
, xListener
);
254 if (aAnalyzedURL
.getAlias(sRequest
))
255 impl_dispatchAlias(sRequest
, lArgs
, xListener
);
259 //________________________________
261 @short dispatch an event
262 @descr We search all registered jobs for this event and execute it.
263 After doing so, we inform the given listener about the results.
264 (There will be one notify for every executed job!)
267 the event, for which jobs can be registered
270 optional arguments for this request
274 an interested listener for possible results of this operation
276 void JobDispatch::impl_dispatchEvent( /*IN*/ const ::rtl::OUString
& sEvent
,
277 /*IN*/ const css::uno::Sequence
< css::beans::PropertyValue
>& lArgs
,
278 /*IN*/ const css::uno::Reference
< css::frame::XDispatchResultListener
>& xListener
)
280 // get list of all enabled jobs
281 // The called static helper methods read it from the configuration and
282 // filter disabled jobs using it's time stamp values.
284 ReadGuard
aReadLock(m_aLock
);
285 css::uno::Sequence
< ::rtl::OUString
> lJobs
= JobData::getEnabledJobsForEvent(m_xSMGR
, sEvent
);
289 css::uno::Reference
< css::frame::XDispatchResultListener
> xThis( static_cast< ::cppu::OWeakObject
* >(this), css::uno::UNO_QUERY
);
291 // no jobs ... no execution
292 // But a may given listener will know something ...
293 // I think this operaton was finished successfully.
294 // It's not realy an error, if no registered jobs could be located.
295 if (lJobs
.getLength()<1 && xListener
.is())
297 css::frame::DispatchResultEvent aEvent
;
298 aEvent
.Source
= xThis
;
299 aEvent
.State
= css::frame::DispatchResultState::SUCCESS
;
300 xListener
->dispatchFinished(aEvent
);
304 // Step over all found jobs and execute it
305 for (int j
=0; j
<lJobs
.getLength(); ++j
)
310 JobData
aCfg(m_xSMGR
);
311 aCfg
.setEvent(sEvent
, lJobs
[j
]);
312 aCfg
.setEnvironment(JobData::E_DISPATCH
);
315 Jobs implements interfaces and dies by ref count!
316 And freeing of such uno object is done by uno itself.
317 So we have to use dynamic memory everytimes.
319 Job
* pJob
= new Job(m_xSMGR
, m_xFrame
);
320 css::uno::Reference
< css::uno::XInterface
> xJob(static_cast< ::cppu::OWeakObject
* >(pJob
), css::uno::UNO_QUERY
);
321 pJob
->setJobData(aCfg
);
326 // Special mode for listener.
327 // We dont notify it directly here. We delegate that
328 // to the job implementation. But we must set ourself there too.
329 // Because this job must fake the source adress of the event.
330 // Otherwhise the listener may will ignore it.
332 pJob
->setDispatchResultFake(xListener
, xThis
);
333 pJob
->execute(Converter::convert_seqPropVal2seqNamedVal(lArgs
));
337 //________________________________
339 @short dispatch a service
340 @descr We use the given name only to create and if possible to initialize
341 it as an uno service. It can be usefully for creating (caching?)
342 of e.g. one instance services.
345 the uno implementation or service name of the job, which should be instanciated
348 optional arguments for this request
352 an interested listener for possible results of this operation
354 void JobDispatch::impl_dispatchService( /*IN*/ const ::rtl::OUString
& sService
,
355 /*IN*/ const css::uno::Sequence
< css::beans::PropertyValue
>& lArgs
,
356 /*IN*/ const css::uno::Reference
< css::frame::XDispatchResultListener
>& xListener
)
359 ReadGuard
aReadLock(m_aLock
);
361 JobData
aCfg(m_xSMGR
);
362 aCfg
.setService(sService
);
363 aCfg
.setEnvironment(JobData::E_DISPATCH
);
366 Jobs implements interfaces and dies by ref count!
367 And freeing of such uno object is done by uno itself.
368 So we have to use dynamic memory everytimes.
370 Job
* pJob
= new Job(m_xSMGR
, m_xFrame
);
371 css::uno::Reference
< css::uno::XInterface
> xJob(static_cast< ::cppu::OWeakObject
* >(pJob
), css::uno::UNO_QUERY
);
372 pJob
->setJobData(aCfg
);
377 css::uno::Reference
< css::frame::XDispatchResultListener
> xThis( static_cast< ::cppu::OWeakObject
* >(this), css::uno::UNO_QUERY
);
379 // Special mode for listener.
380 // We dont notify it directly here. We delegate that
381 // to the job implementation. But we must set ourself there too.
382 // Because this job must fake the source adress of the event.
383 // Otherwhise the listener may will ignore it.
385 pJob
->setDispatchResultFake(xListener
, xThis
);
386 pJob
->execute(Converter::convert_seqPropVal2seqNamedVal(lArgs
));
389 //________________________________
391 @short dispatch an alias
392 @descr We use this alias to locate a job inside the configuration
393 and execute it. Further we inform the given listener about the results.
396 the alias name of the configured job
399 optional arguments for this request
403 an interested listener for possible results of this operation
405 void JobDispatch::impl_dispatchAlias( /*IN*/ const ::rtl::OUString
& sAlias
,
406 /*IN*/ const css::uno::Sequence
< css::beans::PropertyValue
>& lArgs
,
407 /*IN*/ const css::uno::Reference
< css::frame::XDispatchResultListener
>& xListener
)
410 ReadGuard
aReadLock(m_aLock
);
412 JobData
aCfg(m_xSMGR
);
413 aCfg
.setAlias(sAlias
);
414 aCfg
.setEnvironment(JobData::E_DISPATCH
);
417 Jobs implements interfaces and dies by ref count!
418 And freeing of such uno object is done by uno itself.
419 So we have to use dynamic memory everytimes.
421 Job
* pJob
= new Job(m_xSMGR
, m_xFrame
);
422 css::uno::Reference
< css::uno::XInterface
> xJob(static_cast< ::cppu::OWeakObject
* >(pJob
), css::uno::UNO_QUERY
);
423 pJob
->setJobData(aCfg
);
428 css::uno::Reference
< css::frame::XDispatchResultListener
> xThis( static_cast< ::cppu::OWeakObject
* >(this), css::uno::UNO_QUERY
);
430 // Special mode for listener.
431 // We dont notify it directly here. We delegate that
432 // to the job implementation. But we must set ourself there too.
433 // Because this job must fake the source adress of the event.
434 // Otherwhise the listener may will ignore it.
436 pJob
->setDispatchResultFake(xListener
, xThis
);
437 pJob
->execute(Converter::convert_seqPropVal2seqNamedVal(lArgs
));
440 //________________________________
442 @short implementation of XDispatch::dispatch()
443 @descr Because the methods dispatch() and dispatchWithNotification() are different in her parameters
444 only, we can forward this request to dispatchWithNotification() by using an empty listener!
447 describe the job(s), which should be started
450 optional arguments for this request
452 @see dispatchWithNotification()
454 void SAL_CALL
JobDispatch::dispatch( /*IN*/ const css::util::URL
& aURL
,
455 /*IN*/ const css::uno::Sequence
< css::beans::PropertyValue
>& lArgs
) throw(css::uno::RuntimeException
)
457 dispatchWithNotification(aURL
, lArgs
, css::uno::Reference
< css::frame::XDispatchResultListener
>());
460 //________________________________
464 void SAL_CALL
JobDispatch::addStatusListener( /*IN*/ const css::uno::Reference
< css::frame::XStatusListener
>&,
465 /*IN*/ const css::util::URL
& ) throw(css::uno::RuntimeException
)
469 //________________________________
473 void SAL_CALL
JobDispatch::removeStatusListener( /*IN*/ const css::uno::Reference
< css::frame::XStatusListener
>&,
474 /*IN*/ const css::util::URL
& ) throw(css::uno::RuntimeException
)
478 } // namespace framework