xref: /haiku/src/kits/package/ActivateRepositoryConfigJob.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/ActivateRepositoryConfigJob.h>
11 
12 #include <package/Context.h>
13 #include <package/RepositoryConfig.h>
14 #include <package/RepositoryInfo.h>
15 
16 
17 namespace BPackageKit {
18 
19 namespace BPrivate {
20 
21 
22 ActivateRepositoryConfigJob::ActivateRepositoryConfigJob(
23 	const BContext& context, const BString& title,
24 	const BEntry& archivedRepoInfoEntry, const BString& repositoryBaseURL,
25 	const BDirectory& targetDirectory)
26 	:
27 	inherited(context, title),
28 	fArchivedRepoInfoEntry(archivedRepoInfoEntry),
29 	fRepositoryBaseURL(repositoryBaseURL),
30 	fTargetDirectory(targetDirectory)
31 {
32 }
33 
34 
35 ActivateRepositoryConfigJob::~ActivateRepositoryConfigJob()
36 {
37 }
38 
39 
40 status_t
41 ActivateRepositoryConfigJob::Execute()
42 {
43 	BRepositoryInfo repoInfo(fArchivedRepoInfoEntry);
44 	status_t result = repoInfo.InitCheck();
45 	if (result != B_OK)
46 		return result;
47 
48 	fTargetEntry.SetTo(&fTargetDirectory, repoInfo.Name().String());
49 	if (fTargetEntry.Exists()) {
50 		BString description = BString("A repository configuration for ")
51 			<< repoInfo.Name() << " already exists.";
52 		BString question("overwrite?");
53 		bool yes = fContext.DecisionProvider().YesNoDecisionNeeded(
54 			description, question, "yes", "no", "no");
55 		if (!yes) {
56 			fTargetEntry.Unset();
57 			return B_CANCELED;
58 		}
59 	}
60 
61 	// create and store the configuration (injecting the BaseURL that was
62 	// actually used)
63 	BRepositoryConfig repoConfig;
64 	repoConfig.SetName(repoInfo.Name());
65 	repoConfig.SetBaseURL(fRepositoryBaseURL);
66 	repoConfig.SetPriority(repoInfo.Priority());
67 	if ((result = repoConfig.Store(fTargetEntry)) != B_OK)
68 		return result;
69 
70 	// store name of activated repository as result
71 	fRepositoryName = repoConfig.Name();
72 
73 	return B_OK;
74 }
75 
76 
77 void
78 ActivateRepositoryConfigJob::Cleanup(status_t jobResult)
79 {
80 	if (jobResult != B_OK && State() != BSupportKit::B_JOB_STATE_ABORTED
81 		&& fTargetEntry.InitCheck() == B_OK)
82 		fTargetEntry.Remove();
83 }
84 
85 
86 const BString&
87 ActivateRepositoryConfigJob::RepositoryName() const
88 {
89 	return fRepositoryName;
90 }
91 
92 
93 }	// namespace BPrivate
94 
95 }	// namespace BPackageKit
96