xref: /haiku/src/kits/package/FetchFileJob.cpp (revision 5bd0fbd13a1e832f91643aaa921fbc0879abd518)
1 /*
2  * Copyright 2011-2015, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler <axeld@pinc-software.de>
7  *		Rene Gollent <rene@gollent.com>
8  *		Oliver Tappe <zooey@hirschkaefer.de>
9  */
10 
11 
12 #include "FetchFileJob.h"
13 
14 #include <stdio.h>
15 #include <sys/wait.h>
16 
17 #include <Path.h>
18 
19 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
20 #	include <UrlRequest.h>
21 #	include <UrlProtocolRoster.h>
22 #endif
23 
24 
25 namespace BPackageKit {
26 
27 namespace BPrivate {
28 
29 
30 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
31 
32 FetchFileJob::FetchFileJob(const BContext& context, const BString& title,
33 	const BString& fileURL, const BEntry& targetEntry)
34 	:
35 	inherited(context, title),
36 	fFileURL(fileURL),
37 	fTargetEntry(targetEntry),
38 	fTargetFile(&targetEntry, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY),
39 	fDownloadProgress(0.0)
40 {
41 }
42 
43 
44 FetchFileJob::~FetchFileJob()
45 {
46 }
47 
48 
49 float
50 FetchFileJob::DownloadProgress() const
51 {
52 	return fDownloadProgress;
53 }
54 
55 
56 const char*
57 FetchFileJob::DownloadURL() const
58 {
59 	return fFileURL.String();
60 }
61 
62 
63 const char*
64 FetchFileJob::DownloadFileName() const
65 {
66 	return fTargetEntry.Name();
67 }
68 
69 
70 off_t
71 FetchFileJob::DownloadBytes() const
72 {
73 	return fBytes;
74 }
75 
76 
77 off_t
78 FetchFileJob::DownloadTotalBytes() const
79 {
80 	return fTotalBytes;
81 }
82 
83 
84 status_t
85 FetchFileJob::Execute()
86 {
87 	status_t result = fTargetFile.InitCheck();
88 	if (result != B_OK)
89 		return result;
90 
91 	BUrlRequest* request = BUrlProtocolRoster::MakeRequest(fFileURL.String(),
92 		this);
93 	if (request == NULL)
94 		return B_BAD_VALUE;
95 
96 	thread_id thread = request->Run();
97 	wait_for_thread(thread, NULL);
98 
99 	if (fSuccess == true)
100 		return B_OK;
101 	else
102 		return B_ERROR;
103 
104 	//TODO: More detailed error codes?
105 #if 0
106 	const BHttpResult& outResult = dynamic_cast<const BHttpResult&>
107 		(request->Result());
108 
109 	switch (outResult.StatusCode()) {
110 		case B_HTTP_STATUS_OK:
111 			return B_OK;
112 		case B_HTTP_STATUS_PARTIAL_CONTENT:
113 			return B_PARTIAL_READ;
114 		case B_HTTP_STATUS_REQUEST_TIMEOUT:
115 		case B_HTTP_STATUS_GATEWAY_TIMEOUT:
116 			return B_TIMED_OUT;
117 		case B_HTTP_STATUS_NOT_IMPLEMENTED:
118 		case B_HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE:
119 			return B_NOT_SUPPORTED;
120 		case B_HTTP_STATUS_UNAUTHORIZED:
121 			return B_PERMISSION_DENIED;
122 		case B_HTTP_STATUS_FORBIDDEN:
123 		case B_HTTP_STATUS_METHOD_NOT_ALLOWED:
124 		case B_HTTP_STATUS_NOT_ACCEPTABLE:
125 			return B_NOT_ALLOWED;
126 		case B_HTTP_STATUS_NOT_FOUND:
127 			return B_NAME_NOT_FOUND;
128 		case B_HTTP_STATUS_BAD_GATEWAY:
129 			return B_BAD_DATA;
130 		default:
131 			return B_ERROR;
132 	}
133 #endif
134 }
135 
136 
137 void
138 FetchFileJob::DataReceived(BUrlRequest*, const char* data, off_t position,
139 	ssize_t size)
140 {
141 	fTargetFile.WriteAt(position, data, size);
142 }
143 
144 
145 void
146 FetchFileJob::DownloadProgress(BUrlRequest*, ssize_t bytesReceived,
147 	ssize_t bytesTotal)
148 {
149 	if (bytesTotal != 0) {
150 		fBytes = bytesReceived;
151 		fTotalBytes = bytesTotal;
152 		fDownloadProgress = (float)bytesReceived/bytesTotal;
153 		NotifyStateListeners();
154 	}
155 }
156 
157 
158 void
159 FetchFileJob::RequestCompleted(BUrlRequest*, bool success)
160 {
161 	fSuccess = success;
162 }
163 
164 
165 void
166 FetchFileJob::Cleanup(status_t jobResult)
167 {
168 	if (jobResult != B_OK)
169 		fTargetEntry.Remove();
170 }
171 
172 
173 #else // HAIKU_TARGET_PLATFORM_HAIKU
174 
175 
176 FetchFileJob::FetchFileJob(const BContext& context, const BString& title,
177 	const BString& fileURL, const BEntry& targetEntry)
178 	:
179 	inherited(context, title),
180 	fFileURL(fileURL),
181 	fTargetEntry(targetEntry),
182 	fTargetFile(&targetEntry, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY),
183 	fDownloadProgress(0.0)
184 {
185 }
186 
187 
188 FetchFileJob::~FetchFileJob()
189 {
190 }
191 
192 
193 float
194 FetchFileJob::DownloadProgress() const
195 {
196 	return fDownloadProgress;
197 }
198 
199 
200 const char*
201 FetchFileJob::DownloadURL() const
202 {
203 	return fFileURL.String();
204 }
205 
206 
207 const char*
208 FetchFileJob::DownloadFileName() const
209 {
210 	return fTargetEntry.Name();
211 }
212 
213 
214 off_t
215 FetchFileJob::DownloadBytes() const
216 {
217 	return fBytes;
218 }
219 
220 
221 off_t
222 FetchFileJob::DownloadTotalBytes() const
223 {
224 	return fTotalBytes;
225 }
226 
227 
228 status_t
229 FetchFileJob::Execute()
230 {
231 	return B_UNSUPPORTED;
232 }
233 
234 
235 void
236 FetchFileJob::Cleanup(status_t jobResult)
237 {
238 	if (jobResult != B_OK)
239 		fTargetEntry.Remove();
240 }
241 
242 
243 #endif // HAIKU_TARGET_PLATFORM_HAIKU
244 
245 }	// namespace BPrivate
246 
247 }	// namespace BPackageKit
248