/3rd/autoupdater/github-releases-autoupdater/src/updaterUI/cupdaterdialog.cpp

https://github.com/setvisible/DownZemAll · C++ · 102 lines · 84 code · 16 blank · 2 comment · 7 complexity · a1fb6853c6b5859472bb34a2e185b55e MD5 · raw file

  1. #include "cupdaterdialog.h"
  2. #include "ui_cupdaterdialog.h"
  3. #include <QDebug>
  4. #include <QDesktopServices>
  5. #include <QMessageBox>
  6. #include <QPushButton>
  7. #include <QStringBuilder>
  8. CUpdaterDialog::CUpdaterDialog(QWidget *parent, const QString& githubRepoAddress,
  9. const QString& versionString, bool silentCheck) :
  10. QDialog(parent),
  11. ui(new Ui::CUpdaterDialog),
  12. _silent(silentCheck),
  13. _updater(githubRepoAddress, versionString)
  14. {
  15. ui->setupUi(this);
  16. if (_silent) {
  17. hide();
  18. }
  19. connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  20. connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &CUpdaterDialog::applyUpdate);
  21. ui->buttonBox->button(QDialogButtonBox::Ok)->setText("Install");
  22. ui->stackedWidget->setCurrentIndex(0);
  23. ui->progressBar->setMaximum(0);
  24. ui->progressBar->setValue(0);
  25. ui->lblPercentage->setVisible(false);
  26. _updater.setUpdateStatusListener(this);
  27. _updater.checkForUpdates();
  28. }
  29. CUpdaterDialog::~CUpdaterDialog()
  30. {
  31. delete ui;
  32. }
  33. void CUpdaterDialog::applyUpdate()
  34. {
  35. #ifdef _WIN32
  36. ui->progressBar->setMaximum(100);
  37. ui->progressBar->setValue(0);
  38. ui->lblPercentage->setVisible(true);
  39. ui->lblOperationInProgress->setText("Downloading the update...");
  40. ui->stackedWidget->setCurrentIndex(0);
  41. _updater.downloadAndInstallUpdate(_latestUpdateUrl);
  42. #else
  43. QMessageBox msg(
  44. QMessageBox::Question,
  45. tr("Manual update required"),
  46. tr("Automatic update is not supported on this operating system. Do you want to download and install the update manually?"),
  47. QMessageBox::Yes | QMessageBox::No,
  48. this);
  49. if (msg.exec() == QMessageBox::Yes)
  50. QDesktopServices::openUrl(QUrl(_latestUpdateUrl));
  51. #endif
  52. }
  53. // If no updates are found, the changelog is empty
  54. void CUpdaterDialog::onUpdateAvailable(CAutoUpdaterGithub::ChangeLog changelog)
  55. {
  56. if (!changelog.empty())
  57. {
  58. ui->stackedWidget->setCurrentIndex(1);
  59. for (const auto& changelogItem: changelog)
  60. ui->changeLogViewer->append("<b>" % changelogItem.versionString % "</b>" % '\n' % changelogItem.versionChanges % "<p></p>");
  61. _latestUpdateUrl = changelog.front().versionUpdateUrl;
  62. show();
  63. }
  64. else
  65. {
  66. accept();
  67. if (!_silent)
  68. QMessageBox::information(this, tr("No update available"), tr("You already have the latest version of the program."));
  69. }
  70. }
  71. // percentageDownloaded >= 100.0f means the download has finished
  72. void CUpdaterDialog::onUpdateDownloadProgress(float percentageDownloaded)
  73. {
  74. ui->progressBar->setValue((int)percentageDownloaded);
  75. ui->lblPercentage->setText(QString::number(percentageDownloaded, 'f', 2) + " %");
  76. }
  77. void CUpdaterDialog::onUpdateDownloadFinished()
  78. {
  79. accept();
  80. }
  81. void CUpdaterDialog::onUpdateError(QString errorMessage)
  82. {
  83. reject();
  84. if (!_silent)
  85. QMessageBox::critical(this, tr("Error checking for updates"), tr(errorMessage.toUtf8().data()));
  86. }