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