From cf0bb2f9013d8d2c61a59fbf8c3ce3d6344867eb Mon Sep 17 00:00:00 2001 From: Edward Sheldrake Date: Sep 30 2011 11:21:40 +0000 Subject: update xdg-menu to use pyxdg and gtk via introspection (#737112) --- diff --git a/openbox.spec b/openbox.spec index 18c1c59..76184bd 100644 --- a/openbox.spec +++ b/openbox.spec @@ -21,7 +21,7 @@ BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) Requires: %{name}-libs = %{version}-%{release} # required by xdg-menu and xdg-autostart scripts -Requires: gnome-menus pyxdg +Requires: pyxdg BuildRequires: gettext BuildRequires: desktop-file-utils diff --git a/xdg-menu b/xdg-menu index 72d5ad8..71f9026 100644 --- a/xdg-menu +++ b/xdg-menu @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python # # Copyright (C) 2008 Red Hat, Inc. # @@ -17,35 +17,82 @@ # # Author(s): Luke Macken # Miroslav Lichvar +# Edward Sheldrake -import gmenu, re, sys +import xdg.Menu, xdg.DesktopEntry, xdg.Config +import re, sys, os from xml.sax.saxutils import escape +icons = True +try: + from gi.repository import Gtk +except ImportError: + icons = False + +def icon_attr(entry): + if icons is False: + return '' + + name = entry.getIcon() + + if os.path.exists(name): + return ' icon="' + name + '"' + + # work around broken .desktop files + # unless the icon is a full path it should not have an extension + name = re.sub('\..{3,4}$', '', name) + + # imlib2 cannot load svg + iconinfo = theme.lookup_icon(name, 22, Gtk.IconLookupFlags.NO_SVG) + if iconinfo: + iconfile = iconinfo.get_filename() + iconinfo.free() + return ' icon="' + iconfile + '"' + return '' + +def entry_name(entry): + return escape(entry.getName().encode('utf-8', 'xmlcharrefreplace')) + def walk_menu(entry): - if entry.get_type() == gmenu.TYPE_DIRECTORY: - print '' \ - % (escape(entry.menu_id), escape(entry.get_name())) - map(walk_menu, entry.get_contents()) + if isinstance(entry, xdg.Menu.Menu) and entry.Show is True: + print '' \ + % (entry_name(entry), + entry_name(entry), + escape(icon_attr(entry))) + map(walk_menu, entry.getEntries()) print '' - elif entry.get_type() == gmenu.TYPE_ENTRY and not entry.is_excluded: - print ' ' % \ - escape(entry.get_name().replace('"', '')) - command = re.sub(' -caption "%c"| -caption %c', ' -caption "%s"' % entry.get_name(), entry.get_exec()) + elif isinstance(entry, xdg.Menu.MenuEntry) and entry.Show is True: + print ' ' % \ + (entry_name(entry.DesktopEntry).replace('"', ''), + escape(icon_attr(entry.DesktopEntry))) + command = re.sub(' -caption "%c"| -caption %c', ' -caption "%s"' % entry_name(entry.DesktopEntry), entry.DesktopEntry.getExec()) command = re.sub(' [^ ]*%[fFuUdDnNickvm]', '', command) - if entry.launch_in_terminal: + if entry.DesktopEntry.getTerminal(): command = 'xterm -title "%s" -e %s' % \ - (entry.get_name(), command) + (entry_name(entry.DesktopEntry), command) print ' ' + \ - '%s' % escape(command) + '%s' % command print ' ' if len(sys.argv) > 1: - menu = sys.argv[1] + '.menu' + menufile = sys.argv[1] + '.menu' else: - menu = 'applications.menu' + menufile = 'applications.menu' + +lang = os.environ.get('LANG') +if lang: + xdg.Config.setLocale(lang) + +# lie to get the same menu as in GNOME +xdg.Config.setWindowManager('GNOME') + +if icons: + theme = Gtk.IconTheme.get_default() + +menu = xdg.Menu.parse(menufile) print '' print '' -map(walk_menu, gmenu.lookup_tree(menu).root.get_contents()) +map(walk_menu, menu.getEntries()) print ''