Helpviewer/Download/DownloadItem.py

changeset 679
b427350a9d97
parent 673
1c1f3a125e68
child 757
d3daf1d8f058
equal deleted inserted replaced
678:d388291f5b6c 679:b427350a9d97
63 self.fileIcon.setPixmap(icon.pixmap(48, 48)) 63 self.fileIcon.setPixmap(icon.pixmap(48, 48))
64 64
65 self.__reply = reply 65 self.__reply = reply
66 self.__requestFilename = requestFilename 66 self.__requestFilename = requestFilename
67 self.__page = webPage 67 self.__page = webPage
68 self.__pageUrl = webPage and webPage.mainFrame().url() or QUrl()
68 self.__toDownload = download 69 self.__toDownload = download
69 self.__bytesReceived = 0 70 self.__bytesReceived = 0
71 self.__bytesTotal = -1
70 self.__downloadTime = QTime() 72 self.__downloadTime = QTime()
71 self.__output = QFile() 73 self.__output = QFile()
72 self.__fileName = "" 74 self.__fileName = ""
73 self.__startedSaving = False 75 self.__startedSaving = False
74 self.__finishedDownloading = False 76 self.__finishedDownloading = False
90 if self.__reply is None: 92 if self.__reply is None:
91 return 93 return
92 94
93 self.__startedSaving = False 95 self.__startedSaving = False
94 self.__finishedDownloading = False 96 self.__finishedDownloading = False
97 self.__bytesReceived = 0
98 self.__bytesTotal = -1
95 99
96 self.openButton.setEnabled(False) 100 self.openButton.setEnabled(False)
97 self.openButton.setVisible(False) 101 self.openButton.setVisible(False)
98 102
99 # start timer for the download estimation 103 # start timer for the download estimation
241 @pyqtSlot() 245 @pyqtSlot()
242 def on_tryAgainButton_clicked(self): 246 def on_tryAgainButton_clicked(self):
243 """ 247 """
244 Private slot to retry the download. 248 Private slot to retry the download.
245 """ 249 """
250 self.retry()
251
252 def retry(self):
253 """
254 Public slot to retry the download.
255 """
246 if not self.tryAgainButton.isEnabled(): 256 if not self.tryAgainButton.isEnabled():
247 return 257 return
248 258
249 self.tryAgainButton.setEnabled(False) 259 self.tryAgainButton.setEnabled(False)
250 self.tryAgainButton.setVisible(False) 260 self.tryAgainButton.setVisible(False)
269 @pyqtSlot() 279 @pyqtSlot()
270 def on_stopButton_clicked(self): 280 def on_stopButton_clicked(self):
271 """ 281 """
272 Private slot to stop the download. 282 Private slot to stop the download.
273 """ 283 """
284 self.cancelDownload()
285
286 def cancelDownload(self):
287 """
288 Public slot to stop the download.
289 """
274 self.setUpdatesEnabled(False) 290 self.setUpdatesEnabled(False)
275 self.stopButton.setEnabled(False) 291 self.stopButton.setEnabled(False)
276 self.stopButton.setVisible(False) 292 self.stopButton.setVisible(False)
277 self.tryAgainButton.setEnabled(True) 293 self.tryAgainButton.setEnabled(True)
278 self.tryAgainButton.setVisible(True) 294 self.tryAgainButton.setVisible(True)
284 @pyqtSlot() 300 @pyqtSlot()
285 def on_openButton_clicked(self): 301 def on_openButton_clicked(self):
286 """ 302 """
287 Private slot to open the downloaded file. 303 Private slot to open the downloaded file.
288 """ 304 """
289 info = QFileInfo(self.__output) 305 self.openFile()
306
307 def openFile(self):
308 """
309 Public slot to open the downloaded file.
310 """
311 info = QFileInfo(self.__fileName)
290 url = QUrl.fromLocalFile(info.absoluteFilePath()) 312 url = QUrl.fromLocalFile(info.absoluteFilePath())
313 QDesktopServices.openUrl(url)
314
315 def openFolder(self):
316 """
317 Public slot to open the folder containing the downloaded file.
318 """
319 info = QFileInfo(self.__fileName)
320 url = QUrl.fromLocalFile(info.absolutePath())
291 QDesktopServices.openUrl(url) 321 QDesktopServices.openUrl(url)
292 322
293 def __readyRead(self): 323 def __readyRead(self):
294 """ 324 """
295 Private slot to read the available data. 325 Private slot to read the available data.
314 self.infoLabel.setText(self.trUtf8("Error saving: {0}")\ 344 self.infoLabel.setText(self.trUtf8("Error saving: {0}")\
315 .format(self.__output.errorString())) 345 .format(self.__output.errorString()))
316 self.on_stopButton_clicked() 346 self.on_stopButton_clicked()
317 else: 347 else:
318 self.__startedSaving = True 348 self.__startedSaving = True
319 if (self.bytesTotal() == 0 and self.__reply.atEnd()) or \ 349 if self.__finishedDownloading:
320 self.__finishedDownloading:
321 self.__finished() 350 self.__finished()
322 351
323 def __networkError(self): 352 def __networkError(self):
324 """ 353 """
325 Private slot to handle a network error. 354 Private slot to handle a network error.
346 375
347 @param bytesReceived number of bytes received (integer) 376 @param bytesReceived number of bytes received (integer)
348 @param bytesTotal number of total bytes (integer) 377 @param bytesTotal number of total bytes (integer)
349 """ 378 """
350 self.__bytesReceived = bytesReceived 379 self.__bytesReceived = bytesReceived
380 self.__bytesTotal = bytesTotal
351 currentValue = 0 381 currentValue = 0
352 totalValue = 0 382 totalValue = 0
353 if bytesTotal > 0: 383 if bytesTotal > 0:
354 currentValue = bytesReceived * 100 / bytesTotal 384 currentValue = bytesReceived * 100 / bytesTotal
355 totalValue = 100 385 totalValue = 100
363 """ 393 """
364 Public method to get the total number of bytes of the download. 394 Public method to get the total number of bytes of the download.
365 395
366 @return total number of bytes (integer) 396 @return total number of bytes (integer)
367 """ 397 """
368 total = self.__reply.header(QNetworkRequest.ContentLengthHeader) 398 if self.__bytesTotal == -1:
369 if total is None: 399 self.__bytesTotal = self.__reply.header(QNetworkRequest.ContentLengthHeader)
370 total = 0 400 if self.__bytesTotal is None:
371 return total 401 self.__bytesTotal = -1
402 return self.__bytesTotal
372 403
373 def bytesReceived(self): 404 def bytesReceived(self):
374 """ 405 """
375 Public method to get the number of bytes received. 406 Public method to get the number of bytes received.
376 407
385 @return estimation for the remaining time (float) 416 @return estimation for the remaining time (float)
386 """ 417 """
387 if not self.downloading(): 418 if not self.downloading():
388 return -1.0 419 return -1.0
389 420
390 if self.bytesTotal() == 0: 421 if self.bytesTotal() == -1:
391 return -1.0 422 return -1.0
392 423
393 timeRemaining = (self.bytesTotal() - self.bytesReceived()) / self.currentSpeed() 424 timeRemaining = (self.bytesTotal() - self.bytesReceived()) / self.currentSpeed()
394 425
395 # ETA should never be 0 426 # ETA should never be 0
424 455
425 info = "" 456 info = ""
426 if running: 457 if running:
427 remaining = "" 458 remaining = ""
428 459
429 if bytesTotal != 0: 460 if bytesTotal > 0:
430 remaining = timeString(timeRemaining) 461 remaining = timeString(timeRemaining)
431 462
432 info = self.trUtf8("{0} of {1} ({2}/sec) - {3}")\ 463 info = self.trUtf8("{0} of {1} ({2}/sec) - {3}")\
433 .format( 464 .format(
434 dataString(self.__bytesReceived), 465 dataString(self.__bytesReceived),
435 bytesTotal == 0 and self.trUtf8("?") \ 466 bytesTotal == -1 and self.trUtf8("?") \
436 or dataString(bytesTotal), 467 or dataString(bytesTotal),
437 dataString(int(speed)), 468 dataString(int(speed)),
438 remaining) 469 remaining)
439 else: 470 else:
440 if self.__bytesReceived == bytesTotal or bytesTotal == 0: 471 if self.__bytesReceived == bytesTotal or bytesTotal == -1:
441 info = self.trUtf8("{0} downloaded")\ 472 info = self.trUtf8("{0} downloaded")\
442 .format(dataString(self.__output.size())) 473 .format(dataString(self.__output.size()))
443 else: 474 else:
444 info = self.trUtf8("{0} of {1} - Stopped")\ 475 info = self.trUtf8("{0} of {1} - Stopped")\
445 .format(dataString(self.__bytesReceived), 476 .format(dataString(self.__bytesReceived),
459 Public method to check for a successful download. 490 Public method to check for a successful download.
460 491
461 @return flag indicating a successful download (boolean) 492 @return flag indicating a successful download (boolean)
462 """ 493 """
463 return self.stopButton.isHidden() and self.tryAgainButton.isHidden() 494 return self.stopButton.isHidden() and self.tryAgainButton.isHidden()
495
496 def downloadCanceled(self):
497 """
498 Public method to check, if the download was cancelled.
499
500 @return flag indicating a canceled download (boolean)
501 """
502 return self.tryAgainButton.isEnabled()
464 503
465 def __finished(self): 504 def __finished(self):
466 """ 505 """
467 Private slot to handle the download finished. 506 Private slot to handle the download finished.
468 """ 507 """
518 557
519 def getData(self): 558 def getData(self):
520 """ 559 """
521 Public method to get the relevant download data. 560 Public method to get the relevant download data.
522 561
523 @return tuple of URL, save location and done flag 562 @return tuple of URL, save location, flag and the
524 (QUrl, string, boolean) 563 URL of the related web page (QUrl, string, boolean,QUrl)
525 """ 564 """
526 return (self.__url, QFileInfo(self.__fileName).filePath(), 565 return (self.__url, QFileInfo(self.__fileName).filePath(),
527 self.downloadedSuccessfully()) 566 self.downloadedSuccessfully(), self.__pageUrl)
528 567
529 def setData(self, data): 568 def setData(self, data):
530 """ 569 """
531 Public method to set the relevant download data. 570 Public method to set the relevant download data.
532 571
533 @param data tuple of URL, save location and done flag 572 @param data tuple of URL, save location, flag and the
534 (QUrl, string, boolean) 573 URL of the related web page (QUrl, string, boolean, QUrl)
535 """ 574 """
536 self.__url = data[0] 575 self.__url = data[0]
537 self.__fileName = data[1] 576 self.__fileName = data[1]
577 self.__pageUrl = data[3]
538 578
539 self.filenameLabel.setText(QFileInfo(self.__fileName).fileName()) 579 self.filenameLabel.setText(QFileInfo(self.__fileName).fileName())
540 self.infoLabel.setText(self.__fileName) 580 self.infoLabel.setText(self.__fileName)
541 581
542 self.stopButton.setEnabled(False) 582 self.stopButton.setEnabled(False)
552 Public method to get the text of the info label. 592 Public method to get the text of the info label.
553 593
554 @return text of the info label (string) 594 @return text of the info label (string)
555 """ 595 """
556 return self.infoLabel.text() 596 return self.infoLabel.text()
597
598 def getPageUrl(self):
599 """
600 Public method to get the URL of the download page.
601
602 @return URL of the download page (QUrl)
603 """
604 return self.__pageUrl

eric ide

mercurial