updatedialog_moc.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * updatedialog_moc.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "updatedialog_moc.h"
  12. #include "ui_updatedialog_moc.h"
  13. #include "../lib/CConfigHandler.h"
  14. #include "../lib/GameConstants.h"
  15. #include <QNetworkReply>
  16. #include <QNetworkRequest>
  17. UpdateDialog::UpdateDialog(bool calledManually, QWidget *parent):
  18. QDialog(parent),
  19. ui(new Ui::UpdateDialog),
  20. calledManually(calledManually)
  21. {
  22. ui->setupUi(this);
  23. if(calledManually)
  24. {
  25. setWindowModality(Qt::ApplicationModal);
  26. show();
  27. }
  28. connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
  29. if(settings["launcher"]["updateOnStartup"].Bool())
  30. ui->checkOnStartup->setCheckState(Qt::CheckState::Checked);
  31. currentVersion = GameConstants::VCMI_VERSION;
  32. setWindowTitle(QString::fromStdString(currentVersion));
  33. #ifdef VCMI_WINDOWS
  34. platformParameter = "windows";
  35. #elif defined(VCMI_MAC)
  36. platformParameter = "macos";
  37. #elif defined(VCMI_IOS)
  38. platformParameter = "ios";
  39. #elif defined(VCMI_ANDROID)
  40. platformParameter = "android";
  41. #elif defined(VCMI_UNIX)
  42. platformParameter = "linux";
  43. #endif
  44. QString url = QString::fromStdString(settings["launcher"]["updateConfigUrl"].String());
  45. QNetworkReply *response = networkManager.get(QNetworkRequest(QUrl(url)));
  46. connect(response, &QNetworkReply::finished, [&, response]{
  47. response->deleteLater();
  48. if(response->error() != QNetworkReply::NoError)
  49. {
  50. ui->versionLabel->setStyleSheet("QLabel { background-color : red; color : black; }");
  51. ui->versionLabel->setText(tr("Network error"));
  52. ui->plainTextEdit->setPlainText(response->errorString());
  53. return;
  54. }
  55. auto byteArray = response->readAll();
  56. JsonNode node(reinterpret_cast<const std::byte*>(byteArray.constData()), byteArray.size(), "<network packet from server at updateConfigUrl>");
  57. loadFromJson(node);
  58. });
  59. }
  60. UpdateDialog::~UpdateDialog()
  61. {
  62. delete ui;
  63. }
  64. void UpdateDialog::showUpdateDialog(bool isManually)
  65. {
  66. auto * dialog = new UpdateDialog(isManually);
  67. dialog->setAttribute(Qt::WA_DeleteOnClose);
  68. }
  69. void UpdateDialog::on_checkOnStartup_stateChanged(int state)
  70. {
  71. Settings node = settings.write["launcher"]["updateOnStartup"];
  72. node->Bool() = ui->checkOnStartup->isChecked();
  73. }
  74. void UpdateDialog::loadFromJson(const JsonNode & node)
  75. {
  76. if(node.getType() != JsonNode::JsonType::DATA_STRUCT ||
  77. node["updateType"].getType() != JsonNode::JsonType::DATA_STRING ||
  78. node["version"].getType() != JsonNode::JsonType::DATA_STRING ||
  79. node["changeLog"].getType() != JsonNode::JsonType::DATA_STRING ||
  80. node["downloadLinks"].getType() != JsonNode::JsonType::DATA_STRUCT) //we need at least one link - other are optional
  81. {
  82. ui->plainTextEdit->setPlainText(tr("Cannot read JSON from URL or incorrect JSON data"));
  83. return;
  84. }
  85. //check whether update is needed
  86. bool isFutureVersion = true;
  87. std::string newVersion = node["version"].String();
  88. for(auto & prevVersion : node["history"].Vector())
  89. {
  90. if(prevVersion.String() == currentVersion)
  91. isFutureVersion = false;
  92. }
  93. if(isFutureVersion || currentVersion == newVersion)
  94. {
  95. if(!calledManually)
  96. close();
  97. return;
  98. }
  99. if(!calledManually)
  100. {
  101. setWindowModality(Qt::ApplicationModal);
  102. show();
  103. }
  104. const auto updateType = node["updateType"].String();
  105. QString bgColor;
  106. if(updateType == "minor")
  107. bgColor = "gray";
  108. else if(updateType == "major")
  109. bgColor = "orange";
  110. else if(updateType == "critical")
  111. bgColor = "red";
  112. ui->versionLabel->setStyleSheet(QString("QLabel { background-color : %1; color : black; }").arg(bgColor));
  113. ui->versionLabel->setText(QString::fromStdString(newVersion));
  114. ui->plainTextEdit->setPlainText(QString::fromStdString(node["changeLog"].String()));
  115. QString downloadLink = QString::fromStdString(node["downloadLinks"]["other"].String());
  116. if(node["downloadLinks"][platformParameter].getType() == JsonNode::JsonType::DATA_STRING)
  117. downloadLink = QString::fromStdString(node["downloadLinks"][platformParameter].String());
  118. ui->downloadLink->setText(QString{"<a href=\"%1\">Download page</a>"}.arg(downloadLink));
  119. }