From f3a9dde52e99385eba01c9356ed8ef9548833e34 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 28 Nov 2013 21:40:40 -0600 Subject: [PATCH] Rework the password dialog It's now used as a general purpose "account edit dialog". It'll be used for entering usernames, passwords, or both. --- CMakeLists.txt | 6 +- gui/MainWindow.cpp | 5 +- gui/dialogs/AccountListDialog.cpp | 91 ++++++++----------- gui/dialogs/AccountListDialog.h | 12 +-- gui/dialogs/AccountListDialog.ui | 7 -- ...sswordDialog.cpp => EditAccountDialog.cpp} | 24 +++-- .../{PasswordDialog.h => EditAccountDialog.h} | 28 ++++-- ...PasswordDialog.ui => EditAccountDialog.ui} | 24 +++-- 8 files changed, 101 insertions(+), 96 deletions(-) rename gui/dialogs/{PasswordDialog.cpp => EditAccountDialog.cpp} (57%) rename gui/dialogs/{PasswordDialog.h => EditAccountDialog.h} (59%) rename gui/dialogs/{PasswordDialog.ui => EditAccountDialog.ui} (73%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 720f3f1f..a8038fff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -217,8 +217,8 @@ gui/dialogs/EditNotesDialog.h gui/dialogs/EditNotesDialog.cpp gui/dialogs/CustomMessageBox.h gui/dialogs/CustomMessageBox.cpp -gui/dialogs/PasswordDialog.h -gui/dialogs/PasswordDialog.cpp +gui/dialogs/EditAccountDialog.h +gui/dialogs/EditAccountDialog.cpp gui/dialogs/AccountListDialog.h gui/dialogs/AccountListDialog.cpp gui/dialogs/AccountSelectDialog.h @@ -370,7 +370,6 @@ gui/dialogs/SettingsDialog.ui gui/dialogs/CopyInstanceDialog.ui gui/dialogs/NewInstanceDialog.ui gui/dialogs/LoginDialog.ui -gui/dialogs/PasswordDialog.ui gui/dialogs/AboutDialog.ui gui/dialogs/VersionSelectDialog.ui gui/dialogs/LwjglSelectDialog.ui @@ -382,6 +381,7 @@ gui/dialogs/OneSixModEditDialog.ui gui/dialogs/EditNotesDialog.ui gui/dialogs/AccountListDialog.ui gui/dialogs/AccountSelectDialog.ui +gui/dialogs/EditAccountDialog.ui # Widgets/other gui/widgets/MCModInfoFrame.ui diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 72e754ee..65d9f4cc 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -60,7 +60,7 @@ #include "gui/dialogs/CopyInstanceDialog.h" #include "gui/dialogs/AccountListDialog.h" #include "gui/dialogs/AccountSelectDialog.h" -#include "gui/dialogs/PasswordDialog.h" +#include "gui/dialogs/EditAccountDialog.h" #include "gui/ConsoleWindow.h" @@ -599,7 +599,6 @@ void MainWindow::on_actionSettings_triggered() void MainWindow::on_actionManageAccounts_triggered() { AccountListDialog dialog(this); - connect(&dialog, SIGNAL(activeAccountChanged()), SLOT(activeAccountChanged())); dialog.exec(); } @@ -812,7 +811,7 @@ void MainWindow::doLaunchInst(BaseInstance* instance, MojangAccountPtr account) bool MainWindow::doRefreshToken(MojangAccountPtr account, const QString& errorMsg) { - PasswordDialog passDialog(errorMsg, this); + EditAccountDialog passDialog(errorMsg, this, EditAccountDialog::PasswordField); if (passDialog.exec() == QDialog::Accepted) { // To refresh the token, we just create an authenticate task with the given account and the user's password. diff --git a/gui/dialogs/AccountListDialog.cpp b/gui/dialogs/AccountListDialog.cpp index 29f3838d..dfac4989 100644 --- a/gui/dialogs/AccountListDialog.cpp +++ b/gui/dialogs/AccountListDialog.cpp @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include @@ -40,10 +40,12 @@ AccountListDialog::AccountListDialog(QWidget *parent) : ui->listView->setModel(m_accounts.get()); QItemSelectionModel* selectionModel = ui->listView->selectionModel(); + connect(selectionModel, &QItemSelectionModel::selectionChanged, [this] (const QItemSelection& sel, const QItemSelection& dsel) { updateButtonStates(); }); - connect(m_accounts.get(), &MojangAccountList::listChanged, - [this] () { updateButtonStates(); }); + + connect(m_accounts.get(), SIGNAL(listChanged), SLOT(listChanged)); + connect(m_accounts.get(), SIGNAL(activeAccountChanged), SLOT(listChanged)); updateButtonStates(); } @@ -53,10 +55,15 @@ AccountListDialog::~AccountListDialog() delete ui; } +void AccountListDialog::listChanged() +{ + updateButtonStates(); +} + void AccountListDialog::on_addAccountBtn_clicked() { - doLogin("Please log in to add your account."); + addAccount(tr("Please enter your Mojang or Minecraft account username and password to add your account.")); } void AccountListDialog::on_rmAccountBtn_clicked() @@ -66,16 +73,9 @@ void AccountListDialog::on_rmAccountBtn_clicked() { QModelIndex selected = selection.first(); m_accounts->removeAccount(selected); - - emit activeAccountChanged(); } } -void AccountListDialog::on_editAccountBtn_clicked() -{ - // TODO -} - void AccountListDialog::on_setDefaultBtn_clicked() { QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes(); @@ -84,16 +84,12 @@ void AccountListDialog::on_setDefaultBtn_clicked() QModelIndex selected = selection.first(); MojangAccountPtr account = selected.data(MojangAccountList::PointerRole).value(); m_accounts->setActiveAccount(account->username()); - - emit activeAccountChanged(); } } void AccountListDialog::on_noDefaultBtn_clicked() { m_accounts->setActiveAccount(""); - - emit activeAccountChanged(); } void AccountListDialog::on_closeBtnBox_rejected() @@ -107,58 +103,47 @@ void AccountListDialog::updateButtonStates() QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes(); ui->rmAccountBtn->setEnabled(selection.size() > 0); - ui->editAccountBtn->setEnabled(selection.size() > 0); ui->setDefaultBtn->setEnabled(selection.size() > 0); ui->noDefaultBtn->setDown(m_accounts->activeAccount().get() == nullptr); } -void AccountListDialog::doLogin(const QString& errMsg) +void AccountListDialog::addAccount(const QString& errMsg) { // TODO: We can use the login dialog for this for now, but we'll have to make something better for it eventually. - LoginDialog loginDialog(this); + EditAccountDialog loginDialog(errMsg, this, EditAccountDialog::UsernameField | EditAccountDialog::PasswordField); loginDialog.exec(); if (loginDialog.result() == QDialog::Accepted) { - QString username(loginDialog.getUsername()); - QString password(loginDialog.getPassword()); + QString username(loginDialog.username()); + QString password(loginDialog.password()); MojangAccountPtr account = MojangAccountPtr(new MojangAccount(username)); - ProgressDialog* progDialog = new ProgressDialog(this); - m_authTask = new AuthenticateTask(account, password, progDialog); - connect(m_authTask, SIGNAL(succeeded()), SLOT(onLoginComplete()), Qt::QueuedConnection); - connect(m_authTask, SIGNAL(failed(QString)), SLOT(doLogin(QString)), Qt::QueuedConnection); - progDialog->exec(m_authTask); - //delete m_authTask; + ProgressDialog progDialog(this); + AuthenticateTask authTask(account, password, &progDialog); + if (progDialog.exec(&authTask)) + { + // Add the authenticated account to the accounts list. + MojangAccountPtr account = authTask.getMojangAccount(); + m_accounts->addAccount(account); + + // Grab associated player skins + auto job = new NetJob("Player skins: " + account->username()); + + for(AccountProfile profile : account->profiles()) + { + auto meta = MMC->metacache()->resolveEntry("skins", profile.name() + ".png"); + auto action = CacheDownload::make( + QUrl("http://skins.minecraft.net/MinecraftSkins/" + profile.name() + ".png"), + meta); + job->addNetAction(action); + meta->stale = true; + } + + job->start(); + } } } -void AccountListDialog::onLoginComplete() -{ - // Add the authenticated account to the accounts list. - MojangAccountPtr account = m_authTask->getMojangAccount(); - m_accounts->addAccount(account); - - emit activeAccountChanged(); - - //ui->listView->update(); - - // Grab associated player skins - auto job = new NetJob("Player skins: " + account->username()); - - for(AccountProfile profile : account->profiles()) - { - auto meta = MMC->metacache()->resolveEntry("skins", profile.name() + ".png"); - auto action = CacheDownload::make( - QUrl("http://skins.minecraft.net/MinecraftSkins/" + profile.name() + ".png"), - meta); - job->addNetAction(action); - meta->stale = true; - } - - connect(job, SIGNAL(succeeded()), SIGNAL(activeAccountChanged())); - job->start(); -} - diff --git a/gui/dialogs/AccountListDialog.h b/gui/dialogs/AccountListDialog.h index 63720f58..634d944a 100644 --- a/gui/dialogs/AccountListDialog.h +++ b/gui/dialogs/AccountListDialog.h @@ -40,8 +40,6 @@ slots: void on_rmAccountBtn_clicked(); - void on_editAccountBtn_clicked(); - void on_setDefaultBtn_clicked(); void on_noDefaultBtn_clicked(); @@ -49,21 +47,17 @@ slots: // This will be sent when the "close" button is clicked. void on_closeBtnBox_rejected(); + void listChanged(); + //! Updates the states of the dialog's buttons. void updateButtonStates(); -signals: - void activeAccountChanged(); - protected: std::shared_ptr m_accounts; - AuthenticateTask* m_authTask; - protected slots: - void doLogin(const QString& errMsg=""); - void onLoginComplete(); + void addAccount(const QString& errMsg=""); private: Ui::AccountListDialog *ui; diff --git a/gui/dialogs/AccountListDialog.ui b/gui/dialogs/AccountListDialog.ui index b4baf3f9..571f9be7 100644 --- a/gui/dialogs/AccountListDialog.ui +++ b/gui/dialogs/AccountListDialog.ui @@ -38,13 +38,6 @@ - - - - &Edit - - - diff --git a/gui/dialogs/PasswordDialog.cpp b/gui/dialogs/EditAccountDialog.cpp similarity index 57% rename from gui/dialogs/PasswordDialog.cpp rename to gui/dialogs/EditAccountDialog.cpp index c67fc6a2..d28336f7 100644 --- a/gui/dialogs/PasswordDialog.cpp +++ b/gui/dialogs/EditAccountDialog.cpp @@ -13,25 +13,33 @@ * limitations under the License. */ -#include "PasswordDialog.h" -#include "ui_PasswordDialog.h" +#include "EditAccountDialog.h" +#include "ui_EditAccountDialog.h" -PasswordDialog::PasswordDialog(const QString& errorMsg, QWidget *parent) : +EditAccountDialog::EditAccountDialog(const QString& text, QWidget *parent, int flags) : QDialog(parent), - ui(new Ui::PasswordDialog) + ui(new Ui::EditAccountDialog) { ui->setupUi(this); - ui->errorLabel->setText(errorMsg); - ui->errorLabel->setVisible(!errorMsg.isEmpty()); + ui->label->setText(text); + ui->label->setVisible(!text.isEmpty()); + + ui->userTextBox->setVisible(flags & UsernameField); + ui->passTextBox->setVisible(flags & PasswordField); } -PasswordDialog::~PasswordDialog() +EditAccountDialog::~EditAccountDialog() { delete ui; } -QString PasswordDialog::password() const +QString EditAccountDialog::username() const +{ + return ui->userTextBox->text(); +} + +QString EditAccountDialog::password() const { return ui->passTextBox->text(); } diff --git a/gui/dialogs/PasswordDialog.h b/gui/dialogs/EditAccountDialog.h similarity index 59% rename from gui/dialogs/PasswordDialog.h rename to gui/dialogs/EditAccountDialog.h index 0919e6e4..847c3be5 100644 --- a/gui/dialogs/PasswordDialog.h +++ b/gui/dialogs/EditAccountDialog.h @@ -18,23 +18,39 @@ #include namespace Ui { -class PasswordDialog; +class EditAccountDialog; } -class PasswordDialog : public QDialog +class EditAccountDialog : public QDialog { - Q_OBJECT +Q_OBJECT public: - explicit PasswordDialog(const QString& errorMsg="", QWidget *parent = 0); - ~PasswordDialog(); + explicit EditAccountDialog(const QString& text="", QWidget *parent = 0, int flags=UsernameField | PasswordField); + ~EditAccountDialog(); + + /*! + * Gets the text entered in the dialog's username field. + */ + QString username() const; /*! * Gets the text entered in the dialog's password field. */ QString password() const; + enum Flags + { + NoFlags=0, + + //! Specifies that the dialog should have a username field. + UsernameField, + + //! Specifies that the dialog should have a password field. + PasswordField, + }; + private: - Ui::PasswordDialog *ui; + Ui::EditAccountDialog *ui; }; diff --git a/gui/dialogs/PasswordDialog.ui b/gui/dialogs/EditAccountDialog.ui similarity index 73% rename from gui/dialogs/PasswordDialog.ui rename to gui/dialogs/EditAccountDialog.ui index 6c70b033..15d371ee 100644 --- a/gui/dialogs/PasswordDialog.ui +++ b/gui/dialogs/EditAccountDialog.ui @@ -1,13 +1,13 @@ - PasswordDialog - + EditAccountDialog + 0 0 400 - 94 + 128 @@ -15,9 +15,16 @@ - + - Error message here... + Message label placeholder. + + + + + + + Email / Username @@ -26,6 +33,9 @@ QLineEdit::Password + + Password + @@ -45,7 +55,7 @@ buttonBox accepted() - PasswordDialog + EditAccountDialog accept() @@ -61,7 +71,7 @@ buttonBox rejected() - PasswordDialog + EditAccountDialog reject()