summaryrefslogtreecommitdiff
path: root/libmenu/menu-util.c
blob: 2f992b0183efe72a92866e5432b70d57809487ae (plain)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* Random utility functions for menu code */

/*
 * Copyright (C) 2003 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include "menu-util.h"

#include <stdio.h>
#include <stdarg.h>


#ifdef G_ENABLE_DEBUG

static gboolean verbose = FALSE;
static gboolean initted = FALSE;

static inline gboolean menu_verbose_enabled(void)
{
	if (!initted)
	{
		verbose = g_getenv("MENU_VERBOSE") != NULL;
		initted = TRUE;
	}

	return verbose;
}

static int utf8_fputs(const char* str, FILE* f)
{
	char* l;
	int ret;

	l = g_locale_from_utf8(str, -1, NULL, NULL, NULL);

	if (l == NULL)
	{
		ret = fputs(str, f); /* just print it anyway, better than nothing */
	}
	else
	{
		ret = fputs(l, f);
	}

	g_free(l);

	return ret;
}

void menu_verbose(const char* format, ...)
{
	va_list args;
	char* str;

	if (!menu_verbose_enabled())
	{
		return;
	}

	va_start(args, format);
	str = g_strdup_vprintf(format, args);
	va_end(args);

	utf8_fputs(str, stderr);
	fflush(stderr);

	g_free(str);
}

static void append_to_string(MenuLayoutNode* node, gboolean onelevel, int depth, GString* str);

static void append_spaces(GString* str, int depth)
{
	while (depth > 0)
	{
		g_string_append_c(str, ' ');
		--depth;
	}
}

static void append_children(MenuLayoutNode* node, int depth, GString* str)
{
	MenuLayoutNode* iter;

	iter = menu_layout_node_get_children(node);

	while (iter != NULL)
	{
		append_to_string(iter, FALSE, depth, str);

		iter = menu_layout_node_get_next(iter);
	}
}

static void append_simple_with_attr(MenuLayoutNode* node, int depth, const char* node_name, const char* attr_name, const char* attr_value, GString* str)
{
	const char* content;

	append_spaces(str, depth);

	if ((content = menu_layout_node_get_content(node)))
	{
		char* escaped;

		escaped = g_markup_escape_text(content, -1);

		if (attr_name && attr_value)
		{
			char* attr_escaped;

			attr_escaped = g_markup_escape_text(attr_value, -1);

			g_string_append_printf(str, "<%s %s=\"%s\">%s</%s>\n", node_name, attr_name, attr_escaped, escaped, node_name);

			g_free(attr_escaped);
		}
		else
		{
			g_string_append_printf(str, "<%s>%s</%s>\n", node_name, escaped, node_name);
		}

		g_free(escaped);
	}
	else
	{
		if (attr_name && attr_value)
		{
			char* attr_escaped;

			attr_escaped = g_markup_escape_text(attr_value, -1);

			g_string_append_printf(str, "<%s %s=\"%s\"/>\n", node_name, attr_name, attr_escaped);

			g_free(attr_escaped);
		}
		else
		{
			g_string_append_printf(str, "<%s/>\n", node_name);
		}
	}
}

static void append_layout(MenuLayoutNode* node, int depth, const char* node_name, MenuLayoutValues* layout_values, GString* str)
{
	const char* content;

	append_spaces(str, depth);

	if ((content = menu_layout_node_get_content(node)))
	{
		char* escaped;

		escaped = g_markup_escape_text(content, -1);

		g_string_append_printf(str,
			"<%s show_empty=\"%s\" inline=\"%s\" inline_header=\"%s\""
			" inline_alias=\"%s\" inline_limit=\"%d\">%s</%s>\n",
			node_name,
			layout_values->show_empty    ? "true" : "false",
			layout_values->inline_menus  ? "true" : "false",
			layout_values->inline_header ? "true" : "false",
			layout_values->inline_alias  ? "true" : "false",
			layout_values->inline_limit,
			escaped,
			node_name);

		g_free(escaped);
	}
	else
	{
	  g_string_append_printf(str,
		"<%s show_empty=\"%s\" inline=\"%s\" inline_header=\"%s\""
		" inline_alias=\"%s\" inline_limit=\"%d\"/>\n",
		node_name,
		layout_values->show_empty    ? "true" : "false",
		layout_values->inline_menus  ? "true" : "false",
		layout_values->inline_header ? "true" : "false",
		layout_values->inline_alias  ? "true" : "false",
		layout_values->inline_limit);
	}
}

