xref: /haiku/src/bin/package/PackageInfoPrinter.h (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 /*
2  * Copyright 2009-2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef PACKAGE_INFO_PRINTER_H
6 #define PACKAGE_INFO_PRINTER_H
7 
8 
9 #include <stdio.h>
10 
11 #include <package/hpkg/PackageInfoAttributeValue.h>
12 #include <package/PackageInfo.h>
13 
14 
15 using namespace BPackageKit;
16 using BPackageKit::BHPKG::BGlobalWritableFileInfoData;
17 using BPackageKit::BHPKG::BPackageInfoAttributeValue;
18 using BPackageKit::BHPKG::BUserData;
19 using BPackageKit::BHPKG::BUserSettingsFileInfoData;
20 
21 
22 class PackageInfoPrinter {
23 public:
24 	void PrintPackageInfo(const BPackageInfo& info)
25 	{
26 		PrintName(info.Name());
27 		PrintSummary(info.Summary());
28 		PrintDescription(info.Description());
29 		PrintVendor(info.Vendor());
30 		PrintPackager(info.Packager());
31 
32 		if (!info.BasePackage().IsEmpty())
33 			PrintBasePackage(info.BasePackage());
34 
35 		PrintFlags(info.Flags());
36 		PrintArchitecture((uint32)info.Architecture());
37 		PrintVersion(info.Version());
38 
39 		int32 count = info.CopyrightList().CountStrings();
40 		for (int32 i = 0; i < count; i++)
41 			PrintCopyright(info.CopyrightList().StringAt(i));
42 
43 		count = info.LicenseList().CountStrings();
44 		for (int32 i = 0; i < count; i++)
45 			PrintLicense(info.LicenseList().StringAt(i));
46 
47 		count = info.URLList().CountStrings();
48 		for (int32 i = 0; i < count; i++)
49 			PrintUrl(info.URLList().StringAt(i));
50 
51 		count = info.SourceURLList().CountStrings();
52 		for (int32 i = 0; i < count; i++)
53 			PrintSourceUrl(info.SourceURLList().StringAt(i));
54 
55 		count = info.ProvidesList().CountItems();
56 		for (int32 i = 0; i < count; i++)
57 			PrintProvides(*info.ProvidesList().ItemAt(i));
58 
59 		count = info.RequiresList().CountItems();
60 		for (int32 i = 0; i < count; i++)
61 			PrintRequires(*info.RequiresList().ItemAt(i));
62 
63 		count = info.SupplementsList().CountItems();
64 		for (int32 i = 0; i < count; i++)
65 			PrintSupplements(*info.SupplementsList().ItemAt(i));
66 
67 		count = info.ConflictsList().CountItems();
68 		for (int32 i = 0; i < count; i++)
69 			PrintConflicts(*info.ConflictsList().ItemAt(i));
70 
71 		count = info.FreshensList().CountItems();
72 		for (int32 i = 0; i < count; i++)
73 			PrintFreshens(*info.FreshensList().ItemAt(i));
74 
75 		count = info.ReplacesList().CountStrings();
76 		for (int32 i = 0; i < count; i++)
77 			PrintReplaces(info.ReplacesList().StringAt(i));
78 
79 		count = info.GlobalWritableFileInfos().CountItems();
80 		for (int32 i = 0; i < count; i++)
81 			PrintGlobalWritableFile(*info.GlobalWritableFileInfos().ItemAt(i));
82 
83 		count = info.UserSettingsFileInfos().CountItems();
84 		for (int32 i = 0; i < count; i++)
85 			PrintUserSettingsFile(*info.UserSettingsFileInfos().ItemAt(i));
86 
87 		count = info.Users().CountItems();
88 		for (int32 i = 0; i < count; i++)
89 			PrintUser(*info.Users().ItemAt(i));
90 
91 		count = info.Groups().CountStrings();
92 		for (int32 i = 0; i < count; i++)
93 			PrintGroup(info.Groups().StringAt(i));
94 
95 		count = info.PostInstallScripts().CountStrings();
96 		for (int32 i = 0; i < count; i++)
97 			PrintPostInstallScript(info.PostInstallScripts().StringAt(i));
98 
99 		count = info.PreUninstallScripts().CountStrings();
100 		for (int32 i = 0; i < count; i++)
101 			PrintPreUninstallScript(info.PreUninstallScripts().StringAt(i));
102 
103 		if (!info.InstallPath().IsEmpty())
104 			PrintInstallPath(info.InstallPath());
105 	}
106 
107 	bool PrintAttribute(const BPackageInfoAttributeValue& value)
108 	{
109 		switch (value.attributeID) {
110 			case B_PACKAGE_INFO_NAME:
111 				PrintName(value.string);
112 				break;
113 
114 			case B_PACKAGE_INFO_SUMMARY:
115 				PrintSummary(value.string);
116 				break;
117 
118 			case B_PACKAGE_INFO_DESCRIPTION:
119 				PrintDescription(value.string);
120 				break;
121 
122 			case B_PACKAGE_INFO_VENDOR:
123 				PrintVendor(value.string);
124 				break;
125 
126 			case B_PACKAGE_INFO_PACKAGER:
127 				PrintPackager(value.string);
128 				break;
129 
130 			case B_PACKAGE_INFO_BASE_PACKAGE:
131 				PrintBasePackage(value.string);
132 				break;
133 
134 			case B_PACKAGE_INFO_FLAGS:
135 				PrintFlags(value.unsignedInt);
136 				break;
137 
138 			case B_PACKAGE_INFO_ARCHITECTURE:
139 				PrintArchitecture(value.unsignedInt);
140 				break;
141 
142 			case B_PACKAGE_INFO_VERSION:
143 				PrintVersion(value.version);
144 				break;
145 
146 			case B_PACKAGE_INFO_COPYRIGHTS:
147 				PrintCopyright(value.string);
148 				break;
149 
150 			case B_PACKAGE_INFO_LICENSES:
151 				PrintLicense(value.string);
152 				break;
153 
154 			case B_PACKAGE_INFO_URLS:
155 				PrintUrl(value.string);
156 				break;
157 
158 			case B_PACKAGE_INFO_SOURCE_URLS:
159 				PrintSourceUrl(value.string);
160 				break;
161 
162 			case B_PACKAGE_INFO_PROVIDES:
163 				PrintProvides(value.resolvable);
164 				break;
165 
166 			case B_PACKAGE_INFO_REQUIRES:
167 				PrintRequires(value.resolvableExpression);
168 				break;
169 
170 			case B_PACKAGE_INFO_SUPPLEMENTS:
171 				PrintSupplements(value.resolvableExpression);
172 				break;
173 
174 			case B_PACKAGE_INFO_CONFLICTS:
175 				PrintConflicts(value.resolvableExpression);
176 				break;
177 
178 			case B_PACKAGE_INFO_FRESHENS:
179 				PrintFreshens(value.resolvableExpression);
180 				break;
181 
182 			case B_PACKAGE_INFO_REPLACES:
183 				PrintReplaces(value.string);
184 				break;
185 
186 			case B_PACKAGE_INFO_GLOBAL_WRITABLE_FILES:
187 				PrintGlobalWritableFile(value.globalWritableFileInfo);
188 				break;
189 
190 			case B_PACKAGE_INFO_USER_SETTINGS_FILES:
191 				PrintUserSettingsFile(value.userSettingsFileInfo);
192 				break;
193 
194 			case B_PACKAGE_INFO_USERS:
195 				PrintUser(value.user);
196 				break;
197 
198 			case B_PACKAGE_INFO_GROUPS:
199 				PrintGroup(value.string);
200 				break;
201 
202 			case B_PACKAGE_INFO_POST_INSTALL_SCRIPTS:
203 				PrintPostInstallScript(value.string);
204 				break;
205 
206 			case B_PACKAGE_INFO_PRE_UNINSTALL_SCRIPTS:
207 				PrintPreUninstallScript(value.string);
208 				break;
209 
210 			case B_PACKAGE_INFO_INSTALL_PATH:
211 				PrintInstallPath(value.string);
212 				break;
213 
214 			case B_PACKAGE_INFO_CHECKSUM:
215 				PrintChecksum(value.string);
216 				break;
217 
218 			default:
219 				printf("\tunknown or future attribute: "
220 					"BPackageInfoAttributeID #%d\n", value.attributeID);
221 				return true;
222 		}
223 
224 		return true;
225 	}
226 
227 	void PrintName(const char* name) const
228 	{
229 		printf("\tname: %s\n", name);
230 	}
231 
232 	void PrintSummary(const char* summary) const
233 	{
234 		printf("\tsummary: %s\n", summary);
235 	}
236 
237 	void PrintDescription(const char* description) const
238 	{
239 		printf("\tdescription: %s\n", description);
240 	}
241 
242 	void PrintVendor(const char* vendor) const
243 	{
244 		printf("\tvendor: %s\n", vendor);
245 	}
246 
247 	void PrintPackager(const char* packager) const
248 	{
249 		printf("\tpackager: %s\n", packager);
250 	}
251 
252 	void PrintBasePackage(const char* basePackage) const
253 	{
254 		printf("\tbase package: %s\n", basePackage);
255 	}
256 
257 	void PrintFlags(uint32 flags) const
258 	{
259 		if (flags == 0)
260 			return;
261 
262 		printf("\tflags:\n");
263 		if ((flags & B_PACKAGE_FLAG_APPROVE_LICENSE) != 0)
264 			printf("\t\tapprove_license\n");
265 		if ((flags & B_PACKAGE_FLAG_SYSTEM_PACKAGE) != 0)
266 			printf("\t\tsystem_package\n");
267 	}
268 
269 	void PrintArchitecture(uint32 architecture) const
270 	{
271 		printf("\tarchitecture: %s\n",
272 			architecture < B_PACKAGE_ARCHITECTURE_ENUM_COUNT
273 				? BPackageInfo::kArchitectureNames[architecture]
274 				: "<invalid>");
275 	}
276 
277 	void PrintVersion(const BPackageVersion& version) const
278 	{
279 		printf("\tversion: %s\n", version.ToString().String());
280 	}
281 
282 	void PrintCopyright(const char* copyright) const
283 	{
284 		printf("\tcopyright: %s\n", copyright);
285 	}
286 
287 	void PrintLicense(const char* license) const
288 	{
289 		printf("\tlicense: %s\n", license);
290 	}
291 
292 	void PrintUrl(const char* url) const
293 	{
294 		printf("\tURL: %s\n", url);
295 	}
296 
297 	void PrintSourceUrl(const char* sourceUrl) const
298 	{
299 		printf("\tsource URL: %s\n", sourceUrl);
300 	}
301 
302 	void PrintProvides(const BPackageResolvable& provides) const
303 	{
304 		printf("\tprovides: %s", provides.Name().String());
305 		if (provides.Version().InitCheck() == B_OK)
306 			printf(" = %s", provides.Version().ToString().String());
307 
308 		if (provides.CompatibleVersion().InitCheck() == B_OK) {
309 			printf(" (compatible >= %s)",
310 				provides.CompatibleVersion().ToString().String());
311 		}
312 		printf("\n");
313 	}
314 
315 	void PrintRequires(const BPackageResolvableExpression& require) const
316 	{
317 		_PrintResolvableExpression("requires", require);
318 	}
319 
320 	void PrintSupplements(const BPackageResolvableExpression& supplements)
321 		const
322 	{
323 		_PrintResolvableExpression("supplements", supplements);
324 	}
325 
326 	void PrintConflicts(const BPackageResolvableExpression& conflicts) const
327 	{
328 		_PrintResolvableExpression("conflicts", conflicts);
329 	}
330 
331 	void PrintFreshens(const BPackageResolvableExpression& freshens) const
332 	{
333 		_PrintResolvableExpression("freshens", freshens);
334 	}
335 
336 	void PrintReplaces(const char* replaces) const
337 	{
338 		printf("\treplaces: %s\n", replaces);
339 	}
340 
341 	void PrintGlobalWritableFile(const BGlobalWritableFileInfo& info) const
342 	{
343 		printf("\tglobal writable file: %s", info.Path().String());
344 		if (info.IsDirectory())
345 			printf( " directory");
346 		if (info.UpdateType() < B_WRITABLE_FILE_UPDATE_TYPE_ENUM_COUNT) {
347 			printf(" %s\n",
348 				BPackageInfo::kWritableFileUpdateTypes[info.UpdateType()]);
349 		} else
350 			printf("\n");
351 	}
352 
353 	void PrintUserSettingsFile(const BUserSettingsFileInfo& info) const
354 	{
355 		printf("\tuser settings file: %s", info.Path().String());
356 		if (info.IsDirectory())
357 			printf( " directory\n");
358 		else if (!info.TemplatePath().IsEmpty())
359 			printf(" template %s\n", info.TemplatePath().String());
360 		else
361 			printf("\n");
362 	}
363 
364 	void PrintUser(const BUser& user) const
365 	{
366 		printf("\tuser: %s\n", user.Name().String());
367 		if (!user.RealName().IsEmpty())
368 			printf("\t\treal name: %s\n", user.RealName().String());
369 		if (!user.Home().IsEmpty())
370 			printf("\t\thome:      %s\n", user.Home().String());
371 		if (!user.Shell().IsEmpty())
372 			printf("\t\tshell:     %s\n", user.Shell().String());
373 
374 		int32 groupCount = user.Groups().CountStrings();
375 		for (int32 i = 0; i < groupCount; i++)
376 			printf("\t\tgroup:     %s\n", user.Groups().StringAt(i).String());
377 	}
378 
379 	void PrintGroup(const char* group) const
380 	{
381 		printf("\tgroup: %s\n", group);
382 	}
383 
384 	void PrintPostInstallScript(const char* script) const
385 	{
386 		printf("\tpost-install script: %s\n", script);
387 	}
388 
389 	void PrintPreUninstallScript(const char* script) const
390 	{
391 		printf("\tpre-uninstall script: %s\n", script);
392 	}
393 
394 	void PrintInstallPath(const char* path) const
395 	{
396 		printf("\tinstall path: %s\n", path);
397 	}
398 
399 	void PrintChecksum(const char* checksum) const
400 	{
401 		printf("\tchecksum: %s\n", checksum);
402 	}
403 
404 private:
405 	void _PrintResolvableExpression(const char* fieldName,
406 		const BPackageResolvableExpression& expression) const
407 	{
408 		printf("\t%s: %s\n", fieldName, expression.ToString().String());
409 	}
410 };
411 
412 
413 #endif	// PACKAGE_INFO_PRINTER_H
414