From 8bc6fc9fd8e22a58d5c507e86cd4aab4de00292a Mon Sep 17 00:00:00 2001 From: jenli669 Date: Mon, 24 Jun 2019 01:35:23 +0200 Subject: [PATCH] refactor(startup): condense and rename scanProfiles --- src/persistence/profile.cpp | 8 ++------ src/persistence/profile.h | 3 +-- src/widget/loginscreen.cpp | 29 +++++++++++++++-------------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/persistence/profile.cpp b/src/persistence/profile.cpp index 2bb208769..1bc48bda1 100644 --- a/src/persistence/profile.cpp +++ b/src/persistence/profile.cpp @@ -269,21 +269,17 @@ QStringList Profile::getFilesByExt(QString extension) * @brief Scan for profile, automatically importing them if needed. * @warning NOT thread-safe. */ -void Profile::scanProfiles() +const QStringList Profile::getAllProfileNames() { profiles.clear(); QStringList toxfiles = getFilesByExt("tox"), inifiles = getFilesByExt("ini"); - for (QString toxfile : toxfiles) { + for (const QString& toxfile : toxfiles) { if (!inifiles.contains(toxfile)) { Settings::getInstance().createPersonal(toxfile); } profiles.append(toxfile); } -} - -QStringList Profile::getProfiles() -{ return profiles; } diff --git a/src/persistence/profile.h b/src/persistence/profile.h index 6aa718525..4e14c522e 100644 --- a/src/persistence/profile.h +++ b/src/persistence/profile.h @@ -67,8 +67,7 @@ public: bool rename(QString newName); - static void scanProfiles(); - static QStringList getProfiles(); + static const QStringList getAllProfileNames(); static bool exists(QString name); static bool isEncrypted(QString name); diff --git a/src/widget/loginscreen.cpp b/src/widget/loginscreen.cpp index acaf8ed7e..d8fcf4b12 100644 --- a/src/widget/loginscreen.cpp +++ b/src/widget/loginscreen.cpp @@ -59,7 +59,7 @@ LoginScreen::LoginScreen(const QString& initialProfileName, QWidget* parent) connect(ui->autoLoginCB, &QCheckBox::stateChanged, this, &LoginScreen::onAutoLoginToggled); connect(ui->importButton, &QPushButton::clicked, this, &LoginScreen::onImportProfile); - reset(initialProfile); + reset(initialProfileName); this->setStyleSheet(Style::getStylesheet("loginScreen/loginScreen.css")); retranslateUi(); @@ -84,7 +84,7 @@ void LoginScreen::closeEvent(QCloseEvent* event) /** * @brief Resets the UI, clears all fields. */ -void LoginScreen::reset(QString initialProfile) +void LoginScreen::reset(const QString& initialProfileName) { ui->newUsername->clear(); ui->newPass->clear(); @@ -92,25 +92,26 @@ void LoginScreen::reset(QString initialProfile) ui->loginPassword->clear(); ui->loginUsernames->clear(); - Profile::scanProfiles(); - if (initialProfile.isEmpty()) { - initialProfile = Settings::getInstance().getCurrentProfile(); - } - QStringList profiles = Profile::getProfiles(); - for (QString profile : profiles) { - ui->loginUsernames->addItem(profile); - if (profile == initialProfile) { - ui->loginUsernames->setCurrentIndex(ui->loginUsernames->count() - 1); - } - } + QStringList allProfileNames = Profile::getAllProfileNames(); - if (profiles.isEmpty()) { + if (allProfileNames.isEmpty()) { ui->stackedWidget->setCurrentIndex(0); ui->newUsername->setFocus(); } else { + for (const QString& profileName : allProfileNames) { + ui->loginUsernames->addItem(profileName); + } + + ui->loginUsernames->setCurrentText(initialProfileName); ui->stackedWidget->setCurrentIndex(1); ui->loginPassword->setFocus(); } +} + +void LoginScreen::onProfileLoaded() +{ + done(0); +} void LoginScreen::onProfileLoadFailed() { QMessageBox::critical(this, tr("Couldn't load this profile"), tr("Wrong password."));