aa3375f
#!/usr/bin/python3
be11092
#
be11092
# Copyright (C) 2008  Red Hat, Inc.
be11092
#
be11092
# This program is free software; you can redistribute it and/or modify
be11092
# it under the terms of the GNU General Public License as published by
be11092
# the Free Software Foundation; either version 2 of the License, or
be11092
# (at your option) any later version.
be11092
#
be11092
# This program is distributed in the hope that it will be useful,
be11092
# but WITHOUT ANY WARRANTY; without even the implied warranty of
be11092
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
be11092
# GNU General Public License for more details.
be11092
#
be11092
# You should have received a copy of the GNU General Public License
be11092
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
be11092
#
be11092
# Author(s): Luke Macken <lmacken@redhat.com>
be11092
#            Miroslav Lichvar <mlichvar@redhat.com>
Edward Sheldrake cf0bb2f
#            Edward Sheldrake <ejsheldrake@gmail.com>
be11092
3f61e81
Edward Sheldrake cf0bb2f
import xdg.Menu, xdg.DesktopEntry, xdg.Config
Edward Sheldrake cf0bb2f
import re, sys, os
3f61e81
from xml.sax.saxutils import escape
3f61e81
Edward Sheldrake cf0bb2f
icons = True
Edward Sheldrake cf0bb2f
try:
aa3375f
	import gi
aa3375f
	gi.require_version('Gtk', '3.0')
Edward Sheldrake cf0bb2f
	from gi.repository import Gtk
Edward Sheldrake cf0bb2f
except ImportError:
Edward Sheldrake cf0bb2f
	icons = False
Edward Sheldrake cf0bb2f
Edward Sheldrake cf0bb2f
def icon_attr(entry):
Edward Sheldrake cf0bb2f
	if icons is False:
Edward Sheldrake cf0bb2f
		return ''
Edward Sheldrake cf0bb2f
Edward Sheldrake cf0bb2f
	name = entry.getIcon()
Edward Sheldrake cf0bb2f
Edward Sheldrake cf0bb2f
	if os.path.exists(name):
Edward Sheldrake cf0bb2f
		return ' icon="' + name + '"'
Edward Sheldrake cf0bb2f
Edward Sheldrake cf0bb2f
	# work around broken .desktop files
Edward Sheldrake cf0bb2f
	# unless the icon is a full path it should not have an extension
Edward Sheldrake cf0bb2f
	name = re.sub('\..{3,4}$', '', name)
Edward Sheldrake cf0bb2f
Edward Sheldrake cf0bb2f
	# imlib2 cannot load svg
Edward Sheldrake cf0bb2f
	iconinfo = theme.lookup_icon(name, 22, Gtk.IconLookupFlags.NO_SVG)
Edward Sheldrake cf0bb2f
	if iconinfo:
Edward Sheldrake cf0bb2f
		iconfile = iconinfo.get_filename()
4edb1fd
		if hasattr(iconinfo, 'free'):
4edb1fd
			iconinfo.free()
Ralph Giles 3221497
		if iconfile:
Ralph Giles 3221497
			return ' icon="' + iconfile + '"'
Edward Sheldrake cf0bb2f
	return ''
Edward Sheldrake cf0bb2f
Edward Sheldrake 27345f9
def escape_utf8(s):
aa3375f
	if sys.version_info[0] < 3 and isinstance(s, unicode):
5bb02a8
		s = s.encode('utf-8', 'xmlcharrefreplace')
5bb02a8
	return escape(s)
Edward Sheldrake 27345f9
Edward Sheldrake cf0bb2f
def entry_name(entry):
Edward Sheldrake 27345f9
	return escape_utf8(entry.getName())
Edward Sheldrake cf0bb2f
3f61e81
def walk_menu(entry):
Edward Sheldrake cf0bb2f
	if isinstance(entry, xdg.Menu.Menu) and entry.Show is True:
aa3375f
		print('<menu id="%s" label="%s"%s>' \
Edward Sheldrake cf0bb2f
			% (entry_name(entry),
Edward Sheldrake cf0bb2f
			entry_name(entry),
aa3375f
			escape_utf8(icon_attr(entry))))
aa3375f
		list(map(walk_menu, entry.getEntries()))
aa3375f
		print('</menu>')
Edward Sheldrake cf0bb2f
	elif isinstance(entry, xdg.Menu.MenuEntry) and entry.Show is True:
5bb02a8
		name = entry_name(entry.DesktopEntry)
aa3375f
		print('	<item label="%s"%s>' % (name.replace('"', ''),
aa3375f
			escape_utf8(icon_attr(entry.DesktopEntry))))
5bb02a8
		command = re.sub(' -caption "%c"| -caption %c',
5bb02a8
			' -caption "%s"' % name,
5bb02a8
			escape_utf8(entry.DesktopEntry.getExec()))
Edward Sheldrake 544853c
		command = re.sub(' [^ ]*%[fFuUdDnNickvm]', '', command)
Edward Sheldrake cf0bb2f
		if entry.DesktopEntry.getTerminal():
5bb02a8
			command = 'xterm -title "%s" -e %s' % (name, command)
aa3375f
		print('		<action name="Execute">' + \
aa3375f
			'<command>%s</command></action>' % command)
aa3375f
		print('	</item>')
3f61e81
3f61e81
if len(sys.argv) > 1:
Edward Sheldrake cf0bb2f
	menufile = sys.argv[1] + '.menu'
3f61e81
else:
Edward Sheldrake cf0bb2f
	menufile = 'applications.menu'
Edward Sheldrake cf0bb2f
Edward Sheldrake cf0bb2f
lang = os.environ.get('LANG')
Edward Sheldrake cf0bb2f
if lang:
Edward Sheldrake cf0bb2f
	xdg.Config.setLocale(lang)
Edward Sheldrake cf0bb2f
Edward Sheldrake cf0bb2f
# lie to get the same menu as in GNOME
Edward Sheldrake cf0bb2f
xdg.Config.setWindowManager('GNOME')
Edward Sheldrake cf0bb2f
Edward Sheldrake cf0bb2f
if icons:
Edward Sheldrake cf0bb2f
  theme = Gtk.IconTheme.get_default()
Edward Sheldrake cf0bb2f
Edward Sheldrake cf0bb2f
menu = xdg.Menu.parse(menufile)
3f61e81
aa3375f
print('')
aa3375f
print('<openbox_pipe_menu>')
aa3375f
list(map(walk_menu, menu.getEntries()))
aa3375f
print('</openbox_pipe_menu>')