static void append_merge(MenuLayoutNode* node, int depth, const char* node_name, MenuLayoutMergeType merge_type, GString* str)
{
	const char* merge_type_str;

	merge_type_str = NULL;

	switch (merge_type)
	{
		case MENU_LAYOUT_MERGE_NONE:
			merge_type_str = "none";
			break;

		case MENU_LAYOUT_MERGE_MENUS:
			merge_type_str = "menus";
			break;

		case MENU_LAYOUT_MERGE_FILES:
			merge_type_str = "files";
			break;

		case MENU_LAYOUT_MERGE_ALL:
			merge_type_str = "all";
			break;

		default:
			g_assert_not_reached();
			break;
	}

	append_simple_with_attr(node, depth, node_name, "type", merge_type_str, str);
}

static void append_simple(MenuLayoutNode* node, int depth, const char* node_name, GString* str)
{
	append_simple_with_attr(node, depth, node_name, NULL, NULL, str);
}

static void append_start(MenuLayoutNode* node, int depth, const char* node_name, GString* str)
{
	append_spaces(str, depth);

	g_string_append_printf(str, "<%s>\n", node_name);
}

static void append_end(MenuLayoutNode* node, int depth, const char* node_name, GString* str)
{
	append_spaces(str, depth);

	g_string_append_printf(str, "</%s>\n", node_name);
}

static void append_container(MenuLayoutNode* node, gboolean onelevel, int depth, const char* node_name, GString* str)
{
	append_start(node, depth, node_name, str);

	if (!onelevel)
	{
		append_children(node, depth + 2, str);
		append_end(node, depth, node_name, str);
	}
}

