1/*
2 * Copyright 2017, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Brian Hill <supernova@tycho.email>
7 */
8
9
10#include "UpdateAction.h"
11
12#include <Application.h>
13#include <Catalog.h>
14#include <package/manager/Exceptions.h>
15
16
17#undef B_TRANSLATION_CONTEXT
18#define B_TRANSLATION_CONTEXT "UpdateAction"
19
20
21using namespace BPackageKit;
22//using namespace BPackageKit::BPrivate;
23using namespace BPackageKit::BManager::BPrivate;
24
25
26UpdateAction::UpdateAction(bool verbose)
27	:
28	fVerbose(verbose)
29{
30	fUpdateManager = new(std::nothrow)
31		UpdateManager(B_PACKAGE_INSTALLATION_LOCATION_SYSTEM, verbose);
32}
33
34
35UpdateAction::~UpdateAction()
36{
37	delete fUpdateManager;
38}
39
40
41status_t
42UpdateAction::Perform(update_type action_request)
43{
44	try {
45		fUpdateManager->CheckNetworkConnection();
46
47		update_type action = action_request;
48		// Prompt the user if needed
49		if (action == USER_SELECTION_NEEDED)
50			action = fUpdateManager->GetUpdateType();
51
52		if (action == CANCEL_UPDATE)
53			throw BAbortedByUserException();
54		else if (action <= INVALID_SELECTION || action >= UPDATE_TYPE_END)
55			throw BException(B_TRANSLATE(
56				"Invalid update type, cannot continue with updates"));
57
58		fUpdateManager->Init(BPackageManager::B_ADD_INSTALLED_REPOSITORIES
59			| BPackageManager::B_ADD_REMOTE_REPOSITORIES
60			| BPackageManager::B_REFRESH_REPOSITORIES);
61		fUpdateManager->CheckRepositories();
62
63//		fUpdateManager->SetDebugLevel(1);
64		if(action == UPDATE) {
65			// These values indicate that all updates should be installed
66			int packageCount = 0;
67			const char* const packages = "";
68			fUpdateManager->Update(&packages, packageCount);
69		} else if (action == FULLSYNC)
70			fUpdateManager->FullSync();
71		else
72			// Should not happen but just in case
73			throw BException(B_TRANSLATE(
74				"Invalid update type, cannot continue with updates"));
75
76	} catch (BFatalErrorException ex) {
77		fUpdateManager->FinalUpdate(B_TRANSLATE("Updates did not complete"),
78			ex.Message());
79		return ex.Error();
80	} catch (BAbortedByUserException ex) {
81		if (fVerbose)
82			fprintf(stderr, "Updates aborted by user: %s\n",
83				ex.Message().String());
84		// No need for a final message since user initiated cancel request
85		be_app->PostMessage(kMsgFinalQuit);
86		return B_OK;
87	} catch (BNothingToDoException ex) {
88		if (fVerbose)
89			fprintf(stderr, "Nothing to do while updating packages : %s\n",
90				ex.Message().String());
91		fUpdateManager->FinalUpdate(B_TRANSLATE("No updates available"),
92			B_TRANSLATE("There were no updates found."));
93		return B_OK;
94	} catch (BException ex) {
95		if (fVerbose)
96			fprintf(stderr, B_TRANSLATE(
97				"Exception occurred while updating packages : %s\n"),
98				ex.Message().String());
99		fUpdateManager->FinalUpdate(B_TRANSLATE("Updates did not complete"),
100			ex.Message());
101		return B_ERROR;
102	}
103
104	return B_OK;
105}
106