| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/NetPlay/GameDigestDialog.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <functional> |
| 8 | |
| 9 | #include <QDialogButtonBox> |
| 10 | #include <QGroupBox> |
| 11 | #include <QLabel> |
| 12 | #include <QProgressBar> |
| 13 | #include <QPushButton> |
| 14 | #include <QVBoxLayout> |
| 15 | |
| 16 | #include "Core/NetPlayClient.h" |
| 17 | #include "Core/NetPlayServer.h" |
| 18 | |
| 19 | #include "DolphinQt/Settings.h" |
| 20 | |
| 21 | static QString GetPlayerNameFromPID(int pid) |
| 22 | { |
| 23 | QString player_name = QObject::tr("Invalid Player ID"); |
| 24 | const auto client = Settings::Instance().GetNetPlayClient(); |
| 25 | if (!client) |
| 26 | return player_name; |
| 27 | |
| 28 | for (const auto* player : client->GetPlayers()) |
| 29 | { |
| 30 | if (player->pid == pid) |
| 31 | { |
| 32 | player_name = QString::fromStdString(player->name); |
| 33 | break; |
| 34 | } |
| 35 | } |
| 36 | return player_name; |
| 37 | } |
| 38 | |
| 39 | GameDigestDialog::GameDigestDialog(QWidget* parent) : QDialog(parent) |
| 40 | { |
| 41 | CreateWidgets(); |
| 42 | ConnectWidgets(); |
| 43 | setWindowTitle(tr("SHA1 Digest")); |
| 44 | setWindowFlags(Qt::Sheet | Qt::Dialog); |
| 45 | setWindowModality(Qt::WindowModal); |
| 46 | } |
| 47 | |
| 48 | void GameDigestDialog::CreateWidgets() |
| 49 | { |
| 50 | m_main_layout = new QVBoxLayout; |
| 51 | m_progress_box = new QGroupBox; |
| 52 | m_progress_layout = new QVBoxLayout; |
| 53 | m_button_box = new QDialogButtonBox(QDialogButtonBox::NoButton); |
| 54 | m_check_label = new QLabel; |
| 55 | |
| 56 | m_progress_box->setLayout(m_progress_layout); |
| 57 | |
| 58 | m_main_layout->addWidget(m_progress_box); |
| 59 | m_main_layout->addWidget(m_check_label); |
| 60 | m_main_layout->addWidget(m_button_box); |
| 61 | setLayout(m_main_layout); |
| 62 | } |
| 63 | |
| 64 | void GameDigestDialog::ConnectWidgets() |
| 65 | { |
| 66 | connect(m_button_box, &QDialogButtonBox::rejected, this, &GameDigestDialog::reject); |
| 67 | } |
| 68 | |
| 69 | void GameDigestDialog::show(const QString& title) |
| 70 | { |
| 71 | m_progress_box->setTitle(title); |
| 72 | |
| 73 | for (const auto& pair : m_progress_bars) |
| 74 | { |
| 75 | m_progress_layout->removeWidget(pair.second); |
| 76 | pair.second->deleteLater(); |
| 77 | } |
| 78 | |
| 79 | for (const auto& pair : m_status_labels) |
| 80 | { |
| 81 | m_progress_layout->removeWidget(pair.second); |
| 82 | pair.second->deleteLater(); |
| 83 | } |
| 84 | |
| 85 | m_progress_bars.clear(); |
| 86 | m_status_labels.clear(); |
| 87 | m_results.clear(); |
| 88 | m_check_label->setText(QString::fromStdString("")); |
| 89 | |
| 90 | const auto client = Settings::Instance().GetNetPlayClient(); |
| 91 | if (!client) |
| 92 | return; |
| 93 | |
| 94 | if (Settings::Instance().GetNetPlayServer()) |
| 95 | { |
| 96 | m_button_box->setStandardButtons(QDialogButtonBox::Cancel); |
| 97 | QPushButton* cancel_button = m_button_box->button(QDialogButtonBox::Cancel); |
| 98 | cancel_button->setAutoDefault(false); |
| 99 | cancel_button->setDefault(false); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | m_button_box->setStandardButtons(QDialogButtonBox::Close); |
| 104 | QPushButton* close_button = m_button_box->button(QDialogButtonBox::Close); |
| 105 | close_button->setAutoDefault(false); |
| 106 | close_button->setDefault(false); |
| 107 | } |
| 108 | |
| 109 | for (const auto* player : client->GetPlayers()) |
| 110 | { |
| 111 | m_progress_bars[player->pid] = new QProgressBar; |
| 112 | m_status_labels[player->pid] = new QLabel; |
| 113 | |
| 114 | m_progress_layout->addWidget(m_progress_bars[player->pid]); |
| 115 | m_progress_layout->addWidget(m_status_labels[player->pid]); |
| 116 | } |
| 117 | |
| 118 | QDialog::show(); |
| 119 | } |
| 120 | |
| 121 | void GameDigestDialog::SetProgress(int pid, int progress) |
| 122 | { |
| 123 | QString player_name = GetPlayerNameFromPID(pid); |
| 124 | |
| 125 | if (!m_status_labels.contains(pid)) |
| 126 | return; |
| 127 | |
| 128 | m_status_labels[pid]->setText( |
| 129 | tr("%1[%2]: %3 %").arg(player_name, QString::number(pid), QString::number(progress))); |
| 130 | m_progress_bars[pid]->setValue(progress); |
| 131 | } |
| 132 | |
| 133 | void GameDigestDialog::SetResult(int pid, const std::string& result) |
| 134 | { |
| 135 | QString player_name = GetPlayerNameFromPID(pid); |
| 136 | |
| 137 | if (!m_status_labels.contains(pid)) |
| 138 | return; |
| 139 | |
| 140 | m_status_labels[pid]->setText( |
| 141 | tr("%1[%2]: %3").arg(player_name, QString::number(pid), QString::fromStdString(result))); |
| 142 | |
| 143 | m_results.push_back(result); |
| 144 | |
| 145 | const auto client = Settings::Instance().GetNetPlayClient(); |
| 146 | if (client && m_results.size() >= client->GetPlayers().size()) |
| 147 | { |
| 148 | if (std::ranges::adjacent_find(m_results, std::ranges::not_equal_to{}) == m_results.end()) |
| 149 | { |
| 150 | m_check_label->setText(tr("The hashes match!")); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | m_check_label->setText(tr("The hashes do not match!")); |
| 155 | } |
| 156 | |
| 157 | m_button_box->setStandardButtons(QDialogButtonBox::Close); |
| 158 | QPushButton* close_button = m_button_box->button(QDialogButtonBox::Close); |
| 159 | close_button->setAutoDefault(false); |
| 160 | close_button->setDefault(false); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void GameDigestDialog::reject() |
| 165 | { |
| 166 | const auto server = Settings::Instance().GetNetPlayServer(); |
| 167 | |
| 168 | if (server) |
| 169 | server->AbortGameDigest(); |
| 170 | |
| 171 | QDialog::reject(); |
| 172 | } |