static void append_to_string(MenuLayoutNode* node, gboolean onelevel, int depth, GString* str)
{
	MenuLayoutValues layout_values;

	switch (menu_layout_node_get_type(node))
	{
		case MENU_LAYOUT_NODE_ROOT:
			if (!onelevel)
				append_children(node, depth - 1, str); /* -1 to ignore depth of root */
			else
			append_start(node, depth - 1, "Root", str);
			break;

		case MENU_LAYOUT_NODE_PASSTHROUGH:
			g_string_append(str, menu_layout_node_get_content(node));
			g_string_append_c(str, '\n');
			break;

		case MENU_LAYOUT_NODE_MENU:
			append_container(node, onelevel, depth, "Menu", str);
			break;

		case MENU_LAYOUT_NODE_APP_DIR:
			append_simple(node, depth, "AppDir", str);
			break;

		case MENU_LAYOUT_NODE_DEFAULT_APP_DIRS:
			append_simple(node, depth, "DefaultAppDirs", str);
			break;

		case MENU_LAYOUT_NODE_DIRECTORY_DIR:
			append_simple(node, depth, "DirectoryDir", str);
			break;

		case MENU_LAYOUT_NODE_DEFAULT_DIRECTORY_DIRS:
			append_simple(node, depth, "DefaultDirectoryDirs", str);
			break;

		case MENU_LAYOUT_NODE_DEFAULT_MERGE_DIRS:
			append_simple(node, depth, "DefaultMergeDirs", str);
			break;

		case MENU_LAYOUT_NODE_NAME:
			append_simple(node, depth, "Name", str);
			break;

		case MENU_LAYOUT_NODE_DIRECTORY:
			append_simple(node, depth, "Directory", str);
			break;

		case MENU_LAYOUT_NODE_ONLY_UNALLOCATED:
			append_simple(node, depth, "OnlyUnallocated", str);
			break;

		case MENU_LAYOUT_NODE_NOT_ONLY_UNALLOCATED:
			append_simple(node, depth, "NotOnlyUnallocated", str);
			break;

		case MENU_LAYOUT_NODE_INCLUDE:
			append_container(node, onelevel, depth, "Include", str);
			break;

		case MENU_LAYOUT_NODE_EXCLUDE:
			append_container(node, onelevel, depth, "Exclude", str);
			break;

		case MENU_LAYOUT_NODE_FILENAME:
			append_simple(node, depth, "Filename", str);
			break;

		case MENU_LAYOUT_NODE_CATEGORY:
			append_simple(node, depth, "Category", str);
			break;

		case MENU_LAYOUT_NODE_ALL:
			append_simple(node, depth, "All", str);
			break;

		case MENU_LAYOUT_NODE_AND:
			append_container(node, onelevel, depth, "And", str);
			break;

		case MENU_LAYOUT_NODE_OR:
			append_container(node, onelevel, depth, "Or", str);
			break;

		case MENU_LAYOUT_NODE_NOT:
			append_container(node, onelevel, depth, "Not", str);
			break;

		case MENU_LAYOUT_NODE_MERGE_FILE:
		{
			MenuMergeFileType type;

			type = menu_layout_node_merge_file_get_type(node);

			append_simple_with_attr(node, depth, "MergeFile", "type", type == MENU_MERGE_FILE_TYPE_PARENT ? "parent" : "path", str);
			break;
		}

		case MENU_LAYOUT_NODE_MERGE_DIR:
			append_simple(node, depth, "MergeDir", str);
			break;

		case MENU_LAYOUT_NODE_LEGACY_DIR:
			append_simple_with_attr(node, depth, "LegacyDir", "prefix", menu_layout_node_legacy_dir_get_prefix (node), str);
			break;

		case MENU_LAYOUT_NODE_KDE_LEGACY_DIRS:
			append_simple(node, depth, "KDELegacyDirs", str);
			break;

		case MENU_LAYOUT_NODE_MOVE:
			append_container(node, onelevel, depth, "Move", str);
			break;

		case MENU_LAYOUT_NODE_OLD:
			append_simple(node, depth, "Old", str);
			break;

		case MENU_LAYOUT_NODE_NEW:
			append_simple(node, depth, "New", str);
			break;

		case MENU_LAYOUT_NODE_DELETED:
			append_simple(node, depth, "Deleted", str);
			break;

		case MENU_LAYOUT_NODE_NOT_DELETED:
			append_simple(node, depth, "NotDeleted", str);
			break;

		case MENU_LAYOUT_NODE_LAYOUT:
			append_container(node, onelevel, depth, "Layout", str);
			break;

		case MENU_LAYOUT_NODE_DEFAULT_LAYOUT:
			menu_layout_node_default_layout_get_values(node, &layout_values);
			append_layout(node, depth, "DefaultLayout", &layout_values, str);
			break;

		case MENU_LAYOUT_NODE_MENUNAME:
			menu_layout_node_menuname_get_values(node, &layout_values);
			append_layout(node, depth, "MenuName", &layout_values, str);
			break;

		case MENU_LAYOUT_NODE_SEPARATOR:
			append_simple(node, depth, "Name", str);
			break;

		case MENU_LAYOUT_NODE_MERGE:
			append_merge(node, depth, "Merge", menu_layout_node_merge_get_type(node), str);
			break;

		default:
			g_assert_not_reached();
			break;
	}
}

void menu_debug_print_layout(MenuLayoutNode* node, gboolean onelevel)
{
	if (menu_verbose_enabled())
	{
		GString* str = g_string_new(NULL);
		append_to_string(node, onelevel, 0, str);

		utf8_fputs(str->str, stderr);
		fflush(stderr);

		g_string_free(str, TRUE);
	}
}

#endif /* G_ENABLE_DEBUG */