BroadbandAdapterSettingsDialog.cpp 5.9 KB

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