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
|
/*
* Copyright (C) 2004 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <glib/gi18n.h>
#ifdef ENABLE_NLS
#include <locale.h>
#endif
#include "matemenu-tree.h"
#include <string.h>
static char *menu_file = NULL;
static gboolean monitor = FALSE;
static gboolean include_excluded = FALSE;
static gboolean include_nodisplay = FALSE;
static gboolean include_unallocated = FALSE;
static GOptionEntry options[] = {
{ "file", 'f', 0, G_OPTION_ARG_STRING, &menu_file, N_("Menu file"), N_("MENU_FILE") },
{ "monitor", 'm', 0, G_OPTION_ARG_NONE, &monitor, N_("Monitor for menu changes"), NULL },
{ "include-excluded", 'i', 0, G_OPTION_ARG_NONE, &include_excluded, N_("Include <Exclude>d entries"), NULL },
{ "include-nodisplay", 'n', 0, G_OPTION_ARG_NONE, &include_nodisplay, N_("Include NoDisplay=true entries"), NULL },
{ "include-unallocated", 'u', 0, G_OPTION_ARG_NONE, &include_unallocated, N_("Include unallocated entries"), NULL },
{ NULL }
};
static void
append_directory_path (MateMenuTreeDirectory *directory,
GString *path)
{
MateMenuTreeDirectory *parent;
parent = matemenu_tree_directory_get_parent(directory);
if (!parent)
{
g_string_append_c(path, '/');
return;
}
append_directory_path(parent, path);
g_string_append(path, matemenu_tree_directory_get_name (directory));
g_string_append_c(path, '/');
matemenu_tree_item_unref(parent);
}
static char *
make_path (MateMenuTreeDirectory *directory)
{
GString *path;
g_return_val_if_fail(directory != NULL, NULL);
path = g_string_new(NULL);
append_directory_path(directory, path);
return g_string_free(path, FALSE);
}
static void
print_entry (MateMenuTreeEntry *entry,
const char *path)
{
char *utf8_path;
char *utf8_file_id;
utf8_path = g_filename_to_utf8(matemenu_tree_entry_get_desktop_file_path (entry),
-1, NULL, NULL, NULL);
utf8_file_id = g_filename_to_utf8(matemenu_tree_entry_get_desktop_file_id (entry),
-1, NULL, NULL, NULL);
g_print("%s\t%s\t%s%s\n",
path,
utf8_file_id ? utf8_file_id : _("Invalid desktop file ID"),
utf8_path ? utf8_path : _("[Invalid Filename]"),
matemenu_tree_entry_get_is_excluded(entry) ? _(" <excluded>") : "");
g_free(utf8_file_id);
g_free(utf8_path);
}
static void
print_directory(MateMenuTreeDirectory *directory)
{
MateMenuTreeIter *iter;
const char *path;
char *freeme;
freeme = make_path(directory);
if (!strcmp (freeme, "/"))
path = freeme;
else
path = freeme + 1;
iter = matemenu_tree_directory_iter(directory);
while(TRUE)
{
gpointer item;
switch (matemenu_tree_iter_next (iter))
{
case MATEMENU_TREE_ITEM_INVALID:
goto done;
case MATEMENU_TREE_ITEM_ENTRY:
item = matemenu_tree_iter_get_entry(iter);
print_entry((MateMenuTreeEntry*)item, path);
break;
case MATEMENU_TREE_ITEM_DIRECTORY:
item = matemenu_tree_iter_get_directory(iter);
print_directory((MateMenuTreeDirectory*)item);
break;
case MATEMENU_TREE_ITEM_HEADER:
case MATEMENU_TREE_ITEM_SEPARATOR:
item = NULL;
break;
case MATEMENU_TREE_ITEM_ALIAS:
{
item = matemenu_tree_iter_get_alias(iter);
if (matemenu_tree_alias_get_aliased_item_type (item) == MATEMENU_TREE_ITEM_ENTRY)
{
MateMenuTreeEntry *entry = matemenu_tree_alias_get_aliased_entry(item);
print_entry(entry, path);
matemenu_tree_item_unref(entry);
}
}
break;
default:
g_assert_not_reached();
break;
}
matemenu_tree_item_unref(item);
continue;
done:
break;
}
matemenu_tree_iter_unref(iter);
g_free(freeme);
}
static void
handle_tree_changed (MateMenuTree *tree)
{
MateMenuTreeDirectory *root;
GError *error = NULL;
g_print(_("\n\n\n==== Menu changed, reloading ====\n\n\n"));
if(!matemenu_tree_load_sync (tree, &error))
{
g_printerr("Failed to load tree: %s\n", error->message);
g_clear_error(&error);
return;
}
root = matemenu_tree_get_root_directory(tree);
if (root == NULL)
{
g_warning(_("Menu tree is empty"));
return;
}
print_directory(root);
matemenu_tree_item_unref(root);
}
int
main (int argc, char **argv)
{
GOptionContext *options_context;
MateMenuTree *tree;
MateMenuTreeDirectory *root;
MateMenuTreeFlags flags;
GError *error = NULL;
#ifdef ENABLE_NLS
setlocale(LC_ALL, "");
bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
#endif /* ENABLE_NLS */
options_context = g_option_context_new(_("- test MATE's implementation of the Desktop Menu Specification"));
g_option_context_add_main_entries(options_context, options, GETTEXT_PACKAGE);
g_option_context_parse(options_context, &argc, &argv, NULL);
g_option_context_free(options_context);
flags = MATEMENU_TREE_FLAGS_NONE;
if (include_excluded)
flags |= MATEMENU_TREE_FLAGS_INCLUDE_EXCLUDED;
if (include_nodisplay)
flags |= MATEMENU_TREE_FLAGS_INCLUDE_NODISPLAY;
if (include_unallocated)
flags |= MATEMENU_TREE_FLAGS_INCLUDE_UNALLOCATED;
tree = matemenu_tree_new(menu_file ? menu_file : "mate-applications.menu", flags);
g_assert(tree != NULL);
if (!matemenu_tree_load_sync (tree, &error))
{
g_printerr("Failed to load tree: %s\n", error->message);
return 1;
}
g_print("Loaded menu from %s\n", matemenu_tree_get_canonical_menu_path(tree));
root = matemenu_tree_get_root_directory(tree);
if (root != NULL)
{
print_directory(root);
matemenu_tree_item_unref(root);
}
else
{
g_warning(_("Menu tree is empty"));
}
if (monitor)
{
GMainLoop *main_loop;
g_signal_connect(tree, "changed", G_CALLBACK(handle_tree_changed), NULL);
main_loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(main_loop);
g_main_loop_unref(main_loop);
}
g_object_unref(tree);
return 0;
}
|