1 """Adding icons and menu items using the freedesktop.org system.
2 (xdg = X Desktop Group)
4 # Copyright (C) 2008, Thomas Leonard
5 # See the README file for details, or visit http://0install.net.
7 import shutil
, os
, tempfile
9 from zeroinstall
import SafeException
11 _template
= """[Desktop Entry]
12 # This file was generated by zero2desktop.
13 # See the Zero Install project for details: http://0install.net
18 Exec=0launch -- %s %%f
19 Categories=Application;%s
22 _icon_template
= """Icon=%s
25 def add_to_menu(iface
, icon_path
, category
):
26 """Write a .desktop file for this application.
27 @param iface: the program being added
28 @param icon_path: the path of the icon, or None
29 @param category: the freedesktop.org menu category"""
30 tmpdir
= tempfile
.mkdtemp(prefix
= 'zero2desktop-')
32 desktop_name
= os
.path
.join(tmpdir
, 'zeroinstall-%s.desktop' % iface
.get_name().lower())
33 desktop
= file(desktop_name
, 'w')
34 desktop
.write(_template
% (iface
.get_name(), iface
.summary
, iface
.uri
, category
))
36 desktop
.write(_icon_template
% icon_path
)
38 status
= os
.spawnlp(os
.P_WAIT
, 'xdg-desktop-menu', 'xdg-desktop-menu', 'install', desktop_name
)
43 raise SafeException('Failed to run xdg-desktop-menu (error code %d)' % status
)