xref: /haiku/src/kits/package/Request.cpp (revision 9f81ca838ce7b92b5689e57d3f86765db4705a7b)
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 using BPrivate::JobQueue;
22 
23 
24 BRequest::BRequest(const BContext& context)
25 	:
26 	fContext(context),
27 	fJobQueue(new (std::nothrow) JobQueue())
28 {
29 	fInitStatus = fJobQueue == NULL ? B_NO_MEMORY : B_OK;
30 }
31 
32 
33 BRequest::~BRequest()
34 {
35 }
36 
37 
38 status_t
39 BRequest::InitCheck() const
40 {
41 	return fInitStatus;
42 }
43 
44 
45 BJob*
46 BRequest::PopRunnableJob()
47 {
48 	if (fJobQueue == NULL)
49 		return NULL;
50 
51 	return fJobQueue->Pop();
52 }
53 
54 
55 status_t
56 BRequest::Process(bool failIfCanceledOnly)
57 {
58 	status_t error = InitCheck();
59 	if (error != B_OK)
60 		return error;
61 
62 	error = CreateInitialJobs();
63 	if (error != B_OK)
64 		return error;
65 
66 	while (BJob* job = PopRunnableJob()) {
67 		error = job->Run();
68 		delete job;
69 		if (error != B_OK) {
70 			if (!failIfCanceledOnly || error == B_CANCELED)
71 				return error;
72 		}
73 	}
74 
75 	return B_OK;
76 }
77 
78 
79 status_t
80 BRequest::QueueJob(BJob* job)
81 {
82 	if (fJobQueue == NULL)
83 		return B_NO_INIT;
84 
85 	job->AddStateListener(this);
86 	job->AddStateListener(&fContext.JobStateListener());
87 
88 	return fJobQueue->AddJob(job);
89 }
90 
91 
92 }	// namespace BPackageKit
93