2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* cdownloadmanager_moc.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2013-08-22 17:22:49 +03:00
|
|
|
#include "StdInc.h"
|
2013-08-24 23:11:51 +03:00
|
|
|
#include "cdownloadmanager_moc.h"
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2013-09-02 20:26:52 +03:00
|
|
|
#include "../launcherdirs.h"
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
CDownloadManager::CDownloadManager()
|
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
connect(&manager, SIGNAL(finished(QNetworkReply *)),
|
|
|
|
SLOT(downloadFinished(QNetworkReply *)));
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
void CDownloadManager::downloadFile(const QUrl & url, const QString & file)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
QNetworkRequest request(url);
|
|
|
|
FileEntry entry;
|
|
|
|
entry.file.reset(new QFile(CLauncherDirs::get().downloadsPath() + '/' + file));
|
|
|
|
entry.bytesReceived = 0;
|
|
|
|
entry.totalSize = 0;
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(entry.file->open(QIODevice::WriteOnly | QIODevice::Truncate))
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
entry.status = FileEntry::IN_PROGRESS;
|
|
|
|
entry.reply = manager.get(request);
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
connect(entry.reply, SIGNAL(downloadProgress(qint64,qint64)),
|
|
|
|
SLOT(downloadProgressChanged(qint64,qint64)));
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entry.status = FileEntry::FAILED;
|
2018-04-13 07:34:58 +02:00
|
|
|
entry.reply = nullptr;
|
2013-08-22 17:22:49 +03:00
|
|
|
encounteredErrors += entry.file->errorString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// even if failed - add it into list to report it in finished() call
|
|
|
|
currentDownloads.push_back(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
CDownloadManager::FileEntry & CDownloadManager::getEntry(QNetworkReply * reply)
|
|
|
|
{
|
|
|
|
assert(reply);
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto & entry : currentDownloads)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(entry.reply == reply)
|
2013-08-22 17:22:49 +03:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
static FileEntry errorValue;
|
|
|
|
return errorValue;
|
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
void CDownloadManager::downloadFinished(QNetworkReply * reply)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
FileEntry & file = getEntry(reply);
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(file.reply->error())
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
encounteredErrors += file.reply->errorString();
|
|
|
|
file.file->remove();
|
|
|
|
file.status = FileEntry::FAILED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file.file->write(file.reply->readAll());
|
|
|
|
file.file->close();
|
|
|
|
file.status = FileEntry::FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool downloadComplete = true;
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto & entry : currentDownloads)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(entry.status == FileEntry::IN_PROGRESS)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
downloadComplete = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList successful;
|
|
|
|
QStringList failed;
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto & entry : currentDownloads)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(entry.status == FileEntry::FINISHED)
|
2013-08-22 17:22:49 +03:00
|
|
|
successful += entry.file->fileName();
|
|
|
|
else
|
|
|
|
failed += entry.file->fileName();
|
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(downloadComplete)
|
2013-08-22 17:22:49 +03:00
|
|
|
emit finished(successful, failed, encounteredErrors);
|
2013-09-08 19:49:23 +03:00
|
|
|
|
|
|
|
file.reply->deleteLater();
|
|
|
|
file.reply = nullptr;
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CDownloadManager::downloadProgressChanged(qint64 bytesReceived, qint64 bytesTotal)
|
|
|
|
{
|
|
|
|
auto reply = dynamic_cast<QNetworkReply *>(sender());
|
|
|
|
FileEntry & entry = getEntry(reply);
|
|
|
|
|
|
|
|
entry.file->write(entry.reply->readAll());
|
|
|
|
entry.bytesReceived = bytesReceived;
|
|
|
|
entry.totalSize = bytesTotal;
|
|
|
|
|
|
|
|
quint64 total = 0;
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto & entry : currentDownloads)
|
2013-08-22 17:22:49 +03:00
|
|
|
total += entry.totalSize > 0 ? entry.totalSize : 0;
|
|
|
|
|
|
|
|
quint64 received = 0;
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto & entry : currentDownloads)
|
2013-08-22 17:22:49 +03:00
|
|
|
received += entry.bytesReceived > 0 ? entry.bytesReceived : 0;
|
|
|
|
|
|
|
|
emit downloadProgress(received, total);
|
|
|
|
}
|
2014-03-23 15:08:01 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
bool CDownloadManager::downloadInProgress(const QUrl & url)
|
2014-03-23 15:08:01 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto & entry : currentDownloads)
|
2014-03-23 15:08:01 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(entry.reply->url() == url)
|
2014-03-23 15:08:01 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|