| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/WiiUpdate.h" |
| 5 | |
| 6 | #include <future> |
| 7 | |
| 8 | #include <QCloseEvent> |
| 9 | #include <QObject> |
| 10 | #include <QProgressDialog> |
| 11 | #include <QPushButton> |
| 12 | |
| 13 | #include "Common/Assert.h" |
| 14 | #include "Common/Flag.h" |
| 15 | |
| 16 | #include "Core/WiiUtils.h" |
| 17 | |
| 18 | #include "DiscIO/NANDImporter.h" |
| 19 | |
| 20 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 21 | #include "DolphinQt/QtUtils/QueueOnObject.h" |
| 22 | |
| 23 | namespace WiiUpdate |
| 24 | { |
| 25 | static void ShowResult(QWidget* parent, WiiUtils::UpdateResult result) |
| 26 | { |
| 27 | switch (result) |
| 28 | { |
| 29 | case WiiUtils::UpdateResult::Succeeded: |
| 30 | ModalMessageBox::information(parent, QObject::tr("Update completed"), |
| 31 | QObject::tr("The emulated Wii console has been updated.")); |
| 32 | DiscIO::NANDImporter().ExtractCertificates(); |
| 33 | break; |
| 34 | case WiiUtils::UpdateResult::AlreadyUpToDate: |
| 35 | ModalMessageBox::information(parent, QObject::tr("Update completed"), |
| 36 | QObject::tr("The emulated Wii console is already up-to-date.")); |
| 37 | DiscIO::NANDImporter().ExtractCertificates(); |
| 38 | break; |
| 39 | case WiiUtils::UpdateResult::ServerFailed: |
| 40 | ModalMessageBox::critical(parent, QObject::tr("Update failed"), |
| 41 | QObject::tr("Could not download update information from Nintendo. " |
| 42 | "Please check your Internet connection and try again.")); |
| 43 | break; |
| 44 | case WiiUtils::UpdateResult::DownloadFailed: |
| 45 | ModalMessageBox::critical(parent, QObject::tr("Update failed"), |
| 46 | QObject::tr("Could not download update files from Nintendo. " |
| 47 | "Please check your Internet connection and try again.")); |
| 48 | break; |
| 49 | case WiiUtils::UpdateResult::ImportFailed: |
| 50 | ModalMessageBox::critical(parent, QObject::tr("Update failed"), |
| 51 | QObject::tr("Could not install an update to the Wii system memory. " |
| 52 | "Please refer to logs for more information.")); |
| 53 | break; |
| 54 | case WiiUtils::UpdateResult::Cancelled: |
| 55 | ModalMessageBox::warning( |
| 56 | parent, QObject::tr("Update cancelled"), |
| 57 | QObject::tr("The update has been cancelled. It is strongly recommended to " |
| 58 | "finish it in order to avoid inconsistent system software versions.")); |
| 59 | break; |
| 60 | case WiiUtils::UpdateResult::RegionMismatch: |
| 61 | ModalMessageBox::critical( |
| 62 | parent, QObject::tr("Update failed"), |
| 63 | QObject::tr("The game's region does not match your console's. " |
| 64 | "To avoid issues with the system menu, it is not possible " |
| 65 | "to update the emulated console using this disc.")); |
| 66 | break; |
| 67 | case WiiUtils::UpdateResult::MissingUpdatePartition: |
| 68 | case WiiUtils::UpdateResult::DiscReadFailed: |
| 69 | ModalMessageBox::critical(parent, QObject::tr("Update failed"), |
| 70 | QObject::tr("The game disc does not contain any usable " |
| 71 | "update information.")); |
| 72 | break; |
| 73 | default: |
| 74 | ASSERT(false); |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | template <typename Callable, typename... Args> |
| 80 | static WiiUtils::UpdateResult ShowProgress(QWidget* parent, Callable function, Args&&... args) |
| 81 | { |
| 82 | // Do not allow the user to close the dialog. Instead, wait until the update is finished |
| 83 | // or cancelled. |
| 84 | class UpdateProgressDialog final : public QProgressDialog |
| 85 | { |
| 86 | public: |
| 87 | using QProgressDialog::QProgressDialog; |
| 88 | |
| 89 | protected: |
| 90 | void reject() override {} |
| 91 | }; |
| 92 | |
| 93 | UpdateProgressDialog dialog{parent}; |
| 94 | dialog.setLabelText(QObject::tr("Preparing to update...\nThis can take a while.")); |
| 95 | dialog.setWindowTitle(QObject::tr("Updating")); |
| 96 | // QProgressDialog doesn't set its minimum size correctly. |
| 97 | dialog.setMinimumSize(360, 150); |
| 98 | |
| 99 | // QProgressDialog doesn't allow us to disable the cancel button when it's pressed, |
| 100 | // so we have to pass it our own push button. Note: the dialog takes ownership of it. |
| 101 | auto* cancel_button = new QPushButton(QObject::tr("&Cancel"), parent); |
| 102 | dialog.setCancelButton(cancel_button); |
| 103 | Common::Flag was_cancelled; |
| 104 | QObject::disconnect(&dialog, &QProgressDialog::canceled, nullptr, nullptr); |
| 105 | QObject::connect(&dialog, &QProgressDialog::canceled, [&] { |
| 106 | dialog.setLabelText(QObject::tr("Finishing the update...\nThis can take a while.")); |
| 107 | cancel_button->setEnabled(false); |
| 108 | was_cancelled.Set(); |
| 109 | }); |
| 110 | |
| 111 | std::future<WiiUtils::UpdateResult> result = std::async(std::launch::async, [&] { |
| 112 | const WiiUtils::UpdateResult res = function( |
| 113 | [&](size_t processed, size_t total, u64 title_id) { |
| 114 | QueueOnObject(&dialog, [&dialog, &was_cancelled, processed, total, title_id] { |
| 115 | if (was_cancelled.IsSet()) |
| 116 | return; |
| 117 | |
| 118 | dialog.setRange(0, static_cast<int>(total)); |
| 119 | dialog.setValue(static_cast<int>(processed)); |
| 120 | dialog.setLabelText(QObject::tr("Updating title %1...\nThis can take a while.") |
| 121 | .arg(title_id, 16, 16, QLatin1Char('0'))); |
| 122 | }); |
| 123 | return !was_cancelled.IsSet(); |
| 124 | }, |
| 125 | std::forward<Args>(args)...); |
| 126 | QueueOnObject(&dialog, [&dialog] { dialog.done(0); }); |
| 127 | return res; |
| 128 | }); |
| 129 | |
| 130 | dialog.exec(); |
| 131 | return result.get(); |
| 132 | } |
| 133 | |
| 134 | void PerformOnlineUpdate(const std::string& region, QWidget* parent) |
| 135 | { |
| 136 | const int confirm = ModalMessageBox::question( |
| 137 | parent, QObject::tr("Confirm"), |
| 138 | QObject::tr("Connect to the Internet and perform an online system update?")); |
| 139 | if (confirm != QMessageBox::Yes) |
| 140 | return; |
| 141 | |
| 142 | const WiiUtils::UpdateResult result = ShowProgress(parent, WiiUtils::DoOnlineUpdate, region); |
| 143 | ShowResult(parent, result); |
| 144 | } |
| 145 | |
| 146 | void PerformDiscUpdate(const std::string& file_path, QWidget* parent) |
| 147 | { |
| 148 | const WiiUtils::UpdateResult result = ShowProgress(parent, WiiUtils::DoDiscUpdate, file_path); |
| 149 | ShowResult(parent, result); |
| 150 | } |
| 151 | } // namespace WiiUpdate |