/*------------------------------------------------------------------------------ -- This file is a part of the QLop Software -- Copyright (C) 2015, Plasma Physics Laboratory - CNRS -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -------------------------------------------------------------------------------*/ /*-- Author : Alexis Jeandet -- Mail : alexis.jeandet@member.fsf.org ----------------------------------------------------------------------------*/ #include "filedownloadertask.h" FileDownloaderTask::FileDownloaderTask(QNetworkReply *headerreply, int ID, const QString &fileName, QObject *parent) : QObject(parent),p_acceptRestart(false) { this->m_Reply = headerreply; 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::Append); this->m_startPos = this->m_file->size(); this->m_startDateTime = QDateTime::currentDateTime(); this->m_URL = m_Reply->url().toString(); connect(this->m_Reply,SIGNAL(finished()),this,SLOT(gotHead())); } 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::restart(QNetworkReply *reply, bool acceptRestart) { if(!acceptRestart) this->m_file->seek(0); this->p_acceptRestart = acceptRestart; this->m_Reply = reply; 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())); } int FileDownloaderTask::startPosition() { return m_startPos; } void FileDownloaderTask::gotHead() { emit gotHead(this,m_Reply); } 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); }