xref: /haiku/src/apps/haikudepot/util/PackageUtils.cpp (revision 344ded80d400028c8f561b4b876257b94c12db4a)
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*/ bool
56 PackageUtils::IsNativeDesktop(const PackageInfoRef& package)
57 {
58 	if (package.IsSet()) {
59 		PackageClassificationInfoRef classificationInfo = package->PackageClassificationInfo();
60 
61 		if (classificationInfo.IsSet())
62 			return classificationInfo->IsNativeDesktop();
63 	}
64 
65 	return false;
66 }
67 
68 
69 /*static*/ PackageLocalizedTextRef
70 PackageUtils::NewLocalizedText(const PackageInfoRef& package)
71 {
72 	if (!package.IsSet())
73 		HDFATAL("it is not possible to get the `LocalizedText` from a not-existing package");
74 
75 	PackageLocalizedTextRef localizedText = package->LocalizedText();
76 
77 	if (localizedText.IsSet())
78 		return PackageLocalizedTextRef(new PackageLocalizedText(*(localizedText.Get())), true);
79 
80 	return PackageLocalizedTextRef(new PackageLocalizedText(), true);
81 }
82 
83 
84 /*static*/ PackageState
85 PackageUtils::State(const PackageInfoRef& package)
86 {
87 	if (package.IsSet()) {
88 		PackageLocalInfoRef localInfo = package->LocalInfo();
89 
90 		if (localInfo.IsSet())
91 			return localInfo->State();
92 	}
93 
94 	return NONE;
95 }
96 
97 
98 /*static*/ off_t
99 PackageUtils::Size(const PackageInfoRef& package)
100 {
101 	if (package.IsSet()) {
102 		PackageLocalInfoRef localInfo = package->LocalInfo();
103 
104 		if (localInfo.IsSet())
105 			return localInfo->Size();
106 	}
107 
108 	return 0;
109 }
110 
111 
112 /*static*/ bool
113 PackageUtils::Viewed(const PackageInfoRef& package)
114 {
115 	if (package.IsSet()) {
116 		PackageLocalInfoRef localInfo = package->LocalInfo();
117 
118 		if (localInfo.IsSet())
119 			return localInfo->Viewed();
120 	}
121 
122 	return false;
123 }
124 
125 
126 /*static*/ bool
127 PackageUtils::IsActivatedOrLocalFile(const PackageInfoRef& package)
128 {
129 	if (package.IsSet()) {
130 		PackageLocalInfoRef localInfo = package->LocalInfo();
131 
132 		if (localInfo.IsSet())
133 			return localInfo->IsLocalFile() || localInfo->State() == ACTIVATED;
134 	}
135 
136 	return false;
137 }
138 
139 
140 /*static*/ float
141 PackageUtils::DownloadProgress(const PackageInfoRef& package)
142 {
143 	if (package.IsSet()) {
144 		PackageLocalInfoRef localInfo = package->LocalInfo();
145 
146 		if (localInfo.IsSet())
147 			return localInfo->DownloadProgress();
148 	}
149 
150 	return 0.0f;
151 }
152 
153 
154 /*static*/ int32
155 PackageUtils::Flags(const PackageInfoRef& package)
156 {
157 	if (package.IsSet()) {
158 		PackageLocalInfoRef localInfo = package->LocalInfo();
159 
160 		if (localInfo.IsSet())
161 			return localInfo->Flags();
162 	}
163 
164 	return false;
165 }
166 
167 
168 /*static*/ PackageLocalInfoRef
169 PackageUtils::NewLocalInfo(const PackageInfoRef& package)
170 {
171 	if (!package.IsSet())
172 		HDFATAL("it is not possible to get the `LocalInfo` from a not-existing package");
173 
174 	PackageLocalInfoRef localInfo = package->LocalInfo();
175 
176 	if (localInfo.IsSet())
177 		return PackageLocalInfoRef(new PackageLocalInfo(*(localInfo.Get())), true);
178 
179 	return PackageLocalInfoRef(new PackageLocalInfo(), true);
180 }
181 
182 
183 /*static*/ const char*
184 PackageUtils::StateToString(PackageState state)
185 {
186 	switch (state) {
187 		case NONE:
188 			return "NONE";
189 		case INSTALLED:
190 			return "INSTALLED";
191 		case DOWNLOADING:
192 			return "DOWNLOADING";
193 		case ACTIVATED:
194 			return "ACTIVATED";
195 		case UNINSTALLED:
196 			return "UNINSTALLED";
197 		case PENDING:
198 			return "PENDING";
199 		default:
200 			debugger("unknown package state");
201 			return "???";
202 	}
203 }
204