1
0
mirror of https://git.suyu.dev/suyu/suyu synced 2025-03-21 14:45:49 -05:00
suyu-mirror/src/suyu/configuration/configure_web.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

168 lines
6.4 KiB
C++
Raw Normal View History

2024-03-08 09:06:48 +00:00
// SPDX-FileCopyrightText: 2017 Citra Emulator Project & 2024 suyu Emulator Project
chore: make yuzu REUSE compliant [REUSE] is a specification that aims at making file copyright information consistent, so that it can be both human and machine readable. It basically requires that all files have a header containing copyright and licensing information. When this isn't possible, like when dealing with binary assets, generated files or embedded third-party dependencies, it is permitted to insert copyright information in the `.reuse/dep5` file. Oh, and it also requires that all the licenses used in the project are present in the `LICENSES` folder, that's why the diff is so huge. This can be done automatically with `reuse download --all`. The `reuse` tool also contains a handy subcommand that analyzes the project and tells whether or not the project is (still) compliant, `reuse lint`. Following REUSE has a few advantages over the current approach: - Copyright information is easy to access for users / downstream - Files like `dist/license.md` do not need to exist anymore, as `.reuse/dep5` is used instead - `reuse lint` makes it easy to ensure that copyright information of files like binary assets / images is always accurate and up to date To add copyright information of files that didn't have it I looked up who committed what and when, for each file. As yuzu contributors do not have to sign a CLA or similar I couldn't assume that copyright ownership was of the "yuzu Emulator Project", so I used the name and/or email of the commit author instead. [REUSE]: https://reuse.software Follow-up to 01cf05bc75b1e47beb08937439f3ed9339e7b254
2022-05-15 02:06:02 +02:00
// SPDX-License-Identifier: GPL-2.0-or-later
2018-09-16 20:05:51 +02:00
#include <QIcon>
#include <QMessageBox>
#include <QtConcurrent/QtConcurrentRun>
#include "common/settings.h"
2024-03-08 09:06:48 +00:00
#include "suyu/configuration/configure_web.h"
#include "suyu/uisettings.h"
2024-03-08 20:38:28 +01:00
#include "ui_configure_web.h"
2024-03-15 21:47:58 -03:00
#include "web_service/verify_login.h"
2018-09-16 20:05:51 +02:00
static constexpr char token_delimiter{':'};
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);
}
2018-09-16 20:05:51 +02:00
ConfigureWeb::ConfigureWeb(QWidget* parent)
: QWidget(parent), ui(std::make_unique<Ui::ConfigureWeb>()) {
ui->setupUi(this);
connect(ui->button_verify_login, &QPushButton::clicked, this, &ConfigureWeb::VerifyLogin);
connect(&verify_watcher, &QFutureWatcher<bool>::finished, this, &ConfigureWeb::OnLoginVerified);
#ifndef USE_DISCORD_PRESENCE
ui->discord_group->setVisible(false);
#endif
SetConfiguration();
RetranslateUI();
2018-09-16 20:05:51 +02:00
}
2018-09-17 17:16:01 +02:00
ConfigureWeb::~ConfigureWeb() = default;
2018-09-16 20:05:51 +02:00
void ConfigureWeb::changeEvent(QEvent* event) {
if (event->type() == QEvent::LanguageChange) {
RetranslateUI();
}
QWidget::changeEvent(event);
}
void ConfigureWeb::RetranslateUI() {
ui->retranslateUi(this);
2018-09-16 20:05:51 +02:00
ui->web_signup_link->setText(
2024-03-19 20:21:09 +01:00
tr("<a href='https://suyu.dev/signup'><span style=\"text-decoration: underline; "
2018-09-16 20:05:51 +02:00
"color:#039be5;\">Sign up</span></a>"));
2018-09-16 20:05:51 +02:00
ui->web_token_info_link->setText(
2024-03-19 20:21:09 +01:00
tr("<a href='https://suyu.dev/account'><span style=\"text-decoration: "
2018-09-16 20:05:51 +02:00
"underline; color:#039be5;\">What is my token?</span></a>"));
}
void ConfigureWeb::SetConfiguration() {
ui->web_credentials_disclaimer->setWordWrap(true);
ui->web_signup_link->setOpenExternalLinks(true);
ui->web_token_info_link->setOpenExternalLinks(true);
2024-03-08 09:06:48 +00:00
if (Settings::values.suyu_username.GetValue().empty()) {
ui->username->setText(tr("Unspecified"));
} else {
2024-03-08 09:06:48 +00:00
ui->username->setText(QString::fromStdString(Settings::values.suyu_username.GetValue()));
}
ui->edit_token->setText(QString::fromStdString(GenerateDisplayToken(
2024-03-08 09:06:48 +00:00
Settings::values.suyu_username.GetValue(), Settings::values.suyu_token.GetValue())));
2018-09-16 20:05:51 +02:00
// Connect after setting the values, to avoid calling OnLoginChanged now
connect(ui->edit_token, &QLineEdit::textChanged, this, &ConfigureWeb::OnLoginChanged);
2018-09-16 20:05:51 +02:00
user_verified = true;
ui->toggle_discordrpc->setChecked(UISettings::values.enable_discord_presence.GetValue());
2018-09-16 20:05:51 +02:00
}
void ConfigureWeb::ApplyConfiguration() {
2018-09-16 20:05:51 +02:00
UISettings::values.enable_discord_presence = ui->toggle_discordrpc->isChecked();
if (user_verified) {
2024-03-08 09:06:48 +00:00
Settings::values.suyu_username =
UsernameFromDisplayToken(ui->edit_token->text().toStdString());
2024-03-08 09:06:48 +00:00
Settings::values.suyu_token = TokenFromDisplayToken(ui->edit_token->text().toStdString());
2018-09-16 20:05:51 +02:00
} else {
QMessageBox::warning(
this, tr("Token not verified"),
tr("Token was not verified. The change to your token has not been saved."));
2018-09-16 20:05:51 +02:00
}
}
void ConfigureWeb::OnLoginChanged() {
if (ui->edit_token->text().isEmpty()) {
2018-09-16 20:05:51 +02:00
user_verified = true;
// Empty = no icon
ui->label_token_verified->setPixmap(QPixmap());
ui->label_token_verified->setToolTip(QString());
2018-09-16 20:05:51 +02:00
} 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"));
2018-09-16 20:05:51 +02:00
}
}
void ConfigureWeb::VerifyLogin() {
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())] {
2024-03-13 15:11:09 -03:00
#ifdef ENABLE_WEB_SERVICE
return WebService::VerifyLogin(Settings::values.web_api_url.GetValue(), username,
token);
#else
return false;
#endif
}));
2018-09-16 20:05:51 +02:00
}
void ConfigureWeb::OnLoginVerified() {
ui->button_verify_login->setEnabled(true);
ui->button_verify_login->setText(tr("Verify"));
if (verify_watcher.result()) {
user_verified = true;
ui->label_token_verified->setPixmap(QIcon::fromTheme(QStringLiteral("checked")).pixmap(16));
ui->label_token_verified->setToolTip(tr("Verified", "Tooltip"));
ui->username->setText(
QString::fromStdString(UsernameFromDisplayToken(ui->edit_token->text().toStdString())));
2018-09-16 20:05:51 +02:00
} 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."));
2018-09-16 20:05:51 +02:00
}
}
void ConfigureWeb::SetWebServiceConfigEnabled(bool enabled) {
ui->label_disable_info->setVisible(!enabled);
ui->groupBoxWebConfig->setEnabled(enabled);
}