#include "filedownloadertask.h" FileDownloaderTask::FileDownloaderTask(QNetworkReply *reply, int ID, const QString &fileName, QObject *parent) : QObject(parent) { this->m_Reply = reply; this->m_downloadComplete = false; this->m_FileName = fileName; this->m_taskId = ID; this->m_file = new QFile(fileName+".part"); this->m_file->open(QIODevice::WriteOnly|QIODevice::Truncate); this->m_startDateTime = QDateTime::currentDateTime(); this->m_URL = m_Reply->url().toString(); connect(this->m_Reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadProgress(qint64,qint64))); connect(this->m_Reply,SIGNAL(downloadProgress(qint64,qint64)),this,SIGNAL(updateProgress(qint64,qint64))); connect(this->m_Reply,SIGNAL(readyRead()),this,SLOT(readReady())); connect(this->m_Reply,SIGNAL(finished()),this,SLOT(downloadFinished())); } FileDownloaderTask::~FileDownloaderTask() { delete m_file; delete m_Reply; } int FileDownloaderTask::ID(){return m_taskId;} const QString &FileDownloaderTask::fileName(){return m_FileName;} const QString &FileDownloaderTask::url(){return m_URL;} const QDateTime &FileDownloaderTask::startDateTime(){return m_startDateTime;} bool FileDownloaderTask::downloadComplete(){return m_downloadComplete;} void FileDownloaderTask::downloadProgress(qint64 bytesSent, qint64 bytesTotal) { if(bytesTotal!=0) emit updateProgress((100*bytesSent)/bytesTotal); } void FileDownloaderTask::readReady() { this->m_file->write(this->m_Reply->readAll()); } void FileDownloaderTask::downloadFinished() { this->m_downloadComplete = true; this->m_file->write(this->m_Reply->readAll()); this->m_file->close(); this->m_file->rename(this->m_FileName); }