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