Updater.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2018 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DolphinQt/Updater.h"
  4. #include <cstdlib>
  5. #include <utility>
  6. #include <QCheckBox>
  7. #include <QDialog>
  8. #include <QDialogButtonBox>
  9. #include <QLabel>
  10. #include <QPushButton>
  11. #include <QTextBrowser>
  12. #include <QVBoxLayout>
  13. #include "Common/Version.h"
  14. #include "DolphinQt/QtUtils/RunOnObject.h"
  15. #include "DolphinQt/QtUtils/SetWindowDecorations.h"
  16. #include "DolphinQt/Settings.h"
  17. // Refer to docs/autoupdate_overview.md for a detailed overview of the autoupdate process
  18. Updater::Updater(QWidget* parent, std::string update_track, std::string hash_override)
  19. : m_parent(parent), m_update_track(std::move(update_track)),
  20. m_hash_override(std::move(hash_override))
  21. {
  22. connect(this, &QThread::finished, this, &QObject::deleteLater);
  23. }
  24. void Updater::run()
  25. {
  26. AutoUpdateChecker::CheckForUpdate(m_update_track, m_hash_override,
  27. AutoUpdateChecker::CheckType::Automatic);
  28. }
  29. void Updater::CheckForUpdate()
  30. {
  31. AutoUpdateChecker::CheckForUpdate(m_update_track, m_hash_override,
  32. AutoUpdateChecker::CheckType::Manual);
  33. }
  34. void Updater::OnUpdateAvailable(const NewVersionInformation& info)
  35. {
  36. if (std::getenv("DOLPHIN_UPDATE_SERVER_URL"))
  37. {
  38. TriggerUpdate(info, AutoUpdateChecker::RestartMode::RESTART_AFTER_UPDATE);
  39. RunOnObject(m_parent, [this] {
  40. m_parent->close();
  41. return 0;
  42. });
  43. return;
  44. }
  45. bool later = false;
  46. std::optional<int> choice = RunOnObject(m_parent, [&] {
  47. QDialog* dialog = new QDialog(m_parent);
  48. dialog->setAttribute(Qt::WA_DeleteOnClose, true);
  49. dialog->setWindowTitle(tr("Update available"));
  50. dialog->setWindowFlags(dialog->windowFlags() & ~Qt::WindowContextHelpButtonHint);
  51. auto* label = new QLabel(
  52. tr("<h2>A new version of Dolphin is available!</h2>Dolphin %1 is available for "
  53. "download. "
  54. "You are running %2.<br> Would you like to update?<br><h4>Release Notes:</h4>")
  55. .arg(QString::fromStdString(info.new_shortrev))
  56. .arg(QString::fromStdString(Common::GetScmDescStr())));
  57. label->setTextFormat(Qt::RichText);
  58. auto* changelog = new QTextBrowser;
  59. changelog->setHtml(QString::fromStdString(info.changelog_html));
  60. changelog->setOpenExternalLinks(true);
  61. changelog->setMinimumWidth(400);
  62. auto* update_later_check = new QCheckBox(tr("Update after closing Dolphin"));
  63. connect(update_later_check, &QCheckBox::toggled, [&](bool checked) { later = checked; });
  64. auto* buttons = new QDialogButtonBox;
  65. auto* never_btn =
  66. buttons->addButton(tr("Never Auto-Update"), QDialogButtonBox::DestructiveRole);
  67. buttons->addButton(tr("Remind Me Later"), QDialogButtonBox::RejectRole);
  68. buttons->addButton(tr("Install Update"), QDialogButtonBox::AcceptRole);
  69. auto* layout = new QVBoxLayout;
  70. dialog->setLayout(layout);
  71. layout->addWidget(label);
  72. layout->addWidget(changelog);
  73. layout->addWidget(update_later_check);
  74. layout->addWidget(buttons);
  75. connect(never_btn, &QPushButton::clicked, [dialog] {
  76. Settings::Instance().SetAutoUpdateTrack(QString{});
  77. dialog->reject();
  78. });
  79. connect(buttons, &QDialogButtonBox::accepted, dialog, &QDialog::accept);
  80. connect(buttons, &QDialogButtonBox::rejected, dialog, &QDialog::reject);
  81. SetQWidgetWindowDecorations(dialog);
  82. return dialog->exec();
  83. });
  84. if (choice && *choice == QDialog::Accepted)
  85. {
  86. TriggerUpdate(info, later ? AutoUpdateChecker::RestartMode::NO_RESTART_AFTER_UPDATE :
  87. AutoUpdateChecker::RestartMode::RESTART_AFTER_UPDATE);
  88. if (!later)
  89. {
  90. RunOnObject(m_parent, [this] {
  91. m_parent->close();
  92. return 0;
  93. });
  94. }
  95. }
  96. }