| 1 | // Copyright 2021 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Settings/BroadbandAdapterSettingsDialog.h" |
| 5 | |
| 6 | #include <regex> |
| 7 | #include <string> |
| 8 | |
| 9 | #include <QDialogButtonBox> |
| 10 | #include <QLabel> |
| 11 | #include <QLineEdit> |
| 12 | #include <QString> |
| 13 | #include <QVBoxLayout> |
| 14 | |
| 15 | #include "Common/StringUtil.h" |
| 16 | #include "Core/Config/MainSettings.h" |
| 17 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 18 | |
| 19 | BroadbandAdapterSettingsDialog::BroadbandAdapterSettingsDialog(QWidget* parent, Type bba_type) |
| 20 | : QDialog(parent) |
| 21 | { |
| 22 | m_bba_type = bba_type; |
| 23 | InitControls(); |
| 24 | } |
| 25 | |
| 26 | void BroadbandAdapterSettingsDialog::InitControls() |
| 27 | { |
| 28 | QLabel* address_label = nullptr; |
| 29 | QLabel* description = nullptr; |
| 30 | QString address_placeholder; |
| 31 | QString current_address; |
| 32 | QString window_title; |
| 33 | |
| 34 | switch (m_bba_type) |
| 35 | { |
| 36 | case Type::Ethernet: |
| 37 | // i18n: MAC stands for Media Access Control. A MAC address uniquely identifies a network |
| 38 | // interface (physical) like a serial number. "MAC" should be kept in translations. |
| 39 | address_label = new QLabel(tr("Enter new Broadband Adapter MAC address:")); |
| 40 | address_placeholder = QString::fromStdString("aa:bb:cc:dd:ee:ff"); |
| 41 | current_address = QString::fromStdString(Config::Get(Config::MAIN_BBA_MAC)); |
| 42 | description = new QLabel(tr("For setup instructions, <a " |
| 43 | "href=\"https://wiki.dolphin-emu.org/" |
| 44 | "index.php?title=Broadband_Adapter\">refer to this page</a>.")); |
| 45 | |
| 46 | // i18n: MAC stands for Media Access Control. A MAC address uniquely identifies a network |
| 47 | // interface (physical) like a serial number. "MAC" should be kept in translations. |
| 48 | window_title = tr("Broadband Adapter MAC Address"); |
| 49 | break; |
| 50 | |
| 51 | case Type::TapServer: |
| 52 | case Type::ModemTapServer: |
| 53 | { |
| 54 | const bool is_modem = (m_bba_type == Type::ModemTapServer); |
| 55 | current_address = |
| 56 | QString::fromStdString(Config::Get(is_modem ? Config::MAIN_MODEM_TAPSERVER_DESTINATION : |
| 57 | Config::MAIN_BBA_TAPSERVER_DESTINATION)); |
| 58 | #ifdef _WIN32 |
| 59 | address_label = new QLabel(tr("Destination (address:port):")); |
| 60 | address_placeholder = QStringLiteral(""); |
| 61 | description = new QLabel( |
| 62 | tr("Enter the IP address and port of the tapserver instance you want to connect to.")); |
| 63 | #else |
| 64 | address_label = new QLabel(tr("Destination (UNIX socket path or address:port):")); |
| 65 | address_placeholder = |
| 66 | is_modem ? QStringLiteral(u"/tmp/dolphin-modem-tap") : QStringLiteral(u"/tmp/dolphin-tap"); |
| 67 | description = |
| 68 | new QLabel(tr("The default value \"%1\" will work with a local tapserver and newserv." |
| 69 | " You can also enter a network location (address:port) to connect to a " |
| 70 | "remote tapserver.") |
| 71 | .arg(address_placeholder)); |
| 72 | #endif |
| 73 | window_title = tr("BBA destination address"); |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | case Type::BuiltIn: |
| 78 | address_label = new QLabel(tr("Enter the DNS server to use:")); |
| 79 | address_placeholder = QStringLiteral("8.8.8.8"); |
| 80 | current_address = QString::fromStdString(Config::Get(Config::MAIN_BBA_BUILTIN_DNS)); |
| 81 | description = new QLabel(tr("Use 8.8.8.8 for normal DNS, else enter your custom one")); |
| 82 | |
| 83 | window_title = tr("Broadband Adapter DNS setting"); |
| 84 | break; |
| 85 | |
| 86 | case Type::XLinkKai: |
| 87 | address_label = new QLabel(tr("Enter IP address of device running the XLink Kai Client:")); |
| 88 | address_placeholder = QString::fromStdString("127.0.0.1"); |
| 89 | current_address = QString::fromStdString(Config::Get(Config::MAIN_BBA_XLINK_IP)); |
| 90 | description = |
| 91 | new QLabel(tr("For setup instructions, <a " |
| 92 | "href=\"https://www.teamxlink.co.uk/wiki/Dolphin\">refer to this page</a>.")); |
| 93 | window_title = tr("XLink Kai BBA Destination Address"); |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | setWindowTitle(window_title); |
| 98 | |
| 99 | m_address_input = new QLineEdit(current_address); |
| 100 | m_address_input->setPlaceholderText(address_placeholder); |
| 101 | |
| 102 | auto buttonbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 103 | connect(buttonbox, &QDialogButtonBox::accepted, this, |
| 104 | &BroadbandAdapterSettingsDialog::SaveAddress); |
| 105 | connect(buttonbox, &QDialogButtonBox::rejected, this, &BroadbandAdapterSettingsDialog::reject); |
| 106 | |
| 107 | description->setTextFormat(Qt::RichText); |
| 108 | description->setWordWrap(true); |
| 109 | description->setTextInteractionFlags(Qt::TextBrowserInteraction); |
| 110 | description->setOpenExternalLinks(true); |
| 111 | |
| 112 | auto* main_layout = new QVBoxLayout(); |
| 113 | main_layout->addWidget(address_label); |
| 114 | main_layout->addWidget(m_address_input); |
| 115 | main_layout->addWidget(description); |
| 116 | main_layout->addWidget(buttonbox); |
| 117 | |
| 118 | setLayout(main_layout); |
| 119 | } |
| 120 | |
| 121 | void BroadbandAdapterSettingsDialog::SaveAddress() |
| 122 | { |
| 123 | const std::string bba_new_address(StripWhitespace(m_address_input->text().toStdString())); |
| 124 | |
| 125 | switch (m_bba_type) |
| 126 | { |
| 127 | case Type::Ethernet: |
| 128 | { |
| 129 | static const std::regex re_mac_address("([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2})"); |
| 130 | if (!std::regex_match(bba_new_address, re_mac_address)) |
| 131 | { |
| 132 | ModalMessageBox::critical( |
| 133 | this, tr("Broadband Adapter Error"), |
| 134 | // i18n: MAC stands for Media Access Control. A MAC address uniquely identifies a network |
| 135 | // interface (physical) like a serial number. "MAC" should be kept in translations. |
| 136 | tr("The entered MAC address is invalid.")); |
| 137 | return; |
| 138 | } |
| 139 | Config::SetBaseOrCurrent(Config::MAIN_BBA_MAC, bba_new_address); |
| 140 | break; |
| 141 | } |
| 142 | case Type::TapServer: |
| 143 | Config::SetBaseOrCurrent(Config::MAIN_BBA_TAPSERVER_DESTINATION, bba_new_address); |
| 144 | break; |
| 145 | case Type::ModemTapServer: |
| 146 | Config::SetBaseOrCurrent(Config::MAIN_MODEM_TAPSERVER_DESTINATION, bba_new_address); |
| 147 | break; |
| 148 | case Type::BuiltIn: |
| 149 | Config::SetBaseOrCurrent(Config::MAIN_BBA_BUILTIN_DNS, bba_new_address); |
| 150 | break; |
| 151 | case Type::XLinkKai: |
| 152 | Config::SetBaseOrCurrent(Config::MAIN_BBA_XLINK_IP, bba_new_address); |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | accept(); |
| 157 | } |