xref: /haiku/src/apps/softwareupdater/UpdateAction.cpp (revision 73c2c7b4b5044a152e9d194364578c1c9a13d8c1)
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@warpmail.net>
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 
21 using namespace BPackageKit;
22 //using namespace BPackageKit::BPrivate;
23 using namespace BPackageKit::BManager::BPrivate;
24 
25 
26 UpdateAction::UpdateAction()
27 {
28 	fUpdateManager = new(std::nothrow)
29 		UpdateManager(B_PACKAGE_INSTALLATION_LOCATION_SYSTEM);
30 }
31 
32 
33 UpdateAction::~UpdateAction()
34 {
35 	delete fUpdateManager;
36 }
37 
38 
39 status_t
40 UpdateAction::Perform(update_type action_request)
41 {
42 	try {
43 		fUpdateManager->CheckNetworkConnection();
44 
45 		update_type action = action_request;
46 		// Prompt the user if needed
47 		if (action == USER_SELECTION_NEEDED)
48 			action = fUpdateManager->GetUpdateType();
49 
50 		if (action == CANCEL_UPDATE)
51 			throw BAbortedByUserException();
52 		else if (action <= INVALID_SELECTION || action >= UPDATE_TYPE_END)
53 			throw BException(B_TRANSLATE(
54 				"Invalid update type, cannot continue with updates"));
55 
56 		fUpdateManager->Init(BPackageManager::B_ADD_INSTALLED_REPOSITORIES
57 			| BPackageManager::B_ADD_REMOTE_REPOSITORIES
58 			| BPackageManager::B_REFRESH_REPOSITORIES);
59 
60 //		fUpdateManager->SetDebugLevel(1);
61 		if(action == UPDATE) {
62 			// These values indicate that all updates should be installed
63 			int packageCount = 0;
64 			const char* const packages = "";
65 			fUpdateManager->Update(&packages, packageCount);
66 		} else if (action == FULLSYNC)
67 			fUpdateManager->FullSync();
68 		else
69 			// Should not happen but just in case
70 			throw BException(B_TRANSLATE(
71 				"Invalid update type, cannot continue with updates"));
72 
73 	} catch (BFatalErrorException ex) {
74 		fUpdateManager->FinalUpdate(B_TRANSLATE("Updates did not complete"),
75 			ex.Message());
76 		return ex.Error();
77 	} catch (BAbortedByUserException ex) {
78 		fprintf(stderr, "Updates aborted by user: %s\n",
79 			ex.Message().String());
80 		// No need for a final message since user initiated cancel request
81 		be_app->PostMessage(kMsgFinalQuit);
82 		return B_OK;
83 	} catch (BNothingToDoException ex) {
84 		fprintf(stderr, "Nothing to do while updating packages : %s\n",
85 			ex.Message().String());
86 		fUpdateManager->FinalUpdate(B_TRANSLATE("No updates available"),
87 			B_TRANSLATE("There were no updates found."));
88 		return B_OK;
89 	} catch (BException ex) {
90 		fprintf(stderr, B_TRANSLATE(
91 				"Exception occurred while updating packages : %s\n"),
92 			ex.Message().String());
93 		fUpdateManager->FinalUpdate(B_TRANSLATE("Updates did not complete"),
94 			ex.Message());
95 		return B_ERROR;
96 	}
97 
98 	return B_OK;
99 }
100