add timeout and better error msg to getshared

This commit is contained in:
Damien Elmes 2009-10-01 19:39:19 +09:00
parent cbb7f236cd
commit f4f3c94ec0

View file

@ -5,7 +5,7 @@
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import ankiqt, simplejson, time, cStringIO, zipfile, tempfile, os, re
import traceback, urllib2
import traceback, urllib2, socket, cgi
from ankiqt.ui.utils import saveGeom, restoreGeom, showInfo
from anki.utils import fmtTimeSpan
@ -29,6 +29,11 @@ class GetShared(QDialog):
self.form = ankiqt.forms.getshared.Ui_Dialog()
self.form.setupUi(self)
self.ok = True
self.conErrMsg = _("""\
<b>Unable to connect to the server.<br><br>
Please check your network connection or try again in a few minutes.</b><br>
<br>
Error was:<pre>%s</pre>""")
restoreGeom(self, "getshared")
self.setupTable()
self.onChangeType(type)
@ -49,16 +54,24 @@ class GetShared(QDialog):
self.limit)
def fetchData(self):
self.parent.setProgressParent(None)
self.parent.deck.startProgress()
self.parent.deck.updateProgress()
try:
socket.setdefaulttimeout(30)
try:
sock = urllib2.urlopen(
"http://anki.ichi2.net/file/search?t=%d" % self.type)
self.allList = simplejson.loads(unicode(sock.read()))
except:
showInfo(_("Unable to connect to server.\n\n") +
traceback.format_exc())
showInfo(self.conErrMsg % cgi.escape(unicode(
traceback.format_exc(), "utf-8", "replace")))
self.close()
self.ok = False
return
finally:
self.parent.deck.finishProgress()
socket.setdefaulttimeout(None)
self.form.search.setFocus()
self.typeChanged()
self.limit()
@ -156,8 +169,10 @@ class GetShared(QDialog):
tmpfile = os.fdopen(fd, "w+b")
cnt = 0
try:
socket.setdefaulttimeout(30)
self.parent.setProgressParent(self)
self.parent.startProgress()
self.parent.updateProgress()
try:
sock = urllib2.urlopen(
"http://anki.ichi2.net/file/get?id=%d" %
@ -171,11 +186,12 @@ class GetShared(QDialog):
self.parent.updateProgress(
label=_("Downloaded %dKB") % (cnt/1024.0))
except:
showInfo(_("Unable to connect to server.\n\n") +
unicode(traceback.format_exc(), "utf-8", "replace"))
showInfo(self.conErrMsg % cgi.escape(unicode(
traceback.format_exc(), "utf-8", "replace")))
self.close()
return
finally:
socket.setdefaulttimeout(None)
self.parent.setProgressParent(None)
self.parent.finishProgress()
QDialog.accept(self)