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