Debian Codegraph @

Source/Core/DolphinQt/NetPlay/NetPlaySetupDialog.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/NetPlay/NetPlaySetupDialog.h"
5
6 #include <memory>
7
8 #include <QCheckBox>
9 #include <QComboBox>
10 #include <QDialogButtonBox>
11 #include <QGridLayout>
12 #include <QLabel>
13 #include <QLineEdit>
14 #include <QListWidget>
15 #include <QPushButton>
16 #include <QSignalBlocker>
17 #include <QSpinBox>
18 #include <QTabWidget>
19
20 #include "Core/Config/NetplaySettings.h"
21 #include "Core/NetPlayProto.h"
22
23 #include "DolphinQt/QtUtils/ModalMessageBox.h"
24 #include "DolphinQt/QtUtils/NonDefaultQPushButton.h"
25 #include "DolphinQt/QtUtils/UTF8CodePointCountValidator.h"
26 #include "DolphinQt/Settings.h"
27
28 #include "UICommon/GameFile.h"
29 #include "UICommon/NetPlayIndex.h"
30
31 NetPlaySetupDialog::NetPlaySetupDialog(const GameListModel& game_list_model, QWidget* parent)
32 : QDialog(parent), m_game_list_model(game_list_model)
33 {
34 setWindowTitle(tr("NetPlay Setup"));
35
36 CreateMainLayout();
37
38 const bool use_index = Config::Get(Config::NETPLAY_USE_INDEX);
39 const std::string index_region = Config::Get(Config::NETPLAY_INDEX_REGION);
40 const std::string index_name = Config::Get(Config::NETPLAY_INDEX_NAME);
41 const std::string index_password = Config::Get(Config::NETPLAY_INDEX_PASSWORD);
42 const std::string nickname = Config::Get(Config::NETPLAY_NICKNAME);
43 const std::string traversal_choice = Config::Get(Config::NETPLAY_TRAVERSAL_CHOICE);
44 const int connect_port = Config::Get(Config::NETPLAY_CONNECT_PORT);
45 const int host_port = Config::Get(Config::NETPLAY_HOST_PORT);
46 const int host_listen_port = Config::Get(Config::NETPLAY_LISTEN_PORT);
47 const bool enable_chunked_upload_limit = Config::Get(Config::NETPLAY_ENABLE_CHUNKED_UPLOAD_LIMIT);
48 const u32 chunked_upload_limit = Config::Get(Config::NETPLAY_CHUNKED_UPLOAD_LIMIT);
49 #ifdef USE_UPNP
50 const bool use_upnp = Config::Get(Config::NETPLAY_USE_UPNP);
51
52 m_host_upnp->setChecked(use_upnp);
53 #endif
54
55 m_nickname_edit->setText(QString::fromStdString(nickname));
56 m_connection_type->setCurrentIndex(traversal_choice == "direct" ? 0 : 1);
57 m_connect_port_box->setValue(connect_port);
58 m_host_port_box->setValue(host_port);
59
60 m_host_force_port_box->setValue(host_listen_port);
61 m_host_force_port_box->setEnabled(false);
62
63 m_host_server_browser->setChecked(use_index);
64
65 m_host_server_region->setEnabled(use_index);
66 m_host_server_region->setCurrentIndex(
67 m_host_server_region->findData(QString::fromStdString(index_region)));
68
69 m_host_server_name->setEnabled(use_index);
70 m_host_server_name->setText(QString::fromStdString(index_name));
71
72 m_host_server_password->setEnabled(use_index);
73 m_host_server_password->setText(QString::fromStdString(index_password));
74
75 m_host_chunked_upload_limit_check->setChecked(enable_chunked_upload_limit);
76 m_host_chunked_upload_limit_box->setValue(chunked_upload_limit);
77 m_host_chunked_upload_limit_box->setEnabled(enable_chunked_upload_limit);
78
79 OnConnectionTypeChanged(m_connection_type->currentIndex());
80
81 ConnectWidgets();
82 }
83
84 void NetPlaySetupDialog::CreateMainLayout()
85 {
86 m_main_layout = new QGridLayout;
87 m_button_box = new QDialogButtonBox(QDialogButtonBox::Cancel);
88 m_nickname_edit = new QLineEdit;
89 m_connection_type = new QComboBox;
90 m_reset_traversal_button = new NonDefaultQPushButton(tr("Reset Traversal Settings"));
91 m_tab_widget = new QTabWidget;
92
93 m_nickname_edit->setValidator(
94 new UTF8CodePointCountValidator(NetPlay::MAX_NAME_LENGTH, m_nickname_edit));
95
96 // Connection widget
97 auto* connection_widget = new QWidget;
98 auto* connection_layout = new QGridLayout;
99
100 m_ip_label = new QLabel;
101 m_ip_edit = new QLineEdit;
102 m_connect_port_label = new QLabel(tr("Port:"));
103 m_connect_port_box = new QSpinBox;
104 m_connect_button = new NonDefaultQPushButton(tr("Connect"));
105
106 m_connect_port_box->setMaximum(65535);
107
108 connection_layout->addWidget(m_ip_label, 0, 0);
109 connection_layout->addWidget(m_ip_edit, 0, 1);
110 connection_layout->addWidget(m_connect_port_label, 0, 2);
111 connection_layout->addWidget(m_connect_port_box, 0, 3);
112 auto* const alert_label = new QLabel(
113 tr("ALERT:\n\n"
114 "All players must use the same Dolphin version.\n"
115 "If enabled, SD cards must be identical between players.\n"
116 "If DSP LLE is used, DSP ROMs must be identical between players.\n"
117 "If a game is hanging on boot, it may not support Dual Core Netplay."
118 " Disable Dual Core.\n"
119 "If connecting directly, the host must have the chosen UDP port open/forwarded!\n"
120 "\n"
121 "Wii Remote support in netplay is experimental and may not work correctly.\n"
122 "Use at your own risk.\n"));
123
124 // Prevent the label from stretching vertically so the spacer gets all the extra space
125 alert_label->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
126
127 connection_layout->addWidget(alert_label, 1, 0, 1, -1);
128 connection_layout->addItem(new QSpacerItem(1, 1), 2, 0, -1, -1);
129 connection_layout->addWidget(m_connect_button, 3, 3, Qt::AlignRight);
130
131 connection_widget->setLayout(connection_layout);
132
133 // Host widget
134 auto* host_widget = new QWidget;
135 auto* host_layout = new QGridLayout;
136 m_host_port_label = new QLabel(tr("Port:"));
137 m_host_port_box = new QSpinBox;
138 m_host_force_port_check = new QCheckBox(tr("Force Listen Port:"));
139 m_host_force_port_box = new QSpinBox;
140 m_host_chunked_upload_limit_check = new QCheckBox(tr("Limit Chunked Upload Speed:"));
141 m_host_chunked_upload_limit_box = new QSpinBox;
142 m_host_server_browser = new QCheckBox(tr("Show in server browser"));
143 m_host_server_name = new QLineEdit;
144 m_host_server_password = new QLineEdit;
145 m_host_server_region = new QComboBox;
146
147 #ifdef USE_UPNP
148 m_host_upnp = new QCheckBox(tr("Forward port (UPnP)"));
149 #endif
150 m_host_games = new QListWidget;
151 m_host_button = new NonDefaultQPushButton(tr("Host"));
152
153 m_host_port_box->setMaximum(65535);
154 m_host_force_port_box->setMaximum(65535);
155 m_host_chunked_upload_limit_box->setRange(1, 1000000);
156 m_host_chunked_upload_limit_box->setSingleStep(100);
157 m_host_chunked_upload_limit_box->setSuffix(QStringLiteral(" kbps"));
158
159 m_host_chunked_upload_limit_check->setToolTip(tr(
160 "This will limit the speed of chunked uploading per client, which is used for save sync."));
161
162 m_host_server_name->setToolTip(tr("Name of your session shown in the server browser"));
163 m_host_server_name->setPlaceholderText(tr("Name"));
164 m_host_server_password->setToolTip(tr("Password for joining your game (leave empty for none)"));
165 m_host_server_password->setPlaceholderText(tr("Password"));
166
167 for (const auto& region : NetPlayIndex::GetRegions())
168 {
169 m_host_server_region->addItem(
170 tr("%1 (%2)").arg(tr(region.second.c_str())).arg(QString::fromStdString(region.first)),
171 QString::fromStdString(region.first));
172 }
173
174 host_layout->addWidget(m_host_port_label, 0, 0);
175 host_layout->addWidget(m_host_port_box, 0, 1);
176 #ifdef USE_UPNP
177 host_layout->addWidget(m_host_upnp, 0, 2);
178 #endif
179 host_layout->addWidget(m_host_server_browser, 1, 0);
180 host_layout->addWidget(m_host_server_region, 1, 1);
181 host_layout->addWidget(m_host_server_name, 1, 2);
182 host_layout->addWidget(m_host_server_password, 1, 3);
183 host_layout->addWidget(m_host_games, 2, 0, 1, -1);
184 host_layout->addWidget(m_host_force_port_check, 3, 0);
185 host_layout->addWidget(m_host_force_port_box, 3, 1, Qt::AlignLeft);
186 host_layout->addWidget(m_host_chunked_upload_limit_check, 4, 0);
187 host_layout->addWidget(m_host_chunked_upload_limit_box, 4, 1, Qt::AlignLeft);
188 host_layout->addWidget(m_host_button, 4, 3, 2, 1, Qt::AlignRight);
189
190 host_widget->setLayout(host_layout);
191
192 m_connection_type->addItem(tr("Direct Connection"));
193 m_connection_type->addItem(tr("Traversal Server"));
194
195 m_main_layout->addWidget(new QLabel(tr("Connection Type:")), 0, 0);
196 m_main_layout->addWidget(m_connection_type, 0, 1);
197 m_main_layout->addWidget(m_reset_traversal_button, 0, 2);
198 m_main_layout->addWidget(new QLabel(tr("Nickname:")), 1, 0);
199 m_main_layout->addWidget(m_nickname_edit, 1, 1);
200 m_main_layout->addWidget(m_tab_widget, 2, 0, 1, -1);
201 m_main_layout->addWidget(m_button_box, 3, 0, 1, -1);
202
203 // Tabs
204 m_tab_widget->addTab(connection_widget, tr("Connect"));
205 m_tab_widget->addTab(host_widget, tr("Host"));
206
207 setLayout(m_main_layout);
208 }
209
210 void NetPlaySetupDialog::ConnectWidgets()
211 {
212 connect(m_connection_type, &QComboBox::currentIndexChanged, this,
213 &NetPlaySetupDialog::OnConnectionTypeChanged);
214 connect(m_nickname_edit, &QLineEdit::textChanged, this, &NetPlaySetupDialog::SaveSettings);
215
216 // Connect widget
217 connect(m_ip_edit, &QLineEdit::textChanged, this, &NetPlaySetupDialog::SaveSettings);
218 connect(m_connect_port_box, &QSpinBox::valueChanged, this, &NetPlaySetupDialog::SaveSettings);
219 // Host widget
220 connect(m_host_port_box, &QSpinBox::valueChanged, this, &NetPlaySetupDialog::SaveSettings);
221 connect(m_host_games, &QListWidget::currentRowChanged, [this](int index) {
222 Settings::GetQSettings().setValue(QStringLiteral("netplay/hostgame"),
223 m_host_games->item(index)->text());
224 });
225
226 connect(m_host_games, &QListWidget::itemDoubleClicked, this, &NetPlaySetupDialog::accept);
227
228 connect(m_host_force_port_check, &QCheckBox::toggled,
229 [this](bool value) { m_host_force_port_box->setEnabled(value); });
230 connect(m_host_chunked_upload_limit_check, &QCheckBox::toggled, this, [this](bool value) {
231 m_host_chunked_upload_limit_box->setEnabled(value);
232 SaveSettings();
233 });
234 connect(m_host_chunked_upload_limit_box, &QSpinBox::valueChanged, this,
235 &NetPlaySetupDialog::SaveSettings);
236
237 connect(m_host_server_browser, &QCheckBox::toggled, this, &NetPlaySetupDialog::SaveSettings);
238 connect(m_host_server_name, &QLineEdit::textChanged, this, &NetPlaySetupDialog::SaveSettings);
239 connect(m_host_server_password, &QLineEdit::textChanged, this, &NetPlaySetupDialog::SaveSettings);
240 connect(m_host_server_region,
241 static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
242 &NetPlaySetupDialog::SaveSettings);
243
244 #ifdef USE_UPNP
245 #if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
246 connect(m_host_upnp, &QCheckBox::checkStateChanged, this, &NetPlaySetupDialog::SaveSettings);
247 #else
248 connect(m_host_upnp, &QCheckBox::stateChanged, this, &NetPlaySetupDialog::SaveSettings);
249 #endif
250 #endif
251
252 connect(m_connect_button, &QPushButton::clicked, this, &QDialog::accept);
253 connect(m_host_button, &QPushButton::clicked, this, &QDialog::accept);
254 connect(m_button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
255 connect(m_reset_traversal_button, &QPushButton::clicked, this,
256 &NetPlaySetupDialog::ResetTraversalHost);
257 connect(m_host_server_browser, &QCheckBox::toggled, this, [this](bool value) {
258 m_host_server_region->setEnabled(value);
259 m_host_server_name->setEnabled(value);
260 m_host_server_password->setEnabled(value);
261 });
262 }
263
264 void NetPlaySetupDialog::SaveSettings()
265 {
266 Config::ConfigChangeCallbackGuard config_guard;
267
268 Config::SetBaseOrCurrent(Config::NETPLAY_NICKNAME, m_nickname_edit->text().toStdString());
269 Config::SetBaseOrCurrent(m_connection_type->currentIndex() == 0 ? Config::NETPLAY_ADDRESS :
270 Config::NETPLAY_HOST_CODE,
271 m_ip_edit->text().toStdString());
272 Config::SetBaseOrCurrent(Config::NETPLAY_CONNECT_PORT,
273 static_cast<u16>(m_connect_port_box->value()));
274 Config::SetBaseOrCurrent(Config::NETPLAY_HOST_PORT, static_cast<u16>(m_host_port_box->value()));
275 #ifdef USE_UPNP
276 Config::SetBaseOrCurrent(Config::NETPLAY_USE_UPNP, m_host_upnp->isChecked());
277 #endif
278
279 if (m_host_force_port_check->isChecked())
280 Config::SetBaseOrCurrent(Config::NETPLAY_LISTEN_PORT,
281 static_cast<u16>(m_host_force_port_box->value()));
282
283 Config::SetBaseOrCurrent(Config::NETPLAY_ENABLE_CHUNKED_UPLOAD_LIMIT,
284 m_host_chunked_upload_limit_check->isChecked());
285 Config::SetBaseOrCurrent(Config::NETPLAY_CHUNKED_UPLOAD_LIMIT,
286 m_host_chunked_upload_limit_box->value());
287
288 Config::SetBaseOrCurrent(Config::NETPLAY_USE_INDEX, m_host_server_browser->isChecked());
289 Config::SetBaseOrCurrent(Config::NETPLAY_INDEX_REGION,
290 m_host_server_region->currentData().toString().toStdString());
291 Config::SetBaseOrCurrent(Config::NETPLAY_INDEX_NAME, m_host_server_name->text().toStdString());
292 Config::SetBaseOrCurrent(Config::NETPLAY_INDEX_PASSWORD,
293 m_host_server_password->text().toStdString());
294 }
295
296 void NetPlaySetupDialog::OnConnectionTypeChanged(int index)
297 {
298 m_connect_port_box->setHidden(index != 0);
299 m_connect_port_label->setHidden(index != 0);
300
301 m_host_port_label->setHidden(index != 0);
302 m_host_port_box->setHidden(index != 0);
303 #ifdef USE_UPNP
304 m_host_upnp->setHidden(index != 0);
305 #endif
306 m_host_force_port_check->setHidden(index == 0);
307 m_host_force_port_box->setHidden(index == 0);
308
309 m_reset_traversal_button->setHidden(index == 0);
310
311 const std::string address =
312 index == 0 ? Config::Get(Config::NETPLAY_ADDRESS) : Config::Get(Config::NETPLAY_HOST_CODE);
313
314 m_ip_label->setText(index == 0 ? tr("IP Address:") : tr("Host Code:"));
315 m_ip_edit->setText(QString::fromStdString(address));
316
317 Config::SetBaseOrCurrent(Config::NETPLAY_TRAVERSAL_CHOICE,
318 std::string(index == 0 ? "direct" : "traversal"));
319 }
320
321 void NetPlaySetupDialog::show()
322 {
323 PopulateGameList();
324 QDialog::show();
325 }
326
327 void NetPlaySetupDialog::accept()
328 {
329 SaveSettings();
330 if (m_tab_widget->currentIndex() == 0)
331 {
332 emit Join();
333 }
334 else
335 {
336 auto items = m_host_games->selectedItems();
337 if (items.empty())
338 {
339 ModalMessageBox::critical(this, tr("Error"), tr("You must select a game to host!"));
340 return;
341 }
342
343 if (m_host_server_browser->isChecked() && m_host_server_name->text().isEmpty())
344 {
345 ModalMessageBox::critical(this, tr("Error"), tr("You must provide a name for your session!"));
346 return;
347 }
348
349 if (m_host_server_browser->isChecked() &&
350 m_host_server_region->currentData().toString().isEmpty())
351 {
352 ModalMessageBox::critical(this, tr("Error"),
353 tr("You must provide a region for your session!"));
354 return;
355 }
356
357 emit Host(*items[0]->data(Qt::UserRole).value<std::shared_ptr<const UICommon::GameFile>>());
358 }
359 }
360
361 void NetPlaySetupDialog::PopulateGameList()
362 {
363 QSignalBlocker blocker(m_host_games);
364
365 m_host_games->clear();
366 for (int i = 0; i < m_game_list_model.rowCount(QModelIndex()); i++)
367 {
368 std::shared_ptr<const UICommon::GameFile> game = m_game_list_model.GetGameFile(i);
369
370 auto* item =
371 new QListWidgetItem(QString::fromStdString(m_game_list_model.GetNetPlayName(*game)));
372 item->setData(Qt::UserRole, QVariant::fromValue(std::move(game)));
373 m_host_games->addItem(item);
374 }
375
376 m_host_games->sortItems();
377
378 const QString selected_game =
379 Settings::GetQSettings().value(QStringLiteral("netplay/hostgame"), QString{}).toString();
380 const auto find_list = m_host_games->findItems(selected_game, Qt::MatchFlag::MatchExactly);
381
382 if (find_list.count() > 0)
383 m_host_games->setCurrentItem(find_list[0]);
384 }
385
386 void NetPlaySetupDialog::ResetTraversalHost()
387 {
388 Config::SetBaseOrCurrent(Config::NETPLAY_TRAVERSAL_SERVER,
389 Config::NETPLAY_TRAVERSAL_SERVER.GetDefaultValue());
390 Config::SetBaseOrCurrent(Config::NETPLAY_TRAVERSAL_PORT,
391 Config::NETPLAY_TRAVERSAL_PORT.GetDefaultValue());
392
393 ModalMessageBox::information(
394 this, tr("Reset Traversal Server"),
395 tr("Reset Traversal Server to %1:%2")
396 .arg(QString::fromStdString(Config::NETPLAY_TRAVERSAL_SERVER.GetDefaultValue()),
397 QString::number(Config::NETPLAY_TRAVERSAL_PORT.GetDefaultValue())));
398 }