# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import QHttp
class QDownloader(QWidget):
def __init__(self, parent=None):
QTabWidget.__init__(self, parent)
self.parent = parent
self.url = ""
self.downloadPath = ""
self.qhttp = QHttp(self)
self.progressbar = QProgressBar(self)
self.cancel = QPushButton("Cancel", self)
self.layout = QHBoxLayout(self)
self.layout.addWidget(self.progressbar)
self.layout.addWidget(self.cancel)
self.show()
self.resize(self.size().width()*2, self.size().height())
self.connect(self.cancel, QtCore.SIGNAL("clicked()"), self.cancelDownload)
self.connect(self.qhttp, QtCore.SIGNAL("requestFinished(int, bool)"), self.httpRequestFinished)
self.connect(self.qhttp, QtCore.SIGNAL("dataReadProgress(int, int)"), self.updateDataReadProgress)
def download(self):
url = QUrl(self.url)
self.qhttp.setHost(url.host())
url = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/")
self.downloadPathName = QFile(self.downloadPath)
self.downloadPathName.open(QIODevice.WriteOnly)
self.downloadID = self.qhttp.get(QString(url), self.downloadPathName)
def httpRequestFinished(self, requestID, error):
if not requestID == self.downloadID:
return
self.downloadPathName.close()
self.close()
def updateDataReadProgress(self, done, total):
self.progressbar.setMaximum(total)
self.progressbar.setValue(done)
def cancelDownload(self):
self.qhttp.abort()
self.downloadPathName.close()
self.downloadPathName.remove()
self.close()