1// dump_mime_types.cpp
2
3#include <stdio.h>
4#include <string.h>
5#include <sys/utsname.h>
6
7#include <Application.h>
8#include <Message.h>
9#include <Mime.h>
10
11// main
12int
13main()
14{
15	// create a BApplication on BeOS
16	struct utsname unameInfo;
17	if (uname(&unameInfo) < 0 || strcmp(unameInfo.sysname, "Haiku") != 0)
18		new BApplication("application/x-vnd.haiku.dump-mime-types");
19
20	// get the list of installed types
21	BMessage installedTypes;
22	status_t error = BMimeType::GetInstalledTypes(&installedTypes);
23	if (error != B_OK) {
24		fprintf(stderr, "Failed to get installed types: %s\n", strerror(error));
25		exit(1);
26	}
27
28	// print the types (including some additional info)
29	const char *type;
30	for (int i = 0; installedTypes.FindString("types", i, &type) == B_OK; i++) {
31		printf("%s:\n", type);
32
33		// get mime type
34		BMimeType mimeType;
35		error = mimeType.SetTo(type);
36		if (error != B_OK) {
37			printf("  failed to init type: %s\n", strerror(error));
38			continue;
39		}
40
41		// preferred app
42		char preferredApp[B_MIME_TYPE_LENGTH];
43		if (mimeType.GetPreferredApp(preferredApp) == B_OK)
44			printf("  preferred app:     %s\n", preferredApp);
45
46		// short description
47		char shortDescription[256];
48		if (mimeType.GetShortDescription(shortDescription) == B_OK)
49			printf("  short description: %s\n", shortDescription);
50
51		// long description
52		char longDescription[256];
53		if (mimeType.GetLongDescription(longDescription) == B_OK)
54			printf("  long description:  %s\n", longDescription);
55
56		// extensions
57		BMessage extensions;
58		if (mimeType.GetFileExtensions(&extensions) == B_OK) {
59			printf("  extensions:        ");
60			const char *extension;
61			for (int k = 0;
62				 extensions.FindString("extensions", k, &extension) == B_OK;
63				 k++) {
64				if (k > 0)
65					printf(" ");
66				printf("%s", extension);
67			}
68			printf("\n");
69		}
70
71		// supporting apps
72		BMessage supportingApps;
73		if (mimeType.GetSupportingApps(&supportingApps) == B_OK) {
74			const char *app;
75			for (int k = 0;
76				 supportingApps.FindString("applications", k, &app) == B_OK;
77				 k++) {
78				if (k == 0)
79					printf("  supporting apps:   ");
80				else
81					printf("                     ");
82				printf("%s\n", app);
83			}
84		}
85
86		// attr info
87		BMessage attrInfo;
88		if (mimeType.GetAttrInfo(&attrInfo) == B_OK) {
89			printf("  attributes:\n");
90			const char *name;
91			const char *publicName;
92			type_code type;
93			bool isViewable;
94			bool isPublic;
95			bool isEditable;
96			for (int k = 0;
97				 attrInfo.FindString("attr:name", k, &name) == B_OK
98				 && (attrInfo.FindString("attr:public_name", k,
99				 	&publicName) == B_OK || (publicName = name, true))
100				 && (attrInfo.FindInt32("attr:type", k, (int32*)&type) == B_OK
101				 	|| (type = '____', true))
102				 && (attrInfo.FindBool("attr:viewable", k, &isViewable) == B_OK
103				 	|| (isViewable = false, true))
104				 && (attrInfo.FindBool("attr:public", k, &isPublic) == B_OK
105				 	|| (isPublic = isViewable, true))
106				 && (attrInfo.FindBool("attr:editable", k, &isEditable) == B_OK
107				 	|| (isEditable = false, true));
108				 k++) {
109				printf("    `%s' (`%s')\n", name, publicName);
110				printf("      type:     %c%c%c%c (0x%lx)\n", char(type >> 24),
111					char(type >> 16), char(type >> 8), char(type), type);
112				printf("      public:   %s\n", (isPublic ? "true" : "false"));
113				printf("      editable: %s\n", (isEditable ? "true" : "false"));
114			}
115		}
116	}
117
118	return 0;
119}
120