Added <executable-in-var>
[zeroinstall.git] / zeroinstall / gtkui / addbox.py
blobf2a2420080ba895777654541a2dfa4adabe7f4d6
1 """A GTK dialog which lets the user add a new application to their desktop."""
3 # Copyright (C) 2009, Thomas Leonard
4 # See the README file for details, or visit http://0install.net.
6 from zeroinstall import _
7 import os, sys
8 import gtk, gobject
10 from zeroinstall import SafeException
11 from zeroinstall.injector import model
12 from zeroinstall.injector.namespaces import XMLNS_IFACE
13 from zeroinstall.injector.iface_cache import iface_cache
15 _URI_LIST = 0
16 _UTF_16 = 1
18 _RESPONSE_PREV = 0
19 _RESPONSE_NEXT = 1
21 def N_(message): return message
23 categories = [
24 N_('AudioVideo'),
25 N_('Audio'),
26 N_('Video'),
27 N_('Development'),
28 N_('Education'),
29 N_('Game'),
30 N_('Graphics'),
31 N_('Network'),
32 N_('Office'),
33 N_('Settings'),
34 N_('System'),
35 N_('Utility'),
38 class AddBox:
39 """A dialog box which prompts the user to choose the program to be added."""
40 def __init__(self, interface_uri = None):
41 builderfile = os.path.join(os.path.dirname(__file__), 'desktop.ui')
43 builder = gtk.Builder()
44 builder.set_translation_domain('zero-install')
45 builder.add_from_file(builderfile)
46 self.window = builder.get_object('main')
47 self.set_keep_above(True)
49 def set_uri_ok(uri):
50 text = uri.get_text()
51 self.window.set_response_sensitive(_RESPONSE_NEXT, bool(text))
53 uri = builder.get_object('interface_uri')
54 about = builder.get_object('about')
55 icon_widget = builder.get_object('icon')
56 category = builder.get_object('category')
57 dialog_next = builder.get_object('dialog_next')
58 dialog_ok = builder.get_object('dialog_ok')
60 if interface_uri:
61 uri.set_text(interface_uri)
63 uri.connect('changed', set_uri_ok)
64 set_uri_ok(uri)
66 for c in categories:
67 category.append_text(_(c))
68 category.set_active(11)
70 def uri_dropped(eb, drag_context, x, y, selection_data, info, timestamp):
71 if info == _UTF_16:
72 import codecs
73 data = codecs.getdecoder('utf16')(selection_data.data)[0]
74 data = data.split('\n', 1)[0].strip()
75 else:
76 data = selection_data.data.split('\n', 1)[0].strip()
77 if self._sanity_check(data):
78 uri.set_text(data)
79 drag_context.finish(True, False, timestamp)
80 self.window.response(_RESPONSE_NEXT)
81 return True
82 self.window.drag_dest_set(gtk.DEST_DEFAULT_MOTION | gtk.DEST_DEFAULT_DROP | gtk.DEST_DEFAULT_HIGHLIGHT,
83 [('text/uri-list', 0, _URI_LIST),
84 ('text/x-moz-url', 0, _UTF_16)],
85 gtk.gdk.ACTION_COPY)
86 self.window.connect('drag-data-received', uri_dropped)
88 nb = builder.get_object('notebook1')
90 def update_details_page():
91 iface = iface_cache.get_interface(model.canonical_iface_uri(uri.get_text()))
92 about.set_text('%s - %s' % (iface.get_name(), iface.summary))
93 icon_path = iface_cache.get_icon_path(iface)
94 from zeroinstall.gtkui import icon
95 icon_pixbuf = icon.load_icon(icon_path)
96 if icon_pixbuf:
97 icon_widget.set_from_pixbuf(icon_pixbuf)
99 feed_category = None
100 for meta in iface.get_metadata(XMLNS_IFACE, 'category'):
101 feed_category = meta.content
102 break
103 if feed_category:
104 i = 0
105 for row in categories:
106 if row.lower() == feed_category.lower():
107 category.set_active(i)
108 break
109 i += 1
110 self.window.set_response_sensitive(_RESPONSE_PREV, True)
112 def finish():
113 from . import xdgutils
114 iface = iface_cache.get_interface(model.canonical_iface_uri(uri.get_text()))
116 try:
117 icon_path = iface_cache.get_icon_path(iface)
118 xdgutils.add_to_menu(iface, icon_path, categories[category.get_active()])
119 except SafeException as ex:
120 box = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, str(ex))
121 box.run()
122 box.destroy()
123 else:
124 self.window.destroy()
126 def response(box, resp):
127 if resp == _RESPONSE_NEXT:
128 iface = uri.get_text()
129 if not self._sanity_check(iface):
130 return
131 self.window.set_sensitive(False)
132 self.set_keep_above(False)
133 import subprocess
134 child = subprocess.Popen(['0launch',
135 '--gui', '--download-only',
136 '--', iface],
137 stdout = subprocess.PIPE,
138 stderr = subprocess.STDOUT)
139 errors = ['']
140 def output_ready(src, cond):
141 got = os.read(src.fileno(), 100)
142 if got:
143 errors[0] += got
144 else:
145 status = child.wait()
146 self.window.set_sensitive(True)
147 self.set_keep_above(True)
148 if status == 0:
149 update_details_page()
150 nb.next_page()
151 dialog_next.set_property('visible', False)
152 dialog_ok.set_property('visible', True)
153 dialog_ok.grab_focus()
154 else:
155 box = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
156 _('Failed to run 0launch.\n') + errors[0])
157 box.run()
158 box.destroy()
159 return False
160 return True
161 gobject.io_add_watch(child.stdout,
162 gobject.IO_IN | gobject.IO_HUP,
163 output_ready)
164 elif resp == gtk.RESPONSE_OK:
165 finish()
166 elif resp == _RESPONSE_PREV:
167 dialog_next.set_property('visible', True)
168 dialog_ok.set_property('visible', False)
169 dialog_next.grab_focus()
170 nb.prev_page()
171 self.window.set_response_sensitive(_RESPONSE_PREV, False)
172 else:
173 box.destroy()
174 self.window.connect('response', response)
176 if len(sys.argv) > 1:
177 self.window.response(_RESPONSE_NEXT)
179 def set_keep_above(self, above):
180 if hasattr(self.window, 'set_keep_above'):
181 # This isn't very nice, but GNOME defaults to
182 # click-to-raise and in that mode drag-and-drop
183 # is useless without this...
184 self.window.set_keep_above(above)
186 def _sanity_check(self, uri):
187 if uri.endswith('.tar.bz2') or \
188 uri.endswith('.tar.gz') or \
189 uri.endswith('.exe') or \
190 uri.endswith('.rpm') or \
191 uri.endswith('.deb') or \
192 uri.endswith('.tgz'):
193 box = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
194 _("This URI (%s) looks like an archive, not a Zero Install feed. Make sure you're using the feed link!") % uri)
195 box.run()
196 box.destroy()
197 return False
198 return True