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