2018-08-19 19:02:49 +02:00
|
|
|
#include "DownloadManager.hpp"
|
|
|
|
|
2018-09-30 19:15:17 +02:00
|
|
|
#include "debug/Log.hpp"
|
2018-08-29 23:39:02 +02:00
|
|
|
#include "singletons/Paths.hpp"
|
|
|
|
|
2018-08-19 19:02:49 +02:00
|
|
|
#include <QDesktopServices>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
DownloadManager::DownloadManager(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
manager = new QNetworkAccessManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadManager::~DownloadManager()
|
|
|
|
{
|
|
|
|
manager->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManager::setFile(QString fileURL, const QString &channelName)
|
|
|
|
{
|
|
|
|
QString filePath = fileURL;
|
|
|
|
QString saveFilePath;
|
|
|
|
QStringList filePathList = filePath.split('/');
|
2018-08-29 23:39:02 +02:00
|
|
|
saveFilePath =
|
|
|
|
getPaths()->twitchProfileAvatars + "/twitch/" + channelName + ".png";
|
2018-08-19 19:02:49 +02:00
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(QUrl(fileURL));
|
|
|
|
reply = manager->get(request);
|
|
|
|
|
|
|
|
file = new QFile;
|
|
|
|
file->setFileName(saveFilePath);
|
|
|
|
file->open(QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this,
|
|
|
|
SLOT(onDownloadProgress(qint64, qint64)));
|
|
|
|
connect(manager, SIGNAL(finished(QNetworkReply *)), this,
|
|
|
|
SLOT(onFinished(QNetworkReply *)));
|
|
|
|
connect(reply, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
|
|
|
|
connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManager::onDownloadProgress(qint64 bytesRead, qint64 bytesTotal)
|
|
|
|
{
|
2018-09-30 19:15:17 +02:00
|
|
|
log("Download progress: {}/{}", bytesRead, bytesTotal);
|
2018-08-19 19:02:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManager::onFinished(QNetworkReply *reply)
|
|
|
|
{
|
|
|
|
switch (reply->error()) {
|
|
|
|
case QNetworkReply::NoError: {
|
|
|
|
qDebug("file is downloaded successfully.");
|
|
|
|
} break;
|
|
|
|
default: {
|
2018-09-30 19:15:17 +02:00
|
|
|
qDebug() << reply->errorString().toLatin1();
|
2018-08-19 19:02:49 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file->isOpen()) {
|
|
|
|
file->close();
|
|
|
|
file->deleteLater();
|
|
|
|
}
|
|
|
|
emit downloadComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManager::onReadyRead()
|
|
|
|
{
|
|
|
|
file->write(reply->readAll());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManager::onReplyFinished()
|
|
|
|
{
|
|
|
|
if (file->isOpen()) {
|
|
|
|
file->close();
|
|
|
|
file->deleteLater();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace chatterino
|