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