pollymc/logic/net/DownloadJob.cpp

93 lines
2.3 KiB
C++
Raw Normal View History

#include "DownloadJob.h"
#include "pathutils.h"
#include "MultiMC.h"
#include "FileDownload.h"
#include "ByteArrayDownload.h"
#include "CacheDownload.h"
2013-07-06 05:20:07 +05:30
ByteArrayDownloadPtr DownloadJob::add ( QUrl url )
2013-07-06 05:20:07 +05:30
{
ByteArrayDownloadPtr ptr (new ByteArrayDownload(url));
ptr->index_within_job = downloads.size();
downloads.append(ptr);
2013-09-10 03:59:25 +05:30
parts_progress.append(QPair<qint64, qint64>(0,1));
total_progress++;
return ptr;
2013-07-06 05:20:07 +05:30
}
FileDownloadPtr DownloadJob::add ( QUrl url, QString rel_target_path)
2013-07-06 05:20:07 +05:30
{
FileDownloadPtr ptr (new FileDownload(url, rel_target_path));
ptr->index_within_job = downloads.size();
downloads.append(ptr);
2013-09-10 03:59:25 +05:30
parts_progress.append(QPair<qint64, qint64>(0,1));
total_progress++;
return ptr;
2013-07-06 05:20:07 +05:30
}
2013-09-02 03:55:40 +05:30
CacheDownloadPtr DownloadJob::add ( QUrl url, MetaEntryPtr entry)
2013-09-02 03:55:40 +05:30
{
CacheDownloadPtr ptr (new CacheDownload(url, entry));
2013-09-02 03:55:40 +05:30
ptr->index_within_job = downloads.size();
downloads.append(ptr);
2013-09-10 03:59:25 +05:30
parts_progress.append(QPair<qint64, qint64>(0,1));
total_progress++;
2013-09-02 03:55:40 +05:30
return ptr;
}
void DownloadJob::partSucceeded ( int index )
{
num_succeeded++;
qDebug() << m_job_name.toLocal8Bit() << " progress: " << num_succeeded << "/" << downloads.size();
2013-09-02 03:55:40 +05:30
if(num_failed + num_succeeded == downloads.size())
{
if(num_failed)
{
qDebug() << m_job_name.toLocal8Bit() << " failed.";
2013-09-02 03:55:40 +05:30
emit failed();
}
else
{
qDebug() << m_job_name.toLocal8Bit() << " succeeded.";
2013-09-02 03:55:40 +05:30
emit succeeded();
}
}
}
void DownloadJob::partFailed ( int index )
{
num_failed++;
if(num_failed + num_succeeded == downloads.size())
{
qDebug() << m_job_name.toLocal8Bit() << " failed.";
2013-09-07 01:28:51 +05:30
emit failed();
2013-09-02 03:55:40 +05:30
}
}
void DownloadJob::partProgress ( int index, qint64 bytesReceived, qint64 bytesTotal )
{
2013-09-10 03:59:25 +05:30
auto & slot = parts_progress[index];
current_progress -= slot.first;
slot.first = bytesReceived;
current_progress += slot.first;
total_progress -= slot.second;
slot.second = bytesTotal;
total_progress += slot.second;
emit progress(current_progress, total_progress);
2013-09-02 03:55:40 +05:30
}
void DownloadJob::start()
{
qDebug() << m_job_name.toLocal8Bit() << " started.";
2013-09-02 03:55:40 +05:30
for(auto iter: downloads)
{
connect(iter.data(), SIGNAL(succeeded(int)), SLOT(partSucceeded(int)));
2013-09-10 03:59:25 +05:30
connect(iter.data(), SIGNAL(failed(int)), SLOT(partFailed(int)));
connect(iter.data(), SIGNAL(progress(int,qint64,qint64)), SLOT(partProgress(int,qint64,qint64)));
2013-09-02 03:55:40 +05:30
iter->start();
}
}