xref: /haiku/src/apps/haikudepot/util/PackageKitUtils.cpp (revision 344ded80d400028c8f561b4b876257b94c12db4a)
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 PackageInfoRef package, BPath& path)
14 {
15 	if (!package.IsSet())
16 		return B_ERROR;
17 
18 	PackageLocalInfoRef localInfo = package->LocalInfo();
19 
20 	if (localInfo.IsSet() && localInfo->IsLocalFile()) {
21 		path.SetTo(localInfo->LocalFilePath());
22 		return B_OK;
23 	}
24 
25 	path.Unset();
26 	BPackageInstallationLocation installationLocation = DeriveInstallLocation(package);
27 	directory_which which;
28 	status_t result = _DeriveDirectoryWhich(installationLocation, &which);
29 
30 	if (result == B_OK)
31 		result = find_directory(which, &path);
32 
33 	if (result == B_OK)
34 		path.Append(localInfo->FileName());
35 
36 	return result;
37 }
38 
39 
40 /*static*/ status_t
41 PackageKitUtils::_DeriveDirectoryWhich(BPackageInstallationLocation location,
42 	directory_which* which)
43 {
44 	switch (location) {
45 		case B_PACKAGE_INSTALLATION_LOCATION_SYSTEM:
46 			*which = B_SYSTEM_PACKAGES_DIRECTORY;
47 			break;
48 		case B_PACKAGE_INSTALLATION_LOCATION_HOME:
49 			*which = B_USER_PACKAGES_DIRECTORY;
50 			break;
51 		default:
52 			debugger("illegal state: unsupported package installation location");
53 			return B_BAD_VALUE;
54 	}
55 	return B_OK;
56 }
57 
58 
59 /*static*/ BPackageInstallationLocation
60 PackageKitUtils::DeriveInstallLocation(const PackageInfoRef package)
61 {
62 	if (package.IsSet()) {
63 
64 		PackageLocalInfoRef localInfo = package->LocalInfo();
65 
66 		if (localInfo.IsSet()) {
67 			const PackageInstallationLocationSet& locations = localInfo->InstallationLocations();
68 
69 			// If the package is already installed, return its first installed location
70 			if (locations.size() != 0)
71 				return static_cast<BPackageInstallationLocation>(*locations.begin());
72 		}
73 	}
74 
75 	return B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
76 }
77