1 # -*- coding: utf-8 -*-
3 from __future__
import absolute_import
5 import sys
, os
, Queue
, urllib2
7 from ..widget
.storedsize
import StoredSizeDialog
8 from jben
.download_thread
import DownloadThread
11 class DictDownload(StoredSizeDialog
):
13 """Downloads dictionaries from a specified mirror."""
15 def __init__(self
, app
, parent
, mirror
, files
):
16 StoredSizeDialog
.__init
__(
17 self
, "gui.dialog.dict_download.size", -1, -1,
18 title
=_("Download dictionaries"),
20 flags
=gtk
.DIALOG_MODAL
24 self
.connect("show", self
.on_show
)
25 self
.connect("destroy", self
.on_destroy
)
26 self
.urls
= ["/".join((mirror
, f
)) for f
in files
]
27 self
.handled_urls
= []
29 self
.timeout_src
= None
31 def on_show(self
, widget
):
34 def on_destroy(self
, widget
):
35 print "on_destroy called"
36 self
.set_sensitive(False)
38 if not gobject
.source_remove(self
.timeout_src
):
39 print >> sys
.stderr
, "WARNING: Could not remove timeout"
45 """Single-time run command; hides GTK boilerplate."""
50 self
.ok_btn
= self
.add_button(gtk
.STOCK_OK
, gtk
.RESPONSE_OK
)
51 self
.ok_btn
.set_sensitive(False)
53 def _add_progress_bar(self
):
54 layout
= self
.get_content_area()
55 self
.cur_prog_bar
= gtk
.ProgressBar()
56 self
.cur_prog_bar
.set_pulse_step(0.01)
57 layout
.pack_start(self
.cur_prog_bar
, expand
=False)
60 def _do_new_thread(self
):
63 print "Starting new thread for download:", url
64 fname
= url
.rsplit('/', 1)[-1]
65 out_fname
= os
.path
.join(self
.app
.dictmgr
.get_dict_dir(), fname
)
66 self
.dt
= DownloadThread(url
, out_fname
, timeout
=5)
68 self
._add
_progress
_bar
()
69 self
.timeout_src
= gobject
.timeout_add(10, self
.on_thread_poll
,
72 print "URL queue empty, finishing up"
75 def on_thread_poll(self
, dt
):
78 (message
, status
) = dt
.out_queue
.get(block
=False)
79 if message
== dt
.CONNECTING
:
80 self
.cur_prog_bar
.set_text("Connecting...")
81 elif message
== dt
.CONNECTED
:
82 self
.cur_prog_bar
.set_text(dt
.realurl
)
83 elif message
== dt
.DOWNLOADING
:
85 frct
= status
/ float(dt
.filesize
)
86 pct
= (100 * frct
) if status
else 0
87 out_str
= "%s: %d bytes (%f)%%" % (
88 dt
.realurl
, status
, pct
)
89 self
.cur_prog_bar
.set_text(out_str
)
90 self
.cur_prog_bar
.set_fraction(max(frct
, 1.0))
92 out_str
= "%s: %d bytes" % (dt
.realurl
, status
)
93 self
.cur_prog_bar
.set_text(out_str
)
94 self
.cur_prog_bar
.pulse()
95 elif message
== dt
.DONE
:
96 self
.cur_prog_bar
.set_text("Download complete")
97 self
.cur_prog_bar
.set_fraction(1.0)
100 elif message
== dt
.ERROR
:
101 if type(status
) is urllib2
.URLError
:
102 err_str
= "Error opening %s: %s" % \
103 (dt
.realurl
or dt
.url
, status
.reason
)
105 err_str
= "Error occurred during download: %s" % \
107 self
.cur_prog_bar
.set_text(err_str
)
108 self
._do
_new
_thread
()
111 raise Exception("Unknown thread message: (%s, %s)" %
116 def _thread_finished(self
, thread
):
117 self
.handled_urls
.append(thread
.realurl
)
118 self
._do
_new
_thread
()
121 self
.ok_btn
.set_sensitive(True)