From 003fedde018c399a148b1e5caae34350d720024e Mon Sep 17 00:00:00 2001 From: Sky Date: Mon, 21 Oct 2013 21:25:50 +0100 Subject: Implement auto login --- gui/mainwindow.cpp | 78 +++++++++++++++++++++++++++++++++++++++++------------- gui/mainwindow.h | 2 ++ 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index ab718f98..d602efcf 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -20,6 +20,7 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#include "keyring.h" #include #include @@ -235,15 +236,6 @@ void MainWindow::setCatBackground(bool enabled) } } -void MainWindow::instanceActivated(QModelIndex index) -{ - if (!index.isValid()) - return; - BaseInstance *inst = - (BaseInstance *)index.data(InstanceList::InstancePointerRole).value(); - doLogin(); -} - void MainWindow::on_actionAddInstance_triggered() { if (!MMC->minecraftlist()->isLoaded() && m_versionLoadTask && @@ -453,14 +445,70 @@ void MainWindow::on_instanceView_customContextMenuRequested(const QPoint &pos) instContextMenu->exec(view->mapToGlobal(pos)); } */ +void MainWindow::instanceActivated(QModelIndex index) +{ + if (!index.isValid()) + return; + BaseInstance *inst = + (BaseInstance *)index.data(InstanceList::InstancePointerRole).value(); + + bool autoLogin = MMC->settings()->get("AutoLogin").toBool(); + if(autoLogin) doAutoLogin(); + else doLogin(); +} + void MainWindow::on_actionLaunchInstance_triggered() { if (m_selectedInstance) { - doLogin(); + bool autoLogin = MMC->settings()->get("AutoLogin").toBool(); + if(autoLogin) doAutoLogin(); + else doLogin(); } } +void MainWindow::doAutoLogin() +{ + Keyring * k = Keyring::instance(); + QStringList accounts = k->getStoredAccounts("minecraft"); + + if(!accounts.isEmpty()) + { + QString username = accounts[0]; + QString password = k->getPassword("minecraft", username); + + if(!password.isEmpty()) + { + QLOG_INFO() << "Automatically logging in with stored account: " << username; + doLogin(username, password); + } + else + { + QLOG_ERROR() << "Auto login set for account, but no password was found: " << username; + doLogin(tr("Auto login attempted, but no password is stored.")); + } + } + else + { + QLOG_ERROR() << "Auto login set but no accounts were stored."; + doLogin(tr("Auto login attempted, but no accounts are stored.")); + } +} + +void MainWindow::doLogin(QString username, QString password) +{ + UserInfo uInfo{username, password}; + + ProgressDialog *tDialog = new ProgressDialog(this); + LoginTask *loginTask = new LoginTask(uInfo, tDialog); + connect(loginTask, SIGNAL(succeeded()), SLOT(onLoginComplete()), + Qt::QueuedConnection); + connect(loginTask, SIGNAL(failed(QString)), SLOT(doLogin(QString)), + Qt::QueuedConnection); + + tDialog->exec(loginTask); +} + void MainWindow::doLogin(const QString &errorMsg) { if (!m_selectedInstance) @@ -475,16 +523,8 @@ void MainWindow::doLogin(const QString &errorMsg) { if (loginDlg->isOnline()) { - UserInfo uInfo{loginDlg->getUsername(), loginDlg->getPassword()}; - - ProgressDialog *tDialog = new ProgressDialog(this); - LoginTask *loginTask = new LoginTask(uInfo, tDialog); - connect(loginTask, SIGNAL(succeeded()), SLOT(onLoginComplete()), - Qt::QueuedConnection); - connect(loginTask, SIGNAL(failed(QString)), SLOT(doLogin(QString)), - Qt::QueuedConnection); m_activeInst = m_selectedInstance; - tDialog->exec(loginTask); + doLogin(loginDlg->getUsername(), loginDlg->getPassword()); } else { diff --git a/gui/mainwindow.h b/gui/mainwindow.h index dbf7c4c3..4490d474 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -99,6 +99,8 @@ private slots: void on_actionEditInstNotes_triggered(); void doLogin(const QString &errorMsg = ""); + void doLogin(QString username, QString password); + void doAutoLogin(); void onLoginComplete(); -- cgit v1.2.3