Added 0desktop command for desktop integration.
[zeroinstall/zeroinstall-mseaborn.git] / zeroinstall / gtkui / xdgutils.py
blob93b83929fe02445bf04916f1b6a4407e7c70d177
1 """Adding icons and menu items using the freedesktop.org system.
2 (xdg = X Desktop Group)
3 """
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
14 Type=Application
15 Version=1.0
16 Name=%s
17 Comment=%s
18 Exec=0launch -- %s %%f
19 Categories=Application;%s
20 """
22 _icon_template = """Icon=%s
23 """
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-')
31 try:
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))
35 if icon_path:
36 desktop.write(_icon_template % icon_path)
37 desktop.close()
38 status = os.spawnlp(os.P_WAIT, 'xdg-desktop-menu', 'xdg-desktop-menu', 'install', desktop_name)
39 finally:
40 shutil.rmtree(tmpdir)
42 if status:
43 raise SafeException('Failed to run xdg-desktop-menu (error code %d)' % status)