From 67ca7ae54d0ebb0d5b2427ef4bf6ae03824798d2 Mon Sep 17 00:00:00 2001 From: spectranator Date: Fri, 26 Jul 2024 17:35:38 +0200 Subject: [PATCH] Refactor --- src/yuzu/configuration/configure_web.cpp | 146 ++++------------------- src/yuzu/configuration/configure_web.h | 7 +- src/yuzu/configuration/configure_web.ui | 21 ++-- 3 files changed, 36 insertions(+), 138 deletions(-) diff --git a/src/yuzu/configuration/configure_web.cpp b/src/yuzu/configuration/configure_web.cpp index 34e9df6b7..d84addd82 100644 --- a/src/yuzu/configuration/configure_web.cpp +++ b/src/yuzu/configuration/configure_web.cpp @@ -5,46 +5,15 @@ #include #include #include "common/settings.h" +#include "common/uuid.h" #include "ui_configure_web.h" #include "yuzu/configuration/configure_web.h" #include "yuzu/uisettings.h" -static constexpr char token_delimiter{':'}; - -static std::string GetGeneratedTokenCode() -{ - using std::chrono::system_clock; - return std::string("token-") + std::string(std::to_string(std::chrono::system_clock::now().time_since_epoch().count())); -} - -static std::string GenerateDisplayToken(const std::string& username, const std::string& token) { - if (username.empty() || token.empty()) { - return {}; - } - - const std::string unencoded_display_token{username + token_delimiter + token}; - QByteArray b{unencoded_display_token.c_str()}; - QByteArray b64 = b.toBase64(); - return b64.toStdString(); -} - -static std::string UsernameFromDisplayToken(const std::string& display_token) { - const std::string unencoded_display_token{ - QByteArray::fromBase64(display_token.c_str()).toStdString()}; - return unencoded_display_token.substr(0, unencoded_display_token.find(token_delimiter)); -} - -static std::string TokenFromDisplayToken(const std::string& display_token) { - const std::string unencoded_display_token{ - QByteArray::fromBase64(display_token.c_str()).toStdString()}; - return unencoded_display_token.substr(unencoded_display_token.find(token_delimiter) + 1); -} - ConfigureWeb::ConfigureWeb(QWidget* parent) : QWidget(parent), ui(std::make_unique()) { ui->setupUi(this); - connect(ui->button_verify_login, &QPushButton::clicked, this, &ConfigureWeb::VerifyLogin); - connect(&verify_watcher, &QFutureWatcher::finished, this, &ConfigureWeb::OnLoginVerified); + connect(ui->button_reset_token, &QPushButton::clicked, this, &ConfigureWeb::ResetToken); #ifndef USE_DISCORD_PRESENCE ui->discord_group->setVisible(false); @@ -66,126 +35,53 @@ void ConfigureWeb::changeEvent(QEvent* event) { void ConfigureWeb::RetranslateUI() { ui->retranslateUi(this); - - ui->web_signup_link->setText( - tr("Sign up")); - - ui->web_token_info_link->setText( - tr("What is my token?")); } void ConfigureWeb::SetConfiguration() { ui->web_credentials_disclaimer->setWordWrap(true); - ui->web_signup_link->setOpenExternalLinks(true); - ui->web_token_info_link->setOpenExternalLinks(true); - if (Settings::values.yuzu_username.GetValue().empty()) { ui->username->setText(tr("Unspecified")); } else { ui->username->setText(QString::fromStdString(Settings::values.yuzu_username.GetValue())); } - ui->edit_token->setText(QString::fromStdString(GenerateDisplayToken( - Settings::values.yuzu_username.GetValue(), Settings::values.yuzu_token.GetValue()))); - - // Connect after setting the values, to avoid calling OnLoginChanged now - connect(ui->edit_token, &QLineEdit::textChanged, this, &ConfigureWeb::OnLoginChanged); - - user_verified = true; + ui->edit_token->setText(QString::fromStdString(Settings::values.yuzu_token.GetValue())); ui->toggle_discordrpc->setChecked(UISettings::values.enable_discord_presence.GetValue()); - - ui->web_signup_link->setVisible(false); - ui->web_token_info_link->setVisible(false); } void ConfigureWeb::ApplyConfiguration() { UISettings::values.enable_discord_presence = ui->toggle_discordrpc->isChecked(); - if (user_verified) { - if (Settings::values.yuzu_username.GetValue().empty()) { - // backup: default name should already be set by ConfigureProfileManager::UpdateCurrentUser() - Settings::values.yuzu_username = "torzu"; - } else { - // if a name already exist, reassign it to itself (needed for change set with a profile switch) - Settings::values.yuzu_username = Settings::values.yuzu_username.GetValue(); - } - - if (Settings::values.yuzu_token.GetValue().empty()) { - // if empty, automatically generate a new one - Settings::values.yuzu_token = GetGeneratedTokenCode(); - } else { - // if already exists, use that value - Settings::values.yuzu_token = TokenFromDisplayToken(ui->edit_token->text().toStdString()); - } + if (Settings::values.yuzu_username.GetValue().empty()) { + // Backup: default name should already be set by ConfigureProfileManager::UpdateCurrentUser() + Settings::values.yuzu_username = "torzu"; } else { - QMessageBox::warning( - this, tr("Token not verified"), - tr("Token was not verified. The change to your token has not been saved.")); + // If a name already exist, reassign it to itself (needed for change set with a profile switch) + Settings::values.yuzu_username = Settings::values.yuzu_username.GetValue(); } -} -void ConfigureWeb::OnLoginChanged() { if (ui->edit_token->text().isEmpty()) { - user_verified = true; - // Empty = no icon - ui->label_token_verified->setPixmap(QPixmap()); - ui->label_token_verified->setToolTip(QString()); + // If no token specified, automatically generate one + Settings::values.yuzu_token = Common::UUID::MakeRandom().FormattedString(); } else { - user_verified = false; - - // Show an info icon if it's been changed, clearer than showing failure - const QPixmap pixmap = QIcon::fromTheme(QStringLiteral("info")).pixmap(16); - ui->label_token_verified->setPixmap(pixmap); - ui->label_token_verified->setToolTip( - tr("Unverified, please click Verify before saving configuration", "Tooltip")); + // Otherwise use user-specified value + Settings::values.yuzu_token = ui->edit_token->text().toStdString(); } } -// repurposed for resetting token -void ConfigureWeb::VerifyLogin() { - // ORIGINAL CODE FOR THIS FUNCTION - //ui->button_verify_login->setDisabled(true); - //ui->button_verify_login->setText(tr("Verifying...")); - //ui->label_token_verified->setPixmap(QIcon::fromTheme(QStringLiteral("sync")).pixmap(16)); - //ui->label_token_verified->setToolTip(tr("Verifying...")); - //verify_watcher.setFuture(QtConcurrent::run( - // [username = UsernameFromDisplayToken(ui->edit_token->text().toStdString()), - // token = TokenFromDisplayToken(ui->edit_token->text().toStdString())] { - // return Core::VerifyLogin(username, token); - // })); - // END ORIGINAL CODE - - // set a new token - Settings::values.yuzu_token = GetGeneratedTokenCode(); - // just to display the label_token_verified pic and tooltip for visual confirmation - OnLoginVerified(); - // apply the changes +void ConfigureWeb::ResetToken() { + // Generate and set token + const auto token = Common::UUID::MakeRandom().FormattedString(); + Settings::values.yuzu_token = token; + // Just to display the label_token_icon pic and tooltip for visual confirmation + ui->label_token_icon->setPixmap(QIcon::fromTheme(QStringLiteral("checked")).pixmap(16)); + ui->label_token_icon->setToolTip(tr("Token Changed", "Tooltip")); + ui->username->setText(QString::fromStdString(token)); + // Apply the changes SetConfiguration(); } -void ConfigureWeb::OnLoginVerified() { - //ui->button_verify_login->setEnabled(true); - //ui->button_verify_login->setText(tr("Verify")); - if (true) { //(verify_watcher.result()) { - user_verified = true; - - ui->label_token_verified->setPixmap(QIcon::fromTheme(QStringLiteral("checked")).pixmap(16)); - ui->label_token_verified->setToolTip(tr("Token Changed", "Tooltip")); - ui->username->setText( - QString::fromStdString(UsernameFromDisplayToken(ui->edit_token->text().toStdString()))); - } else { - ui->label_token_verified->setPixmap(QIcon::fromTheme(QStringLiteral("failed")).pixmap(16)); - ui->label_token_verified->setToolTip(tr("Verification failed", "Tooltip")); - ui->username->setText(tr("Unspecified")); - QMessageBox::critical(this, tr("Verification failed"), - tr("Verification failed. Check that you have entered your token " - "correctly, and that your internet connection is working.")); - } -} - void ConfigureWeb::SetWebServiceConfigEnabled(bool enabled) { ui->label_disable_info->setVisible(!enabled); ui->groupBoxWebConfig->setEnabled(enabled); diff --git a/src/yuzu/configuration/configure_web.h b/src/yuzu/configuration/configure_web.h index 92625dd44..1535535a1 100644 --- a/src/yuzu/configuration/configure_web.h +++ b/src/yuzu/configuration/configure_web.h @@ -25,14 +25,9 @@ private: void changeEvent(QEvent* event) override; void RetranslateUI(); - void OnLoginChanged(); - void VerifyLogin(); - void OnLoginVerified(); + void ResetToken(); void SetConfiguration(); - bool user_verified = true; - QFutureWatcher verify_watcher; - std::unique_ptr ui; }; diff --git a/src/yuzu/configuration/configure_web.ui b/src/yuzu/configuration/configure_web.ui index e574cd0e0..75c5dda11 100644 --- a/src/yuzu/configuration/configure_web.ui +++ b/src/yuzu/configuration/configure_web.ui @@ -6,7 +6,7 @@ 0 0 - 926 + 2280 561 @@ -28,14 +28,14 @@ - Tokens for hosting public rooms in a lobby are automatically generated the first time you save Configuration settings. To change the token username, switch to a different profile under the System >> Profiles tab. If you have already opened the multiplayer lobby, exit and restart the emulator for the name change to apply there. To generate a new token for any reason, use the "Reset Token" button. + This token is for hosting public rooms in a lobby and are automatically generated the first time you save settings. To change your username, rename or switch the profile. If you are currently connected to multiplayer, exit and restart the emulator for changes to apply. - + 0 @@ -52,6 +52,9 @@ + + false + Sign up @@ -68,7 +71,11 @@ - + + + true + + @@ -82,13 +89,13 @@ 80 - - QLineEdit::Password - + + false + What is my token?