From 221fda6e95d4366e8f47e0eb21cc55afd0660e34 Mon Sep 17 00:00:00 2001 From: redstrate Date: Tue, 9 Nov 2021 12:25:54 -0500 Subject: [PATCH] Properly reload controls on LauncherWindow on profile change --- src/xivlauncher.cpp | 99 +++++++++++++++++++++++---------------------- src/xivlauncher.h | 16 ++++++-- 2 files changed, 64 insertions(+), 51 deletions(-) diff --git a/src/xivlauncher.cpp b/src/xivlauncher.cpp index 72f125f..7f0fe23 100755 --- a/src/xivlauncher.cpp +++ b/src/xivlauncher.cpp @@ -116,6 +116,8 @@ QString LauncherWindow::readVersion(QString path) { } void LauncherWindow::readInitialInformation() { + defaultProfileIndex = settings.value("defaultProfile", 0).toInt(); + auto profiles = settings.childGroups(); // create the Default profile if it doesnt exist @@ -252,60 +254,32 @@ LauncherWindow::LauncherWindow(QWidget* parent) : auto layout = new QFormLayout(); - auto profileSelect = new QComboBox(); - profileSelect->addItem("Default"); + profileSelect = new QComboBox(); + layout->addRow("Profile", profileSelect); - auto usernameEdit = new QLineEdit(); + usernameEdit = new QLineEdit(); layout->addRow("Username", usernameEdit); - if(currentProfile().rememberUsername) { - auto job = new QKeychain::ReadPasswordJob("LauncherWindow"); - job->setKey("username"); - job->start(); - - connect(job, &QKeychain::ReadPasswordJob::finished, [=](QKeychain::Job* j) { - usernameEdit->setText(job->textData()); - }); - } - - auto rememberUsernameBox = new QCheckBox(); - rememberUsernameBox->setChecked(currentProfile().rememberUsername); + rememberUsernameBox = new QCheckBox(); layout->addRow("Remember Username?", rememberUsernameBox); - auto passwordEdit = new QLineEdit(); + passwordEdit = new QLineEdit(); passwordEdit->setEchoMode(QLineEdit::EchoMode::Password); layout->addRow("Password", passwordEdit); - if(currentProfile().rememberPassword) { - auto job = new QKeychain::ReadPasswordJob("LauncherWindow"); - job->setKey("password"); - job->start(); - - connect(job, &QKeychain::ReadPasswordJob::finished, [=](QKeychain::Job* j) { - passwordEdit->setText(job->textData()); - }); - } - - auto rememberPasswordBox = new QCheckBox(); - rememberPasswordBox->setChecked(currentProfile().rememberPassword); + rememberPasswordBox = new QCheckBox(); layout->addRow("Remember Password?", rememberPasswordBox); - auto otpEdit = new QLineEdit(); + otpEdit = new QLineEdit(); layout->addRow("One-Time Password", otpEdit); auto loginButton = new QPushButton("Login"); layout->addRow(loginButton); - auto registerButton = new QPushButton("Register"); + registerButton = new QPushButton("Register"); layout->addRow(registerButton); - const auto refreshControls = [=]() { - registerButton->setEnabled(currentProfile().isSapphire); - otpEdit->setEnabled(!currentProfile().isSapphire); - }; - refreshControls(); - auto emptyWidget = new QWidget(); emptyWidget->setLayout(layout); setCentralWidget(emptyWidget); @@ -340,16 +314,18 @@ LauncherWindow::LauncherWindow(QWidget* parent) : sapphireLauncher->registerAccount(currentProfile().lobbyURL, info); } }); + + reloadControls(); } LauncherWindow::~LauncherWindow() = default; ProfileSettings LauncherWindow::currentProfile() const { - return getProfile(currentProfileIndex); + return getProfile(profileSelect->currentIndex()); } ProfileSettings& LauncherWindow::currentProfile() { - return getProfile(currentProfileIndex); + return getProfile(profileSelect->currentIndex()); } ProfileSettings LauncherWindow::getProfile(int index) const { @@ -360,16 +336,6 @@ ProfileSettings& LauncherWindow::getProfile(int index) { return profileSettings[index]; } -void LauncherWindow::setProfile(QString name) { - currentProfileIndex = getProfileIndex(name); - settingsChanged(); -} - -void LauncherWindow::setProfile(int index) { - currentProfileIndex = index; - settingsChanged(); -} - int LauncherWindow::getProfileIndex(QString name) { for(int i = 0; i < profileSettings.size(); i++) { if(profileSettings[i].name == name) @@ -407,4 +373,41 @@ void LauncherWindow::saveSettings() { settings.endGroup(); } +} + +void LauncherWindow::reloadControls() { + profileSelect->clear(); + + for(const auto& profile : profileList()) { + profileSelect->addItem(profile); + } + + if(profileSelect->currentIndex() == -1) { + profileSelect->setCurrentIndex(defaultProfileIndex); + } + + rememberUsernameBox->setChecked(currentProfile().rememberUsername); + if(currentProfile().rememberUsername) { + auto job = new QKeychain::ReadPasswordJob("LauncherWindow"); + job->setKey("username"); + job->start(); + + connect(job, &QKeychain::ReadPasswordJob::finished, [=](QKeychain::Job* j) { + usernameEdit->setText(job->textData()); + }); + } + + rememberPasswordBox->setChecked(currentProfile().rememberPassword); + if(currentProfile().rememberPassword) { + auto job = new QKeychain::ReadPasswordJob("LauncherWindow"); + job->setKey("password"); + job->start(); + + connect(job, &QKeychain::ReadPasswordJob::finished, [=](QKeychain::Job* j) { + passwordEdit->setText(job->textData()); + }); + } + + registerButton->setEnabled(currentProfile().isSapphire); + otpEdit->setEnabled(!currentProfile().isSapphire); } \ No newline at end of file diff --git a/src/xivlauncher.h b/src/xivlauncher.h index ac5dcde..31521a5 100755 --- a/src/xivlauncher.h +++ b/src/xivlauncher.h @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include class SapphireLauncher; class SquareLauncher; @@ -52,8 +55,6 @@ public: ProfileSettings getProfile(int index) const; ProfileSettings& getProfile(int index); - void setProfile(QString name); - void setProfile(int index); int getProfileIndex(QString name); QList profileList() const; int addProfile(); @@ -68,6 +69,9 @@ public: QSettings settings; +public slots: + void reloadControls(); + signals: void settingsChanged(); @@ -76,6 +80,12 @@ private: SquareBoot* squareBoot; SquareLauncher* squareLauncher; + QComboBox* profileSelect; + QLineEdit* usernameEdit, *passwordEdit; + QLineEdit* otpEdit; + QCheckBox* rememberUsernameBox, *rememberPasswordBox; + QPushButton* registerButton; + QList profileSettings; - int currentProfileIndex = 0; + int defaultProfileIndex = 0; };