Skip to content

Commit

Permalink
asynchronous login via LoginController
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 10, 2024
1 parent 9bb4c99 commit 4bda56b
Show file tree
Hide file tree
Showing 25 changed files with 313 additions and 158 deletions.
3 changes: 2 additions & 1 deletion source/Gui/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "OverlayMessageController.h"
#include "GenomeEditorWindow.h"
#include "HelpStrings.h"
#include "LoginController.h"

namespace
{
Expand Down Expand Up @@ -468,7 +469,7 @@ void _BrowserWindow::processUserList()
processShortenedText(item->userName, isBoldFont);

ImGui::TableNextColumn();
if (isLoggedIn && _loginDialog.lock()->isShareGpuInfo()) {
if (isLoggedIn && LoginController::get().shareGpuInfo()) {
processShortenedText(getGpuString(item->gpu), isBoldFont);
}

Expand Down
2 changes: 2 additions & 0 deletions source/Gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ PUBLIC
InspectorWindow.h
LastSessionBrowserData.cpp
LastSessionBrowserData.h
LoginController.cpp
LoginController.h
LoginDialog.cpp
LoginDialog.h
LogWindow.cpp
Expand Down
94 changes: 94 additions & 0 deletions source/Gui/LoginController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "LoginController.h"

#include "Base/GlobalSettings.h"
#include "Network/NetworkService.h"
#include "EngineInterface/SimulationController.h"
#include "PersisterInterface/LoginRequestData.h"
#include "PersisterInterface/SenderInfo.h"

namespace
{
auto constexpr LoginSenderId = "Login";
}

LoginController& LoginController::get()
{
static LoginController instance;
return instance;
}

void LoginController::init(SimulationController const& simController, PersisterController const& persisterController)
{
_simController = simController;
_persisterController = persisterController;

auto& settings = GlobalSettings::getInstance();
_remember = settings.getBool("dialogs.login.remember", _remember);
_shareGpuInfo = settings.getBool("dialogs.login.share gpu info", _shareGpuInfo);

if (_remember) {
auto userName = settings.getString("dialogs.login.user name", "");
auto password = settings.getString("dialogs.login.password", "");

if (!userName.empty()) {
persisterController->scheduleLogin(
SenderInfo{.senderId = LoginSenderId, .wishResultData = false, .wishErrorInfo = true},
LoginRequestData{.userName = userName, .password = password, .userInfo = getUserInfo()});
//LoginErrorCode errorCode;
//if (!NetworkService::login(errorCode, _userName, _password, getUserInfo())) {
// if (errorCode != LoginErrorCode_UnknownUser) {
// MessageDialog::getInstance().information("Error", "Login failed.");
// }
//}
}
}
}

void LoginController::shutdown()
{
saveSettings();
}

void LoginController::saveSettings()
{
auto& settings = GlobalSettings::getInstance();
settings.setBool("dialogs.login.remember", _remember);
settings.setBool("dialogs.login.share gpu info", _shareGpuInfo);
if (_remember) {
auto userName = NetworkService::getLoggedInUserName();
auto password = NetworkService::getPassword();
if (userName.has_value() && password.has_value()) {
settings.setString("dialogs.login.user name", *userName);
settings.setString("dialogs.login.password", *password);
}
}
}

bool LoginController::shareGpuInfo() const
{
return _shareGpuInfo;
}

void LoginController::setShareGpuInfo(bool value)
{
_shareGpuInfo = value;
}

bool LoginController::isRemember() const
{
return _remember;
}

void LoginController::setRemember(bool value)
{
_remember = value;
}

UserInfo LoginController::getUserInfo()
{
UserInfo result;
if (_shareGpuInfo) {
result.gpu = _simController->getGpuName();
}
return result;
}
30 changes: 30 additions & 0 deletions source/Gui/LoginController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "Definitions.h"
#include "PersisterInterface/PersisterController.h"

class LoginController
{
public:
static LoginController& get();

void init(SimulationController const& simController, PersisterController const& persisterController);
void shutdown();

void saveSettings();

bool shareGpuInfo() const;
void setShareGpuInfo(bool value);

bool isRemember() const;
void setRemember(bool value);

UserInfo getUserInfo();

private:
SimulationController _simController;
PersisterController _persisterController;

bool _shareGpuInfo = true;
bool _remember = true;
};
67 changes: 17 additions & 50 deletions source/Gui/LoginDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,28 @@
#include "ActivateUserDialog.h"
#include "StyleRepository.h"
#include "HelpStrings.h"
#include "LoginController.h"

_LoginDialog::_LoginDialog(
SimulationController const& simController,
PersisterController const& persisterController,
BrowserWindow const& browserWindow,
CreateUserDialog const& createUserDialog,
ActivateUserDialog const& activateUserDialog,
ResetPasswordDialog const& resetPasswordDialog)
: _AlienDialog("Login")
, _simController(simController)
, _persisterController(persisterController)
, _browserWindow(browserWindow)
, _createUserDialog(createUserDialog)
, _activateUserDialog(activateUserDialog)
, _resetPasswordDialog(resetPasswordDialog)

{
auto& settings = GlobalSettings::getInstance();
_remember = settings.getBool("dialogs.login.remember", _remember);
_shareGpuInfo = settings.getBool("dialogs.login.share gpu info", _shareGpuInfo);

if (_remember) {
_userName = settings.getString("dialogs.login.user name", "");
_password = settings.getString("dialogs.login.password", "");
if (!_userName.empty()) {
LoginErrorCode errorCode;
if (!NetworkService::login(errorCode, _userName, _password, getUserInfo())) {
if (errorCode != LoginErrorCode_UnconfirmedUser) {
MessageDialog::getInstance().information("Error", "Login failed.");
}
}
}
}
}

_LoginDialog::~_LoginDialog()
{
saveSettings();
}

bool _LoginDialog::isShareGpuInfo() const
{
return _shareGpuInfo;
}

void _LoginDialog::processIntern()
Expand All @@ -74,12 +55,18 @@ void _LoginDialog::processIntern()
AlienImGui::InputText(AlienImGui::InputTextParameters().hint("Password").password(true).textWidth(0), _password);
AlienImGui::Separator();
ImGui::Spacing();
AlienImGui::ToggleButton(AlienImGui::ToggleButtonParameters().name("Remember").tooltip(Const::LoginRememberTooltip), _remember);

auto remember = LoginController::get().isRemember();
AlienImGui::ToggleButton(AlienImGui::ToggleButtonParameters().name("Remember").tooltip(Const::LoginRememberTooltip), remember);
LoginController::get().setRemember(remember);

auto shareGpuInfo = LoginController::get().shareGpuInfo();
AlienImGui::ToggleButton(
AlienImGui::ToggleButtonParameters()
.name("Share GPU model info")
.tooltip(Const::LoginShareGpuInfoTooltip1 + _simController->getGpuName() + "\n" + Const::LoginShareGpuInfoTooltip2),
_shareGpuInfo);
shareGpuInfo);
LoginController::get().setShareGpuInfo(shareGpuInfo);

ImGui::Dummy({0, ImGui::GetContentRegionAvail().y - scale(50.0f)});
AlienImGui::Separator();
Expand All @@ -88,7 +75,7 @@ void _LoginDialog::processIntern()
if (AlienImGui::Button("Login")) {
close();
onLogin();
if (!_remember) {
if (!remember) {
_userName.clear();
_password.clear();
}
Expand All @@ -103,15 +90,15 @@ void _LoginDialog::processIntern()
ImGui::BeginDisabled(_userName.empty() || _password.empty());
if (AlienImGui::Button("Create user")) {
close();
_createUserDialog->open(_userName, _password, getUserInfo());
_createUserDialog->open(_userName, _password, LoginController::get().getUserInfo());
}
ImGui::EndDisabled();

ImGui::SameLine();
ImGui::BeginDisabled(_userName.empty());
if (AlienImGui::Button("Reset password")) {
close();
_resetPasswordDialog->open(_userName, getUserInfo());
_resetPasswordDialog->open(_userName, LoginController::get().getUserInfo());
}
ImGui::EndDisabled();

Expand All @@ -128,11 +115,11 @@ void _LoginDialog::onLogin()
{
LoginErrorCode errorCode;

auto userInfo = getUserInfo();
auto userInfo = LoginController::get().getUserInfo();

if (!NetworkService::login(errorCode, _userName, _password, userInfo)) {
switch (errorCode) {
case LoginErrorCode_UnconfirmedUser: {
case LoginErrorCode_UnknownUser: {
_activateUserDialog->open(_userName, _password, userInfo);
} break;
default: {
Expand All @@ -142,25 +129,5 @@ void _LoginDialog::onLogin()
return;
}
_browserWindow->onRefresh();
saveSettings();
}

void _LoginDialog::saveSettings()
{
auto& settings = GlobalSettings::getInstance();
settings.setBool("dialogs.login.remember", _remember);
settings.setBool("dialogs.login.share gpu info", _shareGpuInfo);
if (_remember) {
settings.setString("dialogs.login.user name", _userName);
settings.setString("dialogs.login.password", _password);
}
}

UserInfo _LoginDialog::getUserInfo()
{
UserInfo result;
if (_shareGpuInfo) {
result.gpu = _simController->getGpuName();
}
return result;
LoginController::get().saveSettings();
}
16 changes: 6 additions & 10 deletions source/Gui/LoginDialog.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Network/Definitions.h"
#include "PersisterInterface/PersisterController.h"

#include "AlienDialog.h"
#include "Definitions.h"
Expand All @@ -9,31 +10,26 @@ class _LoginDialog : public _AlienDialog
{
public:
_LoginDialog(
SimulationController const& simController,
SimulationController const& simController,
PersisterController const& persisterController,
BrowserWindow const& browserWindow,
CreateUserDialog const& createUserDialog,
ActivateUserDialog const& activateUserDialog,
ResetPasswordDialog const& resetPasswordDialog);
~_LoginDialog();

bool isShareGpuInfo() const;

private:
void processIntern();

void onLogin();

void saveSettings();
UserInfo getUserInfo(); //can only be called when a simulation is loaded

SimulationController _simController;
SimulationController _simController;
PersisterController _persisterController;
BrowserWindow _browserWindow;
CreateUserDialog _createUserDialog;
ActivateUserDialog _activateUserDialog;
ResetPasswordDialog _resetPasswordDialog;

bool _shareGpuInfo = true;
bool _remember = true;
std::string _userName;
std::string _password;
};
};
Loading

0 comments on commit 4bda56b

Please sign in to comment.