configure_service.cpp 4.8 KB

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