xref: /haiku/src/apps/haikudepot/util/PackageKitUtils.cpp (revision 8b5cc9cf52bc7de09b9fa2db0f16fced0f4ae2e9)
1 /*
2  * Copyright 2022, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include "PackageKitUtils.h"
7 
8 
9 using namespace BPackageKit;
10 
11 
12 /*static*/ status_t
13 PackageKitUtils::DeriveLocalFilePath(const PackageInfo* package, BPath& path)
14 {
15 	if (package->IsLocalFile()) {
16 		path.SetTo(package->LocalFilePath());
17 		return B_OK;
18 	}
19 
20 	path.Unset();
21 	BPackageInstallationLocation installationLocation = DeriveInstallLocation(package);
22 	directory_which which;
23 	status_t result = _DeriveDirectoryWhich(installationLocation, &which);
24 
25 	if (result == B_OK)
26 		result = find_directory(which, &path);
27 
28 	if (result == B_OK)
29 		path.Append(package->FileName());
30 
31 	return result;
32 }
33 
34 
35 /*static*/ status_t
36 PackageKitUtils::_DeriveDirectoryWhich(BPackageInstallationLocation location,
37 	directory_which* which)
38 {
39 	switch (location) {
40 		case B_PACKAGE_INSTALLATION_LOCATION_SYSTEM:
41 			*which = B_SYSTEM_PACKAGES_DIRECTORY;
42 			break;
43 		case B_PACKAGE_INSTALLATION_LOCATION_HOME:
44 			*which = B_USER_PACKAGES_DIRECTORY;
45 			break;
46 		default:
47 			debugger("illegal state: unsupported package installation location");
48 			return B_BAD_VALUE;
49 	}
50 	return B_OK;
51 }
52 
53 
54 /*static*/ BPackageInstallationLocation
55 PackageKitUtils::DeriveInstallLocation(const PackageInfo* package)
56 {
57 	const PackageInstallationLocationSet& locations = package->InstallationLocations();
58 
59 	// If the package is already installed, return its first installed location
60 	if (locations.size() != 0)
61 		return static_cast<BPackageInstallationLocation>(*locations.begin());
62 
63 	return B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
64 }
65