1 """A GTK dialog which lets the user add a new application to their desktop."""
2 # Copyright (C) 2008, Thomas Leonard
3 # See the README file for details, or visit http://0install.net.
9 from zeroinstall
import SafeException
10 from zeroinstall
.injector
import model
11 from zeroinstall
.injector
.namespaces
import XMLNS_IFACE
12 from zeroinstall
.injector
.iface_cache
import iface_cache
21 """A dialog box which prompts the user to choose the program to be added."""
22 def __init__(self
, interface_uri
= None):
23 gladefile
= os
.path
.join(os
.path
.dirname(__file__
), 'desktop.glade')
25 widgets
= gtk
.glade
.XML(gladefile
, 'main')
26 self
.window
= widgets
.get_widget('main')
27 self
.set_keep_above(True)
31 self
.window
.set_response_sensitive(_RESPONSE_NEXT
, bool(text
))
33 uri
= widgets
.get_widget('interface_uri')
34 about
= widgets
.get_widget('about')
35 icon_widget
= widgets
.get_widget('icon')
36 category
= widgets
.get_widget('category')
37 dialog_next
= widgets
.get_widget('dialog_next')
38 dialog_ok
= widgets
.get_widget('dialog_ok')
41 uri
.set_text(interface_uri
)
43 uri
.connect('changed', set_uri_ok
)
46 category
.set_active(11)
48 def uri_dropped(eb
, drag_context
, x
, y
, selection_data
, info
, timestamp
):
51 data
= codecs
.getdecoder('utf16')(selection_data
.data
)[0]
52 data
= data
.split('\n', 1)[0].strip()
54 data
= selection_data
.data
.split('\n', 1)[0].strip()
56 drag_context
.finish(True, False, timestamp
)
57 self
.window
.response(_RESPONSE_NEXT
)
59 self
.window
.drag_dest_set(gtk
.DEST_DEFAULT_MOTION | gtk
.DEST_DEFAULT_DROP | gtk
.DEST_DEFAULT_HIGHLIGHT
,
60 [('text/uri-list', 0, _URI_LIST
),
61 ('text/x-moz-url', 0, _UTF_16
)],
63 self
.window
.connect('drag-data-received', uri_dropped
)
65 nb
= widgets
.get_widget('notebook1')
67 def update_details_page():
68 iface
= iface_cache
.get_interface(model
.canonical_iface_uri(uri
.get_text()))
69 about
.set_text('%s - %s' % (iface
.get_name(), iface
.summary
))
70 icon_path
= iface_cache
.get_icon_path(iface
)
71 from zeroinstall
.gtkui
import icon
72 icon_pixbuf
= icon
.load_icon(icon_path
)
74 icon_widget
.set_from_pixbuf(icon_pixbuf
)
77 for meta
in iface
.get_metadata(XMLNS_IFACE
, 'category'):
78 feed_category
= meta
.content
82 for row
in category
.get_model():
83 if row
[0].lower() == feed_category
.lower():
84 category
.set_active(i
)
87 self
.window
.set_response_sensitive(_RESPONSE_PREV
, True)
91 iface
= iface_cache
.get_interface(model
.canonical_iface_uri(uri
.get_text()))
94 icon_path
= iface_cache
.get_icon_path(iface
)
95 xdgutils
.add_to_menu(iface
, icon_path
, category
.get_active_text())
96 except SafeException
, ex
:
97 box
= gtk
.MessageDialog(self
.window
, gtk
.DIALOG_MODAL
, gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_OK
, str(ex
))
101 self
.window
.destroy()
103 def response(box
, resp
):
104 if resp
== _RESPONSE_NEXT
:
105 iface
= uri
.get_text()
106 self
.window
.set_sensitive(False)
107 self
.set_keep_above(False)
109 child
= popen2
.Popen4(['0launch',
110 '--gui', '--download-only',
112 child
.tochild
.close()
114 def output_ready(src
, cond
):
115 got
= os
.read(src
.fileno(), 100)
119 status
= child
.wait()
120 self
.window
.set_sensitive(True)
121 self
.set_keep_above(True)
123 update_details_page()
125 dialog_next
.set_property('visible', False)
126 dialog_ok
.set_property('visible', True)
127 dialog_ok
.grab_focus()
129 box
= gtk
.MessageDialog(self
.window
, gtk
.DIALOG_MODAL
, gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_OK
,
130 'Failed to run 0launch.\n' + errors
[0])
135 gobject
.io_add_watch(child
.fromchild
,
136 gobject
.IO_IN | gobject
.IO_HUP
,
138 elif resp
== gtk
.RESPONSE_OK
:
140 elif resp
== _RESPONSE_PREV
:
141 dialog_next
.set_property('visible', True)
142 dialog_ok
.set_property('visible', False)
143 dialog_next
.grab_focus()
145 self
.window
.set_response_sensitive(_RESPONSE_PREV
, False)
148 self
.window
.connect('response', response
)
150 if len(sys
.argv
) > 1:
151 self
.window
.response(_RESPONSE_NEXT
)
153 def set_keep_above(self
, above
):
154 if hasattr(self
.window
, 'set_keep_above'):
155 # This isn't very nice, but GNOME defaults to
156 # click-to-raise and in that mode drag-and-drop
157 # is useless without this...
158 self
.window
.set_keep_above(above
)