4 from model
import SafeException
6 from dialog
import Dialog
10 def pretty_size(size
):
11 return str(size
) + ' b'
13 def download_and_run(mainwindow
, prog_args
):
14 """If all downloads are ready, runs the program. Otherwise,
15 hides mainwindow, shows the download progress box and then runs
16 it. On error, mainwindow is re-shown."""
18 for iface
, impl
in policy
.get_uncached_implementations():
19 if not impl
.download_sources
:
20 raise SafeException("Implementation " + impl
+ " of "
21 "interface " + iface
+ " cannot be "
22 "downloaded (no download locations given in "
24 dl
= download
.begin_impl_download(impl
.download_sources
[0],
26 downloads
.append((iface
, dl
))
30 run
.execute(policy
, prog_args
)
32 except SafeException
, ex
:
33 box
= gtk
.MessageDialog(self
, gtk
.DIALOG_MODAL
,
34 gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_OK
,
40 DownloadProgessBox(run_it
, downloads
, mainwindow
).show()
44 class DownloadProgessBox(Dialog
):
50 def __init__(self
, run_it
, downloads
, mainwindow
):
52 self
.set_title('Downloading, please wait...')
53 self
.mainwindow
= mainwindow
57 self
.add_button(gtk
.STOCK_CANCEL
, gtk
.RESPONSE_CANCEL
)
59 table
= gtk
.Table(len(downloads
) + 1, 3)
60 self
.vbox
.pack_start(table
, False, False, 0)
61 table
.set_row_spacings(4)
62 table
.set_col_spacings(10)
63 table
.set_border_width(10)
68 for iface
, dl
in downloads
:
69 bar
= gtk
.ProgressBar()
70 bars
.append((dl
, bar
))
71 table
.attach(gtk
.Label(iface
.get_name()),
73 table
.attach(gtk
.Label(pretty_size(dl
.source
.size
)),
75 table
.attach(bar
, 2, 3, row
, row
+ 1)
77 self
.start_download(dl
)
83 for iface
, dl
in downloads
:
85 gtk
.timeout_remove(self
.idle_timeout
)
86 self
.idle_timeout
= None
89 self
.connect('response', resp
)
92 if self
.n_downloads
== 0:
96 perc
= dl
.get_current_fraction()
97 bar
.set_fraction(perc
)
100 self
.idle_timeout
= gtk
.timeout_add(250, update_bars
)
102 def start_download(self
, dl
):
103 error_stream
= dl
.start()
104 def error_ready(src
, cond
):
105 got
= os
.read(src
.fileno(), 100)
108 self
.n_downloads
-= 1
110 data
= dl
.error_stream_closed()
111 except download
.DownloadError
, ex
:
113 "Error downloading '%s':\n\n%s" %
116 policy
.add_to_cache(dl
.source
, data
)
117 except SafeException
, ex
:
119 "Error unpacking '%s':\n\n%s" %
122 dl
.error_stream_data(got
)
125 self
.n_downloads
+= 1
126 gtk
.input_add(error_stream
, gtk
.gdk
.INPUT_READ
, error_ready
)