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