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