xref: /haiku/src/kits/package/AddRepositoryRequest.cpp (revision 15fb7d88e971c4d6c787c6a3a5c159afb1ebf77b)
1 /*
2  * Copyright 2011-2015, 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/AddRepositoryRequest.h>
11 
12 #include <Directory.h>
13 #include <JobQueue.h>
14 #include <Path.h>
15 
16 #include <package/ActivateRepositoryConfigJob.h>
17 #include <package/PackageRoster.h>
18 
19 #include "FetchFileJob.h"
20 
21 
22 namespace BPackageKit {
23 
24 
25 using namespace BPrivate;
26 
27 
28 AddRepositoryRequest::AddRepositoryRequest(const BContext& context,
29 	const BString& repositoryBaseURL, bool asUserRepository)
30 	:
31 	inherited(context),
32 	fRepositoryBaseURL(repositoryBaseURL),
33 	fAsUserRepository(asUserRepository),
34 	fActivateJob(NULL)
35 {
36 }
37 
38 
39 AddRepositoryRequest::~AddRepositoryRequest()
40 {
41 }
42 
43 
44 status_t
45 AddRepositoryRequest::CreateInitialJobs()
46 {
47 	status_t result = InitCheck();
48 	if (result != B_OK)
49 		return B_NO_INIT;
50 
51 	BEntry tempEntry;
52 	result = fContext.GetNewTempfile("repoinfo-", &tempEntry);
53 	if (result != B_OK)
54 		return result;
55 	BString repoInfoURL = BString(fRepositoryBaseURL) << "/" << "repo.info";
56 	FetchFileJob* fetchJob = new (std::nothrow) FetchFileJob(fContext,
57 		BString("Fetching repository info from ") << fRepositoryBaseURL,
58 		repoInfoURL, tempEntry);
59 	if (fetchJob == NULL)
60 		return B_NO_MEMORY;
61 	if ((result = QueueJob(fetchJob)) != B_OK) {
62 		delete fetchJob;
63 		return result;
64 	}
65 
66 	BPackageRoster roster;
67 	BPath targetRepoConfigPath;
68 	result = fAsUserRepository
69 		? roster.GetUserRepositoryConfigPath(&targetRepoConfigPath, true)
70 		: roster.GetCommonRepositoryConfigPath(&targetRepoConfigPath, true);
71 	if (result != B_OK)
72 		return result;
73 	BDirectory targetDirectory(targetRepoConfigPath.Path());
74 	ActivateRepositoryConfigJob* activateJob
75 		= new (std::nothrow) ActivateRepositoryConfigJob(fContext,
76 			BString("Activating repository config from ") << fRepositoryBaseURL,
77 			tempEntry, fRepositoryBaseURL, targetDirectory);
78 	if (activateJob == NULL)
79 		return B_NO_MEMORY;
80 	result = activateJob->AddDependency(fetchJob);
81 	if (result != B_OK)
82 		return result;
83 	if ((result = QueueJob(activateJob)) != B_OK) {
84 		delete activateJob;
85 		return result;
86 	}
87 	fActivateJob = activateJob;
88 
89 	return B_OK;
90 }
91 
92 
93 void
94 AddRepositoryRequest::JobSucceeded(BSupportKit::BJob* job)
95 {
96 	if (job == fActivateJob)
97 		fRepositoryName = fActivateJob->RepositoryName();
98 }
99 
100 
101 const BString&
102 AddRepositoryRequest::RepositoryName() const
103 {
104 	return fRepositoryName;
105 }
106 
107 
108 }	// namespace BPackageKit
109