1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/launcher/updatedialog_moc.cpp

148 lines
4.0 KiB
C++
Raw Normal View History

2022-08-25 05:34:23 +02:00
/*
* updatedialog_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
*
*/
#include "StdInc.h"
2022-08-25 01:14:12 +02:00
#include "updatedialog_moc.h"
#include "ui_updatedialog_moc.h"
2022-08-25 01:43:58 +02:00
#include "../lib/CConfigHandler.h"
2022-08-25 03:09:20 +02:00
#include "../lib/GameConstants.h"
#include <QNetworkReply>
#include <QNetworkRequest>
2022-08-25 01:43:58 +02:00
2022-08-26 01:11:35 +02:00
UpdateDialog::UpdateDialog(bool calledManually, QWidget *parent):
2022-08-25 01:14:12 +02:00
QDialog(parent),
ui(new Ui::UpdateDialog),
calledManually(calledManually)
2022-08-25 01:14:12 +02:00
{
ui->setupUi(this);
2022-08-25 01:43:58 +02:00
if(calledManually)
{
setWindowModality(Qt::ApplicationModal);
show();
}
2022-08-25 01:43:58 +02:00
connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
2022-08-26 01:07:07 +02:00
if(settings["launcher"]["updateOnStartup"].Bool())
2022-08-25 01:43:58 +02:00
ui->checkOnStartup->setCheckState(Qt::CheckState::Checked);
2022-08-25 03:09:20 +02:00
2022-08-25 03:34:36 +02:00
currentVersion = GameConstants::VCMI_VERSION;
2022-08-25 03:34:36 +02:00
setWindowTitle(QString::fromStdString(currentVersion));
2022-08-25 03:09:20 +02:00
#ifdef VCMI_WINDOWS
platformParameter = "windows";
#elif defined(VCMI_MAC)
platformParameter = "macos";
#elif defined(VCMI_IOS)
platformParameter = "ios";
#elif defined(VCMI_ANDROID)
platformParameter = "android";
2022-08-25 05:34:23 +02:00
#elif defined(VCMI_UNIX)
2022-08-25 03:09:20 +02:00
platformParameter = "linux";
#endif
QString url = QString::fromStdString(settings["launcher"]["updateConfigUrl"].String());
2022-08-26 01:07:07 +02:00
QNetworkReply *response = networkManager.get(QNetworkRequest(QUrl(url)));
2022-08-25 03:09:20 +02:00
2022-08-26 20:26:56 +02:00
connect(response, &QNetworkReply::finished, [&, response]{
2022-08-25 03:09:20 +02:00
response->deleteLater();
2022-08-26 01:07:07 +02:00
2022-08-25 03:09:20 +02:00
if(response->error() != QNetworkReply::NoError)
{
2022-08-25 05:30:50 +02:00
ui->versionLabel->setStyleSheet("QLabel { background-color : red; color : black; }");
ui->versionLabel->setText("Network error");
2022-08-25 03:09:20 +02:00
ui->plainTextEdit->setPlainText(response->errorString());
return;
}
auto byteArray = response->readAll();
JsonNode node(byteArray.constData(), byteArray.size());
loadFromJson(node);
2022-08-26 01:07:07 +02:00
});
2022-08-25 01:14:12 +02:00
}
UpdateDialog::~UpdateDialog()
{
delete ui;
}
void UpdateDialog::showUpdateDialog(bool isManually)
2022-08-25 01:14:12 +02:00
{
2022-08-26 01:11:35 +02:00
UpdateDialog * dialog = new UpdateDialog(isManually);
2022-08-25 01:14:12 +02:00
dialog->setAttribute(Qt::WA_DeleteOnClose);
}
2022-08-25 01:43:58 +02:00
void UpdateDialog::on_checkOnStartup_stateChanged(int state)
{
Settings node = settings.write["launcher"]["updateOnStartup"];
2022-08-26 20:26:56 +02:00
node->Bool() = ui->checkOnStartup->isChecked();
2022-08-25 01:43:58 +02:00
}
2022-08-25 03:09:20 +02:00
void UpdateDialog::loadFromJson(const JsonNode & node)
{
if(node.getType() != JsonNode::JsonType::DATA_STRUCT ||
node["updateType"].getType() != JsonNode::JsonType::DATA_STRING ||
node["version"].getType() != JsonNode::JsonType::DATA_STRING ||
node["changeLog"].getType() != JsonNode::JsonType::DATA_STRING ||
node.getType() != JsonNode::JsonType::DATA_STRUCT) //we need at least one link - other are optional
{
ui->plainTextEdit->setPlainText("Cannot read JSON from url or incorrect JSON data");
return;
}
2022-08-25 03:34:36 +02:00
//check whether update is needed
2022-08-28 10:41:36 +02:00
bool isFutureVersion = true;
2022-08-25 03:34:36 +02:00
std::string newVersion = node["version"].String();
2022-08-28 10:41:36 +02:00
for(auto & prevVersion : node["history"].Vector())
{
if(prevVersion.String() == currentVersion)
isFutureVersion = false;
}
if(isFutureVersion || currentVersion == newVersion)
{
if(!calledManually)
close();
2022-08-25 03:34:36 +02:00
return;
}
if(!calledManually)
{
setWindowModality(Qt::ApplicationModal);
show();
}
2022-08-25 03:34:36 +02:00
2022-08-26 01:07:07 +02:00
const auto updateType = node["updateType"].String();
QString bgColor;
if(updateType == "minor")
bgColor = "gray";
else if(updateType == "major")
bgColor = "orange";
else if(updateType == "critical")
bgColor = "red";
2022-08-25 03:09:20 +02:00
2022-08-26 20:58:46 +02:00
ui->versionLabel->setStyleSheet(QString("QLabel { background-color : %1; color : black; }").arg(bgColor));
2022-08-25 03:34:36 +02:00
ui->versionLabel->setText(QString::fromStdString(newVersion));
2022-08-25 03:09:20 +02:00
ui->plainTextEdit->setPlainText(QString::fromStdString(node["changeLog"].String()));
QString downloadLink = QString::fromStdString(node["downloadLinks"]["other"].String());
if(node["downloadLinks"][platformParameter].getType() == JsonNode::JsonType::DATA_STRING)
downloadLink = QString::fromStdString(node["downloadLinks"][platformParameter].String());
2022-08-26 20:26:56 +02:00
ui->downloadLink->setText(QString{"<a href=\"%1\">Download page</a>"}.arg(downloadLink));
2022-08-25 03:09:20 +02:00
}