xref: /haiku/src/kits/package/DownloadFileRequest.cpp (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 /*
2  * Copyright 2013, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Ingo Weinhold <ingo_weinhold@gmx.de>
7  */
8 
9 
10 #include <package/DownloadFileRequest.h>
11 
12 #include <package/ValidateChecksumJob.h>
13 
14 #include "FetchFileJob.h"
15 
16 
17 namespace BPackageKit {
18 
19 
20 using namespace BPrivate;
21 
22 
23 DownloadFileRequest::DownloadFileRequest(const BContext& context,
24 	const BString& fileURL, const BEntry& targetEntry, const BString& checksum)
25 	:
26 	inherited(context),
27 	fFileURL(fileURL),
28 	fTargetEntry(targetEntry),
29 	fChecksum(checksum)
30 {
31 	if (fInitStatus == B_OK) {
32 		if (fFileURL.IsEmpty())
33 			fInitStatus = B_BAD_VALUE;
34 		else
35 			fInitStatus = targetEntry.InitCheck();
36 	}
37 }
38 
39 
40 DownloadFileRequest::~DownloadFileRequest()
41 {
42 }
43 
44 
45 status_t
46 DownloadFileRequest::CreateInitialJobs()
47 {
48 	status_t error = InitCheck();
49 	if (error != B_OK)
50 		return B_NO_INIT;
51 
52 	// create the download job
53 	FetchFileJob* fetchJob = new (std::nothrow) FetchFileJob(fContext,
54 		BString("Downloading ") << fFileURL, fFileURL, fTargetEntry);
55 	if (fetchJob == NULL)
56 		return B_NO_MEMORY;
57 
58 	if ((error = QueueJob(fetchJob)) != B_OK) {
59 		delete fetchJob;
60 		return error;
61 	}
62 
63 	// create the checksum validation job
64 	if (fChecksum.IsEmpty())
65 		return B_OK;
66 
67 	ValidateChecksumJob* validateJob = new (std::nothrow) ValidateChecksumJob(
68 		fContext, BString("Validating checksum for ") << fFileURL,
69 		new (std::nothrow) StringChecksumAccessor(fChecksum),
70 		new (std::nothrow) GeneralFileChecksumAccessor(fTargetEntry, true));
71 
72 	if (validateJob == NULL)
73 		return B_NO_MEMORY;
74 
75 	if ((error = QueueJob(validateJob)) != B_OK) {
76 		delete validateJob;
77 		return error;
78 	}
79 
80 	return B_OK;
81 }
82 
83 
84 }	// namespace BPackageKit
85