Implement auto login

This commit is contained in:
Sky 2013-10-21 21:25:50 +01:00
parent 11813a0621
commit 003fedde01
2 changed files with 61 additions and 19 deletions

View File

@ -20,6 +20,7 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "keyring.h"
#include <QMenu>
#include <QMessageBox>
@ -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<void *>();
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<void *>();
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
{

View File

@ -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();