File: desktop-load.c

package info (click to toggle)
libsfdo 0.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 740 kB
  • sloc: ansic: 6,491; python: 111; makefile: 4
file content (217 lines) | stat: -rw-r--r-- 6,144 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <assert.h>
#include <getopt.h>
#include <sfdo-basedir.h>
#include <sfdo-desktop.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void log_handler(enum sfdo_log_level level, const char *fmt, va_list args, void *data) {
	(void)level;
	(void)data;
	static const char *levels[] = {
		[SFDO_LOG_LEVEL_SILENT] = "",
		[SFDO_LOG_LEVEL_ERROR] = "error",
		[SFDO_LOG_LEVEL_INFO] = "info",
		[SFDO_LOG_LEVEL_DEBUG] = "debug",
	};
	fprintf(stdout, "[%s] ", levels[level]);
	vfprintf(stdout, fmt, args);
	fprintf(stdout, "\n");
}

static void print_str(const char *name, const char *str, size_t str_len) {
	printf("%s: ", name);
	if (str != NULL) {
		printf("%s (%zu bytes)\n", str, str_len);
	} else {
		printf("<unset>\n");
	}
}

static void print_exec(const char *name, struct sfdo_desktop_exec *exec) {
	printf("%s: <", name);
	if (exec != NULL) {
		if (sfdo_desktop_exec_get_has_target(exec)) {
			printf("has target");
			if (sfdo_desktop_exec_get_supports_list(exec)) {
				printf(", supports lists");
			}
			if (sfdo_desktop_exec_get_supports_uri(exec)) {
				printf(", supports URIs");
			}
		} else {
			printf("has no target");
		}
	} else {
		printf("none");
	}
	printf(">\n");
}

static void print_list(const char *name, const struct sfdo_string *strs, size_t n_strs) {
	printf("%s:\n", name);
	for (size_t i = 0; i < n_strs; i++) {
		printf(" %zu: %s (%zu bytes)\n", i, strs[i].data, strs[i].len);
	}
}

static void print_boolean(const char *name, bool value) {
	printf("%s: %s\n", name, value ? "true" : "false");
}

static void die_usage(const char *prog) {
	printf("Usage: %s [-d] [-e environment] [-l locale] <id>\n", prog);
	exit(1);
}

