Update year to 2009 in various places
[zeroinstall/zeroinstall-rsl.git] / zeroinstall / gtkui / xdgutils.py
blob8a66d57157f9c17dc4ff549a775757874dce86e0
1 """Adding icons and menu items using the freedesktop.org system.
2 (xdg = X Desktop Group)
3 """
4 # Copyright (C) 2009, Thomas Leonard
5 # See the README file for details, or visit http://0install.net.
7 import shutil, os, tempfile
8 from logging import info, warn
10 from zeroinstall import SafeException
11 from zeroinstall.support import basedir
13 _template = """[Desktop Entry]
14 # This file was generated by zero2desktop.
15 # See the Zero Install project for details: http://0install.net
16 Type=Application
17 Version=1.0
18 Name=%s
19 Comment=%s
20 Exec=0launch -- %s %%f
21 Categories=Application;%s
22 """
24 _icon_template = """Icon=%s
25 """
27 def add_to_menu(iface, icon_path, category):
28 """Write a .desktop file for this application.
29 @param iface: the program being added
30 @param icon_path: the path of the icon, or None
31 @param category: the freedesktop.org menu category"""
32 tmpdir = tempfile.mkdtemp(prefix = 'zero2desktop-')
33 try:
34 desktop_name = os.path.join(tmpdir, 'zeroinstall-%s.desktop' % iface.get_name().lower().replace(' ', ''))
35 desktop = file(desktop_name, 'w')
36 desktop.write(_template % (iface.get_name(), iface.summary, iface.uri, category))
37 if icon_path:
38 desktop.write(_icon_template % icon_path)
39 desktop.close()
40 status = os.spawnlp(os.P_WAIT, 'xdg-desktop-menu', 'xdg-desktop-menu', 'install', desktop_name)
41 finally:
42 shutil.rmtree(tmpdir)
44 if status:
45 raise SafeException('Failed to run xdg-desktop-menu (error code %d)' % status)
47 def discover_existing_apps():
48 """Search through the configured XDG datadirs looking for .desktop files created by L{add_to_menu}.
49 @return: a map from application URIs to .desktop filenames"""
50 already_installed = {}
51 for d in basedir.load_data_paths('applications'):
52 for desktop_file in os.listdir(d):
53 if desktop_file.startswith('zeroinstall-') and desktop_file.endswith('.desktop'):
54 full = os.path.join(d, desktop_file)
55 try:
56 for line in file(full):
57 line = line.strip()
58 if line.startswith('Exec=0launch '):
59 bits = line.split(' -- ', 1)
60 if ' ' in bits[0]:
61 uri = bits[0].split(' ', 1)[1] # 0launch URI -- %u
62 else:
63 uri = bits[1].split(' ', 1)[0].strip() # 0launch -- URI %u
64 already_installed[uri] = full
65 break
66 else:
67 info("Failed to find Exec line in %s", full)
68 except Exception, ex:
69 warn("Failed to load .desktop file %s: %s", full, ex)
70 return already_installed