PrismLauncher/launcher/ui/dialogs/OfflineLoginDialog.cpp

105 lines
2.9 KiB
C++
Raw Normal View History

2022-01-17 11:08:10 +00:00
#include "OfflineLoginDialog.h"
#include "ui_OfflineLoginDialog.h"
#include "minecraft/auth/AccountTask.h"
#include <QtWidgets/QPushButton>
OfflineLoginDialog::OfflineLoginDialog(QWidget* parent) : QDialog(parent), ui(new Ui::OfflineLoginDialog)
2022-01-17 11:08:10 +00:00
{
ui->setupUi(this);
ui->progressBar->setVisible(false);
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}
OfflineLoginDialog::~OfflineLoginDialog()
{
delete ui;
}
// Stage 1: User interaction
void OfflineLoginDialog::accept()
{
setUserInputsEnabled(false);
ui->progressBar->setVisible(true);
// Setup the login task and start it
m_account = MinecraftAccount::createOffline(ui->userTextBox->text());
m_loginTask = m_account->loginOffline();
connect(m_loginTask.get(), &Task::failed, this, &OfflineLoginDialog::onTaskFailed);
connect(m_loginTask.get(), &Task::succeeded, this, &OfflineLoginDialog::onTaskSucceeded);
connect(m_loginTask.get(), &Task::status, this, &OfflineLoginDialog::onTaskStatus);
connect(m_loginTask.get(), &Task::progress, this, &OfflineLoginDialog::onTaskProgress);
m_loginTask->start();
}
void OfflineLoginDialog::setUserInputsEnabled(bool enable)
{
ui->userTextBox->setEnabled(enable);
ui->buttonBox->setEnabled(enable);
}
void OfflineLoginDialog::on_allowLongUsernames_stateChanged(int value)
{
if (value == Qt::Checked) {
ui->userTextBox->setMaxLength(INT_MAX);
} else {
ui->userTextBox->setMaxLength(16);
}
}
2022-01-17 11:08:10 +00:00
// Enable the OK button only when the textbox contains something.
void OfflineLoginDialog::on_userTextBox_textEdited(const QString& newText)
2022-01-17 11:08:10 +00:00
{
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!newText.isEmpty());
2022-01-17 11:08:10 +00:00
}
void OfflineLoginDialog::onTaskFailed(const QString& reason)
2022-01-17 11:08:10 +00:00
{
// Set message
auto lines = reason.split('\n');
QString processed;
for (auto line : lines) {
if (line.size()) {
2022-01-17 11:08:10 +00:00
processed += "<font color='red'>" + line + "</font><br />";
} else {
2022-01-17 11:08:10 +00:00
processed += "<br />";
}
}
ui->label->setText(processed);
// Re-enable user-interaction
setUserInputsEnabled(true);
ui->progressBar->setVisible(false);
}
void OfflineLoginDialog::onTaskSucceeded()
{
QDialog::accept();
}
void OfflineLoginDialog::onTaskStatus(const QString& status)
2022-01-17 11:08:10 +00:00
{
ui->label->setText(status);
}
void OfflineLoginDialog::onTaskProgress(qint64 current, qint64 total)
{
ui->progressBar->setMaximum(total);
ui->progressBar->setValue(current);
}
// Public interface
MinecraftAccountPtr OfflineLoginDialog::newAccount(QWidget* parent, QString msg)
2022-01-17 11:08:10 +00:00
{
OfflineLoginDialog dlg(parent);
dlg.ui->label->setText(msg);
if (dlg.exec() == QDialog::Accepted) {
2022-01-17 11:08:10 +00:00
return dlg.m_account;
}
return nullptr;
2022-01-17 11:08:10 +00:00
}