xref: /haiku/src/kits/package/DownloadFileRequest.cpp (revision e81a954787e50e56a7f06f72705b7859b6ab06d1)
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/FetchFileJob.h>
13 #include <package/ValidateChecksumJob.h>
14 
15 
16 namespace BPackageKit {
17 
18 
19 using namespace BPrivate;
20 
21 
22 DownloadFileRequest::DownloadFileRequest(const BContext& context,
23 	const BString& fileURL, const BEntry& targetEntry, const BString& checksum)
24 	:
25 	inherited(context),
26 	fFileURL(fileURL),
27 	fTargetEntry(targetEntry),
28 	fChecksum(checksum)
29 {
30 	if (fInitStatus == B_OK) {
31 		if (fFileURL.IsEmpty())
32 			fInitStatus = B_BAD_VALUE;
33 		else
34 			fInitStatus = targetEntry.InitCheck();
35 	}
36 }
37 
38 
39 DownloadFileRequest::~DownloadFileRequest()
40 {
41 }
42 
43 
44 status_t
45 DownloadFileRequest::CreateInitialJobs()
46 {
47 	status_t error = InitCheck();
48 	if (error != B_OK)
49 		return B_NO_INIT;
50 
51 	// create the download job
52 	FetchFileJob* fetchJob = new (std::nothrow) FetchFileJob(fContext,
53 		BString("Downloading ") << fFileURL, fFileURL, fTargetEntry);
54 	if (fetchJob == NULL)
55 		return B_NO_MEMORY;
56 
57 	if ((error = QueueJob(fetchJob)) != B_OK) {
58 		delete fetchJob;
59 		return error;
60 	}
61 
62 	// create the checksum validation job
63 	if (fChecksum.IsEmpty())
64 		return B_OK;
65 
66 	ValidateChecksumJob* validateJob = new (std::nothrow) ValidateChecksumJob(
67 		fContext, BString("Validating checksum for ") << fFileURL,
68 		new (std::nothrow) StringChecksumAccessor(fChecksum),
69 		new (std::nothrow) GeneralFileChecksumAccessor(fTargetEntry, true));
70 
71 	if (validateJob == NULL)
72 		return B_NO_MEMORY;
73 
74 	if ((error = QueueJob(validateJob)) != B_OK) {
75 		delete validateJob;
76 		return error;
77 	}
78 
79 	return B_OK;
80 }
81 
82 
83 }	// namespace BPackageKit
84