configure_service.cpp 4.8 KB

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