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 _
11 from zeroinstall
import SafeException
12 from zeroinstall
.injector
import model
13 from zeroinstall
.injector
.namespaces
import XMLNS_IFACE
14 from zeroinstall
.injector
.iface_cache
import iface_cache
23 """A dialog box which prompts the user to choose the program to be added."""
24 def __init__(self
, interface_uri
= None):
25 gladefile
= os
.path
.join(os
.path
.dirname(__file__
), 'desktop.glade')
27 widgets
= gtk
.glade
.XML(gladefile
, 'main')
28 self
.window
= widgets
.get_widget('main')
29 self
.set_keep_above(True)
33 self
.window
.set_response_sensitive(_RESPONSE_NEXT
, bool(text
))
35 uri
= widgets
.get_widget('interface_uri')
36 about
= widgets
.get_widget('about')
37 icon_widget
= widgets
.get_widget('icon')
38 category
= widgets
.get_widget('category')
39 dialog_next
= widgets
.get_widget('dialog_next')
40 dialog_ok
= widgets
.get_widget('dialog_ok')
43 uri
.set_text(interface_uri
)
45 uri
.connect('changed', set_uri_ok
)
48 category
.set_active(11)
50 def uri_dropped(eb
, drag_context
, x
, y
, selection_data
, info
, timestamp
):
53 data
= codecs
.getdecoder('utf16')(selection_data
.data
)[0]
54 data
= data
.split('\n', 1)[0].strip()
56 data
= selection_data
.data
.split('\n', 1)[0].strip()
57 if self
._sanity
_check
(data
):
59 drag_context
.finish(True, False, timestamp
)
60 self
.window
.response(_RESPONSE_NEXT
)
62 self
.window
.drag_dest_set(gtk
.DEST_DEFAULT_MOTION | gtk
.DEST_DEFAULT_DROP | gtk
.DEST_DEFAULT_HIGHLIGHT
,
63 [('text/uri-list', 0, _URI_LIST
),
64 ('text/x-moz-url', 0, _UTF_16
)],
66 self
.window
.connect('drag-data-received', uri_dropped
)
68 nb
= widgets
.get_widget('notebook1')
70 def update_details_page():
71 iface
= iface_cache
.get_interface(model
.canonical_iface_uri(uri
.get_text()))
72 about
.set_text('%s - %s' % (iface
.get_name(), iface
.summary
))
73 icon_path
= iface_cache
.get_icon_path(iface
)
74 from zeroinstall
.gtkui
import icon
75 icon_pixbuf
= icon
.load_icon(icon_path
)
77 icon_widget
.set_from_pixbuf(icon_pixbuf
)
80 for meta
in iface
.get_metadata(XMLNS_IFACE
, 'category'):
81 feed_category
= meta
.content
85 for row
in category
.get_model():
86 if row
[0].lower() == feed_category
.lower():
87 category
.set_active(i
)
90 self
.window
.set_response_sensitive(_RESPONSE_PREV
, True)
94 iface
= iface_cache
.get_interface(model
.canonical_iface_uri(uri
.get_text()))
97 icon_path
= iface_cache
.get_icon_path(iface
)
98 xdgutils
.add_to_menu(iface
, icon_path
, category
.get_active_text())
99 except SafeException
, ex
:
100 box
= gtk
.MessageDialog(self
.window
, gtk
.DIALOG_MODAL
, gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_OK
, str(ex
))
104 self
.window
.destroy()
106 def response(box
, resp
):
107 if resp
== _RESPONSE_NEXT
:
108 iface
= uri
.get_text()
109 if not self
._sanity
_check
(iface
):
111 self
.window
.set_sensitive(False)
112 self
.set_keep_above(False)
114 child
= subprocess
.Popen(['0launch',
115 '--gui', '--download-only',
117 stdout
= subprocess
.PIPE
,
118 stderr
= subprocess
.STDOUT
)
120 def output_ready(src
, cond
):
121 got
= os
.read(src
.fileno(), 100)
125 status
= child
.wait()
126 self
.window
.set_sensitive(True)
127 self
.set_keep_above(True)
129 update_details_page()
131 dialog_next
.set_property('visible', False)
132 dialog_ok
.set_property('visible', True)
133 dialog_ok
.grab_focus()
135 box
= gtk
.MessageDialog(self
.window
, gtk
.DIALOG_MODAL
, gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_OK
,
136 _('Failed to run 0launch.\n') + errors
[0])
141 gobject
.io_add_watch(child
.stdout
,
142 gobject
.IO_IN | gobject
.IO_HUP
,
144 elif resp
== gtk
.RESPONSE_OK
:
146 elif resp
== _RESPONSE_PREV
:
147 dialog_next
.set_property('visible', True)
148 dialog_ok
.set_property('visible', False)
149 dialog_next
.grab_focus()
151 self
.window
.set_response_sensitive(_RESPONSE_PREV
, False)
154 self
.window
.connect('response', response
)
156 if len(sys
.argv
) > 1:
157 self
.window
.response(_RESPONSE_NEXT
)
159 def set_keep_above(self
, above
):
160 if hasattr(self
.window
, 'set_keep_above'):
161 # This isn't very nice, but GNOME defaults to
162 # click-to-raise and in that mode drag-and-drop
163 # is useless without this...
164 self
.window
.set_keep_above(above
)
166 def _sanity_check(self
, uri
):
167 if uri
.endswith('.tar.bz2') or \
168 uri
.endswith('.tar.gz') or \
169 uri
.endswith('.exe') or \
170 uri
.endswith('.rpm') or \
171 uri
.endswith('.deb') or \
172 uri
.endswith('.tgz'):
173 box
= gtk
.MessageDialog(self
.window
, gtk
.DIALOG_MODAL
, gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_OK
,
174 _("This URI (%s) looks like an archive, not a Zero Install feed. Make sure you're using the feed link!") % uri
)