xref: /haiku/src/apps/softwareupdater/UpdateAction.cpp (revision ad7783e44df140933511f09e6750f2de4b3ba608)
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 <Alert.h>
13 #include <Application.h>
14 #include <Catalog.h>
15 #include <package/manager/Exceptions.h>
16 
17 #include "constants.h"
18 
19 
20 #undef B_TRANSLATION_CONTEXT
21 #define B_TRANSLATION_CONTEXT "UpdateAction"
22 
23 
24 using namespace BPackageKit;
25 //using namespace BPackageKit::BPrivate;
26 using namespace BPackageKit::BManager::BPrivate;
27 
28 
29 UpdateAction::UpdateAction()
30 {
31 	fUpdateManager = new(std::nothrow)
32 		UpdateManager(B_PACKAGE_INSTALLATION_LOCATION_SYSTEM);
33 }
34 
35 
36 UpdateAction::~UpdateAction()
37 {
38 	delete fUpdateManager;
39 }
40 
41 
42 status_t
43 UpdateAction::Perform()
44 {
45 	fUpdateManager->Init(BPackageManager::B_ADD_INSTALLED_REPOSITORIES
46 		| BPackageManager::B_ADD_REMOTE_REPOSITORIES
47 		| BPackageManager::B_REFRESH_REPOSITORIES);
48 
49 	try {
50 		// These values indicate that all updates should be installed
51 		int packageCount = 0;
52 		const char* const packages = "";
53 
54 		// perform the update
55 //		fUpdateManager->SetDebugLevel(1);
56 		fUpdateManager->Update(&packages, packageCount);
57 	} catch (BFatalErrorException ex) {
58 		BString errorString;
59 		errorString.SetToFormat(
60 			"Fatal error occurred while updating packages: "
61 			"%s (%s)\n", ex.Message().String(),
62 			ex.Details().String());
63 		BAlert* alert(new(std::nothrow) BAlert(B_TRANSLATE("Fatal error"),
64 			errorString, B_TRANSLATE("Close"), NULL, NULL,
65 			B_WIDTH_AS_USUAL, B_STOP_ALERT));
66 		if (alert != NULL)
67 			alert->Go();
68 		fUpdateManager->FinalUpdate(B_TRANSLATE("Updates did not complete"),
69 			ex.Message());
70 		return ex.Error();
71 	} catch (BAbortedByUserException ex) {
72 		fprintf(stderr, "Updates aborted by user: %s\n",
73 			ex.Message().String());
74 		// No need for a final message since user initiated cancel request
75 		// Note: activate FinalUpdate() call for testing final message window
76 		//fUpdateManager->FinalUpdate(B_TRANSLATE("Updates cancelled"),
77 		//	B_TRANSLATE("No packages have been updated."));
78 		// Note: comment out when testing final message window
79 		be_app->PostMessage(kMsgFinalQuit);
80 		return B_OK;
81 	} catch (BNothingToDoException ex) {
82 		fprintf(stderr, "Nothing to do while updating packages : %s\n",
83 			ex.Message().String());
84 		fUpdateManager->FinalUpdate(B_TRANSLATE("No updates available"),
85 			B_TRANSLATE("There were no updates found."));
86 		return B_OK;
87 	} catch (BException ex) {
88 		fprintf(stderr, "Exception occurred while updating packages : %s\n",
89 			ex.Message().String());
90 		fUpdateManager->FinalUpdate(B_TRANSLATE("Updates did not complete"),
91 			ex.Message());
92 		return B_ERROR;
93 	}
94 
95 	return B_OK;
96 }
97