On Tue, May 20, 2008 at 11:41:05AM +0100, Chris G wrote:

> If it's of any interest I've been looking for alternatives and there
> is actually a Python script out there for extracting menus from
> 'standard' XDG information into FVWM menus, take a look at:-
> 
>     http://www.cl.cam.ac.uk/~pz215/automenus.html
> 
> The Python script itself is:-
> 
>     http://www.cl.cam.ac.uk/~pz215/fvwm-scripts/scripts/fvwm-xdg-menu.py
> 
> Just running it with the little wrapper that Piotr Zeilinski provides
> produces a pretty good shot at a set of menus for FVWM.  Since I much
> prefer Python to Perl anyway I may spend a little more time with this.

That's NEAT! It worked almost perfectly for me out of the box. I tweaked
it a little to accept SVG files, and some other minor fixes. I've
attached them.

Now doing

    fvwm-xdg-menu.py -f -s22 -i ~/.fvwm/icons/xdg -m "KDE menu" \
    /usr/kde/3.5/etc/xdg/menus/applications.menu > xdg_menus_22x22.fvwm

Generates a perfect menu!! Thanks for the tip,

GI

-- 
'Inflation' -- Having to pay next years prices on last year's salary.
#!/usr/bin/python

# Author: Piotr Zielinski (http://www.cl.cam.ac.uk/~pz215/)
# Licence: GPL 2
# Date: 03.12.2005

# This script takes names of menu files conforming to the XDG Desktop
# Menu Specification, and outputs their FVWM equivalents to the
# standard output.
#
#   http://standards.freedesktop.org/menu-spec/latest/

# Syntax:
#
#   fvwm-xdg-menu.py menufile1 menufile2 menufile3 ...
#
# Each menufile is an XDG menu description file.  Example:
#
#   fvwm-xdg-menu.py /etc/xdg/menus/gnome-applications.menu 

# This script requires the python-xdg module, which in Debian can be
# installed by typing
#
#   apt-get install python-xdg

import sys
import xdg.Menu
import xdg.IconTheme
import xdg.Locale
import optparse
import os.path
import os
from xdg.DesktopEntry import *

usage = """

   %prog [options] file1 file2 ...

This script takes names of menu files conforming to the XDG Desktop
Menu Specification, and outputs their FVWM equivalents to the standard
output.

   http://standards.freedesktop.org/menu-spec/latest/

examples:

   %prog /etc/xdg/menus/gnome-applications.menu
   %prog /etc/xdg/menus/kde-applications.menu
   %prog /etc/xdg/menus/debian-menu.menu"""

parser = optparse.OptionParser(usage=usage)
parser.add_option("-e", "--exec", dest="exec_command", type="string",
                  default="Exec exec",
                  help="FVWM command used to execute programs [Exec exec]")
parser.add_option("-s", "--size", dest="icon_size", type="int",
                  default=24, help="Default icon size [24]")
parser.add_option("-f", "--force", action="store_true", dest="force",
                  default=False,
                  help="Force icon size (requires imagemagick and writes \
                  into ICON_DIR)")
parser.add_option("-i", "--icon-dir", dest="icon_dir", type="string",
                  default="~/.fvwm/icons",
                  help="Directory for converted icons [~/.fvwm/icons]")
parser.add_option("-t", "--theme", dest="theme", type="string",
                  default="gnome",
                  help="Icon theme [gnome]")
parser.add_option("-m", "--top-menu", dest="top", type="string",
                  default="",
                  help="Top menu name")


options, args = parser.parse_args()

def printtext(text):
    print text.encode("utf-8")

def geticonfile(icon, size=options.icon_size, theme=options.theme):
    iconpath = xdg.IconTheme.getIconPath(icon, size, theme, ['png', 'xpm'])

    if not iconpath:
        return None

    if not options.force:
        return iconpath
    
    if iconpath.find("%ix%i" % (size, size)) >= 0: # ugly hack!!!
        return iconpath

    # Try svg
    scalable = xdg.IconTheme.getIconPath(icon, None, theme, ['svg'] )
    if scalable.endswith( ".svg" ):
	return "%s:0x%i" % (iconpath, size)

    # Try and rescale PNG/XPM
    #printtext(iconpath)
    iconfile = os.path.join(os.path.expanduser(options.icon_dir),
                            "%ix%i-" % (size, size) + 
                            os.path.basename(iconpath))
    os.system("if test \\( ! -f '%s' \\) -o \\( '%s' -nt '%s' \\) ; then convert '%s' -resize %i '%s' ; fi"% 
              (iconfile, iconpath, iconfile, iconpath, size, iconfile))
    return iconfile

    
def getdefaulticonfile(command):
    if command.startswith("Popup"):
        return geticonfile("gnome-fs-directory")
    else:
        return geticonfile("gnome-applications")    

def printmenu(name, icon, command):
    iconfile = geticonfile(icon) or getdefaulticonfile(command) or icon
    printtext('+ "%s%%%s%%" %s' % (name, iconfile, command))

def parsemenu(menu, name=""):
    if not name:
	name = menu.getPath()
    printtext( 'DestroyMenu "%s"' % name )
    printtext('AddToMenu "%s"' % name)
    for entry in menu.getEntries():
	if isinstance(entry, xdg.Menu.Menu):
	    printmenu(entry.getName(), entry.getIcon(),
		      'Popup "%s"' % entry.getPath())
	elif isinstance(entry, xdg.Menu.MenuEntry):
	    desktop = DesktopEntry(entry.DesktopEntry.getFileName())
	    printmenu(desktop.getName(), desktop.getIcon(),
		      options.exec_command + " " + desktop.getExec())
	else:
	    printtext('# not supported: ' + str(entry))

    print
    for entry in menu.getEntries():
	if isinstance(entry, xdg.Menu.Menu):
	    parsemenu(entry)


for arg in args:
    print '# %s' % arg
    parsemenu(xdg.Menu.parse(arg), options.top)
        

Attachment: pgpO4wWfrBl36.pgp
Description: PGP signature

Reply via email to