update xdg-menu to use pyxdg and gtk via introspection (#737112)

epel9
Edward Sheldrake 14 years ago committed by Miroslav Lichvar
parent 544853c705
commit cf0bb2f901

@ -21,7 +21,7 @@ BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
Requires: %{name}-libs = %{version}-%{release} Requires: %{name}-libs = %{version}-%{release}
# required by xdg-menu and xdg-autostart scripts # required by xdg-menu and xdg-autostart scripts
Requires: gnome-menus pyxdg Requires: pyxdg
BuildRequires: gettext BuildRequires: gettext
BuildRequires: desktop-file-utils BuildRequires: desktop-file-utils

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/python
# #
# Copyright (C) 2008 Red Hat, Inc. # Copyright (C) 2008 Red Hat, Inc.
# #
@ -17,35 +17,82 @@
# #
# Author(s): Luke Macken <lmacken@redhat.com> # Author(s): Luke Macken <lmacken@redhat.com>
# Miroslav Lichvar <mlichvar@redhat.com> # Miroslav Lichvar <mlichvar@redhat.com>
# Edward Sheldrake <ejsheldrake@gmail.com>
import gmenu, re, sys import xdg.Menu, xdg.DesktopEntry, xdg.Config
import re, sys, os
from xml.sax.saxutils import escape 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): def walk_menu(entry):
if entry.get_type() == gmenu.TYPE_DIRECTORY: if isinstance(entry, xdg.Menu.Menu) and entry.Show is True:
print '<menu id="%s" label="%s">' \ print '<menu id="%s" label="%s"%s>' \
% (escape(entry.menu_id), escape(entry.get_name())) % (entry_name(entry),
map(walk_menu, entry.get_contents()) entry_name(entry),
escape(icon_attr(entry)))
map(walk_menu, entry.getEntries())
print '</menu>' print '</menu>'
elif entry.get_type() == gmenu.TYPE_ENTRY and not entry.is_excluded: elif isinstance(entry, xdg.Menu.MenuEntry) and entry.Show is True:
print ' <item label="%s">' % \ print ' <item label="%s"%s>' % \
escape(entry.get_name().replace('"', '')) (entry_name(entry.DesktopEntry).replace('"', ''),
command = re.sub(' -caption "%c"| -caption %c', ' -caption "%s"' % entry.get_name(), entry.get_exec()) 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) command = re.sub(' [^ ]*%[fFuUdDnNickvm]', '', command)
if entry.launch_in_terminal: if entry.DesktopEntry.getTerminal():
command = 'xterm -title "%s" -e %s' % \ command = 'xterm -title "%s" -e %s' % \
(entry.get_name(), command) (entry_name(entry.DesktopEntry), command)
print ' <action name="Execute">' + \ print ' <action name="Execute">' + \
'<command>%s</command></action>' % escape(command) '<command>%s</command></action>' % command
print ' </item>' print ' </item>'
if len(sys.argv) > 1: if len(sys.argv) > 1:
menu = sys.argv[1] + '.menu' menufile = sys.argv[1] + '.menu'
else: 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 '<?xml version="1.0" encoding="UTF-8"?>' print '<?xml version="1.0" encoding="UTF-8"?>'
print '<openbox_pipe_menu>' print '<openbox_pipe_menu>'
map(walk_menu, gmenu.lookup_tree(menu).root.get_contents()) map(walk_menu, menu.getEntries())
print '</openbox_pipe_menu>' print '</openbox_pipe_menu>'

Loading…
Cancel
Save