int main(int argc, char **argv) {
	bool debug = false;
	const char *locale = NULL;
	const char *env = NULL;

	char *prog = argv[0];
	int opt;
	while ((opt = getopt(argc, argv, "de:l:")) != -1) {
		switch (opt) {
		case 'd':
			debug = true;
			break;
		case 'e':
			env = optarg;
			break;
		case 'l':
			locale = optarg;
			break;
		default:
			die_usage(prog);
		}
	}

	argv += optind;
	argc -= optind;

	if (argc < 1) {
		die_usage(prog);
	}

	const char *id = argv[0];
	size_t id_len = strlen(id);

	struct sfdo_basedir_ctx *basedir_ctx = sfdo_basedir_ctx_create();
	struct sfdo_desktop_ctx *ctx = sfdo_desktop_ctx_create(basedir_ctx);

	sfdo_desktop_ctx_set_log_handler(
			ctx, debug ? SFDO_LOG_LEVEL_DEBUG : SFDO_LOG_LEVEL_ERROR, log_handler, NULL);

	struct sfdo_desktop_db *db = sfdo_desktop_db_load(ctx, locale);
	if (db == NULL) {
		fprintf(stderr, "Failed to load a database\n");
		exit(1);
	}

	struct sfdo_desktop_entry *entry = sfdo_desktop_db_get_entry_by_id(db, id, id_len);
	if (entry != NULL) {
		const char *str;
		size_t str_len;

		const struct sfdo_string *strs;
		size_t n_strs;

		str = sfdo_desktop_entry_get_id(entry, &str_len);
		print_str("ID", str, str_len);

		str = sfdo_desktop_entry_get_file_path(entry, &str_len);
		print_str("File path", str, str_len);

		str = sfdo_desktop_entry_get_name(entry, &str_len);
		print_str("Name", str, str_len);
		str = sfdo_desktop_entry_get_generic_name(entry, &str_len);
		print_str("GenericName", str, str_len);
		print_boolean("NoDisplay", sfdo_desktop_entry_get_no_display(entry));
		str = sfdo_desktop_entry_get_comment(entry, &str_len);
		print_str("Comment", str, str_len);
		str = sfdo_desktop_entry_get_icon(entry, &str_len);
		print_str("Icon", str, str_len);

		print_boolean("Shown", sfdo_desktop_entry_show_in(entry, env, SFDO_NT));

		strs = sfdo_desktop_entry_get_implements(entry, &n_strs);
		print_list("Implements", strs, n_strs);

		switch (sfdo_desktop_entry_get_type(entry)) {
		case SFDO_DESKTOP_ENTRY_APPLICATION:
			print_boolean("DBusActivatable", sfdo_desktop_entry_get_dbus_activatable(entry));

			print_exec("Exec", sfdo_desktop_entry_get_exec(entry));

			str = sfdo_desktop_entry_get_try_exec(entry, &str_len);
			print_str("TryExec", str, str_len);
			str = sfdo_desktop_entry_get_path(entry, &str_len);
			print_str("Path", str, str_len);

			print_boolean("Terminal", sfdo_desktop_entry_get_terminal(entry));

			strs = sfdo_desktop_entry_get_mimetypes(entry, &n_strs);
			print_list("MimeType", strs, n_strs);
			strs = sfdo_desktop_entry_get_categories(entry, &n_strs);
			print_list("Categories", strs, n_strs);
			strs = sfdo_desktop_entry_get_keywords(entry, &n_strs);
			print_list("Keywords", strs, n_strs);

			const char *startup_notify = NULL;
			switch (sfdo_desktop_entry_get_startup_notify(entry)) {
			case SFDO_DESKTOP_ENTRY_STARTUP_NOTIFY_FALSE:
				startup_notify = "false";
				break;
			case SFDO_DESKTOP_ENTRY_STARTUP_NOTIFY_TRUE:
				startup_notify = "true";
				break;
			case SFDO_DESKTOP_ENTRY_STARTUP_NOTIFY_UNKNOWN:
				startup_notify = "unknown";
				break;
			}
			assert(startup_notify != NULL);
			printf("StartupNotify: %s\n", startup_notify);

			str = sfdo_desktop_entry_get_startup_wm_class(entry, &str_len);
			print_str("StartupWMClass", str, str_len);
			print_boolean(
					"PrefersNonDefaultGPU", sfdo_desktop_entry_get_prefers_non_default_gpu(entry));
			print_boolean("SingleMainWindow", sfdo_desktop_entry_get_single_main_window(entry));

			size_t n_actions;
			struct sfdo_desktop_entry_action **actions =
					sfdo_desktop_entry_get_actions(entry, &n_actions);
			printf("Actions:\n");
			for (size_t i = 0; i < n_actions; i++) {
				struct sfdo_desktop_entry_action *action = actions[i];

				str = sfdo_desktop_entry_action_get_id(action, &str_len);
				assert(str != NULL);
				printf(" %s (%zu bytes):\n", str, str_len);

				str = sfdo_desktop_entry_action_get_name(action, &str_len);
				print_str("  Name", str, str_len);
				str = sfdo_desktop_entry_action_get_icon(action, &str_len);
				print_str("  Icon", str, str_len);

				print_exec("  Exec", sfdo_desktop_entry_action_get_exec(action));
			}

			break;
		case SFDO_DESKTOP_ENTRY_LINK:
			str = sfdo_desktop_entry_get_url(entry, &str_len);
			print_str("URL", str, str_len);
			break;
		case SFDO_DESKTOP_ENTRY_DIRECTORY:
			break;
		}
	}

	sfdo_desktop_db_destroy(db);
	sfdo_desktop_ctx_destroy(ctx);
	sfdo_basedir_ctx_destroy(basedir_ctx);

	return 0;
}