xref: /haiku/src/apps/haikudepot/packagemanagement/AbstractPackageProcess.cpp (revision 344ded80d400028c8f561b4b876257b94c12db4a)
1 /*
2  * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2013, Rene Gollent, <rene@gollent.com>
4  * Copyright 2020-2024, Andrew Lindesay <apl@lindesay.co.nz>
5  *
6  * All rights reserved. Distributed under the terms of the MIT License.
7  */
8 
9 
10 #include "AbstractPackageProcess.h"
11 
12 #include "Logger.h"
13 #include "Model.h"
14 #include "PackageKitUtils.h"
15 #include "PackageManager.h"
16 #include "PackageUtils.h"
17 
18 
19 using namespace BPackageKit;
20 
21 // #pragma mark - PackageAction
22 
23 
24 AbstractPackageProcess::AbstractPackageProcess(
25 		PackageInfoRef package, Model* model)
26 	:
27 	fPackage(package),
28 	fModel(model)
29 {
30 	if (package.IsSet())
31 		fInstallLocation = PackageKitUtils::DeriveInstallLocation(package.Get());
32 	else
33 		fInstallLocation = B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
34 
35 	// TODO: ideally if the package is installed at multiple locations,
36 	// the user should be able to pick which one to remove.
37 	// TODO: allow configuring the installation location
38 	fPackageManager = new(std::nothrow) PackageManager(
39 		(BPackageInstallationLocation)fInstallLocation);
40 }
41 
42 
43 AbstractPackageProcess::~AbstractPackageProcess()
44 {
45 	delete fPackageManager;
46 }
47 
48 
49 PackageInfoRef
50 AbstractPackageProcess::FindPackageByName(const BString& name)
51 {
52 	return fModel->PackageForName(name);
53 }
54 
55 
56 // TODO; will refactor once the models go immutable.
57 void
58 AbstractPackageProcess::SetPackageState(PackageInfoRef& package, PackageState state)
59 {
60 	if (package.IsSet()) {
61 		PackageLocalInfoRef localInfo = PackageUtils::NewLocalInfo(package);
62 		localInfo->SetState(state);
63 		package->SetLocalInfo(localInfo);
64 	} else {
65 		HDERROR("setting state, but the package is not set");
66 	}
67 }
68 
69 
70 // TODO; will refactor once the models go immutable.
71 void
72 AbstractPackageProcess::SetPackageDownloadProgress(PackageInfoRef& package, float value)
73 {
74 	if (package.IsSet()) {
75 		PackageLocalInfoRef localInfo = PackageUtils::NewLocalInfo(package);
76 		localInfo->SetDownloadProgress(value);
77 		package->SetLocalInfo(localInfo);
78 	} else {
79 		HDERROR("setting progress, but the package is not set");
80 	}
81 }
82 
83 
84 // TODO; will refactor once the models go immutable.
85 void
86 AbstractPackageProcess::ClearPackageInstallationLocations(PackageInfoRef& package)
87 {
88 	if (package.IsSet()) {
89 		PackageLocalInfoRef localInfo = PackageUtils::NewLocalInfo(package);
90 		localInfo->ClearInstallationLocations();
91 		package->SetLocalInfo(localInfo);
92 	} else {
93 		HDERROR("clearing installation locations, but the package is not set");
94 	}
95 }
96