2021-08-17 09:20:21 +12:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from logging import getLogger
|
|
|
|
|
|
|
|
from PyQt5.QtCore import Qt
|
2021-10-21 05:04:29 +13:00
|
|
|
from PyQt5.QtWidgets import QSizePolicy, QLayout, QDialog, QMessageBox
|
2021-02-18 01:58:29 +13:00
|
|
|
|
2021-09-07 07:10:18 +12:00
|
|
|
from legendary.core import LegendaryCore
|
2021-04-08 08:39:23 +12:00
|
|
|
from rare.components.dialogs.login.browser_login import BrowserLogin
|
2021-05-29 06:34:34 +12:00
|
|
|
from rare.components.dialogs.login.import_login import ImportLogin
|
2021-08-17 09:20:21 +12:00
|
|
|
from rare.ui.components.dialogs.login.login_dialog import Ui_LoginDialog
|
2021-02-18 01:58:29 +13:00
|
|
|
|
2021-08-17 09:20:21 +12:00
|
|
|
logger = getLogger("Login")
|
2021-02-18 01:58:29 +13:00
|
|
|
|
2021-02-18 22:22:15 +13:00
|
|
|
|
2021-08-17 09:20:21 +12:00
|
|
|
@dataclass
|
|
|
|
class LoginPages:
|
|
|
|
landing: int
|
|
|
|
browser: int
|
|
|
|
import_egl: int
|
2021-02-18 01:58:29 +13:00
|
|
|
|
|
|
|
|
2021-08-17 09:20:21 +12:00
|
|
|
class LoginDialog(QDialog, Ui_LoginDialog):
|
|
|
|
logged_in: bool = False
|
2021-10-08 08:58:50 +13:00
|
|
|
pages = LoginPages(landing=0, browser=1, import_egl=2)
|
2021-02-18 01:58:29 +13:00
|
|
|
|
2021-08-17 09:20:21 +12:00
|
|
|
def __init__(self, core: LegendaryCore, parent=None):
|
|
|
|
super(LoginDialog, self).__init__(parent=parent)
|
|
|
|
self.setupUi(self)
|
|
|
|
self.setAttribute(Qt.WA_DeleteOnClose, True)
|
|
|
|
self.setWindowFlags(Qt.Dialog | Qt.CustomizeWindowHint | Qt.WindowTitleHint)
|
2021-02-18 01:58:29 +13:00
|
|
|
|
2021-08-17 09:20:21 +12:00
|
|
|
self.core = core
|
2021-02-18 01:58:29 +13:00
|
|
|
|
2021-08-17 09:20:21 +12:00
|
|
|
self.browser_page = BrowserLogin(self.core, self.login_stack)
|
|
|
|
self.login_stack.insertWidget(self.pages.browser, self.browser_page)
|
|
|
|
self.browser_page.success.connect(self.login_successful)
|
|
|
|
self.browser_page.changed.connect(
|
2021-10-10 01:08:54 +13:00
|
|
|
lambda: self.next_button.setEnabled(self.browser_page.is_valid())
|
2021-08-17 09:20:21 +12:00
|
|
|
)
|
|
|
|
self.import_page = ImportLogin(self.core, self.login_stack)
|
|
|
|
self.login_stack.insertWidget(self.pages.import_egl, self.import_page)
|
|
|
|
self.import_page.success.connect(self.login_successful)
|
|
|
|
self.import_page.changed.connect(
|
|
|
|
lambda: self.next_button.setEnabled(self.import_page.is_valid())
|
|
|
|
)
|
|
|
|
|
|
|
|
self.next_button.setEnabled(False)
|
|
|
|
self.back_button.setEnabled(False)
|
|
|
|
|
|
|
|
self.login_browser_radio.clicked.connect(lambda: self.next_button.setEnabled(True))
|
|
|
|
self.login_import_radio.clicked.connect(lambda: self.next_button.setEnabled(True))
|
|
|
|
self.exit_button.clicked.connect(self.close)
|
|
|
|
self.back_button.clicked.connect(self.back_clicked)
|
|
|
|
self.next_button.clicked.connect(self.next_clicked)
|
|
|
|
|
|
|
|
self.login_stack.setCurrentIndex(self.pages.landing)
|
|
|
|
|
2021-10-21 05:04:29 +13:00
|
|
|
self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
|
2021-10-21 06:19:03 +13:00
|
|
|
self.dialog_layout.setSizeConstraint(QLayout.SetFixedSize)
|
2021-10-21 05:04:29 +13:00
|
|
|
# self.resize(self.minimumSizeHint())
|
2021-08-17 09:20:21 +12:00
|
|
|
|
|
|
|
def back_clicked(self):
|
|
|
|
self.back_button.setEnabled(False)
|
|
|
|
self.next_button.setEnabled(True)
|
|
|
|
self.login_stack.setCurrentIndex(self.pages.landing)
|
|
|
|
|
|
|
|
def next_clicked(self):
|
|
|
|
if self.login_stack.currentIndex() == self.pages.landing:
|
|
|
|
if self.login_browser_radio.isChecked():
|
|
|
|
self.login_stack.setCurrentIndex(self.pages.browser)
|
|
|
|
self.next_button.setEnabled(False)
|
|
|
|
if self.login_import_radio.isChecked():
|
|
|
|
self.login_stack.setCurrentIndex(self.pages.import_egl)
|
|
|
|
self.next_button.setEnabled(self.import_page.is_valid())
|
|
|
|
self.back_button.setEnabled(True)
|
|
|
|
elif self.login_stack.currentIndex() == self.pages.browser:
|
|
|
|
self.browser_page.do_login()
|
|
|
|
elif self.login_stack.currentIndex() == self.pages.import_egl:
|
|
|
|
self.import_page.do_login()
|
2021-02-18 01:58:29 +13:00
|
|
|
|
|
|
|
def login(self):
|
|
|
|
self.exec_()
|
|
|
|
return self.logged_in
|
|
|
|
|
2021-08-17 09:20:21 +12:00
|
|
|
def login_successful(self):
|
|
|
|
try:
|
|
|
|
if self.core.login():
|
|
|
|
self.logged_in = True
|
2021-10-08 08:58:50 +13:00
|
|
|
self.close()
|
2021-08-17 09:20:21 +12:00
|
|
|
else:
|
|
|
|
raise ValueError("Login failed.")
|
|
|
|
except ValueError as e:
|
|
|
|
logger.error(str(e))
|
|
|
|
self.next_button.setEnabled(False)
|
|
|
|
self.logged_in = False
|
2021-10-10 01:08:54 +13:00
|
|
|
QMessageBox.warning(self, "Error", str(e))
|
|
|
|
|