3 import sets
# Note: for Python 2.3; frozenset is only in Python 2.4
5 from zeroinstall
.injector
.model
import SafeException
6 from zeroinstall
.injector
import download
, writer
7 from zeroinstall
.support
import tasks
8 from zeroinstall
import support
10 from dialog
import Dialog
13 warnings
.filterwarnings('ignore', category
= DeprecationWarning, module
='download_box')
15 def download_with_gui(mainwindow
):
16 """If all downloads are ready, prints the selected versions and exits. Otherwise,
17 hides mainwindow, shows the download progress box and then prints them.
18 On error, mainwindow is re-shown and returns False. Before starting,
19 any current downloads (interfaces) are aborted.
20 On success, calls sys.exit(0)."""
22 policy
.abort_all_downloads()
24 # Existing downloads don't disappear until the kill signal takes
25 # effect. Rather than waiting, just filter them out later.
26 existing_downloads
= sets
.ImmutableSet(policy
.handler
.monitored_downloads
)
28 task
= tasks
.Task(policy
.download_impls(), "download implementations")
31 policy
.abort_all_downloads()
33 from zeroinstall
.injector
import selections
34 sels
= selections
.Selections(policy
)
36 reply
= doc
.toxml('utf-8')
37 sys
.stdout
.write(('Length:%8x\n' % len(reply
)) + reply
)
38 mainwindow
.window
.destroy()
40 if sets
.ImmutableSet(policy
.handler
.monitored_downloads
) - existing_downloads
:
41 DownloadProgessBox(run_it
, mainwindow
).show()
44 except SafeException
, ex
:
45 box
= gtk
.MessageDialog(None, gtk
.DIALOG_MODAL
,
46 gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_OK
,
50 mainwindow
.window
.show()
52 class DownloadProgessBox(Dialog
):
58 def __init__(self
, run_it
, mainwindow
):
60 self
.set_title('Downloading, please wait...')
61 self
.mainwindow
= mainwindow
62 assert self
.mainwindow
.download_box
is None
63 self
.mainwindow
.download_box
= self
66 self
.add_button(gtk
.STOCK_CANCEL
, gtk
.RESPONSE_CANCEL
)
68 downloads
= [(x
, x
.expected_size
) for x
in policy
.handler
.monitored_downloads
69 if hasattr(x
, 'expected_size')]
71 table
= gtk
.Table(len(downloads
) + 1, 3)
72 self
.vbox
.pack_start(table
, False, False, 0)
73 table
.set_row_spacings(4)
74 table
.set_col_spacings(10)
75 table
.set_border_width(10)
80 for dl
, size
in downloads
:
81 bar
= gtk
.ProgressBar()
82 bars
.append((dl
, bar
))
83 table
.attach(gtk
.Label(os
.path
.basename(dl
.url
)),
85 table
.attach(gtk
.Label(support
.pretty_size(size
)),
87 table
.attach(bar
, 2, 3, row
, row
+ 1)
90 mainwindow
.window
.hide()
94 for dl
, size
in downloads
:
96 gtk
.timeout_remove(self
.idle_timeout
)
97 self
.idle_timeout
= None
99 mainwindow
.download_box
= None
100 mainwindow
.window
.show()
102 self
.connect('response', resp
)
105 if not policy
.handler
.monitored_downloads
:
106 if policy
.get_uncached_implementations():
111 perc
= dl
.get_current_fraction()
113 bar
.set_fraction(perc
)
116 self
.idle_timeout
= gtk
.timeout_add(250, update_bars
)