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 _
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
21 def N_(message
): return message
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)
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')
61 uri
.set_text(interface_uri
)
63 uri
.connect('changed', set_uri_ok
)
67 category
.append_text(_(c
))
68 category
.set_active(11)
70 def uri_dropped(eb
, drag_context
, x
, y
, selection_data
, info
, timestamp
):
73 data
= codecs
.getdecoder('utf16')(selection_data
.data
)[0]
74 data
= data
.split('\n', 1)[0].strip()
76 data
= selection_data
.data
.split('\n', 1)[0].strip()
77 if self
._sanity
_check
(data
):
79 drag_context
.finish(True, False, timestamp
)
80 self
.window
.response(_RESPONSE_NEXT
)
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
)],
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
)
97 icon_widget
.set_from_pixbuf(icon_pixbuf
)
100 for meta
in iface
.get_metadata(XMLNS_IFACE
, 'category'):
101 feed_category
= meta
.content
105 for row
in categories
:
106 if row
.lower() == feed_category
.lower():
107 category
.set_active(i
)
110 self
.window
.set_response_sensitive(_RESPONSE_PREV
, True)
113 from . import xdgutils
114 iface
= iface_cache
.get_interface(model
.canonical_iface_uri(uri
.get_text()))
117 icon_path
= iface_cache
.get_icon_path(iface
)
118 xdgutils
.add_to_menu(iface
, icon_path
, categories
[category
.get_active()])
119 except SafeException
, ex
:
120 box
= gtk
.MessageDialog(self
.window
, gtk
.DIALOG_MODAL
, gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_OK
, str(ex
))
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
):
131 self
.window
.set_sensitive(False)
132 self
.set_keep_above(False)
134 child
= subprocess
.Popen(['0launch',
135 '--gui', '--download-only',
137 stdout
= subprocess
.PIPE
,
138 stderr
= subprocess
.STDOUT
)
140 def output_ready(src
, cond
):
141 got
= os
.read(src
.fileno(), 100)
145 status
= child
.wait()
146 self
.window
.set_sensitive(True)
147 self
.set_keep_above(True)
149 update_details_page()
151 dialog_next
.set_property('visible', False)
152 dialog_ok
.set_property('visible', True)
153 dialog_ok
.grab_focus()
155 box
= gtk
.MessageDialog(self
.window
, gtk
.DIALOG_MODAL
, gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_OK
,
156 _('Failed to run 0launch.\n') + errors
[0])
161 gobject
.io_add_watch(child
.stdout
,
162 gobject
.IO_IN | gobject
.IO_HUP
,
164 elif resp
== gtk
.RESPONSE_OK
:
166 elif resp
== _RESPONSE_PREV
:
167 dialog_next
.set_property('visible', True)
168 dialog_ok
.set_property('visible', False)
169 dialog_next
.grab_focus()
171 self
.window
.set_response_sensitive(_RESPONSE_PREV
, False)
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
)