xref: /haiku/src/apps/haikudepot/packagemanagement/UninstallPackageProcess.cpp (revision 344ded80d400028c8f561b4b876257b94c12db4a)
1 /*
2  * Copyright 2013-2021, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Ingo Weinhold <ingo_weinhold@gmx.de>
7  * 		Stephan Aßmus <superstippi@gmx.de>
8  * 		Rene Gollent <rene@gollent.com>
9  *		Julian Harnath <julian.harnath@rwth-aachen.de>
10  *		Andrew Lindesay <apl@lindesay.co.nz>
11  *
12  * Note that this file has been re-factored from `PackageManager.cpp` and
13  * authors have been carried across in 2021.
14  */
15 
16 
17 #include "UninstallPackageProcess.h"
18 
19 #include <package/manager/Exceptions.h>
20 #include <package/solver/SolverPackage.h>
21 
22 #include <Catalog.h>
23 
24 #include "Alert.h"
25 #include "AppUtils.h"
26 #include "Logger.h"
27 #include "PackageManager.h"
28 #include "PackageUtils.h"
29 
30 
31 #undef B_TRANSLATION_CONTEXT
32 #define B_TRANSLATION_CONTEXT "UninstallPackageAction"
33 
34 
35 using namespace BPackageKit;
36 using namespace BPackageKit::BManager::BPrivate;
37 
38 using BPackageKit::BSolverPackage;
39 
40 
41 UninstallPackageProcess::UninstallPackageProcess(PackageInfoRef package,
42 		Model* model)
43 	:
44 	AbstractPackageProcess(package, model)
45 {
46 	fDescription = B_TRANSLATE("Uninstalling \"%PackageName%\"");
47 	fDescription.ReplaceAll("%PackageName%", fPackage->Name());
48 }
49 
50 
51 UninstallPackageProcess::~UninstallPackageProcess()
52 {
53 }
54 
55 
56 const char*
57 UninstallPackageProcess::Name() const
58 {
59 	return "UninstallPackageAction";
60 }
61 
62 
63 const char*
64 UninstallPackageProcess::Description() const
65 {
66 	return fDescription;
67 }
68 
69 
70 status_t
71 UninstallPackageProcess::RunInternal()
72 {
73 	fPackageManager->Init(BPackageManager::B_ADD_INSTALLED_REPOSITORIES);
74 
75 	PackageInfoRef ref(fPackage);
76 	PackageState state = PackageUtils::State(fPackage);
77 	const char* packageName = ref->Name().String();
78 
79 	fPackageManager->SetCurrentActionPackage(ref, false);
80 	fPackageManager->AddProgressListener(this);
81 
82 	try {
83 		fPackageManager->Uninstall(&packageName, 1);
84 	} catch (BFatalErrorException& ex) {
85 		BString errorString;
86 		errorString.SetToFormat(
87 			"Fatal error occurred while uninstalling package %s: "
88 			"%s (%s)\n", packageName, ex.Message().String(),
89 			ex.Details().String());
90 		AppUtils::NotifySimpleError(B_TRANSLATE("Fatal error"), errorString,
91 			B_STOP_ALERT);
92 		SetPackageState(ref, state);
93 		return ex.Error();
94 	} catch (BAbortedByUserException& ex) {
95 		return B_OK;
96 	} catch (BNothingToDoException& ex) {
97 		return B_OK;
98 	} catch (BException& ex) {
99 		HDERROR("Exception occurred while uninstalling package %s: %s",
100 			packageName, ex.Message().String());
101 		SetPackageState(ref, state);
102 		return B_ERROR;
103 	}
104 
105 	fPackageManager->RemoveProgressListener(this);
106 
107 	return B_OK;
108 }
109 
110 
111 void
112 UninstallPackageProcess::StartApplyingChanges(
113 		BPackageManager::InstalledRepository& repository)
114 {
115 	BPackageManager::InstalledRepository::PackageList& packages
116 		= repository.PackagesToDeactivate();
117 	for (int32 i = 0; i < packages.CountItems(); i++) {
118 		PackageInfoRef ref(FindPackageByName(packages.ItemAt(i)
119 				->Name()));
120 		if (ref.IsSet())
121 			fRemovedPackages.push_back(ref);
122 	}
123 }
124 
125 
126 void
127 UninstallPackageProcess::ApplyingChangesDone(
128 		BPackageManager::InstalledRepository& repository)
129 {
130 	std::vector<PackageInfoRef>::iterator it;
131 	for (it = fRemovedPackages.begin(); it != fRemovedPackages.end(); it++) {
132 		PackageInfoRef packageInfoRef = *it;
133 		SetPackageState(packageInfoRef, NONE);
134 		ClearPackageInstallationLocations(packageInfoRef);
135 	}
136 }
137