1 /* 2 * Copyright 2011-2015, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _SUPPORT_JOB_H_ 6 #define _SUPPORT_JOB_H_ 7 8 9 #include <ObjectList.h> 10 #include <String.h> 11 12 13 namespace BSupportKit { 14 15 16 class BJob; 17 18 19 struct BJobStateListener { 20 virtual ~BJobStateListener(); 21 22 // these default implementations do nothing 23 virtual void JobStarted(BJob* job); 24 virtual void JobProgress(BJob* job); 25 virtual void JobSucceeded(BJob* job); 26 virtual void JobFailed(BJob* job); 27 virtual void JobAborted(BJob* job); 28 }; 29 30 31 enum BJobState { 32 B_JOB_STATE_WAITING_TO_RUN, 33 B_JOB_STATE_STARTED, 34 B_JOB_STATE_IN_PROGRESS, 35 B_JOB_STATE_SUCCEEDED, 36 B_JOB_STATE_FAILED, 37 B_JOB_STATE_ABORTED, 38 }; 39 40 41 class BJob { 42 public: 43 BJob(const BString& title); 44 virtual ~BJob(); 45 46 status_t InitCheck() const; 47 48 virtual status_t Run(); 49 50 const BString& Title() const; 51 BJobState State() const; 52 status_t Result() const; 53 const BString& ErrorString() const; 54 55 uint32 TicketNumber() const; 56 57 status_t AddStateListener(BJobStateListener* listener); 58 status_t RemoveStateListener( 59 BJobStateListener* listener); 60 61 bool IsRunnable() const; 62 status_t AddDependency(BJob* job); 63 status_t RemoveDependency(BJob* job); 64 int32 CountDependencies() const; 65 66 BJob* DependantJobAt(int32 index) const; 67 68 class Private; 69 70 protected: 71 virtual status_t Execute() = 0; 72 virtual void Cleanup(status_t jobResult); 73 74 void SetErrorString(const BString&); 75 76 void NotifyStateListeners(); 77 78 private: 79 friend class Private; 80 81 void _SetTicketNumber(uint32 ticketNumber); 82 void _ClearTicketNumber(); 83 84 private: 85 status_t fInitStatus; 86 BString fTitle; 87 88 BJobState fState; 89 status_t fResult; 90 BString fErrorString; 91 92 uint32 fTicketNumber; 93 94 typedef BObjectList<BJob> JobList; 95 JobList fDependencies; 96 JobList fDependantJobs; 97 98 typedef BObjectList<BJobStateListener> StateListenerList; 99 StateListenerList fStateListeners; 100 }; 101 102 103 } // namespace BSupportKit 104 105 106 #endif // _SUPPORT_JOB_H_ 107