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