xref: /haiku/src/kits/package/ActivateRepositoryConfigJob.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright 2011-2018, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Oliver Tappe <zooey@hirschkaefer.de>
7  *		Andrew Lindesay <apl@lindesay.co.nz>
8  */
9 
10 
11 #include <package/ActivateRepositoryConfigJob.h>
12 
13 #include <package/Context.h>
14 #include <package/RepositoryConfig.h>
15 #include <package/RepositoryInfo.h>
16 
17 
18 namespace BPackageKit {
19 
20 namespace BPrivate {
21 
22 
23 ActivateRepositoryConfigJob::ActivateRepositoryConfigJob(
24 	const BContext& context, const BString& title,
25 	const BEntry& archivedRepoInfoEntry, const BString& repositoryBaseURL,
26 	const BDirectory& targetDirectory)
27 	:
28 	inherited(context, title),
29 	fArchivedRepoInfoEntry(archivedRepoInfoEntry),
30 	fRepositoryBaseURL(repositoryBaseURL),
31 	fTargetDirectory(targetDirectory)
32 {
33 }
34 
35 
36 ActivateRepositoryConfigJob::~ActivateRepositoryConfigJob()
37 {
38 }
39 
40 
41 status_t
42 ActivateRepositoryConfigJob::Execute()
43 {
44 	BRepositoryInfo repoInfo(fArchivedRepoInfoEntry);
45 	status_t result = repoInfo.InitCheck();
46 	if (result != B_OK)
47 		return result;
48 
49 	fTargetEntry.SetTo(&fTargetDirectory, repoInfo.Name().String());
50 	if (fTargetEntry.Exists()) {
51 		BString description = BString("A repository configuration for ")
52 			<< repoInfo.Name() << " already exists.";
53 		BString question("overwrite?");
54 		bool yes = fContext.DecisionProvider().YesNoDecisionNeeded(
55 			description, question, "yes", "no", "no");
56 		if (!yes) {
57 			fTargetEntry.Unset();
58 			return B_CANCELED;
59 		}
60 	}
61 
62 	// create and store the configuration (injecting the BaseURL that was
63 	// actually used).
64 	BRepositoryConfig repoConfig;
65 	repoConfig.SetName(repoInfo.Name());
66 	repoConfig.SetBaseURL(fRepositoryBaseURL);
67 	repoConfig.SetURL(repoInfo.URL());
68 	repoConfig.SetPriority(repoInfo.Priority());
69 
70 	if (fRepositoryBaseURL.IsEmpty()) {
71 		repoConfig.SetBaseURL(repoInfo.BaseURL());
72 	} else {
73 		repoConfig.SetBaseURL(fRepositoryBaseURL);
74 	}
75 
76 	if ((result = repoConfig.Store(fTargetEntry)) != B_OK)
77 		return result;
78 
79 	// store name of activated repository as result
80 	fRepositoryName = repoConfig.Name();
81 
82 	return B_OK;
83 }
84 
85 
86 void
87 ActivateRepositoryConfigJob::Cleanup(status_t jobResult)
88 {
89 	if (jobResult != B_OK && State() != BSupportKit::B_JOB_STATE_ABORTED
90 		&& fTargetEntry.InitCheck() == B_OK)
91 		fTargetEntry.Remove();
92 }
93 
94 
95 const BString&
96 ActivateRepositoryConfigJob::RepositoryName() const
97 {
98 	return fRepositoryName;
99 }
100 
101 
102 }	// namespace BPrivate
103 
104 }	// namespace BPackageKit
105