1
2
3
4
5
6
7
8
9
10
11 from PyQt4.QtCore import QUrl, QFile, QIODevice
12 from PyQt4.QtGui import QDialog, QProgressBar
13 from PyQt4.QtGui import QLabel, QPushButton, QDialogButtonBox, QVBoxLayout, QMessageBox
14 from PyQt4.QtNetwork import QHttp
15 import os
16
17
19
20 - def __init__(self, parent, url, archivo_destino):
21 super(Descargar, self).__init__(parent)
22
23 self.url = url
24 self.httpGetId = 0
25 self.httpRequestAborted = False
26 self.statusLabel = QLabel('Descargando el manual completo ...')
27 self.closeButton = QPushButton("Cerrar")
28 self.closeButton.setAutoDefault(False)
29 self.barra = QProgressBar()
30
31 buttonBox = QDialogButtonBox()
32 buttonBox.addButton(self.closeButton, QDialogButtonBox.RejectRole)
33
34 self.http = QHttp(self)
35 self.http.requestFinished.connect(self.cuando_finalizar_request)
36 self.http.dataReadProgress.connect(self.cuando_actualiza_descarga)
37 self.http.responseHeaderReceived.connect(self.cuando_responder_header)
38 self.closeButton.clicked.connect(self.cuando_cancela)
39
40 mainLayout = QVBoxLayout()
41 mainLayout.addWidget(self.statusLabel)
42 mainLayout.addWidget(self.barra)
43 mainLayout.addWidget(buttonBox)
44 self.setLayout(mainLayout)
45
46 self.setWindowTitle('Descargando manual')
47 self.downloadFile(url, archivo_destino)
48
50 url = QUrl(url)
51 fileName = archivo_destino
52 directorio_usuario_para_pilas = os.path.dirname(archivo_destino)
53
54 if not os.path.exists(directorio_usuario_para_pilas):
55 os.mkdir(directorio_usuario_para_pilas)
56
57 if QFile.exists(fileName):
58 QFile.remove(fileName)
59
60 self.outFile = QFile(fileName)
61
62 if not self.outFile.open(QIODevice.WriteOnly):
63 QMessageBox.information(self, 'Error', 'Lo siento, no se puede descargar el archivo desde %s: %s.' % (self.url, self.outFile.errorString()))
64 self.outFile = None
65 return
66
67 mode = QHttp.ConnectionModeHttp
68 port = url.port()
69 if port == -1:
70 port = 0
71 self.http.setHost(url.host(), mode, port)
72 self.httpRequestAborted = False
73
74 path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/")
75 if path:
76 path = str(path)
77 else:
78 path = '/'
79
80 self.httpGetId = self.http.get(path, self.outFile)
81
83 self.statusLabel.setText("Descarga cancelada")
84 self.httpRequestAborted = True
85 self.http.abort()
86 self.close()
87
89 if request_id != self.httpGetId:
90 return
91
92 if self.httpRequestAborted:
93 if self.outFile is not None:
94 self.outFile.close()
95 self.outFile.remove()
96 self.outFile = None
97 return
98
99 self.outFile.close()
100
101 if error:
102 self.outFile.remove()
103 QMessageBox.information(self, 'Error', u'Hay un error de conexión: %s.' % self.http.errorString())
104
105 self.statusLabel.setText(u'Perfecto, ahora podrás explorar el manual.')
106
108 if responseHeader.statusCode() not in (200, 300, 301, 302, 303, 307):
109 mensaje = 'Ha fallado la descarga del archivo: \n"%s" \n%s.' %(self.url, responseHeader.reasonPhrase())
110 QMessageBox.information(self, 'Error', mensaje)
111
112 self.httpRequestAborted = True
113 self.http.abort()
114 self.close()
115
117 if self.httpRequestAborted:
118 return
119
120 self.barra.setMaximum(total_bytes)
121 self.barra.setValue(bytes_leidos)
122