Skip to content

Commit

Permalink
[EngineVersionTabWidget] Progress bar added to track engine version d…
Browse files Browse the repository at this point in the history
…ownload.
  • Loading branch information
Unarelith committed May 7, 2020
1 parent f9574fe commit 6913dac
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
1 change: 1 addition & 0 deletions source/DatabaseLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void DatabaseLoader::update() const {

const auto &repositoryList = m_data.repositoryList();
for (auto &it : repositoryList) {
emit updateProgressed(0);
emit stateChanged("Loading users from repository '" + it.second.name() + "'...");

updateModel<ContentUser>(it.second, "/api/user",
Expand Down
45 changes: 41 additions & 4 deletions source/EngineVersionTabWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
#include <QDir>
#include <QMenu>
#include <QNetworkReply>
#include <QProgressBar>
#include <QVBoxLayout>

#include <quazip/quazipfile.h>
Expand All @@ -34,12 +36,13 @@
#include "PathUtils.hpp"

EngineVersionTabWidget::EngineVersionTabWidget(ContentData &data, QWidget *parent) : QWidget(parent), m_data(data) {
m_versionListWidget.setHeaderLabels({"", tr("ID"), tr("Name"), tr("Repository"), tr("Creation date")});
m_versionListWidget.setHeaderLabels({"", tr("ID"), tr("Name"), tr("Repository"), tr("Creation date"), "", ""});
m_versionListWidget.setRootIsDecorated(false);
m_versionListWidget.setSortingEnabled(true);
m_versionListWidget.setContextMenuPolicy(Qt::CustomContextMenu);
m_versionListWidget.sortItems(2, Qt::AscendingOrder);
m_versionListWidget.sortItems(2, Qt::DescendingOrder);
m_versionListWidget.setColumnWidth(0, 27);
m_versionListWidget.setColumnWidth(5, 32);
m_versionListWidget.hideColumn(1);
m_versionListWidget.setSelectionMode(QAbstractItemView::NoSelection);
m_versionListWidget.setFocusPolicy(Qt::NoFocus);
Expand Down Expand Up @@ -71,7 +74,14 @@ void EngineVersionTabWidget::update() {
item->setText(3, repository->name());
else
item->setText(3, "N/A");

auto *progressBar = new QProgressBar;
progressBar->setRange(0, 100);
m_versionListWidget.setItemWidget(item, 5, progressBar);
}

for (int i = 0 ; i < 5 ; ++i)
m_versionListWidget.resizeColumnToContents(i);
}

void EngineVersionTabWidget::showContextMenu(const QPoint &pos) {
Expand Down Expand Up @@ -104,17 +114,42 @@ void EngineVersionTabWidget::showContextMenu(const QPoint &pos) {
void EngineVersionTabWidget::downloadActionTriggered() {
ContentEngineVersion *engineVersion = m_data.getEngineVersion(m_currentItem->text(1).toUInt());

if (engineVersion) {
QNetworkReply *reply = m_session.downloadRequest(engineVersion->doc());
connect(reply, &QNetworkReply::finished, [this, reply]() { unzipFile(reply); });
connect(reply, &QNetworkReply::downloadProgress, this, [this, reply](qint64 bytesReceived, qint64 bytesTotal) {
updateProgressBar(reply, bytesReceived, bytesTotal);
});

m_downloads.emplace(reply, m_currentItem);
}
}

void EngineVersionTabWidget::updateProgressBar(QNetworkReply *reply, qint64 bytesReceived, qint64 bytesTotal) {
QProgressBar *progressBar = (QProgressBar *)m_versionListWidget.itemWidget(m_downloads.at(reply), 5);
progressBar->setValue((float)bytesReceived / bytesTotal * 100.f);
}

void EngineVersionTabWidget::unzipFile(QNetworkReply *reply) {
ContentEngineVersion *engineVersion = m_data.getEngineVersion(m_downloads.at(reply)->text(1).toUInt());

if (engineVersion) {
QString path = PathUtils::getEngineVersionPath(*engineVersion);

QDir dir;
if (!dir.exists(path))
dir.mkpath(path);

m_session.download(engineVersion->doc(), path + "/content.zip");
if (!m_session.saveFileToDisk(reply, path + "/content.zip")) {
qDebug() << "Failed to save" << path + "/content.zip";
return;
}

QuaZip archive{path + "/content.zip"};
archive.open(QuaZip::mdUnzip);
if (!archive.open(QuaZip::mdUnzip)) {
qDebug() << "Failed to open archive. Error code:" << archive.getZipError();
return;
}

for(bool f = archive.goToFirstFile(); f; f = archive.goToNextFile()) {
QString filePath = archive.getCurrentFileName();
Expand Down Expand Up @@ -155,6 +190,8 @@ void EngineVersionTabWidget::downloadActionTriggered() {
engineVersion->setState(ContentEngineVersion::State::Downloaded);

update();

m_downloads.erase(reply);
}
}

Expand Down
7 changes: 7 additions & 0 deletions source/EngineVersionTabWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#ifndef ENGINEVERSIONTABWIDGET_HPP_
#define ENGINEVERSIONTABWIDGET_HPP_

#include <unordered_map>

#include <QTreeWidget>

#include "Session.hpp"
Expand All @@ -47,6 +49,9 @@ class EngineVersionTabWidget : public QWidget {
void showContextMenu(const QPoint &pos);

void downloadActionTriggered();
void updateProgressBar(QNetworkReply *reply, qint64 bytesReceived, qint64 bytesTotal);
void unzipFile(QNetworkReply *reply);

void removeActionTriggered();

ContentData &m_data;
Expand All @@ -56,6 +61,8 @@ class EngineVersionTabWidget : public QWidget {
QTreeWidgetItem *m_currentItem = nullptr;

Session m_session;

std::unordered_map<QNetworkReply *, QTreeWidgetItem *> m_downloads;
};

#endif // ENGINEVERSIONTABWIDGET_HPP_
33 changes: 33 additions & 0 deletions source/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,36 @@ bool Session::download(const QUrl &url, const QString &path) const {
return false;
}

QNetworkReply *Session::downloadRequest(const QUrl &url) const {
return m_network->get(QNetworkRequest(QUrl(url)));
}

bool Session::saveFileToDisk(QNetworkReply *reply, const QString &path) const {
QVariant statusCodeVariant = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
int statusCode = statusCodeVariant.toInt();

if (statusCode != 200) {
std::cerr << "Error: Http request failed. Code: " << statusCode << std::endl;
emit stateChanged("Request failed. (" + QString::number(statusCode) + ")");
emit httpError(statusCode);

if (statusCode == 401) {
QThread::currentThread()->requestInterruption();
emit userLoginRequired();
}
}
else {
QByteArray data = reply->readAll();
QSaveFile file{path};
if (file.open(QIODevice::WriteOnly)) {
file.write(data);
file.commit();
return true;
}
else
std::cerr << "Error: Failed to save file: " << path.toStdString() << std::endl;
}

return false;
}

2 changes: 2 additions & 0 deletions source/Session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Session : public QObject {
QJsonDocument get(const QString &url, const ParameterList &parameters = {}) const;

bool download(const QUrl &url, const QString &path) const;
QNetworkReply *downloadRequest(const QUrl &url) const;
bool saveFileToDisk(QNetworkReply *reply, const QString &path) const;

bool isLoggedIn() const { return m_isLoggedIn; }

Expand Down

0 comments on commit 6913dac

Please sign in to comment.