Helpviewer/Download/DownloadItem.py

changeset 670
bb833c4bcf28
parent 668
b0061a6f7484
child 673
1c1f3a125e68
equal deleted inserted replaced
669:d405dc3e2ed6 670:bb833c4bcf28
8 """ 8 """
9 9
10 from PyQt4.QtCore import pyqtSlot, pyqtSignal, Qt, QTime, QFile, QFileInfo, QUrl, \ 10 from PyQt4.QtCore import pyqtSlot, pyqtSignal, Qt, QTime, QFile, QFileInfo, QUrl, \
11 QIODevice 11 QIODevice
12 from PyQt4.QtGui import QWidget, QPalette, QStyle, QDesktopServices, QFileDialog 12 from PyQt4.QtGui import QWidget, QPalette, QStyle, QDesktopServices, QFileDialog
13 from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply 13 from PyQt4.QtNetwork import QNetworkRequest, QNetworkReply
14 14
15 from E5Gui import E5MessageBox 15 from E5Gui import E5MessageBox
16 16
17 from .Ui_DownloadItem import Ui_DownloadItem 17 from .Ui_DownloadItem import Ui_DownloadItem
18 18
67 self.__page = webPage 67 self.__page = webPage
68 self.__toDownload = download 68 self.__toDownload = download
69 self.__bytesReceived = 0 69 self.__bytesReceived = 0
70 self.__downloadTime = QTime() 70 self.__downloadTime = QTime()
71 self.__output = QFile() 71 self.__output = QFile()
72 self.__fileName = ""
72 self.__startedSaving = False 73 self.__startedSaving = False
73 self.__finishedDownloading = False 74 self.__finishedDownloading = False
74 self.__gettingFileName = False 75 self.__gettingFileName = False
75 self.__canceledFileSelect = False 76 self.__canceledFileSelect = False
76 self.__autoOpen = False 77 self.__autoOpen = False
92 self.__startedSaving = False 93 self.__startedSaving = False
93 self.__finishedDownloading = False 94 self.__finishedDownloading = False
94 95
95 self.openButton.setEnabled(False) 96 self.openButton.setEnabled(False)
96 self.openButton.setVisible(False) 97 self.openButton.setVisible(False)
98
99 # start timer for the download estimation
100 self.__downloadTime.start()
97 101
98 # attach to the reply object 102 # attach to the reply object
99 self.__url = self.__reply.url() 103 self.__url = self.__reply.url()
100 self.__reply.setParent(self) 104 self.__reply.setParent(self)
101 self.__reply.readyRead[()].connect(self.__readyRead) 105 self.__reply.readyRead[()].connect(self.__readyRead)
107 # reset info 111 # reset info
108 self.infoLabel.clear() 112 self.infoLabel.clear()
109 self.progressBar.setValue(0) 113 self.progressBar.setValue(0)
110 self.__getFileName() 114 self.__getFileName()
111 115
112 # start timer for the download estimation
113 self.__downloadTime.start()
114
115 if self.__reply.error() != QNetworkReply.NoError: 116 if self.__reply.error() != QNetworkReply.NoError:
116 self.__networkError() 117 self.__networkError()
117 self.__finished() 118 self.__finished()
118 119
119 def __getFileName(self): 120 def __getFileName(self):
126 return 127 return
127 128
128 downloadDirectory = Helpviewer.HelpWindow.HelpWindow\ 129 downloadDirectory = Helpviewer.HelpWindow.HelpWindow\
129 .downloadManager().downloadDirectory() 130 .downloadManager().downloadDirectory()
130 131
131 defaultFileName = self.__saveFileName(downloadDirectory) 132 if self.__fileName:
132 fileName = defaultFileName 133 fileName = self.__fileName
134 self.__toDownload = True
135 ask = False
136 else:
137 defaultFileName = self.__saveFileName(downloadDirectory)
138 fileName = defaultFileName
139 ask = True
133 self.__autoOpen = False 140 self.__autoOpen = False
134 if not self.__toDownload: 141 if not self.__toDownload:
135 res = E5MessageBox.question(self, 142 res = E5MessageBox.question(self,
136 self.trUtf8("Downloading"), 143 self.trUtf8("Downloading"),
137 self.trUtf8("""<p>You are about to download the file <b>{0}</b>.</p>""" 144 self.trUtf8("""<p>You are about to download the file <b>{0}</b>.</p>"""
151 158
152 self.__autoOpen = res == E5MessageBox.Open 159 self.__autoOpen = res == E5MessageBox.Open
153 fileName = QDesktopServices.storageLocation(QDesktopServices.TempLocation) + \ 160 fileName = QDesktopServices.storageLocation(QDesktopServices.TempLocation) + \
154 '/' + QFileInfo(fileName).completeBaseName() 161 '/' + QFileInfo(fileName).completeBaseName()
155 162
156 if not self.__autoOpen and self.__requestFilename: 163 if ask and not self.__autoOpen and self.__requestFilename:
157 self.__gettingFileName = True 164 self.__gettingFileName = True
158 fileName = QFileDialog.getSaveFileName( 165 fileName = QFileDialog.getSaveFileName(
159 None, 166 None,
160 self.trUtf8("Save File"), 167 self.trUtf8("Save File"),
161 defaultFileName, 168 defaultFileName,
167 self.on_stopButton_clicked() 174 self.on_stopButton_clicked()
168 self.filenameLabel.setText(self.trUtf8("Download canceled: {0}")\ 175 self.filenameLabel.setText(self.trUtf8("Download canceled: {0}")\
169 .format(QFileInfo(defaultFileName).fileName())) 176 .format(QFileInfo(defaultFileName).fileName()))
170 self.__canceledFileSelect = True 177 self.__canceledFileSelect = True
171 return 178 return
172 fileInfo = QFileInfo(fileName) 179
173 Helpviewer.HelpWindow.HelpWindow.downloadManager().setDownloadDirectory( 180 fileInfo = QFileInfo(fileName)
174 fileInfo.absoluteDir().absolutePath()) 181 Helpviewer.HelpWindow.HelpWindow.downloadManager().setDownloadDirectory(
175 self.filenameLabel.setText(fileInfo.fileName()) 182 fileInfo.absoluteDir().absolutePath())
183 self.filenameLabel.setText(fileInfo.fileName())
176 184
177 self.__output.setFileName(fileName) 185 self.__output.setFileName(fileName)
186 self.__fileName = fileName
178 187
179 # check file path for saving 188 # check file path for saving
180 saveDirPath = QFileInfo(self.__output.fileName()).dir() 189 saveDirPath = QFileInfo(self.__fileName).dir()
181 if not saveDirPath.exists(): 190 if not saveDirPath.exists():
182 if not saveDirPath.mkpath(saveDirPath.absolutePath()): 191 if not saveDirPath.mkpath(saveDirPath.absolutePath()):
183 self.progressBar.setVisible(False) 192 self.progressBar.setVisible(False)
184 self.on_stopButton_clicked() 193 self.on_stopButton_clicked()
185 self.infoLabel.setText( 194 self.infoLabel.setText(
186 self.trUtf8("Download directory ({0}) couldn't be created.")\ 195 self.trUtf8("Download directory ({0}) couldn't be created.")\
187 .format(saveDirPath.absolutePath())) 196 .format(saveDirPath.absolutePath()))
188 return 197 return
189 198
190 self.filenameLabel.setText(QFileInfo(self.__output.fileName()).fileName()) 199 self.filenameLabel.setText(QFileInfo(self.__fileName).fileName())
191 if self.__requestFilename: 200 if self.__requestFilename:
192 self.__readyRead() 201 self.__readyRead()
193 202
194 def __saveFileName(self, directory): 203 def __saveFileName(self, directory):
195 """ 204 """
250 else: 259 else:
251 nam = Helpviewer.HelpWindow.HelpWindow.networkAccessManager() 260 nam = Helpviewer.HelpWindow.HelpWindow.networkAccessManager()
252 reply = nam.get(QNetworkRequest(self.__url)) 261 reply = nam.get(QNetworkRequest(self.__url))
253 if self.__output.exists(): 262 if self.__output.exists():
254 self.__output.remove() 263 self.__output.remove()
264 self.__output = QFile()
255 self.__reply = reply 265 self.__reply = reply
256 self.__initialize(tryAgain = True) 266 self.__initialize(tryAgain = True)
257 self.statusChanged.emit() 267 self.statusChanged.emit()
258 268
259 @pyqtSlot() 269 @pyqtSlot()
447 457
448 def __finished(self): 458 def __finished(self):
449 """ 459 """
450 Private slot to handle the download finished. 460 Private slot to handle the download finished.
451 """ 461 """
452 self.__readyRead()
453
454 self.__finishedDownloading = True 462 self.__finishedDownloading = True
455 if not self.__startedSaving: 463 if not self.__startedSaving:
456 return 464 return
457 465
458 noError = self.__reply.error() == QNetworkReply.NoError 466 noError = self.__reply.error() == QNetworkReply.NoError
481 """ 489 """
482 Public method to set the download icon. 490 Public method to set the download icon.
483 491
484 @param icon reference to the icon to be set (QIcon) 492 @param icon reference to the icon to be set (QIcon)
485 """ 493 """
486 # TODO: adjust the size
487 self.fileIcon.setPixmap(icon.pixmap(48, 48)) 494 self.fileIcon.setPixmap(icon.pixmap(48, 48))
488 495
489 def fileName(self): 496 def fileName(self):
490 """ 497 """
491 Public method to get the name of the output file. 498 Public method to get the name of the output file.
492 499
493 @return name of the output file (string) 500 @return name of the output file (string)
494 """ 501 """
495 return self.__output.fileName() 502 return self.__fileName
496 503
497 def absoluteFilePath(self): 504 def absoluteFilePath(self):
498 """ 505 """
499 Public method to get the absolute path of the output file. 506 Public method to get the absolute path of the output file.
500 507
501 @return absolute path of the output file (string) 508 @return absolute path of the output file (string)
502 """ 509 """
503 return QFileInfo(self.__output).absoluteFilePath() 510 return QFileInfo(self.__fileName).absoluteFilePath()
504 511
505 def getData(self): 512 def getData(self):
506 """ 513 """
507 Public method to get the relevant download data. 514 Public method to get the relevant download data.
508 515
509 @return tuple of URL, save location and done flag 516 @return tuple of URL, save location and done flag
510 (QUrl, string, boolean) 517 (QUrl, string, boolean)
511 """ 518 """
512 return (self.__url, QFileInfo(self.__output).filePath(), 519 return (self.__url, QFileInfo(self.__fileName).filePath(),
513 self.downloadedSuccessfully()) 520 self.downloadedSuccessfully())
514 521
515 def setData(self, data): 522 def setData(self, data):
516 """ 523 """
517 Public method to set the relevant download data. 524 Public method to set the relevant download data.
518 525
519 @param data tuple of URL, save location and done flag 526 @param data tuple of URL, save location and done flag
520 (QUrl, string, boolean) 527 (QUrl, string, boolean)
521 """ 528 """
522 self.__url = data[0] 529 self.__url = data[0]
523 self.__output.setFileName(data[1]) 530 self.__fileName = data[1]
524 531
525 self.filenameLabel.setText(QFileInfo(self.__output.fileName()).fileName()) 532 self.filenameLabel.setText(QFileInfo(self.__fileName).fileName())
533 self.infoLabel.setText(self.__fileName)
526 534
527 self.stopButton.setEnabled(False) 535 self.stopButton.setEnabled(False)
528 self.stopButton.setVisible(False) 536 self.stopButton.setVisible(False)
529 self.openButton.setEnabled(data[2]) 537 self.openButton.setEnabled(data[2])
530 self.openButton.setVisible(data[2]) 538 self.openButton.setVisible(data[2])

eric ide

mercurial