pcsx2/pcsx2-qt/Settings/AchievementLoginDialog.cpp

119 lines
4.0 KiB
C++
Raw Normal View History

/* PCSX2 - PS2 Emulator for PCs
2023-09-17 10:19:51 +00:00
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "PrecompiledHeader.h"
#include "AchievementLoginDialog.h"
#include "QtHost.h"
#include "pcsx2/Achievements.h"
2023-09-17 10:19:51 +00:00
#include "common/Error.h"
#include <QtWidgets/QMessageBox>
AchievementLoginDialog::AchievementLoginDialog(QWidget* parent, Achievements::LoginRequestReason reason)
: QDialog(parent)
2023-09-17 10:19:51 +00:00
, m_reason(reason)
{
m_ui.setupUi(this);
m_ui.loginIcon->setPixmap(QIcon::fromTheme("login-box-line").pixmap(32));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
// Adjust text if needed based on reason.
if (reason == Achievements::LoginRequestReason::TokenInvalid)
{
m_ui.instructionText->setText(
tr("<strong>Your RetroAchievements login token is no longer valid.</strong> You must re-enter your "
"credentials for achievements to be tracked. Your password will not be saved in PCSX2, an access token "
"will be generated and used instead."));
}
m_login = m_ui.buttonBox->addButton(tr("&Login"), QDialogButtonBox::AcceptRole);
m_login->setEnabled(false);
connectUi();
}
AchievementLoginDialog::~AchievementLoginDialog() = default;
void AchievementLoginDialog::loginClicked()
{
std::string username(m_ui.userName->text().toStdString());
std::string password(m_ui.password->text().toStdString());
// TODO: Make cancellable.
m_ui.status->setText(tr("Logging in..."));
enableUI(false);
Host::RunOnCPUThread([this, username = std::move(username), password = std::move(password)]() {
2023-09-17 10:19:51 +00:00
Error error;
const bool result = Achievements::Login(username.c_str(), password.c_str(), &error);
const QString message = QString::fromStdString(error.GetDescription());
QMetaObject::invokeMethod(this, "processLoginResult", Qt::QueuedConnection, Q_ARG(bool, result), Q_ARG(const QString&, message));
});
}
void AchievementLoginDialog::cancelClicked()
{
2023-09-17 10:19:51 +00:00
// Disable hardcore mode if we cancelled reauthentication.
if (m_reason == Achievements::LoginRequestReason::TokenInvalid && QtHost::IsVMValid())
{
Host::RunOnCPUThread([]() {
if (VMManager::HasValidVM() && !Achievements::HasActiveGame())
Achievements::DisableHardcoreMode();
});
}
done(1);
}
2023-09-17 10:19:51 +00:00
void AchievementLoginDialog::processLoginResult(bool result, const QString& message)
{
if (!result)
{
2023-09-17 10:19:51 +00:00
QMessageBox::critical(
this, tr("Login Error"),
tr("Login failed.\nError: %1\n\nPlease check your username and password, and try again.").arg(message));
m_ui.status->setText(tr("Login failed."));
enableUI(true);
return;
}
done(0);
}
void AchievementLoginDialog::connectUi()
{
connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &AchievementLoginDialog::loginClicked);
connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &AchievementLoginDialog::cancelClicked);
auto enableLoginButton = [this](const QString&) { m_login->setEnabled(canEnableLoginButton()); };
connect(m_ui.userName, &QLineEdit::textChanged, enableLoginButton);
connect(m_ui.password, &QLineEdit::textChanged, enableLoginButton);
}
void AchievementLoginDialog::enableUI(bool enabled)
{
m_ui.userName->setEnabled(enabled);
m_ui.password->setEnabled(enabled);
m_ui.buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(enabled);
m_login->setEnabled(enabled && canEnableLoginButton());
}
bool AchievementLoginDialog::canEnableLoginButton() const
{
return !m_ui.userName->text().isEmpty() && !m_ui.password->text().isEmpty();
}