xref: /aoo41x/main/framework/inc/jobs/job.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef __FRAMEWORK_JOBS_JOB_HXX_
29 #define __FRAMEWORK_JOBS_JOB_HXX_
30 
31 //_______________________________________
32 // my own includes
33 
34 #include <jobs/jobresult.hxx>
35 #include <jobs/jobdata.hxx>
36 #include <threadhelp/threadhelpbase.hxx>
37 #include <macros/debug.hxx>
38 #include <macros/xinterface.hxx>
39 #include <macros/xtypeprovider.hxx>
40 #include <stdtypes.h>
41 #include <general.h>
42 
43 //_______________________________________
44 // interface includes
45 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46 #include <com/sun/star/lang/XTypeProvider.hpp>
47 #include <com/sun/star/frame/XFrame.hpp>
48 #include <com/sun/star/frame/XDesktop.hpp>
49 #include <com/sun/star/frame/XDispatchResultListener.hpp>
50 #include <com/sun/star/task/XJobListener.hpp>
51 #include <com/sun/star/util/XCloseListener.hpp>
52 #include <com/sun/star/frame/DispatchResultEvent.hpp>
53 
54 //_______________________________________
55 // other includes
56 #include <cppuhelper/weak.hxx>
57 #include <rtl/ustring.hxx>
58 
59 //_______________________________________
60 // namespace
61 
62 namespace framework{
63 
64 //_______________________________________
65 // public const
66 
67 //_______________________________________
68 // definitions
69 
70 /**
71     @short  it represent a job; execute it and control it's lifetime
72 
73     @descr  This implemetation can be used to wrapp jobs, execute it
74             synchronously or asynchronous, control it's lifetime
75             and differe between jobs with and without configuration.
76  */
77 class Job : public  css::lang::XTypeProvider
78           , public  css::task::XJobListener
79           , public  css::frame::XTerminateListener
80           , public  css::util::XCloseListener
81           , private ThreadHelpBase
82           , public  ::cppu::OWeakObject
83 {
84     //___________________________________
85     // structs
86 
87     private:
88 
89     /** different possible states for the internal wrapped job.
90         It can be started, stopped by a queryClosing() request or
91         disposed() by a notifyClosing() request ...
92      */
93     enum ERunState
94     {
95         E_NEW,
96         E_RUNNING,
97         E_STOPPED_OR_FINISHED,
98         E_DISPOSED
99     };
100 
101     //___________________________________
102     // member
103 
104     private:
105 
106         /**
107             hold all neccessary informations about this job.
108             It can be used for both modes: with and without configuration.
109          */
110         JobData m_aJobCfg;
111 
112         /**
113             We need it to create own services on demand.
114          */
115         css::uno::Reference< css::lang::XMultiServiceFactory > m_xSMGR;
116 
117         /**
118             Hold the (may asynchronous) job alive.
119          */
120         css::uno::Reference< css::uno::XInterface > m_xJob;
121 
122         /**
123             Used to wait for finishing of asynchronous started jobs.
124          */
125         ::osl::Condition m_aAsyncWait;
126 
127         /**
128             For some special cases we must know the environment, in which
129             this job runs. Means the frame inside which we may was triggered.
130             We use it too, to listen for closing events of this ressource.
131 
132             Please note: If m_xFrame is set - m_xModel should be NULL.
133             Only one environment can be supported realy.
134          */
135         css::uno::Reference< css::frame::XFrame > m_xFrame;
136 
137         /**
138             For some special cases we must know the environment, in which
139             this job runs. Means the document inside which we may was triggered.
140             We use it too, to listen for closing events of this ressource.
141 
142             Please note: If m_xModel is set - m_xFrame should be NULL.
143             Only one environment can be supported realy.
144          */
145         css::uno::Reference< css::frame::XModel > m_xModel;
146 
147         /**
148             We are registered at this instance to listen for office shutdown events.
149             It's neccessary supress it (if possible) or to react in the right way.
150          */
151         css::uno::Reference< css::frame::XDesktop > m_xDesktop;
152 
153         /**
154             A job can return a dispatch result event after finishing its work.
155             We have to transport it to any outside interested listener then.
156             (see m_xResultSourceFake for further informations too!)
157          */
158         css::uno::Reference< css::frame::XDispatchResultListener > m_xResultListener;
159 
160         /**
161             We can't set ourself as source of a dispatch result event ... nor our job.
162             Because the listener (set as m_xResultListener) expect the original instance,
163             where it was registered. This original instance is the user of this class.
164             It must be set explicitly and will be used to fake the source of the event!
165          */
166         css::uno::Reference< css::uno::XInterface > m_xResultSourceFake;
167 
168         /**
169             Holds the state, if we are listen for desktop/frame or model closing events or not.
170             The used references are not realy enough to detect a valid listener connection.
171             Thats why we use this additional information here too.
172          */
173         sal_Bool m_bListenOnDesktop;
174         sal_Bool m_bListenOnFrame;
175         sal_Bool m_bListenOnModel;
176 
177         /**
178             In case we got a close request from our desktop/frame/model (on which we listen) ... and
179             the ownership was delivered there ... we have to close ourself and this object
180             in case the internal wrapped and running job finish his work.
181          */
182         sal_Bool m_bPendingCloseFrame;
183         sal_Bool m_bPendingCloseModel;
184 
185         /**
186             indicates in which state the internal job currently exist.
187 
188             We can use this information to throw any suitable veto exception
189             to prevent the environment against dieing or supress superflous dispose()
190             calls at the job.
191          */
192         ERunState m_eRunState;
193 
194     //___________________________________
195     // native interface
196 
197     public:
198 
199                  Job( const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR  ,
200                       const css::uno::Reference< css::frame::XFrame >&              xFrame );
201                  Job( const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR  ,
202                       const css::uno::Reference< css::frame::XModel >&              xModel );
203         virtual ~Job(                                                                      );
204 
205         void     setDispatchResultFake( const css::uno::Reference< css::frame::XDispatchResultListener >& xListener    ,
206                                         const css::uno::Reference< css::uno::XInterface >&                xSourceFake  );
207         void     setJobData           ( const JobData&                                                    aData        );
208         void     execute              ( const css::uno::Sequence< css::beans::NamedValue >&               lDynamicArgs );
209         void     die                  (                                                                                );
210 
211     private:
212 
213         css::uno::Sequence< css::beans::NamedValue > impl_generateJobArgs  ( const css::uno::Sequence< css::beans::NamedValue >& lDynamicArgs );
214         void                                         impl_reactForJobResult( const css::uno::Any&                                aResult      );
215         void                                         impl_startListening   (                                                                  );
216         void                                         impl_stopListening    (                                                                  );
217 
218     //___________________________________
219     // uno interface
220 
221     public:
222 
223         FWK_DECLARE_XINTERFACE
224         FWK_DECLARE_XTYPEPROVIDER
225 
226         // XJobListener
227         virtual void SAL_CALL jobFinished( const css::uno::Reference< css::task::XAsyncJob >& xJob,
228                                            const css::uno::Any&                               aResult ) throw(css::uno::RuntimeException);
229 
230         // XTerminateListener
231         virtual void SAL_CALL queryTermination ( const css::lang::EventObject& aEvent ) throw(css::frame::TerminationVetoException,
232                                                                                               css::uno::RuntimeException          );
233         virtual void SAL_CALL notifyTermination( const css::lang::EventObject& aEvent ) throw(css::uno::RuntimeException          );
234 
235         // XCloseListener
236         virtual void SAL_CALL queryClosing ( const css::lang::EventObject& aEvent         ,
237                                                    sal_Bool                bGetsOwnership ) throw(css::util::CloseVetoException,
238                                                                                                   css::uno::RuntimeException   );
239         virtual void SAL_CALL notifyClosing( const css::lang::EventObject& aEvent         ) throw(css::uno::RuntimeException   );
240 
241         // XEventListener
242         virtual void SAL_CALL disposing( const css::lang::EventObject& aEvent ) throw(css::uno::RuntimeException);
243 };
244 
245 } // namespace framework
246 
247 #endif // __FRAMEWORK_JOBS_JOB_HXX_
248