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