| 1 | // Copyright 2026 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Settings/TriforcePane.h" |
| 5 | |
| 6 | #include <QDialogButtonBox> |
| 7 | #include <QGroupBox> |
| 8 | #include <QHeaderView> |
| 9 | #include <QPushButton> |
| 10 | #include <QTableWidget> |
| 11 | #include <QVBoxLayout> |
| 12 | |
| 13 | #include <fmt/format.h> |
| 14 | #include <fmt/ranges.h> |
| 15 | |
| 16 | #include "Common/Logging/Log.h" |
| 17 | |
| 18 | #include "Core/Config/MainSettings.h" |
| 19 | #include "Core/HW/DVD/AMMediaboard.h" |
| 20 | |
| 21 | #include "DolphinQt/Config/Mapping/MappingWindow.h" |
| 22 | #include "DolphinQt/QtUtils/NonDefaultQPushButton.h" |
| 23 | #include "DolphinQt/QtUtils/QtUtils.h" |
| 24 | |
| 25 | namespace |
| 26 | { |
| 27 | |
| 28 | constexpr int COLUMN_EMULATED = 0; |
| 29 | constexpr int COLUMN_REAL = 1; |
| 30 | constexpr int COLUMN_DESCRIPTION = 2; |
| 31 | |
| 32 | class IPRedirectionsDialog final : public QDialog |
| 33 | { |
| 34 | public: |
| 35 | explicit IPRedirectionsDialog(QWidget* parent); |
| 36 | |
| 37 | private: |
| 38 | void LoadConfig(); |
| 39 | void SaveConfig() const; |
| 40 | |
| 41 | void LoadDefault(); |
| 42 | void OnClear(); |
| 43 | |
| 44 | QTableWidget* m_ip_redirections_table; |
| 45 | }; |
| 46 | |
| 47 | } // namespace |
| 48 | |
| 49 | TriforcePane::TriforcePane() |
| 50 | { |
| 51 | auto* const main_layout = new QVBoxLayout{this}; |
| 52 | |
| 53 | auto* const controllers_group = new QGroupBox{tr("Controllers")}; |
| 54 | main_layout->addWidget(controllers_group); |
| 55 | |
| 56 | auto* const controllers_layout = new QVBoxLayout{controllers_group}; |
| 57 | |
| 58 | auto* const configure_controllers_button = new NonDefaultQPushButton{tr("Configure")}; |
| 59 | controllers_layout->addWidget(configure_controllers_button); |
| 60 | |
| 61 | connect(configure_controllers_button, &QPushButton::clicked, this, [this] { |
| 62 | auto* const window = new MappingWindow(this, MappingWindow::Type::MAPPING_AM_BASEBOARD, 0); |
| 63 | window->setAttribute(Qt::WA_DeleteOnClose, true); |
| 64 | window->setWindowModality(Qt::WindowModality::WindowModal); |
| 65 | window->show(); |
| 66 | }); |
| 67 | |
| 68 | auto* const ip_redirection_group = new QGroupBox{tr("IP Address Redirections")}; |
| 69 | main_layout->addWidget(ip_redirection_group); |
| 70 | |
| 71 | auto* const ip_redirection_layout = new QVBoxLayout{ip_redirection_group}; |
| 72 | |
| 73 | auto* const configure_ip_redirections_button = new NonDefaultQPushButton{tr("Configure")}; |
| 74 | ip_redirection_layout->addWidget(configure_ip_redirections_button); |
| 75 | |
| 76 | connect(configure_ip_redirections_button, &QPushButton::clicked, this, [this] { |
| 77 | auto* const ip_redirections = new IPRedirectionsDialog{this}; |
| 78 | ip_redirections->setAttribute(Qt::WA_DeleteOnClose); |
| 79 | ip_redirections->open(); |
| 80 | }); |
| 81 | |
| 82 | main_layout->addStretch(1); |
| 83 | } |
| 84 | |
| 85 | IPRedirectionsDialog::IPRedirectionsDialog(QWidget* parent) : QDialog{parent} |
| 86 | { |
| 87 | setWindowTitle(tr("IP Address Redirections")); |
| 88 | |
| 89 | auto* const main_layout = new QVBoxLayout{this}; |
| 90 | |
| 91 | auto* const load_default_button = new QPushButton(tr("Default")); |
| 92 | connect(load_default_button, &QPushButton::clicked, this, &IPRedirectionsDialog::LoadDefault); |
| 93 | |
| 94 | auto* const clear_button = new QPushButton(tr("Clear")); |
| 95 | connect(clear_button, &QPushButton::clicked, this, &IPRedirectionsDialog::OnClear); |
| 96 | |
| 97 | m_ip_redirections_table = new QTableWidget; |
| 98 | main_layout->addWidget(m_ip_redirections_table); |
| 99 | |
| 100 | m_ip_redirections_table->setColumnCount(3); |
| 101 | m_ip_redirections_table->setHorizontalHeaderLabels( |
| 102 | {tr("Emulated"), tr("Real"), tr("Description")}); |
| 103 | m_ip_redirections_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
| 104 | m_ip_redirections_table->setSizeAdjustPolicy(QTableWidget::AdjustToContents); |
| 105 | |
| 106 | m_ip_redirections_table->setEditTriggers(QAbstractItemView::DoubleClicked | |
| 107 | QAbstractItemView::EditKeyPressed); |
| 108 | |
| 109 | auto* const button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 110 | main_layout->addWidget(button_box); |
| 111 | |
| 112 | button_box->addButton(load_default_button, QDialogButtonBox::ActionRole); |
| 113 | button_box->addButton(clear_button, QDialogButtonBox::ActionRole); |
| 114 | |
| 115 | connect(button_box, &QDialogButtonBox::accepted, this, &IPRedirectionsDialog::accept); |
| 116 | connect(button_box, &QDialogButtonBox::rejected, this, &IPRedirectionsDialog::reject); |
| 117 | |
| 118 | connect(this, &IPRedirectionsDialog::accepted, this, &IPRedirectionsDialog::SaveConfig); |
| 119 | |
| 120 | connect(m_ip_redirections_table, &QTableWidget::itemChanged, this, |
| 121 | [this](QTableWidgetItem* item) { |
| 122 | const int row_count = m_ip_redirections_table->rowCount(); |
| 123 | const bool is_last_row = item->row() == row_count - 1; |
| 124 | if (is_last_row) |
| 125 | { |
| 126 | if (!item->text().isEmpty()) |
| 127 | m_ip_redirections_table->insertRow(row_count); |
| 128 | } |
| 129 | else if (item->column() != COLUMN_DESCRIPTION && item->text().isEmpty()) |
| 130 | { |
| 131 | // Just remove inner rows that have text erased. |
| 132 | // This UX could be less weird, but it's good enough for now. |
| 133 | m_ip_redirections_table->removeRow(item->row()); |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | LoadConfig(); |
| 138 | |
| 139 | QtUtils::AdjustSizeWithinScreen(this); |
| 140 | } |
| 141 | |
| 142 | void IPRedirectionsDialog::LoadConfig() |
| 143 | { |
| 144 | m_ip_redirections_table->setRowCount(0); |
| 145 | |
| 146 | // Add the final empty row. |
| 147 | m_ip_redirections_table->insertRow(0); |
| 148 | |
| 149 | int row = 0; |
| 150 | |
| 151 | const auto ip_redirections_str = Config::Get(Config::MAIN_TRIFORCE_IP_REDIRECTIONS); |
| 152 | for (auto&& ip_pair : ip_redirections_str | std::views::split(',')) |
| 153 | { |
| 154 | const auto ip_pair_str = std::string_view{ip_pair}; |
| 155 | const auto parsed = AMMediaboard::ParseIPRedirection(ip_pair_str); |
| 156 | if (!parsed.has_value()) |
| 157 | { |
| 158 | ERROR_LOG_FMT(COMMON, "Bad IP redirection string: {}", ip_pair_str); |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | m_ip_redirections_table->insertRow(row); |
| 163 | |
| 164 | m_ip_redirections_table->setItem(row, COLUMN_EMULATED, |
| 165 | new QTableWidgetItem(QString::fromUtf8(parsed->original))); |
| 166 | m_ip_redirections_table->setItem(row, COLUMN_REAL, |
| 167 | new QTableWidgetItem(QString::fromUtf8(parsed->replacement))); |
| 168 | m_ip_redirections_table->setItem(row, COLUMN_DESCRIPTION, |
| 169 | new QTableWidgetItem(QString::fromUtf8(parsed->description))); |
| 170 | |
| 171 | ++row; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void IPRedirectionsDialog::SaveConfig() const |
| 176 | { |
| 177 | // Ignore our empty row. |
| 178 | const int row_count = m_ip_redirections_table->rowCount() - 1; |
| 179 | |
| 180 | std::vector<std::string> replacements(row_count); |
| 181 | for (int row = 0; row < row_count; ++row) |
| 182 | { |
| 183 | auto* const original_item = m_ip_redirections_table->item(row, COLUMN_EMULATED); |
| 184 | auto* const replacement_item = m_ip_redirections_table->item(row, COLUMN_REAL); |
| 185 | |
| 186 | // Skip incomplete rows. |
| 187 | if (original_item == nullptr || replacement_item == nullptr) |
| 188 | continue; |
| 189 | |
| 190 | replacements[row] = fmt::format("{}={}", StripWhitespace(original_item->text().toStdString()), |
| 191 | replacement_item->text().toStdString()); |
| 192 | |
| 193 | auto* const description_item = m_ip_redirections_table->item(row, COLUMN_DESCRIPTION); |
| 194 | if (description_item == nullptr) |
| 195 | continue; |
| 196 | |
| 197 | const auto description = description_item->text().toStdString(); |
| 198 | |
| 199 | if (!description.empty()) |
| 200 | (replacements[row] += ' ') += description; |
| 201 | } |
| 202 | |
| 203 | Config::SetBaseOrCurrent(Config::MAIN_TRIFORCE_IP_REDIRECTIONS, |
| 204 | fmt::format("{}", fmt::join(replacements, ","))); |
| 205 | } |
| 206 | |
| 207 | void IPRedirectionsDialog::LoadDefault() |
| 208 | { |
| 209 | // This alters the config before "OK" is pressed. Bad UX.. |
| 210 | |
| 211 | Config::SetBaseOrCurrent(Config::MAIN_TRIFORCE_IP_REDIRECTIONS, Config::DefaultState{}); |
| 212 | |
| 213 | LoadConfig(); |
| 214 | } |
| 215 | |
| 216 | void IPRedirectionsDialog::OnClear() |
| 217 | { |
| 218 | m_ip_redirections_table->setRowCount(0); |
| 219 | // Add the final empty row. |
| 220 | m_ip_redirections_table->insertRow(0); |
| 221 | } |