diff options
-rw-r--r-- | util/Makefile.am | 2 | ||||
-rw-r--r-- | util/mate-menus-ls.py | 48 | ||||
-rw-r--r-- | util/test-menu-spec.c | 187 |
3 files changed, 127 insertions, 110 deletions
diff --git a/util/Makefile.am b/util/Makefile.am index 71e3e69..064518e 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -18,7 +18,7 @@ mate_menu_spec_test_LDADD = \ ../libmenu/libmate-menu.la exampledir = $(pkgdatadir)/examples -example_DATA = mate-menus-ls.py mate-menus-ls.js +example_DATA = mate-menus-ls.py EXTRA_DIST = $(example_DATA) diff --git a/util/mate-menus-ls.py b/util/mate-menus-ls.py index 92f303f..65c32ac 100644 --- a/util/mate-menus-ls.py +++ b/util/mate-menus-ls.py @@ -23,16 +23,9 @@ import optparse import sys - -import matemenu - -def print_entry(entry, path): - if entry.get_is_excluded(): - excluded = ' <excluded>' - else: - excluded = '' - - print '%s\t%s\t%s%s' % (path, entry.get_desktop_file_id(), entry.get_desktop_file_path(), excluded) +import gi +gi.require_version('MateMenu', '2.0') +from gi.repository import MateMenu def print_directory(dir, parent_path = None): if not parent_path: @@ -40,20 +33,15 @@ def print_directory(dir, parent_path = None): else: path = '%s%s/' % (parent_path, dir.get_name()) - for item in dir.get_contents(): - type = item.get_type() - if type == matemenu.TYPE_ENTRY: - print_entry(item, path) - elif type == matemenu.TYPE_DIRECTORY: - print_directory(item, path) - elif type == matemenu.TYPE_ALIAS: - aliased = item.get_item() - if aliased.get_type() == matemenu.TYPE_ENTRY: - print_entry(aliased, path) - elif type in [ matemenu.TYPE_HEADER, matemenu.TYPE_SEPARATOR ]: - pass - else: - print >> sys.stderr, 'Unsupported item type: %s' % type + iter = dir.iter() + nextType = iter.next() + while(nextType != MateMenu.TreeItemType.INVALID): + if (nextType == MateMenu.TreeItemType.ENTRY): + entry = iter.get_entry() + print(path + "\t" + entry.get_app_info().get_name() + "\t" + entry.get_desktop_file_path()) + elif (nextType == MateMenu.TreeItemType.DIRECTORY): + print_directory(iter.get_directory(), path); + nextType = iter.next() def main(args): parser = optparse.OptionParser() @@ -74,17 +62,17 @@ def main(args): else: menu_file = 'mate-applications.menu' - flags = matemenu.FLAGS_NONE + flags = MateMenu.TreeFlags.NONE if options.exclude: - flags |= matemenu.FLAGS_INCLUDE_EXCLUDED + flags |= MateMenu.TreeFlags.INCLUDE_EXCLUDED if options.nodisplay: - flags |= matemenu.FLAGS_INCLUDE_NODISPLAY - - tree = matemenu.lookup_tree(menu_file, flags) + flags |= MateMenu.TreeFlags.INCLUDE_NODISPLAY + tree = MateMenu.Tree(menu_basename = "mate-applications.menu", flags = flags) + tree.load_sync(); root = tree.get_root_directory() if not root: - print 'Menu tree is empty' + print('Menu tree is empty') else: print_directory(root) diff --git a/util/test-menu-spec.c b/util/test-menu-spec.c index cda6e02..bb621fe 100644 --- a/util/test-menu-spec.c +++ b/util/test-menu-spec.c @@ -18,34 +18,34 @@ */ #include <config.h> +#include <glib/gi18n.h> #include "matemenu-tree.h" #include <string.h> -/* This is only a test program, so we don't need translations. Still keep the - * infrastructure in place in case we suddenly decide we want to localize this - * program. Don't forget to reenable the call to bindtextdomain() if going back - * to real localization. */ -#define _(x) x -#define N_(x) x - -static char* menu_file = NULL; -static gboolean monitor = FALSE; -static gboolean include_excluded = FALSE; -static gboolean include_nodisplay = FALSE; +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}, - {NULL} + { "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) +static void +append_directory_path (MateMenuTreeDirectory *directory, + GString *path) { - MateMenuTreeDirectory* parent = matemenu_tree_item_get_parent(MATEMENU_TREE_ITEM(directory)); + MateMenuTreeDirectory *parent; + + parent = matemenu_tree_directory_get_parent(directory); if (!parent) { @@ -55,81 +55,97 @@ static void append_directory_path(MateMenuTreeDirectory* directory, GString* pat append_directory_path(parent, path); - g_string_append(path, matemenu_tree_directory_get_name(directory)); + 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) +static char * +make_path (MateMenuTreeDirectory *directory) { + GString *path; + g_return_val_if_fail(directory != NULL, NULL); - GString* path = g_string_new(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) +static void +print_entry (MateMenuTreeEntry *entry, + const char *path) { - char* utf8_path = g_filename_to_utf8(matemenu_tree_entry_get_desktop_file_path(entry), -1, NULL, NULL, NULL); - char* utf8_file_id = g_filename_to_utf8(matemenu_tree_entry_get_desktop_file_id(entry), -1, NULL, NULL, NULL); + 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); - g_print("%s %s %s%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>") : "", - matemenu_tree_entry_get_is_nodisplay(entry) ? _(" <nodisplay>") : ""); + 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) +static void +print_directory(MateMenuTreeDirectory *directory) { - const char* path; - char* freeme = make_path(directory); + MateMenuTreeIter *iter; + const char *path; + char *freeme; - if (!strcmp(freeme, "/")) - { + freeme = make_path(directory); + if (!strcmp (freeme, "/")) path = freeme; - } else - { path = freeme + 1; - } - GSList* items = matemenu_tree_directory_get_contents(directory); - GSList* tmp = items; + iter = matemenu_tree_directory_iter(directory); - while (tmp != NULL) + while(TRUE) { - MateMenuTreeItem* item = tmp->data; + gpointer item; - switch (matemenu_tree_item_get_type(item)) + switch (matemenu_tree_iter_next (iter)) { + case MATEMENU_TREE_ITEM_INVALID: + goto done; + case MATEMENU_TREE_ITEM_ENTRY: - print_entry(MATEMENU_TREE_ENTRY(item), path); + item = matemenu_tree_iter_get_entry(iter); + print_entry((MateMenuTreeEntry*)item, path); break; case MATEMENU_TREE_ITEM_DIRECTORY: - print_directory(MATEMENU_TREE_DIRECTORY(item)); + 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: { - MateMenuTreeItem* aliased_item = matemenu_tree_alias_get_item(MATEMENU_TREE_ALIAS(item)); + item = matemenu_tree_iter_get_alias(iter); - if (matemenu_tree_item_get_type(aliased_item) == MATEMENU_TREE_ITEM_ENTRY) + if (matemenu_tree_alias_get_aliased_item_type (item) == MATEMENU_TREE_ITEM_ENTRY) { - print_entry(MATEMENU_TREE_ENTRY(aliased_item), path); + MateMenuTreeEntry *entry = matemenu_tree_alias_get_aliased_entry(item); + print_entry(entry, path); + matemenu_tree_item_unref(entry); } } break; @@ -139,22 +155,33 @@ static void print_directory(MateMenuTreeDirectory* directory) break; } - matemenu_tree_item_unref(tmp->data); - - tmp = tmp->next; + matemenu_tree_item_unref(item); + continue; +done: + break; } - g_slist_free(items); + matemenu_tree_iter_unref(iter); g_free(freeme); } -static void handle_tree_changed(MateMenuTree* tree) +static void +handle_tree_changed (MateMenuTree *tree) { + MateMenuTreeDirectory *root; + GError *error = NULL; + g_print(_("\n\n\n==== Menu changed, reloading ====\n\n\n")); - MateMenuTreeDirectory* root = matemenu_tree_get_root_directory(tree); + 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")); @@ -165,39 +192,41 @@ static void handle_tree_changed(MateMenuTree* tree) matemenu_tree_item_unref(root); } -int main(int argc, char** argv) +int +main (int argc, char **argv) { - #if 0 - /* See comment when defining _() at the top of this file. */ - bindtextdomain(GETTEXT_PACKAGE, MATELOCALEDIR); - bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); - textdomain(GETTEXT_PACKAGE); - #endif - - GOptionContext* options_context = g_option_context_new(_("- test MATE's implementation of the Desktop Menu Specification")); + GOptionContext *options_context; + MateMenuTree *tree; + MateMenuTreeDirectory *root; + MateMenuTreeFlags flags; + GError *error = NULL; + + setlocale(LC_ALL, ""); + 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); - MateMenuTreeFlags flags = MATEMENU_TREE_FLAGS_NONE; - + flags = MATEMENU_TREE_FLAGS_NONE; if (include_excluded) - { flags |= MATEMENU_TREE_FLAGS_INCLUDE_EXCLUDED; - } - if (include_nodisplay) - { flags |= MATEMENU_TREE_FLAGS_INCLUDE_NODISPLAY; - } - - // Usamos applications.menu is existe. Para compatibilidad con GNOME - MateMenuTree* tree = matemenu_tree_lookup(menu_file ? menu_file : "mate-applications.menu", flags); + 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); - MateMenuTreeDirectory* root = matemenu_tree_get_root_directory(tree); + 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); @@ -210,16 +239,16 @@ int main(int argc, char** argv) if (monitor) { - matemenu_tree_add_monitor(tree, (MateMenuTreeChangedFunc) handle_tree_changed, NULL); + GMainLoop *main_loop; + + g_signal_connect(tree, "changed", G_CALLBACK(handle_tree_changed), NULL); - GMainLoop* main_loop = g_main_loop_new(NULL, FALSE); + main_loop = g_main_loop_new(NULL, FALSE); g_main_loop_run(main_loop); g_main_loop_unref(main_loop); - - matemenu_tree_remove_monitor(tree, (MateMenuTreeChangedFunc) handle_tree_changed, NULL); } - matemenu_tree_unref(tree); + g_object_unref(tree); return 0; } |