xref: /haiku/src/apps/haikudepot/util/PackageUtils.cpp (revision c078962803230aa4e37330a125accf955e4d8287)
1 /*
2  * Copyright 2024, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include "PackageUtils.h"
7 
8 #include "Logger.h"
9 
10 /*!	This method will obtain the title from the package if this is possible or
11 	otherwise it will return the name of the package.
12 */
13 
14 /*static*/ void
15 PackageUtils::TitleOrName(const PackageInfoRef& package, BString& title)
16 {
17 	PackageUtils::Title(package, title);
18 	if (title.IsEmpty() && package.IsSet())
19 		title.SetTo(package->Name());
20 }
21 
22 
23 /*static*/ void
24 PackageUtils::Title(const PackageInfoRef& package, BString& title)
25 {
26 	if (package.IsSet()) {
27 		PackageLocalizedTextRef localizedText = package->LocalizedText();
28 
29 		if (localizedText.IsSet())
30 			title.SetTo(localizedText->Title());
31 		else
32 			title.SetTo("");
33 	} else {
34 		title.SetTo("");
35 	}
36 }
37 
38 
39 /*static*/ void
40 PackageUtils::Summary(const PackageInfoRef& package, BString& summary)
41 {
42 	if (package.IsSet()) {
43 		PackageLocalizedTextRef localizedText = package->LocalizedText();
44 
45 		if (localizedText.IsSet())
46 			summary.SetTo(localizedText->Summary());
47 		else
48 			summary.SetTo("");
49 	} else {
50 		summary.SetTo("");
51 	}
52 }
53 
54 
55 /*static*/ PackageLocalizedTextRef
56 PackageUtils::NewLocalizedText(const PackageInfoRef& package)
57 {
58 	if (!package.IsSet())
59 		HDFATAL("it is not possible to get the `LocalizedText` from a not-existing package");
60 
61 	PackageLocalizedTextRef localizedText = package->LocalizedText();
62 
63 	if (localizedText.IsSet())
64 		return PackageLocalizedTextRef(new PackageLocalizedText(*(localizedText.Get())), true);
65 
66 	return PackageLocalizedTextRef(new PackageLocalizedText(), true);
67 }
68 
69 
70 /*static*/ PackageState
71 PackageUtils::State(const PackageInfoRef& package)
72 {
73 	if (package.IsSet()) {
74 		PackageLocalInfoRef localInfo = package->LocalInfo();
75 
76 		if (localInfo.IsSet())
77 			return localInfo->State();
78 	}
79 
80 	return NONE;
81 }
82 
83 
84 /*static*/ off_t
85 PackageUtils::Size(const PackageInfoRef& package)
86 {
87 	if (package.IsSet()) {
88 		PackageLocalInfoRef localInfo = package->LocalInfo();
89 
90 		if (localInfo.IsSet())
91 			return localInfo->Size();
92 	}
93 
94 	return 0;
95 }
96 
97 
98 /*static*/ bool
99 PackageUtils::Viewed(const PackageInfoRef& package)
100 {
101 	if (package.IsSet()) {
102 		PackageLocalInfoRef localInfo = package->LocalInfo();
103 
104 		if (localInfo.IsSet())
105 			return localInfo->Viewed();
106 	}
107 
108 	return false;
109 }
110 
111 
112 /*static*/ bool
113 PackageUtils::IsActivatedOrLocalFile(const PackageInfoRef& package)
114 {
115 	if (package.IsSet()) {
116 		PackageLocalInfoRef localInfo = package->LocalInfo();
117 
118 		if (localInfo.IsSet())
119 			return localInfo->IsLocalFile() || localInfo->State() == ACTIVATED;
120 	}
121 
122 	return false;
123 }
124 
125 
126 /*static*/ float
127 PackageUtils::DownloadProgress(const PackageInfoRef& package)
128 {
129 	if (package.IsSet()) {
130 		PackageLocalInfoRef localInfo = package->LocalInfo();
131 
132 		if (localInfo.IsSet())
133 			return localInfo->DownloadProgress();
134 	}
135 
136 	return 0.0f;
137 }
138 
139 
140 /*static*/ int32
141 PackageUtils::Flags(const PackageInfoRef& package)
142 {
143 	if (package.IsSet()) {
144 		PackageLocalInfoRef localInfo = package->LocalInfo();
145 
146 		if (localInfo.IsSet())
147 			return localInfo->Flags();
148 	}
149 
150 	return false;
151 }
152 
153 
154 /*static*/ PackageLocalInfoRef
155 PackageUtils::NewLocalInfo(const PackageInfoRef& package)
156 {
157 	if (!package.IsSet())
158 		HDFATAL("it is not possible to get the `LocalInfo` from a not-existing package");
159 
160 	PackageLocalInfoRef localInfo = package->LocalInfo();
161 
162 	if (localInfo.IsSet())
163 		return PackageLocalInfoRef(new PackageLocalInfo(*(localInfo.Get())), true);
164 
165 	return PackageLocalInfoRef(new PackageLocalInfo(), true);
166 }
167 
168 
169 /*static*/ const char*
170 PackageUtils::StateToString(PackageState state)
171 {
172 	switch (state) {
173 		case NONE:
174 			return "NONE";
175 		case INSTALLED:
176 			return "INSTALLED";
177 		case DOWNLOADING:
178 			return "DOWNLOADING";
179 		case ACTIVATED:
180 			return "ACTIVATED";
181 		case UNINSTALLED:
182 			return "UNINSTALLED";
183 		case PENDING:
184 			return "PENDING";
185 		default:
186 			debugger("unknown package state");
187 			return "???";
188 	}
189 }
190