installdialog.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "installdialog.h"
  2. #include "mainwindow.h"
  3. #include "ui_installdialog.h"
  4. #include "../node/Defaults.hpp"
  5. #include "../node/SoftwareUpdater.hpp"
  6. #include <QMainWindow>
  7. #include <QMessageBox>
  8. #include <QByteArray>
  9. #include <QSslSocket>
  10. InstallDialog::InstallDialog(QWidget *parent) :
  11. QDialog(parent),
  12. ui(new Ui::InstallDialog),
  13. nam(new QNetworkAccessManager(this)),
  14. phase(FETCHING_NFO)
  15. {
  16. ui->setupUi(this);
  17. QObject::connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(on_networkReply(QNetworkReply*)));
  18. const char *nfoUrl = ZeroTier::ZT_DEFAULTS.updateLatestNfoURL.c_str();
  19. if (!*nfoUrl) {
  20. QMessageBox::critical(this,"Download Failed","Download failed: internal error: no update URL configured in build!",QMessageBox::Ok,QMessageBox::NoButton);
  21. QApplication::exit(1);
  22. return;
  23. }
  24. QNetworkReply *reply = nam->get(QNetworkRequest(QUrl(nfoUrl)));
  25. QObject::connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(on_downloadProgress(qint64,qint64)));
  26. }
  27. InstallDialog::~InstallDialog()
  28. {
  29. delete ui;
  30. }
  31. void InstallDialog::on_networkReply(QNetworkReply *reply)
  32. {
  33. reply->deleteLater();
  34. if (reply->error() != QNetworkReply::NoError) {
  35. QMessageBox::critical(this,"Download Failed",QString("Download failed: ") + reply->errorString(),QMessageBox::Ok,QMessageBox::NoButton);
  36. QApplication::exit(1);
  37. } else {
  38. if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) {
  39. QByteArray installerData(reply->readAll());
  40. switch(phase) {
  41. case FETCHING_NFO: {
  42. unsigned int vMajor = 0,vMinor = 0,vRevision = 0;
  43. installerData.append((char)0);
  44. const char *err = ZeroTier::SoftwareUpdater::parseNfo(installerData.data(),vMajor,vMinor,vRevision,signedBy,signature,url);
  45. if (err) {
  46. QMessageBox::critical(this,"Download Failed","Download failed: there is a problem with the software update web site.\nTry agian later. (invalid .nfo file)",QMessageBox::Ok,QMessageBox::NoButton);
  47. QApplication::exit(1);
  48. return;
  49. }
  50. phase = FETCHING_INSTALLER;
  51. reply = nam->get(QNetworkRequest(QUrl(url.c_str())));
  52. QObject::connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(on_downloadProgress(qint64,qint64)));
  53. } break;
  54. case FETCHING_INSTALLER: {
  55. if (!ZeroTier::SoftwareUpdater::validateUpdate(installerData.data(),installerData.length(),signedBy,signature)) {
  56. QMessageBox::critical(this,"Download Failed","Download failed: there is a problem with the software update web site.\nTry agian later. (failed signature check)",QMessageBox::Ok,QMessageBox::NoButton);
  57. QApplication::exit(1);
  58. return;
  59. }
  60. } break;
  61. }
  62. ui->progressBar->setMinimum(0);
  63. ui->progressBar->setMaximum(100);
  64. ui->progressBar->setValue(0);
  65. } else {
  66. QMessageBox::critical(this,"Download Failed",QString("Download failed: HTTP status code ") + reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString(),QMessageBox::Ok,QMessageBox::NoButton);
  67. QApplication::exit(1);
  68. }
  69. }
  70. }
  71. void InstallDialog::on_InstallDialog_rejected()
  72. {
  73. QApplication::exit();
  74. }
  75. //((QMainWindow *)this->parent())->setHidden(false);
  76. void InstallDialog::on_cancelButton_clicked()
  77. {
  78. QApplication::exit();
  79. }
  80. void InstallDialog::on_downloadProgress(qint64 bytesReceived,qint64 bytesTotal)
  81. {
  82. if (bytesTotal <= 0) {
  83. ui->progressBar->setValue(0);
  84. ui->progressBar->setMinimum(0);
  85. ui->progressBar->setMaximum(0);
  86. } else {
  87. double pct = ((double)bytesReceived / (double)bytesTotal) * 100.0;
  88. if (pct > 100.0)
  89. pct = 100.0;
  90. ui->progressBar->setMinimum(0);
  91. ui->progressBar->setMaximum(100);
  92. ui->progressBar->setValue((int)pct);
  93. }
  94. }