configure_network.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <QGraphicsItem>
  5. #include <QtConcurrent/QtConcurrent>
  6. #include "common/settings.h"
  7. #include "core/core.h"
  8. #include "core/hle/service/bcat/backend/boxcat.h"
  9. #include "core/network/network_interface.h"
  10. #include "ui_configure_network.h"
  11. #include "yuzu/configuration/configure_network.h"
  12. #ifdef YUZU_ENABLE_BOXCAT
  13. namespace {
  14. QString FormatEventStatusString(const Service::BCAT::EventStatus& status) {
  15. QString out;
  16. if (status.header.has_value()) {
  17. out += QStringLiteral("<i>%1</i><br>").arg(QString::fromStdString(*status.header));
  18. }
  19. if (status.events.size() == 1) {
  20. out += QStringLiteral("%1<br>").arg(QString::fromStdString(status.events.front()));
  21. } else {
  22. for (const auto& event : status.events) {
  23. out += QStringLiteral("- %1<br>").arg(QString::fromStdString(event));
  24. }
  25. }
  26. if (status.footer.has_value()) {
  27. out += QStringLiteral("<i>%1</i><br>").arg(QString::fromStdString(*status.footer));
  28. }
  29. return out;
  30. }
  31. } // Anonymous namespace
  32. #endif
  33. ConfigureNetwork::ConfigureNetwork(QWidget* parent)
  34. : QWidget(parent), ui(std::make_unique<Ui::ConfigureNetwork>()) {
  35. ui->setupUi(this);
  36. ui->bcat_source->addItem(QStringLiteral("None"));
  37. ui->bcat_empty_label->setHidden(true);
  38. ui->bcat_empty_header->setHidden(true);
  39. #ifdef YUZU_ENABLE_BOXCAT
  40. ui->bcat_source->addItem(QStringLiteral("Boxcat"), QStringLiteral("boxcat"));
  41. #endif
  42. ui->network_interface->addItem(tr("None"));
  43. for (const auto& iface : Network::GetAvailableNetworkInterfaces()) {
  44. ui->network_interface->addItem(QString::fromStdString(iface.name));
  45. }
  46. connect(ui->bcat_source, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  47. &ConfigureNetwork::OnBCATImplChanged);
  48. this->SetConfiguration();
  49. }
  50. ConfigureNetwork::~ConfigureNetwork() = default;
  51. void ConfigureNetwork::ApplyConfiguration() {
  52. Settings::values.bcat_backend = ui->bcat_source->currentText().toLower().toStdString();
  53. Settings::values.network_interface = ui->network_interface->currentText().toStdString();
  54. }
  55. void ConfigureNetwork::RetranslateUi() {
  56. ui->retranslateUi(this);
  57. }
  58. void ConfigureNetwork::SetConfiguration() {
  59. const bool runtime_lock = !Core::System::GetInstance().IsPoweredOn();
  60. const int index =
  61. ui->bcat_source->findData(QString::fromStdString(Settings::values.bcat_backend.GetValue()));
  62. ui->bcat_source->setCurrentIndex(index == -1 ? 0 : index);
  63. const std::string& network_interface = Settings::values.network_interface.GetValue();
  64. ui->network_interface->setCurrentText(QString::fromStdString(network_interface));
  65. ui->network_interface->setEnabled(runtime_lock);
  66. }
  67. std::pair<QString, QString> ConfigureNetwork::BCATDownloadEvents() {
  68. #ifdef YUZU_ENABLE_BOXCAT
  69. std::optional<std::string> global;
  70. std::map<std::string, Service::BCAT::EventStatus> map;
  71. const auto res = Service::BCAT::Boxcat::GetStatus(global, map);
  72. switch (res) {
  73. case Service::BCAT::Boxcat::StatusResult::Success:
  74. break;
  75. case Service::BCAT::Boxcat::StatusResult::Offline:
  76. return {QString{},
  77. tr("The boxcat service is offline or you are not connected to the internet.")};
  78. case Service::BCAT::Boxcat::StatusResult::ParseError:
  79. return {QString{},
  80. tr("There was an error while processing the boxcat event data. Contact the yuzu "
  81. "developers.")};
  82. case Service::BCAT::Boxcat::StatusResult::BadClientVersion:
  83. return {QString{},
  84. tr("The version of yuzu you are using is either too new or too old for the server. "
  85. "Try updating to the latest official release of yuzu.")};
  86. }
  87. if (map.empty()) {
  88. return {QStringLiteral("Current Boxcat Events"),
  89. tr("There are currently no events on boxcat.")};
  90. }
  91. QString out;
  92. if (global.has_value()) {
  93. out += QStringLiteral("%1<br>").arg(QString::fromStdString(*global));
  94. }
  95. for (const auto& [key, value] : map) {
  96. out += QStringLiteral("%1<b>%2</b><br>%3")
  97. .arg(out.isEmpty() ? QString{} : QStringLiteral("<br>"))
  98. .arg(QString::fromStdString(key))
  99. .arg(FormatEventStatusString(value));
  100. }
  101. return {tr("Current Boxcat Events"), std::move(out)};
  102. #else
  103. return {tr("Current Boxcat Events"), tr("There are currently no events on boxcat.")};
  104. #endif
  105. }
  106. void ConfigureNetwork::OnBCATImplChanged() {
  107. #ifdef YUZU_ENABLE_BOXCAT
  108. const auto boxcat = ui->bcat_source->currentText() == QStringLiteral("Boxcat");
  109. ui->bcat_empty_header->setHidden(!boxcat);
  110. ui->bcat_empty_label->setHidden(!boxcat);
  111. ui->bcat_empty_header->setText(QString{});
  112. ui->bcat_empty_label->setText(tr("Yuzu is retrieving the latest boxcat status..."));
  113. if (!boxcat)
  114. return;
  115. const auto future = QtConcurrent::run([this] { return BCATDownloadEvents(); });
  116. watcher.setFuture(future);
  117. connect(&watcher, &QFutureWatcher<std::pair<QString, QString>>::finished, this,
  118. [this] { OnUpdateBCATEmptyLabel(watcher.result()); });
  119. #endif
  120. }
  121. void ConfigureNetwork::OnUpdateBCATEmptyLabel(std::pair<QString, QString> string) {
  122. #ifdef YUZU_ENABLE_BOXCAT
  123. const auto boxcat = ui->bcat_source->currentText() == QStringLiteral("Boxcat");
  124. if (boxcat) {
  125. ui->bcat_empty_header->setText(string.first);
  126. ui->bcat_empty_label->setText(string.second);
  127. }
  128. #endif
  129. }