/src/gui/src/updater/update-dialog.cpp

https://github.com/Bionus/imgbrd-grabber · C++ · 104 lines · 83 code · 21 blank · 0 comment · 5 complexity · 0f9bd3e8a3b4306dffbb22d837e2fd3a MD5 · raw file

  1. #include "updater/update-dialog.h"
  2. #include <QMessageBox>
  3. #include <QProcess>
  4. #include <ui_update-dialog.h>
  5. #include "functions.h"
  6. #if !defined(Q_OS_WIN)
  7. #include <QDesktopServices>
  8. #include <QUrl>
  9. #endif
  10. UpdateDialog::UpdateDialog(bool *shouldQuit, QWidget *parent)
  11. : QDialog(parent), ui(new Ui::UpdateDialog), m_shouldQuit(shouldQuit), m_parent(parent)
  12. {
  13. ui->setupUi(this);
  14. ui->checkShowChangelog->hide();
  15. ui->scrollArea->hide();
  16. ui->progressDownload->hide();
  17. resize(300, 0);
  18. connect(&m_updater, &ProgramUpdater::finished, this, &UpdateDialog::checkForUpdatesDone);
  19. }
  20. UpdateDialog::~UpdateDialog()
  21. {
  22. delete ui;
  23. }
  24. void UpdateDialog::resizeToFit()
  25. {
  26. ui->scrollArea->setVisible(ui->checkShowChangelog->isChecked());
  27. const int width = ui->labelUpdateAvailable->size().width();
  28. ui->labelUpdateAvailable->setMinimumWidth(width);
  29. adjustSize();
  30. ui->labelUpdateAvailable->setMinimumWidth(0);
  31. }
  32. void UpdateDialog::checkForUpdates()
  33. {
  34. m_updater.checkForUpdates();
  35. }
  36. void UpdateDialog::checkForUpdatesDone(const QString &newVersion, bool available, const QString &changelog)
  37. {
  38. if (!available) {
  39. emit noUpdateAvailable();
  40. return;
  41. }
  42. const bool hasChangelog = !changelog.isEmpty();
  43. if (hasChangelog) {
  44. ui->labelChangelog->setTextFormat(Qt::RichText);
  45. ui->labelChangelog->setText(parseMarkdown(changelog));
  46. }
  47. ui->checkShowChangelog->setVisible(hasChangelog);
  48. ui->scrollArea->setVisible(hasChangelog && ui->checkShowChangelog->isChecked());
  49. ui->labelVersion->setText(tr("Version <b>%1</b>").arg(newVersion));
  50. show();
  51. activateWindow();
  52. }
  53. void UpdateDialog::accept()
  54. {
  55. #if defined(Q_OS_WIN)
  56. downloadUpdate();
  57. #else
  58. QDesktopServices::openUrl(m_updater.latestUrl());
  59. close();
  60. #endif
  61. }
  62. void UpdateDialog::downloadUpdate()
  63. {
  64. ui->progressDownload->show();
  65. m_updater.downloadUpdate();
  66. connect(&m_updater, &ProgramUpdater::downloadProgress, this, &UpdateDialog::downloadProgress);
  67. connect(&m_updater, &ProgramUpdater::downloadFinished, this, &UpdateDialog::downloadFinished);
  68. }
  69. void UpdateDialog::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
  70. {
  71. ui->progressDownload->setMaximum(bytesTotal);
  72. ui->progressDownload->setValue(bytesReceived);
  73. }
  74. void UpdateDialog::downloadFinished(const QString &path)
  75. {
  76. ui->progressDownload->setValue(ui->progressDownload->maximum());
  77. QProcess::startDetached(path);
  78. if (m_parent != nullptr) {
  79. m_parent->close();
  80. }
  81. *m_shouldQuit = true;
  82. qApp->exit();
  83. }