[openbox/f16] update xdg-menu to use pyxdg and gtk via introspection (#737112)

Miroslav Lichvar mlichvar at fedoraproject.org
Fri Sep 30 11:41:25 UTC 2011


commit cf0bb2f9013d8d2c61a59fbf8c3ce3d6344867eb
Author: Edward Sheldrake <ejsheldrake at gmail.com>
Date:   Fri Sep 30 13:18:32 2011 +0200

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

 openbox.spec |    2 +-
 xdg-menu     |   79 ++++++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 64 insertions(+), 17 deletions(-)
---
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 <lmacken at redhat.com>
 #            Miroslav Lichvar <mlichvar at redhat.com>
+#            Edward Sheldrake <ejsheldrake at 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>'


More information about the scm-commits mailing list