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
18namespace BPackageKit {
19
20namespace BPrivate {
21
22
23ActivateRepositoryConfigJob::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
36ActivateRepositoryConfigJob::~ActivateRepositoryConfigJob()
37{
38}
39
40
41status_t
42ActivateRepositoryConfigJob::Execute()
43{
44	BRepositoryInfo repoInfo(fArchivedRepoInfoEntry);
45	status_t result = repoInfo.InitCheck();
46	if (result != B_OK)
47		return result;
48
49	result = fTargetEntry.SetTo(&fTargetDirectory, repoInfo.Name().String());
50	if (result != B_OK)
51		return result;
52
53	if (fTargetEntry.Exists()) {
54		BString description = BString("A repository configuration for ")
55			<< repoInfo.Name() << " already exists.";
56		BString question("overwrite?");
57		bool yes = fContext.DecisionProvider().YesNoDecisionNeeded(
58			description, question, "yes", "no", "no");
59		if (!yes) {
60			fTargetEntry.Unset();
61			return B_CANCELED;
62		}
63	}
64
65	// create and store the configuration (injecting the BaseURL that was
66	// actually used).
67	BRepositoryConfig repoConfig;
68	repoConfig.SetName(repoInfo.Name());
69	repoConfig.SetBaseURL(fRepositoryBaseURL);
70	repoConfig.SetIdentifier(repoInfo.Identifier());
71	repoConfig.SetPriority(repoInfo.Priority());
72
73	if (fRepositoryBaseURL.IsEmpty()) {
74		repoConfig.SetBaseURL(repoInfo.BaseURL());
75	} else {
76		repoConfig.SetBaseURL(fRepositoryBaseURL);
77	}
78
79	if ((result = repoConfig.Store(fTargetEntry)) != B_OK)
80		return result;
81
82	// store name of activated repository as result
83	fRepositoryName = repoConfig.Name();
84
85	return B_OK;
86}
87
88
89void
90ActivateRepositoryConfigJob::Cleanup(status_t jobResult)
91{
92	if (jobResult != B_OK && State() != BSupportKit::B_JOB_STATE_ABORTED
93		&& fTargetEntry.InitCheck() == B_OK)
94		fTargetEntry.Remove();
95}
96
97
98const BString&
99ActivateRepositoryConfigJob::RepositoryName() const
100{
101	return fRepositoryName;
102}
103
104
105}	// namespace BPrivate
106
107}	// namespace BPackageKit
108