configure_service.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. std::optional<std::string> global;
  56. std::map<std::string, Service::BCAT::EventStatus> map;
  57. const auto res = Service::BCAT::Boxcat::GetStatus(global, map);
  58. switch (res) {
  59. case Service::BCAT::Boxcat::StatusResult::Success:
  60. break;
  61. case Service::BCAT::Boxcat::StatusResult::Offline:
  62. return {QString{},
  63. tr("The boxcat service is offline or you are not connected to the internet.")};
  64. case Service::BCAT::Boxcat::StatusResult::ParseError:
  65. return {QString{},
  66. tr("There was an error while processing the boxcat event data. Contact the yuzu "
  67. "developers.")};
  68. case Service::BCAT::Boxcat::StatusResult::BadClientVersion:
  69. return {QString{},
  70. tr("The version of yuzu you are using is either too new or too old for the server. "
  71. "Try updating to the latest official release of yuzu.")};
  72. }
  73. if (map.empty()) {
  74. return {QStringLiteral("Current Boxcat Events"),
  75. tr("There are currently no events on boxcat.")};
  76. }
  77. QString out;
  78. if (global.has_value()) {
  79. out += QStringLiteral("%1<br>").arg(QString::fromStdString(*global));
  80. }
  81. for (const auto& [key, value] : map) {
  82. out += QStringLiteral("%1<b>%2</b><br>%3")
  83. .arg(out.isEmpty() ? QString{} : QStringLiteral("<br>"))
  84. .arg(QString::fromStdString(key))
  85. .arg(FormatEventStatusString(value));
  86. }
  87. return {QStringLiteral("Current Boxcat Events"), std::move(out)};
  88. }
  89. void ConfigureService::OnBCATImplChanged() {
  90. #ifdef YUZU_ENABLE_BOXCAT
  91. const auto boxcat = ui->bcat_source->currentText() == QStringLiteral("Boxcat");
  92. ui->bcat_empty_header->setHidden(!boxcat);
  93. ui->bcat_empty_label->setHidden(!boxcat);
  94. ui->bcat_empty_header->setText(QString{});
  95. ui->bcat_empty_label->setText(tr("Yuzu is retrieving the latest boxcat status..."));
  96. if (!boxcat)
  97. return;
  98. const auto future = QtConcurrent::run([this] { return BCATDownloadEvents(); });
  99. watcher.setFuture(future);
  100. connect(&watcher, &QFutureWatcher<std::pair<QString, QString>>::finished, this,
  101. [this] { OnUpdateBCATEmptyLabel(watcher.result()); });
  102. #endif
  103. }
  104. void ConfigureService::OnUpdateBCATEmptyLabel(std::pair<QString, QString> string) {
  105. #ifdef YUZU_ENABLE_BOXCAT
  106. const auto boxcat = ui->bcat_source->currentText() == QStringLiteral("Boxcat");
  107. if (boxcat) {
  108. ui->bcat_empty_header->setText(string.first);
  109. ui->bcat_empty_label->setText(string.second);
  110. }
  111. #endif
  112. }