xref: /haiku/src/kits/package/Request.cpp (revision 204dee708a999d5a71d0cb9497650ee7cef85d0a)
1 /*
2  * Copyright 2011, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Oliver Tappe <zooey@hirschkaefer.de>
7  */
8 
9 
10 #include <package/Request.h>
11 
12 #include <new>
13 
14 #include <package/Context.h>
15 #include <package/JobQueue.h>
16 
17 
18 namespace BPackageKit {
19 
20 
21 BRequest::BRequest(const BContext& context)
22 	:
23 	fContext(context),
24 	fJobQueue(new (std::nothrow) JobQueue())
25 {
26 	fInitStatus = fJobQueue == NULL ? B_NO_MEMORY : B_OK;
27 }
28 
29 
30 BRequest::~BRequest()
31 {
32 }
33 
34 
35 status_t
36 BRequest::InitCheck() const
37 {
38 	return fInitStatus;
39 }
40 
41 
42 BJob*
43 BRequest::PopRunnableJob()
44 {
45 	if (fJobQueue == NULL)
46 		return NULL;
47 
48 	return fJobQueue->Pop();
49 }
50 
51 
52 status_t
53 BRequest::Process(bool failIfCanceledOnly)
54 {
55 	status_t error = InitCheck();
56 	if (error != B_OK)
57 		return error;
58 
59 	error = CreateInitialJobs();
60 	if (error != B_OK)
61 		return error;
62 
63 	while (BJob* job = PopRunnableJob()) {
64 		error = job->Run();
65 		delete job;
66 		if (error != B_OK) {
67 			if (!failIfCanceledOnly || error == B_CANCELED)
68 				return error;
69 		}
70 	}
71 
72 	return B_OK;
73 }
74 
75 
76 status_t
77 BRequest::QueueJob(BJob* job)
78 {
79 	if (fJobQueue == NULL)
80 		return B_NO_INIT;
81 
82 	job->AddStateListener(this);
83 	job->AddStateListener(&fContext.JobStateListener());
84 
85 	return fJobQueue->AddJob(job);
86 }
87 
88 
89 }	// namespace BPackageKit
90