update xdg-menu to use pyxdg and gtk via introspection (#737112)
This commit is contained in:
parent
544853c705
commit
cf0bb2f901
2 changed files with 64 additions and 17 deletions
|
|
@ -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
|
||||
|
|
|
|||
79
xdg-menu
79
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 <lmacken@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
|
||||
|
||||
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 '<menu id="%s" label="%s">' \
|
||||
% (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 '<menu id="%s" label="%s"%s>' \
|
||||
% (entry_name(entry),
|
||||
entry_name(entry),
|
||||
escape(icon_attr(entry)))
|
||||
map(walk_menu, entry.getEntries())
|
||||
print '</menu>'
|
||||
elif entry.get_type() == gmenu.TYPE_ENTRY and not entry.is_excluded:
|
||||
print ' <item label="%s">' % \
|
||||
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 ' <item label="%s"%s>' % \
|
||||
(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 ' <action name="Execute">' + \
|
||||
'<command>%s</command></action>' % escape(command)
|
||||
'<command>%s</command></action>' % command
|
||||
print ' </item>'
|
||||
|
||||
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 '<?xml version="1.0" encoding="UTF-8"?>'
|
||||
print '<openbox_pipe_menu>'
|
||||
map(walk_menu, gmenu.lookup_tree(menu).root.get_contents())
|
||||
map(walk_menu, menu.getEntries())
|
||||
print '</openbox_pipe_menu>'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue